FileDocCategorySizeDatePackage
JavaResource.javaAPI DocApache Ant 1.706576Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.types.resources

JavaResource

public class JavaResource extends org.apache.tools.ant.types.Resource
A Resource representation of something loadable via a Java classloader.
since
Ant 1.7

Fields Summary
private org.apache.tools.ant.types.Path
classpath
private org.apache.tools.ant.types.Reference
loader
Constructors Summary
public JavaResource()
Default constructor.

    
public JavaResource(String name, org.apache.tools.ant.types.Path path)
Construct a new JavaResource using the specified name and classpath.

param
name the resource name.
param
path the classpath.

        setName(name);
        classpath = path;
    
Methods Summary
public intcompareTo(java.lang.Object another)
Compare this JavaResource to another Resource.

param
another the other Resource against which to compare.
return
a negative integer, zero, or a positive integer as this JavaResource is less than, equal to, or greater than the specified Resource.

        if (isReference()) {
            return ((Comparable) getCheckedRef()).compareTo(another);
        }
        if (another.getClass().equals(getClass())) {
            JavaResource otherjr = (JavaResource) another;
            if (!getName().equals(otherjr.getName())) {
                return getName().compareTo(otherjr.getName());
            }
            if (loader != otherjr.loader) {
                if (loader == null) {
                    return -1;
                }
                if (otherjr.loader == null) {
                    return 1;
                }
                return loader.getRefId().compareTo(otherjr.loader.getRefId());
            }
            Path p = getClasspath();
            Path op = otherjr.getClasspath();
            if (p != op) {
                if (p == null) {
                    return -1;
                }
                if (op == null) {
                    return 1;
                }
                return p.toString().compareTo(op.toString());
            }
            return 0;
        }
        return super.compareTo(another);
    
public org.apache.tools.ant.types.PathcreateClasspath()
Add a classpath to use when looking up a resource.

return
The classpath to be configured

        checkChildrenAllowed();
        if (this.classpath == null) {
            this.classpath = new Path(getProject());
        }
        return this.classpath.createPath();
    
public org.apache.tools.ant.types.PathgetClasspath()
get the classpath used by this LoadProperties.

return
The classpath

        return isReference()
            ? ((JavaResource) getCheckedRef()).getClasspath() : classpath;
    
public java.io.InputStreamgetInputStream()
Return an InputStream for reading the contents of this Resource.

return
an InputStream object.
throws
IOException if an error occurs.

        if (isReference()) {
            return ((Resource) getCheckedRef()).getInputStream();
        }
        ClassLoader cl = null;
        if (loader != null) {
            cl = (ClassLoader) loader.getReferencedObject();
        }
        if (cl == null) {
            if (getClasspath() != null) {
                cl = getProject().createClassLoader(classpath);
            } else {
                cl = JavaResource.class.getClassLoader();
            }
            if (loader != null && cl != null) {
                getProject().addReference(loader.getRefId(), cl);
            }
        }

        return cl == null ? ClassLoader.getSystemResourceAsStream(getName())
            : cl.getResourceAsStream(getName());
    
public booleanisExists()
Learn whether this file exists.

return
true if this resource exists.

        InputStream is = null;
        try {
            return isReference() ? ((Resource) getCheckedRef()).isExists()
                : (is = getInputStream()) != null;
        } catch (IOException ex) {
            return false;
        } finally {
            FileUtils.close(is);
        }
    
public voidsetClasspath(org.apache.tools.ant.types.Path classpath)
Set the classpath to use when looking up a resource.

param
classpath to add to any existing classpath

        checkAttributesAllowed();
        if (this.classpath == null) {
            this.classpath = classpath;
        } else {
            this.classpath.append(classpath);
        }
    
public voidsetClasspathRef(org.apache.tools.ant.types.Reference r)
Set the classpath to use when looking up a resource, given as reference to a <path> defined elsewhere

param
r The reference value

        checkAttributesAllowed();
        createClasspath().setRefid(r);
    
public voidsetLoaderRef(org.apache.tools.ant.types.Reference r)
Use the reference to locate the loader. If the loader is not found, taskdef will use the specified classpath and register it with the specified name. This allow multiple taskdef/typedef to use the same class loader, so they can be used together. It eliminate the need to put them in the CLASSPATH.

param
r the reference to locate the loader.

        checkAttributesAllowed();
        loader = r;
    
public voidsetRefid(org.apache.tools.ant.types.Reference r)
Overrides the super version.

param
r the Reference to set.

        if (loader != null || classpath != null) {
            throw tooManyAttributes();
        }
        super.setRefid(r);