cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r662201 - 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 11:55:20 GMT
Author: aadamchik
Date: Sun Jun  1 04:55:19 2008
New Revision: 662201

URL: http://svn.apache.org/viewvc?rev=662201&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=662201&r1=662200&r2=662201&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 04:55:19 2008
@@ -20,7 +20,7 @@
 
 import java.net.URL;
 import java.util.Collection;
-import java.util.List;
+import java.util.LinkedHashSet;
 
 /**
  * A ResourceFinder that looks up resources in the classpath.
@@ -31,7 +31,12 @@
 public class ClasspathResourceFinder implements ResourceFinder {
 
     protected ClassLoader classLoader;
-    protected List<String> extraResourcePackages;
+    protected Collection<String> rootPaths;
+
+    public ClasspathResourceFinder() {
+        rootPaths = new LinkedHashSet<String>(2);
+        rootPaths.add("");
+    }
 
     public URL getResource(String name) {
         return null;
@@ -41,14 +46,40 @@
         return null;
     }
 
-    public void addResourcePackage() {
+    /**
+     * Adds a base path to be used for resource lookup. In the context of
+     * ClasspathResourceFinder, a "path" corresponds to a package name, only separated by
+     * "/" instead of ".". Default root path is empty String. This method allows to add
+     * more lookup roots.
+     */
+    public void addRootPath(String path) {
+        if (path == null) {
+            throw new NullPointerException("Null path");
+        }
+
+        if (path.startsWith("/")) {
+            path = path.substring(1);
+        }
+
+        if (path.endsWith("/")) {
+            path = path.substring(0, path.length() - 1);
+        }
 
+        rootPaths.add(path);
     }
 
+    /**
+     * Returns ClassLoader override initialized via {@link #setClassLoader(ClassLoader)}.
+     * Null by default.
+     */
     public ClassLoader getClassLoader() {
         return classLoader;
     }
 
+    /**
+     * Sets an overriding ClassLoader for this resource finder. Setting it is only needed
+     * if the default thread class loader is not appropriate for looking up the resources.
+     */
     public void setClassLoader(ClassLoader classLoader) {
         this.classLoader = classLoader;
     }
@@ -72,6 +103,7 @@
         if (loader == null) {
             loader = ClassLoader.getSystemClassLoader();
         }
+
         if (loader == null) {
             throw new IllegalStateException(
                     "Can't detect ClassLoader to use for resouyrce location");

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=662201&r1=662200&r2=662201&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 04:55:19 2008
@@ -57,4 +57,18 @@
             Thread.currentThread().setContextClassLoader(old);
         }
     }
+    
+    public void testRootPaths() {
+        ClasspathResourceFinder f1 = new ClasspathResourceFinder();
+        assertEquals(1, f1.rootPaths.size());
+        assertTrue(f1.rootPaths.contains(""));
+        
+        f1.addRootPath("/");
+        assertEquals(1, f1.rootPaths.size());
+        assertTrue(f1.rootPaths.contains(""));
+        
+        f1.addRootPath("/my/package/name/");
+        assertEquals(2, f1.rootPaths.size());
+        assertTrue("" + f1.rootPaths, f1.rootPaths.contains("my/package/name"));
+    }
 }



Mime
View raw message