FileDocCategorySizeDatePackage
Classloader.javaAPI DocApache Ant 1.708006Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs

Classloader

public class Classloader extends org.apache.tools.ant.Task
EXPERIMENTAL Create or modifies ClassLoader. The required pathRef parameter will be used to add classpath elements. The classpath is a regular path. Currently only file components are supported (future extensions may allow URLs). You can modify the core loader by not specifying any name or using "ant.coreLoader". (the core loader is used to load system ant tasks and for taskdefs that don't specify an explicit path). Taskdef and typedef can use the loader you create if the name follows the "ant.loader.NAME" pattern. NAME will be used as a pathref when calling taskdef. This tasks will not modify the core loader if "build.sysclasspath=only" The typical use is:
<path id="ant.deps" >
<fileset dir="myDir" >
<include name="junit.jar, bsf.jar, js.jar, etc"/>
</fileset>
</path>

<classloader pathRef="ant.deps" />

Fields Summary
public static final String
SYSTEM_LOADER_REF
private String
name
private org.apache.tools.ant.types.Path
classpath
private boolean
reset
private boolean
parentFirst
private String
parentName
Constructors Summary
public Classloader()
Default constructor


           
      
    
Methods Summary
public org.apache.tools.ant.types.PathcreateClasspath()
Create a classpath.

return
a path for configuration.

        if (this.classpath == null) {
            this.classpath = new Path(null);
        }
        return this.classpath.createPath();
    
public voidexecute()
do the classloader manipulation.

        try {
            // Gump friendly - don't mess with the core loader if only classpath
            if ("only".equals(getProject().getProperty("build.sysclasspath"))
                && (name == null || SYSTEM_LOADER_REF.equals(name))) {
                log("Changing the system loader is disabled "
                    + "by build.sysclasspath=only", Project.MSG_WARN);
                return;
            }

            String loaderName = (name == null) ? SYSTEM_LOADER_REF : name;

            Object obj = getProject().getReference(loaderName);
            if (reset) {
                // Are any other references held ? Can we 'close' the loader
                // so it removes the locks on jars ?
                obj = null; // a new one will be created.
            }

            // XXX maybe use reflection to addPathElement (other patterns ?)
            if (obj != null && !(obj instanceof AntClassLoader)) {
                log("Referenced object is not an AntClassLoader",
                        Project.MSG_ERR);
                return;
            }

            AntClassLoader acl = (AntClassLoader) obj;

            if (acl == null) {
                // Construct a new class loader
                Object parent = null;
                if (parentName != null) {
                    parent = getProject().getReference(parentName);
                    if (!(parent instanceof ClassLoader)) {
                        parent = null;
                    }
                }
                // TODO: allow user to request the system or no parent
                if (parent == null) {
                    parent = this.getClass().getClassLoader();
                }

                if (name == null) {
                    // The core loader must be reverse
                    //reverse=true;
                }
                getProject().log("Setting parent loader " + name + " "
                    + parent + " " + parentFirst, Project.MSG_DEBUG);

                // The param is "parentFirst"
                acl = new AntClassLoader((ClassLoader) parent,
                         getProject(), classpath, parentFirst);

                getProject().addReference(loaderName, acl);

                if (name == null) {
                    // This allows the core loader to load optional tasks
                    // without delegating
                    acl.addLoaderPackageRoot("org.apache.tools.ant.taskdefs.optional");
                    getProject().setCoreLoader(acl);
                }
            }
            if (classpath != null) {
                String[] list = classpath.list();
                for (int i = 0; i < list.length; i++) {
                    File f = new File(list[i]);
                    if (f.exists()) {
                        acl.addPathElement(f.getAbsolutePath());
                        log("Adding to class loader " +  acl + " " + f.getAbsolutePath(),
                                Project.MSG_DEBUG);
                    }
                }
            }

            // XXX add exceptions

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    
public voidsetClasspath(org.apache.tools.ant.types.Path classpath)
Set the classpath to be used when searching for component being defined

param
classpath an Ant Path object containing the classpath.

        if (this.classpath == null) {
            this.classpath = classpath;
        } else {
            this.classpath.append(classpath);
        }
    
public voidsetClasspathRef(org.apache.tools.ant.types.Reference pathRef)
Specify which path will be used. If the loader already exists and is an AntClassLoader (or any other loader we can extend), the path will be added to the loader.

param
pathRef a reference to a path.
throws
BuildException if there is a problem.

        classpath = (Path) pathRef.getReferencedObject(getProject());
    
public voidsetName(java.lang.String name)
Name of the loader. If none, the default loader will be modified

param
name the name of this loader

        this.name = name;
    
public voidsetParentFirst(boolean b)
Set reverse attribute.

param
b if true reverse the normal classloader lookup.

        this.parentFirst = b;
    
public voidsetParentName(java.lang.String name)
Set the name of the parent.

param
name the parent name.

        this.parentName = name;
    
public voidsetReset(boolean b)
Reset the classloader, if it already exists. A new loader will be created and all the references to the old one will be removed. (it is not possible to remove paths from a loader). The new path will be used.

param
b true if the loader is to be reset.

        this.reset = b;
    
public voidsetReverse(boolean b)
Set reverse attribute.

param
b if true reverse the normal classloader lookup.

        this.parentFirst = !b;