Return-Path: Delivered-To: apmail-jakarta-ant-dev-archive@jakarta.apache.org Received: (qmail 2345 invoked by uid 500); 2 Jul 2001 09:56:12 -0000 Mailing-List: contact ant-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk Reply-To: ant-dev@jakarta.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list ant-dev@jakarta.apache.org Received: (qmail 1921 invoked by uid 500); 2 Jul 2001 09:56:04 -0000 Delivered-To: apmail-jakarta-ant-cvs@apache.org Date: 2 Jul 2001 09:55:46 -0000 Message-ID: <20010702095546.1827.qmail@apache.org> From: conor@apache.org To: jakarta-ant-cvs@apache.org Subject: cvs commit: jakarta-ant/src/main/org/apache/tools/ant AntClassLoader.java conor 01/07/02 02:55:45 Modified: src/main/org/apache/tools/ant AntClassLoader.java Log: Add support for getResources to the classloader Submitted by: David A. Herman Revision Changes Path 1.20 +103 -0 jakarta-ant/src/main/org/apache/tools/ant/AntClassLoader.java Index: AntClassLoader.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/AntClassLoader.java,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- AntClassLoader.java 2001/05/11 06:27:03 1.19 +++ AntClassLoader.java 2001/07/02 09:55:41 1.20 @@ -71,6 +71,97 @@ * @author Jesse Glick */ public class AntClassLoader extends ClassLoader { + + /** + * An enumeration of all resources of a given name found within the + * classpath of this class loader. This enumeration is used by the + * {@link #findResources(String) findResources} method, which is in + * turn used by the + * {@link ClassLoader#getResources ClassLoader.getResources} method. + * + * @see AntClassLoader#findResources(String) + * @see java.lang.ClassLoader#getResources(String) + * @author David A. Herman + */ + private class ResourceEnumeration implements Enumeration { + + /** + * The name of the resource being searched for. + */ + private String resourceName; + + /** + * The array of classpath elements. + */ + private String[] pathElements; + + /** + * The index of the next classpath element to search. + */ + private int pathElementsIndex; + + /** + * The URL of the next resource to return in the enumeration. If this + * field is null then the enumeration has been completed, + * i.e., there are no more elements to return. + */ + private URL nextResource; + + /** + * Construct a new enumeration of resources of the given name found + * within this class loader's classpath. + * + * @param name the name of the resource to search for. + */ + ResourceEnumeration(String name) { + this.resourceName = name; + this.pathElements = AntClassLoader.this.classpath.list(); + this.pathElementsIndex = 0; + findNextResource(); + } + + /** + * Indicates whether there are more elements in the enumeration to + * return. + * + * @return true if there are more elements in the + * enumeration; false otherwise. + */ + public boolean hasMoreElements() { + return (this.nextResource != null); + } + + /** + * Returns the next resource in the enumeration. + * + * @return the next resource in the enumeration. + */ + public Object nextElement() { + URL ret = this.nextResource; + findNextResource(); + return ret; + } + + /** + * Locates the next resource of the correct name in the classpath and + * sets nextResource to the URL of that resource. If no + * more resources can be found, nextResource is set to + * null. + */ + private void findNextResource() { + URL url = null; + while ((this.pathElementsIndex < this.pathElements.length) && + (url == null)) { + File pathComponent = AntClassLoader.this.project.resolveFile( + (String)this.pathElements[this.pathElementsIndex]); + url = getResourceURL(pathComponent, this.resourceName); + this.pathElementsIndex++; + } + this.nextResource = url; + } + + } + /** * The size of buffers to be used in this classloader. */ @@ -436,6 +527,18 @@ } return url; + } + + /** + * Returns an enumeration of URLs representing all the resources with the + * given name by searching the class loader's classpath. + * + * @param name the resource name. + * @return an enumeration of URLs for the resources. + * @throws IOException if I/O errors occurs (can't happen) + */ + protected Enumeration findResources(String name) throws IOException { + return new ResourceEnumeration(name); } /**