org.apache.java.lang
Class AdaptiveClassLoader

java.lang.Object
  |
  +--java.lang.ClassLoader
        |
        +--org.apache.java.lang.AdaptiveClassLoader

public class AdaptiveClassLoader
extends java.lang.ClassLoader

A class loader that loads classes from directories and/or zip-format file such as JAR file. It tracks the modification time of the classes it loads to permit reloading through re-instantiation.

When the classloader reports its creator that one of the classes it has loaded has changed on disk, it should discard the classloader and create a new instance using reinstantiate. The classes are then reloaded into the new classloader as required.

The classloader can also load resources, which are a means for packaging application data such as images within a jar file or directory.

The classloader always first tries to load classes and resources from the system, and uses it's own path if that fails. This is also done if an empty repository is passed at construction.

How autoreload works:

The Java VM considers two classes the same if they have the same fully-qualified name and if they were loaded from the same ClassLoader.

There is no way for a classloader to 'undefine' a class once it has been loaded. However, the servlet engine can discard a classloader and the classes it contains, causing the

The JServServletManager creates a new instance of the classloader each time it detects that any of the loaded classes have changed.

Before terminating, all servlets are destroyed.

According to the Java Language Specification (JLS), classes may be garbage-collected when there are no longer any instances of that class and the java.lang.Class object is finalizable. It is intended that this be the case when a JServClassLoader is discarded.

Many VM releases did not implement class garbage collection properly. In such a VM, the memory usage will continue to grow if autoreloading is enable. Running the VM with -verbosegc (or the corresponding option for non-Javasoft VMs) may give some debugging information.

It is important that the destroy method be implemented properly, as servlets may be destroyed and reinitialized several times in the life of a VM.

Version:
$Revision: 1.20 $ $Date: 2000/03/12 05:34:42 $
See Also:
ClassLoader

Inner Class Summary
private static class AdaptiveClassLoader.ClassCacheEntry
          Private class used to maintain information about the classes that we loaded.
 
Inner classes inherited from class java.lang.ClassLoader
java.lang.ClassLoader.NativeLibrary
 
Field Summary
private  java.util.Hashtable cache
          Cache of the loaded classes.
private  int generation
          Generation number of the classloader, used to distinguish between different instances.
private static int generationCounter
          Generation counter, incremented for each classloader as they are created.
private  java.lang.ClassLoader myParentClassLoader
          Save our class loader for chaining, and speed purposes.
private  java.util.Vector repository
          The classpath which this classloader searches for class definitions.
 
Fields inherited from class java.lang.ClassLoader
bootstrapClassPath, classes, defaultDomain, defaultPermissions, getClassLoaderPerm, initialized, loadedLibraryNames, nativeLibraries, nativeLibraryContext, packages, parent, scl, sclSet, sys_paths, systemNativeLibraries, usr_paths
 
Constructor Summary
AdaptiveClassLoader(java.util.Vector classRepository)
          Creates a new class loader that will load classes from specified class repositories.
AdaptiveClassLoader(java.util.Vector classRepository, java.lang.ClassLoader chainedClassLoader)
          Creates a new class loader that will load classes from specified class repositories.
 
Method Summary
 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.
private  boolean isZipOrJarArchive(java.io.File file)
          Test if a file is a ZIP or JAR archive.
 long lastModified(java.lang.String name)
          Return the last modified time for a class in the ClassCache.
private  byte[] loadBytesFromStream(java.io.InputStream in, int length)
          Loads all the bytes of an InputStream.
protected  java.lang.Class loadClass(java.lang.String name, boolean resolve)
          Resolves the specified name to a Class.
private  byte[] loadClassFromDirectory(java.io.File dir, java.lang.String name, AdaptiveClassLoader.ClassCacheEntry cache)
          Tries to load the class from a directory.
private  byte[] loadClassFromZipfile(java.io.File file, java.lang.String name, AdaptiveClassLoader.ClassCacheEntry cache)
          Tries to load the class from a zip file.
private  java.io.InputStream loadResourceFromDirectory(java.io.File dir, java.lang.String name)
          Loads resource from a directory.
private  java.io.InputStream loadResourceFromZipfile(java.io.File file, java.lang.String name)
          Loads resource from a zip file
private  java.lang.Class loadSystemClass(java.lang.String name, boolean resolve)
          Load a class using the system classloader.
 AdaptiveClassLoader reinstantiate()
          Re-instantiate this class loader.
private  boolean securityAllowsClass(java.lang.String className)
          Checks whether a classloader is allowed to define a given class, within the security manager restrictions.
 boolean shouldReload()
          Check whether the classloader should be reinstantiated.
 boolean shouldReload(java.lang.String classname)
          Check to see if a given class should be reloaded because of a modification to the original class.
 
Methods inherited from class java.lang.ClassLoader
, addClass, check, defineClass, defineClass, defineClass, defineClass0, definePackage, findBootstrapClass, findClass, findLibrary, findLoadedClass, findNative, findResource, findResources, findSystemClass, getBootstrapClassPath, getBootstrapResource, getBootstrapResources, getCallerClassLoader, getDefaultDomain, getGetClassLoaderPerm, getPackage, getPackages, getParent, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, initializePath, isAncestor, loadClass, loadLibrary, loadLibrary0, resolveClass, resolveClass0, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

generationCounter

private static int generationCounter
Generation counter, incremented for each classloader as they are created.

generation

private int generation
Generation number of the classloader, used to distinguish between different instances.

cache

private java.util.Hashtable cache
Cache of the loaded classes. This contains ClassCacheEntry keyed by class names.

myParentClassLoader

private java.lang.ClassLoader myParentClassLoader
Save our class loader for chaining, and speed purposes.

repository

private java.util.Vector repository
The classpath which this classloader searches for class definitions. Each element of the vector should be either a directory, a .zip file, or a .jar file.

It may be empty when only system classes are controlled.

Constructor Detail

AdaptiveClassLoader

public AdaptiveClassLoader(java.util.Vector classRepository)
                    throws java.lang.IllegalArgumentException
Creates a new class loader that will load classes from specified class repositories.
Parameters:
classRepository - An set of File classes indicating directories and/or zip/jar files. It may be empty when only system classes are loaded.

AdaptiveClassLoader

public AdaptiveClassLoader(java.util.Vector classRepository,
                           java.lang.ClassLoader chainedClassLoader)
                    throws java.lang.IllegalArgumentException
Creates a new class loader that will load classes from specified class repositories.
Parameters:
classRepository - An set of File classes indicating directories and/or zip/jar files. It may be empty when only system classes are loaded.
chainedClassLoader - A class loader to attempt to load classes as resources thru before falling back on the default system loaders.
Method Detail

isZipOrJarArchive

private boolean isZipOrJarArchive(java.io.File file)
Test if a file is a ZIP or JAR archive.
Parameters:
file - the file to be tested.
Returns:
true if the file is a ZIP/JAR archive, false otherwise.

shouldReload

public boolean shouldReload(java.lang.String classname)
Check to see if a given class should be reloaded because of a modification to the original class.
Parameters:
className - The name of the class to check for modification.

shouldReload

public boolean shouldReload()
Check whether the classloader should be reinstantiated.

The classloader must be replaced if there is any class whose origin file has changed since it was last loaded.


reinstantiate

public AdaptiveClassLoader reinstantiate()
Re-instantiate this class loader.

This method creates a new instance of the class loader that will load classes form the same path as this one.


loadClass

protected java.lang.Class loadClass(java.lang.String name,
                                    boolean resolve)
                             throws java.lang.ClassNotFoundException
Resolves the specified name to a Class. The method loadClass() is called by the virtual machine. As an abstract method, loadClass() must be defined in a subclass of ClassLoader.
Parameters:
name - the name of the desired Class.
resolve - true if the Class needs to be resolved; false if the virtual machine just wants to determine whether the class exists or not
Returns:
the resulting Class.
Throws:
java.lang.ClassNotFoundException - if the class loader cannot find a the requested class.
Overrides:
loadClass in class java.lang.ClassLoader

loadSystemClass

private java.lang.Class loadSystemClass(java.lang.String name,
                                        boolean resolve)
                                 throws java.lang.NoClassDefFoundError,
                                        java.lang.ClassNotFoundException
Load a class using the system classloader.
Throws:
java.lang.ClassNotFoundException - if the class loader cannot find a the requested class.
java.lang.NoClassDefFoundError - if the class loader cannot find a definition for the class.

securityAllowsClass

private boolean securityAllowsClass(java.lang.String className)
Checks whether a classloader is allowed to define a given class, within the security manager restrictions.

loadClassFromDirectory

private byte[] loadClassFromDirectory(java.io.File dir,
                                      java.lang.String name,
                                      AdaptiveClassLoader.ClassCacheEntry cache)
                               throws java.io.IOException
Tries to load the class from a directory.
Parameters:
dir - The directory that contains classes.
name - The classname
cache - The cache entry to set the file if successful.

loadClassFromZipfile

private byte[] loadClassFromZipfile(java.io.File file,
                                    java.lang.String name,
                                    AdaptiveClassLoader.ClassCacheEntry cache)
                             throws java.io.IOException
Tries to load the class from a zip file.
Parameters:
file - The zipfile that contains classes.
name - The classname
cache - The cache entry to set the file if successful.

loadBytesFromStream

private byte[] loadBytesFromStream(java.io.InputStream in,
                                   int length)
                            throws java.io.IOException
Loads all the bytes of an InputStream.

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.

The JServClassLoader translate the resource's name to a file or a zip entry. It looks for the resource in all its repository entry.

Parameters:
name - the name of the resource, to be used as is.
Returns:
an InputStream on the resource, or null if not found.
Overrides:
getResourceAsStream in class java.lang.ClassLoader
See Also:
Class.getResourceAsStream(String)

loadResourceFromDirectory

private java.io.InputStream loadResourceFromDirectory(java.io.File dir,
                                                      java.lang.String name)
Loads resource from a directory.

loadResourceFromZipfile

private java.io.InputStream loadResourceFromZipfile(java.io.File file,
                                                    java.lang.String name)
Loads resource from a zip file

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.

This classloader looks for the resource only in the directory repository for this resource.

Parameters:
name - the name of the resource, to be used as is.
Returns:
an URL on the resource, or null if not found.
Overrides:
getResource in class java.lang.ClassLoader

lastModified

public long lastModified(java.lang.String name)
                  throws java.lang.ClassNotFoundException
Return the last modified time for a class in the ClassCache.
Throws:
java.lang.ClassNotFoundException - if class is not found