git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@349633 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -22,14 +22,16 @@ explicit use beginning in <b>Ant 1.7</b>. | |||||
| <ul> | <ul> | ||||
| <li><a href="#basic">resource</a> - a basic resource.</li> | <li><a href="#basic">resource</a> - a basic resource.</li> | ||||
| <li><a href="#bzip2resource">bzip2resource</a> - a BZip2 compressed resource.</li> | |||||
| <li><a href="#file">file</a> - a file.</li> | <li><a href="#file">file</a> - a file.</li> | ||||
| <li><a href="#tarentry">tarentry</a> - an entry in a tar file.</li> | |||||
| <li><a href="#zipentry">zipentry</a> - an entry in a zip file.</li> | |||||
| <li><a href="#gzipresource">gzipresource</a> - a GZip compressed resource.</li> | <li><a href="#gzipresource">gzipresource</a> - a GZip compressed resource.</li> | ||||
| <li><a href="#bzip2resource">bzip2resource</a> - a BZip2 compressed resource.</li> | |||||
| <li><a href="#url">url</a> - a URL.</li> | |||||
| <li><a href="#string">string</a> - a text string.</li> | |||||
| <li><a href="#javaresource">javaresource</a> - a resource loadable | |||||
| via a Java classloader.</li> | |||||
| <li><a href="#property">property</a> - an Ant property.</li> | <li><a href="#property">property</a> - an Ant property.</li> | ||||
| <li><a href="#string">string</a> - a text string.</li> | |||||
| <li><a href="#tarentry">tarentry</a> - an entry in a tar file.</li> | |||||
| <li><a href="#url">url</a> - a URL.</li> | |||||
| <li><a href="#zipentry">zipentry</a> - an entry in a zip file.</li> | |||||
| </ul> | </ul> | ||||
| <h4><a name="basic">resource</a></h4> | <h4><a name="basic">resource</a></h4> | ||||
| @@ -98,6 +100,45 @@ implementations are also usable as single-element | |||||
| </tr> | </tr> | ||||
| </table> | </table> | ||||
| <h4><a name="javaresource">javaresource</a></h4> | |||||
| <p>Represents a resource loadable via a Java classloader.</p> | |||||
| <table border="1" cellpadding="2" cellspacing="0"> | |||||
| <tr> | |||||
| <td valign="top"><b>Attribute</b></td> | |||||
| <td valign="top"><b>Description</b></td> | |||||
| <td align="center" valign="top"><b>Required</b></td> | |||||
| </tr> | |||||
| <tr> | |||||
| <td valign="top">name</td> | |||||
| <td valign="top">The namer of the resource.</td> | |||||
| <td align="center" valign="top">Yes</td> | |||||
| </tr> | |||||
| <tr> | |||||
| <td valign="top">classpath</td> | |||||
| <td valign="top">the classpath to use when looking up a resource.</td> | |||||
| <td align="center" valign="top">No</td> | |||||
| </tr> | |||||
| <tr> | |||||
| <td valign="top">classpathref</td> | |||||
| <td valign="top">the classpath to use when looking up a resource, | |||||
| given as <a href="../using.html#references">reference</a> | |||||
| to a <code><path></code> defined elsewhere..</td> | |||||
| <td align="center" valign="top">No</td> | |||||
| </tr> | |||||
| <tr> | |||||
| <td valign="top">loaderRef</td> | |||||
| <td valign="top">the name of the loader that is | |||||
| used to load the resource, constructed from the specified classpath.</td> | |||||
| <td align="center" valign="top">No</td> | |||||
| </tr> | |||||
| </table> | |||||
| <p>The classpath can also be specified as nested classpath element, | |||||
| where <b><classpath></b> is a <a | |||||
| href="../using.html#path">path-like</a> structure.</p> | |||||
| <h4><a name="zipentry">zipentry</a></h4> | <h4><a name="zipentry">zipentry</a></h4> | ||||
| <p>Represents an entry in a ZIP archive. The archive can be specified | <p>Represents an entry in a ZIP archive. The archive can be specified | ||||
| @@ -157,14 +198,14 @@ collection.</p> | |||||
| <h4><a name="gzipresource">gzipresource</a></h4> | <h4><a name="gzipresource">gzipresource</a></h4> | ||||
| <p>This is not a stand-alone reource, but a wrapper around another | |||||
| <p>This is not a stand-alone resource, but a wrapper around another | |||||
| resource providing compression of the resource's contents on the fly. | resource providing compression of the resource's contents on the fly. | ||||
| A single element resource collection must be specified as a nested | A single element resource collection must be specified as a nested | ||||
| element.</p> | element.</p> | ||||
| <h4><a name="bzip2resource">bzip2resource</a></h4> | <h4><a name="bzip2resource">bzip2resource</a></h4> | ||||
| <p>This is not a stand-alone reource, but a wrapper around another | |||||
| <p>This is not a stand-alone resource, but a wrapper around another | |||||
| resource providing compression of the resource's contents on the fly. | resource providing compression of the resource's contents on the fly. | ||||
| A single element resource collection must be specified as a nested | A single element resource collection must be specified as a nested | ||||
| element.</p> | element.</p> | ||||
| @@ -0,0 +1,7 @@ | |||||
| <project> | |||||
| <target name="loadManifest"> | |||||
| <loadresource property="manifest"> | |||||
| <javaresource name="META-INF/MANIFEST.MF"/> | |||||
| </loadresource> | |||||
| </target> | |||||
| </project> | |||||
| @@ -67,3 +67,4 @@ propertyresource=org.apache.tools.ant.types.resources.PropertyResource | |||||
| tarentry=org.apache.tools.ant.types.resources.TarResource | tarentry=org.apache.tools.ant.types.resources.TarResource | ||||
| gzipresource=org.apache.tools.ant.types.resources.GZipResource | gzipresource=org.apache.tools.ant.types.resources.GZipResource | ||||
| bzip2resource=org.apache.tools.ant.types.resources.BZip2Resource | bzip2resource=org.apache.tools.ant.types.resources.BZip2Resource | ||||
| javaresource=org.apache.tools.ant.types.resources.JavaResource | |||||
| @@ -0,0 +1,210 @@ | |||||
| /* | |||||
| * Copyright 2005 The Apache Software Foundation | |||||
| * | |||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | |||||
| * you may not use this file except in compliance with the License. | |||||
| * You may obtain a copy of the License at | |||||
| * | |||||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||||
| * | |||||
| * Unless required by applicable law or agreed to in writing, software | |||||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
| * See the License for the specific language governing permissions and | |||||
| * limitations under the License. | |||||
| * | |||||
| */ | |||||
| package org.apache.tools.ant.types.resources; | |||||
| import java.io.IOException; | |||||
| import java.io.InputStream; | |||||
| import org.apache.tools.ant.util.FileUtils; | |||||
| import org.apache.tools.ant.types.Path; | |||||
| import org.apache.tools.ant.types.Resource; | |||||
| import org.apache.tools.ant.types.Reference; | |||||
| /** | |||||
| * A Resource representation of something loadable via a Java classloader. | |||||
| * @since Ant 1.7 | |||||
| */ | |||||
| public class JavaResource extends Resource { | |||||
| private Path classpath; | |||||
| private Reference loader; | |||||
| /** | |||||
| * Default constructor. | |||||
| */ | |||||
| public JavaResource() { | |||||
| } | |||||
| /** | |||||
| * Construct a new JavaResource using the specified name and | |||||
| * classpath. | |||||
| * | |||||
| * @param name the resource name. | |||||
| * @param path the classpath. | |||||
| */ | |||||
| public JavaResource(String name, Path path) { | |||||
| setName(name); | |||||
| classpath = path; | |||||
| } | |||||
| /** | |||||
| * Set the classpath to use when looking up a resource. | |||||
| * @param classpath to add to any existing classpath | |||||
| */ | |||||
| public void setClasspath(Path classpath) { | |||||
| checkAttributesAllowed(); | |||||
| if (this.classpath == null) { | |||||
| this.classpath = classpath; | |||||
| } else { | |||||
| this.classpath.append(classpath); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * Add a classpath to use when looking up a resource. | |||||
| * @return The classpath to be configured | |||||
| */ | |||||
| public Path createClasspath() { | |||||
| checkChildrenAllowed(); | |||||
| if (this.classpath == null) { | |||||
| this.classpath = new Path(getProject()); | |||||
| } | |||||
| return this.classpath.createPath(); | |||||
| } | |||||
| /** | |||||
| * Set the classpath to use when looking up a resource, | |||||
| * given as reference to a <path> defined elsewhere | |||||
| * @param r The reference value | |||||
| */ | |||||
| public void setClasspathRef(Reference r) { | |||||
| checkAttributesAllowed(); | |||||
| createClasspath().setRefid(r); | |||||
| } | |||||
| /** | |||||
| * get the classpath used by this <code>LoadProperties</code>. | |||||
| * @return The classpath | |||||
| */ | |||||
| public Path getClasspath() { | |||||
| return isReference() | |||||
| ? ((JavaResource) getCheckedRef()).getClasspath() : classpath; | |||||
| } | |||||
| /** | |||||
| * 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. | |||||
| */ | |||||
| public void setLoaderRef(Reference r) { | |||||
| checkAttributesAllowed(); | |||||
| loader = r; | |||||
| } | |||||
| /** | |||||
| * Overrides the super version. | |||||
| * @param r the Reference to set. | |||||
| */ | |||||
| public void setRefid(Reference r) { | |||||
| if (loader != null || classpath != null) { | |||||
| throw tooManyAttributes(); | |||||
| } | |||||
| super.setRefid(r); | |||||
| } | |||||
| /** | |||||
| * Learn whether this file exists. | |||||
| * @return true if this resource exists. | |||||
| */ | |||||
| public boolean isExists() { | |||||
| InputStream is = null; | |||||
| try { | |||||
| return isReference() ? ((Resource) getCheckedRef()).isExists() | |||||
| : (is = getInputStream()) != null; | |||||
| } catch (IOException ex) { | |||||
| return false; | |||||
| } finally { | |||||
| FileUtils.close(is); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * Return an InputStream for reading the contents of this Resource. | |||||
| * @return an InputStream object. | |||||
| * @throws IOException if an error occurs. | |||||
| */ | |||||
| public InputStream getInputStream() throws IOException { | |||||
| 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()); | |||||
| } | |||||
| /** | |||||
| * 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. | |||||
| */ | |||||
| public int compareTo(Object another) { | |||||
| 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); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,40 @@ | |||||
| /* | |||||
| * Copyright 2005 The Apache Software Foundation | |||||
| * | |||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | |||||
| * you may not use this file except in compliance with the License. | |||||
| * You may obtain a copy of the License at | |||||
| * | |||||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||||
| * | |||||
| * Unless required by applicable law or agreed to in writing, software | |||||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
| * See the License for the specific language governing permissions and | |||||
| * limitations under the License. | |||||
| * | |||||
| */ | |||||
| package org.apache.tools.ant.types.resources; | |||||
| import org.apache.tools.ant.BuildFileTest; | |||||
| public class JavaResourceTest extends BuildFileTest { | |||||
| public JavaResourceTest(String name) { | |||||
| super(name); | |||||
| } | |||||
| public void setUp() { | |||||
| configureProject("src/etc/testcases/types/resources/javaresource.xml"); | |||||
| } | |||||
| public void testLoadManifest() { | |||||
| executeTarget("loadManifest"); | |||||
| assertNotNull(getProject().getProperty("manifest")); | |||||
| // this actually relies on the first manifest being found on | |||||
| // the classpath (probably rt.jar's) being valid | |||||
| assertTrue(getProject().getProperty("manifest") | |||||
| .startsWith("Manifest-Version:")); | |||||
| } | |||||
| } | |||||