You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by da...@apache.org on 2006/05/13 01:51:40 UTC

svn commit: r405948 - in /geronimo/branches/1.1/modules/kernel: ./ src/java/org/apache/geronimo/kernel/classloader/ src/java/org/apache/geronimo/kernel/classloader/util/ src/test-data/resourceFinderTest/jar1/ src/test-data/resourceFinderTest/jar2/ src/...

Author: dain
Date: Fri May 12 16:51:36 2006
New Revision: 405948

URL: http://svn.apache.org/viewcvs?rev=405948&view=rev
Log:
Fixed GERONIMO-2008 Deployment of war with config file on classpath root fails due to '!' in url
Backed out and reapplied GERONIMO-1636 with changes for url 
Replaced JarFile subclasses with ResourceLocation wich abstracts management of resources from a single location
Added hooks for downloading a caching of remote resources
Added extensive test

Added:
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/AbstractUrlResourceLocation.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/DirectoryResourceHandle.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/DirectoryResourceLocation.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/IoUtil.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarResourceLocation.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceEnumeration.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceLocation.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/UnionEnumeration.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/UrlResourceFinder.java
    geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/classloader/UrlResourceFinderTest.java
Removed:
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileResourceFinder.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/util/
    geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/classloader/JarFileResourceFinderTest.java
Modified:
    geronimo/branches/1.1/modules/kernel/project.properties
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/AbstractResourceHandle.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileClassLoader.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileUrlConnection.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileUrlStreamHandler.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarResourceHandle.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceHandle.java
    geronimo/branches/1.1/modules/kernel/src/test-data/resourceFinderTest/jar1/resource
    geronimo/branches/1.1/modules/kernel/src/test-data/resourceFinderTest/jar2/resource

Modified: geronimo/branches/1.1/modules/kernel/project.properties
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/project.properties?rev=405948&r1=405947&r2=405948&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/project.properties (original)
+++ geronimo/branches/1.1/modules/kernel/project.properties Fri May 12 16:51:36 2006
@@ -4,3 +4,5 @@
 
 # set eclipse project name (used in eclipse plugin V1.11 onwards)
 maven.eclipse.project.name=${geronimo.maven.eclipse.modules.project.prefix}${pom.artifactId}
+maven.junit.fork=true
+maven.junit.forkmode=once

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/AbstractResourceHandle.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/AbstractResourceHandle.java?rev=405948&r1=405947&r2=405948&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/AbstractResourceHandle.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/AbstractResourceHandle.java Fri May 12 16:51:36 2006
@@ -22,8 +22,6 @@
 import java.util.jar.Attributes;
 import java.util.jar.Manifest;
 
-import org.apache.geronimo.kernel.classloader.util.ClassLoaderUtil;
-
 /**
  * @version $Rev$ $Date$
  */
@@ -31,10 +29,10 @@
     public byte[] getBytes() throws IOException {
         InputStream in = getInputStream();
         try {
-            byte[] bytes = ClassLoaderUtil.getBytes(in);
+            byte[] bytes = IoUtil.getBytes(in);
             return bytes;
         } finally {
-            ClassLoaderUtil.close(in);
+            IoUtil.close(in);
         }
     }
 
@@ -52,7 +50,7 @@
             return null;
         }
 
-        String entry = getURL().getFile();
+        String entry = getUrl().getFile();
         return m.getAttributes(entry);
     }
 
@@ -60,6 +58,6 @@
     }
 
     public String toString() {
-        return "[" + getName() + ": " + getURL() + "; code source: " + getCodeSourceURL() + "]";
+        return "[" + getName() + ": " + getUrl() + "; code source: " + getCodeSourceUrl() + "]";
     }
 }

Added: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/AbstractUrlResourceLocation.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/AbstractUrlResourceLocation.java?rev=405948&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/AbstractUrlResourceLocation.java (added)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/AbstractUrlResourceLocation.java Fri May 12 16:51:36 2006
@@ -0,0 +1,53 @@
+/**
+ *
+ * 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.geronimo.kernel.classloader;
+
+import java.net.URL;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public abstract class AbstractUrlResourceLocation implements ResourceLocation {
+    private final URL codeSource;
+
+    public AbstractUrlResourceLocation(URL codeSource) {
+        this.codeSource = codeSource;
+    }
+
+    public final URL getCodeSource() {
+        return codeSource;
+    }
+
+    public void close() {
+    }
+
+    public final boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        AbstractUrlResourceLocation that = (AbstractUrlResourceLocation) o;
+        return codeSource.equals(that.codeSource);
+    }
+
+    public final int hashCode() {
+        return codeSource.hashCode();
+    }
+
+    public final String toString() {
+        return "[" + getClass().getName() + ": " + codeSource + "]";
+    }
+}

Added: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/DirectoryResourceHandle.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/DirectoryResourceHandle.java?rev=405948&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/DirectoryResourceHandle.java (added)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/DirectoryResourceHandle.java Fri May 12 16:51:36 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.geronimo.kernel.classloader;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.security.cert.Certificate;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class DirectoryResourceHandle extends AbstractResourceHandle {
+    private final String name;
+    private final File file;
+    private final Manifest manifest;
+    private final URL url;
+    private final URL codeSource;
+
+    public DirectoryResourceHandle(String name, File file, File codeSource, Manifest manifest) throws MalformedURLException {
+        this.name = name;
+        this.file = file;
+        this.codeSource = codeSource.toURL();
+        this.manifest = manifest;
+        url = file.toURL();
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public URL getUrl() {
+        return url;
+    }
+
+    public URL getCodeSourceUrl() {
+        return codeSource;
+    }
+
+    public boolean isDirectory() {
+        return file.isDirectory();
+    }
+
+    public InputStream getInputStream() throws IOException {
+        if (file.isDirectory()) {
+            return new IoUtil.EmptyInputStream();
+        }
+        return new FileInputStream(file);
+    }
+
+    public int getContentLength() {
+        if (file.isDirectory() || file.length() > Integer.MAX_VALUE) {
+            return -1;
+        } else {
+            return (int) file.length();
+        }
+    }
+
+    public Manifest getManifest() throws IOException {
+        return manifest;
+    }
+
+    public Attributes getAttributes() throws IOException {
+        if (manifest == null) {
+            return null;
+        }
+        return manifest.getAttributes(getName());
+    }
+
+    /**
+     * Always return null.  This could be implementd by verifing the signatures
+     * in the manifest file against the actual file, but we don't need this right now.
+     * @return null
+     */
+    public Certificate[] getCertificates() {
+        return null;
+    }
+}

Added: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/DirectoryResourceLocation.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/DirectoryResourceLocation.java?rev=405948&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/DirectoryResourceLocation.java (added)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/DirectoryResourceLocation.java Fri May 12 16:51:36 2006
@@ -0,0 +1,79 @@
+/**
+ *
+ * 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.geronimo.kernel.classloader;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.jar.Manifest;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class DirectoryResourceLocation extends AbstractUrlResourceLocation {
+    private final File baseDir;
+    private boolean manifestLoaded = false;
+    private Manifest manifest;
+
+    public DirectoryResourceLocation(File baseDir) throws MalformedURLException {
+        super(baseDir.toURL());
+        this.baseDir = baseDir;
+    }
+
+    public ResourceHandle getResourceHandle(String resourceName) {
+        File file = new File(baseDir, resourceName);
+        if (!file.exists()) {
+            return null;
+        }
+
+        try {
+            ResourceHandle resourceHandle = new DirectoryResourceHandle(resourceName, file, baseDir, getManifestSafe());
+            return resourceHandle;
+        } catch (MalformedURLException e) {
+            return null;
+        }
+    }
+
+    public Manifest getManifest() throws IOException {
+        if (!manifestLoaded) {
+            File manifestFile = new File(baseDir, "META-INF/MANIFEST.MF");
+
+            if (manifestFile.isFile() && manifestFile.canRead()) {
+                FileInputStream in = null;
+                try {
+                    in = new FileInputStream(manifestFile);
+                    manifest = new Manifest(in);
+                } finally {
+                    IoUtil.close(in);
+                }
+            }
+            manifestLoaded = true;
+        }
+        return manifest;
+    }
+
+    private Manifest getManifestSafe() {
+        Manifest manifest = null;
+        try {
+            manifest = getManifest();
+        } catch (IOException e) {
+            // ignore
+        }
+        return manifest;
+    }
+}

Added: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/IoUtil.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/IoUtil.java?rev=405948&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/IoUtil.java (added)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/IoUtil.java Fri May 12 16:51:36 2006
@@ -0,0 +1,167 @@
+/**
+ *
+ * Copyright 2004 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.geronimo.kernel.classloader;
+
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.io.FileNotFoundException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLStreamHandler;
+import java.net.URLConnection;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.apache.geronimo.kernel.classloader.ResourceLocation;
+import org.apache.geronimo.kernel.classloader.JarResourceLocation;
+import org.apache.geronimo.kernel.classloader.DirectoryResourceLocation;
+
+/**
+ * @version $Rev$ $Date: 2006-05-03 19:53:50 -0700 (Wed, 03 May 2006) $
+ */
+public final class IoUtil {
+    private IoUtil() {
+    }
+
+    public static byte[] getBytes(InputStream inputStream) throws IOException {
+        try {
+            byte[] buffer = new byte[4096];
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            for (int count = inputStream.read(buffer); count >= 0; count = inputStream.read(buffer)) {
+                out.write(buffer, 0, count);
+            }
+            byte[] bytes = out.toByteArray();
+            return bytes;
+        } finally {
+            close(inputStream);
+        }
+    }
+
+    public static void flush(OutputStream thing) {
+        if (thing != null) {
+            try {
+                thing.flush();
+            } catch(Exception ignored) {
+            }
+        }
+    }
+
+    public static void flush(Writer thing) {
+        if (thing != null) {
+            try {
+                thing.flush();
+            } catch(Exception ignored) {
+            }
+        }
+    }
+
+    public static void close(JarFile thing) {
+        if (thing != null) {
+            try {
+                thing.close();
+            } catch(Exception ignored) {
+            }
+        }
+    }
+
+    public static void close(InputStream thing) {
+        if (thing != null) {
+            try {
+                thing.close();
+            } catch(Exception ignored) {
+            }
+        }
+    }
+
+    public static void close(OutputStream thing) {
+        if (thing != null) {
+            try {
+                thing.close();
+            } catch(Exception ignored) {
+            }
+        }
+    }
+
+    public static void close(Reader thing) {
+        if (thing != null) {
+            try {
+                thing.close();
+            } catch(Exception ignored) {
+            }
+        }
+    }
+
+    public static void close(Writer thing) {
+        if (thing != null) {
+            try {
+                thing.close();
+            } catch(Exception ignored) {
+            }
+        }
+    }
+
+    public static final class EmptyInputStream extends InputStream {
+        public int read() {
+            return -1;
+        }
+
+        public int read(byte b[])  {
+            return -1;
+        }
+
+        public int read(byte b[], int off, int len) {
+            return -1;
+        }
+
+        public long skip(long n) {
+            return 0;
+        }
+
+        public int available() {
+            return 0;
+        }
+
+        public void close() {
+        }
+
+        public synchronized void mark(int readlimit) {
+        }
+
+        public synchronized void reset() {
+        }
+
+        public boolean markSupported() {
+            return false;
+        }
+    }
+}

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileClassLoader.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileClassLoader.java?rev=405948&r1=405947&r2=405948&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileClassLoader.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileClassLoader.java Fri May 12 16:51:36 2006
@@ -32,7 +32,6 @@
 import java.util.jar.Attributes;
 import java.util.jar.Manifest;
 
-import org.apache.geronimo.kernel.classloader.util.UnionEnumeration;
 import org.apache.geronimo.kernel.config.MultiParentClassLoader;
 import org.apache.geronimo.kernel.repository.Artifact;
 
@@ -52,7 +51,7 @@
 public class JarFileClassLoader extends MultiParentClassLoader {
     private static final URL[] EMPTY_URLS = new URL[0];
 
-    private final JarFileResourceFinder resourceFinder = new JarFileResourceFinder();
+    private final UrlResourceFinder resourceFinder = new UrlResourceFinder();
     private final AccessControlContext acc;
 
     /**
@@ -211,7 +210,7 @@
         }
 
         // the library must be accessable on the file system
-        URL url = resourceHandle.getURL();
+        URL url = resourceHandle.getUrl();
         if (!"file".equals(url.getProtocol())) {
             return null;
         }
@@ -264,7 +263,7 @@
                     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();
+                    URL codeSourceUrl = resourceHandle.getCodeSourceUrl();
 
                     // define the package (required for security)
                     definePackage(className, codeSourceUrl, manifest);

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileUrlConnection.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileUrlConnection.java?rev=405948&r1=405947&r2=405948&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileUrlConnection.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileUrlConnection.java Fri May 12 16:51:36 2006
@@ -22,6 +22,8 @@
 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;
@@ -29,19 +31,30 @@
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
 
-import org.apache.geronimo.kernel.classloader.util.ClassLoaderUtil;
-
 /**
  * @version $Rev$ $Date$
  */
 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(ClassLoaderUtil.DUMMY_JAR_URL);
+        super(DUMMY_JAR_URL);
 
         if (url == null) throw new NullPointerException("url is null");
         if (jarFile == null) throw new NullPointerException("jarFile is null");

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileUrlStreamHandler.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileUrlStreamHandler.java?rev=405948&r1=405947&r2=405948&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileUrlStreamHandler.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarFileUrlStreamHandler.java Fri May 12 16:51:36 2006
@@ -29,9 +29,12 @@
  */
 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 jarFileUrl = new File(jarFile.getName()).toURL();
-        URL url = new URL("jar", "", -1, jarFileUrl + "!/" + jarEntry.getName(), handler);
+        URL url = new URL("jar", "", -1, codeSource + "!/" + jarEntry.getName(), handler);
         handler.setExpectedUrl(url);
         return url;
     }

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarResourceHandle.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarResourceHandle.java?rev=405948&r1=405947&r2=405948&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarResourceHandle.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarResourceHandle.java Fri May 12 16:51:36 2006
@@ -21,6 +21,7 @@
 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;
@@ -34,10 +35,10 @@
     private final URL url;
     private final URL codeSource;
 
-    public JarResourceHandle(JarFile jarFile, JarEntry jarEntry, URL url, URL codeSource) {
+    public JarResourceHandle(JarFile jarFile, JarEntry jarEntry, URL codeSource) throws MalformedURLException {
         this.jarFile = jarFile;
         this.jarEntry = jarEntry;
-        this.url = url;
+        this.url = JarFileUrlStreamHandler.createUrl(jarFile, jarEntry, codeSource);
         this.codeSource = codeSource;
     }
 
@@ -45,12 +46,16 @@
         return jarEntry.getName();
     }
 
-    public URL getURL() {
+    public URL getUrl() {
         return url;
     }
 
-    public URL getCodeSourceURL() {
+    public URL getCodeSourceUrl() {
         return codeSource;
+    }
+
+    public boolean isDirectory() {
+        return jarEntry.isDirectory();
     }
 
     public InputStream getInputStream() throws IOException {

Added: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarResourceLocation.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarResourceLocation.java?rev=405948&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarResourceLocation.java (added)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/JarResourceLocation.java Fri May 12 16:51:36 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.geronimo.kernel.classloader;
+
+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$ $Date$
+ */
+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: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceEnumeration.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceEnumeration.java?rev=405948&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceEnumeration.java (added)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceEnumeration.java Fri May 12 16:51:36 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.geronimo.kernel.classloader;
+
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Collection;
+import java.util.NoSuchElementException;
+
+/**
+ * @version $Rev$ $Date$
+ */
+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;
+        }
+    }
+}

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceHandle.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceHandle.java?rev=405948&r1=405947&r2=405948&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceHandle.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceHandle.java Fri May 12 16:51:36 2006
@@ -45,12 +45,18 @@
     /**
      * Returns the URL of the resource.
      */
-    URL getURL();
+    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();
+    URL getCodeSourceUrl();
 
     /**
      * Returns and InputStream for reading this resource data.

Added: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceLocation.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceLocation.java?rev=405948&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceLocation.java (added)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/ResourceLocation.java Fri May 12 16:51:36 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.geronimo.kernel.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$
+ */
+public interface ResourceLocation {
+    URL getCodeSource();    
+    ResourceHandle getResourceHandle(String resourceName);
+    Manifest getManifest() throws IOException;
+    void close();
+}

Added: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/UnionEnumeration.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/UnionEnumeration.java?rev=405948&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/UnionEnumeration.java (added)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/UnionEnumeration.java Fri May 12 16:51:36 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.geronimo.kernel.classloader;
+
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+/**
+ * @version $Rev$ $Date$
+ */
+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: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/UrlResourceFinder.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/UrlResourceFinder.java?rev=405948&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/UrlResourceFinder.java (added)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/classloader/UrlResourceFinder.java Fri May 12 16:51:36 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.geronimo.kernel.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$
+ */
+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;
+        }
+    }
+}

Modified: geronimo/branches/1.1/modules/kernel/src/test-data/resourceFinderTest/jar1/resource
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/test-data/resourceFinderTest/jar1/resource?rev=405948&r1=405947&r2=405948&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/test-data/resourceFinderTest/jar1/resource (original)
+++ geronimo/branches/1.1/modules/kernel/src/test-data/resourceFinderTest/jar1/resource Fri May 12 16:51:36 2006
@@ -0,0 +1 @@
+resource1
\ No newline at end of file

Modified: geronimo/branches/1.1/modules/kernel/src/test-data/resourceFinderTest/jar2/resource
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/test-data/resourceFinderTest/jar2/resource?rev=405948&r1=405947&r2=405948&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/test-data/resourceFinderTest/jar2/resource (original)
+++ geronimo/branches/1.1/modules/kernel/src/test-data/resourceFinderTest/jar2/resource Fri May 12 16:51:36 2006
@@ -0,0 +1 @@
+resource2
\ No newline at end of file

Added: geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/classloader/UrlResourceFinderTest.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/classloader/UrlResourceFinderTest.java?rev=405948&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/classloader/UrlResourceFinderTest.java (added)
+++ geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/kernel/classloader/UrlResourceFinderTest.java Fri May 12 16:51:36 2006
@@ -0,0 +1,362 @@
+/**
+ *
+ * Copyright 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.geronimo.kernel.classloader;
+
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.zip.ZipEntry;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+import java.util.jar.Attributes;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.File;
+import java.io.FileOutputStream;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev:$ $Date:$
+ */
+public class UrlResourceFinderTest extends TestCase {
+    private File jarFile;
+    private Manifest manifest;
+    private Attributes resourceAttributes;
+
+    /**
+     * There are 2 "jars" with a "resource" inside.  Make sure the enumeration has exactly 2 elements and
+     * that hasMoreElements() doesn't advance the iterator.
+     *
+     * @throws Exception
+     */
+    public void testResourceEnumeration() throws Exception {
+        URL jar1 = new File("src/test-data/resourceFinderTest/jar1/").toURL();
+        URL jar2 = new File("src/test-data/resourceFinderTest/jar2/").toURL();
+        UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL[] {jar1, jar2});
+
+        Enumeration enumeration = resourceFinder.findResources("resource");
+
+        // resource1
+        assertTrue(enumeration.hasMoreElements());
+        assertTrue(enumeration.hasMoreElements());
+        URL resource1 = (URL) enumeration.nextElement();
+        assertNotNull(resource1);
+        assertEquals("resource1", toString(resource1.openStream()));
+
+        // resource2
+        assertTrue(enumeration.hasMoreElements());
+        assertTrue(enumeration.hasMoreElements());
+        URL resource2 = (URL) enumeration.nextElement();
+        assertNotNull(resource2);
+        assertEquals("resource2", toString(resource2.openStream()));
+        assertFalse(enumeration.hasMoreElements());
+    }
+
+    public void testDirectoryResource() throws Exception {
+        URL jar = new File("src/test-data/resourceFinderTest/jar1/").toURL();
+        UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL[] {jar});
+
+        ResourceHandle resource = resourceFinder.getResource("resource");
+        assertNotNull(resource);
+
+        // handle.getBytes()
+        assertEquals("resource1", new String(resource.getBytes()));
+
+        // handle.getInputStream()
+        assertEquals("resource1", toString(resource.getInputStream()));
+
+        // handle.getUrl()
+        URL url = resource.getUrl();
+        assertEquals("resource1", toString(url.openStream()));
+
+        // copy the url and verify we can still get the data
+        URL copyUrl = new URL(url.toExternalForm());
+        assertEquals("resource1", toString(copyUrl.openStream()));
+
+        // resourceFinder.findResource
+        URL directUrl = resourceFinder.findResource("resource");
+        assertEquals("resource1", toString(directUrl.openStream()));
+        assertEquals("resource1", toString(new URL(directUrl.toExternalForm()).openStream()));
+
+        // handle.getContentLength()
+        assertEquals("resource1".length(), resource.getContentLength());
+
+        // handle.getName()
+        assertEquals("resource", resource.getName());
+
+        // handle.getAttributes()
+        assertNull(resource.getAttributes());
+
+        // handle.getManifest()
+        assertNull(resource.getManifest());
+    }
+
+    public void testJarResource() throws Exception {
+        URL jar = jarFile.toURL();
+        UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL[] {jar});
+
+        ResourceHandle resource = resourceFinder.getResource("resource");
+        assertNotNull(resource);
+
+        // handle.getBytes()
+        assertEquals("resource3", new String(resource.getBytes()));
+
+        // handle.getInputStream()
+        assertEquals("resource3", toString(resource.getInputStream()));
+
+        // handle.getUrl()
+        URL url = resource.getUrl();
+        assertEquals("resource3", toString(url.openStream()));
+
+        // copy the url and verify we can still get the data
+        URL copyUrl = new URL(url.toExternalForm());
+        assertEquals("resource3", toString(copyUrl.openStream()));
+
+        // resourceFinder.findResource
+        URL directUrl = resourceFinder.findResource("resource");
+        assertEquals("resource3", toString(directUrl.openStream()));
+        assertEquals("resource3", toString(new URL(directUrl.toExternalForm()).openStream()));
+
+        // handle.getContentLength()
+        assertEquals("resource3".length(), resource.getContentLength());
+
+        // handle.getName()
+        assertEquals("resource", resource.getName());
+
+        // handle.getAttributes()
+        assertEquals(resourceAttributes, resource.getAttributes());
+
+        // handle.getManifest()
+        assertEquals(manifest, resource.getManifest());
+    }
+
+    public void testAddURL() throws Exception {
+        URL jar1 = new File("src/test-data/resourceFinderTest/jar1/").toURL();
+        UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL[] {jar1});
+
+        Enumeration enumeration = resourceFinder.findResources("resource");
+
+        // resource1
+        assertTrue(enumeration.hasMoreElements());
+        assertTrue(enumeration.hasMoreElements());
+        URL resource1 = (URL) enumeration.nextElement();
+        assertNotNull(resource1);
+        assertEquals("resource1", toString(resource1.openStream()));
+        assertFalse(enumeration.hasMoreElements());
+
+        // addUrl
+        URL jar2 = new File("src/test-data/resourceFinderTest/jar2/").toURL();
+        resourceFinder.addUrl(jar2);
+
+        // getResource should find the first jar only
+        ResourceHandle resource = resourceFinder.getResource("resource");
+        assertNotNull(resource);
+        assertEquals("resource1", new String(resource.getBytes()));
+
+        // findResource should find the first jar only
+        resource1 = resourceFinder.findResource("resource");
+        assertEquals("resource1", toString(resource1.openStream()));
+
+        // findResouces should see both jars
+        enumeration = resourceFinder.findResources("resource");
+
+        // resource1
+        assertTrue(enumeration.hasMoreElements());
+        assertTrue(enumeration.hasMoreElements());
+        resource1 = (URL) enumeration.nextElement();
+        assertNotNull(resource1);
+        assertEquals("resource1", toString(resource1.openStream()));
+        assertTrue(enumeration.hasMoreElements());
+
+        // resource2
+        assertTrue(enumeration.hasMoreElements());
+        assertTrue(enumeration.hasMoreElements());
+        URL resource2 = (URL) enumeration.nextElement();
+        assertNotNull(resource2);
+        assertEquals("resource2", toString(resource2.openStream()));
+        assertFalse(enumeration.hasMoreElements());
+    }
+
+    public void testConcurrentAddURL() throws Exception {
+        URL jar1 = new File("src/test-data/resourceFinderTest/jar1/").toURL();
+        URL jar2 = new File("src/test-data/resourceFinderTest/jar2/").toURL();
+        UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL[] {jar1, jar2});
+
+        Enumeration enumeration = resourceFinder.findResources("resource");
+
+        // resource1
+        assertTrue(enumeration.hasMoreElements());
+        assertTrue(enumeration.hasMoreElements());
+        URL resource1 = (URL) enumeration.nextElement();
+        assertNotNull(resource1);
+        assertEquals("resource1", toString(resource1.openStream()));
+        assertTrue(enumeration.hasMoreElements());
+
+        //
+        // addURL
+        //
+        URL newJar = jarFile.toURL();
+        resourceFinder.addUrl(newJar);
+
+        // new resources should be available
+        // getResource should find the first jar only
+        ResourceHandle jar3Resouce = resourceFinder.getResource("jar3");
+        assertNotNull(jar3Resouce);
+        assertEquals("jar3", new String(jar3Resouce.getBytes()));
+
+        // findResource should find the first jar only
+        URL jar3Url = resourceFinder.findResource("jar3");
+        assertEquals("jar3", toString(jar3Url.openStream()));
+
+        //
+        // enumeration from above should still be valid, but only see the resources available at the time it was created
+        //
+
+        // resource2
+        assertTrue(enumeration.hasMoreElements());
+        assertTrue(enumeration.hasMoreElements());
+        URL resource2 = (URL) enumeration.nextElement();
+        assertNotNull(resource2);
+        assertEquals("resource2", toString(resource2.openStream()));
+        assertFalse(enumeration.hasMoreElements());
+    }
+
+    public void testDirectoryDestroy() throws Exception {
+        URL jar = new File("src/test-data/resourceFinderTest/jar1/").toURL();
+        UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL[] {jar});
+        assertDestroyed(resourceFinder, "resource1", null);
+    }
+
+    public void testJarDestroy() throws Exception {
+        URL jar = jarFile.toURL();
+        UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL[] {jar});
+        assertDestroyed(resourceFinder, "resource3", manifest);
+    }
+
+    public void assertDestroyed(UrlResourceFinder resourceFinder, String resourceValue, Manifest expectedManifest) throws Exception {
+        ResourceHandle resource = resourceFinder.getResource("resource");
+        assertNotNull(resource);
+        assertEquals(resourceValue, new String(resource.getBytes()));
+
+        // handle.getUrl()
+        URL url = resource.getUrl();
+        assertEquals(resourceValue, toString(url.openStream()));
+
+        // copy the url and verify we can still get the data
+        URL copyUrl = new URL(url.toExternalForm());
+        assertEquals(resourceValue, toString(copyUrl.openStream()));
+
+        // resourceFinder.findResource
+        URL directUrl = resourceFinder.findResource("resource");
+        assertEquals(resourceValue, toString(directUrl.openStream()));
+        URL directUrlCopy = new URL(directUrl.toExternalForm());
+        assertEquals(resourceValue, toString(directUrlCopy.openStream()));
+
+
+        // destroy
+        resourceFinder.destroy();
+
+        // getResource always returns null
+        assertNull(resourceFinder.getResource("resource"));
+
+        // findResource always returns null
+        assertNull(resourceFinder.findResource("resource"));
+
+        // findResources always returns an empty enumeration
+        assertFalse(resourceFinder.findResources("resource").hasMoreElements());
+
+        // existing url may not work
+        try {
+            assertEquals(resourceValue, toString(url.openStream()));
+        } catch (IllegalStateException expected) {
+        } catch (IOException expected) {
+        }
+        try {
+            assertEquals(resourceValue, toString(directUrl.openStream()));
+        } catch (IllegalStateException expected) {
+        } catch (IOException expected) {
+        }
+
+        // the copied urls will work since they are proviced by the vm
+        assertEquals(resourceValue, toString(copyUrl.openStream()));
+        assertEquals(resourceValue, toString(directUrlCopy.openStream()));
+
+        // existing resource handle may not work since the location was closed
+        assertEquals("resource", resource.getName());
+        try {
+            if (expectedManifest != null) {
+                assertEquals(expectedManifest.getAttributes("resource"), resource.getAttributes());
+            }
+        } catch (IllegalStateException expected) {
+        }
+        try {
+            assertEquals(expectedManifest, resource.getManifest());
+        } catch (IllegalStateException expected) {
+        }
+        try {
+            assertEquals(resourceValue, toString(resource.getUrl().openStream()));
+        } catch (IllegalStateException expected) {
+        }
+        try {
+            assertEquals(resourceValue, toString(resource.getInputStream()));
+        } catch (IllegalStateException expected) {
+        } catch (IOException expected) {
+        }
+        try {
+            assertEquals(resourceValue, new String(resource.getBytes()));
+        } catch (IllegalStateException expected) {
+        } catch (IOException expected) {
+        }
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        manifest = new Manifest();
+        Attributes mainAttributes = manifest.getMainAttributes();
+        mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
+        mainAttributes.putValue("food", "nacho");
+        resourceAttributes = new Attributes();
+        resourceAttributes.putValue("drink", "margarita");
+        manifest.getEntries().put("resource", resourceAttributes);
+
+        jarFile = new File("target/resourceFinderTest.jar");
+        System.out.println(jarFile.getAbsolutePath());
+        JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(jarFile), manifest);
+        jarOutputStream.putNextEntry(new ZipEntry("resource"));
+        jarOutputStream.write("resource3".getBytes());
+        jarOutputStream.putNextEntry(new ZipEntry("jar3"));
+        jarOutputStream.write("jar3".getBytes());
+        IoUtil.close(jarOutputStream);
+    }
+
+    protected void tearDown() throws Exception {
+        jarFile.delete();
+        super.tearDown();
+    }
+
+    private static String toString(InputStream in) throws IOException {
+        try {
+            byte[] bytes = IoUtil.getBytes(in);
+            String string = new String(bytes);
+            return string;
+        } finally {
+            IoUtil.close(in);
+        }
+    }
+}