KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > util > UnpackedJarFile


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.jar.JarEntry JavaDoc;
30 import java.util.jar.JarFile JavaDoc;
31 import java.util.jar.Manifest JavaDoc;
32 import java.util.zip.ZipEntry JavaDoc;
33
34 /**
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class UnpackedJarFile extends JarFile JavaDoc {
38     private final File JavaDoc baseDir;
39     private boolean manifestLoaded = false;
40     private Manifest JavaDoc manifest;
41
42     public UnpackedJarFile(File JavaDoc baseDir) throws IOException JavaDoc {
43         super(DeploymentUtil.DUMMY_JAR_FILE);
44         this.baseDir = baseDir;
45         if (!baseDir.isDirectory()) {
46             throw new IOException JavaDoc("File must be a directory: file=" + baseDir.getAbsolutePath());
47         }
48     }
49
50     public File JavaDoc getBaseDir() {
51         return baseDir;
52     }
53
54     public Manifest JavaDoc getManifest() throws IOException JavaDoc {
55         if (!manifestLoaded) {
56             File JavaDoc manifestFile = getFile("META-INF/MANIFEST.MF");
57
58             if (manifestFile != null && manifestFile.isFile()) {
59                 FileInputStream JavaDoc in = null;
60                 try {
61                     in = new FileInputStream JavaDoc(manifestFile);
62                     manifest = new Manifest JavaDoc(in);
63                 } finally {
64                     if (in != null) {
65                         try {
66                             in.close();
67                         } catch (IOException JavaDoc e) {
68                             // ignore
69
}
70                     }
71                 }
72             }
73             manifestLoaded = true;
74         }
75         return manifest;
76     }
77
78     public UnpackedJarEntry getUnpackedJarEntry(String JavaDoc name) {
79         File JavaDoc file = getFile(name);
80         if (file == null) {
81             return null;
82         }
83         return new UnpackedJarEntry(name, file, getManifestSafe());
84     }
85
86     public JarEntry JavaDoc getJarEntry(String JavaDoc name) {
87         return getUnpackedJarEntry(name);
88     }
89
90     public ZipEntry JavaDoc getEntry(String JavaDoc name) {
91         return getUnpackedJarEntry(name);
92     }
93
94     public Enumeration JavaDoc entries() {
95         Collection JavaDoc files = DeploymentUtil.listRecursiveFiles(baseDir);
96
97         Manifest JavaDoc manifest = getManifestSafe();
98         LinkedList JavaDoc entries = new LinkedList JavaDoc();
99         URI JavaDoc baseURI = baseDir.getAbsoluteFile().toURI();
100         for (Iterator JavaDoc iterator = files.iterator(); iterator.hasNext();) {
101             File JavaDoc entryFile = ((File JavaDoc) iterator.next()).getAbsoluteFile();
102             URI JavaDoc entryURI = entryFile.toURI();
103             URI JavaDoc relativeURI = baseURI.relativize(entryURI);
104             entries.add(new UnpackedJarEntry(relativeURI.getPath(), entryFile, manifest));
105         }
106         return Collections.enumeration(entries);
107     }
108
109     public InputStream JavaDoc getInputStream(ZipEntry JavaDoc zipEntry) throws IOException JavaDoc {
110         File JavaDoc 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 JavaDoc("Entry not found: name=" + file.getAbsolutePath());
119         } else if (file.isDirectory()) {
120             return new DeploymentUtil.EmptyInputStream();
121         }
122         return new FileInputStream JavaDoc(file);
123     }
124
125     public String JavaDoc 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 JavaDoc {
138     }
139
140     protected void finalize() throws IOException JavaDoc {
141     }
142
143     public File JavaDoc getFile(String JavaDoc name) {
144         File JavaDoc file = new File JavaDoc(baseDir, name);
145         if (!file.exists()) {
146             return null;
147         }
148         return file;
149     }
150
151     private Manifest JavaDoc getManifestSafe() {
152         Manifest JavaDoc manifest = null;
153         try {
154             manifest = getManifest();
155         } catch (IOException JavaDoc e) {
156             // ignore
157
}
158         return manifest;
159     }
160 }
161
Popular Tags