org.apache.catalina.loader
Class WebappClassLoader

java.lang.Object
  |
  +--java.lang.ClassLoader
        |
        +--java.security.SecureClassLoader
              |
              +--java.net.URLClassLoader
                    |
                    +--org.apache.catalina.loader.WebappClassLoader
All Implemented Interfaces:
Lifecycle, Reloader

public class WebappClassLoader
extends java.net.URLClassLoader
implements Reloader, Lifecycle

Specialized web application class loader.

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

IMPLEMENTATION NOTE - No check for sealing violations or security is made unless a security manager is present.

FIXME - Implement findResources.

Version:
$Revision: 1.15.2.5 $ $Date: 2001/10/11 20:56:57 $
Author:
Remy Maucherat, Craig R. McClanahan

Inner Class Summary
protected  class WebappClassLoader.PrivilegedFindResource
           
protected static class WebappClassLoader.ResourceEntry
          Resource entry.
 
Field Summary
protected  java.util.ArrayList available
          The set of optional packages (formerly standard extensions) that are available in the repositories associated with this class loader.
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  java.io.File[] files
          Repositories translated as path in the work directory (for Jasper originally), but which is used to generate fake URLs should getURLs be called.
protected  java.util.jar.JarFile[] jarFiles
          The list of JARs, in the order they should be searched for locally loaded classes or resources.
protected  java.lang.String[] jarNames
          The list of JARs, in the order they should be searched for locally loaded classes or resources.
protected  java.lang.String jarPath
          The path which will be monitored for added Jar files.
protected  java.io.File[] jarRealFiles
          The list of JARs, in the order they should be searched for locally loaded classes or resources.
protected  long[] lastModifiedDates
          The list of JARs last modified dates, in the order they should be searched for locally loaded classes or resources.
protected  java.util.HashMap notFoundResources
          The list of not found resources.
protected  java.lang.String[] paths
          The list of resources which should be checked when checking for modifications.
protected  java.lang.String[] repositories
          The list of local repositories, in the order they should be searched for locally loaded classes or resources.
protected  java.util.ArrayList required
          The set of optional packages (formerly standard extensions) that are required in the repositories associated with this class loader.
protected  java.util.HashMap resourceEntries
          The cache of ResourceEntry for classes and resources we have loaded, keyed by resource name.
protected  javax.naming.directory.DirContext resources
          Associated directory context giving access to the resources in this webapp.
protected  boolean started
          Has this component been started?
 
Fields inherited from interface org.apache.catalina.Lifecycle
START_EVENT, STOP_EVENT
 
Constructor Summary
WebappClassLoader(java.lang.ClassLoader parent, javax.naming.directory.DirContext resources)
          Construct a new ClassLoader with no defined repositories and no parent ClassLoader.
WebappClassLoader(javax.naming.directory.DirContext resources)
          Construct a new ClassLoader with no defined repositories and no parent ClassLoader.
 
Method Summary
 void addLifecycleListener(LifecycleListener listener)
          Add a lifecycle event listener to this component.
 void addRepository(java.lang.String repository)
          Add a new repository to the set of places this ClassLoader can look for classes to be loaded.
 Extension[] findAvailable()
          Return a list of "optional packages" (formerly "standard extensions") that have been declared to be available in the repositories associated with this class loader, plus any parent class loader implemented with the same class.
 java.lang.Class findClass(java.lang.String name)
          Find the specified class in our local repositories, if possible.
protected  java.lang.Class findClassInternal(java.lang.String name)
          Find specified class in local repositories.
protected  java.lang.Class findLoadedClass0(java.lang.String name)
          Finds the class with the given name if it has previously been loaded and cached by this class loader, and return the Class object.
protected  java.io.InputStream findLoadedResource(java.lang.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.
 java.lang.String[] findRepositories()
          Return a String array of the current repositories for this class loader.
 Extension[] findRequired()
          Return a list of "optional packages" (formerly "standard extensions") that have been declared to be required in the repositories associated with this class loader, plus any parent class loader implemented with the same class.
 java.net.URL findResource(java.lang.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.
protected  WebappClassLoader.ResourceEntry findResourceInternal(java.lang.String name, java.lang.String path)
          Find specified resource in local repositories.
 java.util.Enumeration findResources(java.lang.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.
 java.lang.String getJarPath()
          Return the JAR path.
protected  java.security.PermissionCollection getPermissions(java.security.CodeSource codeSource)
          Get the Permissions for a CodeSource.
 java.net.URL getResource(java.lang.String name)
          Find the resource with the given name.
 java.io.InputStream getResourceAsStream(java.lang.String name)
          Find the resource with the given name, and return an input stream that can be used for reading it.
 java.net.URL[] getURLs()
          Returns the search path of URLs for loading classes and resources.
protected  boolean isPackageSealed(java.lang.String name, java.util.jar.Manifest man)
          Returns true if the specified package name is sealed according to the given manifest.
 java.lang.Class loadClass(java.lang.String name)
          Load the class with the specified name.
 java.lang.Class loadClass(java.lang.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()
          Have one or more classes or resources been modified so that a reload is appropriate?
protected  void refreshPolicy()
          Refresh the system policy file, to pick up eventual changes.
 void removeLifecycleListener(LifecycleListener listener)
          Remove a lifecycle event listener from this component.
 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.
 void setJarPath(java.lang.String jarPath)
          Change the Jar path.
 void setPermissions(java.lang.String path)
          If there is a Java SecurityManager create a read FilePermission or JndiPermission for the file directory path.
 void setPermissions(java.net.URL url)
          If there is a Java SecurityManager create a read FilePermission or JndiPermission for URL.
 void start()
          Start the class loader.
 void stop()
          Stop the class loader.
 java.lang.String toString()
          Render a String representation of this object.
protected  boolean validate(java.lang.String name)
          Validate a classname.
 
Methods inherited from class java.net.URLClassLoader
addURL, definePackage, newInstance, newInstance
 
Methods inherited from class java.security.SecureClassLoader
defineClass
 
Methods inherited from class java.lang.ClassLoader
defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, resolveClass, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

resources

protected javax.naming.directory.DirContext resources
Associated directory context giving access to the resources in this webapp.

available

protected java.util.ArrayList available
The set of optional packages (formerly standard extensions) that are available in the repositories associated with this class loader. Each object in this list is of type org.apache.catalina.loader.Extension.

resourceEntries

protected java.util.HashMap resourceEntries
The cache of ResourceEntry for classes and resources we have loaded, keyed by resource name.

notFoundResources

protected java.util.HashMap notFoundResources
The list of not found resources.

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.

repositories

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

files

protected java.io.File[] files
Repositories translated as path in the work directory (for Jasper originally), but which is used to generate fake URLs should getURLs be called.

jarFiles

protected java.util.jar.JarFile[] jarFiles
The list of JARs, in the order they should be searched for locally loaded classes or resources.

jarRealFiles

protected java.io.File[] jarRealFiles
The list of JARs, in the order they should be searched for locally loaded classes or resources.

jarPath

protected java.lang.String jarPath
The path which will be monitored for added Jar files.

jarNames

protected java.lang.String[] jarNames
The list of JARs, in the order they should be searched for locally loaded classes or resources.

lastModifiedDates

protected long[] lastModifiedDates
The list of JARs last modified dates, in the order they should be searched for locally loaded classes or resources.

paths

protected java.lang.String[] paths
The list of resources which should be checked when checking for modifications.

required

protected java.util.ArrayList required
The set of optional packages (formerly standard extensions) that are required in the repositories associated with this class loader. Each object in this list is of type org.apache.catalina.loader.Extension.

started

protected boolean started
Has this component been started?
Constructor Detail

WebappClassLoader

public WebappClassLoader(javax.naming.directory.DirContext resources)
Construct a new ClassLoader with no defined repositories and no parent ClassLoader.

WebappClassLoader

public WebappClassLoader(java.lang.ClassLoader parent,
                         javax.naming.directory.DirContext resources)
Construct a new ClassLoader with no defined repositories and no parent ClassLoader.
Method Detail

getDebug

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

setDebug

public void setDebug(int debug)
Set the debugging detail level for this component.
Parameters:
debug - The new debugging detail level

getDelegate

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

setDelegate

public void setDelegate(boolean delegate)
Set the "delegate first" flag for this class loader.
Parameters:
delegate - The new "delegate first" flag

setPermissions

public void setPermissions(java.lang.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

public void setPermissions(java.net.URL url)
If there is a Java SecurityManager create a read FilePermission or JndiPermission for URL.
Parameters:
url - URL for a file or directory on local system

getJarPath

public java.lang.String getJarPath()
Return the JAR path.

setJarPath

public void setJarPath(java.lang.String jarPath)
Change the Jar path.

addRepository

public void addRepository(java.lang.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
Throws:
java.lang.IllegalArgumentException - if the specified repository is invalid or does not exist

findAvailable

public Extension[] findAvailable()
Return a list of "optional packages" (formerly "standard extensions") that have been declared to be available in the repositories associated with this class loader, plus any parent class loader implemented with the same class.

findRepositories

public java.lang.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.
Specified by:
findRepositories in interface Reloader

findRequired

public Extension[] findRequired()
Return a list of "optional packages" (formerly "standard extensions") that have been declared to be required in the repositories associated with this class loader, plus any parent class loader implemented with the same class.

modified

public boolean modified()
Have one or more classes or resources been modified so that a reload is appropriate?
Specified by:
modified in interface Reloader

toString

public java.lang.String toString()
Render a String representation of this object.
Overrides:
toString in class java.lang.Object

findClass

public java.lang.Class findClass(java.lang.String name)
                          throws java.lang.ClassNotFoundException
Find the specified class in our local repositories, if possible. If not found, throw ClassNotFoundException.
Overrides:
findClass in class java.net.URLClassLoader
Parameters:
name - Name of the class to be loaded
Throws:
java.lang.ClassNotFoundException - if the class was not found

findResource

public java.net.URL findResource(java.lang.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.
Overrides:
findResource in class java.net.URLClassLoader
Parameters:
name - Name of the resource to be found

findResources

public java.util.Enumeration findResources(java.lang.String name)
                                    throws java.io.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.
Overrides:
findResources in class java.net.URLClassLoader
Parameters:
name - Name of the resources to be found
Throws:
java.io.IOException - if an input/output error occurs

getResource

public java.net.URL getResource(java.lang.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.

Overrides:
getResource in class java.lang.ClassLoader
Parameters:
name - Name of the resource to return a URL for

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.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.
Overrides:
getResourceAsStream in class java.lang.ClassLoader
Parameters:
name - Name of the resource to return an input stream for

loadClass

public java.lang.Class loadClass(java.lang.String name)
                          throws java.lang.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.
Overrides:
loadClass in class java.lang.ClassLoader
Parameters:
name - Name of the class to be loaded
Throws:
java.lang.ClassNotFoundException - if the class was not found

loadClass

public java.lang.Class loadClass(java.lang.String name,
                                 boolean resolve)
                          throws java.lang.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. 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.
Overrides:
loadClass in class java.lang.ClassLoader
Parameters:
name - Name of the class to be loaded
resolve - If true then resolve the class
Throws:
java.lang.ClassNotFoundException - if the class was not found

getPermissions

protected java.security.PermissionCollection getPermissions(java.security.CodeSource codeSource)
Get the Permissions for a CodeSource. If this instance of WebappClassLoader is for a web application context, add read FilePermission or JndiPermissions for the base directory (if unpacked), the context URL, and jar file resources.
Overrides:
getPermissions in class java.net.URLClassLoader
Parameters:
CodeSource - where the code was loaded from
Returns:
PermissionCollection for CodeSource

getURLs

public java.net.URL[] getURLs()
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.
Overrides:
getURLs in class java.net.URLClassLoader
Returns:
the search path of URLs for loading classes and resources.

addLifecycleListener

public void addLifecycleListener(LifecycleListener listener)
Add a lifecycle event listener to this component.
Specified by:
addLifecycleListener in interface Lifecycle
Parameters:
listener - The listener to add

removeLifecycleListener

public void removeLifecycleListener(LifecycleListener listener)
Remove a lifecycle event listener from this component.
Specified by:
removeLifecycleListener in interface Lifecycle
Parameters:
listener - The listener to remove

start

public void start()
           throws LifecycleException
Start the class loader.
Specified by:
start in interface Lifecycle
Throws:
LifecycleException - if a lifecycle error occurs

stop

public void stop()
          throws LifecycleException
Stop the class loader.
Specified by:
stop in interface Lifecycle
Throws:
LifecycleException - if a lifecycle error occurs

findClassInternal

protected java.lang.Class findClassInternal(java.lang.String name)
                                     throws java.lang.ClassNotFoundException
Find specified class in local repositories.
Returns:
the loaded class, or null if the class isn't found

findResourceInternal

protected WebappClassLoader.ResourceEntry findResourceInternal(java.lang.String name,
                                                               java.lang.String path)
Find specified resource in local repositories.
Returns:
the loaded resource, or null if the resource isn't found

isPackageSealed

protected boolean isPackageSealed(java.lang.String name,
                                  java.util.jar.Manifest man)
Returns true if the specified package name is sealed according to the given manifest.

findLoadedResource

protected java.io.InputStream findLoadedResource(java.lang.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

findLoadedClass0

protected java.lang.Class findLoadedClass0(java.lang.String name)
Finds the class with the given name if it has previously been loaded and cached by this class loader, and return the Class object. If this class has not been cached, return null.
Parameters:
name - Name of the resource to return

refreshPolicy

protected void refreshPolicy()
Refresh the system policy file, to pick up eventual changes.

validate

protected boolean validate(java.lang.String name)
Validate a classname. As per SRV.9.7.2, we must restict loading of classes from J2SE (java.*) and classes of the servlet API (javax.servlet.*). That should enhance robustness and prevent a number of user error (where an older version of servlet.jar would be present in /WEB-INF/lib).
Parameters:
name - class name
Returns:
true if the name is valid


Copyright © 2000-2001 Apache Software Foundation. All Rights Reserved.