1
0

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.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@209571 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Kitching
2005-07-07 07:32:16 +00:00
parent d0281e782f
commit 3083b9a334
2 changed files with 23 additions and 45 deletions

View File

@@ -31,12 +31,24 @@ import java.io.InputStream;
import java.io.IOException; 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> * <p>
* Note that this classloader is not "industrial strength"; users * Note that this classloader is not "industrial strength"; users
* looking for such a class may wish to look at the Tomcat sourcecode * looking for such a class may wish to look at the Tomcat sourcecode
* instead. In particular, this class may not be threadsafe. * 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 { public class PathableClassLoader extends URLClassLoader {
private static final URL[] NO_URLS = new URL[0]; private static final URL[] NO_URLS = new URL[0];
@@ -239,44 +251,4 @@ public class PathableClassLoader extends URLClassLoader {
return super.getResourceAsStream(name); 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();
}
}
} }

View File

@@ -230,15 +230,21 @@ public class ChildFirstTestCase extends TestCase {
assertEquals("Unexpected number of PathableTestSuite.class resources found", 1, urls.length); assertEquals("Unexpected number of PathableTestSuite.class resources found", 1, urls.length);
// getResources where the resource exists in both. // 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"); resources = childLoader.getResources("org/apache/commons/logging/impl/Log4J12Logger.class");
urls = toURLArray(resources); urls = toURLArray(resources);
assertEquals("Unexpected number of Log4J12Logger.class resources found", 2, urls.length); assertEquals("Unexpected number of Log4J12Logger.class resources found", 2, urls.length);
assertTrue("Incorrect source for Log4J12Logger class", 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", assertTrue("Incorrect source for Log4J12Logger class",
urls[1].toString().indexOf("/commons-logging-1.") > 0); urls[1].toString().indexOf("/commons-logging-adapters-1.") > 0);
} }
/** /**