📄 urlclassloader.java
字号:
Certificate[] getCertificates() { // We have to get the entry from the jar file again, because the // certificates will not be available until the entire entry has // been read. return ((JarEntry) ((JarURLLoader) loader).jarfile.getEntry(name)) .getCertificates(); } URL getURL() { try { return new URL(((JarURLLoader) loader).baseJarURL, name, loader.classloader.getURLStreamHandler("jar")); } catch (MalformedURLException e) { InternalError ie = new InternalError(); ie.initCause(e); throw ie; } } } /** * Loader for remote directories. */ static final class RemoteURLLoader extends URLLoader { private final String protocol; RemoteURLLoader(URLClassLoader classloader, URL url) { super(classloader, url); protocol = url.getProtocol(); } /** * Get a remote resource. * Returns null if no such resource exists. */ Resource getResource(String name) { try { URL url = new URL(baseURL, name, classloader.getURLStreamHandler(protocol)); URLConnection connection = url.openConnection(); // Open the connection and check the stream // just to be sure it exists. int length = connection.getContentLength(); InputStream stream = connection.getInputStream(); // We can do some extra checking if it is a http request if (connection instanceof HttpURLConnection) { int response = ((HttpURLConnection) connection).getResponseCode(); if (response / 100 != 2) return null; } if (stream != null) return new RemoteResource(this, name, url, stream, length); else return null; } catch (IOException ioe) { return null; } } } /** * A resource from some remote location. */ static final class RemoteResource extends Resource { private final URL url; private final InputStream stream; private final int length; RemoteResource(RemoteURLLoader loader, String name, URL url, InputStream stream, int length) { super(loader); this.url = url; this.stream = stream; this.length = length; } InputStream getInputStream() throws IOException { return stream; } public int getLength() { return length; } public URL getURL() { return url; } } /** * A <code>FileURLLoader</code> is a type of <code>URLLoader</code> * only loading from file url. */ static final class FileURLLoader extends URLLoader { File dir; //the file for this file url FileURLLoader(URLClassLoader classloader, URL url) { super(classloader, url); dir = new File(baseURL.getFile()); } /** get resource with the name "name" in the file url */ Resource getResource(String name) { try { File file = new File(dir, name).getCanonicalFile(); if (file.exists() && !file.isDirectory()) return new FileResource(this, file); } catch (IOException e) { // Fall through... } return null; } } static final class FileResource extends Resource { final File file; FileResource(FileURLLoader loader, File file) { super(loader); this.file = file; } InputStream getInputStream() throws IOException { return new FileInputStream(file); } public int getLength() { return (int) file.length(); } public URL getURL() { try { return file.toURL(); } catch (MalformedURLException e) { InternalError ie = new InternalError(); ie.initCause(e); throw ie; } } } // Constructors /** * Creates a URLClassLoader that gets classes from the supplied URLs. * To determine if this classloader may be created the constructor of * the super class (<code>SecureClassLoader</code>) is called first, which * can throw a SecurityException. Then the supplied URLs are added * in the order given to the URLClassLoader which uses these URLs to * load classes and resources (after using the default parent ClassLoader). * * @param urls Locations that should be searched by this ClassLoader when * resolving Classes or Resources. * @exception SecurityException if the SecurityManager disallows the * creation of a ClassLoader. * @see SecureClassLoader */ public URLClassLoader(URL[] urls) throws SecurityException { super(); this.factory = null; this.securityContext = null; addURLs(urls); } /** * Creates a <code>URLClassLoader</code> that gets classes from the supplied * <code>URL</code>s. * To determine if this classloader may be created the constructor of * the super class (<code>SecureClassLoader</code>) is called first, which * can throw a SecurityException. Then the supplied URLs are added * in the order given to the URLClassLoader which uses these URLs to * load classes and resources (after using the supplied parent ClassLoader). * @param urls Locations that should be searched by this ClassLoader when * resolving Classes or Resources. * @param parent The parent class loader used before trying this class * loader. * @exception SecurityException if the SecurityManager disallows the * creation of a ClassLoader. * @exception SecurityException * @see SecureClassLoader */ public URLClassLoader(URL[] urls, ClassLoader parent) throws SecurityException { super(parent); this.factory = null; this.securityContext = null; addURLs(urls); } // Package-private to avoid a trampoline constructor. /** * Package-private constructor used by the static * <code>newInstance(URL[])</code> method. Creates an * <code>URLClassLoader</code> with the given parent but without any * <code>URL</code>s yet. This is used to bypass the normal security * check for creating classloaders, but remembers the security * context which will be used when defining classes. The * <code>URL</code>s to load from must be added by the * <code>newInstance()</code> method in the security context of the * caller. * * @param securityContext the security context of the unprivileged code. */ URLClassLoader(ClassLoader parent, AccessControlContext securityContext) { super(parent); this.factory = null; this.securityContext = securityContext; } /** * Creates a URLClassLoader that gets classes from the supplied URLs. * To determine if this classloader may be created the constructor of * the super class (<CODE>SecureClassLoader</CODE>) is called first, which * can throw a SecurityException. Then the supplied URLs are added * in the order given to the URLClassLoader which uses these URLs to * load classes and resources (after using the supplied parent ClassLoader). * It will use the supplied <CODE>URLStreamHandlerFactory</CODE> to get the * protocol handlers of the supplied URLs. * @param urls Locations that should be searched by this ClassLoader when * resolving Classes or Resources. * @param parent The parent class loader used before trying this class * loader. * @param factory Used to get the protocol handler for the URLs. * @exception SecurityException if the SecurityManager disallows the * creation of a ClassLoader. * @exception SecurityException * @see SecureClassLoader */ public URLClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) throws SecurityException { super(parent); this.securityContext = null; this.factory = factory; addURLs(urls); // If this factory is still not in factoryCache, add it, // since we only support three protocols so far, 5 is enough // for cache initial size synchronized (factoryCache) { if (factory != null && factoryCache.get(factory) == null) factoryCache.put(factory, new HashMap(5)); } } // Methods /** * Adds a new location to the end of the internal URL store. * @param newUrl the location to add */ protected void addURL(URL newUrl) { urls.add(newUrl); addURLImpl(newUrl); } private void addURLImpl(URL newUrl) { synchronized (this) { if (newUrl == null) return; // Silently ignore... // Reset the toString() value. thisString = null; // Check global cache to see if there're already url loader // for this url. URLLoader loader = (URLLoader) urlloaders.get(newUrl); if (loader == null) { String file = newUrl.getFile(); String protocol = newUrl.getProtocol(); // Check that it is not a directory if (! (file.endsWith("/") || file.endsWith(File.separator))) loader = new JarURLLoader(this, newUrl); else if ("file".equals(protocol)) loader = new FileURLLoader(this, newUrl); else loader = new RemoteURLLoader(this, newUrl); // Cache it. urlloaders.put(newUrl, loader); } urlinfos.add(loader); Vector extraUrls = loader.getClassPath(); if (extraUrls != null) { Iterator it = extraUrls.iterator(); while (it.hasNext()) { URL url = (URL)it.next(); URLLoader extraLoader = (URLLoader) urlloaders.get(url); if (! urlinfos.contains (extraLoader)) addURLImpl(url); } } } } /** * Adds an array of new locations to the end of the internal URL * store. Called from the the constructors. Should not call to the * protected addURL() method since that can be overridden and * subclasses are not yet in a good state at this point. * jboss 4.0.3 for example depends on this. * * @param newUrls the locations to add */ private void addURLs(URL[] newUrls) { for (int i = 0; i < newUrls.length; i++) { urls.add(newUrls[i]); addURLImpl(newUrls[i]); } } /** * Look in both Attributes for a given value. The first Attributes * object, if not null, has precedence. */ private String getAttributeValue(Attributes.Name name, Attributes first, Attributes second) { String result = null; if (first != null) result = first.getValue(name); if (result == null) result = second.getValue(name); return result; } /** * Defines a Package based on the given name and the supplied manifest * information. The manifest indicates the title, version and * vendor information of the specification and implementation and whether the * package is sealed. If the Manifest indicates that the package is sealed * then the Package will be sealed with respect to the supplied URL. * * @param name The name of the package * @param manifest The manifest describing the specification, * implementation and sealing details of the package * @param url the code source url to seal the package * @return the defined Package * @throws IllegalArgumentException If this package name already exists * in this class loader */ protected Package definePackage(String name, Manifest manifest, URL url) throws IllegalArgumentException { // Compute the name of the package as it may appear in the // Manifest. StringBuffer xform = new StringBuffer(name); for (int i = xform.length () - 1; i >= 0; --i) if (xform.charAt(i) == '.') xform.setCharAt(i, '/'); xform.append('/'); String xformName = xform.toString(); Attributes entryAttr = manifest.getAttributes(xformName); Attributes attr = manifest.getMainAttributes(); String specTitle = getAttributeValue(Attributes.Name.SPECIFICATION_TITLE,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -