You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2006/02/23 08:19:10 UTC

svn commit: r380059 - in /incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource: ResourceLoader.java impl/ impl/ResourceLoaderImpl.java loader/

Author: jsdelfino
Date: Wed Feb 22 23:19:06 2006
New Revision: 380059

URL: http://svn.apache.org/viewcvs?rev=380059&view=rev
Log:
some minor refactoring

Added:
    incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/ResourceLoader.java   (with props)
    incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/impl/
    incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/impl/ResourceLoaderImpl.java   (with props)
Removed:
    incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/loader/

Added: incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/ResourceLoader.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/ResourceLoader.java?rev=380059&view=auto
==============================================================================
--- incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/ResourceLoader.java (added)
+++ incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/ResourceLoader.java Wed Feb 22 23:19:06 2006
@@ -0,0 +1,83 @@
+/**
+ *
+ *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed 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.tuscany.common.resource;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Interface which abstracts the implementation of something that is able to
+ * load resources (such as a ClassLoader). All Tuscany code should use this
+ * API rather than a ClassLoader directly in order to reduce the risk of
+ * memory leaks due to ClassLoader references.
+ *
+ * @version $Rev: 379878 $ $Date: 2006-02-22 12:45:50 -0800 (Wed, 22 Feb 2006) $
+ */
+public interface ResourceLoader {
+
+    /**
+     * Returns the parent resource loaders.
+     *
+     * @return resource loaders that are parents to this one
+     */
+    List<ResourceLoader> getParents();
+
+    /**
+     * Loads the class with the specified binary name.
+     *
+     * @param name the binary name of the class
+     * @return the resulting Class object
+     * @throws ClassNotFoundException if the class was not found
+     * @see ClassLoader#loadClass(String)
+     */
+    Class<?> loadClass(String name) throws ClassNotFoundException;
+
+    /**
+     * Finds the first resource with the given name.
+     * <p/>
+     * Each parent is searched first (in the order returned by {@link #getParents()})
+     * and the first resource located is found. If no parent returns a resource then
+     * the first resource defined by this ResourceLoader is returned.
+     *
+     * @param name the resource name
+     * @return a {@link URL} that can be used to read the resource, or null if no resource could be found
+     * @throws IOException if there was a problem locating the resource
+     */
+    URL getResource(String name) throws IOException;
+
+    /**
+     * Find resources with the given name that are available directly from this
+     * ResourceLoader. Resources from parent ResourceLoaders are not returned.
+     *
+     * @param name the resource name
+     * @return an Iterator of {@link URL} objects for the resource
+     * @throws IOException if there was a problem locating the resources
+     */
+    Iterator<URL> getResources(String name) throws IOException;
+
+    /**
+     * Find resources with the given name that are available from this
+     * ResourceLoader or any of its parents.
+     *
+     * @param name the resource name
+     * @return an Iterator of {@link URL} objects for the resource
+     * @throws IOException if there was a problem locating the resources
+     */
+    Iterator<URL> getAllResources(String name) throws IOException;
+}

Propchange: incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/ResourceLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/ResourceLoader.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/impl/ResourceLoaderImpl.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/impl/ResourceLoaderImpl.java?rev=380059&view=auto
==============================================================================
--- incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/impl/ResourceLoaderImpl.java (added)
+++ incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/impl/ResourceLoaderImpl.java Wed Feb 22 23:19:06 2006
@@ -0,0 +1,135 @@
+/**
+ *
+ *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed 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.tuscany.common.resource.impl;
+
+import java.io.IOException;
+import java.lang.ref.WeakReference;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.tuscany.common.resource.ResourceLoader;
+
+/**
+ * Default implementation of the ResourceLoader interface.
+ *
+ * @version $Rev: 369102 $ $Date: 2006-01-14 13:48:56 -0800 (Sat, 14 Jan 2006) $
+ */
+public class ResourceLoaderImpl implements ResourceLoader {
+    private final WeakReference<ClassLoader> classLoaderReference;
+    private final List<ResourceLoader> parents;
+
+    /**
+     * Constructs a new ResourceLoaderImpl.
+     * @param classLoader
+     */
+    public ResourceLoaderImpl(ClassLoader classLoader) {
+        classLoaderReference = new WeakReference(classLoader);
+        ClassLoader parentCL = classLoader.getParent();
+        parents = parentCL == null ? Collections.EMPTY_LIST : Collections.singletonList(new ResourceLoaderImpl(parentCL));
+    }
+
+
+    /**
+     * Return the classloader backing this resource loader.
+     *
+     * @return the classloader that backs this resource loader
+     * @throws IllegalStateException if the classloader has been garbage collected
+     */
+    private ClassLoader getClassLoader() throws IllegalStateException {
+        ClassLoader cl = classLoaderReference.get();
+        if (cl == null) {
+            throw new IllegalStateException("Referenced ClassLoader has been garbage collected");
+        }
+        return cl;
+    }
+
+    public List<ResourceLoader> getParents() {
+        return parents;
+    }
+
+    public Class loadClass(String name) throws ClassNotFoundException {
+        return getClassLoader().loadClass(name);
+    }
+
+    public Iterator<URL> getResources(String name) throws IOException {
+        // This implementation used to cache but users are not likely
+        // to ask for the same resource multiple times.
+
+        // Create a new set, add all the resources visible from the current ClassLoader
+        Set<URL> set = new HashSet();
+        ClassLoader classLoader = getClassLoader();
+        for (Enumeration<URL> e = classLoader.getResources(name); e.hasMoreElements();) {
+            set.add(e.nextElement());
+        }
+
+        // Remove the resources visible from the parent ClassLoaders
+        for (ResourceLoader parent : getParents()) {
+            for (Iterator<URL> i = parent.getAllResources(name); i.hasNext();) {
+                set.remove(i.next());
+            }
+        }
+        return set.iterator();
+    }
+
+    public Iterator<URL> getAllResources(String name) throws IOException {
+        return new EnumerationIterator(getClassLoader().getResources(name));
+    }
+
+    public URL getResource(String name) throws IOException {
+        return getClassLoader().getResource(name);
+    }
+
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (!(obj instanceof ResourceLoaderImpl)) {
+            return false;
+        }
+        final ResourceLoaderImpl other = (ResourceLoaderImpl) obj;
+        return getClassLoader() == other.getClassLoader();
+    }
+
+    public int hashCode() {
+        return getClassLoader().hashCode();
+    }
+
+    private static class EnumerationIterator<E> implements Iterator<E> {
+        private final Enumeration<E> e;
+
+        public EnumerationIterator(Enumeration<E> e) {
+            this.e = e;
+        }
+
+        public boolean hasNext() {
+            return e.hasMoreElements();
+        }
+
+        public E next() {
+            return e.nextElement();
+        }
+
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+    }
+}

Propchange: incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/impl/ResourceLoaderImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/common/src/main/java/org/apache/tuscany/common/resource/impl/ResourceLoaderImpl.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date