org.apache.tools.ant
Class AntClassLoader

java.lang.Object
  |
  +--java.lang.ClassLoader
        |
        +--org.apache.tools.ant.AntClassLoader
All Implemented Interfaces:
BuildListener, java.util.EventListener

public class AntClassLoader
extends java.lang.ClassLoader
implements BuildListener

Used to load classes within ant with a different claspath from that used to start ant. Note that it is possible to force a class into this loader even when that class is on the system classpath by using the forceLoadClass method. Any subsequent classes loaded by that class will then use this loader rather than the system class loader.

Author:
Conor MacNeill, Jesse Glick

Constructor Summary
AntClassLoader(java.lang.ClassLoader parent, boolean parentFirst)
          Create an empty class loader.
AntClassLoader(java.lang.ClassLoader parent, Project project, Path classpath, boolean parentFirst)
          Create a classloader for the given project using the classpath given.
AntClassLoader(Project project, Path classpath)
          Create a classloader for the given project using the classpath given.
AntClassLoader(Project project, Path classpath, boolean parentFirst)
          Create a classloader for the given project using the classpath given.
 
Method Summary
 void addLoaderPackageRoot(java.lang.String packageRoot)
          Add a package root to the list of packages which must be loaded using this loader.
 void addPathElement(java.lang.String pathElement)
          Add an element to the classpath to be searched
 void addSystemPackageRoot(java.lang.String packageRoot)
          Add a package root to the list of packages which must be loaded on the parent loader.
 void buildFinished(BuildEvent event)
          Fired after the last target has finished.
 void buildStarted(BuildEvent event)
          Fired before any targets are started.
 void cleanup()
           
 java.lang.Class findClass(java.lang.String name)
          Search for and load a class on the classpath of this class loader.
protected  java.util.Enumeration findResources(java.lang.String name)
          Returns an enumeration of URLs representing all the resources with the given name by searching the class loader's classpath.
 java.lang.Class forceLoadClass(java.lang.String classname)
          Load a class through this class loader even if that class is available on the parent classpath.
 java.lang.Class forceLoadSystemClass(java.lang.String classname)
          Load a class through this class loader but defer to the parent class loader This ensures that instances of the returned class will be compatible with instances which which have already been loaded on the parent loader.
 java.net.URL getResource(java.lang.String name)
          Finds the resource with the given name.
 java.io.InputStream getResourceAsStream(java.lang.String name)
          Get a stream to read the requested resource name.
static void initializeClass(java.lang.Class theClass)
          Force initialization of a class in a JDK 1.1 compatible, albeit hacky way
protected  java.lang.Class loadClass(java.lang.String classname, boolean resolve)
          Load a class with this class loader.
protected  void log(java.lang.String message, int priority)
          Log a message through the project object if one has been provided.
 void messageLogged(BuildEvent event)
          Fired whenever a message is logged.
 void resetThreadContextLoader()
          Reset the current thread's context loader to its original value
 void setIsolated(boolean isolated)
          Set this classloader to run in isolated mode.
 void setThreadContextLoader()
          Set the current thread's context loader to this classloader, storing the current loader value for later resetting
 void targetFinished(BuildEvent event)
          Fired when a target has finished.
 void targetStarted(BuildEvent event)
          Fired when a target is started.
 void taskFinished(BuildEvent event)
          Fired when a task has finished.
 void taskStarted(BuildEvent event)
          Fired when a task is started.
 
Methods inherited from class java.lang.ClassLoader
defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findResource, 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, toString, wait, wait, wait
 

Constructor Detail

AntClassLoader

public AntClassLoader(Project project,
                      Path classpath)
Create a classloader for the given project using the classpath given.
Parameters:
project - the project to which this classloader is to belong.
classpath - the classpath to use to load the classes. This is combined with the system classpath in a manner determined by the value of ${build.sysclasspath}

AntClassLoader

public AntClassLoader(java.lang.ClassLoader parent,
                      Project project,
                      Path classpath,
                      boolean parentFirst)
Create a classloader for the given project using the classpath given.
Parameters:
parent - the parent classloader to which unsatisfied loading attempts are delgated
project - the project to which this classloader is to belong.
classpath - the classpath to use to load the classes.
parentFirst - if true indicates that the parent classloader should be consulted before trying to load the a class through this loader.

AntClassLoader

public AntClassLoader(Project project,
                      Path classpath,
                      boolean parentFirst)
Create a classloader for the given project using the classpath given.
Parameters:
project - the project to which this classloader is to belong.
classpath - the classpath to use to load the classes.
parentFirst - if true indicates that the parent classloader should be consulted before trying to load the a class through this loader.

AntClassLoader

public AntClassLoader(java.lang.ClassLoader parent,
                      boolean parentFirst)
Create an empty class loader. The classloader should be configured with path elements to specify where the loader is to look for classes.
Parameters:
parent - the parent classloader to which unsatisfied loading attempts are delgated
parentFirst - if true indicates that the parent classloader should be consulted before trying to load the a class through this loader.
Method Detail

log

protected void log(java.lang.String message,
                   int priority)
Log a message through the project object if one has been provided.
Parameters:
message - the message to log
priority - the logging priority of the message

setThreadContextLoader

public void setThreadContextLoader()
Set the current thread's context loader to this classloader, storing the current loader value for later resetting

resetThreadContextLoader

public void resetThreadContextLoader()
Reset the current thread's context loader to its original value

addPathElement

public void addPathElement(java.lang.String pathElement)
                    throws BuildException
Add an element to the classpath to be searched

setIsolated

public void setIsolated(boolean isolated)
Set this classloader to run in isolated mode. In isolated mode, classes not found on the given classpath will not be referred to the base class loader but will cause a classNotFoundException.

initializeClass

public static void initializeClass(java.lang.Class theClass)
Force initialization of a class in a JDK 1.1 compatible, albeit hacky way

addSystemPackageRoot

public void addSystemPackageRoot(java.lang.String packageRoot)
Add a package root to the list of packages which must be loaded on the parent loader. All subpackages are also included.
Parameters:
packageRoot - the root of all packages to be included.

addLoaderPackageRoot

public void addLoaderPackageRoot(java.lang.String packageRoot)
Add a package root to the list of packages which must be loaded using this loader. All subpackages are also included.
Parameters:
packageRoot - the root of akll packages to be included.

forceLoadClass

public java.lang.Class forceLoadClass(java.lang.String classname)
                               throws java.lang.ClassNotFoundException
Load a class through this class loader even if that class is available on the parent classpath. This ensures that any classes which are loaded by the returned class will use this classloader.
Parameters:
classname - the classname to be loaded.
Returns:
the required Class object
Throws:
java.lang.ClassNotFoundException - if the requested class does not exist on this loader's classpath.

forceLoadSystemClass

public java.lang.Class forceLoadSystemClass(java.lang.String classname)
                                     throws java.lang.ClassNotFoundException
Load a class through this class loader but defer to the parent class loader This ensures that instances of the returned class will be compatible with instances which which have already been loaded on the parent loader.
Parameters:
classname - the classname to be loaded.
Returns:
the required Class object
Throws:
java.lang.ClassNotFoundException - if the requested class does not exist on this loader's classpath.

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String name)
Get a stream to read the requested resource name.
Overrides:
getResourceAsStream in class java.lang.ClassLoader
Parameters:
name - the name of the resource for which a stream is required.
Returns:
a stream to the required resource or null if the resource cannot be found on the loader's classpath.

getResource

public java.net.URL getResource(java.lang.String name)
Finds 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.
Overrides:
getResource in class java.lang.ClassLoader
Parameters:
name - the name of the resource for which a stream is required.
Returns:
a URL for reading the resource, or null if the resource could not be found or the caller doesn't have adequate privileges to get the resource.

findResources

protected java.util.Enumeration findResources(java.lang.String name)
                                       throws java.io.IOException
Returns an enumeration of URLs representing all the resources with the given name by searching the class loader's classpath.
Overrides:
findResources in class java.lang.ClassLoader
Parameters:
name - the resource name.
Returns:
an enumeration of URLs for the resources.
Throws:
java.io.IOException - if I/O errors occurs (can't happen)

loadClass

protected java.lang.Class loadClass(java.lang.String classname,
                                    boolean resolve)
                             throws java.lang.ClassNotFoundException
Load a class with this class loader. This method will load a class. This class attempts to load the class firstly using the parent class loader. For JDK 1.1 compatability, this uses the findSystemClass method.
Overrides:
loadClass in class java.lang.ClassLoader
Parameters:
classname - the name of the class to be loaded.
resolve - true if all classes upon which this class depends are to be loaded.
Returns:
the required Class object
Throws:
java.lang.ClassNotFoundException - if the requested class does not exist on the system classpath or this loader's classpath.

findClass

public java.lang.Class findClass(java.lang.String name)
                          throws java.lang.ClassNotFoundException
Search for and load a class on the classpath of this class loader.
Overrides:
findClass in class java.lang.ClassLoader
Parameters:
name - the classname to be loaded.
Returns:
the required Class object
Throws:
java.lang.ClassNotFoundException - if the requested class does not exist on this loader's classpath.

cleanup

public void cleanup()

buildStarted

public void buildStarted(BuildEvent event)
Description copied from interface: BuildListener
Fired before any targets are started.
Specified by:
buildStarted in interface BuildListener

buildFinished

public void buildFinished(BuildEvent event)
Description copied from interface: BuildListener
Fired after the last target has finished. This event will still be thrown if an error occured during the build.
Specified by:
buildFinished in interface BuildListener
Following copied from interface: org.apache.tools.ant.BuildListener
See Also:
BuildEvent.getException()

targetStarted

public void targetStarted(BuildEvent event)
Description copied from interface: BuildListener
Fired when a target is started.
Specified by:
targetStarted in interface BuildListener
Following copied from interface: org.apache.tools.ant.BuildListener
See Also:
BuildEvent.getTarget()

targetFinished

public void targetFinished(BuildEvent event)
Description copied from interface: BuildListener
Fired when a target has finished. This event will still be thrown if an error occured during the build.
Specified by:
targetFinished in interface BuildListener
Following copied from interface: org.apache.tools.ant.BuildListener
See Also:
BuildEvent.getException()

taskStarted

public void taskStarted(BuildEvent event)
Description copied from interface: BuildListener
Fired when a task is started.
Specified by:
taskStarted in interface BuildListener
Following copied from interface: org.apache.tools.ant.BuildListener
See Also:
BuildEvent.getTask()

taskFinished

public void taskFinished(BuildEvent event)
Description copied from interface: BuildListener
Fired when a task has finished. This event will still be throw if an error occured during the build.
Specified by:
taskFinished in interface BuildListener
Following copied from interface: org.apache.tools.ant.BuildListener
See Also:
BuildEvent.getException()

messageLogged

public void messageLogged(BuildEvent event)
Description copied from interface: BuildListener
Fired whenever a message is logged.
Specified by:
messageLogged in interface BuildListener
Following copied from interface: org.apache.tools.ant.BuildListener
See Also:
BuildEvent.getMessage(), BuildEvent.getPriority()


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