Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.lutris.classloader
Class MultiClassLoader  view MultiClassLoader download MultiClassLoader.java

java.lang.Object
  extended byjava.lang.ClassLoader
      extended bycom.lutris.classloader.MultiClassLoader

public class MultiClassLoader
extends java.lang.ClassLoader

Summary:

A class loader that can load classes from class files and zip files residing in a specified class path.

This class loader can also load resources that reside on the class path and return them as is, as input streams, or as byte arrays.

Features:

Class Path

If classes are to be loaded from a class path, it must be set by the setClassPath or addClassPath methods or by the constructor prior to calling loadClass. If the class path is not set, the class will be loaded with the system class loader.

The class path can consist of directories, files, and/or URLs.

Example valid class path entries are:

     Files and directories on the local file system
     ../../java/classes
     /users/kristen/java/classes
     /users/kristen/java/classes/
     /users/kristen/java/zipfiles/MyClasses.zip
     /users/kristen/java/jarfiles/MyClasses.jar
     file:///users/kristen/java/classes
     file://localhost/users/kristen/java/classes
     
Files and directories on a remote file system (must be in URL format) ftp://www.foo.com/pub/java/classes file://www.foo.com/pub/java/classes/ http://www.foo.com/web/java/classes/ file://www.foo.com:8080/pub/java/zipfiles/MyClasses.zip http://www.foo.com:8080/web/java/jarfiles/MyClasses.jar

Note that the location of the entry includes the protocol, the host name, and the port while the file name is everything else. For example,

     http://www.foo.com:8080/web/java/jarfiles/MyClasses.jar
 
has the form [location][name] or
     [http://www.foo.com:8080/][/web/java/jarfiles/MyClasses.jar]
 
so the location is "http://www.foo.com:8080/" and the name is "/web/java/jarfiles/MyClasses.jar".

Note that the two references

     /users/kristen/java/classes/
     file:///users/kristen/java/classes/
 
represent the same directory on a Unix machine, and
     C|/windows/java/classes/
     file:///C|/windows/java/classes/
 
are equivalent directories on a Windows box.

But the two references

     /users/kristen/java/classes/
     file://monet.lutris.com/users/kristen/java/classes/
 
are not equivalent even if the directory /users/kristen/java/classes/ lives on the machine named monet.lutris.com and all development is on this machine. Why? Because the web (browser?) protocol is different for URLs with host information and those without. If no host is specified, the file is assumed to be on the local machine and the path is determined from the ROOT of the machine. If the host is specified, then the ftp protocol is used and the path is determined from the ftp ROOT (e.g. /users/ftp/) rather than the machine's ROOT. Thus, on a machine that support's anonymous ftp, the following two directories are the same:
     /users/ftp/pub/classes/
     file://picasso.lutris.com/pub/classes/
 
assuming the development is being done on picasso.lutris.com.

System Class Path

The system class path is the system-dependent path of directories and files (e.g. CLASSPATH on Unix and Windows) used by the system class loader to load classes. This class path is usually configured prior to executing a Java program but can be dynamically configured during runtime if desired. If you want to use the system class path for this class loader, the convenience method getSystemClassPath has been provided.

Valid system class path entries are directories and zip files, specified by absolute path or relative path on the system. Any valid system class path entry is also valid for this class loader.

Example

Here is an example of how to use this class loader:

     MultiClassLoader loader = new MultiClassLoader();
     loader.setClassPath("/web/java/lutris.jar");
     loader.addClassPath("/users/kristen/java/");
     loader.addClassPath("/usr/local/lib/graphics.zip");
     try {
         Class c = loader.loadClass("com.lutris.util.MyClass");
         System.out.println("My loader is " + c.getClassLoader());
         Object o = (Object) c.newInstance();
         System.out.println("My class is " + o.getClass());
     } catch (ClassNotFoundException e) {
         Throwable t = new Throwable();
         t.printStackTrace();
     }
 

Warning: This class loader is not yet fully compliant with Java 1.2. It maybe used on 1.2, but not all features are available. The parent loader, secondary loader, and filter may change in a future release without maintaining compatibility.

Version:
$Revision : 1.0 $

Nested Class Summary
static interface MultiClassLoader.ClassFilter
          A filter interface, used in deciding if a class should be loaded by this class loader.
 class MultiClassLoader.ClassResource
          Information kept about a loaded class.
(package private) static class MultiClassLoader.SysClassLoader
          Class use to as a proxy for the system class loader under java 1.*, where no system class loader object existed.
 
Nested classes inherited from class java.lang.ClassLoader
 
Field Summary
private  ClassPath classPath
          The class path to check when loading classes.
private  java.util.Vector filters
          If any class filters are supplied, this is a non-null list of filters to check.
private  java.util.Hashtable loadedClasses
          The ClassResource objects for classes loaded by this classloader (but not those handled by the parent).
static java.lang.String LOG_LEVEL
          Log level symbolic name
private  com.lutris.logging.LogChannel logChannel
          Log channel to write messages to; maybe null.
private  boolean loggingEnabled
          Is logging enabled?
private  int logLevel
          Numeric log level number for LOG_LEVEL string
private  java.lang.ClassLoader parentClassLoader
          Parent class loader, or null if there isn't one.
private  java.lang.ClassLoader secondaryClassLoader
          Secondary class loader, or null if there isn't one.
private static java.lang.ClassLoader sysClassLoader
          Singleton instance of system class loader
 
Fields inherited from class java.lang.ClassLoader
 
Constructor Summary
MultiClassLoader(java.lang.ClassLoader parent, java.lang.ClassLoader secondary, com.lutris.logging.LogChannel loadLogChannel)
          Constructs class loader with no initial class path and a specified parent class loader.
MultiClassLoader(java.io.File[] path, com.lutris.logging.LogChannel loadLogChannel)
          Constructs class loader with specified class path.
MultiClassLoader(java.io.File path, com.lutris.logging.LogChannel loadLogChannel)
          Constructs class loader with specified class path.
MultiClassLoader(com.lutris.logging.LogChannel loadLogChannel)
          Constructs class loader with no initial class path and the system class loader as the secondary.
MultiClassLoader(java.lang.String[] path, com.lutris.logging.LogChannel loadLogChannel)
          Constructs class loader with specified class path.
MultiClassLoader(java.lang.String path, com.lutris.logging.LogChannel loadLogChannel)
          Constructs class loader with specified class path.
MultiClassLoader(java.net.URL[] path, com.lutris.logging.LogChannel loadLogChannel)
          Constructs class loader with specified class path.
MultiClassLoader(java.net.URL path, com.lutris.logging.LogChannel loadLogChannel)
          Constructs class loader with specified class path.
 
Method Summary
 void addClassFilter(MultiClassLoader.ClassFilter filter)
          Add a filter to the list of filters that check if a class maybe loaded by this class loader.
 void addClassPath(java.io.File path)
          Adds specified class path to beginning of existing path.
 void addClassPath(java.io.File[] path)
          Adds specified class path to beginning of existing path.
 void addClassPath(java.lang.String path)
          Adds specified class path to beginning of existing path.
 void addClassPath(java.lang.String[] path)
          Adds specified class path to beginning of existing path.
 void addClassPath(java.net.URL path)
          Adds specified class path to beginning of existing path.
 void addClassPath(java.net.URL[] path)
          Adds specified class path to beginning of existing path.
private  int checkFilters(java.lang.String className)
          Check class filters to determine if a class should be loaded by this class loader.
private  MultiClassLoader.ClassResource checkForLoadedClass(java.lang.String className)
          Check the table of loaded classes to determine if a class is already loaded.
 void clearClassPath()
          Clears class path entries.
private  MultiClassLoader.ClassResource doLoadClass(java.lang.String className)
          Loads specified class.
 java.net.URL[] getClassPath()
          Gets class path for class loader defined previously by constructor and setClassPath/addClassPath methods.
 Resource getClassResource(java.lang.String className)
          Get the resource for a class loaded by this class loader.
 com.lutris.logging.LogChannel getLogChannel()
          Get the log channel associated this class loader.
 java.net.URL getResource(java.lang.String name)
          Gets specified resource as URL.
 byte[] getResourceAsByteArray(java.lang.String name)
          Gets specified resource as array of bytes.
 Resource getResourceAsIs(java.lang.String name)
          Gets specified resource object.
 java.io.InputStream getResourceAsStream(java.lang.String name)
          Gets specified resource as input stream.
 Resource getResourceObject(java.lang.String name)
          Gets specified resource object.
 java.lang.ClassLoader getSecondary()
          Get the secondary class loader.
static java.lang.ClassLoader getSystemClassLoader()
          Get the system class loader.
static java.net.URL[] getSystemClassPath()
          Gets class path from system.
 java.lang.Class loadClass(java.lang.String className, boolean resolve)
          Loads and, optionally, resolves the specified class.
private  MultiClassLoader.ClassResource loadClassHere(java.lang.String className)
          Load a class with this class loader.
private  MultiClassLoader.ClassResource loadClassOther(java.lang.String className, java.lang.ClassLoader loader, java.lang.String loaderName)
          Load a class with the parent or secondary class loader.
private  MultiClassLoader.ClassResource loadClassResource(java.lang.String className, boolean resolve)
          Loads and, optionally, resolves the specified class, returning the ClassResource object.
private  void logClassLoadFailure(java.lang.String className, java.lang.Throwable except)
          Log a class load failer.
private  void logReadFailure(java.lang.String name, java.lang.Throwable except)
          Log a resource read failer.
static java.lang.String[] parseClassPath(java.lang.String path)
          Parse a class-path string using the system path separator.
 void setClassPath(java.io.File path)
          Sets class loader with specified class path.
 void setClassPath(java.io.File[] path)
          Sets class loader with specified class path.
 void setClassPath(java.lang.String path)
          Sets class loader with specified class path.
 void setClassPath(java.lang.String[] path)
          Sets class loader with specified class path.
 void setClassPath(java.net.URL path)
          Sets class loader with specified class path.
 void setClassPath(java.net.URL[] path)
          Sets class loader with specified class path.
 void setParent(java.lang.ClassLoader parent)
          Set the parent class loader for delegation.
 void setSecondary(java.lang.ClassLoader secondary)
          Set the secondary class loader.
 boolean shouldReload()
          Determine if the classes loaded by this class loader have been modified.
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getPackage, getPackages, getParent, getResources, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

loadedClasses

private java.util.Hashtable loadedClasses
The ClassResource objects for classes loaded by this classloader (but not those handled by the parent).


classPath

private ClassPath classPath
The class path to check when loading classes.


LOG_LEVEL

public static final java.lang.String LOG_LEVEL
Log level symbolic name

See Also:
Constant Field Values

loggingEnabled

private boolean loggingEnabled
Is logging enabled?


logChannel

private com.lutris.logging.LogChannel logChannel
Log channel to write messages to; maybe null.


logLevel

private int logLevel
Numeric log level number for LOG_LEVEL string


filters

private java.util.Vector filters
If any class filters are supplied, this is a non-null list of filters to check.


parentClassLoader

private java.lang.ClassLoader parentClassLoader
Parent class loader, or null if there isn't one.


secondaryClassLoader

private java.lang.ClassLoader secondaryClassLoader
Secondary class loader, or null if there isn't one.


sysClassLoader

private static java.lang.ClassLoader sysClassLoader
Singleton instance of system class loader

Constructor Detail

MultiClassLoader

public MultiClassLoader(java.lang.ClassLoader parent,
                        java.lang.ClassLoader secondary,
                        com.lutris.logging.LogChannel loadLogChannel)
Constructs class loader with no initial class path and a specified parent class loader.


MultiClassLoader

public MultiClassLoader(com.lutris.logging.LogChannel loadLogChannel)
Constructs class loader with no initial class path and the system class loader as the secondary.


MultiClassLoader

public MultiClassLoader(java.lang.String path,
                        com.lutris.logging.LogChannel loadLogChannel)
Constructs class loader with specified class path. The parameter is assumed to be either a directory, URL, or zip file.


MultiClassLoader

public MultiClassLoader(java.lang.String[] path,
                        com.lutris.logging.LogChannel loadLogChannel)
Constructs class loader with specified class path. The parameter is assumed to be an array of directories, URLs, and/or zip files.


MultiClassLoader

public MultiClassLoader(java.io.File path,
                        com.lutris.logging.LogChannel loadLogChannel)
Constructs class loader with specified class path. The parameter is assumed to be either a zip file or directory.


MultiClassLoader

public MultiClassLoader(java.io.File[] path,
                        com.lutris.logging.LogChannel loadLogChannel)
Constructs class loader with specified class path. The parameter is assumed to be an array of zip files and/or directories.


MultiClassLoader

public MultiClassLoader(java.net.URL path,
                        com.lutris.logging.LogChannel loadLogChannel)
Constructs class loader with specified class path. The parameter is represent a directory or zip file on the local machine or a remote machine.


MultiClassLoader

public MultiClassLoader(java.net.URL[] path,
                        com.lutris.logging.LogChannel loadLogChannel)
Constructs class loader with specified class path. The parameter is represent directories and/or zip files on the local machine and/or on remote machines.

Method Detail

getSystemClassLoader

public static java.lang.ClassLoader getSystemClassLoader()
Get the system class loader.


setClassPath

public void setClassPath(java.lang.String path)
Sets class loader with specified class path. The parameter is assumed to be either a directory, URL, or zip file.


setClassPath

public void setClassPath(java.lang.String[] path)
Sets class loader with specified class path. The parameter is assumed to be an array of directories, URLs, and/or zip files.


setClassPath

public void setClassPath(java.io.File path)
Sets class loader with specified class path. The parameter is assumed to be either a zip file or directory.


setClassPath

public void setClassPath(java.io.File[] path)
Sets class loader with specified class path. The parameter is assumed to be an array of zip files and/or directories.


setClassPath

public void setClassPath(java.net.URL path)
Sets class loader with specified class path. The parameter is represent a directory or zip file on the local machine or a remote machine.


setClassPath

public void setClassPath(java.net.URL[] path)
Sets class loader with specified class path. The parameter is represent directories and/or zip files on the local machine and/or on remote machines.


addClassPath

public void addClassPath(java.lang.String path)
Adds specified class path to beginning of existing path. The parameter is assumed to be either a directory, URL, or zip file.


addClassPath

public void addClassPath(java.lang.String[] path)
Adds specified class path to beginning of existing path. The parameter is assumed to be an array of directories, URLs, and/or zip files.


addClassPath

public void addClassPath(java.io.File path)
Adds specified class path to beginning of existing path. The parameter is assumed to be either a zip file or directory.


addClassPath

public void addClassPath(java.io.File[] path)
Adds specified class path to beginning of existing path. The parameter is assumed to be an array of zip files and/or directories.


addClassPath

public void addClassPath(java.net.URL path)
Adds specified class path to beginning of existing path. The parameter is represent a directory or zip file on the local machine or a remote machine.


addClassPath

public void addClassPath(java.net.URL[] path)
Adds specified class path to beginning of existing path. The parameter is represent directories and/or zip files on the local machine and/or on remote machines.


clearClassPath

public void clearClassPath()
Clears class path entries.


getClassPath

public java.net.URL[] getClassPath()
Gets class path for class loader defined previously by constructor and setClassPath/addClassPath methods.


parseClassPath

public static java.lang.String[] parseClassPath(java.lang.String path)
Parse a class-path string using the system path separator.


getSystemClassPath

public static java.net.URL[] getSystemClassPath()
Gets class path from system.


setParent

public void setParent(java.lang.ClassLoader parent)
Set the parent class loader for delegation.


getSecondary

public java.lang.ClassLoader getSecondary()
Get the secondary class loader.


setSecondary

public void setSecondary(java.lang.ClassLoader secondary)
Set the secondary class loader.


getLogChannel

public com.lutris.logging.LogChannel getLogChannel()
Get the log channel associated this class loader.


addClassFilter

public void addClassFilter(MultiClassLoader.ClassFilter filter)
Add a filter to the list of filters that check if a class maybe loaded by this class loader.


checkFilters

private int checkFilters(java.lang.String className)
Check class filters to determine if a class should be loaded by this class loader. This also handles check an implicit filter for java.lang.* classes, which must go to the main class loader.


checkForLoadedClass

private MultiClassLoader.ClassResource checkForLoadedClass(java.lang.String className)
Check the table of loaded classes to determine if a class is already loaded. Purposely not synchronized, allowing a quick check for a class being loaded. A second check, which higher level synchronization is required if the class is not found. This function does logs a message when a class is found.


loadClass

public java.lang.Class loadClass(java.lang.String className,
                                 boolean resolve)
                          throws java.lang.ClassNotFoundException
Loads and, optionally, resolves the specified class. If the class needs to be instantiated, the class must be resolved. If only the existence of the class needs verification, class resolution is unnecessary. Calling loadClass(String className) is equivalent to calling this method with resolve set to true. Purposely not synchronized. If class is not found in loadedClasses table, then doLoadClass will do the operation in a synchronized manner.


logClassLoadFailure

private void logClassLoadFailure(java.lang.String className,
                                 java.lang.Throwable except)
Log a class load failer.


logReadFailure

private void logReadFailure(java.lang.String name,
                            java.lang.Throwable except)
Log a resource read failer.


loadClassResource

private MultiClassLoader.ClassResource loadClassResource(java.lang.String className,
                                                         boolean resolve)
                                                  throws java.lang.ClassNotFoundException
Loads and, optionally, resolves the specified class, returning the ClassResource object.


getClassResource

public Resource getClassResource(java.lang.String className)
                          throws java.lang.ClassNotFoundException
Get the resource for a class loaded by this class loader.


loadClassOther

private MultiClassLoader.ClassResource loadClassOther(java.lang.String className,
                                                      java.lang.ClassLoader loader,
                                                      java.lang.String loaderName)
Load a class with the parent or secondary class loader. Must already be synchronized


loadClassHere

private MultiClassLoader.ClassResource loadClassHere(java.lang.String className)
                                              throws java.io.IOException
Load a class with this class loader. Must already be synchronized


doLoadClass

private MultiClassLoader.ClassResource doLoadClass(java.lang.String className)
                                            throws java.lang.ClassNotFoundException,
                                                   java.io.IOException
Loads specified class. If possible, the class will loaded from the class path defined previously with setClassPath, addClassPath, and/or the constructor. Otherwise, the class will be passed off to the parent class loader. Classes from the java.* packages cannot be loaded by this class loader so will be passed off to the parent class loader.


getResource

public java.net.URL getResource(java.lang.String name)
Gets specified resource as URL. Doing a getContent() on the URL may return an Image, an AudioClip, or an InputStream.


getResourceObject

public Resource getResourceObject(java.lang.String name)
Gets specified resource object.


getResourceAsIs

public Resource getResourceAsIs(java.lang.String name)
Gets specified resource object.


getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String name)
Gets specified resource as input stream.


getResourceAsByteArray

public byte[] getResourceAsByteArray(java.lang.String name)
Gets specified resource as array of bytes.


shouldReload

public boolean shouldReload()
Determine if the classes loaded by this class loader have been modified. If any file associated with loaded in the class loader's class path has changed, the classes should be reloaded. If the classes need to be reloaded, a new instance of this class loader must be created because a particular class loader instance can only load classes once.