org.apache.catalina.loader
Class FileClassLoader

java.lang.Object
  |
  +--java.lang.ClassLoader
        |
        +--org.apache.catalina.loader.FileClassLoader
All Implemented Interfaces:
Reloader

public final class FileClassLoader
extends java.lang.ClassLoader
implements Reloader

Implementation of java.lang.ClassLoader that knows how to load classes from disk directories and JAR files. It also implements the Reloader interface, to provide automatic reloading support to StandardLoader.

This code was partially based on the AdaptiveClassLoader module originally copied from Apache JServ, and used in Tomcat 3.x. However, it does class loading in a different order (webapp first then system classes), and allows the set of associated repositories to be modified at runtime.

Besides the usual functions of loading and caching the bytecodes for requested classes, this class loader also supports optional in-memory caching of resources acquired via getResourceAsStream(). Because most applications probably read their resource files only once (during initialization), this is disabled by default, but it can be enabled by setting the maxCount, maxSize, and minSize properties.

Version:
$Revision: 1.2 $ $Date: 2000/09/21 20:06:00 $
Author:
Craig R. McClanahan

Constructor Summary
FileClassLoader()
          Construct a new ClassLoader instance with no defined repositories and no parent ClassLoader.
FileClassLoader(java.lang.ClassLoader parent)
          Construct a new ClassLoader instance with no defined repositories and the specified parent ClassLoader.
FileClassLoader(java.lang.ClassLoader parent, java.lang.String[] respositories)
          Construct a new ClassLoader instance with the specified repositories and parent ClassLoader.
FileClassLoader(java.lang.String[] repositories)
          Construct a new ClassLoader instance with the specified repositories and no parent ClassLoader.
 
Method Summary
 void addAllowed(java.lang.String name)
          Add a new fully qualified class or resource name to which access will be allowed, even if the class or resource name would otherwise match one of the restricted patterns.
 void addRepository(java.lang.String repository)
          Add a new repository to the set of places this ClassLoader can look for classes to be loaded.
 void addRestricted(java.lang.String prefix)
          Add a fully qualified class or resource name prefix that, if it matches the name of a requested class or resource, will cause access to that class or resource to fail (unless the complete name is on the allowed list).
 void addSystem(java.lang.String prefix)
          Add a fully qualified class or resource name prefix that, if it matches the name of a requested class or resource, will cause access to that class or resource to be attempted in the system class loader only (bypassing the repositories defined in this class loader).
 java.lang.String[] findAllowed()
          Return a String array of the allowed class or resource name list for this class loader.
 java.lang.String[] findRepositories()
          Return a String array of the current repositories for this class loader.
 java.lang.String[] findRestricted()
          Return a String array of the restricted class or resource name prefixes for this class loader.
 java.lang.String[] findSystem()
          Return a Striong array of the sytsem class or resource name prefixes for this class loader.
 int getDebug()
          Return the debugging detail level of this component.
 int getMaxCount()
          Return the maximum count of resources to be cached.
 long getMaxSize()
          Return the maximum size of resources to be cached.
 long getMinSize()
          Return the minimum size of resources to be cached.
 java.net.URL getResource(java.lang.String name)
          Find a resource with a given name.
 java.io.InputStream getResourceAsStream(java.lang.String name)
          Get an InputStream on a given resource.
 java.lang.Class loadClass(java.lang.String name, boolean resolve)
          Requests the class loader to load and resolve a class with the specified name.
 boolean modified()
          Have one or more classes or resources been modified so that a reload is appropriate?
 void removeAllowed(java.lang.String name)
          Remove a fully qualified class or resource name from the allowed list.
 void removeRestricted(java.lang.String prefix)
          Remove a class or resource name prefix from the restricted list.
 void removeSystem(java.lang.String prefix)
          Remove a class or resource name prefix from the system list.
 void setDebug(int debug)
          Set the debugging detail level of this component.
 void setMaxCount(int maxCount)
          Set the maximum count of resources to be cached.
 void setMaxSize(long maxSize)
          Set the maximum size of resources to be cached.
 void setMinSize(long minSize)
          Set the minimum size of resources to be cached.
 java.lang.String toString()
          Return a String representation of this class.
 
Methods inherited from class java.lang.ClassLoader
defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getPackage, getPackages, getParent, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, resolveClass, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FileClassLoader

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

FileClassLoader

public FileClassLoader(java.lang.ClassLoader parent)
Construct a new ClassLoader instance with no defined repositories and the specified parent ClassLoader.
Parameters:
parent - The parent ClassLoader

FileClassLoader

public FileClassLoader(java.lang.String[] repositories)
Construct a new ClassLoader instance with the specified repositories and no parent ClassLoader.
Parameters:
repositories - Initial list of repositories

FileClassLoader

public FileClassLoader(java.lang.ClassLoader parent,
                       java.lang.String[] respositories)
Construct a new ClassLoader instance with the specified repositories and parent ClassLoader.
Parameters:
parent - The parent ClassLoader
repositories - Initial list of repositories
Method Detail

getDebug

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

setDebug

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

getMaxCount

public int getMaxCount()
Return the maximum count of resources to be cached.

setMaxCount

public void setMaxCount(int maxCount)
Set the maximum count of resources to be cached.
Parameters:
maxCount - The new maximum count

getMaxSize

public long getMaxSize()
Return the maximum size of resources to be cached.

setMaxSize

public void setMaxSize(long maxSize)
Set the maximum size of resources to be cached.

getMinSize

public long getMinSize()
Return the minimum size of resources to be cached.

setMinSize

public void setMinSize(long minSize)
Set the minimum size of resources to be cached.

addAllowed

public void addAllowed(java.lang.String name)
Add a new fully qualified class or resource name to which access will be allowed, even if the class or resource name would otherwise match one of the restricted patterns.
Specified by:
addAllowed in interface Reloader
Parameters:
name - Class or resource name to allow access for

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

addRestricted

public void addRestricted(java.lang.String prefix)
Add a fully qualified class or resource name prefix that, if it matches the name of a requested class or resource, will cause access to that class or resource to fail (unless the complete name is on the allowed list).
Specified by:
addRestricted in interface Reloader
Parameters:
prefix - The restricted prefix

addSystem

public void addSystem(java.lang.String prefix)
Add a fully qualified class or resource name prefix that, if it matches the name of a requested class or resource, will cause access to that class or resource to be attempted in the system class loader only (bypassing the repositories defined in this class loader). By default, the java. prefix is defined as a system prefix.
Specified by:
addSystem in interface Reloader
Parameters:
prefix - The system prefix

findAllowed

public java.lang.String[] findAllowed()
Return a String array of the allowed class or resource name list for this class loader. If there are none, a zero-length array is returned.
Specified by:
findAllowed in interface Reloader

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

findRestricted

public java.lang.String[] findRestricted()
Return a String array of the restricted class or resource name prefixes for this class loader. If there are none, a zero-length array is returned.
Specified by:
findRestricted in interface Reloader

findSystem

public java.lang.String[] findSystem()
Return a Striong array of the sytsem class or resource name prefixes for this class loader. If there are none, a zero-length array is returned.
Specified by:
findSystem in interface Reloader

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

removeAllowed

public void removeAllowed(java.lang.String name)
Remove a fully qualified class or resource name from the allowed list.
Parameters:
name - The name to remove

removeRestricted

public void removeRestricted(java.lang.String prefix)
Remove a class or resource name prefix from the restricted list.
Parameters:
prefix - Prefix to be removed

removeSystem

public void removeSystem(java.lang.String prefix)
Remove a class or resource name prefix from the system list.
Parameters:
prefix - Prefix to be removed

getResource

public java.net.URL getResource(java.lang.String name)
Find a resource with a given name. The return is a URL to the resource. Doing a getContent() on the URL may return an Image, an AudioClip, or an InputStream.
Overrides:
getResource in class java.lang.ClassLoader
Parameters:
name - The name of the resource, to be used as is

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String name)
Get an InputStream on a given resource. Will return null if no resource with this name is found.
Overrides:
getResourceAsStream in class java.lang.ClassLoader
Parameters:
name - The name of the resource

loadClass

public java.lang.Class loadClass(java.lang.String name,
                                 boolean resolve)
                          throws java.lang.ClassNotFoundException
Requests the class loader to load and resolve a class with the specified name. The loadClass method is called by the Java Virtual Machine when a class loaded by a class loader first references another class. Every subclass of ClassLoader must define this method.
Overrides:
loadClass in class java.lang.ClassLoader
Parameters:
name - Name of the desired Class
resolve - true if this Class needs to be resolved.
Returns:
The resulting Class, or null if it was not found
Throws:
java.lang.ClassNotFoundException - if the class loader cannot find a definition for this class

toString

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


Copyright © 2000 Apache Software Foundation. All Rights Reserved.