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

Print this page




  34 import java.security.AccessController;
  35 import java.security.AccessControlContext;
  36 import java.security.CodeSource;
  37 import java.security.Policy;
  38 import java.security.PrivilegedAction;
  39 import java.security.PrivilegedActionException;
  40 import java.security.PrivilegedExceptionAction;
  41 import java.security.ProtectionDomain;
  42 import java.security.cert.Certificate;
  43 import java.util.Collections;
  44 import java.util.Enumeration;
  45 import java.util.HashMap;
  46 import java.util.HashSet;
  47 import java.util.Set;
  48 import java.util.Stack;
  49 import java.util.Map;
  50 import java.util.Vector;
  51 import java.util.Hashtable;
  52 import java.util.WeakHashMap;
  53 import java.util.concurrent.ConcurrentHashMap;
  54 import sun.misc.BootClassLoaderHook;
  55 import sun.misc.ClassFileTransformer;
  56 import sun.misc.CompoundEnumeration;
  57 import sun.misc.Resource;
  58 import sun.misc.URLClassPath;
  59 import sun.misc.VM;
  60 import sun.reflect.Reflection;
  61 import sun.security.util.SecurityConstants;
  62 
  63 /**
  64  * A class loader is an object that is responsible for loading classes. The
  65  * class <tt>ClassLoader</tt> is an abstract class.  Given the <a
  66  * href="#name">binary name</a> of a class, a class loader should attempt to
  67  * locate or generate data that constitutes a definition for the class.  A
  68  * typical strategy is to transform the name into a file name and then read a
  69  * "class file" of that name from a file system.
  70  *
  71  * <p> Every {@link Class <tt>Class</tt>} object contains a {@link
  72  * Class#getClassLoader() reference} to the <tt>ClassLoader</tt> that defined
  73  * it.
  74  *


1283      *
1284      * @throws  IOException
1285      *          If I/O errors occur
1286 
1287      * @since  1.2
1288      */
1289     public static Enumeration<URL> getSystemResources(String name)
1290         throws IOException
1291     {
1292         ClassLoader system = getSystemClassLoader();
1293         if (system == null) {
1294             return getBootstrapResources(name);
1295         }
1296         return system.getResources(name);
1297     }
1298 
1299     /**
1300      * Find resources from the VM's built-in classloader.
1301      */
1302     private static URL getBootstrapResource(String name) {
1303         BootClassLoaderHook.preLoadResource(name);
1304         URLClassPath ucp = getBootstrapClassPath();
1305         Resource res = ucp.getResource(name);
1306         return res != null ? res.getURL() : null;
1307     }
1308 
1309     /**
1310      * Find resources from the VM's built-in classloader.
1311      */
1312     private static Enumeration<URL> getBootstrapResources(String name)
1313         throws IOException
1314     {
1315         final Enumeration<Resource> e =
1316             getBootstrapClassPath().getResources(name);
1317         return new Enumeration<URL> () {
1318             public URL nextElement() {
1319                 return e.nextElement().getURL();
1320             }
1321             public boolean hasMoreElements() {
1322                 return e.hasMoreElements();
1323             }


1797 
1798         // Fill the array with paths from the ldpath
1799         n = i = 0;
1800         j = ldpath.indexOf(ps);
1801         while (j >= 0) {
1802             if (j - i > 0) {
1803                 paths[n++] = ldpath.substring(i, j);
1804             } else if (j - i == 0) {
1805                 paths[n++] = ".";
1806             }
1807             i = j + 1;
1808             j = ldpath.indexOf(ps, i);
1809         }
1810         paths[n] = ldpath.substring(i, ldlen);
1811         return paths;
1812     }
1813 
1814     // Invoked in the java.lang.Runtime class to implement load and loadLibrary.
1815     static void loadLibrary(Class fromClass, String name,
1816                             boolean isAbsolute) {
1817         BootClassLoaderHook.preLoadLibrary(name);
1818         ClassLoader loader =
1819             (fromClass == null) ? null : fromClass.getClassLoader();
1820         if (sys_paths == null) {
1821             usr_paths = initializePath("java.library.path");
1822             sys_paths = initializePath("sun.boot.library.path");
1823         }
1824         if (isAbsolute) {
1825             if (loadLibrary0(fromClass, new File(name))) {
1826                 return;
1827             }
1828             throw new UnsatisfiedLinkError("Can't load library: " + name);
1829         }
1830         if (loader != null) {
1831             String libfilename = loader.findLibrary(name);
1832             if (libfilename != null) {
1833                 File libfile = new File(libfilename);
1834                 if (!libfile.isAbsolute()) {
1835                     throw new UnsatisfiedLinkError(
1836     "ClassLoader.findLibrary failed to return an absolute path: " + libfilename);
1837                 }




  34 import java.security.AccessController;
  35 import java.security.AccessControlContext;
  36 import java.security.CodeSource;
  37 import java.security.Policy;
  38 import java.security.PrivilegedAction;
  39 import java.security.PrivilegedActionException;
  40 import java.security.PrivilegedExceptionAction;
  41 import java.security.ProtectionDomain;
  42 import java.security.cert.Certificate;
  43 import java.util.Collections;
  44 import java.util.Enumeration;
  45 import java.util.HashMap;
  46 import java.util.HashSet;
  47 import java.util.Set;
  48 import java.util.Stack;
  49 import java.util.Map;
  50 import java.util.Vector;
  51 import java.util.Hashtable;
  52 import java.util.WeakHashMap;
  53 import java.util.concurrent.ConcurrentHashMap;

  54 import sun.misc.ClassFileTransformer;
  55 import sun.misc.CompoundEnumeration;
  56 import sun.misc.Resource;
  57 import sun.misc.URLClassPath;
  58 import sun.misc.VM;
  59 import sun.reflect.Reflection;
  60 import sun.security.util.SecurityConstants;
  61 
  62 /**
  63  * A class loader is an object that is responsible for loading classes. The
  64  * class <tt>ClassLoader</tt> is an abstract class.  Given the <a
  65  * href="#name">binary name</a> of a class, a class loader should attempt to
  66  * locate or generate data that constitutes a definition for the class.  A
  67  * typical strategy is to transform the name into a file name and then read a
  68  * "class file" of that name from a file system.
  69  *
  70  * <p> Every {@link Class <tt>Class</tt>} object contains a {@link
  71  * Class#getClassLoader() reference} to the <tt>ClassLoader</tt> that defined
  72  * it.
  73  *


1282      *
1283      * @throws  IOException
1284      *          If I/O errors occur
1285 
1286      * @since  1.2
1287      */
1288     public static Enumeration<URL> getSystemResources(String name)
1289         throws IOException
1290     {
1291         ClassLoader system = getSystemClassLoader();
1292         if (system == null) {
1293             return getBootstrapResources(name);
1294         }
1295         return system.getResources(name);
1296     }
1297 
1298     /**
1299      * Find resources from the VM's built-in classloader.
1300      */
1301     private static URL getBootstrapResource(String name) {

1302         URLClassPath ucp = getBootstrapClassPath();
1303         Resource res = ucp.getResource(name);
1304         return res != null ? res.getURL() : null;
1305     }
1306 
1307     /**
1308      * Find resources from the VM's built-in classloader.
1309      */
1310     private static Enumeration<URL> getBootstrapResources(String name)
1311         throws IOException
1312     {
1313         final Enumeration<Resource> e =
1314             getBootstrapClassPath().getResources(name);
1315         return new Enumeration<URL> () {
1316             public URL nextElement() {
1317                 return e.nextElement().getURL();
1318             }
1319             public boolean hasMoreElements() {
1320                 return e.hasMoreElements();
1321             }


1795 
1796         // Fill the array with paths from the ldpath
1797         n = i = 0;
1798         j = ldpath.indexOf(ps);
1799         while (j >= 0) {
1800             if (j - i > 0) {
1801                 paths[n++] = ldpath.substring(i, j);
1802             } else if (j - i == 0) {
1803                 paths[n++] = ".";
1804             }
1805             i = j + 1;
1806             j = ldpath.indexOf(ps, i);
1807         }
1808         paths[n] = ldpath.substring(i, ldlen);
1809         return paths;
1810     }
1811 
1812     // Invoked in the java.lang.Runtime class to implement load and loadLibrary.
1813     static void loadLibrary(Class fromClass, String name,
1814                             boolean isAbsolute) {

1815         ClassLoader loader =
1816             (fromClass == null) ? null : fromClass.getClassLoader();
1817         if (sys_paths == null) {
1818             usr_paths = initializePath("java.library.path");
1819             sys_paths = initializePath("sun.boot.library.path");
1820         }
1821         if (isAbsolute) {
1822             if (loadLibrary0(fromClass, new File(name))) {
1823                 return;
1824             }
1825             throw new UnsatisfiedLinkError("Can't load library: " + name);
1826         }
1827         if (loader != null) {
1828             String libfilename = loader.findLibrary(name);
1829             if (libfilename != null) {
1830                 File libfile = new File(libfilename);
1831                 if (!libfile.isAbsolute()) {
1832                     throw new UnsatisfiedLinkError(
1833     "ClassLoader.findLibrary failed to return an absolute path: " + libfilename);
1834                 }