geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject svn commit: r432861 [2/3] - in /geronimo/xbean/branches/classloader: ./ xbean-classloaders/ xbean-classloaders/src/ xbean-classloaders/src/main/ xbean-classloaders/src/main/java/ xbean-classloaders/src/main/java/org/ xbean-classloaders/src/main/java/or...
Date Sat, 19 Aug 2006 18:56:30 GMT
Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/MultiParentClassLoader.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/MultiParentClassLoader.java?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/MultiParentClassLoader.java (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/MultiParentClassLoader.java Sat Aug 19 11:56:27 2006
@@ -0,0 +1,345 @@
+/**
+ *
+ * Copyright 2005-2006 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.xbean.server.classloader;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLStreamHandlerFactory;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+
+/**
+ * A MultiParentClassLoader is a simple extension of the URLClassLoader that simply changes the single parent class
+ * loader model to support a list of parent class loaders.  Each operation that accesses a parent, has been replaced
+ * with a operation that checks each parent in order.  This getParent method of this class will always return null,
+ * which may be interperated by the calling code to mean that this class loader is a direct child of the system class
+ * loader.
+ *
+ * @author Dain Sundstrom
+ * @version $Id$
+ * @since 2.0
+ */
+public class MultiParentClassLoader extends NamedClassLoader {
+    private final ClassLoader[] parents;
+    private final boolean inverseClassLoading;
+    private final String[] hiddenClasses;
+    private final String[] nonOverridableClasses;
+    private final String[] hiddenResources;
+    private final String[] nonOverridableResources;
+
+    /**
+     * Creates a named class loader with no parents.
+     * @param name the name of this class loader
+     * @param urls the urls from which this class loader will classes and resources
+     */
+    public MultiParentClassLoader(String name, URL[] urls) {
+        this(name, urls, ClassLoader.getSystemClassLoader());
+    }
+
+    /**
+     * Creates a named class loader as a child of the specified parent.
+     * @param name the name of this class loader
+     * @param urls the urls from which this class loader will classes and resources
+     * @param parent the parent of this class loader
+     */
+    public MultiParentClassLoader(String name, URL[] urls, ClassLoader parent) {
+        this(name, urls, new ClassLoader[] {parent});
+    }
+
+    /**
+     * Creates a named class loader as a child of the specified parent and using the specified URLStreamHandlerFactory
+     * for accessing the urls..
+     * @param name the name of this class loader
+     * @param urls the urls from which this class loader will classes and resources
+     * @param parent the parent of this class loader
+     * @param factory the URLStreamHandlerFactory used to access the urls
+     */
+    public MultiParentClassLoader(String name, URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
+        this(name, urls, new ClassLoader[] {parent}, factory);
+    }
+
+    /**
+     * Creates a named class loader as a child of the specified parents.
+     * @param name the name of this class loader
+     * @param urls the urls from which this class loader will classes and resources
+     * @param parents the parents of this class loader
+     */
+    public MultiParentClassLoader(String name, URL[] urls, ClassLoader[] parents) {
+        this(name, urls, parents, false, new String[0], new String[0]);
+    }
+
+    public MultiParentClassLoader(String name, URL[] urls, ClassLoader parent, boolean inverseClassLoading, String[] hiddenClasses, String[] nonOverridableClasses) {
+        this(name, urls, new ClassLoader[]{parent}, inverseClassLoading, hiddenClasses, nonOverridableClasses);
+    }
+    
+    /**
+     * Creates a named class loader as a child of the specified parents and using the specified URLStreamHandlerFactory
+     * for accessing the urls..
+     * @param name the name of this class loader
+     * @param urls the urls from which this class loader will classes and resources
+     * @param parents the parents of this class loader
+     * @param factory the URLStreamHandlerFactory used to access the urls
+     */
+    public MultiParentClassLoader(String name, URL[] urls, ClassLoader[] parents, URLStreamHandlerFactory factory) {
+        super(name, urls, null, factory);
+        this.parents = copyParents(parents);
+        this.inverseClassLoading = false;
+        this.hiddenClasses = new String[0];
+        this.nonOverridableClasses = new String[0];
+        this.hiddenResources = new String[0];
+        this.nonOverridableResources = new String[0];
+    }
+
+    public MultiParentClassLoader(String name, URL[] urls, ClassLoader[] parents, boolean inverseClassLoading, Collection hiddenClasses, Collection nonOverridableClasses) {
+        this(name, urls, parents, inverseClassLoading, (String[]) hiddenClasses.toArray(new String[hiddenClasses.size()]), (String[]) nonOverridableClasses.toArray(new String[nonOverridableClasses.size()]));
+    }
+
+    public MultiParentClassLoader(String name, URL[] urls, ClassLoader[] parents, boolean inverseClassLoading, String[] hiddenClasses, String[] nonOverridableClasses) {
+        super(name, urls);
+        this.parents = copyParents(parents);
+        this.inverseClassLoading = inverseClassLoading;
+        this.hiddenClasses = hiddenClasses;
+        this.nonOverridableClasses = nonOverridableClasses;
+        hiddenResources = toResources(hiddenClasses);
+        nonOverridableResources = toResources(nonOverridableClasses);
+    }
+
+    private static String[] toResources(String[] classes) {
+        String[] resources = new String[classes.length];
+        for (int i = 0; i < classes.length; i++) {
+            String className = classes[i];
+            resources[i] = className.replace('.', '/');
+        }
+        return resources;
+    }
+    
+    private static ClassLoader[] copyParents(ClassLoader[] parents) {
+        ClassLoader[] newParentsArray = new ClassLoader[parents.length];
+        for (int i = 0; i < parents.length; i++) {
+            ClassLoader parent = parents[i];
+            if (parent == null) {
+                throw new NullPointerException("parent[" + i + "] is null");
+            }
+            newParentsArray[i] = parent;
+        }
+        return newParentsArray;
+    }
+
+    /**
+     * Gets the parents of this class loader.
+     * @return the parents of this class loader
+     */
+    public ClassLoader[] getParents() {
+        return parents;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
+        //
+        // Check if class is in the loaded classes cache
+        //
+        Class cachedClass = findLoadedClass(name);
+        if (cachedClass != null) {
+            return resolveClass(cachedClass, resolve);
+        }
+        
+        //
+        // if we are using inverse class loading, check local urls first
+        //
+        if (inverseClassLoading && !isDestroyed() && !isNonOverridableClass(name)) {
+            try {
+                Class clazz = findClass(name);
+                return resolveClass(clazz, resolve);
+            } catch (ClassNotFoundException ignored) {
+            }
+        }
+
+        //
+        // Check parent class loaders
+        //
+        if (!isHiddenClass(name)) {
+            for (int i = 0; i < parents.length; i++) {
+                ClassLoader parent = parents[i];
+                try {
+                    Class clazz = parent.loadClass(name);
+                    return resolveClass(clazz, resolve);
+                } catch (ClassNotFoundException ignored) {
+                    // this parent didn't have the class; try the next one
+                }
+            }
+        }
+
+        //
+        // if we are not using inverse class loading, check local urls now
+        //
+        // don't worry about excluding non-overridable classes here... we
+        // have alredy checked he parent and the parent didn't have the
+        // class, so we can override now
+        if (!isDestroyed()) {
+            try {
+                Class clazz = findClass(name);
+                return resolveClass(clazz, resolve);
+            } catch (ClassNotFoundException ignored) {
+            }
+        }
+
+        throw new ClassNotFoundException(name + " in classloader " + name);
+    }
+
+    private boolean isNonOverridableClass(String name) {
+        for (int i = 0; i < nonOverridableClasses.length; i++) {
+            if (name.startsWith(nonOverridableClasses[i])) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private boolean isHiddenClass(String name) {
+        for (int i = 0; i < hiddenClasses.length; i++) {
+            if (name.startsWith(hiddenClasses[i])) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private Class resolveClass(Class clazz, boolean resolve) {
+        if (resolve) {
+            resolveClass(clazz);
+        }
+        return clazz;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public URL getResource(String name) {
+        if (isDestroyed()) {
+            return null;
+        }
+
+        //
+        // if we are using inverse class loading, check local urls first
+        //
+        if (inverseClassLoading && !isDestroyed() && !isNonOverridableResource(name)) {
+            URL url = findResource(name);
+            if (url != null) {
+                return url;
+            }
+        }
+
+        //
+        // Check parent class loaders
+        //
+        if (!isHiddenResource(name)) {
+            for (int i = 0; i < parents.length; i++) {
+                ClassLoader parent = parents[i];
+                URL url = parent.getResource(name);
+                if (url != null) {
+                    return url;
+                }
+            }
+        }
+
+        //
+        // if we are not using inverse class loading, check local urls now
+        //
+        // don't worry about excluding non-overridable resources here... we
+        // have alredy checked he parent and the parent didn't have the
+        // resource, so we can override now
+        if (!isDestroyed()) {
+            // parents didn't have the resource; attempt to load it from my urls
+            return findResource(name);
+        }
+
+        return null;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Enumeration findResources(String name) throws IOException {
+        if (isDestroyed()) {
+            return Collections.enumeration(Collections.EMPTY_SET);
+        }
+
+        List resources = new ArrayList();
+
+        //
+        // if we are using inverse class loading, add the resources from local urls first
+        //
+        if (inverseClassLoading && !isDestroyed()) {
+            List myResources = Collections.list(super.findResources(name));
+            resources.addAll(myResources);
+        }
+
+        //
+        // Add parent resources
+        //
+        for (int i = 0; i < parents.length; i++) {
+            ClassLoader parent = parents[i];
+            List parentResources = Collections.list(parent.getResources(name));
+            resources.addAll(parentResources);
+        }
+
+        //
+        // if we are not using inverse class loading, add the resources from local urls now
+        //
+        if (!inverseClassLoading && !isDestroyed()) {
+            List myResources = Collections.list(super.findResources(name));
+            resources.addAll(myResources);
+        }
+
+        return Collections.enumeration(resources);
+    }
+
+    private boolean isNonOverridableResource(String name) {
+        for (int i = 0; i < nonOverridableResources.length; i++) {
+            if (name.startsWith(nonOverridableResources[i])) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private boolean isHiddenResource(String name) {
+        for (int i = 0; i < hiddenResources.length; i++) {
+            if (name.startsWith(hiddenResources[i])) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String toString() {
+        return "[" + getClass().getName() + ":" +
+                " name=" + getName() +
+                " urls=" + Arrays.asList(getURLs()) +
+                " parents=" + Arrays.asList(parents) +
+                "]";
+    }
+}

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/MultiParentClassLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/MultiParentClassLoader.java
------------------------------------------------------------------------------
    svn:keywords = "Date Revision Id Author"

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/MultiParentClassLoader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/NamedClassLoader.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/NamedClassLoader.java?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/NamedClassLoader.java (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/NamedClassLoader.java Sat Aug 19 11:56:27 2006
@@ -0,0 +1,107 @@
+/**
+ *
+ * Copyright 2005-2006 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.xbean.server.classloader;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.net.URLStreamHandlerFactory;
+import java.util.Arrays;
+
+
+/**
+ * The NamedClassLoader is a simple extension to URLClassLoader that adds a name and a destroy method that cleans up
+ * the commons logging and JavaVM caches of the classloader.
+ *
+ * @author Dain Sundstrom
+ * @version $Id$
+ * @since 2.0
+ */
+public class NamedClassLoader extends URLClassLoader implements DestroyableClassLoader {
+    private final String name;
+    private boolean destroyed = false;
+
+    /**
+     * Creates a named class loader with no parents.
+     * @param name the name of this class loader
+     * @param urls the urls from which this class loader will classes and resources
+     */
+    public NamedClassLoader(String name, URL[] urls) {
+        super(urls);
+        this.name = name;
+    }
+
+    /**
+     * Creates a named class loader as a child of the specified parent.
+     * @param name the name of this class loader
+     * @param urls the urls from which this class loader will classes and resources
+     * @param parent the parent of this class loader
+     */
+    public NamedClassLoader(String name, URL[] urls, ClassLoader parent) {
+        super(urls, parent);
+        this.name = name;
+    }
+
+    /**
+     * Creates a named class loader as a child of the specified parent and using the specified URLStreamHandlerFactory
+     * for accessing the urls..
+     * @param name the name of this class loader
+     * @param urls the urls from which this class loader will classes and resources
+     * @param parent the parent of this class loader
+     * @param factory the URLStreamHandlerFactory used to access the urls
+     */
+    public NamedClassLoader(String name, URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
+        super(urls, parent, factory);
+        this.name = name;
+    }
+
+    /**
+     * Check if this classloader has been destroyed 
+     * @return
+     */
+    public synchronized boolean isDestroyed() {
+        return destroyed;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void destroy() {
+        synchronized(this) {
+            if (destroyed) return;
+            destroyed = true;
+        }
+        ClassLoaderUtil.destroy(this);
+    }
+
+    /**
+     * Gets the name of this class loader.
+     * @return the name of this class loader
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String toString() {
+        return "[" + getClass().getName() + ":" +
+                " name=" + getName() +
+                " urls=" + Arrays.asList(getURLs()) +
+                "]";
+    }
+}

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/NamedClassLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/NamedClassLoader.java
------------------------------------------------------------------------------
    svn:keywords = "Date Revision Id Author"

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/NamedClassLoader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceEnumeration.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceEnumeration.java?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceEnumeration.java (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceEnumeration.java Sat Aug 19 11:56:27 2006
@@ -0,0 +1,83 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  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.xbean.server.classloader;
+
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Collection;
+import java.util.NoSuchElementException;
+
+/**
+ * @version $Rev$ $Date: 2006-06-01 06:35:48 +0200 (Thu, 01 Jun 2006) $
+ */
+public class ResourceEnumeration implements Enumeration {
+    private Iterator iterator;
+    private final String resourceName;
+    private Object next;
+
+    public ResourceEnumeration(Collection resourceLocations, String resourceName) {
+        this.iterator = resourceLocations.iterator();
+        this.resourceName = resourceName;
+    }
+
+    public boolean hasMoreElements() {
+        fetchNext();
+        return (next != null);
+    }
+
+    public Object nextElement() {
+        fetchNext();
+
+        // save next into a local variable and clear the next field
+        Object next = this.next;
+        this.next = null;
+
+        // if we didn't have a next throw an exception
+        if (next == null) {
+            throw new NoSuchElementException();
+        }
+        return next;
+    }
+
+    private void fetchNext() {
+        if (iterator == null) {
+            return;
+        }
+        if (next != null) {
+            return;
+        }
+
+        try {
+            while (iterator.hasNext()) {
+                ResourceLocation resourceLocation = (ResourceLocation) iterator.next();
+                ResourceHandle resourceHandle = resourceLocation.getResourceHandle(resourceName);
+                if (resourceHandle != null) {
+                    next = resourceHandle.getUrl();
+                    return;
+                }
+            }
+            // no more elements
+            // clear the iterator so it can be GCed
+            iterator = null;
+        } catch (IllegalStateException e) {
+            // Jar file was closed... this means the resource finder was destroyed
+            // clear the iterator so it can be GCed
+            iterator = null;
+            throw e;
+        }
+    }
+}

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceEnumeration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceEnumeration.java
------------------------------------------------------------------------------
    svn:keywords = "Date Revision Id Author"

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceEnumeration.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceFinder.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceFinder.java?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceFinder.java (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceFinder.java Sat Aug 19 11:56:27 2006
@@ -0,0 +1,55 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  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.xbean.server.classloader;
+
+import java.net.URL;
+import java.util.Enumeration;
+
+/**
+ * Abstraction of resource searching policy. Given resource name, the resource
+ * finder performs implementation-specific lookup, and, if it is able to locate
+ * the resource, returns the {@link AbstractResourceHandle handle(s)} or URL(s) of it.
+ *
+ * @version $Rev$ $Date: 2006-06-01 06:35:48 +0200 (Thu, 01 Jun 2006) $
+ */
+public interface ResourceFinder {
+    /**
+     * Find the resource by name and return URL of it if found.
+     *
+     * @param name the resource name
+     * @return resource URL or null if resource was not found
+     */
+    public URL findResource(String name);
+
+    /**
+     * Find all resources with given name and return enumeration of their URLs.
+     *
+     * @param name the resource name
+     * @return enumeration of resource URLs (possibly empty).
+     */
+    public Enumeration findResources(String name);
+
+    /**
+     * Get the resource by name and, if found, open connection to it and return
+     * the {@link AbstractResourceHandle handle} of it.
+     *
+     * @param name the resource name
+     * @return resource handle or null if resource was not found
+     */
+    public ResourceHandle getResource(String name);
+
+}

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceFinder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceFinder.java
------------------------------------------------------------------------------
    svn:keywords = "Date Revision Id Author"

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceFinder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceHandle.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceHandle.java?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceHandle.java (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceHandle.java Sat Aug 19 11:56:27 2006
@@ -0,0 +1,97 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  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.xbean.server.classloader;
+
+import java.net.URL;
+import java.io.InputStream;
+import java.io.IOException;
+import java.util.jar.Manifest;
+import java.util.jar.Attributes;
+import java.security.cert.Certificate;
+
+/**
+ * This is a handle (a connection) to some resource, which may
+ * be a class, native library, text file, image, etc. Handles are returned
+ * by a ResourceFinder. A resource handle allows easy access to the resource data
+ * (using methods {@link #getInputStream} or {@link #getBytes}) as well as
+ * access resource metadata, such as attributes, certificates, etc.
+ * <p/>
+ * As soon as the handle is no longer in use, it should be explicitly
+ * {@link #close}d, similarly to I/O streams.
+ *
+ * @version $Rev$ $Date: 2006-06-01 06:35:48 +0200 (Thu, 01 Jun 2006) $
+ */
+public interface ResourceHandle {
+    /**
+     * Return the name of the resource. The name is a "/"-separated path
+     * name that identifies the resource.
+     */
+    String getName();
+
+    /**
+     * Returns the URL of the resource.
+     */
+    URL getUrl();
+
+    /**
+     * Does this resource refer to a directory.  Directory resources are commly used
+     * as the basis for a URL in client application.  A directory resource has 0 bytes for it's content. 
+     */
+    boolean isDirectory();
+
+    /**
+     * Returns the CodeSource URL for the class or resource.
+     */
+    URL getCodeSourceUrl();
+
+    /**
+     * Returns and InputStream for reading this resource data.
+     */
+    InputStream getInputStream() throws IOException;
+
+    /**
+     * Returns the length of this resource data, or -1 if unknown.
+     */
+    int getContentLength();
+
+    /**
+     * Returns this resource data as an array of bytes.
+     */
+    byte[] getBytes() throws IOException;
+
+    /**
+     * Returns the Manifest of the JAR file from which this resource
+     * was loaded, or null if none.
+     */
+    Manifest getManifest() throws IOException;
+
+    /**
+     * Return the Certificates of the resource, or null if none.
+     */
+    Certificate[] getCertificates();
+
+    /**
+     * Return the Attributes of the resource, or null if none.
+     */
+    Attributes getAttributes() throws IOException;
+
+    /**
+     * Closes a connection to the resource indentified by this handle. Releases
+     * any I/O objects associated with the handle.
+     */
+    void close();
+}

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceHandle.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceHandle.java
------------------------------------------------------------------------------
    svn:keywords = "Date Revision Id Author"

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceHandle.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceLocation.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceLocation.java?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceLocation.java (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceLocation.java Sat Aug 19 11:56:27 2006
@@ -0,0 +1,32 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  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.xbean.server.classloader;
+
+import java.util.jar.Manifest;
+import java.io.IOException;
+import java.net.URL;
+
+/**
+ * This is a location which is searched by
+ * @version $Rev$ $Date: 2006-06-01 06:35:48 +0200 (Thu, 01 Jun 2006) $
+ */
+public interface ResourceLocation {
+    URL getCodeSource();    
+    ResourceHandle getResourceHandle(String resourceName);
+    Manifest getManifest() throws IOException;
+    void close();
+}

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceLocation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceLocation.java
------------------------------------------------------------------------------
    svn:keywords = "Date Revision Id Author"

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ResourceLocation.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ThreadContextClassLoaderFactoryBean.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ThreadContextClassLoaderFactoryBean.java?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ThreadContextClassLoaderFactoryBean.java (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ThreadContextClassLoaderFactoryBean.java Sat Aug 19 11:56:27 2006
@@ -0,0 +1,41 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation
+ *
+ * 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.xbean.server.classloader;
+
+import org.springframework.beans.factory.FactoryBean;
+
+/**
+ * A factory bean to expose the current thread context class loader.
+ * 
+ * * @org.apache.xbean.XBean namespace="http://xbean.apache.org/schemas/server"
+ *                         element="threadContextClassLoader" description="References the ClassLoader of the current thread context"
+ * @version $Revision$
+ */
+public class ThreadContextClassLoaderFactoryBean implements FactoryBean {
+
+    public Object getObject() throws Exception {
+        return Thread.currentThread().getContextClassLoader();
+    }
+
+    public Class getObjectType() {
+        return ClassLoader.class;
+    }
+
+    public boolean isSingleton() {
+        return true;
+    }
+}

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ThreadContextClassLoaderFactoryBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ThreadContextClassLoaderFactoryBean.java
------------------------------------------------------------------------------
    svn:keywords = "Date Revision Id Author"

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/ThreadContextClassLoaderFactoryBean.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UnionEnumeration.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UnionEnumeration.java?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UnionEnumeration.java (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UnionEnumeration.java Sat Aug 19 11:56:27 2006
@@ -0,0 +1,63 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  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.xbean.server.classloader;
+
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+/**
+ * @version $Rev$ $Date: 2006-06-01 06:35:48 +0200 (Thu, 01 Jun 2006) $
+ */
+public final class UnionEnumeration implements Enumeration {
+    private final LinkedList enumerations = new LinkedList();
+
+    public UnionEnumeration(List enumerations) {
+        this.enumerations.addAll(enumerations);
+    }
+
+    public UnionEnumeration(Enumeration first, Enumeration second) {
+        if (first == null) throw new NullPointerException("first is null");
+        if (second == null) throw new NullPointerException("second is null");
+
+        enumerations.add(first);
+        enumerations.add(second);
+    }
+
+    public boolean hasMoreElements() {
+        while (!enumerations.isEmpty()) {
+            Enumeration enumeration = (Enumeration) enumerations.getFirst();
+            if (enumeration.hasMoreElements()) {
+                return true;
+            }
+            enumerations.removeFirst();
+        }
+        return false;
+    }
+
+    public Object nextElement() {
+        while (!enumerations.isEmpty()) {
+            Enumeration enumeration = (Enumeration) enumerations.getFirst();
+            if (enumeration.hasMoreElements()) {
+                return enumeration.nextElement();
+            }
+            enumerations.removeFirst();
+        }
+        throw new NoSuchElementException();
+    }
+}

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UnionEnumeration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UnionEnumeration.java
------------------------------------------------------------------------------
    svn:keywords = "Date Revision Id Author"

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UnionEnumeration.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UrlResourceFinder.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UrlResourceFinder.java?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UrlResourceFinder.java (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UrlResourceFinder.java Sat Aug 19 11:56:27 2006
@@ -0,0 +1,296 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  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.xbean.server.classloader;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.FileNotFoundException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.jar.JarFile;
+
+/**
+ * @version $Rev$ $Date: 2006-06-01 06:35:48 +0200 (Thu, 01 Jun 2006) $
+ */
+public class UrlResourceFinder implements ResourceFinder {
+    private final Object lock = new Object();
+
+    private final LinkedHashSet urls = new LinkedHashSet();
+    private final LinkedHashMap classPath = new LinkedHashMap();
+    private final LinkedHashSet watchedFiles = new LinkedHashSet();
+
+    private boolean destroyed = false;
+
+    public UrlResourceFinder() {
+    }
+
+    public UrlResourceFinder(URL[] urls) {
+        addUrls(urls);
+    }
+
+    public void destroy() {
+        synchronized (lock) {
+            if (destroyed) {
+                return;
+            }
+            destroyed = true;
+            urls.clear();
+            for (Iterator iterator = classPath.values().iterator(); iterator.hasNext();) {
+                ResourceLocation resourceLocation = (ResourceLocation) iterator.next();
+                resourceLocation.close();
+            }
+            classPath.clear();
+        }
+    }
+
+    public ResourceHandle getResource(String resourceName) {
+        synchronized (lock) {
+            if (destroyed) {
+                return null;
+            }
+            for (Iterator iterator = getClassPath().entrySet().iterator(); iterator.hasNext();) {
+                Map.Entry entry = (Map.Entry) iterator.next();
+                ResourceLocation resourceLocation = (ResourceLocation) entry.getValue();
+                ResourceHandle resourceHandle = resourceLocation.getResourceHandle(resourceName);
+                if (resourceHandle != null && !resourceHandle.isDirectory()) {
+                    return resourceHandle;
+                }
+            }
+        }
+        return null;
+    }
+
+    public URL findResource(String resourceName) {
+        synchronized (lock) {
+            if (destroyed) {
+                return null;
+            }
+            for (Iterator iterator = getClassPath().entrySet().iterator(); iterator.hasNext();) {
+                Map.Entry entry = (Map.Entry) iterator.next();
+                ResourceLocation resourceLocation = (ResourceLocation) entry.getValue();
+                ResourceHandle resourceHandle = resourceLocation.getResourceHandle(resourceName);
+                if (resourceHandle != null) {
+                    return resourceHandle.getUrl();
+                }
+            }
+        }
+        return null;
+    }
+
+    public Enumeration findResources(String resourceName) {
+        synchronized (lock) {
+            return new ResourceEnumeration(new ArrayList(getClassPath().values()), resourceName);
+        }
+    }
+
+    public void addUrl(URL url) {
+        addUrls(Collections.singletonList(url));
+    }
+
+    public URL[] getUrls() {
+        synchronized (lock) {
+            return (URL[]) urls.toArray(new URL[urls.size()]);
+        }
+    }
+
+    /**
+     * Adds an array of urls to the end of this class loader.
+     * @param urls the URLs to add
+     */
+    protected void addUrls(URL[] urls) {
+        addUrls(Arrays.asList(urls));
+    }
+
+    /**
+     * Adds a list of urls to the end of this class loader.
+     * @param urls the URLs to add
+     */
+    protected void addUrls(List urls) {
+        synchronized (lock) {
+            if (destroyed) {
+                throw new IllegalStateException("UrlResourceFinder has been destroyed");
+            }
+
+            boolean shouldRebuild = this.urls.addAll(urls);
+            if (shouldRebuild) {
+                rebuildClassPath();
+            }
+        }
+    }
+
+    private LinkedHashMap getClassPath() {
+        assert Thread.holdsLock(lock): "This method can only be called while holding the lock";
+
+        for (Iterator iterator = watchedFiles.iterator(); iterator.hasNext();) {
+            File file = (File) iterator.next();
+            if (file.canRead()) {
+                rebuildClassPath();
+                break;
+            }
+        }
+
+        return classPath;
+    }
+
+    /**
+     * Rebuilds the entire class path.  This class is called when new URLs are added or one of the watched files
+     * becomes readable.  This method will not open jar files again, but will add any new entries not alredy open
+     * to the class path.  If any file based url is does not exist, we will watch for that file to appear.
+     */
+    private void rebuildClassPath() {
+        assert Thread.holdsLock(lock): "This method can only be called while holding the lock";
+
+        // copy all of the existing locations into a temp map and clear the class path
+        Map existingJarFiles = new LinkedHashMap(classPath);
+        classPath.clear();
+
+        LinkedList locationStack = new LinkedList(urls);
+        try {
+            while (!locationStack.isEmpty()) {
+                URL url = (URL) locationStack.removeFirst();
+
+                // Skip any duplicate urls in the claspath
+                if (classPath.containsKey(url)) {
+                    continue;
+                }
+
+                // Check is this URL has already been opened
+                ResourceLocation resourceLocation = (ResourceLocation) existingJarFiles.remove(url);
+
+                // If not opened, cache the url and wrap it with a resource location
+                if (resourceLocation == null) {
+                    try {
+                        File file = cacheUrl(url);
+                        resourceLocation = createResourceLocation(url, file);
+                    } catch (FileNotFoundException e) {
+                        // if this is a file URL, the file doesn't exist yet... watch to see if it appears later
+                        if ("file".equals(url.getProtocol())) {
+                            File file = new File(url.getPath());
+                            watchedFiles.add(file);
+                            continue;
+
+                        }
+                    } catch (IOException ignored) {
+                        // can't seem to open the file... this is most likely a bad jar file
+                        // so don't keep a watch out for it because that would require lots of checking
+                        // Dain: We may want to review this decision later
+                        continue;
+                    }
+                }
+
+                // add the jar to our class path
+                classPath.put(resourceLocation.getCodeSource(), resourceLocation);
+
+                // push the manifest classpath on the stack (make sure to maintain the order)
+                List manifestClassPath = getManifestClassPath(resourceLocation);
+                locationStack.addAll(0, manifestClassPath);
+            }
+        } catch (Error e) {
+            destroy();
+            throw e;
+        }
+
+        for (Iterator iterator = existingJarFiles.values().iterator(); iterator.hasNext();) {
+            ResourceLocation resourceLocation = (ResourceLocation) iterator.next();
+            resourceLocation.close();
+        }
+    }
+
+    protected File cacheUrl(URL url) throws IOException {
+        if (!"file".equals(url.getProtocol())) {
+            // download the jar
+            throw new Error("Only local file jars are supported " + url);
+        }
+
+        File file = new File(url.getPath());
+        if (!file.exists()) {
+            throw new FileNotFoundException(file.getAbsolutePath());
+        }
+        if (!file.canRead()) {
+            throw new IOException("File is not readable: " + file.getAbsolutePath());
+        }
+        return file;
+    }
+
+    protected ResourceLocation createResourceLocation(URL codeSource, File cacheFile) throws IOException {
+        if (!cacheFile.exists()) {
+            throw new FileNotFoundException(cacheFile.getAbsolutePath());
+        }
+        if (!cacheFile.canRead()) {
+            throw new IOException("File is not readable: " + cacheFile.getAbsolutePath());
+        }
+
+        ResourceLocation resourceLocation = null;
+        if (cacheFile.isDirectory()) {
+            // DirectoryResourceLocation will only return "file" URLs within this directory
+            // do not user the DirectoryResourceLocation for non file based urls
+            resourceLocation = new DirectoryResourceLocation(cacheFile);
+        } else {
+            resourceLocation = new JarResourceLocation(codeSource, new JarFile(cacheFile));
+        }
+        return resourceLocation;
+    }
+
+    private List getManifestClassPath(ResourceLocation resourceLocation) {
+        try {
+            // get the manifest, if possible
+            Manifest manifest = resourceLocation.getManifest();
+            if (manifest == null) {
+                // some locations don't have a manifest
+                return Collections.EMPTY_LIST;
+            }
+
+            // get the class-path attribute, if possible
+            String manifestClassPath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
+            if (manifestClassPath == null) {
+                return Collections.EMPTY_LIST;
+            }
+
+            // build the urls...
+            // the class-path attribute is space delimited
+            URL codeSource = resourceLocation.getCodeSource();
+            LinkedList classPathUrls = new LinkedList();
+            for (StringTokenizer tokenizer = new StringTokenizer(manifestClassPath, " "); tokenizer.hasMoreTokens();) {
+                String entry = tokenizer.nextToken();
+                try {
+                    // the class path entry is relative to the resource location code source
+                    URL entryUrl = new URL(codeSource, entry);
+                    classPathUrls.addLast(entryUrl);
+                } catch (MalformedURLException ignored) {
+                    // most likely a poorly named entry
+                }
+            }
+            return classPathUrls;
+        } catch (IOException ignored) {
+            // error opening the manifest
+            return Collections.EMPTY_LIST;
+        }
+    }
+}

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UrlResourceFinder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UrlResourceFinder.java
------------------------------------------------------------------------------
    svn:keywords = "Date Revision Id Author"

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/java/org/apache/xbean/server/classloader/UrlResourceFinder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/resources/META-INF/LICENSE
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/main/resources/META-INF/LICENSE?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/main/resources/META-INF/LICENSE (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/main/resources/META-INF/LICENSE Sat Aug 19 11:56:27 2006
@@ -0,0 +1,203 @@
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   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.
+

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/main/resources/META-INF/NOTICE
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/main/resources/META-INF/NOTICE?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/main/resources/META-INF/NOTICE (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/main/resources/META-INF/NOTICE Sat Aug 19 11:56:27 2006
@@ -0,0 +1,3 @@
+This product includes software developed by
+The Apache Software Foundation (http://www.apache.org/).
+

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/test-data/resourceFinderTest/jar1/resource
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/test-data/resourceFinderTest/jar1/resource?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/test-data/resourceFinderTest/jar1/resource (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/test-data/resourceFinderTest/jar1/resource Sat Aug 19 11:56:27 2006
@@ -0,0 +1 @@
+resource1
\ No newline at end of file

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/test-data/resourceFinderTest/jar2/resource
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/test-data/resourceFinderTest/jar2/resource?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/test-data/resourceFinderTest/jar2/resource (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/test-data/resourceFinderTest/jar2/resource Sat Aug 19 11:56:27 2006
@@ -0,0 +1 @@
+resource2
\ No newline at end of file

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/JarFileClassLoaderTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/JarFileClassLoaderTest.java?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/JarFileClassLoaderTest.java (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/JarFileClassLoaderTest.java Sat Aug 19 11:56:27 2006
@@ -0,0 +1,32 @@
+/**
+ *
+ * Copyright 2005-2006 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.xbean.server.classloader;
+
+import java.net.URL;
+
+/**
+ * Test the JarFileClassLoader.
+ *
+ * @author Dain Sundstrom
+ * @version $Id$
+ * @since 2.0
+ */
+public class JarFileClassLoaderTest extends MultiParentClassLoaderTest {
+    protected MultiParentClassLoader createClassLoader(String name, URL[] urls, ClassLoader[] parents) {
+        return new JarFileClassLoader(name, urls, parents);
+    }
+}

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/JarFileClassLoaderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/JarFileClassLoaderTest.java
------------------------------------------------------------------------------
    svn:keywords = "Date Revision Id Author"

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/JarFileClassLoaderTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/MultiParentClassLoaderTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/MultiParentClassLoaderTest.java?rev=432861&view=auto
==============================================================================
--- geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/MultiParentClassLoaderTest.java (added)
+++ geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/MultiParentClassLoaderTest.java Sat Aug 19 11:56:27 2006
@@ -0,0 +1,360 @@
+/**
+ *
+ * Copyright 2005-2006 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.xbean.server.classloader;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Enumeration;
+import java.util.SortedSet;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
+
+import junit.framework.TestCase;
+import net.sf.cglib.core.DefaultGeneratorStrategy;
+import net.sf.cglib.core.NamingPolicy;
+import net.sf.cglib.core.Predicate;
+import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.NoOp;
+
+/**
+ * Tests the MultiParentClassLoader including classloading and resource loading.
+ * @author Dain Sundstrom
+ * @version $Id$
+ * @since 2.0
+ */
+public class MultiParentClassLoaderTest extends TestCase {
+    private static final String CLASS_NAME = "TestClass";
+    private static final String ENTRY_NAME = "foo";
+    private static final String ENTRY_VALUE = "bar";
+    private File[] files;
+    private static final String NON_EXISTANT_RESOURCE = "non-existant-resource";
+    private static final String NON_EXISTANT_CLASS = "NonExistant.class";
+    private URLClassLoader[] parents;
+    private File myFile;
+    private MultiParentClassLoader classLoader;
+    private static final String NAME = "my test class loader";
+
+    /**
+     * Verify that the test jars are valid.
+     * @throws Exception if a problem occurs
+     */
+    public void testTestJars() throws Exception {
+        for (int i = 0; i < files.length; i++) {
+            File file = files[i];
+            JarFile jarFile = new JarFile(files[i]);
+            String urlString = "jar:" + file.toURL() + "!/" + ENTRY_NAME;
+            URL url = new URL(files[i].toURL(), urlString);
+            assertStreamContains(ENTRY_VALUE + i, url.openStream());
+            jarFile.close();
+
+            URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { file.toURL() } );
+            // clazz shared by all
+            Class clazz = urlClassLoader.loadClass(CLASS_NAME);
+            assertNotNull(clazz);
+            assertTrue(SortedSet.class.isAssignableFrom(clazz));
+
+            // clazz specific to this jar
+            clazz = urlClassLoader.loadClass(CLASS_NAME + i);
+            assertNotNull(clazz);
+            assertTrue(SortedSet.class.isAssignableFrom(clazz));
+
+            // resource shared by all jars
+            InputStream in = urlClassLoader.getResourceAsStream(ENTRY_NAME );
+            assertStreamContains("Should have found value from parent " + i, ENTRY_VALUE + i, in);
+
+            // resource specific to this jar
+            in = urlClassLoader.getResourceAsStream(ENTRY_NAME + i);
+            assertStreamContains("Should have found value from parent " + i, ENTRY_VALUE + i + ENTRY_VALUE, in);
+        }
+    }
+
+    /**
+     * Verify the get name method returns the name provided to the constructor.
+     */
+    public void testGetName() {
+        assertEquals(NAME, classLoader.getName());
+    }
+
+    /**
+     * Verufy that the getParents method returns a different array from the one passed to the constructor and that the
+     * parents are in the same order.
+     */
+    public void testGetParents() {
+        ClassLoader[] actualParents = classLoader.getParents();
+        assertNotSame(parents, actualParents);
+        assertEquals(parents.length, actualParents.length);
+        for (int i = 0; i < actualParents.length; i++) {
+            assertEquals(parents[i], actualParents[i]);
+        }
+    }
+
+    /**
+     * Test loadClass loads in preference of the parents, in order, and then the local urls.
+     * @throws Exception if a problem occurs
+     */
+    public void testLoadClass() throws Exception {
+        // load class specific to my class loader
+        Class clazz = classLoader.loadClass(CLASS_NAME + 33);
+        assertNotNull(clazz);
+        assertTrue(SortedSet.class.isAssignableFrom(clazz));
+        assertEquals(classLoader, clazz.getClassLoader());
+
+        // load class specific to each parent class loader
+        for (int i = 0; i < parents.length; i++) {
+            URLClassLoader parent = parents[i];
+            clazz = classLoader.loadClass(CLASS_NAME + i);
+            assertNotNull(clazz);
+            assertTrue(SortedSet.class.isAssignableFrom(clazz));
+            assertEquals(parent, clazz.getClassLoader());
+        }
+
+        // class shared by all class loaders
+        clazz = classLoader.loadClass(CLASS_NAME);
+        assertNotNull(clazz);
+        assertTrue(SortedSet.class.isAssignableFrom(clazz));
+        assertEquals(parents[0], clazz.getClassLoader());
+    }
+
+    /**
+     * Test that an attempt to load a non-existant class causes a ClassNotFoundException.
+     */
+    public void testLoadNonExistantClass() {
+        try {
+            classLoader.loadClass(NON_EXISTANT_CLASS);
+            fail("loadClass should have thrown a ClassNotFoundException");
+        } catch (ClassNotFoundException e) {
+            // expected
+        }
+    }
+
+    /**
+     * Test getResourceAsStream loads in preference of the parents, in order, and then the local urls.
+     * @throws Exception if a problem occurs
+     */
+    public void testGetResourceAsStream() throws Exception {
+        InputStream in = classLoader.getResourceAsStream(ENTRY_NAME + 33);
+        assertStreamContains("Should have found value from my file", ENTRY_VALUE + 33 + ENTRY_VALUE, in);
+
+        for (int i = 0; i < parents.length; i++) {
+            in = classLoader.getResourceAsStream(ENTRY_NAME + i);
+            assertStreamContains("Should have found value from parent " + i, ENTRY_VALUE + i + ENTRY_VALUE, in);
+        }
+
+        in = classLoader.getResourceAsStream(ENTRY_NAME);
+        assertStreamContains("Should have found value from first parent", ENTRY_VALUE + 0, in);
+    }
+
+    /**
+     * Test getResourceAsStream returns null when attempt is made to loade a non-existant resource.
+     * @throws Exception if a problem occurs
+     */
+    public void testGetNonExistantResourceAsStream() throws Exception {
+        InputStream in = classLoader.getResourceAsStream(NON_EXISTANT_RESOURCE);
+        assertNull(in);
+    }
+
+    /**
+     * Test getResource loads in preference of the parents, in order, and then the local urls.
+     * @throws Exception if a problem occurs
+     */
+    public void testGetResource() throws Exception {
+        URL resource = classLoader.getResource(ENTRY_NAME + 33);
+        assertURLContains("Should have found value from my file", ENTRY_VALUE + 33 + ENTRY_VALUE, resource);
+
+        for (int i = 0; i < parents.length; i++) {
+            resource = classLoader.getResource(ENTRY_NAME + i);
+            assertURLContains("Should have found value from parent " + i, ENTRY_VALUE + i + ENTRY_VALUE, resource);
+        }
+
+        resource = classLoader.getResource(ENTRY_NAME);
+        assertURLContains("Should have found value from first parent", ENTRY_VALUE + 0, resource);
+    }
+
+    /**
+     * Test getResource returns null when attempt is made to loade a non-existant resource.
+     * @throws Exception if a problem occurs
+     */
+    public void testGetNonExistantResource() throws Exception {
+        URL resource = classLoader.getResource(NON_EXISTANT_RESOURCE);
+        assertNull(resource);
+    }
+
+    /**
+     * Test getResource returns an enumeration in preference of the parents, in order, and then the local urls.
+     * @throws Exception if a problem occurs
+     */
+    public void testGetResources() throws Exception {
+        Enumeration resources = classLoader.getResources(ENTRY_NAME);
+        assertNotNull(resources);
+        assertTrue(resources.hasMoreElements());
+
+        // there should be one entry for each parent
+        for (int i = 0; i < parents.length; i++) {
+            URL resource = (URL) resources.nextElement();
+            assertURLContains("Should have found value from parent " + i, ENTRY_VALUE + i, resource);
+        }
+
+        // and one entry from my url
+        assertTrue(resources.hasMoreElements());
+        URL resource = (URL) resources.nextElement();
+        assertURLContains("Should have found value from my file", ENTRY_VALUE + 33, resource);
+    }
+
+    /**
+     * Test getResources returns an empty enumeration when attempt is made to loade a non-existant resource.
+     * @throws Exception if a problem occurs
+     */
+    public void testGetNonExistantResources() throws Exception {
+        Enumeration resources = classLoader.getResources(NON_EXISTANT_RESOURCE);
+        assertNotNull(resources);
+        assertFalse(resources.hasMoreElements());
+    }
+
+    private void assertStreamContains(String expectedValue, InputStream in) throws IOException {
+        assertStreamContains(null, expectedValue, in);
+    }
+
+    private void assertStreamContains(String message, String expectedValue, InputStream in) throws IOException {
+        String entryValue;
+        try {
+            StringBuffer stringBuffer = new StringBuffer();
+            byte[] bytes = new byte[4000];
+            for (int count = in.read(bytes); count != -1; count = in.read(bytes)) {
+                stringBuffer.append(new String(bytes, 0, count));
+            }
+            entryValue = stringBuffer.toString();
+        } finally {
+            in.close();
+        }
+        assertEquals(message, expectedValue, entryValue);
+    }
+
+    private void assertURLContains(String message, String expectedValue, URL resource) throws IOException {
+        InputStream in;
+        assertNotNull(resource);
+        in = resource.openStream();
+        assertStreamContains(message, expectedValue, in);
+    }
+
+    private static void assertFileExists(File file) {
+        assertTrue("File should exist: " + file, file.canRead());
+    }
+
+    private static void assertFileNotExists(File file) {
+        assertTrue("File should not exist: " + file, !file.canRead());
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        files = new File[3];
+        for (int i = 0; i < files.length; i++) {
+            files[i] = createJarFile(i);
+        }
+
+        parents = new URLClassLoader[3];
+        for (int i = 0; i < parents.length; i++) {
+            parents[i] = new URLClassLoader(new URL[]{files[i].toURL()});
+        }
+
+        myFile = createJarFile(33);
+        classLoader = createClassLoader(NAME, new URL[]{myFile.toURL()}, parents);
+    }
+
+    /**
+     * Creates the class loader to test.
+     * @param name the name of the classloader
+     * @param urls the urls to load classes and resources from
+     * @param parents the parents of the class loader
+     * @return the class loader to test
+     */
+    protected MultiParentClassLoader createClassLoader(String name, URL[] urls, ClassLoader[] parents) {
+        return new MultiParentClassLoader(name, urls, parents);
+    }
+
+    private static File createJarFile(int i) throws IOException {
+        File file = File.createTempFile("test-" + i + "-", ".jar");
+
+        FileOutputStream out = new FileOutputStream(file);
+        JarOutputStream jarOut = new JarOutputStream(out);
+
+        // common class shared by everyone
+        jarOut.putNextEntry(new JarEntry(CLASS_NAME + ".class"));
+        jarOut.write(createClass(CLASS_NAME));
+
+        // class only available in this jar
+        jarOut.putNextEntry(new JarEntry(CLASS_NAME + i + ".class"));
+        jarOut.write(createClass(CLASS_NAME + i));
+
+        // common resource shared by everyone
+        jarOut.putNextEntry(new JarEntry(ENTRY_NAME));
+        jarOut.write((ENTRY_VALUE + i).getBytes());
+
+        // resource only available in this jar
+        jarOut.putNextEntry(new JarEntry(ENTRY_NAME + i));
+        jarOut.write((ENTRY_VALUE + i + ENTRY_VALUE).getBytes());
+
+        jarOut.close();
+        out.close();
+
+        assertFileExists(file);
+        return file;
+    }
+
+    private static byte[] createClass(final String name) {
+        Enhancer enhancer = new Enhancer();
+        enhancer.setNamingPolicy(new NamingPolicy() {
+            public String getClassName(String prefix, String source, Object key, Predicate names) {
+                return name;
+            }
+        });
+        enhancer.setClassLoader(new URLClassLoader(new URL[0]));
+        enhancer.setSuperclass(Object.class);
+        enhancer.setInterfaces(new Class[]{SortedSet.class});
+        enhancer.setCallbackTypes(new Class[]{NoOp.class});
+        enhancer.setUseFactory(false);
+        ByteCode byteCode = new ByteCode();
+        enhancer.setStrategy(byteCode);
+        enhancer.createClass();
+
+        return byteCode.getByteCode();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        for (int i = 0; i < files.length; i++) {
+            files[i].delete();
+        }
+    }
+
+    private static class ByteCode extends DefaultGeneratorStrategy {
+        private byte[] byteCode;
+
+        public byte[] transform(byte[] byteCode) {
+            this.byteCode = byteCode;
+            return byteCode;
+        }
+
+        public byte[] getByteCode() {
+            return byteCode;
+        }
+    }
+}

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/MultiParentClassLoaderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/MultiParentClassLoaderTest.java
------------------------------------------------------------------------------
    svn:keywords = "Date Revision Id Author"

Propchange: geronimo/xbean/branches/classloader/xbean-classloaders/src/test/java/org/apache/xbean/server/classloader/MultiParentClassLoaderTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message