urlclassloader.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 606 行 · 第 1/2 页

JAVA
606
字号
/* * @(#)URLClassLoader.java	1.81 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * General Public License version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * */package java.net;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.io.File;import java.io.FilePermission;import java.io.InputStream;import java.io.IOException;import java.net.URL;import java.net.URLConnection;import java.net.URLStreamHandlerFactory;import java.util.Enumeration;import java.util.NoSuchElementException;import java.util.StringTokenizer;import java.util.jar.Manifest;import java.util.jar.Attributes;import java.util.jar.Attributes.Name;import java.security.PrivilegedAction;import java.security.PrivilegedExceptionAction;import java.security.AccessController;import java.security.AccessControlContext;import java.security.SecureClassLoader;import java.security.CodeSource;import java.security.Permission;import java.security.PermissionCollection;import sun.misc.Resource;import sun.misc.URLClassPath;import sun.net.www.ParseUtil;import sun.security.util.SecurityConstants;/** * This class loader is used to load classes and resources from a search * path of URLs referring to both JAR files and directories. Any URL that * ends with a '/' is assumed to refer to a directory. Otherwise, the URL * is assumed to refer to a JAR file which will be opened as needed. * <p> * The AccessControlContext of the thread that created the instance of * URLClassLoader will be used when subsequently loading classes and * resources. * <p> * The classes that are loaded are by default granted permission only to * access the URLs specified when the URLClassLoader was created. * * @author  David Connelly * @version 1.72 10/17/00 * @since   1.2 */public class URLClassLoader extends SecureClassLoader {    /* The search path for classes and resources */    private URLClassPath ucp;    /* The context to be used when loading classes and resources */    private AccessControlContext acc;    /**     * Constructs a new URLClassLoader for the given URLs. The URLs will be     * searched in the order specified for classes and resources after first     * searching in the specified parent class loader. Any URL that ends with     * a '/' is assumed to refer to a directory. Otherwise, the URL is assumed     * to refer to a JAR file which will be downloaded and opened as needed.     *     * <p>If there is a security manager, this method first     * calls the security manager's <code>checkCreateClassLoader</code> method     * to ensure creation of a class loader is allowed.     *      * @param urls the URLs from which to load classes and resources     * @param parent the parent class loader for delegation     * @exception  SecurityException  if a security manager exists and its       *             <code>checkCreateClassLoader</code> method doesn't allow      *             creation of a class loader.     * @see SecurityManager#checkCreateClassLoader     */    public URLClassLoader(URL[] urls, ClassLoader parent) {	super(parent);	// this is to make the stack depth consistent with 1.1	SecurityManager security = System.getSecurityManager();	if (security != null) {	    security.checkCreateClassLoader();	}	ucp = new URLClassPath(urls);	acc = AccessController.getContext();    }    /**     * Constructs a new URLClassLoader for the specified URLs using the     * default delegation parent <code>ClassLoader</code>. The URLs will     * be searched in the order specified for classes and resources after     * first searching in the parent class loader. Any URL that ends with     * a '/' is assumed to refer to a directory. Otherwise, the URL is     * assumed to refer to a JAR file which will be downloaded and opened     * as needed.     *     * <p>If there is a security manager, this method first     * calls the security manager's <code>checkCreateClassLoader</code> method     * to ensure creation of a class loader is allowed.     *      * @param urls the URLs from which to load classes and resources     *     * @exception  SecurityException  if a security manager exists and its       *             <code>checkCreateClassLoader</code> method doesn't allow      *             creation of a class loader.     * @see SecurityManager#checkCreateClassLoader     */    public URLClassLoader(URL[] urls) {	super();	// this is to make the stack depth consistent with 1.1	SecurityManager security = System.getSecurityManager();	if (security != null) {	    security.checkCreateClassLoader();	}	ucp = new URLClassPath(urls);	acc = AccessController.getContext();    }    /**     * Constructs a new URLClassLoader for the specified URLs, parent     * class loader, and URLStreamHandlerFactory. The parent argument     * will be used as the parent class loader for delegation. The     * factory argument will be used as the stream handler factory to     * obtain protocol handlers when creating new URLs.     *     * <p>If there is a security manager, this method first     * calls the security manager's <code>checkCreateClassLoader</code> method     * to ensure creation of a class loader is allowed.     *     * @param urls the URLs from which to load classes and resources     * @param parent the parent class loader for delegation     * @param factory the URLStreamHandlerFactory to use when creating URLs     *     * @exception  SecurityException  if a security manager exists and its       *             <code>checkCreateClassLoader</code> method doesn't allow      *             creation of a class loader.     * @see SecurityManager#checkCreateClassLoader     */    public URLClassLoader(URL[] urls, ClassLoader parent,			  URLStreamHandlerFactory factory) {	super(parent);	// this is to make the stack depth consistent with 1.1	SecurityManager security = System.getSecurityManager();	if (security != null) {	    security.checkCreateClassLoader();	}	ucp = new URLClassPath(urls, factory);	acc = AccessController.getContext();    }    /**     * Appends the specified URL to the list of URLs to search for     * classes and resources.     *     * @param url the URL to be added to the search path of URLs     */    protected void addURL(URL url) {	ucp.addURL(url);    }    /**     * Returns the search path of URLs for loading classes and resources.     * This includes the original list of URLs specified to the constructor,     * along with any URLs subsequently appended by the addURL() method.     * @return the search path of URLs for loading classes and resources.     */    public URL[] getURLs() {	return ucp.getURLs();    }    /**     * Finds and loads the class with the specified name from the URL search     * path. Any URLs referring to JAR files are loaded and opened as needed     * until the class is found.     *     * @param name the name of the class     * @return the resulting class     * @exception ClassNotFoundException if the class could not be found     */    protected Class findClass(final String name)	 throws ClassNotFoundException    {	Class retValue = null;	try {	    retValue = (Class)		AccessController.doPrivileged(new PrivilegedExceptionAction() {		    /*		     * Formerly, this method would throw an exception if		     * resource res == null. This would be wrapped in		     * a PrivilegedActionException then unwrapped in 		     * the body of findClass (as it still can be).		     * Fewer exceptions need to be allocated if we return null.		     */		    public Object run() throws ClassNotFoundException {			String path = name.replace('.', '/').concat(".class");			Resource res = ucp.getResource(path, false);			if (res != null) {			    try {				return defineClass(name, res);			    } catch (IOException e) {				throw new ClassNotFoundException(name, e);			    }			}			return null;		    }		}, acc);	} catch (java.security.PrivilegedActionException pae) {	    throw (ClassNotFoundException) pae.getException();	}	if (retValue == null){	    throw new ClassNotFoundException(name);	}	return retValue;    }    /*     * Defines a Class using the class bytes obtained from the specified     * Resource. The resulting Class must be resolved before it can be     * used.     */    private Class defineClass(String name, Resource res) throws IOException {	int i = name.lastIndexOf('.');	URL url = res.getCodeSourceURL();	if (i != -1) {	    String pkgname = name.substring(0, i);	    // Check if package already loaded.	    Package pkg = getPackage(pkgname);	    Manifest man = res.getManifest();	    if (pkg != null) {		// Package found, so check package sealing.		if (pkg.isSealed()) {		    // Verify that code source URL is the same.		    if (!pkg.isSealed(url)) {			throw new SecurityException(			    "sealing violation: package " + pkgname + " is sealed");		    }		} else {		    // Make sure we are not attempting to seal the package		    // at this code source URL.		    if ((man != null) && isSealed(pkgname, man)) {			throw new SecurityException(			    "sealing violation: can't seal package " + pkgname + 			    ": already loaded");		    }		}	    } else {		if (man != null) {		    definePackage(pkgname, man, url);		} else {                    definePackage(pkgname, null, null, null, null, null, null, null);                }	    }	}	// Now read the class bytes and define the class	byte[] b = res.getBytes();	java.security.cert.Certificate[] certs = res.getCertificates();	CodeSource cs = new CodeSource(url, certs);	return defineClass(name, b, 0, b.length, cs);    }    /**     * Defines a new package by name in this ClassLoader. The attributes     * contained in the specified Manifest will be used to obtain package     * version and sealing information. For sealed packages, the additional     * URL specifies the code source URL from which the package was loaded.     *     * @param name  the package name     * @param man   the Manifest containing package version and sealing     *              information     * @param url   the code source url for the package, or null if none     * @exception   IllegalArgumentException if the package name duplicates     *              an existing package either in this class loader or one     *              of its ancestors     * @return the newly defined Package object     */    protected Package definePackage(String name, Manifest man, URL url)	throws IllegalArgumentException    {	String path = name.replace('.', '/').concat("/");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?