You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by co...@apache.org on 2001/07/02 11:55:46 UTC

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 <he...@alumni.grinnell.edu>
  
  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 <a href="mailto:Jesse.Glick@netbeans.com">Jesse Glick</a>
    */
   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 <a href="mailto:hermand@alumni.grinnell.edu">David A. Herman</a>
  +     */
  +    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 <code>null</code> 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 <code>true</code> if there are more elements in the
  +         *         enumeration; <code>false</code> 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 <code>nextResource</code> to the URL of that resource. If no
  +         * more resources can be found, <code>nextResource</code> is set to
  +         * <code>null</code>.
  +         */
  +        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);
       }
   
       /**