You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by jb...@apache.org on 2006/01/14 22:48:58 UTC

svn commit: r369102 - in /incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/loader: ResourceLoader.java impl/ResourceLoaderImpl.java

Author: jboynes
Date: Sat Jan 14 13:48:56 2006
New Revision: 369102

URL: http://svn.apache.org/viewcvs?rev=369102&view=rev
Log:
cosmetic cleanup

Modified:
    incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/loader/ResourceLoader.java
    incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/loader/impl/ResourceLoaderImpl.java

Modified: incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/loader/ResourceLoader.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/loader/ResourceLoader.java?rev=369102&r1=369101&r2=369102&view=diff
==============================================================================
--- incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/loader/ResourceLoader.java (original)
+++ incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/loader/ResourceLoader.java Sat Jan 14 13:48:56 2006
@@ -27,17 +27,20 @@
  * API rather than a ClassLoader directly in order to reduce the risk of
  * memory leaks due to ClassLoader references.
  *
+ * @version $Rev$ $Date$
  */
 public interface ResourceLoader {
 
     /**
      * Returns the parent resource loaders.
+     *
      * @return resource loaders that are parents to this one
      */
     List<ResourceLoader> getParents();
 
     /**
      * Loads the class with the specified binary name.
+     *
      * @param name the binary name of the class
      * @return the resulting Class object
      * @throws ClassNotFoundException if the class was not found
@@ -47,10 +50,11 @@
 
     /**
      * Finds the first resource with the given name.
-     *
+     * <p/>
      * Each parent is searched first (in the order returned by {@link #getParents()})
      * and the first resource located is found. If no parent returns a resource then
      * the first resource defined by this ResourceLoader is returned.
+     *
      * @param name the resource name
      * @return a {@link URL} that can be used to read the resource, or null if no resource could be found
      * @throws IOException if there was a problem locating the resource
@@ -60,6 +64,7 @@
     /**
      * Find resources with the given name that are available directly from this
      * ResourceLoader. Resources from parent ResourceLoaders are not returned.
+     *
      * @param name the resource name
      * @return an Iterator of {@link URL} objects for the resource
      * @throws IOException if there was a problem locating the resources
@@ -69,6 +74,7 @@
     /**
      * Find resources with the given name that are available from this
      * ResourceLoader or any of its parents.
+     *
      * @param name the resource name
      * @return an Iterator of {@link URL} objects for the resource
      * @throws IOException if there was a problem locating the resources

Modified: incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/loader/impl/ResourceLoaderImpl.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/loader/impl/ResourceLoaderImpl.java?rev=369102&r1=369101&r2=369102&view=diff
==============================================================================
--- incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/loader/impl/ResourceLoaderImpl.java (original)
+++ incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/loader/impl/ResourceLoaderImpl.java Sat Jan 14 13:48:56 2006
@@ -19,111 +19,81 @@
 import java.io.IOException;
 import java.lang.ref.WeakReference;
 import java.net.URL;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
 
 import org.apache.tuscany.common.resource.loader.ResourceLoader;
 
 /**
  * Default implementation of the ResourceLoader interface.
+ *
+ * @version $Rev$ $Date$
  */
 public class ResourceLoaderImpl implements ResourceLoader {
+    private final WeakReference<ClassLoader> classLoaderReference;
+    private final List<ResourceLoader> parents;
 
-    private final WeakReference classLoaderReference;
-    private List parents;
-    private Map resources;
-
-    /**
-     * Constructor
-     * @param classLoader
-     */
     protected ResourceLoaderImpl(ClassLoader classLoader) {
         classLoaderReference = new WeakReference(classLoader);
+        ClassLoader parentCL = classLoader.getParent();
+        parents = parentCL == null ? Collections.EMPTY_LIST : Collections.singletonList(new ResourceLoaderImpl(parentCL));
     }
 
+
     /**
-     * @see org.apache.tuscany.common.resource.loader.ResourceLoader#getParents()
+     * Return the classloader backing this resource loader.
+     *
+     * @return the classloader that backs this resource loader
+     * @throws IllegalStateException if the classloader has been garbage collected
      */
-    public List getParents() {
-        if (parents == null) {
-            ClassLoader parentClassLoader = ((ClassLoader) classLoaderReference.get()).getParent();
-            parents = Collections.singletonList(new ResourceLoaderImpl(parentClassLoader));
+    private ClassLoader getClassLoader() throws IllegalStateException {
+        ClassLoader cl = classLoaderReference.get();
+        if (cl == null) {
+            throw new IllegalStateException("Referenced ClassLoader has been garbage collected");
         }
+        return cl;
+    }
+
+    public List<ResourceLoader> getParents() {
         return parents;
     }
 
-    /**
-     * @see org.apache.tuscany.common.resource.loader.ResourceLoader#loadClass(java.lang.String)
-     */
     public Class loadClass(String name) throws ClassNotFoundException {
-        return Class.forName(name, true, (ClassLoader) classLoaderReference.get());
+        return getClassLoader().loadClass(name);
     }
 
-    /**
-     * @see org.apache.tuscany.common.resource.loader.ResourceLoader#getResources(java.lang.String)
-     */
-    public Iterator getResources(String name) throws IOException {
-        if (resources == null)
-            resources = new HashMap();
-
-        // Get the cached set of resources
-        Set set = (Set) resources.get(name);
-        if (set != null) {
-            return set.iterator();
-        }
+    public Iterator<URL> getResources(String name) throws IOException {
+        // This implementation used to cache but users are not likely
+        // to ask for the same resource multiple times.
 
         // Create a new set, add all the resources visible from the current ClassLoader
-        set = new HashSet();
-        ClassLoader classLoader = (ClassLoader) classLoaderReference.get();
-        for (Enumeration e = classLoader.getResources(name); e.hasMoreElements();) {
-            URL resource = (URL) e.nextElement();
-            set.add(resource);
+        Set<URL> set = new HashSet();
+        ClassLoader classLoader = getClassLoader();
+        for (Enumeration<URL> e = classLoader.getResources(name); e.hasMoreElements();) {
+            set.add(e.nextElement());
         }
 
         // Remove the resources visible from the parent ClassLoaders
-        for (Iterator p = getParents().iterator(); p.hasNext();) {
-            ResourceLoaderImpl parent = (ResourceLoaderImpl) p.next();
-            for (Iterator i = parent.getAllResources(name); i.hasNext();) {
+        for (ResourceLoader parent : getParents()) {
+            for (Iterator<URL> i = parent.getAllResources(name); i.hasNext();) {
                 set.remove(i.next());
             }
         }
-
-        // Cache the resulting set
-        resources.put(name, set);
-
         return set.iterator();
     }
 
-    /**
-     * @see org.apache.tuscany.common.resource.loader.ResourceLoader#getAllResources(java.lang.String)
-     */
-    public Iterator getAllResources(String name) throws IOException {
-        ClassLoader classLoader = (ClassLoader) classLoaderReference.get();
-        Enumeration e = classLoader.getResources(name);
-        return Collections.list(e).iterator();
+    public Iterator<URL> getAllResources(String name) throws IOException {
+        return new EnumerationIterator(getClassLoader().getResources(name));
     }
 
-    /**
-     * @see org.apache.tuscany.common.resource.loader.ResourceLoader#getResource(java.lang.String)
-     */
     public URL getResource(String name) throws IOException {
-        Iterator resources = getAllResources(name);
-        if (resources.hasNext())
-            return (URL) resources.next();
-        else
-            return null;
+        return getClassLoader().getResource(name);
     }
-    
-    /**
-     * @see java.lang.Object#equals(java.lang.Object)
-     */
+
     public boolean equals(Object obj) {
         if (obj == this) {
             return true;
@@ -132,13 +102,30 @@
             return false;
         }
         final ResourceLoaderImpl other = (ResourceLoaderImpl) obj;
-        return classLoaderReference.get() == other.classLoaderReference.get();
+        return getClassLoader() == other.getClassLoader();
     }
 
-    /**
-     * @see java.lang.Object#hashCode()
-     */
     public int hashCode() {
-        return classLoaderReference.get().hashCode();
+        return getClassLoader().hashCode();
+    }
+
+    private static class EnumerationIterator<E> implements Iterator<E> {
+        private final Enumeration<E> e;
+
+        public EnumerationIterator(Enumeration<E> e) {
+            this.e = e;
+        }
+
+        public boolean hasNext() {
+            return e.hasMoreElements();
+        }
+
+        public E next() {
+            return e.nextElement();
+        }
+
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
     }
 }