Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

java.net
Class URLClassLoader  view URLClassLoader download URLClassLoader.java

java.lang.Object
  extended byjava.lang.ClassLoader
      extended byjava.security.SecureClassLoader
          extended byjava.net.URLClassLoader

public class URLClassLoader
extends java.security.SecureClassLoader

A secure class loader that can load classes and resources from multiple locations. Given an array of URLs this class loader will retrieve classes and resources by fetching them from possible remote locations. Each URL is searched in order in which it was added. If the file portion of the URL ends with a '/' character then it is interpreted as a base directory, otherwise it is interpreted as a jar file from which the classes/resources are resolved.

New instances can be created by two static newInstance() methods or by three public contructors. Both ways give the option to supply an initial array of URLs and (optionally) a parent classloader (that is different from the standard system class loader).

Normally creating a URLClassLoader throws a SecurityException if a SecurityManager is installed and the checkCreateClassLoader() method does not return true. But the newInstance() methods may be used by any code as long as it has permission to acces the given URLs. URLClassLoaders created by the newInstance() methods also explicitly call the checkPackageAccess() method of SecurityManager if one is installed before trying to load a class. Note that only subclasses of URLClassLoader can add new URLs after the URLClassLoader had been created. But it is always possible to get an array of all URLs that the class loader uses to resolve classes and resources by way of the getURLs() method.

Open issues:

Since:
1.2

Nested Class Summary
(package private) static class URLClassLoader.FileResource
           
(package private) static class URLClassLoader.FileURLLoader
          A FileURLLoader is a type of URLLoader only loading from file url.
(package private) static class URLClassLoader.JarURLLoader
          A JarURLLoader is a type of URLLoader only loading from jar url.
(package private) static class URLClassLoader.JarURLResource
           
(package private) static class URLClassLoader.RemoteResource
          A resource from some remote location.
(package private) static class URLClassLoader.RemoteURLLoader
          Loader for remote directories.
(package private) static class URLClassLoader.Resource
          A Resource represents a resource in some URLLoader.
(package private) static class URLClassLoader.URLLoader
          A URLLoader contains all logic to load resources from a given base URL.
 
Nested classes inherited from class java.lang.ClassLoader
 
Field Summary
private  URLStreamHandlerFactory factory
          Factory used to get the protocol handlers of the URLs
private static java.util.HashMap factoryCache
          A cache to store mappings between handler factory and its private protocol handler cache (also a HashMap), so we can avoid create handlers each time the same protocol comes.
private  java.security.AccessControlContext securityContext
          The security context when created from newInstance() or null when created through a normal constructor or when no SecurityManager was installed.
private  java.lang.String thisString
           
private  java.util.Vector urlinfos
          Store pre-parsed information for each url into this vector: each element is a URL loader.
private static java.util.HashMap urlloaders
          A global cache to store mappings between URLLoader and URL, so we can avoid do all the homework each time the same URL comes.
private  java.util.Vector urls
          Locations to load classes from
 
Fields inherited from class java.security.SecureClassLoader
 
Fields inherited from class java.lang.ClassLoader
 
Constructor Summary
(package private) URLClassLoader(java.lang.ClassLoader parent, java.security.AccessControlContext securityContext)
          Package-private constructor used by the static newInstance(URL[]) method.
  URLClassLoader(URL[] urls)
          Creates a URLClassLoader that gets classes from the supplied URLs.
  URLClassLoader(URL[] urls, java.lang.ClassLoader parent)
          Creates a URLClassLoader that gets classes from the supplied URLs.
  URLClassLoader(URL[] urls, java.lang.ClassLoader parent, URLStreamHandlerFactory factory)
          Creates a URLClassLoader that gets classes from the supplied URLs.
 
Method Summary
protected  void addURL(URL newUrl)
          Adds a new location to the end of the internal URL store.
private  void addURLImpl(URL newUrl)
           
private  void addURLs(URL[] newUrls)
          Adds an array of new locations to the end of the internal URL store.
protected  java.lang.Package definePackage(java.lang.String name, java.util.jar.Manifest manifest, URL url)
          Defines a Package based on the given name and the supplied manifest information.
protected  java.lang.Class findClass(java.lang.String className)
          Finds (the first) class by name from one of the locations.
 URL findResource(java.lang.String resourceName)
          Finds the first occurrence of a resource that can be found.
 java.util.Enumeration findResources(java.lang.String resourceName)
          Finds all the resources with a particular name from all the locations.
private  URLClassLoader.Resource findURLResource(java.lang.String resourceName)
          Finds the first occurrence of a resource that can be found.
private  java.lang.String getAttributeValue(java.util.jar.Attributes.Name name, java.util.jar.Attributes first, java.util.jar.Attributes second)
          Look in both Attributes for a given value.
protected  java.security.PermissionCollection getPermissions(java.security.CodeSource source)
          Returns the permissions needed to access a particular code source.
 URL[] getURLs()
          Returns all the locations that this class loader currently uses the resolve classes and resource.
(package private)  URLStreamHandler getURLStreamHandler(java.lang.String protocol)
          If the URLStreamHandlerFactory has been set this return the appropriate URLStreamHandler for the given protocol, if not set returns null.
static URLClassLoader newInstance(URL[] urls)
          Creates a new instance of a URLClassLoader that gets classes from the supplied URLs.
static URLClassLoader newInstance(URL[] urls, java.lang.ClassLoader parent)
          Creates a new instance of a URLClassLoader that gets classes from the supplied URLs and with the supplied loader as parent class loader.
 java.lang.String toString()
          Returns a String representation of this URLClassLoader giving the actual Class name, the URLs that are searched and the parent ClassLoader.
 
Methods inherited from class java.security.SecureClassLoader
defineClass
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

urlloaders

private static java.util.HashMap urlloaders
A global cache to store mappings between URLLoader and URL, so we can avoid do all the homework each time the same URL comes. XXX - Keeps these loaders forever which prevents garbage collection.


factoryCache

private static java.util.HashMap factoryCache
A cache to store mappings between handler factory and its private protocol handler cache (also a HashMap), so we can avoid create handlers each time the same protocol comes.


urls

private final java.util.Vector urls
Locations to load classes from


urlinfos

private final java.util.Vector urlinfos
Store pre-parsed information for each url into this vector: each element is a URL loader. A jar file has its own class-path attribute which adds to the URLs that will be searched, but this does not add to the list of urls.


factory

private final URLStreamHandlerFactory factory
Factory used to get the protocol handlers of the URLs


securityContext

private final java.security.AccessControlContext securityContext
The security context when created from newInstance() or null when created through a normal constructor or when no SecurityManager was installed.


thisString

private java.lang.String thisString
Constructor Detail

URLClassLoader

public URLClassLoader(URL[] urls)
               throws java.lang.SecurityException
Creates a URLClassLoader that gets classes from the supplied URLs. To determine if this classloader may be created the constructor of the super class (SecureClassLoader) 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).


URLClassLoader

public URLClassLoader(URL[] urls,
                      java.lang.ClassLoader parent)
               throws java.lang.SecurityException
Creates a URLClassLoader that gets classes from the supplied URLs. To determine if this classloader may be created the constructor of the super class (SecureClassLoader) 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).


URLClassLoader

URLClassLoader(java.lang.ClassLoader parent,
               java.security.AccessControlContext securityContext)
Package-private constructor used by the static newInstance(URL[]) method. Creates an URLClassLoader with the given parent but without any URLs 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 URLs to load from must be added by the newInstance() method in the security context of the caller.


URLClassLoader

public URLClassLoader(URL[] urls,
                      java.lang.ClassLoader parent,
                      URLStreamHandlerFactory factory)
               throws java.lang.SecurityException
Creates a URLClassLoader that gets classes from the supplied URLs. To determine if this classloader may be created the constructor of the super class (SecureClassLoader) 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 URLStreamHandlerFactory to get the protocol handlers of the supplied URLs.

Method Detail

addURL

protected void addURL(URL newUrl)
Adds a new location to the end of the internal URL store.


addURLImpl

private void addURLImpl(URL newUrl)

addURLs

private void addURLs(URL[] newUrls)
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.


getAttributeValue

private java.lang.String getAttributeValue(java.util.jar.Attributes.Name name,
                                           java.util.jar.Attributes first,
                                           java.util.jar.Attributes second)
Look in both Attributes for a given value. The first Attributes object, if not null, has precedence.


definePackage

protected java.lang.Package definePackage(java.lang.String name,
                                          java.util.jar.Manifest manifest,
                                          URL url)
                                   throws java.lang.IllegalArgumentException
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.


findClass

protected java.lang.Class findClass(java.lang.String className)
                             throws java.lang.ClassNotFoundException
Finds (the first) class by name from one of the locations. The locations are searched in the order they were added to the URLClassLoader.


toString

public java.lang.String toString()
Returns a String representation of this URLClassLoader giving the actual Class name, the URLs that are searched and the parent ClassLoader.


findURLResource

private URLClassLoader.Resource findURLResource(java.lang.String resourceName)
Finds the first occurrence of a resource that can be found. The locations are searched in the order they were added to the URLClassLoader.


findResource

public URL findResource(java.lang.String resourceName)
Finds the first occurrence of a resource that can be found.


getURLStreamHandler

URLStreamHandler getURLStreamHandler(java.lang.String protocol)
If the URLStreamHandlerFactory has been set this return the appropriate URLStreamHandler for the given protocol, if not set returns null.


findResources

public java.util.Enumeration findResources(java.lang.String resourceName)
                                    throws java.io.IOException
Finds all the resources with a particular name from all the locations.


getPermissions

protected java.security.PermissionCollection getPermissions(java.security.CodeSource source)
Returns the permissions needed to access a particular code source. These permissions includes those returned by SecureClassLoader.getPermissions() and the actual permissions to access the objects referenced by the URL of the code source. The extra permissions added depend on the protocol and file portion of the URL in the code source. If the URL has the "file" protocol ends with a '/' character then it must be a directory and a file Permission to read everything in that directory and all subdirectories is added. If the URL had the "file" protocol and doesn't end with a '/' character then it must be a normal file and a file permission to read that file is added. If the URL has any other protocol then a socket permission to connect and accept connections from the host portion of the URL is added.


getURLs

public URL[] getURLs()
Returns all the locations that this class loader currently uses the resolve classes and resource. This includes both the initially supplied URLs as any URLs added later by the loader.


newInstance

public static URLClassLoader newInstance(URL[] urls)
                                  throws java.lang.SecurityException
Creates a new instance of a URLClassLoader that gets classes from the supplied URLs. This class loader will have as parent the standard system class loader.


newInstance

public static URLClassLoader newInstance(URL[] urls,
                                         java.lang.ClassLoader parent)
                                  throws java.lang.SecurityException
Creates a new instance of a URLClassLoader that gets classes from the supplied URLs and with the supplied loader as parent class loader.