< prev index next >

src/java.base/share/classes/jdk/internal/nicl/LdLoader.java

Print this page




  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.internal.nicl;
  24 
  25 import java.io.File;
  26 import java.lang.reflect.Field;
  27 import java.nicl.Library;
  28 import java.nicl.NativeScope;
  29 import java.nicl.Scope;
  30 import java.nicl.types.Pointer;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import static sun.security.action.GetPropertyAction.privilegedGetProperty;
  34 import static jdk.internal.nicl.UnixDynamicLibraries.*;
  35 
  36 public class LdLoader extends LibraryLoader {
  37     private static final boolean DEBUG = Boolean.parseBoolean(
  38         privilegedGetProperty("jdk.internal.nicl.LdLoader.DEBUG"));
  39 

  40     private final String[] usr_paths;
  41 
  42     // preloaded/built-in library
  43     private final UnixLibrary defaultLibrary;
  44 
  45     private String[] getUserClassPath() {



  46         return AccessController.doPrivileged((PrivilegedAction<String[]>)() -> {
  47             try {
  48                 Field f = ClassLoader.class.getDeclaredField("usr_paths");
  49                 f.setAccessible(true);
  50                 return (String[])f.get(null);
  51             } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
  52                 throw new RuntimeException(e);
  53             }
  54         });
  55     }
  56 
  57     public LdLoader() {
  58         this.usr_paths = getUserClassPath();
  59         this.defaultLibrary = new UnixLibrary(null);
  60     }
  61 
  62     @Override
  63     public Library getDefaultLibrary() {
  64         return defaultLibrary;
  65     }
  66 
  67     private Library tryLoadLibrary(String libPath) {
  68         if (DEBUG) {
  69             System.err.println("Trying " + libPath);
  70         }
  71 
  72         try (Scope scope = new NativeScope()) {
  73             Pointer<Byte> cname = scope.toCString(libPath);
  74 
  75             try {
  76                 Pointer<Void> handle = UnixDynamicLibraries.getInstance().dlopen(cname, RTLD_LAZY);
  77                 if (handle != null) {
  78                     return new UnixLibrary(handle);
  79                 }
  80 
  81                 if (DEBUG) {
  82                     Pointer<Byte> err = UnixDynamicLibraries.getInstance().dlerror();
  83                     if (err != null) {
  84                         System.err.println(Pointer.toString(err));
  85                     }
  86                 }
  87 
  88                 throw new UnsatisfiedLinkError("Can't load library: " + libPath);
  89             } catch (Throwable t) {
  90                 throw new RuntimeException(t);


  91             }
  92         }
  93     }
  94 
  95     @Override
  96     public Library load(String name, boolean isAbsolute) {
  97         if (DEBUG) {
  98             System.err.println("Library.load(U, " + name + ", " + isAbsolute + ")");
  99         }
 100 
 101         if (isAbsolute) {
 102             return tryLoadLibrary(name);
 103         }
 104 
 105         for (String usr_path : usr_paths) {
 106             String libPath = usr_path + File.separator + name;
 107             try {
 108                 return tryLoadLibrary(libPath);
 109             } catch (UnsatisfiedLinkError e) {



 110                 // ignore and try next path
 111             }
 112         }
 113 
 114         throw new UnsatisfiedLinkError(name);
 115     }
 116 }


  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.internal.nicl;
  24 
  25 import java.io.File;
  26 import java.lang.reflect.Field;
  27 import java.nicl.Library;
  28 import java.nicl.NativeScope;
  29 import java.nicl.Scope;
  30 import java.nicl.types.Pointer;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import static sun.security.action.GetPropertyAction.privilegedGetProperty;
  34 import static jdk.internal.nicl.UnixDynamicLibraries.*;
  35 
  36 public class LdLoader extends LibraryLoader {
  37     private static final boolean DEBUG = Boolean.parseBoolean(
  38         privilegedGetProperty("jdk.internal.nicl.LdLoader.DEBUG"));
  39 
  40     // copy of ClassLoader.usr_paths field
  41     private final String[] usr_paths;
  42 
  43     // preloaded/built-in library
  44     private final UnixLibrary defaultLibrary;
  45 
  46     private String[] getUserLibraryPaths() {
  47         // ClassLoader.usr_paths is initialized from "java.library.path" System property.
  48         // FIXME: we should define and use a private API rather than reading a
  49         // private static non-final lazily initialized field from ClassLoader.
  50         return AccessController.doPrivileged((PrivilegedAction<String[]>)() -> {
  51             try {
  52                 Field f = ClassLoader.class.getDeclaredField("usr_paths");
  53                 f.setAccessible(true);
  54                 return (String[])f.get(null);
  55             } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
  56                 throw new RuntimeException(e);
  57             }
  58         });
  59     }
  60 
  61     public LdLoader() {
  62         this.usr_paths = getUserLibraryPaths();
  63         this.defaultLibrary = new UnixLibrary(null);
  64     }
  65 
  66     @Override
  67     public Library getDefaultLibrary() {
  68         return defaultLibrary;
  69     }
  70 
  71     private Library tryLoadLibrary(String libPath) {
  72         if (DEBUG) {
  73             System.err.println("Trying " + libPath);
  74         }
  75 
  76         try (Scope scope = new NativeScope()) {
  77             Pointer<Byte> cname = scope.toCString(libPath);
  78 
  79             try {
  80                 Pointer<Void> handle = UnixDynamicLibraries.getInstance().dlopen(cname, RTLD_LAZY);
  81                 if (handle != null) {
  82                     return new UnixLibrary(handle);
  83                 }
  84 
  85                 if (DEBUG) {
  86                     Pointer<Byte> err = UnixDynamicLibraries.getInstance().dlerror();
  87                     if (err != null) {
  88                         System.err.println(Pointer.toString(err));
  89                     }
  90                 }
  91 
  92                 throw new UnsatisfiedLinkError("Can't load library: " + libPath);
  93             } catch (RuntimeException re) {
  94                 throw re;
  95             } catch (Exception e) {
  96                 throw new RuntimeException(e);
  97             }
  98         }
  99     }
 100 
 101     @Override
 102     public Library load(String name, boolean isAbsolute) {
 103         if (DEBUG) {
 104             System.err.println("Library.load(U, " + name + ", " + isAbsolute + ")");
 105         }
 106 
 107         if (isAbsolute) {
 108             return tryLoadLibrary(name);
 109         }
 110 
 111         for (String usr_path : usr_paths) {
 112             String libPath = usr_path + File.separator + name;
 113             try {
 114                 return tryLoadLibrary(libPath);
 115             } catch (RuntimeException | UnsatisfiedLinkError e) {
 116                 if (DEBUG) {
 117                     System.err.println(e);
 118                 }
 119                 // ignore and try next path
 120             }
 121         }
 122 
 123         throw new UnsatisfiedLinkError(name);
 124     }
 125 }
< prev index next >