KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > adaptor > core > BundleEntry


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.osgi.framework.adaptor.core;
13
14 import java.io.*;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.zip.ZipEntry JavaDoc;
18
19 /**
20  * A BundleEntry represents one entry of a BundleFile.
21  * <p>
22  * Clients may extend this class.
23  * </p>
24  * @since 3.1
25  */

26 public abstract class BundleEntry {
27     /**
28      * Return an InputStream for the entry.
29      *
30      * @return InputStream for the entry.
31      * @throws java.io.IOException If an error occurs reading the bundle.
32      */

33     public abstract InputStream getInputStream() throws IOException;
34
35     /**
36      * Return the size of the entry (uncompressed).
37      *
38      * @return size of entry.
39      */

40     public abstract long getSize();
41
42     /**
43      * Return the name of the entry.
44      *
45      * @return name of entry.
46      */

47     public abstract String JavaDoc getName();
48
49     /**
50      * Get the modification time for this BundleEntry.
51      * <p>If the modification time has not been set,
52      * this method will return <tt>-1</tt>.
53      *
54      * @return last modification time.
55      */

56     public abstract long getTime();
57
58     /**
59      * Get a URL to the bundle entry that uses a common protocol (i.e. file:
60      * jar: or http: etc.).
61      * @return a URL to the bundle entry that uses a common protocol
62      */

63     public abstract URL JavaDoc getLocalURL();
64
65     /**
66      * Get a URL to the content of the bundle entry that uses the file: protocol.
67      * The content of the bundle entry may be downloaded or extracted to the local
68      * file system in order to create a file: URL.
69      * @return a URL to the content of the bundle entry that uses the file: protocol
70      */

71     public abstract URL JavaDoc getFileURL();
72
73     /**
74      * Return the name of this BundleEntry by calling getName().
75      *
76      * @return String representation of this BundleEntry.
77      */

78     public String JavaDoc toString() {
79         return (getName());
80     }
81
82     /**
83      * A BundleEntry represented by a ZipEntry in a ZipFile. The ZipBundleEntry
84      * class is used for bundles that are installed as a ZipFile on a file system.
85      */

86     public static class ZipBundleEntry extends BundleEntry {
87         /**
88          * ZipEntry for this entry.
89          */

90         protected ZipEntry JavaDoc zipEntry;
91
92         /**
93          * The BundleFile for this entry.
94          */

95         protected BundleFile bundleFile;
96
97         /**
98          * Constructs the BundleEntry using a ZipEntry.
99          * @param bundleFile BundleFile object this entry is a member of
100          * @param entry ZipEntry object of this entry
101          */

102         protected ZipBundleEntry(ZipEntry JavaDoc entry, BundleFile bundleFile) {
103             this.zipEntry = entry;
104             this.bundleFile = bundleFile;
105         }
106
107         /**
108          * Return an InputStream for the entry.
109          *
110          * @return InputStream for the entry
111          * @exception java.io.IOException
112          */

113         public InputStream getInputStream() throws IOException {
114             return ((BundleFile.ZipBundleFile) bundleFile).getZipFile().getInputStream(zipEntry);
115         }
116
117         /**
118          * Return size of the uncompressed entry.
119          *
120          * @return size of entry
121          */

122         public long getSize() {
123             return zipEntry.getSize();
124         }
125
126         /**
127          * Return name of the entry.
128          *
129          * @return name of entry
130          */

131         public String JavaDoc getName() {
132             return zipEntry.getName();
133         }
134
135         /**
136          * Get the modification time for this BundleEntry.
137          * <p>If the modification time has not been set,
138          * this method will return <tt>-1</tt>.
139          *
140          * @return last modification time.
141          */

142         public long getTime() {
143             return zipEntry.getTime();
144         }
145
146         public URL JavaDoc getLocalURL() {
147             try {
148                 return new URL JavaDoc("jar:file:" + bundleFile.basefile.getAbsolutePath() + "!/" + zipEntry.getName()); //$NON-NLS-1$//$NON-NLS-2$
149
} catch (MalformedURLException JavaDoc e) {
150                 //This can not happen.
151
return null;
152             }
153         }
154
155         public URL JavaDoc getFileURL() {
156             try {
157                 File file = bundleFile.getFile(zipEntry.getName());
158                 if (file != null)
159                     return file.toURL();
160             } catch (MalformedURLException JavaDoc e) {
161                 //This can not happen.
162
}
163             return null;
164         }
165     }
166
167     /**
168      * A BundleEntry represented by a File object. The FileBundleEntry class is
169      * used for bundles that are installed as extracted zips on a file system.
170      */

171     public static class FileBundleEntry extends BundleEntry {
172         /**
173          * File for this entry.
174          */

175         private File file;
176         /**
177          * The name for this entry
178          */

179         private String JavaDoc name;
180
181         /**
182          * Constructs the BundleEntry using a File.
183          * @param file BundleFile object this entry is a member of
184          * @param name the name of this BundleEntry
185          */

186         FileBundleEntry(File file, String JavaDoc name) {
187             this.file = file;
188             this.name = name;
189         }
190
191         /**
192          * Return an InputStream for the entry.
193          *
194          * @return InputStream for the entry
195          * @exception java.io.IOException
196          */

197         public InputStream getInputStream() throws IOException {
198             return BundleFile.secureAction.getFileInputStream(file);
199         }
200
201         /**
202          * Return size of the uncompressed entry.
203          *
204          * @return size of entry
205          */

206         public long getSize() {
207             return BundleFile.secureAction.length(file);
208         }
209
210         /**
211          * Return name of the entry.
212          *
213          * @return name of entry
214          */

215         public String JavaDoc getName() {
216             return (name);
217         }
218
219         /**
220          * Get the modification time for this BundleEntry.
221          * <p>If the modification time has not been set,
222          * this method will return <tt>-1</tt>.
223          *
224          * @return last modification time.
225          */

226         public long getTime() {
227             return BundleFile.secureAction.lastModified(file);
228         }
229
230         public URL JavaDoc getLocalURL() {
231             return getFileURL();
232         }
233
234         public URL JavaDoc getFileURL() {
235             try {
236                 return file.toURL();
237             } catch (MalformedURLException JavaDoc e) {
238                 return null;
239             }
240         }
241     }
242
243     /**
244      * Represents a directory entry in a ZipBundleFile. This object is used to
245      * reference a directory entry in a ZipBundleFile when the directory entries are
246      * not included in the zip file.
247      */

248     public static class DirZipBundleEntry extends BundleEntry {
249
250         /**
251          * ZipBundleFile for this entry.
252          */

253         private BundleFile.ZipBundleFile bundleFile;
254         /**
255          * The name for this entry
256          */

257         private String JavaDoc name;
258
259         public DirZipBundleEntry(BundleFile.ZipBundleFile bundleFile, String JavaDoc name) {
260             this.name = (name.length() > 0 && name.charAt(0) == '/') ? name.substring(1) : name;
261             this.bundleFile = bundleFile;
262         }
263
264         public InputStream getInputStream() throws IOException {
265             return null;
266         }
267
268         public long getSize() {
269             return 0;
270         }
271
272         public String JavaDoc getName() {
273             return name;
274         }
275
276         public long getTime() {
277             return 0;
278         }
279
280         public URL JavaDoc getLocalURL() {
281             try {
282                 return new URL JavaDoc("jar:file:" + bundleFile.basefile.getAbsolutePath() + "!/" + name); //$NON-NLS-1$ //$NON-NLS-2$
283
} catch (MalformedURLException JavaDoc e) {
284                 //This can not happen, unless the jar protocol is not supported.
285
return null;
286             }
287         }
288
289         public URL JavaDoc getFileURL() {
290             try {
291                 return bundleFile.extractDirectory(name).toURL();
292             } catch (MalformedURLException JavaDoc e) {
293                 // this cannot happen.
294
return null;
295             }
296         }
297     }
298 }
299
Popular Tags