You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2006/06/21 17:00:06 UTC

svn commit: r416002 [2/2] - in /incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi: container/ deployment/impl/ framework/ loaders/

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarFileClassLoader.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarFileClassLoader.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarFileClassLoader.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarFileClassLoader.java Wed Jun 21 08:00:03 2006
@@ -0,0 +1,344 @@
+/**
+ *
+ * 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.servicemix.jbi.loaders;
+
+import java.io.IOException;
+import java.io.File;
+import java.net.URL;
+import java.net.URI;
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.CodeSource;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedExceptionAction;
+import java.security.PrivilegedActionException;
+import java.security.cert.Certificate;
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+
+/**
+ * The JarFileClassLoader that loads classes and resources from a list of JarFiles.  This method is simmilar to URLClassLoader
+ * except it properly closes JarFiles when the classloader is destroyed so that the file read lock will be released, and
+ * the jar file can be modified and deleted.
+ * <p>
+ * Note: This implementation currently does not work reliably on windows, since the jar URL handler included with the Sun JavaVM
+ * holds a read lock on the JarFile, and this lock is not released when the jar url is dereferenced.  To fix this a
+ * replacement for the jar url handler must be written.
+ *
+ * @author Dain Sundstrom
+ * @version $Id: JarFileClassLoader.java 410741 2006-06-01 04:35:48Z jsisson $
+ * @since 2.0
+ */
+public class JarFileClassLoader extends MultiParentClassLoader {
+    private static final URL[] EMPTY_URLS = new URL[0];
+
+    private final UrlResourceFinder resourceFinder = new UrlResourceFinder();
+    private final AccessControlContext acc;
+
+    /**
+     * Creates a JarFileClassLoader that is a child of the system class loader.
+     * @param name the name of this class loader
+     * @param urls a list of URLs from which classes and resources should be loaded
+     */
+    public JarFileClassLoader(String name, URL[] urls) {
+        super(name, EMPTY_URLS);
+        this.acc = AccessController.getContext();
+        addURLs(urls);
+    }
+
+    /**
+     * Creates a JarFileClassLoader that is a child of the specified class loader.
+     * @param id the name of this class loader
+     * @param urls a list of URLs from which classes and resources should be loaded
+     * @param parent the parent of this class loader
+     */
+    public JarFileClassLoader(String name, URL[] urls, ClassLoader parent) {
+        super(name, EMPTY_URLS, parent);
+        this.acc = AccessController.getContext();
+        addURLs(urls);
+    }
+
+    public JarFileClassLoader(String name, URL[] urls, ClassLoader parent, boolean inverseClassLoading, String[] hiddenClasses, String[] nonOverridableClasses) {
+        super(name, EMPTY_URLS, parent, inverseClassLoading, hiddenClasses, nonOverridableClasses);
+        this.acc = AccessController.getContext();
+        addURLs(urls);
+    }
+
+    /**
+     * Creates a named class loader as a child of the specified parents.
+     * @param id 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 JarFileClassLoader(String name, URL[] urls, ClassLoader[] parents) {
+        super(name, EMPTY_URLS, parents);
+        this.acc = AccessController.getContext();
+        addURLs(urls);
+    }
+
+    public JarFileClassLoader(String name, URL[] urls, ClassLoader[] parents, boolean inverseClassLoading, Collection hiddenClasses, Collection nonOverridableClasses) {
+        super(name, EMPTY_URLS, parents, inverseClassLoading, hiddenClasses, nonOverridableClasses);
+        this.acc = AccessController.getContext();
+        addURLs(urls);
+    }
+
+    public JarFileClassLoader(String name, URL[] urls, ClassLoader[] parents, boolean inverseClassLoading, String[] hiddenClasses, String[] nonOverridableClasses) {
+        super(name, EMPTY_URLS, parents, inverseClassLoading, hiddenClasses, nonOverridableClasses);
+        this.acc = AccessController.getContext();
+        addURLs(urls);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public URL[] getURLs() {
+        return resourceFinder.getUrls();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void addURL(final URL url) {
+        AccessController.doPrivileged(new PrivilegedAction() {
+            public Object run() {
+                resourceFinder.addUrl(url);
+                return null;
+            }
+        }, acc);
+    }
+
+    /**
+     * Adds an array of urls to the end of this class loader.
+     * @param urls the URLs to add
+     */
+    protected void addURLs(final URL[] urls) {
+        AccessController.doPrivileged(new PrivilegedAction() {
+            public Object run() {
+                resourceFinder.addUrls(urls);
+                return null;
+            }
+        }, acc);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void destroy() {
+        resourceFinder.destroy();
+        super.destroy();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public URL findResource(final String resourceName) {
+        return (URL) AccessController.doPrivileged(new PrivilegedAction() {
+            public Object run() {
+                return resourceFinder.findResource(resourceName);
+            }
+        }, acc);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Enumeration findResources(final String resourceName) throws IOException {
+        // todo this is not right
+        // first get the resources from the parent classloaders
+        Enumeration parentResources = super.findResources(resourceName);
+
+        // get the classes from my urls
+        Enumeration myResources = (Enumeration) AccessController.doPrivileged(new PrivilegedAction() {
+            public Object run() {
+                return resourceFinder.findResources(resourceName);
+            }
+        }, acc);
+
+        // join the two together
+        Enumeration resources = new UnionEnumeration(parentResources, myResources);
+        return resources;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected String findLibrary(String libraryName) {
+        // if the libraryName is actually a directory it is invalid
+        int pathEnd = libraryName.lastIndexOf('/');
+        if (pathEnd == libraryName.length() - 1) {
+            throw new IllegalArgumentException("libraryName ends with a '/' character: " + libraryName);
+        }
+
+        // get the name if the library file
+        final String resourceName;
+        if (pathEnd < 0) {
+            resourceName = System.mapLibraryName(libraryName);
+        } else {
+            String path = libraryName.substring(0, pathEnd + 1);
+            String file = libraryName.substring(pathEnd + 1);
+            resourceName = path + System.mapLibraryName(file);
+        }
+
+        // get a resource handle to the library
+        ResourceHandle resourceHandle = (ResourceHandle) AccessController.doPrivileged(new PrivilegedAction() {
+            public Object run() {
+                return resourceFinder.getResource(resourceName);
+            }
+        }, acc);
+
+        if (resourceHandle == null) {
+            return null;
+        }
+
+        // the library must be accessable on the file system
+        URL url = resourceHandle.getUrl();
+        if (!"file".equals(url.getProtocol())) {
+            return null;
+        }
+
+        String path = new File(URI.create(url.toString())).getPath();
+        return path;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected Class findClass(final String className) throws ClassNotFoundException {
+        try {
+            return (Class) AccessController.doPrivileged(new PrivilegedExceptionAction() {
+                public Object run() throws ClassNotFoundException {
+                    // first think check if we are allowed to define the package
+                    SecurityManager securityManager = System.getSecurityManager();
+                    if (securityManager != null) {
+                        String packageName;
+                        int packageEnd = className.lastIndexOf('.');
+                        if (packageEnd >= 0) {
+                            packageName = className.substring(0, packageEnd);
+                            securityManager.checkPackageDefinition(packageName);
+                        }
+                    }
+
+
+                    // convert the class name to a file name
+                    String resourceName = className.replace('.', '/') + ".class";
+
+                    // find the class file resource
+                    ResourceHandle resourceHandle = resourceFinder.getResource(resourceName);
+                    if (resourceHandle == null) {
+                        throw new ClassNotFoundException(className);
+                    }
+
+                    byte[] bytes;
+                    Manifest manifest;
+                    try {
+                        // get the bytes from the class file
+                        bytes = resourceHandle.getBytes();
+
+                        // get the manifest for defining the packages
+                        manifest = resourceHandle.getManifest();
+                    } catch (IOException e) {
+                        throw new ClassNotFoundException(className, e);
+                    }
+
+                    // get the certificates for the code source
+                    Certificate[] certificates = resourceHandle.getCertificates();
+
+                    // the code source url is used to define the package and as the security context for the class
+                    URL codeSourceUrl = resourceHandle.getCodeSourceUrl();
+
+                    // define the package (required for security)
+                    definePackage(className, codeSourceUrl, manifest);
+
+                    // this is the security context of the class
+                    CodeSource codeSource = new CodeSource(codeSourceUrl, certificates);
+
+                    // load the class into the vm
+                    Class clazz = defineClass(className, bytes, 0, bytes.length, codeSource);
+                    return clazz;
+                }
+            }, acc);
+        } catch (PrivilegedActionException e) {
+            throw (ClassNotFoundException) e.getException();
+        }
+    }
+
+    private void definePackage(String className, URL jarUrl, Manifest manifest) {
+        int packageEnd = className.lastIndexOf('.');
+        if (packageEnd < 0) {
+            return;
+        }
+
+        String packageName = className.substring(0, packageEnd);
+        String packagePath = packageName.replace('.', '/') + "/";
+
+        Attributes packageAttributes = null;
+        Attributes mainAttributes = null;
+        if (manifest != null) {
+            packageAttributes = manifest.getAttributes(packagePath);
+            mainAttributes = manifest.getMainAttributes();
+        }
+        Package pkg = getPackage(packageName);
+        if (pkg != null) {
+            if (pkg.isSealed()) {
+                if (!pkg.isSealed(jarUrl)) {
+                    throw new SecurityException("Package was already sealed with another URL: package=" + packageName + ", url=" + jarUrl);
+                }
+            } else {
+                if (isSealed(packageAttributes, mainAttributes)) {
+                    throw new SecurityException("Package was already been loaded and not sealed: package=" + packageName + ", url=" + jarUrl);
+                }
+            }
+        } else {
+            String specTitle = getAttribute(Attributes.Name.SPECIFICATION_TITLE, packageAttributes, mainAttributes);
+            String specVendor = getAttribute(Attributes.Name.SPECIFICATION_VENDOR, packageAttributes, mainAttributes);
+            String specVersion = getAttribute(Attributes.Name.SPECIFICATION_VERSION, packageAttributes, mainAttributes);
+            String implTitle = getAttribute(Attributes.Name.IMPLEMENTATION_TITLE, packageAttributes, mainAttributes);
+            String implVendor = getAttribute(Attributes.Name.IMPLEMENTATION_VENDOR, packageAttributes, mainAttributes);
+            String implVersion = getAttribute(Attributes.Name.IMPLEMENTATION_VERSION, packageAttributes, mainAttributes);
+
+            URL sealBase = null;
+            if (isSealed(packageAttributes, mainAttributes)) {
+                sealBase = jarUrl;
+            }
+
+            definePackage(packageName, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor, sealBase);
+        }
+    }
+
+    private String getAttribute(Attributes.Name name, Attributes packageAttributes, Attributes mainAttributes) {
+        if (packageAttributes != null) {
+            String value = packageAttributes.getValue(name);
+            if (value != null) {
+                return value;
+            }
+        }
+        if (mainAttributes != null) {
+            return mainAttributes.getValue(name);
+        }
+        return null;
+    }
+
+    private boolean isSealed(Attributes packageAttributes, Attributes mainAttributes) {
+        String sealed = getAttribute(Attributes.Name.SEALED, packageAttributes, mainAttributes);
+        if (sealed == null) {
+            return false;
+        }
+        return "true".equalsIgnoreCase(sealed);
+    }
+}
\ No newline at end of file

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarFileUrlConnection.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarFileUrlConnection.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarFileUrlConnection.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarFileUrlConnection.java Wed Jun 21 08:00:03 2006
@@ -0,0 +1,132 @@
+/**
+ *
+ * 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.servicemix.jbi.loaders;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.JarURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.security.Permission;
+import java.security.cert.Certificate;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+/**
+ * @version $Rev: 410741 $ $Date: 2006-06-01 06:35:48 +0200 (Thu, 01 Jun 2006) $
+ */
+public class JarFileUrlConnection extends JarURLConnection {
+    public static final URL DUMMY_JAR_URL;
+    static {
+        try {
+            DUMMY_JAR_URL = new URL("jar", "", -1, "file:dummy!/", new URLStreamHandler() {
+                protected URLConnection openConnection(URL u) {
+                    throw new UnsupportedOperationException();
+                }
+            });
+        } catch (Exception e) {
+            throw new ExceptionInInitializerError(e);
+        }
+    }
+
+    private final URL url;
+    private final JarFile jarFile;
+    private final JarEntry jarEntry;
+    private final URL jarFileUrl;
+
+    public JarFileUrlConnection(URL url, JarFile jarFile, JarEntry jarEntry) throws MalformedURLException {
+        super(DUMMY_JAR_URL);
+
+        if (url == null) throw new NullPointerException("url is null");
+        if (jarFile == null) throw new NullPointerException("jarFile is null");
+        if (jarEntry == null) throw new NullPointerException("jarEntry is null");
+
+        this.url = url;
+        this.jarFile = jarFile;
+        this.jarEntry = jarEntry;
+        jarFileUrl = new File(jarFile.getName()).toURL();
+    }
+
+    public JarFile getJarFile() throws IOException {
+        return jarFile;
+    }
+
+    public synchronized void connect() {
+    }
+
+    public URL getJarFileURL() {
+        return jarFileUrl;
+    }
+
+    public String getEntryName() {
+        return getJarEntry().getName();
+    }
+
+    public Manifest getManifest() throws IOException {
+        return jarFile.getManifest();
+    }
+
+    public JarEntry getJarEntry() {
+        return jarEntry;
+    }
+
+    public Attributes getAttributes() throws IOException {
+        return getJarEntry().getAttributes();
+    }
+
+    public Attributes getMainAttributes() throws IOException {
+        return getManifest().getMainAttributes();
+    }
+
+    public Certificate[] getCertificates() throws IOException {
+        return getJarEntry().getCertificates();
+    }
+
+    public URL getURL() {
+        return url;
+    }
+
+    public int getContentLength() {
+        long size = getJarEntry().getSize();
+        if (size > Integer.MAX_VALUE) {
+            return -1;
+        }
+        return (int) size;
+    }
+
+    public long getLastModified() {
+        return getJarEntry().getTime();
+    }
+
+    public synchronized InputStream getInputStream() throws IOException {
+        return jarFile.getInputStream(jarEntry);
+    }
+
+    public Permission getPermission() throws IOException {
+        URL jarFileUrl = new File(jarFile.getName()).toURL();
+        return jarFileUrl.openConnection().getPermission();
+    }
+
+    public String toString() {
+        return JarFileUrlConnection.class.getName() + ":" + url;
+    }
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarFileUrlStreamHandler.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarFileUrlStreamHandler.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarFileUrlStreamHandler.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarFileUrlStreamHandler.java Wed Jun 21 08:00:03 2006
@@ -0,0 +1,67 @@
+/**
+ *
+ * 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.servicemix.jbi.loaders;
+
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.net.MalformedURLException;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.io.File;
+
+/**
+ * @version $Rev: 410741 $ $Date: 2006-06-01 06:35:48 +0200 (Thu, 01 Jun 2006) $
+ */
+public class JarFileUrlStreamHandler extends URLStreamHandler {
+    public static URL createUrl(JarFile jarFile, JarEntry jarEntry) throws MalformedURLException {
+        return createUrl(jarFile, jarEntry, new File(jarFile.getName()).toURL());
+    }
+
+    public static URL createUrl(JarFile jarFile, JarEntry jarEntry, URL codeSource) throws MalformedURLException {
+        JarFileUrlStreamHandler handler = new JarFileUrlStreamHandler(jarFile, jarEntry);
+        URL url = new URL("jar", "", -1, codeSource + "!/" + jarEntry.getName(), handler);
+        handler.setExpectedUrl(url);
+        return url;
+    }
+
+    private URL expectedUrl;
+    private final JarFile jarFile;
+    private final JarEntry jarEntry;
+
+    public JarFileUrlStreamHandler(JarFile jarFile, JarEntry jarEntry) {
+        if (jarFile == null) throw new NullPointerException("jarFile is null");
+        if (jarEntry == null) throw new NullPointerException("jarEntry is null");
+
+        this.jarFile = jarFile;
+        this.jarEntry = jarEntry;
+    }
+
+    public void setExpectedUrl(URL expectedUrl) {
+        if (expectedUrl == null) throw new NullPointerException("expectedUrl is null");
+        this.expectedUrl = expectedUrl;
+    }
+
+    public URLConnection openConnection(URL url) throws MalformedURLException {
+        if (expectedUrl == null) throw new IllegalStateException("expectedUrl was not set");
+
+        // alternatively we could return a connection using the normal jar url connection
+        if (!expectedUrl.equals(url)) throw new IllegalArgumentException("Expected url [" + expectedUrl + "], but was [" + url + "]");
+
+        return new JarFileUrlConnection(url, jarFile, jarEntry);
+    }
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarResourceHandle.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarResourceHandle.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarResourceHandle.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarResourceHandle.java Wed Jun 21 08:00:03 2006
@@ -0,0 +1,80 @@
+/**
+ *
+ * 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.servicemix.jbi.loaders;
+
+import java.util.jar.JarFile;
+import java.util.jar.JarEntry;
+import java.util.jar.Manifest;
+import java.util.jar.Attributes;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.io.InputStream;
+import java.io.IOException;
+import java.security.cert.Certificate;
+
+/**
+ * @version $Rev: 410741 $ $Date: 2006-06-01 06:35:48 +0200 (Thu, 01 Jun 2006) $
+ */
+public class JarResourceHandle extends AbstractResourceHandle {
+    private final JarFile jarFile;
+    private final JarEntry jarEntry;
+    private final URL url;
+    private final URL codeSource;
+
+    public JarResourceHandle(JarFile jarFile, JarEntry jarEntry, URL codeSource) throws MalformedURLException {
+        this.jarFile = jarFile;
+        this.jarEntry = jarEntry;
+        this.url = JarFileUrlStreamHandler.createUrl(jarFile, jarEntry, codeSource);
+        this.codeSource = codeSource;
+    }
+
+    public String getName() {
+        return jarEntry.getName();
+    }
+
+    public URL getUrl() {
+        return url;
+    }
+
+    public URL getCodeSourceUrl() {
+        return codeSource;
+    }
+
+    public boolean isDirectory() {
+        return jarEntry.isDirectory();
+    }
+
+    public InputStream getInputStream() throws IOException {
+        return jarFile.getInputStream(jarEntry);
+    }
+
+    public int getContentLength() {
+        return (int) jarEntry.getSize();
+    }
+
+    public Manifest getManifest() throws IOException {
+        return jarFile.getManifest();
+    }
+
+    public Attributes getAttributes() throws IOException {
+        return jarEntry.getAttributes();
+    }
+
+    public Certificate[] getCertificates() {
+        return jarEntry.getCertificates();
+    }
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarResourceLocation.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarResourceLocation.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarResourceLocation.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/JarResourceLocation.java Wed Jun 21 08:00:03 2006
@@ -0,0 +1,56 @@
+/**
+ *
+ * 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.servicemix.jbi.loaders;
+
+import java.util.jar.Manifest;
+import java.util.jar.JarFile;
+import java.util.jar.JarEntry;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.io.IOException;
+
+/**
+ * @version $Rev: 410741 $ $Date: 2006-06-01 06:35:48 +0200 (Thu, 01 Jun 2006) $
+ */
+public class JarResourceLocation extends AbstractUrlResourceLocation  {
+    private final JarFile jarFile;
+
+    public JarResourceLocation(URL codeSource, JarFile jarFile) {
+        super(codeSource);
+        this.jarFile = jarFile;
+    }
+
+    public ResourceHandle getResourceHandle(String resourceName) {
+        JarEntry jarEntry = jarFile.getJarEntry(resourceName);
+        if (jarEntry != null) {
+            try {
+                URL url = new URL(getCodeSource(), resourceName);
+                return new JarResourceHandle(jarFile, jarEntry, getCodeSource());
+            } catch (MalformedURLException e) {
+            }
+        }
+        return null;
+    }
+
+    public Manifest getManifest() throws IOException {
+        return jarFile.getManifest();
+    }
+
+    public void close() {
+        IoUtil.close(jarFile);
+    }
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/MultiParentClassLoader.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/MultiParentClassLoader.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/MultiParentClassLoader.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/MultiParentClassLoader.java Wed Jun 21 08:00:03 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.servicemix.jbi.loaders;
+
+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) +
+                "]";
+    }
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/NamedClassLoader.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/NamedClassLoader.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/NamedClassLoader.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/NamedClassLoader.java Wed Jun 21 08:00:03 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.servicemix.jbi.loaders;
+
+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()) +
+                "]";
+    }
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceEnumeration.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceEnumeration.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceEnumeration.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceEnumeration.java Wed Jun 21 08:00:03 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.servicemix.jbi.loaders;
+
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Collection;
+import java.util.NoSuchElementException;
+
+/**
+ * @version $Rev: 410741 $ $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;
+        }
+    }
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceFinder.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceFinder.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceFinder.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceFinder.java Wed Jun 21 08:00:03 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.servicemix.jbi.loaders;
+
+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: 410741 $ $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);
+
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceHandle.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceHandle.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceHandle.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceHandle.java Wed Jun 21 08:00:03 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.servicemix.jbi.loaders;
+
+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: 410741 $ $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();
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceLocation.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceLocation.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceLocation.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ResourceLocation.java Wed Jun 21 08:00:03 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.servicemix.jbi.loaders;
+
+import java.util.jar.Manifest;
+import java.io.IOException;
+import java.net.URL;
+
+/**
+ * This is a location which is searched by
+ * @version $Rev: 410741 $ $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();
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ThreadContextClassLoaderFactoryBean.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ThreadContextClassLoaderFactoryBean.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ThreadContextClassLoaderFactoryBean.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/ThreadContextClassLoaderFactoryBean.java Wed Jun 21 08:00:03 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.servicemix.jbi.loaders;
+
+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: 412595 $
+ */
+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;
+    }
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/UnionEnumeration.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/UnionEnumeration.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/UnionEnumeration.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/UnionEnumeration.java Wed Jun 21 08:00:03 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.servicemix.jbi.loaders;
+
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+/**
+ * @version $Rev: 410741 $ $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();
+    }
+}

Added: incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/UrlResourceFinder.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/UrlResourceFinder.java?rev=416002&view=auto
==============================================================================
--- incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/UrlResourceFinder.java (added)
+++ incubator/servicemix/trunk/servicemix-core/src/main/java/org/apache/servicemix/jbi/loaders/UrlResourceFinder.java Wed Jun 21 08:00:03 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.servicemix.jbi.loaders;
+
+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: 410741 $ $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;
+        }
+    }
+}