Java中ClassLoader类源码

Java中ClassLoader类源码

public abstract class ClassLoader {

    private static native void registerNatives();
    static {
        registerNatives();
    }

    // The parent class loader for delegation
    private ClassLoader parent;

    // Hashtable that maps packages to certs
    private Hashtable package2certs = new Hashtable(11);

    // Shared among all packages with unsigned classes
    java.security.cert.Certificate[] nocerts;

    // The classes loaded by this class loader.  The only purpose of this table
    // is to keep the classes from being GC'ed until the loader is GC'ed.
    private Vector classes = new Vector();

    // The initiating protection domains for all classes loaded by this loader
    private Set domains = new HashSet();

    // Invoked by the VM to record every loaded class with this loader.
    void addClass(Class c) {
        classes.addElement(c);
    }

    // The packages defined in this class loader.  Each package name is mapped
    // to its corresponding Package object.
    private final HashMap<String, Package> packages =
                                new HashMap<String, Package>();

    private static Void checkCreateClassLoader() {
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkCreateClassLoader();
        }
        return null;
    }

    private ClassLoader(Void unused, ClassLoader parent) {
        this.parent = parent;
    }

    protected ClassLoader(ClassLoader parent) {
        this(checkCreateClassLoader(), parent);
    }

    protected ClassLoader() {
        this(checkCreateClassLoader(), getSystemClassLoader());
    }

    public Class<?> loadClass(String name) throws ClassNotFoundException {
	return loadClass(name, false);
    }

    protected synchronized Class<?> loadClass(String name, boolean resolve)
	throws ClassNotFoundException
    {
	// First, check if the class has already been loaded
	Class c = findLoadedClass(name);
	if (c == null) {
	    try {
		if (parent != null) {
		    c = parent.loadClass(name, false);
		} else {
		    c = findBootstrapClassOrNull(name);
		}
	    } catch (ClassNotFoundException e) {
                // ClassNotFoundException thrown if class not found
                // from the non-null parent class loader
            }
            if (c == null) {
	        // If still not found, then invoke findClass in order
	        // to find the class.
	        c = findClass(name);
	    }
	}
	if (resolve) {
	    resolveClass(c);
	}
	return c;
    }

    // This method is invoked by the virtual machine to load a class.
    private synchronized Class loadClassInternal(String name)
	throws ClassNotFoundException
    {
	return loadClass(name);
    }

    private void checkPackageAccess(Class cls, ProtectionDomain pd) {
	final SecurityManager sm = System.getSecurityManager();
	if (sm != null) {
	    final String name = cls.getName();
            final int i = name.lastIndexOf('.');
	    if (i != -1) {
                AccessController.doPrivileged(new PrivilegedAction() {
                    public Object run() {
		        sm.checkPackageAccess(name.substring(0, i));
		        return null;
                    }
                }, new AccessControlContext(new ProtectionDomain[] {pd}));
	    }
	}
	domains.add(pd);
    }

    protected Class<?> findClass(String name) throws ClassNotFoundException {
	throw new ClassNotFoundException(name);
    }

    @Deprecated
    protected final Class<?> defineClass(byte[] b, int off, int len)
	throws ClassFormatError
    {
	return defineClass(null, b, off, len, null);
    }

    protected final Class<?> defineClass(String name, byte[] b, int off, int len)
	throws ClassFormatError
    {
	return defineClass(name, b, off, len, null);
    }

    private ProtectionDomain preDefineClass(String name,
					    ProtectionDomain protectionDomain)
    {
	if (!checkName(name))
	    throw new NoClassDefFoundError("IllegalName: " + name);

	if ((name != null) && name.startsWith("java.")) {
	    throw new SecurityException("Prohibited package name: " +
					name.substring(0, name.lastIndexOf('.')));
	}
	if (protectionDomain == null) {
	    protectionDomain = getDefaultDomain();
	}

	if (name != null)
	    checkCerts(name, protectionDomain.getCodeSource());

	return protectionDomain;
    }

    private String defineClassSourceLocation(ProtectionDomain protectionDomain)
    {
	CodeSource cs = protectionDomain.getCodeSource();
	String source = null;
	if (cs != null && cs.getLocation() != null) {
	    source = cs.getLocation().toString();
	}
	return source;
    }

    private Class defineTransformedClass(String name, byte[] b, int off, int len,
					 ProtectionDomain protectionDomain,
					 ClassFormatError cfe, String source,
                                         boolean verify)
      throws ClassFormatError
    {
        // Class format error - try to transform the bytecode and
        // define the class again
        //
        Object[] transformers = ClassFileTransformer.getTransformers();
	Class c = null;

	for (int i = 0; transformers != null && i < transformers.length; i++) {
	    try {
	      // Transform byte code using transformer
	      byte[] tb = ((ClassFileTransformer) transformers[i]).transform(b, off, len);
	      c = defineClass1(name, tb, 0, tb.length, protectionDomain, source,
                               verify);
	      break;
	    } catch (ClassFormatError cfe2)	{
	      // If ClassFormatError occurs, try next transformer
	    }
	}

	// Rethrow original ClassFormatError if unable to transform
	// bytecode to well-formed
	//
	if (c == null)
	    throw cfe;

	return c;
    }

    private void postDefineClass(Class c, ProtectionDomain protectionDomain)
    {
	if (protectionDomain.getCodeSource() != null) {
	    java.security.cert.Certificate certs[] =
		protectionDomain.getCodeSource().getCertificates();
	    if (certs != null)
		setSigners(c, certs);
	}
    }

    protected final Class<?> defineClass(String name, byte[] b, int off, int len,
					 ProtectionDomain protectionDomain)
	throws ClassFormatError
    {
         return defineClassCond(name, b, off, len, protectionDomain, true);
    }

    // Private method w/ an extra argument for skipping class verification
    private final Class<?> defineClassCond(String name,
                                           byte[] b, int off, int len,
                                           ProtectionDomain protectionDomain,
                                           boolean verify)
        throws ClassFormatError
    {
	protectionDomain = preDefineClass(name, protectionDomain);

	Class c = null;
        String source = defineClassSourceLocation(protectionDomain);

	try {
	    c = defineClass1(name, b, off, len, protectionDomain, source,
                             verify);
	} catch (ClassFormatError cfe) {
	    c = defineTransformedClass(name, b, off, len, protectionDomain, cfe,
                                       source, verify);
	}

	postDefineClass(c, protectionDomain);
	return c;
    }

    protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
					 ProtectionDomain protectionDomain)
	throws ClassFormatError
    {
        return defineClassCond(name, b, protectionDomain, true);
    }
 
    // Private method w/ an extra argument for skipping class verification
    private final Class<?> defineClassCond(String name,
                                           java.nio.ByteBuffer b,
                                           ProtectionDomain protectionDomain,
                                           boolean verify)
         throws ClassFormatError
    {
	int len = b.remaining();

	// Use byte[] if not a direct ByteBufer:
	if (!b.isDirect()) {
	    if (b.hasArray()) {
		return defineClassCond(name, b.array(),
				       b.position() + b.arrayOffset(), len,
				       protectionDomain, verify);
	    } else {
		// no array, or read-only array
		byte[] tb = new byte[len];
		b.get(tb);  // get bytes out of byte buffer.
		return defineClassCond(name, tb, 0, len, protectionDomain,
                                       verify);
	    }
	}

        protectionDomain = preDefineClass(name, protectionDomain);

	Class c = null;
	String source = defineClassSourceLocation(protectionDomain);

	try {
	    c = defineClass2(name, b, b.position(), len, protectionDomain,
                             source, verify);
	} catch (ClassFormatError cfe) {
	    byte[] tb = new byte[len];
	    b.get(tb);  // get bytes out of byte buffer.
	    c = defineTransformedClass(name, tb, 0, len, protectionDomain, cfe,
                                       source, verify);
	}

	postDefineClass(c, protectionDomain);
	return c;
    }

    // Need to keep this one release after fixing 4735126 - see 4931983
    private Class defineClass0(String name, byte[] b, int off, int len,
                               ProtectionDomain pd) {
         return defineClass0(name, b, off, len, pd, true);
    }

    private native Class defineClass0(String name, byte[] b, int off, int len,
	                              ProtectionDomain pd, boolean verify);

    private native Class defineClass1(String name, byte[] b, int off, int len,
	                              ProtectionDomain pd, String source,
                                      boolean verify);

    private native Class defineClass2(String name, java.nio.ByteBuffer b,
				      int off, int len, ProtectionDomain pd,
				      String source, boolean verify);

    // true if the name is null or has the potential to be a valid binary name
    private boolean checkName(String name) {
	if ((name == null) || (name.length() == 0))
   	    return true;
	if ((name.indexOf('/') != -1)
	    || (!VM.allowArraySyntax() && (name.charAt(0) == '[')))
   	    return false;
 	return true;
    }

    private synchronized void checkCerts(String name, CodeSource cs) {
	int i = name.lastIndexOf('.');
	String pname = (i == -1) ? "" : name.substring(0, i);
	java.security.cert.Certificate[] pcerts =
	    (java.security.cert.Certificate[]) package2certs.get(pname);
        if (pcerts == null) {
	    // first class in this package gets to define which
	    // certificates must be the same for all other classes
	    // in this package
	    if (cs != null) {
		pcerts = cs.getCertificates();
	    }
	    if (pcerts == null) {
		if (nocerts == null)
		    nocerts = new java.security.cert.Certificate[0];
		pcerts = nocerts;
	    }
	    package2certs.put(pname, pcerts);
	} else {
	    java.security.cert.Certificate[] certs = null;
	    if (cs != null) {
		certs = cs.getCertificates();
	    }

	    if (!compareCerts(pcerts, certs)) {
		throw new SecurityException("class \""+ name +
					    "\"'s signer information does not match signer information of other classes in the same package");
	    }
	}
    }

    private boolean compareCerts(java.security.cert.Certificate[] pcerts,
				 java.security.cert.Certificate[] certs)
    {
	// certs can be null, indicating no certs.
	if ((certs == null) || (certs.length == 0)) {
	    return pcerts.length == 0;
	}

	// the length must be the same at this point
	if (certs.length != pcerts.length)
	    return false;

	// go through and make sure all the certs in one array
	// are in the other and vice-versa.
	boolean match;
	for (int i = 0; i < certs.length; i++) {
	    match = false;
	    for (int j = 0; j < pcerts.length; j++) {
		if (certs[i].equals(pcerts[j])) {
		    match = true;
		    break;
		}
	    }
	    if (!match) return false;
	}

	// now do the same for pcerts
	for (int i = 0; i < pcerts.length; i++) {
	    match = false;
	    for (int j = 0; j < certs.length; j++) {
		if (pcerts[i].equals(certs[j])) {
		    match = true;
		    break;
		}
	    }
	    if (!match) return false;
	}

	return true;
    }

    protected final void resolveClass(Class<?> c) {
	resolveClass0(c);
    }

    private native void resolveClass0(Class c);

    protected final Class<?> findSystemClass(String name)
	throws ClassNotFoundException
    {
	ClassLoader system = getSystemClassLoader();
	if (system == null) {
	    if (!checkName(name))
		throw new ClassNotFoundException(name);
            Class cls = findBootstrapClass(name);
            if (cls == null) {
                throw new ClassNotFoundException(name);
            } 
	    return cls;
	}
	return system.loadClass(name);
    }

    private Class findBootstrapClassOrNull(String name)
    {
        if (!checkName(name)) return null;

        return findBootstrapClass(name);
    }

    // return null if not found
    private native Class findBootstrapClass(String name);

    protected final Class<?> findLoadedClass(String name) {
	if (!checkName(name))
	    return null;
	return findLoadedClass0(name);
    }

    private native final Class findLoadedClass0(String name);

    protected final void setSigners(Class<?> c, Object[] signers) {
	c.setSigners(signers);
    }

    // -- Resource --

    public URL getResource(String name) {
	URL url;
	if (parent != null) {
	    url = parent.getResource(name);
	} else {
	    url = getBootstrapResource(name);
	}
	if (url == null) {
	    url = findResource(name);
	}
	return url;
    }

    public Enumeration<URL> getResources(String name) throws IOException {
	Enumeration[] tmp = new Enumeration[2];
	if (parent != null) {
	    tmp[0] = parent.getResources(name);
	} else {
	    tmp[0] = getBootstrapResources(name);
	}
	tmp[1] = findResources(name);

	return new CompoundEnumeration(tmp);
    }

    protected URL findResource(String name) {
	return null;
    }

    protected Enumeration<URL> findResources(String name) throws IOException {
	return new CompoundEnumeration(new Enumeration[0]);
    }

    public static URL getSystemResource(String name) {
	ClassLoader system = getSystemClassLoader();
	if (system == null) {
	    return getBootstrapResource(name);
	}
	return system.getResource(name);
    }

    public static Enumeration<URL> getSystemResources(String name)
	throws IOException
    {
	ClassLoader system = getSystemClassLoader();
	if (system == null) {
	    return getBootstrapResources(name);
	}
	return system.getResources(name);
    }

   
    private static URL getBootstrapResource(String name) {
        try {
            // If this is a known JRE resource, ensure that its bundle is 
            // downloaded.  If it isn't known, we just ignore the download
            // failure and check to see if we can find the resource anyway
            // (which is possible if the boot class path has been modified).
            sun.jkernel.DownloadManager.getBootClassPathEntryForResource(name);
        } catch (NoClassDefFoundError e) {
            // This happens while Java itself is being compiled; DownloadManager
            // isn't accessible when this code is first invoked.  It isn't an
            // issue, as if we can't find DownloadManager, we can safely assume
            // that additional code is not available for download.
        }
	URLClassPath ucp = getBootstrapClassPath();
	Resource res = ucp.getResource(name);
	return res != null ? res.getURL() : null;
    }

    /**
     * Find resources from the VM's built-in classloader.
     */
    private static Enumeration getBootstrapResources(String name)
	throws IOException
    {
	final Enumeration e = getBootstrapClassPath().getResources(name);
	return new Enumeration () {
	    public Object nextElement() {
		return ((Resource)e.nextElement()).getURL();
	    }
	    public boolean hasMoreElements() {
		return e.hasMoreElements();
	    }
	};
    }

    // Returns the URLClassPath that is used for finding system resources.
    static URLClassPath getBootstrapClassPath() {
        return sun.misc.Launcher.getBootstrapClassPath();
    }

    public InputStream getResourceAsStream(String name) {
	URL url = getResource(name);
	try {
	    return url != null ? url.openStream() : null;
	} catch (IOException e) {
	    return null;
	}
    }

    public static InputStream getSystemResourceAsStream(String name) {
        URL url = getSystemResource(name);
        try {
            return url != null ? url.openStream() : null;
        } catch (IOException e) {
            return null;
        }
    }

    public final ClassLoader getParent() {
	if (parent == null)
	    return null;
	SecurityManager sm = System.getSecurityManager();
	if (sm != null) {
	    ClassLoader ccl = getCallerClassLoader();
	    if (ccl != null && !isAncestor(ccl)) {
		sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
	    }
	}
	return parent;
    }
    public static ClassLoader getSystemClassLoader() {
	initSystemClassLoader();
	if (scl == null) {
	    return null;
	}
	SecurityManager sm = System.getSecurityManager();
	if (sm != null) {
	    ClassLoader ccl = getCallerClassLoader();
	    if (ccl != null && ccl != scl && !scl.isAncestor(ccl)) {
		sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
	    }
	}
	return scl;
    }

    private static synchronized void initSystemClassLoader() {
	if (!sclSet) {
	    if (scl != null)
		throw new IllegalStateException("recursive invocation");
            sun.misc.Launcher l = sun.misc.Launcher.getLauncher();
	    if (l != null) {
		Throwable oops = null;
		scl = l.getClassLoader();
	        try {
		    PrivilegedExceptionAction a;
		    a = new SystemClassLoaderAction(scl);
                    scl = (ClassLoader) AccessController.doPrivileged(a);
	        } catch (PrivilegedActionException pae) {
		    oops = pae.getCause();
	            if (oops instanceof InvocationTargetException) {
		        oops = oops.getCause();
		    }
	        }
		if (oops != null) {
		    if (oops instanceof Error) {
			throw (Error) oops;
		    } else {
		        // wrap the exception
		        throw new Error(oops);
		    }
		}
	    }
	    sclSet = true;
	}
    }

    // Returns true if the specified class loader can be found in this class
    // loader's delegation chain.
    boolean isAncestor(ClassLoader cl) {
	ClassLoader acl = this;
	do {
	    acl = acl.parent;
	    if (cl == acl) {
		return true;
	    }
	} while (acl != null);
	return false;
    }

    // Returns the invoker's class loader, or null if none.
    // NOTE: This must always be invoked when there is exactly one intervening
    // frame from the core libraries on the stack between this method's
    // invocation and the desired invoker.
    static ClassLoader getCallerClassLoader() {
        // NOTE use of more generic Reflection.getCallerClass()
        Class caller = Reflection.getCallerClass(3);
        // This can be null if the VM is requesting it
        if (caller == null) {
            return null;
        }
        // Circumvent security check since this is package-private
        return caller.getClassLoader0();
    }

    // The class loader for the system
    private static ClassLoader scl;

    // Set to true once the system class loader has been set
    private static boolean sclSet;


    protected Package definePackage(String name, String specTitle,
				    String specVersion, String specVendor,
				    String implTitle, String implVersion,
				    String implVendor, URL sealBase)
	throws IllegalArgumentException
    {
	synchronized (packages) {
	    Package pkg = getPackage(name);
	    if (pkg != null) {
		throw new IllegalArgumentException(name);
	    }
	    pkg = new Package(name, specTitle, specVersion, specVendor,
			      implTitle, implVersion, implVendor,
			      sealBase, this);
	    packages.put(name, pkg);
	    return pkg;
	}
    }

    protected Package getPackage(String name) {
        Package pkg;
        synchronized(packages) {
	    pkg = packages.get(name);
        }
	if (pkg == null) {
	    if (parent != null) {
		pkg = parent.getPackage(name);
            } else {
	        pkg = Package.getSystemPackage(name);
	    }
	    if (pkg != null) {
                synchronized(packages) {
                    if (packages.get(name) != null) {
                        packages.put(name, pkg);
                    }
                }
	    }
	}
	return pkg;
    }

    protected Package[] getPackages() {
	Map map;
	synchronized (packages) {
	    map = (Map)packages.clone();
	}
	Package[] pkgs;
	if (parent != null) {
	    pkgs = parent.getPackages();
	} else {
	    pkgs = Package.getSystemPackages();
	}
	if (pkgs != null) {
	    for (int i = 0; i < pkgs.length; i++) {
                String pkgName = pkgs[i].getName();
                if (map.get(pkgName) == null) {
                    map.put(pkgName, pkgs[i]);
                }
	    }
	}
	return (Package[])map.values().toArray(new Package[map.size()]);
    }

    protected String findLibrary(String libname) {
        return null;
    }

    static class NativeLibrary {
	// opaque handle to native library, used in native code.
        long handle;
        // the version of JNI environment the native library requires.
        private int jniVersion;
        // the class from which the library is loaded, also indicates
	// the loader this native library belongs.
        private Class fromClass;
        // the canonicalized name of the native library.
        String name;

        native void load(String name);
        native long find(String name);
        native void unload();

        public NativeLibrary(Class fromClass, String name) {
            this.name = name;
	    this.fromClass = fromClass;
	}

        protected void finalize() {
	    synchronized (loadedLibraryNames) {
	        if (fromClass.getClassLoader() != null && handle != 0) {
		    /* remove the native library name */
		    int size = loadedLibraryNames.size();
		    for (int i = 0; i < size; i++) {
		        if (name.equals(loadedLibraryNames.elementAt(i))) {
			    loadedLibraryNames.removeElementAt(i);
			    break;
			}
		    }
		    /* unload the library. */
		    ClassLoader.nativeLibraryContext.push(this);
		    try {
			unload();
		    } finally {
		        ClassLoader.nativeLibraryContext.pop();
		    }
		}
	    }
	}
        // Invoked in the VM to determine the context class in
	// JNI_Load/JNI_Unload
        static Class getFromClass() {
            return ((NativeLibrary)
		    (ClassLoader.nativeLibraryContext.peek())).fromClass;
	}
    }

    // The "default" domain. Set as the default ProtectionDomain on newly
    // created classes.
    private ProtectionDomain defaultDomain = null;

    // Returns (and initializes) the default domain.
    private synchronized ProtectionDomain getDefaultDomain() {
	if (defaultDomain == null) {
	    CodeSource cs =
		new CodeSource(null, (java.security.cert.Certificate[]) null);
	    defaultDomain = new ProtectionDomain(cs, null, this, null);
	}
	return defaultDomain;
    }

    // All native library names we've loaded.
    private static Vector loadedLibraryNames = new Vector();
    // Native libraries belonging to system classes.
    private static Vector systemNativeLibraries = new Vector();
    // Native libraries associated with the class loader.
    private Vector nativeLibraries = new Vector();

    // native libraries being loaded/unloaded.
    private static Stack nativeLibraryContext = new Stack();

    // The paths searched for libraries
    static private String usr_paths[];
    static private String sys_paths[];

    private static String[] initializePath(String propname) {
        String ldpath = System.getProperty(propname, "");
	String ps = File.pathSeparator;
	int ldlen = ldpath.length();
	int i, j, n;
	// Count the separators in the path
	i = ldpath.indexOf(ps);
	n = 0;
	while (i >= 0) {
	    n++;
	    i = ldpath.indexOf(ps, i + 1);
	}

	// allocate the array of paths - n :'s = n + 1 path elements
	String[] paths = new String[n + 1];

	// Fill the array with paths from the ldpath
	n = i = 0;
	j = ldpath.indexOf(ps);
	while (j >= 0) {
	    if (j - i > 0) {
	        paths[n++] = ldpath.substring(i, j);
	    } else if (j - i == 0) {
	        paths[n++] = ".";
	    }
	    i = j + 1;
	    j = ldpath.indexOf(ps, i);
	}
	paths[n] = ldpath.substring(i, ldlen);
	return paths;
    }

    // Invoked in the java.lang.Runtime class to implement load and loadLibrary.
    static void loadLibrary(Class fromClass, String name,
			    boolean isAbsolute) {
        try {
            if (!DownloadManager.isJREComplete() && 
                    !DownloadManager.isCurrentThreadDownloading()) {
                DownloadManager.downloadFile("bin/" + 
                    System.mapLibraryName(name));
                // it doesn't matter if the downloadFile call returns false --
                // it probably just means that this is a user library, as 
                // opposed to a JRE library
            }
        } catch (IOException e) {
            throw new UnsatisfiedLinkError("Error downloading library " + 
                                                name + ": " + e);
        } catch (NoClassDefFoundError e) {
            // This happens while Java itself is being compiled; DownloadManager
            // isn't accessible when this code is first invoked.  It isn't an
            // issue, as if we can't find DownloadManager, we can safely assume
            // that additional code is not available for download.
        }
        ClassLoader loader =
	    (fromClass == null) ? null : fromClass.getClassLoader();
        if (sys_paths == null) {
	    usr_paths = initializePath("java.library.path");
	    sys_paths = initializePath("sun.boot.library.path");
        }
        if (isAbsolute) {
	    if (loadLibrary0(fromClass, new File(name))) {
	        return;
	    }
	    throw new UnsatisfiedLinkError("Can't load library: " + name);
	}
	if (loader != null) {
	    String libfilename = loader.findLibrary(name);
	    if (libfilename != null) {
	        File libfile = new File(libfilename);
	        if (!libfile.isAbsolute()) {
		    throw new UnsatisfiedLinkError(
    "ClassLoader.findLibrary failed to return an absolute path: " + libfilename);
		}
		if (loadLibrary0(fromClass, libfile)) {
		    return;
		}
		throw new UnsatisfiedLinkError("Can't load " + libfilename);
	    }
	}
	for (int i = 0 ; i < sys_paths.length ; i++) {
	    File libfile = new File(sys_paths[i], System.mapLibraryName(name));
	    if (loadLibrary0(fromClass, libfile)) {
	        return;
	    }
	}
	if (loader != null) {
	    for (int i = 0 ; i < usr_paths.length ; i++) {
	        File libfile = new File(usr_paths[i],
					System.mapLibraryName(name));
		if (loadLibrary0(fromClass, libfile)) {
		    return;
		}
	    }
	}
	// Oops, it failed
        throw new UnsatisfiedLinkError("no " + name + " in java.library.path");
    }

    private static boolean loadLibrary0(Class fromClass, final File file) {
	Boolean exists = (Boolean)
	    AccessController.doPrivileged(new PrivilegedAction() {
		public Object run() {
		    return new Boolean(file.exists());
		}
	    });
	if (!exists.booleanValue()) {
	    return false;
	}
        String name;
	try {
	    name = file.getCanonicalPath();
	} catch (IOException e) {
	    return false;
	}
        ClassLoader loader =
	    (fromClass == null) ? null : fromClass.getClassLoader();
        Vector libs =
	    loader != null ? loader.nativeLibraries : systemNativeLibraries;
	synchronized (libs) {
	    int size = libs.size();
	    for (int i = 0; i < size; i++) {
	        NativeLibrary lib = (NativeLibrary)libs.elementAt(i);
		if (name.equals(lib.name)) {
		    return true;
		}
	    }

	    synchronized (loadedLibraryNames) {
	        if (loadedLibraryNames.contains(name)) {
		    throw new UnsatisfiedLinkError
		        ("Native Library " +
			 name +
			 " already loaded in another classloader");
		}
		
		int n = nativeLibraryContext.size();
		for (int i = 0; i < n; i++) {
		    NativeLibrary lib = (NativeLibrary)
		        nativeLibraryContext.elementAt(i);
		    if (name.equals(lib.name)) {
		        if (loader == lib.fromClass.getClassLoader()) {
			    return true;
			} else {
			    throw new UnsatisfiedLinkError
			        ("Native Library " +
				 name +
				 " is being loaded in another classloader");
			}
		    }
		}
		NativeLibrary lib = new NativeLibrary(fromClass, name);
		nativeLibraryContext.push(lib);
		try {
		    lib.load(name);
		} finally {
		    nativeLibraryContext.pop();
		}
		if (lib.handle != 0) {
		    loadedLibraryNames.addElement(name);
		    libs.addElement(lib);
		    return true;
		}
		return false;
	    }
	}
    }

    // Invoked in the VM class linking code.
    static long findNative(ClassLoader loader, String name) {
        Vector libs =
	    loader != null ? loader.nativeLibraries : systemNativeLibraries;
	synchronized (libs) {
	    int size = libs.size();
	    for (int i = 0; i < size; i++) {
	        NativeLibrary lib = (NativeLibrary)libs.elementAt(i);
		long entry = lib.find(name);
		if (entry != 0)
		    return entry;
	    }
	}
	return 0;
    }


    // -- Assertion management --

    // The default toggle for assertion checking.
    private boolean defaultAssertionStatus = false;

    // Maps String packageName to Boolean package default assertion status Note
    // that the default package is placed under a null map key.  If this field
    // is null then we are delegating assertion status queries to the VM, i.e.,
    // none of this ClassLoader's assertion status modification methods have
    // been invoked.
    private Map packageAssertionStatus = null;

    // Maps String fullyQualifiedClassName to Boolean assertionStatus If this
    // field is null then we are delegating assertion status queries to the VM,
    // i.e., none of this ClassLoader's assertion status modification methods
    // have been invoked.
    Map classAssertionStatus = null;

    public synchronized void setDefaultAssertionStatus(boolean enabled) {
        if (classAssertionStatus == null)
            initializeJavaAssertionMaps();

        defaultAssertionStatus = enabled;
    }

    public synchronized void setPackageAssertionStatus(String packageName,
                                                       boolean enabled)
    {
        if (packageAssertionStatus == null)
            initializeJavaAssertionMaps();

        packageAssertionStatus.put(packageName, Boolean.valueOf(enabled));
    }

    public synchronized void setClassAssertionStatus(String className,
                                                     boolean enabled)
    {
        if (classAssertionStatus == null)
            initializeJavaAssertionMaps();

        classAssertionStatus.put(className, Boolean.valueOf(enabled));
    }

    public synchronized void clearAssertionStatus() {
        /*
         * Whether or not "Java assertion maps" are initialized, set
         * them to empty maps, effectively ignoring any present settings.
         */
        classAssertionStatus = new HashMap();
        packageAssertionStatus = new HashMap();

        defaultAssertionStatus = false;
    }

    synchronized boolean desiredAssertionStatus(String className) {
        Boolean result;

        // assert classAssertionStatus   != null;
        // assert packageAssertionStatus != null;

        // Check for a class entry
        result = (Boolean)classAssertionStatus.get(className);
        if (result != null)
            return result.booleanValue();

        // Check for most specific package entry
        int dotIndex = className.lastIndexOf(".");
        if (dotIndex < 0) { // default package
            result = (Boolean)packageAssertionStatus.get(null);
            if (result != null)
                return result.booleanValue();
        }
        while(dotIndex > 0) {
            className = className.substring(0, dotIndex);
            result = (Boolean)packageAssertionStatus.get(className);
            if (result != null)
                return result.booleanValue();
            dotIndex = className.lastIndexOf(".", dotIndex-1);
        }

        // Return the classloader default
        return defaultAssertionStatus;
    }

    // Set up the assertions with information provided by the VM.
    private void initializeJavaAssertionMaps() {
        // assert Thread.holdsLock(this);

        classAssertionStatus = new HashMap();
        packageAssertionStatus = new HashMap();
        AssertionStatusDirectives directives = retrieveDirectives();

        for(int i = 0; i < directives.classes.length; i++)
            classAssertionStatus.put(directives.classes[i],
                              Boolean.valueOf(directives.classEnabled[i]));

        for(int i = 0; i < directives.packages.length; i++)
            packageAssertionStatus.put(directives.packages[i],
                              Boolean.valueOf(directives.packageEnabled[i]));

        defaultAssertionStatus = directives.deflt;
    }

    // Retrieves the assertion directives from the VM.
    private static native AssertionStatusDirectives retrieveDirectives();
}


class SystemClassLoaderAction implements PrivilegedExceptionAction {
    private ClassLoader parent;

    SystemClassLoaderAction(ClassLoader parent) {
	this.parent = parent;
    }

    public Object run() throws Exception {
	ClassLoader sys;
	Constructor ctor;
	Class c;
	Class cp[] = { ClassLoader.class };
	Object params[] = { parent };

        String cls = System.getProperty("java.system.class.loader");
	if (cls == null) {
	    return parent;
	}

	c = Class.forName(cls, true, parent);
	ctor = c.getDeclaredConstructor(cp);
	sys = (ClassLoader) ctor.newInstance(params);
	Thread.currentThread().setContextClassLoader(sys);
	return sys;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值