All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class org.apache.jserv.JServClassLoader

java.lang.Object
   |
   +----java.lang.ClassLoader
           |
           +----org.apache.jserv.JServClassLoader

public class JServClassLoader
extends ClassLoader
implements JServDebug. DebugConstants
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.

The classloader is managed by a JServServletManager. When the classloader reports that one of the classes it has loaded has changed on disk, the servlet manager discards the classloader and creates 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 its own path, and uses the system classloader if that fails.

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.5 $ $Date: 1998/02/13 13:45:03 $
Author:
Francis J. Lacoste, Martin Pool
See Also:
ClassLoader

Constructor Index

 o JServClassLoader(File)
Creates a new class loader that will load classes from the file specified as argument.
 o JServClassLoader(File[])
Creates a new class loader that will load classes from a specified path.

Method Index

 o getResource(String)
Find a resource with a given name.
 o getResourceAsStream(String)
Get an InputStream on a given resource.
 o loadClass(String, boolean)
Resolves the specified name to a Class.
 o reinstantiate()
Re-instantiate this class loader.
 o shouldReload()
Check whether the classloader should be reinstantiated.
 o shouldReload(String)
Check to see if a given class should be reloaded because of a modification to the original class.

Constructors

 o JServClassLoader
 public JServClassLoader(File classRepository)
Creates a new class loader that will load classes from the file specified as argument.

Parameters:
classRepository - This can either be a directory or a zip/jar file.
 o JServClassLoader
 public JServClassLoader(File classRepository[])
Creates a new class loader that will load classes from a specified path.

Parameters:
classRepository - An array of directories and/or a zip/jar file.

Methods

 o shouldReload
 public synchronized boolean shouldReload(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.
 o shouldReload
 public synchronized 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.

 o reinstantiate
 public JServClassLoader 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.

 o loadClass
 protected synchronized Class loadClass(String name,
                                        boolean resolve) throws 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, or null if it was not found.
Throws: ClassNotFoundException
if the class loader cannot find a definition for the class.
Overrides:
loadClass in class ClassLoader
 o getResourceAsStream
 public InputStream getResourceAsStream(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 ClassLoader
See Also:
getResourceAsStream
 o getResource
 public URL getResource(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.

The JServClassLoader 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 ClassLoader

All Packages  Class Hierarchy  This Package  Previous  Next  Index