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

URLResource

public class URLResource extends org.apache.tools.ant.types.Resource
Exposes a URL as a Resource.
since
Ant 1.7

Fields Summary
private static final org.apache.tools.ant.util.FileUtils
FILE_UTILS
private static final int
NULL_URL
private URL
url
private URLConnection
conn
Constructors Summary
public URLResource()
Default constructor.


           
      
    
public URLResource(URL u)
Convenience constructor.

param
u the URL to expose.

        setURL(u);
    
public URLResource(File f)
Convenience constructor.

param
f the File to set as a URL.

        setFile(f);
    
public URLResource(String u)
String constructor for Ant attribute introspection.

param
u String representation of this URL.
see
org.apache.tools.ant.IntrospectionHelper

        this(newURL(u));
    
Methods Summary
private synchronized voidclose()
Closes the URL connection if: - it is opened (i.e. the field conn is not null) - this type of URLConnection supports some sort of close mechanism This method ensures the field conn will be null after the call.

        if (conn != null) {
            try {
                if (conn instanceof JarURLConnection) {
                    JarURLConnection juc = (JarURLConnection) conn;
                    JarFile jf = juc.getJarFile();
                    jf.close();
                    jf = null;
                } else if (conn instanceof HttpURLConnection) {
                    ((HttpURLConnection) conn).disconnect();
                }
            } catch (IOException exc) {
                //ignore
            } finally {
                conn = null;
            }
        }
    
protected synchronized voidconnect()
Ensure that we have a connection.

throws
IOException if the connection cannot be established.

        URL u = getURL();
        if (u == null) {
            throw new BuildException("URL not set");
        }
        if (conn == null) {
            try {
                conn = u.openConnection();
                conn.connect();
            } catch (IOException e) {
                log(e.toString(), Project.MSG_ERR);
                conn = null;
                throw e;
            }
        }
    
public synchronized booleanequals(java.lang.Object another)
Test whether an Object equals this URLResource.

param
another the other Object to compare.
return
true if the specified Object is equal to this Resource.

        if (this == another) {
            return true;
        }
        if (isReference()) {
            return getCheckedRef().equals(another);
        }
        if (!(another.getClass().equals(getClass()))) {
            return false;
        }
        URLResource otheru = (URLResource) another;
        return getURL() == null
            ? otheru.getURL() == null
            : getURL().equals(otheru.getURL());
    
public synchronized java.io.InputStreamgetInputStream()
Get an InputStream for the Resource.

return
an InputStream containing this Resource's content.
throws
IOException if unable to provide the content of this Resource as a stream.
throws
UnsupportedOperationException if InputStreams are not supported for this Resource type.

        if (isReference()) {
            return ((Resource) getCheckedRef()).getInputStream();
        }
        connect();
        try {
            return conn.getInputStream();
        } finally {
            conn = null;
        }
    
public synchronized longgetLastModified()
Tells the modification time in milliseconds since 01.01.1970 .

return
0 if the resource does not exist to mirror the behavior of {@link java.io.File File}.

        if (isReference()) {
            return ((Resource) getCheckedRef()).getLastModified();
        }
        if (!isExists(false)) {
            return 0L;
        }
        return conn.getLastModified();
    
public synchronized java.lang.StringgetName()
Get the name of this URLResource (its file component minus the leading separator).

return
the name of this resource.

        return isReference() ? ((Resource) getCheckedRef()).getName()
            : getURL().getFile().substring(1);
    
public synchronized java.io.OutputStreamgetOutputStream()
Get an OutputStream for the Resource.

return
an OutputStream to which content can be written.
throws
IOException if unable to provide the content of this Resource as a stream.
throws
UnsupportedOperationException if OutputStreams are not supported for this Resource type.
throws
IOException if the URL cannot be opened.

        if (isReference()) {
            return ((Resource) getCheckedRef()).getOutputStream();
        }
        connect();
        try {
            return conn.getOutputStream();
        } finally {
            conn = null;
        }
    
public synchronized longgetSize()
Get the size of this Resource.

return
the size, as a long, 0 if the Resource does not exist (for compatibility with java.io.File), or UNKNOWN_SIZE if not known.

        if (isReference()) {
            return ((Resource) getCheckedRef()).getSize();
        }
        if (!isExists(false)) {
            return 0L;
        }
        try {
            connect();
            long contentlength = conn.getContentLength();
            close();
            return contentlength;
        } catch (IOException e) {
            return UNKNOWN_SIZE;
        }
    
public synchronized java.net.URLgetURL()
Get the URL used by this URLResource.

return
a URL object.

        if (isReference()) {
            return ((URLResource) getCheckedRef()).getURL();
        }
        return url;
     
public synchronized inthashCode()
Get the hash code for this Resource.

return
hash code as int.

        if (isReference()) {
            return getCheckedRef().hashCode();
        }
        return MAGIC * ((getURL() == null) ? NULL_URL : getURL().hashCode());
    
public synchronized booleanisDirectory()
Tells if the resource is a directory.

return
boolean whether the resource is a directory.

        return isReference()
            ? ((Resource) getCheckedRef()).isDirectory()
            : getName().endsWith("/");
    
public synchronized booleanisExists()
Find out whether the URL exists .

return
true if this resource exists.

        if (isReference()) {
            return ((Resource) getCheckedRef()).isExists();
        }
        return isExists(false);
    
private synchronized booleanisExists(boolean closeConnection)
Find out whether the URL exists, and close the connection opened to the URL if closeConnection is true. Note that this method does ensure that if: - the resource exists (if it returns true) - and if the current object is not a reference (isReference() returns false) - and if it was called with closeConnection to false, then the connection to the URL (stored in the conn private field) will be opened, and require to be closed by the caller.

param
closeConnection true if the connection should be closed after the call, false if it should stay open.
return
true if this resource exists.

        if (getURL() == null) {
            return false;
        }
        try {
            connect();
            return true;
        } catch (IOException e) {
            return false;
        } finally {
            if (closeConnection) {
                close();
            }
        }
    
private static java.net.URLnewURL(java.lang.String u)

        try {
            return new URL(u);
        } catch (MalformedURLException e) {
            throw new BuildException(e);
        }
    
public synchronized voidsetFile(java.io.File f)
Set the URL from a File.

param
f the File to set as a URL.

        try {
            setURL(FILE_UTILS.getFileURL(f));
        } catch (MalformedURLException e) {
            throw new BuildException(e);
        }
    
public synchronized voidsetRefid(org.apache.tools.ant.types.Reference r)
Overrides the super version.

param
r the Reference to set.

        //not using the accessor in this case to avoid side effects
        if (url != null) {
            throw tooManyAttributes();
        }
        super.setRefid(r);
    
public synchronized voidsetURL(java.net.URL u)
Set the URL for this URLResource.

param
u the URL to expose.

        checkAttributesAllowed();
        url = u;
    
public synchronized java.lang.StringtoString()
Return this URLResource formatted as a String.

return
a String representation of this URLResource.

        return isReference()
            ? getCheckedRef().toString() : String.valueOf(getURL());