1
0

Allow libs for test to be discovered via the classpath as well as via system properties.

This has been implemented to suppprt running tests via the maven surefire plugin, but is
a general-purpose mechanism.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@427394 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Kitching
2006-08-01 01:13:11 +00:00
parent f7a013c518
commit e7ab5b24d8

View File

@@ -197,22 +197,66 @@ public class PathableClassLoader extends URLClassLoader {
* classes. * classes.
*/ */
public void addLogicalLib(String logicalLib) { public void addLogicalLib(String logicalLib) {
// first, check the system properties
String filename = System.getProperty(logicalLib); String filename = System.getProperty(logicalLib);
if (filename == null) { if (filename != null) {
throw new UnknownError( try {
"Logical lib [" + logicalLib + "] is not defined" URL libUrl = new File(filename).toURL();
+ " as a System property."); addURL(libUrl);
return;
} catch(java.net.MalformedURLException e) {
throw new UnknownError(
"Invalid file [" + filename + "] for logical lib [" + logicalLib + "]");
}
} }
try { // now check the classpath for a similar-named lib
URL url = new File(filename).toURL(); URL libUrl = libFromClasspath(logicalLib);
addURL(url); if (libUrl != null) {
} catch(java.net.MalformedURLException e) { addURL(libUrl);
throw new UnknownError( return;
"Invalid file [" + filename + "] for logical lib [" + logicalLib + "]");
} }
// lib not found
throw new UnknownError(
"Logical lib [" + logicalLib + "] is not defined"
+ " as a System property.");
}
private URL libFromClasspath(String logicalLib) {
ClassLoader cl = this.getClass().getClassLoader();
if (cl instanceof URLClassLoader == false) {
return null;
}
URLClassLoader ucl = (URLClassLoader) cl;
URL[] path = ucl.getURLs();
for(int i=0; i<path.length; ++i) {
URL u = path[i];
// extract the filename bit on the end of the url
String filename = u.toString();
if (!filename.endsWith(".jar")) {
// not a jarfile, ignore it
continue;
}
int lastSlash = filename.lastIndexOf('/');
if (lastSlash >= 0) {
filename = filename.substring(lastSlash+1);
}
if (filename.startsWith(logicalLib)) {
System.out.println("found lib " + logicalLib + " at url " + u);
return u;
} else {
System.out.println("lib " + logicalLib + " does not match [" + filename + "] at url " + u);
}
}
return null;
} }
/** /**
* Override ClassLoader method. * Override ClassLoader method.
* <p> * <p>