KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > tools > PluginArchiver


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2006 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.tools;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.BufferedOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.zip.ZipEntry JavaDoc;
39 import java.util.zip.ZipInputStream JavaDoc;
40 import java.util.zip.ZipOutputStream JavaDoc;
41
42 import org.java.plugin.ObjectFactory;
43 import org.java.plugin.PathResolver;
44 import org.java.plugin.registry.ManifestProcessingException;
45 import org.java.plugin.registry.PluginDescriptor;
46 import org.java.plugin.registry.PluginFragment;
47 import org.java.plugin.registry.PluginRegistry;
48 import org.java.plugin.registry.Version;
49 import org.java.plugin.util.IoUtil;
50
51 /**
52  * Plug-ins archive support class.
53  * @version $Id: PluginArchiver.java,v 1.1 2006/08/26 15:14:09 ddimon Exp $
54  */

55 public final class PluginArchiver {
56     private static final String JavaDoc DESCRIPTOR_ENTRY_NAME = "JPF-DESCRIPTOR"; //$NON-NLS-1$
57

58     /**
59      * Packs given plug-in into single ZIP file. Resulting file may be used to
60      * run plug-ins from.
61      * @param descr plug-in descriptor
62      * @param pathResolver path resolver instance
63      * @param destFile target file
64      * @throws IOException if an I/O error has occurred
65      */

66     public static void pack(final PluginDescriptor descr,
67             final PathResolver pathResolver, final File JavaDoc destFile)
68             throws IOException JavaDoc {
69         pack(pathResolver.resolvePath(descr, "/"), //$NON-NLS-1$
70
"JPF plug-in "+ descr.getId() //$NON-NLS-1$
71
+ " of version " + descr.getVersion(), destFile); //$NON-NLS-1$
72
}
73     
74     /**
75      * Packs given plug-in fragment into single ZIP file. Resulting file may be
76      * used to run plug-ins from.
77      * @param fragment plug-in fragment descriptor
78      * @param pathResolver path resolver instance
79      * @param destFile target file
80      * @throws IOException if an I/O error has occurred
81      */

82     public static void pack(final PluginFragment fragment,
83             final PathResolver pathResolver, final File JavaDoc destFile)
84             throws IOException JavaDoc {
85         pack(pathResolver.resolvePath(fragment, "/"), //$NON-NLS-1$
86
"JPF plug-in fragment "+ fragment.getId() //$NON-NLS-1$
87
+ " of version " + fragment.getVersion(), destFile); //$NON-NLS-1$
88
}
89     
90     private static void pack(final URL JavaDoc url, final String JavaDoc comment,
91             final File JavaDoc destFile) throws IOException JavaDoc {
92         ZipOutputStream JavaDoc zipStrm = new ZipOutputStream JavaDoc(
93                 new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(
94                         destFile, false)));
95         try {
96             zipStrm.setComment(comment);
97             File JavaDoc file = IoUtil.url2file(url);
98             if (file == null) {
99                 throw new IOException JavaDoc("resolved URL " + url //$NON-NLS-1$
100
+ " is not local file system location pointer"); //$NON-NLS-1$
101
}
102             File JavaDoc[] files = file.listFiles();
103             for (int i = 0; i < files.length; i++) {
104                 packEntry(zipStrm, null, files[i]);
105             }
106         } finally {
107             zipStrm.close();
108         }
109     }
110     
111     /**
112      * Packs all plug-ins from given registry as one archive file.
113      * @param registry plug-ins registry
114      * @param pathResolver path resolver (only local file URLs are supported)
115      * @param destFile target archive file (will be overridden if any exists)
116      * @return collection of UID's of all packed plug-ins
117      * @throws IOException if an I/O error has occurred
118      */

119     public static Collection JavaDoc pack(final PluginRegistry registry,
120             final PathResolver pathResolver, final File JavaDoc destFile)
121             throws IOException JavaDoc {
122         return pack(registry, pathResolver, destFile, new Filter() {
123             public boolean accept(final String JavaDoc id, final Version version,
124                     final boolean isFragment) {
125                 return true;
126             }
127         });
128     }
129
130     /**
131      * Packs plug-ins from given registry as one archive file according to
132      * given filter.
133      * @param registry plug-ins registry
134      * @param pathResolver path resolver (only local file URLs are supported)
135      * @param destFile target archive file (will be overridden if any exists)
136      * @param filter filter to be used when packing plug-ins
137      * @return collection of UID's of all packed plug-ins
138      * @throws IOException if an I/O error has occurred
139      */

140     public static Collection JavaDoc pack(final PluginRegistry registry,
141             final PathResolver pathResolver, final File JavaDoc destFile,
142             final Filter filter) throws IOException JavaDoc {
143         Set JavaDoc result;
144         ZipOutputStream JavaDoc zipStrm = new ZipOutputStream JavaDoc(
145                 new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(
146                         destFile, false)));
147         try {
148             zipStrm.setComment("JPF plug-ins archive"); //$NON-NLS-1$
149
ZipEntry JavaDoc entry = new ZipEntry JavaDoc(DESCRIPTOR_ENTRY_NAME);
150             entry.setComment("JPF plug-ins archive descriptor"); //$NON-NLS-1$
151
zipStrm.putNextEntry(entry);
152             result = writeDescripor(registry, filter,
153                     new ObjectOutputStream JavaDoc(zipStrm));
154             zipStrm.closeEntry();
155             for (Iterator JavaDoc it = registry.getPluginDescriptors().iterator();
156                     it.hasNext();) {
157                 PluginDescriptor descr = (PluginDescriptor) it.next();
158                 if (!result.contains(descr.getUniqueId())) {
159                     continue;
160                 }
161                 URL JavaDoc url = pathResolver.resolvePath(descr, "/"); //$NON-NLS-1$
162
File JavaDoc file = IoUtil.url2file(url);
163                 if (file == null) {
164                     throw new IOException JavaDoc("resolved URL " + url //$NON-NLS-1$
165
+ " is not local file system location pointer"); //$NON-NLS-1$
166
}
167                 entry = new ZipEntry JavaDoc(descr.getUniqueId() + "/"); //$NON-NLS-1$
168
entry.setComment("Content for JPF plug-in " //$NON-NLS-1$
169
+ descr.getId() + " version " + descr.getVersion()); //$NON-NLS-1$
170
entry.setTime(file.lastModified());
171                 zipStrm.putNextEntry(entry);
172                 File JavaDoc[] files = file.listFiles();
173                 for (int i = 0; i < files.length; i++) {
174                     packEntry(zipStrm, entry, files[i]);
175                 }
176             }
177             for (Iterator JavaDoc it = registry.getPluginFragments().iterator();
178                     it.hasNext();) {
179                 PluginFragment fragment = (PluginFragment) it.next();
180                 if (!result.contains(fragment.getUniqueId())) {
181                     continue;
182                 }
183                 URL JavaDoc url = pathResolver.resolvePath(fragment, "/"); //$NON-NLS-1$
184
File JavaDoc file = IoUtil.url2file(url);
185                 if (file == null) {
186                     throw new IOException JavaDoc("resolved URL " + url //$NON-NLS-1$
187
+ " is not local file system location pointer"); //$NON-NLS-1$
188
}
189                 entry = new ZipEntry JavaDoc(fragment.getUniqueId() + "/"); //$NON-NLS-1$
190
entry.setComment("Content for JPF plug-in fragment " //$NON-NLS-1$
191
+ fragment.getId() + " version " //$NON-NLS-1$
192
+ fragment.getVersion());
193                 entry.setTime(file.lastModified());
194                 zipStrm.putNextEntry(entry);
195                 File JavaDoc[] files = file.listFiles();
196                 for (int i = 0; i < files.length; i++) {
197                     packEntry(zipStrm, entry, files[i]);
198                 }
199             }
200         } finally {
201             zipStrm.close();
202         }
203         return result;
204     }
205     
206     private static void packEntry(final ZipOutputStream JavaDoc zipStrm,
207             final ZipEntry JavaDoc parentEntry, final File JavaDoc file) throws IOException JavaDoc {
208         String JavaDoc parentEntryName = (parentEntry == null) ? "" //$NON-NLS-1$
209
: parentEntry.getName();
210         if (file.isFile()) {
211             ZipEntry JavaDoc entry = new ZipEntry JavaDoc(parentEntryName + file.getName());
212             entry.setTime(file.lastModified());
213             zipStrm.putNextEntry(entry);
214             BufferedInputStream JavaDoc fileStrm = new BufferedInputStream JavaDoc(
215                     new FileInputStream JavaDoc(file));
216             try {
217                 IoUtil.copyStream(fileStrm, zipStrm, 1024);
218             } finally {
219                 fileStrm.close();
220             }
221             return;
222         }
223         ZipEntry JavaDoc entry = new ZipEntry JavaDoc(parentEntryName + file.getName()
224                 + "/"); //$NON-NLS-1$
225
entry.setTime(file.lastModified());
226         zipStrm.putNextEntry(entry);
227         File JavaDoc[] files = file.listFiles();
228         for (int i = 0; i < files.length; i++) {
229             packEntry(zipStrm, entry, files[i]);
230         }
231     }
232
233     /**
234      * Extracts plug-ins from the given archive file.
235      * @param archiveFile plug-in archive file
236      * @param registry plug-in registry where to register manifests for
237      * unpacked plug-ins
238      * @param destFolder target folder
239      * @return collection of UID's of all un-packed (and registered) plug-ins
240      * @throws IOException if an I/O error has occurred
241      * @throws ClassNotFoundException if descriptor can't be read
242      * @throws ManifestProcessingException if manifest can't be registered
243      * (optional behavior)
244      *
245      * @see #unpack(URL, PluginRegistry, File, PluginArchiver.Filter)
246      */

247     public static Collection JavaDoc unpack(final URL JavaDoc archiveFile,
248             final PluginRegistry registry, final File JavaDoc destFolder)
249             throws ManifestProcessingException, IOException JavaDoc,
250             ClassNotFoundException JavaDoc {
251         return unpack(archiveFile, registry, destFolder, new Filter() {
252             public boolean accept(final String JavaDoc id, final Version version,
253                     final boolean isFragment) {
254                 return true;
255             }
256         });
257     }
258
259
260     /**
261      * Extracts plug-ins from the given archive file.
262      * <br>
263      * <b>Note:</b>
264      * <br>
265      * In the current implementation all plug-in manifests are extracted to
266      * temporary local storage and deleted immediately after their registration
267      * with plug-in registry. So manifest URL's are actually point to "fake"
268      * locations.
269      * @param archiveFile plug-in archive file
270      * @param registry plug-in registry where to register manifests for
271      * unpacked plug-ins
272      * @param destFolder target folder
273      * @param filter filter to be used when un-packing plug-ins
274      * @return collection of UID's of all un-packed (and registered) plug-ins
275      * @throws ClassNotFoundException if plug-ins archive descriptor can't be
276      * de-serialized
277      * @throws ManifestProcessingException if plug-in manifests can't be
278      * registered
279      * @throws IOException if archive damaged or I/O error has occurred
280      */

281     public static Collection JavaDoc unpack(final URL JavaDoc archiveFile,
282             final PluginRegistry registry, final File JavaDoc destFolder,
283             final Filter filter) throws IOException JavaDoc,
284             ManifestProcessingException, ClassNotFoundException JavaDoc {
285         Set JavaDoc result;
286         int count = 0;
287         ZipInputStream JavaDoc zipStrm = new ZipInputStream JavaDoc(new BufferedInputStream JavaDoc(
288                 archiveFile.openStream()));
289         try {
290             ZipEntry JavaDoc entry = zipStrm.getNextEntry();
291             //NB: we are expecting that descriptor is in the first ZIP entry
292
if (entry == null) {
293                 throw new IOException JavaDoc(
294                         "invalid plug-ins archive, no entries found"); //$NON-NLS-1$
295
}
296             if (!DESCRIPTOR_ENTRY_NAME.equals(entry.getName())) {
297                 throw new IOException JavaDoc("invalid plug-ins archive " + archiveFile //$NON-NLS-1$
298
+ ", entry " + DESCRIPTOR_ENTRY_NAME //$NON-NLS-1$
299
+ " not found as first ZIP entry in the archive file"); //$NON-NLS-1$
300
}
301             ObjectInputStream JavaDoc strm = new ObjectInputStream JavaDoc(zipStrm);
302             result = readDescriptor(strm, registry, destFolder, filter);
303             entry = zipStrm.getNextEntry();
304             while (entry != null) {
305                 String JavaDoc name = entry.getName();
306                 if (name.endsWith("/") //$NON-NLS-1$
307
&& (name.lastIndexOf('/', name.length() - 2) == -1)) {
308                     String JavaDoc uid = name.substring(0, name.length() - 1);
309                     if (!result.contains(uid)) {
310                         entry = zipStrm.getNextEntry();
311                         continue;
312                     }
313                     count++;
314                 } else {
315                     int p = name.indexOf('/');
316                     if ((p == -1) || (p == 0)
317                             || !result.contains(name.substring(0, p))) {
318                         entry = zipStrm.getNextEntry();
319                         continue;
320                     }
321                 }
322                 unpackEntry(zipStrm, entry, destFolder);
323                 entry = zipStrm.getNextEntry();
324             }
325         } finally {
326             zipStrm.close();
327         }
328         if (result.size() != count) {
329             throw new IOException JavaDoc("invalid plug-ins number (" + count //$NON-NLS-1$
330
+ ") found in the archive, expected number according to " //$NON-NLS-1$
331
+ "the archive descriptor is " + result.size()); //$NON-NLS-1$
332
}
333         return result;
334     }
335     
336     /**
337      * Extracts all plug-ins from the given archive file.
338      * <br>
339      * <b>Note:</b>
340      * <br>
341      * {@link ObjectFactory#createRegistry() Standard plug-in registry}
342      * implementation will be used internally to read plug-in manifests.
343      * @param archiveFile plug-in archive file
344      * @param destFolder target folder
345      * @return collection of UID's of all un-packed plug-ins
346      * @throws IOException if an I/O error has occurred
347      * @throws ClassNotFoundException if descriptor can't be read
348      * @throws ManifestProcessingException if manifest can't be registered
349      * (optional behavior)
350      *
351      * @see ObjectFactory#createRegistry()
352      */

353     public static Collection JavaDoc unpack(final URL JavaDoc archiveFile,
354             final File JavaDoc destFolder) throws ManifestProcessingException,
355             IOException JavaDoc, ClassNotFoundException JavaDoc {
356         return unpack(archiveFile, ObjectFactory.newInstance().createRegistry(),
357                 destFolder);
358     }
359     
360     /**
361      * Extracts plug-ins from the given archive file according to given filter.
362      * <br>
363      * <b>Note:</b>
364      * <br>
365      * {@link ObjectFactory#createRegistry() Standard plug-in registry}
366      * implementation will be used internally to read plug-in manifests.
367      * @param archiveFile plug-in archive file
368      * @param destFolder target folder
369      * @param filter filter to be used when un-packing plug-ins
370      * @return collection of UID's of all un-packed plug-ins
371      * @throws IOException if an I/O error has occurred
372      * @throws ClassNotFoundException if descriptor can't be read
373      * @throws ManifestProcessingException if manifest can't be registered
374      * (optional behavior)
375      */

376     public static Collection JavaDoc unpack(final URL JavaDoc archiveFile,
377             final File JavaDoc destFolder, final Filter filter)
378             throws ManifestProcessingException, IOException JavaDoc,
379             ClassNotFoundException JavaDoc {
380         return unpack(archiveFile, ObjectFactory.newInstance().createRegistry(),
381                 destFolder, filter);
382     }
383     
384     private static void unpackEntry(final ZipInputStream JavaDoc zipStrm,
385             final ZipEntry JavaDoc entry, final File JavaDoc destFolder) throws IOException JavaDoc {
386         String JavaDoc name = entry.getName();
387         if (name.endsWith("/")) { //$NON-NLS-1$
388
File JavaDoc folder = new File JavaDoc(destFolder.getCanonicalPath() + "/" + name); //$NON-NLS-1$
389
if (!folder.exists() && !folder.mkdirs()) {
390                 throw new IOException JavaDoc("can't create folder " + folder); //$NON-NLS-1$
391
}
392             folder.setLastModified(entry.getTime());
393             return;
394         }
395         File JavaDoc file = new File JavaDoc(destFolder.getCanonicalPath() + "/" + name); //$NON-NLS-1$
396
File JavaDoc folder = file.getParentFile();
397         if (!folder.exists() && !folder.mkdirs()) {
398             throw new IOException JavaDoc("can't create folder " + folder); //$NON-NLS-1$
399
}
400         OutputStream JavaDoc strm = new BufferedOutputStream JavaDoc(
401                 new FileOutputStream JavaDoc(file, false));
402         try {
403             IoUtil.copyStream(zipStrm, strm, 1024);
404         } finally {
405             strm.close();
406         }
407         file.setLastModified(entry.getTime());
408     }
409
410     /**
411      * Reads meta-information from plug-ins archive file and registers found
412      * plug-in manifest data with given registry for future analysis.
413      * @param archiveFile plug-in archive file
414      * @param registry plug-in registry where to register discovered manifests
415      * for archived plug-ins
416      * @return collection of UID's of all registered plug-ins
417      * @throws IOException if an I/O error has occurred
418      * @throws ClassNotFoundException if descriptor can't be read
419      * @throws ManifestProcessingException if manifest can't be registered
420      * (optional behavior)
421      *
422      * @see #readDescriptor(URL, PluginRegistry, PluginArchiver.Filter)
423      */

424     public static Collection JavaDoc readDescriptor(final URL JavaDoc archiveFile,
425             final PluginRegistry registry)
426             throws IOException JavaDoc, ClassNotFoundException JavaDoc,
427             ManifestProcessingException {
428         return readDescriptor(archiveFile, registry, new Filter() {
429             public boolean accept(final String JavaDoc id, final Version version,
430                     final boolean isFragment) {
431                 return true;
432             }
433         });
434     }
435
436     /**
437      * Reads meta-information from plug-ins archive file and registers found
438      * plug-in manifest data with given registry for future analysis.
439      * <br>
440      * <b>Note:</b>
441      * <br>
442      * In the current implementation all plug-in manifests are extracted to
443      * temporary local storage and deleted immediately after their registration
444      * with plug-in registry. So manifest URL's are actually point to "fake"
445      * locations and main purpose of this method is to allow you to analyze
446      * plug-ins archive without needing to download and unpack it.
447      * @param archiveFile plug-in archive file
448      * @param registry plug-in registry where to register discovered manifests
449      * for archived plug-ins
450      * @param filter filter to be used when un-packing plug-ins
451      * @return collection of UID's of all registered plug-ins
452      * @throws IOException if an I/O error has occurred
453      * @throws ClassNotFoundException if descriptor can't be read
454      * @throws ManifestProcessingException if manifest can't be registered
455      * (optional behavior)
456      */

457     public static Collection JavaDoc readDescriptor(final URL JavaDoc archiveFile,
458             final PluginRegistry registry, final Filter filter)
459             throws IOException JavaDoc, ClassNotFoundException JavaDoc,
460             ManifestProcessingException {
461         ZipInputStream JavaDoc zipStrm = new ZipInputStream JavaDoc(new BufferedInputStream JavaDoc(
462                 archiveFile.openStream()));
463         try {
464             ZipEntry JavaDoc entry = zipStrm.getNextEntry();
465             //NB: we are expecting that descriptor is in the first ZIP entry
466
if (entry == null) {
467                 throw new IOException JavaDoc(
468                         "invalid plug-ins archive, no entries found"); //$NON-NLS-1$
469
}
470             if (!DESCRIPTOR_ENTRY_NAME.equals(entry.getName())) {
471                 throw new IOException JavaDoc("invalid plug-ins archive " + archiveFile //$NON-NLS-1$
472
+ ", entry " + DESCRIPTOR_ENTRY_NAME //$NON-NLS-1$
473
+ " not found as first ZIP entry in the archive file"); //$NON-NLS-1$
474
}
475             ObjectInputStream JavaDoc strm = new ObjectInputStream JavaDoc(zipStrm);
476             return readDescriptor(strm, registry, Util.getTempFolder(), filter);
477         } finally {
478             zipStrm.close();
479         }
480     }
481     
482     private static Set JavaDoc writeDescripor(final PluginRegistry registry,
483             final Filter filter, final ObjectOutputStream JavaDoc strm)
484             throws IOException JavaDoc {
485         Map JavaDoc result = new HashMap JavaDoc();
486         for (Iterator JavaDoc it = registry.getPluginDescriptors().iterator();
487                 it.hasNext();) {
488             PluginDescriptor descr = (PluginDescriptor) it.next();
489             if (!filter.accept(descr.getId(), descr.getVersion(), false)) {
490                 continue;
491             }
492             result.put(descr.getUniqueId(),
493                     new ArchiveDescriptorEntry(descr.getId(),
494                             descr.getVersion(), false,
495                             Util.readUrlContent(descr.getLocation())));
496         }
497         for (Iterator JavaDoc it = registry.getPluginFragments().iterator();
498                 it.hasNext();) {
499             PluginFragment fragment = (PluginFragment) it.next();
500             if (!filter.accept(fragment.getId(), fragment.getVersion(), true)) {
501                 continue;
502             }
503             result.put(fragment.getUniqueId(),
504                     new ArchiveDescriptorEntry(fragment.getId(),
505                             fragment.getVersion(), true,
506                             Util.readUrlContent(fragment.getLocation())));
507         }
508         strm.writeObject(result.values().toArray(
509                 new ArchiveDescriptorEntry[result.size()]));
510         return result.keySet();
511     }
512     
513     private static Set JavaDoc readDescriptor(final ObjectInputStream JavaDoc strm,
514             final PluginRegistry registry, final File JavaDoc tempFolder,
515             final Filter filter) throws IOException JavaDoc, ClassNotFoundException JavaDoc,
516             ManifestProcessingException {
517         ArchiveDescriptorEntry[] data =
518             (ArchiveDescriptorEntry[]) strm.readObject();
519         // For simplicity we'll store manifests to a temporary files rather than
520
// create special URL's and provide special URL handler for them.
521
// More powerful approach will be possibly implemented in the future.
522
Set JavaDoc urls = new HashSet JavaDoc();
523         Set JavaDoc files = new HashSet JavaDoc();
524         for (int i = 0; i < data.length; i++) {
525             if (!filter.accept(data[i].getId(), data[i].getVersion(),
526                     data[i].isFragment())) {
527                 continue;
528             }
529             File JavaDoc file = File.createTempFile("manifest.", null, tempFolder); //$NON-NLS-1$
530
file.deleteOnExit();
531             OutputStream JavaDoc fileStrm = new BufferedOutputStream JavaDoc(
532                     new FileOutputStream JavaDoc(file, false));
533             try {
534                 fileStrm.write(data[i].getData());
535             } finally {
536                 fileStrm.close();
537             }
538             files.add(file);
539             urls.add(IoUtil.file2url(file));
540         }
541         Set JavaDoc result = new HashSet JavaDoc();
542         try {
543             for (Iterator JavaDoc it = registry.register((URL JavaDoc[]) urls.toArray(
544                     new URL JavaDoc[urls.size()])).values().iterator(); it.hasNext();) {
545                 Object JavaDoc obj = it.next();
546                 if (obj instanceof PluginDescriptor) {
547                     result.add(((PluginDescriptor) obj).getUniqueId());
548                 } else if (obj instanceof PluginFragment) {
549                     result.add(((PluginFragment) obj).getUniqueId());
550                 } else {
551                     //NB: ignore all other elements
552
}
553             }
554         } finally {
555             for (Iterator JavaDoc it = files.iterator(); it.hasNext();) {
556                 ((File JavaDoc) it.next()).delete();
557             }
558         }
559         return result;
560     }
561     
562     private PluginArchiver() {
563         // no-op
564
}
565     
566     /**
567      * Callback interface to filter plug-ins being processed.
568      * @version $Id: PluginArchiver.java,v 1.1 2006/08/26 15:14:09 ddimon Exp $
569      */

570     public static interface Filter {
571         /**
572          * @param id plug-in or plug-in fragment identifier
573          * @param version plug-in or plug-in fragment version
574          * @param isFragment <code>true</code> if given identity data
575          * corresponds to plug-in fragment
576          * @return <code>true</code> if plug-in or plug-in fragment with given
577          * identity should be taken into account
578          */

579         boolean accept(String JavaDoc id, Version version, boolean isFragment);
580     }
581     
582     private static class ArchiveDescriptorEntry implements Serializable JavaDoc {
583         private static final long serialVersionUID = 8749937247555974932L;
584         
585         private final String JavaDoc id;
586         private final Version version;
587         private final boolean isFragment;
588         private final byte[] data;
589         
590         protected ArchiveDescriptorEntry(final String JavaDoc anId,
591                 final Version aVersion, final boolean fragment,
592                 final byte[] aData) {
593             id = anId;
594             version = aVersion;
595             isFragment = fragment;
596             data = aData;
597         }
598
599         protected String JavaDoc getId() {
600             return id;
601         }
602
603         protected Version getVersion() {
604             return version;
605         }
606         
607         protected boolean isFragment() {
608             return isFragment;
609         }
610
611         protected byte[] getData() {
612             return data;
613         }
614     }
615 }
616
Popular Tags