You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sk...@apache.org on 2005/07/07 09:32:19 UTC

svn commit: r209571 - in /jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging: PathableClassLoader.java pathable/ChildFirstTestCase.java

Author: skitching
Date: Thu Jul  7 00:32:16 2005
New Revision: 209571

URL: http://svn.apache.org/viewcvs?rev=209571&view=rev
Log:
Removed method PathableClassLoader.getResources as in java1.4 and earlier this
method is final (can't be overridden). This means changing the associated unit
test too. As getResources doesn't explicitly indicate the order in which
resources will be returned this is technically ok, though a little ugly.

Modified:
    jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/PathableClassLoader.java
    jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/pathable/ChildFirstTestCase.java

Modified: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/PathableClassLoader.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/PathableClassLoader.java?rev=209571&r1=209570&r2=209571&view=diff
==============================================================================
--- jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/PathableClassLoader.java (original)
+++ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/PathableClassLoader.java Thu Jul  7 00:32:16 2005
@@ -31,12 +31,24 @@
 import java.io.IOException;
 
 /**
- * A ClassLoader which sees only the specified classes.
+ * A ClassLoader which sees only specified classes, and which can be
+ * set to do parent-first or child-first path lookup.
  * <p>
  * Note that this classloader is not "industrial strength"; users
  * looking for such a class may wish to look at the Tomcat sourcecode
  * instead. In particular, this class may not be threadsafe.
+ * <p>
+ * Note that the ClassLoader.getResources method isn't overloaded here.
+ * It would be nice to ensure that when child-first lookup is set the
+ * resources from the child are returned earlier in the list than the
+ * resources from the parent. However overriding this method isn't possible
+ * as the java 1.4 version of ClassLoader declares this method final
+ * (though the java 1.5 version has removed the final qualifier). As the
+ * ClassLoader javadoc doesn't specify the order in which resources
+ * are returned, it's valid to return the resources in any order (just
+ * untidy) so the inherited implementation is technically ok.
  */
+
 public class PathableClassLoader extends URLClassLoader {
     
     private static final URL[] NO_URLS = new URL[0];
@@ -237,46 +249,6 @@
                 }
             }
             return super.getResourceAsStream(name);
-        }
-    }
-    
-    /**
-     * Same as parent class method except that when parentFirst is false
-     * the resources available from this class are returned at the head of
-     * the list instead of the tail.
-     */
-    public Enumeration getResources(String name) throws IOException {
-        if (parentFirst) {
-            return super.getResources(name);
-        } else {
-            Enumeration localResources = super.findResources(name);
-            ClassLoader parentLoader = getParent();
-            if (parentLoader == null) {
-                // There is no way, as far as I am aware, to call
-                // getResources on the bootclassloader. The Class
-                // class has methods getResource and getResourceAsStream
-                // but not getResources. So I guess we just assume there
-                // aren't any matches in the bootloader..
-                return localResources;
-            }
-            Enumeration parentResources = parentLoader.getResources(name);
-            
-            if (!localResources.hasMoreElements()) {
-                return parentResources;
-            }
-            
-            if (!parentResources.hasMoreElements()) {
-                return localResources;
-            }
-
-            Vector v = new Vector();
-            while (localResources.hasMoreElements()) {
-                v.add(localResources.nextElement());
-            }
-            while (parentResources.hasMoreElements()) {
-                v.add(parentResources.nextElement());
-            }
-            return v.elements();
         }
     }
 }

Modified: jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/pathable/ChildFirstTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/pathable/ChildFirstTestCase.java?rev=209571&r1=209570&r2=209571&view=diff
==============================================================================
--- jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/pathable/ChildFirstTestCase.java (original)
+++ jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/pathable/ChildFirstTestCase.java Thu Jul  7 00:32:16 2005
@@ -230,15 +230,21 @@
         assertEquals("Unexpected number of PathableTestSuite.class resources found", 1, urls.length);
         
         // getResources where the resource exists in both.
-        // resources should be returned in order (child-resource, parent-resource)
+        // resources should be returned in order (child-resource, parent-resource).
+        //
+        // IMPORTANT: due to the fact that in java 1.4 and earlier method
+        // ClassLoader.getResources is final it isn't possible for PathableClassLoader
+        // to override this. So even when child-first is enabled the resource order
+        // is still (parent-resources, child-resources). This test verifies the expected
+        // behaviour - even though it's not the desired behaviour.
+        
         resources = childLoader.getResources("org/apache/commons/logging/impl/Log4J12Logger.class");
         urls = toURLArray(resources);
         assertEquals("Unexpected number of Log4J12Logger.class resources found", 2, urls.length);
         assertTrue("Incorrect source for Log4J12Logger class",
-                urls[0].toString().indexOf("/commons-logging-adapters-1.") > 0);
+                urls[0].toString().indexOf("/commons-logging-1.") > 0);
         assertTrue("Incorrect source for Log4J12Logger class",
-                urls[1].toString().indexOf("/commons-logging-1.") > 0);
-        
+                urls[1].toString().indexOf("/commons-logging-adapters-1.") > 0);
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org