commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skitch...@apache.org
Subject svn commit: r427394 - /jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/PathableClassLoader.java
Date Tue, 01 Aug 2006 01:13:11 GMT
Author: skitching
Date: Mon Jul 31 18:13:11 2006
New Revision: 427394

URL: http://svn.apache.org/viewvc?rev=427394&view=rev
Log:
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.

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/viewvc/jakarta/commons/proper/logging/trunk/src/test/org/apache/commons/logging/PathableClassLoader.java?rev=427394&r1=427393&r2=427394&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
Mon Jul 31 18:13:11 2006
@@ -197,22 +197,66 @@
      * classes. 
      */
     public void addLogicalLib(String logicalLib) {
+        // first, check the system properties
         String filename = System.getProperty(logicalLib);
-        if (filename == null) {
-            throw new UnknownError(
-                "Logical lib [" + logicalLib + "] is not defined"
-                + " as a System property.");
+        if (filename != null) {
+            try {
+                URL libUrl = new File(filename).toURL();
+                addURL(libUrl);
+                return;
+            } catch(java.net.MalformedURLException e) {
+                throw new UnknownError(
+                    "Invalid file [" + filename + "] for logical lib [" + logicalLib + "]");
+            }
         }
 
-        try {
-            URL url = new File(filename).toURL();
-            addURL(url);
-        } catch(java.net.MalformedURLException e) {
-            throw new UnknownError(
-                "Invalid file [" + filename + "] for logical lib [" + logicalLib + "]");
+        // now check the classpath for a similar-named lib
+        URL libUrl = libFromClasspath(logicalLib);
+        if (libUrl != null) {
+            addURL(libUrl);
+            return;
         }
+
+        // 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.
      * <p>



---------------------------------------------------------------------
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