cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r662198 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/conf/ test/java/org/apache/cayenne/conf/
Date Sun, 01 Jun 2008 11:17:55 GMT
Author: aadamchik
Date: Sun Jun  1 04:17:54 2008
New Revision: 662198

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

Added:
    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/main/java/org/apache/cayenne/conf/ResourceFinder.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conf/ClasspathResourceFinderTest.java

Added: 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=662198&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ClasspathResourceFinder.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ClasspathResourceFinder.java
Sun Jun  1 04:17:54 2008
@@ -0,0 +1,82 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.conf;
+
+import java.net.URL;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * A ResourceFinder that looks up resources in the classpath.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+public class ClasspathResourceFinder implements ResourceFinder {
+
+    protected ClassLoader classLoader;
+    protected List<String> extraResourcePackages;
+
+    public URL getResource(String name) {
+        return null;
+    }
+
+    public Collection<URL> getResources(String name) {
+        return null;
+    }
+
+    public void addResourcePackage() {
+
+    }
+
+    public ClassLoader getClassLoader() {
+        return classLoader;
+    }
+
+    public void setClassLoader(ClassLoader classLoader) {
+        this.classLoader = classLoader;
+    }
+
+    /**
+     * Returns a non-null ClassLoader that should be used to locate resources. The lookup
+     * following order is used to find it: explicitly set class loader, current thread
+     * class loader, this class class loader, system class loader.
+     */
+    protected ClassLoader getResourceClassLoader() {
+        ClassLoader loader = this.classLoader;
+
+        if (loader == null) {
+            loader = Thread.currentThread().getContextClassLoader();
+        }
+
+        if (loader == null) {
+            loader = getClass().getClassLoader();
+        }
+
+        if (loader == null) {
+            loader = ClassLoader.getSystemClassLoader();
+        }
+        if (loader == null) {
+            throw new IllegalStateException(
+                    "Can't detect ClassLoader to use for resouyrce location");
+        }
+
+        return loader;
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ResourceFinder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ResourceFinder.java?rev=662198&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ResourceFinder.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/ResourceFinder.java
Sun Jun  1 04:17:54 2008
@@ -0,0 +1,43 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.conf;
+
+import java.net.URL;
+import java.util.Collection;
+
+/**
+ * An object that defines a strategy for locating stream resources.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+public interface ResourceFinder {
+
+    /**
+     * Returns a collection of resource URLs with a given name found in the environment
+     * using some lookup strategy.
+     */
+    Collection<URL> getResources(String name);
+
+    /**
+     * Returns a single resource matching a given name. If more than one resource matches
+     * the name, it is implementation specific which one will be returned.
+     */
+    URL getResource(String name);
+}

Added: 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=662198&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conf/ClasspathResourceFinderTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/conf/ClasspathResourceFinderTest.java
Sun Jun  1 04:17:54 2008
@@ -0,0 +1,60 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.conf;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+
+import org.apache.cayenne.unit.BasicCase;
+
+public class ClasspathResourceFinderTest extends BasicCase {
+
+    public void testClassLoader() {
+
+        ClasspathResourceFinder finder = new ClasspathResourceFinder();
+        assertNull(finder.getClassLoader());
+
+        ClassLoader cl1 = new URLClassLoader(new URL[0]);
+        finder.setClassLoader(cl1);
+        assertSame(cl1, finder.getClassLoader());
+    }
+
+    public void testGetResourceClassLoader() {
+
+        ClasspathResourceFinder f1 = new ClasspathResourceFinder();
+        ClassLoader cl1 = new URLClassLoader(new URL[0]);
+        f1.setClassLoader(cl1);
+        assertSame(cl1, f1.getResourceClassLoader());
+
+        ClasspathResourceFinder f2 = new ClasspathResourceFinder();
+        ClassLoader cl2 = new URLClassLoader(new URL[0]);
+        ClassLoader old = Thread.currentThread().getContextClassLoader();
+        try {
+            Thread.currentThread().setContextClassLoader(cl2);
+            assertSame(cl2, f2.getResourceClassLoader());
+
+            Thread.currentThread().setContextClassLoader(null);
+            assertSame(ClasspathResourceFinder.class.getClassLoader(), f2
+                    .getResourceClassLoader());
+        }
+        finally {
+            Thread.currentThread().setContextClassLoader(old);
+        }
+    }
+}



Mime
View raw message