commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skitch...@apache.org
Subject svn commit: r209244 - /jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/PathableClassLoader.java
Date Tue, 05 Jul 2005 08:49:55 GMT
Author: skitching
Date: Tue Jul  5 01:49:54 2005
New Revision: 209244

URL: http://svn.apache.org/viewcvs?rev=209244&view=rev
Log:
Fix bugs with child-first behaviour.

Modified:
    jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/PathableClassLoader.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=209244&r1=209243&r2=209244&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
Tue Jul  5 01:49:54 2005
@@ -32,6 +32,10 @@
 
 /**
  * A ClassLoader which sees only the specified classes.
+ * <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.
  */
 public class PathableClassLoader extends URLClassLoader {
     
@@ -174,9 +178,18 @@
         if (parentFirst) {
             return super.loadClass(name, resolve);
         } else {
-            // ok, implement child-first
+            // Implement child-first. 
+            //
+            // It appears that the findClass method doesn't check whether the
+            // class has already been loaded. This seems odd to me, but without
+            // first checking via findLoadedClass we can get java.lang.LinkageError
+            // with message "duplicate class definition" which isn't good.
+            
             try {
-                Class clazz = super.findClass(name);
+                Class clazz = findLoadedClass(name);
+                if (clazz == null) {
+                    clazz = super.findClass(name);
+                }
                 if (resolve) {
                     resolveClass(clazz);
                 }
@@ -236,23 +249,32 @@
         if (parentFirst) {
             return super.getResources(name);
         } else {
-            Enumeration local = super.findResources(name);
-            Enumeration parent = getParent().getResources(name);
+            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 (!local.hasMoreElements()) {
-                return parent;
+            if (!localResources.hasMoreElements()) {
+                return parentResources;
             }
             
-            if (!parent.hasMoreElements()) {
-                return local;
+            if (!parentResources.hasMoreElements()) {
+                return localResources;
             }
 
             Vector v = new Vector();
-            while (local.hasMoreElements()) {
-                v.add(local.nextElement());
+            while (localResources.hasMoreElements()) {
+                v.add(localResources.nextElement());
             }
-            while (parent.hasMoreElements()) {
-                v.add(parent.nextElement());
+            while (parentResources.hasMoreElements()) {
+                v.add(parentResources.nextElement());
             }
             return v.elements();
         }



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


Mime
View raw message