< prev index next >

src/java.base/share/classes/java/lang/ClassLoader.java

Print this page
rev 12161 : 8081674: EmptyStackException at startup if running with extended or unsupported charset
Reviewed-by: mchung


1651      * Returns the absolute path name of a native library.  The VM invokes this
1652      * method to locate the native libraries that belong to classes loaded with
1653      * this class loader. If this method returns <tt>null</tt>, the VM
1654      * searches the library along the path specified as the
1655      * "<tt>java.library.path</tt>" property.
1656      *
1657      * @param  libname
1658      *         The library name
1659      *
1660      * @return  The absolute path of the native library
1661      *
1662      * @see  System#loadLibrary(String)
1663      * @see  System#mapLibraryName(String)
1664      *
1665      * @since  1.2
1666      */
1667     protected String findLibrary(String libname) {
1668         return null;
1669     }
1670 


1671     /**
1672      * The inner class NativeLibrary denotes a loaded native library instance.
1673      * Every classloader contains a vector of loaded native libraries in the
1674      * private field <tt>nativeLibraries</tt>.  The native libraries loaded
1675      * into the system are entered into the <tt>systemNativeLibraries</tt>
1676      * vector.
1677      *
1678      * <p> Every native library requires a particular version of JNI. This is
1679      * denoted by the private <tt>jniVersion</tt> field.  This field is set by
1680      * the VM when it loads the library, and used by the VM to pass the correct
1681      * version of JNI to the native methods.  </p>
1682      *
1683      * @see      ClassLoader
1684      * @since    1.2
1685      */
1686     static class NativeLibrary {
1687         // opaque handle to native library, used in native code.
1688         long handle;
1689         // the version of JNI environment the native library requires.
1690         private int jniVersion;
1691         // the class from which the library is loaded, also indicates
1692         // the loader this native library belongs.
1693         private final Class<?> fromClass;
1694         // the canonicalized name of the native library.
1695         // or static library name
1696         String name;
1697         // Indicates if the native library is linked into the VM
1698         boolean isBuiltin;
1699         // Indicates if the native library is loaded
1700         boolean loaded;
1701         native void load(String name, boolean isBuiltin);
1702 
1703         native long find(String name);
1704         native void unload(String name, boolean isBuiltin);
1705         static native String findBuiltinLib(String name);
1706 
1707         public NativeLibrary(Class<?> fromClass, String name, boolean isBuiltin) {
1708             this.name = name;
1709             this.fromClass = fromClass;
1710             this.isBuiltin = isBuiltin;
1711         }
1712 
1713         protected void finalize() {
1714             synchronized (loadedLibraryNames) {
1715                 if (fromClass.getClassLoader() != null && loaded) {
1716                     /* remove the native library name */
1717                     int size = loadedLibraryNames.size();
1718                     for (int i = 0; i < size; i++) {
1719                         if (name.equals(loadedLibraryNames.elementAt(i))) {
1720                             loadedLibraryNames.removeElementAt(i);
1721                             break;
1722                         }
1723                     }
1724                     /* unload the library. */
1725                     ClassLoader.nativeLibraryContext.push(this);


1846             }
1847         }
1848         if (loader != null) {
1849             for (String usr_path : usr_paths) {
1850                 File libfile = new File(usr_path, System.mapLibraryName(name));
1851                 if (loadLibrary0(fromClass, libfile)) {
1852                     return;
1853                 }
1854                 libfile = ClassLoaderHelper.mapAlternativeName(libfile);
1855                 if (libfile != null && loadLibrary0(fromClass, libfile)) {
1856                     return;
1857                 }
1858             }
1859         }
1860         // Oops, it failed
1861         throw new UnsatisfiedLinkError("no " + name + " in java.library.path");
1862     }
1863 
1864     private static boolean loadLibrary0(Class<?> fromClass, final File file) {
1865         // Check to see if we're attempting to access a static library
1866         String name = NativeLibrary.findBuiltinLib(file.getName());
1867         boolean isBuiltin = (name != null);
1868         if (!isBuiltin) {
1869             name = AccessController.doPrivileged(
1870                 new PrivilegedAction<>() {
1871                     public String run() {
1872                         try {
1873                             return file.exists() ? file.getCanonicalPath() : null;
1874                         } catch (IOException e) {
1875                             return null;
1876                         }
1877                     }
1878                 });
1879             if (name == null) {
1880                 return false;
1881             }
1882         }
1883         ClassLoader loader =
1884             (fromClass == null) ? null : fromClass.getClassLoader();
1885         Vector<NativeLibrary> libs =
1886             loader != null ? loader.nativeLibraries : systemNativeLibraries;




1651      * Returns the absolute path name of a native library.  The VM invokes this
1652      * method to locate the native libraries that belong to classes loaded with
1653      * this class loader. If this method returns <tt>null</tt>, the VM
1654      * searches the library along the path specified as the
1655      * "<tt>java.library.path</tt>" property.
1656      *
1657      * @param  libname
1658      *         The library name
1659      *
1660      * @return  The absolute path of the native library
1661      *
1662      * @see  System#loadLibrary(String)
1663      * @see  System#mapLibraryName(String)
1664      *
1665      * @since  1.2
1666      */
1667     protected String findLibrary(String libname) {
1668         return null;
1669     }
1670 
1671     static native String findBuiltinLib(String name);
1672 
1673     /**
1674      * The inner class NativeLibrary denotes a loaded native library instance.
1675      * Every classloader contains a vector of loaded native libraries in the
1676      * private field <tt>nativeLibraries</tt>.  The native libraries loaded
1677      * into the system are entered into the <tt>systemNativeLibraries</tt>
1678      * vector.
1679      *
1680      * <p> Every native library requires a particular version of JNI. This is
1681      * denoted by the private <tt>jniVersion</tt> field.  This field is set by
1682      * the VM when it loads the library, and used by the VM to pass the correct
1683      * version of JNI to the native methods.  </p>
1684      *
1685      * @see      ClassLoader
1686      * @since    1.2
1687      */
1688     static class NativeLibrary {
1689         // opaque handle to native library, used in native code.
1690         long handle;
1691         // the version of JNI environment the native library requires.
1692         private int jniVersion;
1693         // the class from which the library is loaded, also indicates
1694         // the loader this native library belongs.
1695         private final Class<?> fromClass;
1696         // the canonicalized name of the native library.
1697         // or static library name
1698         String name;
1699         // Indicates if the native library is linked into the VM
1700         boolean isBuiltin;
1701         // Indicates if the native library is loaded
1702         boolean loaded;
1703         native void load(String name, boolean isBuiltin);
1704 
1705         native long find(String name);
1706         native void unload(String name, boolean isBuiltin);

1707 
1708         public NativeLibrary(Class<?> fromClass, String name, boolean isBuiltin) {
1709             this.name = name;
1710             this.fromClass = fromClass;
1711             this.isBuiltin = isBuiltin;
1712         }
1713 
1714         protected void finalize() {
1715             synchronized (loadedLibraryNames) {
1716                 if (fromClass.getClassLoader() != null && loaded) {
1717                     /* remove the native library name */
1718                     int size = loadedLibraryNames.size();
1719                     for (int i = 0; i < size; i++) {
1720                         if (name.equals(loadedLibraryNames.elementAt(i))) {
1721                             loadedLibraryNames.removeElementAt(i);
1722                             break;
1723                         }
1724                     }
1725                     /* unload the library. */
1726                     ClassLoader.nativeLibraryContext.push(this);


1847             }
1848         }
1849         if (loader != null) {
1850             for (String usr_path : usr_paths) {
1851                 File libfile = new File(usr_path, System.mapLibraryName(name));
1852                 if (loadLibrary0(fromClass, libfile)) {
1853                     return;
1854                 }
1855                 libfile = ClassLoaderHelper.mapAlternativeName(libfile);
1856                 if (libfile != null && loadLibrary0(fromClass, libfile)) {
1857                     return;
1858                 }
1859             }
1860         }
1861         // Oops, it failed
1862         throw new UnsatisfiedLinkError("no " + name + " in java.library.path");
1863     }
1864 
1865     private static boolean loadLibrary0(Class<?> fromClass, final File file) {
1866         // Check to see if we're attempting to access a static library
1867         String name = findBuiltinLib(file.getName());
1868         boolean isBuiltin = (name != null);
1869         if (!isBuiltin) {
1870             name = AccessController.doPrivileged(
1871                 new PrivilegedAction<>() {
1872                     public String run() {
1873                         try {
1874                             return file.exists() ? file.getCanonicalPath() : null;
1875                         } catch (IOException e) {
1876                             return null;
1877                         }
1878                     }
1879                 });
1880             if (name == null) {
1881                 return false;
1882             }
1883         }
1884         ClassLoader loader =
1885             (fromClass == null) ? null : fromClass.getClassLoader();
1886         Vector<NativeLibrary> libs =
1887             loader != null ? loader.nativeLibraries : systemNativeLibraries;


< prev index next >