geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: rev 46300 - geronimo/trunk/modules/deployment/src/java/org/apache/geronimo/deployment/util
Date Sat, 18 Sep 2004 18:53:09 GMT
Author: dain
Date: Sat Sep 18 11:53:08 2004
New Revision: 46300

Added:
   geronimo/trunk/modules/deployment/src/java/org/apache/geronimo/deployment/util/UnpackedJarEntry.java
   geronimo/trunk/modules/deployment/src/java/org/apache/geronimo/deployment/util/UnpackedJarFile.java
Modified:
   geronimo/trunk/modules/deployment/src/java/org/apache/geronimo/deployment/util/FileUtil.java
Log:
Added UnpackedJarFile which exposes directory as a JarFile.  This is useful for simplifing
deployment of unpacked archives, since we can now simply reuse the packed archive code.


Modified: geronimo/trunk/modules/deployment/src/java/org/apache/geronimo/deployment/util/FileUtil.java
==============================================================================
--- geronimo/trunk/modules/deployment/src/java/org/apache/geronimo/deployment/util/FileUtil.java
(original)
+++ geronimo/trunk/modules/deployment/src/java/org/apache/geronimo/deployment/util/FileUtil.java
Sat Sep 18 11:53:08 2004
@@ -22,6 +22,8 @@
 import java.io.IOException;
 import java.io.FileOutputStream;
 import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Collections;
 
 /**
  *
@@ -62,16 +64,22 @@
         root.delete();
     }
 
-    public static void listRecursiveFiles(File aFile, Collection aColl) {
-        File[] files = aFile.listFiles();
+    public static Collection listRecursiveFiles(File file) {
+        LinkedList list = new LinkedList();
+        listRecursiveFiles(file, list);
+        return Collections.unmodifiableCollection(list);
+    }
+
+    public static void listRecursiveFiles(File file, Collection collection) {
+        File[] files = file.listFiles();
         if ( null == files ) {
             return;
         }
         for (int i = 0; i < files.length; i++) {
-            if ( files[i].isFile() ) {
-                aColl.add(files[i]);
+            if (files[i].isDirectory()) {
+                listRecursiveFiles(files[i], collection);
             } else {
-                listRecursiveFiles(files[i], aColl);
+                collection.add(files[i]);
             }
         }
     }

Added: geronimo/trunk/modules/deployment/src/java/org/apache/geronimo/deployment/util/UnpackedJarEntry.java
==============================================================================
--- (empty file)
+++ geronimo/trunk/modules/deployment/src/java/org/apache/geronimo/deployment/util/UnpackedJarEntry.java
Sat Sep 18 11:53:08 2004
@@ -0,0 +1,175 @@
+/**
+ *
+ * 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.deployment.util;
+
+import java.util.jar.JarEntry;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.io.IOException;
+import java.io.File;
+import java.security.cert.Certificate;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public class UnpackedJarEntry extends JarEntry {
+    private final File file;
+    private final Manifest manifest;
+
+    public UnpackedJarEntry(String name, File file, Manifest manifest) {
+        super(name);
+        this.file = file;
+        this.manifest = manifest;
+    }
+
+    public File getFile() {
+        return file;
+    }
+
+    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 for
+     * Geronimo.
+     * @return null
+     */
+    public Certificate[] getCertificates() {
+        return null;
+    }
+
+    /**
+     * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
+     * @param time ignored
+     * @throws UnsupportedOperationException always
+     */
+    public void setTime(long time) throws UnsupportedOperationException {
+        throw new UnsupportedOperationException("Can not change the time of unpacked jar
entry");
+    }
+
+    public long getTime() {
+        return file.lastModified();
+    }
+
+    /**
+     * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
+     * @param size ignored
+     * @throws UnsupportedOperationException always
+     */
+    public void setSize(long size) throws UnsupportedOperationException {
+        throw new UnsupportedOperationException("Can not change the size of unpacked jar
entry");
+    }
+
+    public long getSize() {
+        if (file.isDirectory()) {
+            return -1;
+        } else {
+            return file.length();
+        }
+    }
+
+    /**
+     * An unpacked jar is not compressed, so this method returns getSize().
+     * @return getSize()
+     */
+    public long getCompressedSize() {
+        return getSize();
+    }
+
+    /**
+     * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
+     * @param compressedSize ignored
+     * @throws UnsupportedOperationException always
+     */
+    public void setCompressedSize(long compressedSize) {
+        throw new UnsupportedOperationException("Can not change the compressed size of unpacked
jar entry");
+    }
+
+    public long getCrc() {
+        return super.getCrc();    //To change body of overridden methods use File | Settings
| File Templates.
+    }
+
+    /**
+     * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
+     * @param crc ignored
+     * @throws UnsupportedOperationException always
+     */
+    public void setCrc(long crc) {
+        throw new UnsupportedOperationException("Can not change the crc of unpacked jar entry");
+    }
+
+    public int getMethod() {
+        return ZipEntry.STORED;
+    }
+
+    /**
+     * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
+     * @param method ignored
+     * @throws UnsupportedOperationException always
+     */
+    public void setMethod(int method) {
+        throw new UnsupportedOperationException("Can not change the method of unpacked jar
entry");
+    }
+
+    /**
+     * Always returns null.
+     * @return null
+     */
+    public byte[] getExtra() {
+        return null;
+    }
+
+    /**
+     * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
+     * @param extra ignored
+     * @throws UnsupportedOperationException always
+     */
+    public void setExtra(byte[] extra) {
+        throw new UnsupportedOperationException("Can not change the extra data of unpacked
jar entry");
+    }
+
+    /**
+     * Always returns null.
+     * @return null
+     */
+    public String getComment() {
+        return null;
+    }
+
+    /**
+     * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
+     * @param comment ignored
+     * @throws UnsupportedOperationException always
+     */
+    public void setComment(String comment) {
+        throw new UnsupportedOperationException("Can not change the comment of unpacked jar
entry");
+    }
+
+    public boolean isDirectory() {
+        return file.isDirectory();
+    }
+
+    public Object clone() {
+        return new UnpackedJarEntry(getName(), file, manifest);
+    }
+}

Added: geronimo/trunk/modules/deployment/src/java/org/apache/geronimo/deployment/util/UnpackedJarFile.java
==============================================================================
--- (empty file)
+++ geronimo/trunk/modules/deployment/src/java/org/apache/geronimo/deployment/util/UnpackedJarFile.java
Sat Sep 18 11:53:08 2004
@@ -0,0 +1,169 @@
+/**
+ *
+ * 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.deployment.util;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.FileInputStream;
+import java.util.Enumeration;
+import java.util.LinkedList;
+import java.util.Iterator;
+import java.util.Collections;
+import java.util.Collection;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+import java.net.URI;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public class UnpackedJarFile extends JarFile {
+    private static final File dummyJarFile;
+    static {
+        try {
+            dummyJarFile = File.createTempFile("fake", null);
+            new JarOutputStream(new FileOutputStream(dummyJarFile), new Manifest()).close();
+            dummyJarFile.deleteOnExit();
+        } catch (IOException e) {
+            throw new ExceptionInInitializerError(e);
+        }
+    }
+
+    private final File baseDir;
+    private boolean manifestLoaded = false;
+    private Manifest manifest;
+
+    public UnpackedJarFile(String name) throws IOException {
+        this(new File(name));
+    }
+
+    public UnpackedJarFile(File baseDir) throws IOException {
+        super(dummyJarFile);
+        this.baseDir = baseDir;
+        if (!baseDir.isDirectory()) {
+            throw new IOException("File must be a directory: file=" + baseDir.getAbsolutePath());
+        }
+    }
+
+    public Manifest getManifest() throws IOException {
+        if (manifestLoaded) {
+            File manifestFile = getFile("META-INF/MANIFEST.MF");
+
+            if (manifestFile.isFile()) {
+                FileInputStream in = null;
+                try {
+                    in = new FileInputStream(manifestFile);
+                    manifest = new Manifest(in);
+                } finally {
+                    if (in != null) {
+                        try {
+                            in.close();
+                        } catch (IOException e) {
+                            // ignore
+                        }
+                    }
+                }
+            }
+            manifestLoaded = true;
+        }
+        return manifest;
+    }
+
+    public JarEntry getJarEntry(String name) {
+        File file = getFile(name);
+        if (file == null) {
+            return null;
+        }
+        return new UnpackedJarEntry(name, file, getManifestSafe());
+    }
+
+    public ZipEntry getEntry(String name) {
+        return getJarEntry(name);
+    }
+
+    public Enumeration entries() {
+        Collection files = FileUtil.listRecursiveFiles(baseDir);
+
+        Manifest manifest = getManifestSafe();
+        LinkedList entries = new LinkedList();
+        URI baseURI = baseDir.getAbsoluteFile().toURI();
+        for (Iterator iterator = files.iterator(); iterator.hasNext();) {
+            File entryFile = ((File) iterator.next()).getAbsoluteFile();
+            URI entryURI = entryFile.toURI();
+            URI relativeURI = baseURI.relativize(entryURI);
+            entries.add(new UnpackedJarEntry(relativeURI.getPath(), entryFile, manifest));
+        }
+        return Collections.enumeration(entries);
+    }
+
+    public InputStream getInputStream(ZipEntry zipEntry) throws IOException {
+        File file;
+        if (zipEntry instanceof UnpackedJarEntry) {
+            file = getFile(zipEntry.getName());
+        } else {
+            file = ((UnpackedJarEntry)zipEntry).getFile();
+        }
+
+        if (file == null) {
+            throw new IOException("Entry not found: name=" + file.getAbsolutePath());
+        } else if (file.isDirectory()) {
+            throw new IOException("Entry is a directory: name=" + file.getAbsolutePath());
+        }
+        return new FileInputStream(file);
+    }
+
+    public String getName() {
+        return baseDir.getAbsolutePath();
+    }
+
+    /**
+     * Always returns -1.
+     * @return -1
+     */
+    public int size() {
+        return -1;
+    }
+
+    public void close() throws IOException {
+    }
+
+    protected void finalize() throws IOException {
+    }
+
+    private File getFile(String name) {
+        File file = new File(baseDir, name);
+        if (!file.exists()) {
+            return null;
+        }
+        return file;
+    }
+
+    private Manifest getManifestSafe() {
+        Manifest manifest = null;
+        try {
+            manifest = getManifest();
+        } catch (IOException e) {
+            // ignore
+        }
+        return manifest;
+    }
+}

Mime
View raw message