< prev index next >

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

Print this page
rev 10818 : 8081674: EmptyStackException at startup if running with extended or unsupported charset
Reviewed-by: mchung
   1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1673      * Returns the absolute path name of a native library.  The VM invokes this
1674      * method to locate the native libraries that belong to classes loaded with
1675      * this class loader. If this method returns <tt>null</tt>, the VM
1676      * searches the library along the path specified as the
1677      * "<tt>java.library.path</tt>" property.
1678      *
1679      * @param  libname
1680      *         The library name
1681      *
1682      * @return  The absolute path of the native library
1683      *
1684      * @see  System#loadLibrary(String)
1685      * @see  System#mapLibraryName(String)
1686      *
1687      * @since  1.2
1688      */
1689     protected String findLibrary(String libname) {
1690         return null;
1691     }
1692 


1693     /**
1694      * The inner class NativeLibrary denotes a loaded native library instance.
1695      * Every classloader contains a vector of loaded native libraries in the
1696      * private field <tt>nativeLibraries</tt>.  The native libraries loaded
1697      * into the system are entered into the <tt>systemNativeLibraries</tt>
1698      * vector.
1699      *
1700      * <p> Every native library requires a particular version of JNI. This is
1701      * denoted by the private <tt>jniVersion</tt> field.  This field is set by
1702      * the VM when it loads the library, and used by the VM to pass the correct
1703      * version of JNI to the native methods.  </p>
1704      *
1705      * @see      ClassLoader
1706      * @since    1.2
1707      */
1708     static class NativeLibrary {
1709         // opaque handle to native library, used in native code.
1710         long handle;
1711         // the version of JNI environment the native library requires.
1712         private int jniVersion;
1713         // the class from which the library is loaded, also indicates
1714         // the loader this native library belongs.
1715         private final Class<?> fromClass;
1716         // the canonicalized name of the native library.
1717         // or static library name
1718         String name;
1719         // Indicates if the native library is linked into the VM
1720         boolean isBuiltin;
1721         // Indicates if the native library is loaded
1722         boolean loaded;
1723         native void load(String name, boolean isBuiltin);
1724 
1725         native long find(String name);
1726         native void unload(String name, boolean isBuiltin);
1727         static native String findBuiltinLib(String name);
1728 
1729         public NativeLibrary(Class<?> fromClass, String name, boolean isBuiltin) {
1730             this.name = name;
1731             this.fromClass = fromClass;
1732             this.isBuiltin = isBuiltin;
1733         }
1734 
1735         protected void finalize() {
1736             synchronized (loadedLibraryNames) {
1737                 if (fromClass.getClassLoader() != null && loaded) {
1738                     /* remove the native library name */
1739                     int size = loadedLibraryNames.size();
1740                     for (int i = 0; i < size; i++) {
1741                         if (name.equals(loadedLibraryNames.elementAt(i))) {
1742                             loadedLibraryNames.removeElementAt(i);
1743                             break;
1744                         }
1745                     }
1746                     /* unload the library. */
1747                     ClassLoader.nativeLibraryContext.push(this);


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


   1 /*
   2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1673      * Returns the absolute path name of a native library.  The VM invokes this
1674      * method to locate the native libraries that belong to classes loaded with
1675      * this class loader. If this method returns <tt>null</tt>, the VM
1676      * searches the library along the path specified as the
1677      * "<tt>java.library.path</tt>" property.
1678      *
1679      * @param  libname
1680      *         The library name
1681      *
1682      * @return  The absolute path of the native library
1683      *
1684      * @see  System#loadLibrary(String)
1685      * @see  System#mapLibraryName(String)
1686      *
1687      * @since  1.2
1688      */
1689     protected String findLibrary(String libname) {
1690         return null;
1691     }
1692 
1693     static native String findBuiltinLib(String name);
1694 
1695     /**
1696      * The inner class NativeLibrary denotes a loaded native library instance.
1697      * Every classloader contains a vector of loaded native libraries in the
1698      * private field <tt>nativeLibraries</tt>.  The native libraries loaded
1699      * into the system are entered into the <tt>systemNativeLibraries</tt>
1700      * vector.
1701      *
1702      * <p> Every native library requires a particular version of JNI. This is
1703      * denoted by the private <tt>jniVersion</tt> field.  This field is set by
1704      * the VM when it loads the library, and used by the VM to pass the correct
1705      * version of JNI to the native methods.  </p>
1706      *
1707      * @see      ClassLoader
1708      * @since    1.2
1709      */
1710     static class NativeLibrary {
1711         // opaque handle to native library, used in native code.
1712         long handle;
1713         // the version of JNI environment the native library requires.
1714         private int jniVersion;
1715         // the class from which the library is loaded, also indicates
1716         // the loader this native library belongs.
1717         private final Class<?> fromClass;
1718         // the canonicalized name of the native library.
1719         // or static library name
1720         String name;
1721         // Indicates if the native library is linked into the VM
1722         boolean isBuiltin;
1723         // Indicates if the native library is loaded
1724         boolean loaded;
1725         native void load(String name, boolean isBuiltin);
1726 
1727         native long find(String name);
1728         native void unload(String name, boolean isBuiltin);

1729 
1730         public NativeLibrary(Class<?> fromClass, String name, boolean isBuiltin) {
1731             this.name = name;
1732             this.fromClass = fromClass;
1733             this.isBuiltin = isBuiltin;
1734         }
1735 
1736         protected void finalize() {
1737             synchronized (loadedLibraryNames) {
1738                 if (fromClass.getClassLoader() != null && loaded) {
1739                     /* remove the native library name */
1740                     int size = loadedLibraryNames.size();
1741                     for (int i = 0; i < size; i++) {
1742                         if (name.equals(loadedLibraryNames.elementAt(i))) {
1743                             loadedLibraryNames.removeElementAt(i);
1744                             break;
1745                         }
1746                     }
1747                     /* unload the library. */
1748                     ClassLoader.nativeLibraryContext.push(this);


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


< prev index next >