View Javadoc

1   /**
2    *
3    * Copyright 2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.deployment.util;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.net.URI;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Enumeration;
27  import java.util.Iterator;
28  import java.util.LinkedList;
29  import java.util.jar.JarEntry;
30  import java.util.jar.JarFile;
31  import java.util.jar.Manifest;
32  import java.util.zip.ZipEntry;
33  
34  /**
35   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
36   */
37  public class UnpackedJarFile extends JarFile {
38      private final File baseDir;
39      private boolean manifestLoaded = false;
40      private Manifest manifest;
41  
42      public UnpackedJarFile(File baseDir) throws IOException {
43          super(DeploymentUtil.DUMMY_JAR_FILE);
44          this.baseDir = baseDir;
45          if (!baseDir.isDirectory()) {
46              throw new IOException("File must be a directory: file=" + baseDir.getAbsolutePath());
47          }
48      }
49  
50      public File getBaseDir() {
51          return baseDir;
52      }
53  
54      public Manifest getManifest() throws IOException {
55          if (!manifestLoaded) {
56              File manifestFile = getFile("META-INF/MANIFEST.MF");
57  
58              if (manifestFile != null && manifestFile.isFile()) {
59                  FileInputStream in = null;
60                  try {
61                      in = new FileInputStream(manifestFile);
62                      manifest = new Manifest(in);
63                  } finally {
64                      if (in != null) {
65                          try {
66                              in.close();
67                          } catch (IOException e) {
68                              // ignore
69                          }
70                      }
71                  }
72              }
73              manifestLoaded = true;
74          }
75          return manifest;
76      }
77  
78      public UnpackedJarEntry getUnpackedJarEntry(String name) {
79          File file = getFile(name);
80          if (file == null) {
81              return null;
82          }
83          return new UnpackedJarEntry(name, file, getManifestSafe());
84      }
85  
86      public JarEntry getJarEntry(String name) {
87          return getUnpackedJarEntry(name);
88      }
89  
90      public ZipEntry getEntry(String name) {
91          return getUnpackedJarEntry(name);
92      }
93  
94      public Enumeration entries() {
95          Collection files = DeploymentUtil.listRecursiveFiles(baseDir);
96  
97          Manifest manifest = getManifestSafe();
98          LinkedList entries = new LinkedList();
99          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     }
139 
140     protected void finalize() throws IOException {
141     }
142 
143     public File getFile(String name) {
144         File file = new File(baseDir, name);
145         if (!file.exists()) {
146             return null;
147         }
148         return file;
149     }
150 
151     private Manifest getManifestSafe() {
152         Manifest manifest = null;
153         try {
154             manifest = getManifest();
155         } catch (IOException e) {
156             // ignore
157         }
158         return manifest;
159     }
160 }