cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r662206 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/conf/ClasspathResourceFinder.java test/java/org/apache/cayenne/conf/ClasspathResourceFinderTest.java
Date Sun, 01 Jun 2008 12:06:30 GMT
Author: aadamchik
Date: Sun Jun  1 05:06:30 2008
New Revision: 662206

URL: http://svn.apache.org/viewvc?rev=662206&view=rev
Log:
CAY-1061 ResourceFinder: abstract API for looking up resources, including multiple resources
for a given name

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ClasspathResourceFinder.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conf/ClasspathResourceFinderTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ClasspathResourceFinder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ClasspathResourceFinder.java?rev=662206&r1=662205&r2=662206&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ClasspathResourceFinder.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ClasspathResourceFinder.java
Sun Jun  1 05:06:30 2008
@@ -18,10 +18,16 @@
  ****************************************************************/
 package org.apache.cayenne.conf;
 
+import java.io.IOException;
 import java.net.URL;
 import java.util.Collection;
+import java.util.Enumeration;
 import java.util.LinkedHashSet;
 
+import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 /**
  * A ResourceFinder that looks up resources in the classpath.
  * 
@@ -30,6 +36,8 @@
  */
 public class ClasspathResourceFinder implements ResourceFinder {
 
+    static final Log logger = LogFactory.getLog(ClasspathResourceFinder.class);
+
     protected ClassLoader classLoader;
     protected Collection<String> rootPaths;
 
@@ -39,11 +47,60 @@
     }
 
     public URL getResource(String name) {
+
+        if (name == null) {
+            throw new NullPointerException("Null resource name");
+        }
+
+        if (name.startsWith("/")) {
+            name = name.substring(1);
+        }
+
+        ClassLoader loader = getResourceClassLoader();
+
+        for (String root : rootPaths) {
+            String fullName = root.length() > 0 ? root + "/" + name : name;
+            logger.debug("searching for resource under: " + fullName);
+            URL url = loader.getResource(fullName);
+            if (url != null) {
+                return url;
+            }
+        }
+
         return null;
     }
 
     public Collection<URL> getResources(String name) {
-        return null;
+        if (name == null) {
+            throw new NullPointerException("Null resource name");
+        }
+
+        if (name.startsWith("/")) {
+            name = name.substring(1);
+        }
+
+        ClassLoader loader = getResourceClassLoader();
+
+        Collection<URL> urls = new LinkedHashSet<URL>();
+        for (String root : rootPaths) {
+            String fullName = root.length() > 0 ? root + "/" + name : name;
+            logger.debug("searching for resources under: " + fullName);
+
+            Enumeration<URL> urlsEn;
+            try {
+                urlsEn = loader.getResources(fullName);
+            }
+            catch (IOException e) {
+                throw new CayenneRuntimeException("Error reading URL resources from "
+                        + fullName);
+            }
+
+            while (urlsEn.hasMoreElements()) {
+                urls.add(urlsEn.nextElement());
+            }
+        }
+
+        return urls;
     }
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conf/ClasspathResourceFinderTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conf/ClasspathResourceFinderTest.java?rev=662206&r1=662205&r2=662206&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conf/ClasspathResourceFinderTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conf/ClasspathResourceFinderTest.java
Sun Jun  1 05:06:30 2008
@@ -20,6 +20,7 @@
 
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.util.Collection;
 
 import org.apache.cayenne.unit.BasicCase;
 
@@ -71,4 +72,28 @@
         assertEquals(2, f1.rootPaths.size());
         assertTrue("" + f1.rootPaths, f1.rootPaths.contains("my/package/name"));
     }
+    
+    public void testGetResource() {
+        ClasspathResourceFinder f1 = new ClasspathResourceFinder();
+        assertNotNull(f1.getResource("org/apache/cayenne/Persistent.class"));
+        
+        assertNull(f1.getResource("/apache/cayenne/Persistent.class"));
+        f1.addRootPath("org");
+        assertNotNull(f1.getResource("/apache/cayenne/Persistent.class"));
+    }
+    
+    public void testGetResources() {
+        ClasspathResourceFinder f1 = new ClasspathResourceFinder();
+        Collection<URL> r1 = f1.getResources("org/apache/cayenne/Persistent.class");
+        assertNotNull(r1);
+        assertEquals(1, r1.size());
+        
+        Collection<URL> r2 = f1.getResources("/apache/cayenne/Persistent.class");
+        assertNotNull(r2);
+        assertEquals(0, r2.size());
+        f1.addRootPath("org");
+        Collection<URL> r3 = f1.getResources("/apache/cayenne/Persistent.class");
+        assertNotNull(r3);
+        assertEquals(1, r3.size());
+    }
 }



Mime
View raw message