1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/*****************************************************************************
* Java Plug-in Framework (JPF)
* Copyright (C) 2004-2007 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.standard;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
import org.java.plugin.PluginManager.PluginLocation;
import org.java.plugin.util.IoUtil;
/**
* A standard implementation of plug-in location interface. It may be used to
* create plug-in locations from JAR or ZIP files of plug-in folders, or from
* any URL pointers.
* <p>
* Inspired by Per Cederberg.
*
* @version $Id$
*/
public class StandardPluginLocation implements PluginLocation {
/**
* Creates plug-in location from a given file and checks that all required
* resources are available. Before creating location object, this method
* probes given ZIP file of folder for presence of any of the following
* files:
* <ul>
* <li>/plugin.xml</li>
* <li>/plugin-fragment.xml</li>
* <li>/META-INF/plugin.xml</li>
* <li>/META-INF/plugin-fragment.xml</li>
* </ul>
* If any of those files present, a new plug-in location object created and
* returned.
* @param file plug-in JAR or ZIP file or plug-in folder
* @return created new plug-in location or <code>null</code> if given file
* doesn't points to a valid plug-in file or folder
* @throws MalformedURLException if the plug-in URL's couldn't be created
*/
public static PluginLocation create(final File file)
throws MalformedURLException {
if (file.isDirectory()) {
URL manifestUrl = getManifestUrl(file);
return (manifestUrl == null) ? null
: new StandardPluginLocation(
IoUtil.file2url(file), manifestUrl);
}
String fileName = file.getName().toLowerCase(Locale.getDefault());
if (!fileName.endsWith(".jar") //$NON-NLS-1$
&& !fileName.endsWith(".zip")) { //$NON-NLS-1$
return null;
}
URL manifestUrl = getManifestUrl(file);
return (manifestUrl == null) ? null
: new StandardPluginLocation(new URL("jar:" //$NON-NLS-1$
+ IoUtil.file2url(file).toExternalForm()
+ "!/"), manifestUrl); //$NON-NLS-1$
}
private static URL getManifestUrl(final File file)
throws MalformedURLException {
if (file.isDirectory()) {
File result = new File(file, "plugin.xml"); //$NON-NLS-1$
if (result.isFile()) {
return IoUtil.file2url(result);
}
result = new File(file, "plugin-fragment.xml"); //$NON-NLS-1$
if (result.isFile()) {
return IoUtil.file2url(result);
}
result = new File(file, "META-INF" + File.separator //$NON-NLS-1$
+ "plugin.xml"); //$NON-NLS-1$
if (result.isFile()) {
return IoUtil.file2url(result);
}
result = new File(file, "META-INF" + File.separator //$NON-NLS-1$
+ "plugin-fragment.xml"); //$NON-NLS-1$
if (result.isFile()) {
return IoUtil.file2url(result);
}
return null;
}
if (!file.isFile()) {
return null;
}
URL url = new URL("jar:" //$NON-NLS-1$
+ IoUtil.file2url(file).toExternalForm()
+ "!/plugin.xml"); //$NON-NLS-1$
if (IoUtil.isResourceExists(url)) {
return url;
}
url = new URL("jar:" //$NON-NLS-1$
+ IoUtil.file2url(file).toExternalForm()
+ "!/plugin-fragment.xml"); //$NON-NLS-1$
if (IoUtil.isResourceExists(url)) {
return url;
}
url = new URL("jar:" //$NON-NLS-1$
+ IoUtil.file2url(file).toExternalForm()
+ "!/META-INF/plugin.xml"); //$NON-NLS-1$
if (IoUtil.isResourceExists(url)) {
return url;
}
url = new URL("jar:" //$NON-NLS-1$
+ IoUtil.file2url(file).toExternalForm()
+ "!/META-INF/plugin-fragment.xml"); //$NON-NLS-1$
if (IoUtil.isResourceExists(url)) {
return url;
}
return null;
}
private final URL context;
private final URL manifest;
/**
* Creates a new plug-in location from a given context an manifest URL's.
* @param aContext plug-in context URL
* @param aManifest plug-in manifest URL
*/
public StandardPluginLocation(final URL aContext, final URL aManifest) {
if (aContext == null) {
throw new NullPointerException("context"); //$NON-NLS-1$
}
if (aManifest == null) {
throw new NullPointerException("manifest"); //$NON-NLS-1$
}
context = aContext;
manifest = aManifest;
}
/**
* Creates a new plug-in location from a jar or a zip file or a folder. This
* plug-in manifest file path specified is relative to the root directory of
* the jar or zip file or given folder.
* @param file the plug-in zip file or plug-in folder
* @param manifestPath the relative manifest path
* @throws MalformedURLException if the plug-in URL's couldn't be created
*/
public StandardPluginLocation(final File file, final String manifestPath)
throws MalformedURLException {
if (file.isDirectory()) {
context = IoUtil.file2url(file);
} else {
context = new URL("jar:" //$NON-NLS-1$
+ IoUtil.file2url(file).toExternalForm() + "!/"); //$NON-NLS-1$
}
manifest = new URL(context, manifestPath.startsWith("/") //$NON-NLS-1$
? manifestPath.substring(1) : manifestPath);
}
/**
* @see org.java.plugin.PluginManager.PluginLocation#getManifestLocation()
*/
public URL getManifestLocation() {
return manifest;
}
/**
* @see org.java.plugin.PluginManager.PluginLocation#getContextLocation()
*/
public URL getContextLocation() {
return context;
}
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return context.toString();
}
}
|