KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > core > JarContentReference


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.update.core;
12
13 import java.io.*;
14 import java.net.*;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.jar.*;
19
20 import org.eclipse.update.core.model.*;
21 import org.eclipse.update.internal.core.*;
22
23 /**
24  * Local .jar file content reference.
25  * <p>
26  * This class may be instantiated or subclassed by clients.
27  * </p>
28  * <p>
29  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
30  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
31  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
32  * (repeatedly) as the API evolves.
33  * </p>
34  * @see org.eclipse.update.core.ContentReference
35  * @see org.eclipse.update.core.JarEntryContentReference
36  * @since 2.0
37  */

38 public class JarContentReference extends ContentReference {
39
40     //private static ArrayList referenceList = new ArrayList();
41
private JarFile jarFile;
42
43     /**
44      * Content selector used in .jar operations.
45      * Default implementation causes all file entries to be selected with
46      * generated identifiers being the same as the original .jar entry name.
47      *
48      * @since 2.0
49      */

50     public static class ContentSelector {
51
52         /**
53          * Indicates whether the .jar entry should be selected.
54          * Default behavior is to select all non-directory entries.
55          *
56          * @param entry .jar entry
57          * @return <code>true</code> if entry is to be selected,
58          * <code>false</code> otherwise
59          * @since 2.0
60          */

61         public boolean include(JarEntry entry) {
62             return entry == null ? false : !entry.isDirectory();
63         }
64
65         /**
66          * Defines the "symbolic" path identifier for the
67          * entry. Default identifier is the same as the jar entry name.
68          *
69          * @param entry .jar entry
70          * @return "symbolic" path identifier
71          * @since 2.0
72          */

73         public String JavaDoc defineIdentifier(JarEntry entry) {
74             return entry == null ? null : entry.getName();
75         }
76     }
77
78     /**
79      * Create jar content reference from URL.
80      *
81      * @param id "symbolic" path identifier
82      * @param url actual referenced URL
83      * @since 2.0
84      */

85     public JarContentReference(String JavaDoc id, URL url) {
86         super(id, url);
87         this.jarFile = null;
88         //referenceList.add(this); // keep track of archives
89
}
90
91     /**
92      * Create jar content reference from file.
93      *
94      * @param id "symbolic" path identifier
95      * @param file actual referenced file
96      * @since 2.0
97      */

98     public JarContentReference(String JavaDoc id, File file) {
99         super(id, file);
100         this.jarFile = null;
101         //referenceList.add(this); // keep track of archives
102
}
103
104     /**
105      * A factory method to create a jar content reference.
106      *
107      * @param id "symbolic" path identifier
108      * @param file actual referenced file
109      * @return jar content reference
110      * @since 2.0
111      */

112     public ContentReference createContentReference(String JavaDoc id, File file) {
113         return new JarContentReference(id, file,true);
114     }
115     /**
116      * Constructor JarContentReference.
117      * @param id
118      * @param file
119      * @param b
120      */

121     public JarContentReference(String JavaDoc id, File file, boolean b) {
122         this(id,file);
123         setTempLocal(b);
124     }
125
126     /**
127      * Returns the content reference as a jar file. Note, that this method
128      * <b>does not</b> cause the file to be downloaded if it
129      * is not already local.
130      *
131      * @return reference as jar file
132      * @exception IOException reference cannot be returned as jar file
133      * @since 2.0
134      */

135     protected JarFile asJarFile() throws IOException {
136         if (this.jarFile == null) {
137             File file = asFile();
138             if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_INSTALL)
139                 UpdateCore.debug("asJarFile :" + file); //$NON-NLS-1$
140
if (file != null && !file.exists()) {
141                 UpdateCore.warn("JarFile does not exits:" + file); //$NON-NLS-1$
142
throw new FileNotFoundException(file.getAbsolutePath());
143             }
144             this.jarFile = new JarFile(file);
145         }
146         return jarFile;
147     }
148
149     /**
150      * Unpacks the referenced jar archive into the specified location.
151      * Returns content references to the unpacked files.
152      *
153      * @param dir location to unpack the jar into
154      * @param selector selector, used to select entries to unpack, and to define
155      * "symbolic" path identifiers for the entries.
156      * @param monitor progress monitor
157      * @exception IOException
158      * @exception InstallAbortedException
159      * @since 2.0
160      */

161     public ContentReference[] unpack(File dir, ContentSelector selector, InstallMonitor monitor) throws IOException, InstallAbortedException {
162
163         // make sure we have a selector
164
if (selector == null)
165             selector = new ContentSelector();
166
167         // get archive content
168
JarFile jarArchive = this.asJarFile();
169         List JavaDoc content = new ArrayList JavaDoc();
170         Enumeration JavaDoc entries = jarArchive.entries();
171
172         // run through the entries and unjar
173
String JavaDoc entryId;
174         JarEntry entry;
175         InputStream is;
176         OutputStream os;
177         File localFile;
178         try {
179             if (monitor != null) {
180                 monitor.saveState();
181                 monitor.setTaskName(Messages.JarContentReference_Unpacking);
182                 monitor.subTask(this.getIdentifier());
183                 monitor.showCopyDetails(false);
184             }
185             while (entries.hasMoreElements()) {
186                 entry = (JarEntry) entries.nextElement();
187                 if (entry != null && selector.include(entry)) {
188                     is = null;
189                     os = null;
190                     entryId = selector.defineIdentifier(entry);
191                     localFile = Utilities.createLocalFile(dir, entryId); // create temp file
192
if (!entry.isDirectory()) {
193                         try {
194                             is = jarArchive.getInputStream(entry);
195                             os = new FileOutputStream(localFile);
196                             Utilities.copy(is, os, monitor);
197                         } finally {
198                             if (is != null)
199                                 try {
200                                     is.close();
201                                 } catch (IOException e) {
202                                 }
203                             if (os != null)
204                                 try {
205                                     os.close();
206                                 } catch (IOException e) {
207                                 }
208                         }
209                         content.add(new ContentReference(entryId, localFile));
210                     }
211                 }
212             }
213         } finally {
214             if (monitor != null)
215                 monitor.restoreState();
216         }
217         return (ContentReference[]) content.toArray(new ContentReference[0]);
218     }
219
220     /**
221      * Unpacks the named jar entry into the specified location.
222      * Returns content reference to the unpacked file.
223      *
224      * @param dir location to unpack the jar into
225      * @param entryName name of the jar entry
226      * @param selector selector, used to define "symbolic" path identifier
227      * for the entry
228      * @param monitor progress monitor
229      * @exception IOException
230      * @exception InstallAbortedException
231      * @since 2.0
232      */

233     public ContentReference unpack(File dir, String JavaDoc entryName, ContentSelector selector, InstallMonitor monitor) throws IOException, InstallAbortedException {
234
235         // make sure we have a selector
236
if (selector == null)
237             selector = new ContentSelector();
238
239         // unjar the entry
240
JarFile jarArchive = this.asJarFile();
241         entryName = entryName.replace(File.separatorChar, '/');
242         JarEntry entry = jarArchive.getJarEntry(entryName);
243         String JavaDoc entryId;
244         if (entry != null) {
245             InputStream is = null;
246             OutputStream os = null;
247             entryId = selector.defineIdentifier(entry);
248             File localFile = Utilities.createLocalFile(dir, entryId); // create temp file
249
if (!entry.isDirectory()) {
250                 try {
251                     is = jarArchive.getInputStream(entry);
252                     os = new FileOutputStream(localFile);
253                     Utilities.copy(is, os, monitor);
254                 } finally {
255                     if (is != null)
256                         try {
257                             is.close();
258                         } catch (IOException e) {
259                         }
260                     if (os != null)
261                         try {
262                             os.close();
263                         } catch (IOException e) {
264                         }
265                 }
266                 return new ContentReference(entryId, localFile);
267             } else
268                 return null; // entry was a directory
269
} else
270             throw new FileNotFoundException(this.asFile().getAbsolutePath() + " " + entryName); //$NON-NLS-1$
271
}
272
273     /**
274      * Peeks into the referenced jar archive.
275      * Returns content references to the jar entries within the jar file.
276      *
277      * @param selector selector, used to select entries to return, and to define
278      * "symbolic" path identifiers for the entries.
279      * @param monitor progress monitor
280      * @exception IOException
281      * @since 2.0
282      */

283     public ContentReference[] peek(ContentSelector selector, InstallMonitor monitor) throws IOException {
284
285         // make sure we have a selector
286
if (selector == null)
287             selector = new ContentSelector();
288
289         // get archive content
290
JarFile jarArchive = this.asJarFile();
291         List JavaDoc content = new ArrayList JavaDoc();
292         Enumeration JavaDoc entries = jarArchive.entries();
293
294         // run through the entries and create content references
295
JarEntry entry;
296         String JavaDoc entryId;
297         while (entries.hasMoreElements()) {
298             entry = (JarEntry) entries.nextElement();
299             if (selector.include(entry)) {
300                 entryId = selector.defineIdentifier(entry);
301                 content.add(new JarEntryContentReference(entryId, this, entry));
302             }
303         }
304         return (ContentReference[]) content.toArray(new ContentReference[0]);
305     }
306
307     /**
308      * Peeks into the referenced jar archive looking for the named entry.
309      * Returns content reference to the jar entry within the jar file.
310      *
311      * @param entryName name of the jar entry
312      * @param selector selector, used to define "symbolic" path identifier
313      * for the entry
314      * @param monitor progress monitor
315      * @return the content reference ofr <code>null</null> if the entry doesn't exist
316      * @exception IOException
317      * @since 2.0
318      */

319     public ContentReference peek(String JavaDoc entryName, ContentSelector selector, InstallMonitor monitor) throws IOException {
320
321         // make sure we have a selector
322
if (selector == null)
323             selector = new ContentSelector();
324
325         // assume we have a reference that represents a jar archive.
326
JarFile jarArchive = this.asJarFile();
327         entryName = entryName.replace(File.separatorChar, '/');
328         JarEntry entry = jarArchive.getJarEntry(entryName);
329         if (entry == null)
330             return null;
331
332         String JavaDoc entryId = selector.defineIdentifier(entry);
333         return new JarEntryContentReference(entryId, this, entry);
334     }
335
336     /**
337      * Closes the jar archive corresponding to this reference.
338      *
339      * @exception IOException
340      * @since 2.0
341      */

342     public void closeArchive() throws IOException {
343         if (this.jarFile != null) {
344             this.jarFile.close();
345             this.jarFile = null;
346         }
347     }
348
349     /**
350      * Perform shutdown processing for jar archive handling.
351      * This method is called when platform is shutting down.
352      * It is not intended to be called at any other time under
353      * normal circumstances. A side-effect of calling this method
354      * is that all jars referenced by JarContentReferences are closed.
355      *
356      * @since 2.0
357      */

358     public static void shutdown() {
359         /*for (int i = 0; i < referenceList.size(); i++) {
360             JarContentReference ref = (JarContentReference) referenceList.get(i);
361             try {
362                 ref.closeArchive(); // ensure we are not leaving open jars
363             } catch (IOException e) {
364                 // we tried, nothing we can do ...
365             }
366         }*/

367     }
368 }
369
Popular Tags