📄 pluginarchiver.java
字号:
/*****************************************************************************
* Java Plug-in Framework (JPF)
* Copyright (C) 2004-2006 Dmitry Olshansky
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*****************************************************************************/
package org.java.plugin.tools;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.java.plugin.ObjectFactory;
import org.java.plugin.PathResolver;
import org.java.plugin.registry.ManifestProcessingException;
import org.java.plugin.registry.PluginDescriptor;
import org.java.plugin.registry.PluginFragment;
import org.java.plugin.registry.PluginRegistry;
import org.java.plugin.registry.Version;
import org.java.plugin.util.IoUtil;
/**
* Plug-ins archive support class.
* @version $Id: PluginArchiver.java,v 1.1 2006/08/26 15:14:09 ddimon Exp $
*/
public final class PluginArchiver {
private static final String DESCRIPTOR_ENTRY_NAME = "JPF-DESCRIPTOR"; //$NON-NLS-1$
/**
* Packs given plug-in into single ZIP file. Resulting file may be used to
* run plug-ins from.
* @param descr plug-in descriptor
* @param pathResolver path resolver instance
* @param destFile target file
* @throws IOException if an I/O error has occurred
*/
public static void pack(final PluginDescriptor descr,
final PathResolver pathResolver, final File destFile)
throws IOException {
pack(pathResolver.resolvePath(descr, "/"), //$NON-NLS-1$
"JPF plug-in "+ descr.getId() //$NON-NLS-1$
+ " of version " + descr.getVersion(), destFile); //$NON-NLS-1$
}
/**
* Packs given plug-in fragment into single ZIP file. Resulting file may be
* used to run plug-ins from.
* @param fragment plug-in fragment descriptor
* @param pathResolver path resolver instance
* @param destFile target file
* @throws IOException if an I/O error has occurred
*/
public static void pack(final PluginFragment fragment,
final PathResolver pathResolver, final File destFile)
throws IOException {
pack(pathResolver.resolvePath(fragment, "/"), //$NON-NLS-1$
"JPF plug-in fragment "+ fragment.getId() //$NON-NLS-1$
+ " of version " + fragment.getVersion(), destFile); //$NON-NLS-1$
}
private static void pack(final URL url, final String comment,
final File destFile) throws IOException {
ZipOutputStream zipStrm = new ZipOutputStream(
new BufferedOutputStream(new FileOutputStream(
destFile, false)));
try {
zipStrm.setComment(comment);
File file = IoUtil.url2file(url);
if (file == null) {
throw new IOException("resolved URL " + url //$NON-NLS-1$
+ " is not local file system location pointer"); //$NON-NLS-1$
}
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
packEntry(zipStrm, null, files[i]);
}
} finally {
zipStrm.close();
}
}
/**
* Packs all plug-ins from given registry as one archive file.
* @param registry plug-ins registry
* @param pathResolver path resolver (only local file URLs are supported)
* @param destFile target archive file (will be overridden if any exists)
* @return collection of UID's of all packed plug-ins
* @throws IOException if an I/O error has occurred
*/
public static Collection pack(final PluginRegistry registry,
final PathResolver pathResolver, final File destFile)
throws IOException {
return pack(registry, pathResolver, destFile, new Filter() {
public boolean accept(final String id, final Version version,
final boolean isFragment) {
return true;
}
});
}
/**
* Packs plug-ins from given registry as one archive file according to
* given filter.
* @param registry plug-ins registry
* @param pathResolver path resolver (only local file URLs are supported)
* @param destFile target archive file (will be overridden if any exists)
* @param filter filter to be used when packing plug-ins
* @return collection of UID's of all packed plug-ins
* @throws IOException if an I/O error has occurred
*/
public static Collection pack(final PluginRegistry registry,
final PathResolver pathResolver, final File destFile,
final Filter filter) throws IOException {
Set result;
ZipOutputStream zipStrm = new ZipOutputStream(
new BufferedOutputStream(new FileOutputStream(
destFile, false)));
try {
zipStrm.setComment("JPF plug-ins archive"); //$NON-NLS-1$
ZipEntry entry = new ZipEntry(DESCRIPTOR_ENTRY_NAME);
entry.setComment("JPF plug-ins archive descriptor"); //$NON-NLS-1$
zipStrm.putNextEntry(entry);
result = writeDescripor(registry, filter,
new ObjectOutputStream(zipStrm));
zipStrm.closeEntry();
for (Iterator it = registry.getPluginDescriptors().iterator();
it.hasNext();) {
PluginDescriptor descr = (PluginDescriptor) it.next();
if (!result.contains(descr.getUniqueId())) {
continue;
}
URL url = pathResolver.resolvePath(descr, "/"); //$NON-NLS-1$
File file = IoUtil.url2file(url);
if (file == null) {
throw new IOException("resolved URL " + url //$NON-NLS-1$
+ " is not local file system location pointer"); //$NON-NLS-1$
}
entry = new ZipEntry(descr.getUniqueId() + "/"); //$NON-NLS-1$
entry.setComment("Content for JPF plug-in " //$NON-NLS-1$
+ descr.getId() + " version " + descr.getVersion()); //$NON-NLS-1$
entry.setTime(file.lastModified());
zipStrm.putNextEntry(entry);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
packEntry(zipStrm, entry, files[i]);
}
}
for (Iterator it = registry.getPluginFragments().iterator();
it.hasNext();) {
PluginFragment fragment = (PluginFragment) it.next();
if (!result.contains(fragment.getUniqueId())) {
continue;
}
URL url = pathResolver.resolvePath(fragment, "/"); //$NON-NLS-1$
File file = IoUtil.url2file(url);
if (file == null) {
throw new IOException("resolved URL " + url //$NON-NLS-1$
+ " is not local file system location pointer"); //$NON-NLS-1$
}
entry = new ZipEntry(fragment.getUniqueId() + "/"); //$NON-NLS-1$
entry.setComment("Content for JPF plug-in fragment " //$NON-NLS-1$
+ fragment.getId() + " version " //$NON-NLS-1$
+ fragment.getVersion());
entry.setTime(file.lastModified());
zipStrm.putNextEntry(entry);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
packEntry(zipStrm, entry, files[i]);
}
}
} finally {
zipStrm.close();
}
return result;
}
private static void packEntry(final ZipOutputStream zipStrm,
final ZipEntry parentEntry, final File file) throws IOException {
String parentEntryName = (parentEntry == null) ? "" //$NON-NLS-1$
: parentEntry.getName();
if (file.isFile()) {
ZipEntry entry = new ZipEntry(parentEntryName + file.getName());
entry.setTime(file.lastModified());
zipStrm.putNextEntry(entry);
BufferedInputStream fileStrm = new BufferedInputStream(
new FileInputStream(file));
try {
IoUtil.copyStream(fileStrm, zipStrm, 1024);
} finally {
fileStrm.close();
}
return;
}
ZipEntry entry = new ZipEntry(parentEntryName + file.getName()
+ "/"); //$NON-NLS-1$
entry.setTime(file.lastModified());
zipStrm.putNextEntry(entry);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
packEntry(zipStrm, entry, files[i]);
}
}
/**
* Extracts plug-ins from the given archive file.
* @param archiveFile plug-in archive file
* @param registry plug-in registry where to register manifests for
* unpacked plug-ins
* @param destFolder target folder
* @return collection of UID's of all un-packed (and registered) plug-ins
* @throws IOException if an I/O error has occurred
* @throws ClassNotFoundException if descriptor can't be read
* @throws ManifestProcessingException if manifest can't be registered
* (optional behavior)
*
* @see #unpack(URL, PluginRegistry, File, PluginArchiver.Filter)
*/
public static Collection unpack(final URL archiveFile,
final PluginRegistry registry, final File destFolder)
throws ManifestProcessingException, IOException,
ClassNotFoundException {
return unpack(archiveFile, registry, destFolder, new Filter() {
public boolean accept(final String id, final Version version,
final boolean isFragment) {
return true;
}
});
}
/**
* Extracts plug-ins from the given archive file.
* <br>
* <b>Note:</b>
* <br>
* In the current implementation all plug-in manifests are extracted to
* temporary local storage and deleted immediately after their registration
* with plug-in registry. So manifest URL's are actually point to "fake"
* locations.
* @param archiveFile plug-in archive file
* @param registry plug-in registry where to register manifests for
* unpacked plug-ins
* @param destFolder target folder
* @param filter filter to be used when un-packing plug-ins
* @return collection of UID's of all un-packed (and registered) plug-ins
* @throws ClassNotFoundException if plug-ins archive descriptor can't be
* de-serialized
* @throws ManifestProcessingException if plug-in manifests can't be
* registered
* @throws IOException if archive damaged or I/O error has occurred
*/
public static Collection unpack(final URL archiveFile,
final PluginRegistry registry, final File destFolder,
final Filter filter) throws IOException,
ManifestProcessingException, ClassNotFoundException {
Set result;
int count = 0;
ZipInputStream zipStrm = new ZipInputStream(new BufferedInputStream(
archiveFile.openStream()));
try {
ZipEntry entry = zipStrm.getNextEntry();
//NB: we are expecting that descriptor is in the first ZIP entry
if (entry == null) {
throw new IOException(
"invalid plug-ins archive, no entries found"); //$NON-NLS-1$
}
if (!DESCRIPTOR_ENTRY_NAME.equals(entry.getName())) {
throw new IOException("invalid plug-ins archive " + archiveFile //$NON-NLS-1$
+ ", entry " + DESCRIPTOR_ENTRY_NAME //$NON-NLS-1$
+ " not found as first ZIP entry in the archive file"); //$NON-NLS-1$
}
ObjectInputStream strm = new ObjectInputStream(zipStrm);
result = readDescriptor(strm, registry, destFolder, filter);
entry = zipStrm.getNextEntry();
while (entry != null) {
String name = entry.getName();
if (name.endsWith("/") //$NON-NLS-1$
&& (name.lastIndexOf('/', name.length() - 2) == -1)) {
String uid = name.substring(0, name.length() - 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -