001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.deployment.util;
018    
019    import java.io.File;
020    import java.io.FileInputStream;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.net.URI;
024    import java.util.Collection;
025    import java.util.Collections;
026    import java.util.Enumeration;
027    import java.util.Iterator;
028    import java.util.LinkedList;
029    import java.util.jar.JarEntry;
030    import java.util.jar.JarFile;
031    import java.util.jar.Manifest;
032    import java.util.zip.ZipEntry;
033    
034    /**
035     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
036     */
037    public class UnpackedJarFile extends JarFile {
038        private final File baseDir;
039        private boolean manifestLoaded = false;
040        private Manifest manifest;
041    
042        public UnpackedJarFile(File baseDir) throws IOException {
043            super(DeploymentUtil.DUMMY_JAR_FILE);
044            this.baseDir = baseDir;
045            if (!baseDir.isDirectory()) {
046                throw new IOException("File must be a directory: file=" + baseDir.getAbsolutePath());
047            }
048        }
049    
050        public File getBaseDir() {
051            return baseDir;
052        }
053    
054        public Manifest getManifest() throws IOException {
055            if (!manifestLoaded) {
056                File manifestFile = getFile("META-INF/MANIFEST.MF");
057    
058                if (manifestFile != null && manifestFile.isFile()) {
059                    FileInputStream in = null;
060                    try {
061                        in = new FileInputStream(manifestFile);
062                        manifest = new Manifest(in);
063                    } finally {
064                        if (in != null) {
065                            try {
066                                in.close();
067                            } catch (IOException e) {
068                                // ignore
069                            }
070                        }
071                    }
072                }
073                manifestLoaded = true;
074            }
075            return manifest;
076        }
077    
078        public UnpackedJarEntry getUnpackedJarEntry(String name) {
079            File file = getFile(name);
080            if (file == null) {
081                return null;
082            }
083            return new UnpackedJarEntry(name, file, getManifestSafe());
084        }
085    
086        public JarEntry getJarEntry(String name) {
087            return getUnpackedJarEntry(name);
088        }
089    
090        public ZipEntry getEntry(String name) {
091            return getUnpackedJarEntry(name);
092        }
093    
094        public Enumeration entries() {
095            Collection files = DeploymentUtil.listRecursiveFiles(baseDir);
096    
097            Manifest manifest = getManifestSafe();
098            LinkedList entries = new LinkedList();
099            URI baseURI = baseDir.getAbsoluteFile().toURI();
100            for (Iterator iterator = files.iterator(); iterator.hasNext();) {
101                File entryFile = ((File) iterator.next()).getAbsoluteFile();
102                URI entryURI = entryFile.toURI();
103                URI relativeURI = baseURI.relativize(entryURI);
104                entries.add(new UnpackedJarEntry(relativeURI.getPath(), entryFile, manifest));
105            }
106            return Collections.enumeration(entries);
107        }
108    
109        public InputStream getInputStream(ZipEntry zipEntry) throws IOException {
110            File file;
111            if (zipEntry instanceof UnpackedJarEntry) {
112                file = ((UnpackedJarEntry)zipEntry).getFile();
113            } else {
114                file = getFile(zipEntry.getName());
115            }
116    
117            if (file == null) {
118                throw new IOException("Entry not found: name=" + file.getAbsolutePath());
119            } else if (file.isDirectory()) {
120                return new DeploymentUtil.EmptyInputStream();
121            }
122            return new FileInputStream(file);
123        }
124    
125        public String getName() {
126            return baseDir.getAbsolutePath();
127        }
128    
129        /**
130         * Always returns -1.
131         * @return -1
132         */
133        public int size() {
134            return -1;
135        }
136    
137        public void close() throws IOException {
138            try {
139                super.close();
140            } catch(IOException ignored) {
141            }
142        }
143    
144        protected void finalize() throws IOException {
145        }
146    
147        public File getFile(String name) {
148            File file = new File(baseDir, name);
149            if (!file.exists()) {
150                return null;
151            }
152            return file;
153        }
154    
155        private Manifest getManifestSafe() {
156            Manifest manifest = null;
157            try {
158                manifest = getManifest();
159            } catch (IOException e) {
160                // ignore
161            }
162            return manifest;
163        }
164    }