diff --git a/src/main/org/apache/tools/ant/AntClassLoader.java b/src/main/org/apache/tools/ant/AntClassLoader.java index cc90af385..5eec9fa6c 100644 --- a/src/main/org/apache/tools/ant/AntClassLoader.java +++ b/src/main/org/apache/tools/ant/AntClassLoader.java @@ -71,6 +71,97 @@ import org.apache.tools.ant.types.Path; * @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. */ @@ -438,6 +529,18 @@ public class AntClassLoader extends ClassLoader { 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); + } + /** * Get an inputstream to a given resource in the given file which may * either be a directory or a zip file.