org.apache.catalina.loader

Class StandardClassLoader

Implemented Interfaces:
Reloader, StandardClassLoaderMBean

public class StandardClassLoader
extends URLClassLoader
implements Reloader, StandardClassLoaderMBean

Subclass implementation of java.net.URLClassLoader that knows how to load classes from disk directories, as well as local and remote JAR files. It also implements the Reloader interface, to provide automatic reloading support to the associated loader.

In all cases, URLs must conform to the contract specified by URLClassLoader - any URL that ends with a "/" character is assumed to represent a directory; all other URLs are assumed to be the address of a JAR file.

IMPLEMENTATION NOTE - Local repositories are searched in the order they are added via the initial constructor and/or any subsequent calls to addRepository().

IMPLEMENTATION NOTE - At present, there are no dependencies from this class to any other Catalina class, so that it could be used independently.

Version:
$Revision: 1.13.2.1 $ $Date: 2004/09/01 15:02:34 $

Authors:
Craig R. McClanahan
Remy Maucherat

Field Summary

protected int
debug
The debugging detail level of this component.
protected boolean
delegate
Should this class loader delegate to the parent class loader before searching its own repositories (i.e.
protected URLStreamHandlerFactory
factory
URL stream handler for additional protocols.
protected String[]
repositories
The list of local repositories, in the order they should be searched for locally loaded classes or resources.

Constructor Summary

StandardClassLoader()
Construct a new ClassLoader with no defined repositories and no parent ClassLoader.
StandardClassLoader(ClassLoader parent)
Construct a new ClassLoader with no defined repositories and the specified parent ClassLoader.
StandardClassLoader(ClassLoader parent, URLStreamHandlerFactory factory)
Construct a new ClassLoader with no defined repositories and the specified parent ClassLoader.
StandardClassLoader(URLStreamHandlerFactory factory)
Construct a new ClassLoader with no defined repositories and no parent ClassLoader, but with a stream handler factory.
StandardClassLoader(repositories[] )
Construct a new ClassLoader with the specified repositories and no parent ClassLoader.
StandardClassLoader(repositories[] , ClassLoader parent)
Construct a new ClassLoader with the specified repositories and parent ClassLoader.
StandardClassLoader(repositories[] , ClassLoader parent)
Construct a new ClassLoader with the specified repositories and parent ClassLoader.

Method Summary

void
addRepository(String repository)
Add a new repository to the set of places this ClassLoader can look for classes to be loaded.
protected void
addRepositoryInternal(String repository)
Add a repository to our internal array only.
protected static URL[]
convert(input[] )
Convert an array of String to an array of URL and return it.
protected static URL[]
convert(input[] , URLStreamHandlerFactory factory)
Convert an array of String to an array of URL and return it.
Class
findClass(String name)
Find the specified class in our local repositories, if possible.
protected InputStream
findLoadedResource(String name)
Finds the resource with the given name if it has previously been loaded and cached by this class loader, and return an input stream to the resource data.
String[]
findRepositories()
Return a String array of the current repositories for this class loader.
URL
findResource(String name)
Find the specified resource in our local repository, and return a URL refering to it, or null if this resource cannot be found.
Enumeration
findResources(String name)
Return an enumeration of URLs representing all of the resources with the given name.
int
getDebug()
Return the debugging detail level for this component.
boolean
getDelegate()
Return the "delegate first" flag for this class loader.
protected PermissionCollection
getPermissions(CodeSource codeSource)
Get the Permissions for a CodeSource.
URL
getResource(String name)
Find the resource with the given name.
InputStream
getResourceAsStream(String name)
Find the resource with the given name, and return an input stream that can be used for reading it.
Class
loadClass(String name)
Load the class with the specified name.
Class
loadClass(String name, boolean resolve)
Load the class with the specified name, searching using the following algorithm until it finds and returns the class.
boolean
modified()
This class loader doesn't check for reloading.
protected static String
parseProtocol(String spec)
Parse URL protocol.
void
setDebug(int debug)
Set the debugging detail level for this component.
void
setDelegate(boolean delegate)
Set the "delegate first" flag for this class loader.
protected void
setPermissions(String path)
If there is a Java SecurityManager create a read FilePermission or JndiPermission for the file directory path.
protected void
setPermissions(URL url)
If there is a Java SecurityManager add a read FilePermission or JndiPermission for URL.
String
toString()
Render a String representation of this object.

Field Details

debug

protected int debug
The debugging detail level of this component.


delegate

protected boolean delegate
Should this class loader delegate to the parent class loader before searching its own repositories (i.e. the usual Java2 delegation model)? If set to false, this class loader will search its own repositories first, and delegate to the parent only if the class or resource is not found locally.


factory

protected URLStreamHandlerFactory factory
URL stream handler for additional protocols.


repositories

protected String[] repositories
The list of local repositories, in the order they should be searched for locally loaded classes or resources.

Constructor Details

StandardClassLoader

public StandardClassLoader()
Construct a new ClassLoader with no defined repositories and no parent ClassLoader.


StandardClassLoader

public StandardClassLoader(ClassLoader parent)
Construct a new ClassLoader with no defined repositories and the specified parent ClassLoader.

Parameters:
parent - The parent ClassLoader


StandardClassLoader

public StandardClassLoader(ClassLoader parent,
                           URLStreamHandlerFactory factory)
Construct a new ClassLoader with no defined repositories and the specified parent ClassLoader.

Parameters:
parent - The parent ClassLoader
factory - the URLStreamHandlerFactory to use when creating URLs


StandardClassLoader

public StandardClassLoader(URLStreamHandlerFactory factory)
Construct a new ClassLoader with no defined repositories and no parent ClassLoader, but with a stream handler factory.

Parameters:
factory - the URLStreamHandlerFactory to use when creating URLs


StandardClassLoader

public StandardClassLoader(repositories[] )
Construct a new ClassLoader with the specified repositories and no parent ClassLoader.

Parameters:


StandardClassLoader

public StandardClassLoader(repositories[] ,
                           ClassLoader parent)
Construct a new ClassLoader with the specified repositories and parent ClassLoader.

Parameters:
parent - The parent ClassLoader


StandardClassLoader

public StandardClassLoader(repositories[] ,
                           ClassLoader parent)
Construct a new ClassLoader with the specified repositories and parent ClassLoader.

Parameters:
parent - The parent ClassLoader

Method Details

addRepository

public void addRepository(String repository)
Add a new repository to the set of places this ClassLoader can look for classes to be loaded.
Specified by:
addRepository in interface Reloader

Parameters:
repository - Name of a source of classes to be loaded, such as a directory pathname, a JAR file pathname, or a ZIP file pathname


addRepositoryInternal

protected void addRepositoryInternal(String repository)
Add a repository to our internal array only.

Parameters:
repository - The new repository


convert

protected static URL[] convert(input[] )
Convert an array of String to an array of URL and return it.

Parameters:


convert

protected static URL[] convert(input[] ,
                               URLStreamHandlerFactory factory)
Convert an array of String to an array of URL and return it.

Parameters:
factory - Handler factory to use to generate the URLs


findClass

public Class findClass(String name)
            throws ClassNotFoundException
Find the specified class in our local repositories, if possible. If not found, throw ClassNotFoundException.

Parameters:
name - Name of the class to be loaded


findLoadedResource

protected InputStream findLoadedResource(String name)
Finds the resource with the given name if it has previously been loaded and cached by this class loader, and return an input stream to the resource data. If this resource has not been cached, return null.

Parameters:
name - Name of the resource to return


findRepositories

public String[] findRepositories()
Return a String array of the current repositories for this class loader. If there are no repositories, a zero-length array is returned. For security reason, returns a clone of the Array (since String are immutable).
Specified by:
findRepositories in interface Reloader


findResource

public URL findResource(String name)
Find the specified resource in our local repository, and return a URL refering to it, or null if this resource cannot be found.

Parameters:
name - Name of the resource to be found


findResources

public Enumeration findResources(String name)
            throws IOException
Return an enumeration of URLs representing all of the resources with the given name. If no resources with this name are found, return an empty enumeration.

Parameters:
name - Name of the resources to be found


getDebug

public int getDebug()
Return the debugging detail level for this component.


getDelegate

public boolean getDelegate()
Return the "delegate first" flag for this class loader.


getPermissions

protected final PermissionCollection getPermissions(CodeSource codeSource)
Get the Permissions for a CodeSource. If this instance of StandardClassLoader is for a web application context, add read FilePermissions for the base directory (if unpacked), the context URL, and jar file resources.

Parameters:
codeSource - where the code was loaded from

Returns:
PermissionCollection for CodeSource


getResource

public URL getResource(String name)
Find the resource with the given name. A resource is some data (images, audio, text, etc.) that can be accessed by class code in a way that is independent of the location of the code. The name of a resource is a "/"-separated path name that identifies the resource. If the resource cannot be found, return null.

This method searches according to the following algorithm, returning as soon as it finds the appropriate URL. If the resource cannot be found, returns null.

  • If the delegate property is set to true, call the getResource() method of the parent class loader, if any.
  • Call findResource() to find this resource in our locally defined repositories.
  • Call the getResource() method of the parent class loader, if any.

Parameters:
name - Name of the resource to return a URL for


getResourceAsStream

public InputStream getResourceAsStream(String name)
Find the resource with the given name, and return an input stream that can be used for reading it. The search order is as described for getResource(), after checking to see if the resource data has been previously cached. If the resource cannot be found, return null.

Parameters:
name - Name of the resource to return an input stream for


loadClass

public Class loadClass(String name)
            throws ClassNotFoundException
Load the class with the specified name. This method searches for classes in the same manner as loadClass(String, boolean) with false as the second argument.

Parameters:
name - Name of the class to be loaded


loadClass

public Class loadClass(String name,
                       boolean resolve)
            throws ClassNotFoundException
Load the class with the specified name, searching using the following algorithm until it finds and returns the class. If the class cannot be found, returns ClassNotFoundException.
  • Call findLoadedClass(String) to check if the class has already been loaded. If it has, the same Class object is returned.
  • If the delegate property is set to true, call the loadClass() method of the parent class loader, if any.
  • Call findClass() to find this class in our locally defined repositories.
  • Call the loadClass() method of our parent class loader, if any.
If the class was found using the above steps, and the resolve flag is true, this method will then call resolveClass(Class) on the resulting Class object.

Parameters:
name - Name of the class to be loaded
resolve - If true then resolve the class


modified

public boolean modified()
This class loader doesn't check for reloading.
Specified by:
modified in interface Reloader


parseProtocol

protected static String parseProtocol(String spec)
Parse URL protocol.

Returns:
String protocol


setDebug

public void setDebug(int debug)
Set the debugging detail level for this component.

Parameters:
debug - The new debugging detail level


setDelegate

public void setDelegate(boolean delegate)
Set the "delegate first" flag for this class loader.

Parameters:
delegate - The new "delegate first" flag


setPermissions

protected void setPermissions(String path)
If there is a Java SecurityManager create a read FilePermission or JndiPermission for the file directory path.

Parameters:
path - file directory path


setPermissions

protected void setPermissions(URL url)
If there is a Java SecurityManager add a read FilePermission or JndiPermission for URL.

Parameters:
url - URL for a file or directory on local system


toString

public String toString()
Render a String representation of this object.


Copyright B) 2000-2003 Apache Software Foundation. All Rights Reserved.