001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2004-2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.standard;
019:
020: import java.io.File;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.util.Locale;
024:
025: import org.java.plugin.PluginManager.PluginLocation;
026: import org.java.plugin.util.IoUtil;
027:
028: /**
029: * A standard implementation of plug-in location interface. It may be used to
030: * create plug-in locations from JAR or ZIP files of plug-in folders, or from
031: * any URL pointers.
032: * <p>
033: * Inspired by Per Cederberg.
034: *
035: * @version $Id$
036: */
037: public class StandardPluginLocation implements PluginLocation {
038: /**
039: * Creates plug-in location from a given file and checks that all required
040: * resources are available. Before creating location object, this method
041: * probes given ZIP file of folder for presence of any of the following
042: * files:
043: * <ul>
044: * <li>/plugin.xml</li>
045: * <li>/plugin-fragment.xml</li>
046: * <li>/META-INF/plugin.xml</li>
047: * <li>/META-INF/plugin-fragment.xml</li>
048: * </ul>
049: * If any of those files present, a new plug-in location object created and
050: * returned.
051: * @param file plug-in JAR or ZIP file or plug-in folder
052: * @return created new plug-in location or <code>null</code> if given file
053: * doesn't points to a valid plug-in file or folder
054: * @throws MalformedURLException if the plug-in URL's couldn't be created
055: */
056: public static PluginLocation create(final File file)
057: throws MalformedURLException {
058: if (file.isDirectory()) {
059: URL manifestUrl = getManifestUrl(file);
060: return (manifestUrl == null) ? null
061: : new StandardPluginLocation(IoUtil.file2url(file),
062: manifestUrl);
063: }
064: String fileName = file.getName().toLowerCase(
065: Locale.getDefault());
066: if (!fileName.endsWith(".jar") //$NON-NLS-1$
067: && !fileName.endsWith(".zip")) { //$NON-NLS-1$
068: return null;
069: }
070: URL manifestUrl = getManifestUrl(file);
071: return (manifestUrl == null) ? null
072: : new StandardPluginLocation(
073: new URL("jar:" //$NON-NLS-1$
074: + IoUtil.file2url(file)
075: .toExternalForm() + "!/"), manifestUrl); //$NON-NLS-1$
076: }
077:
078: private static URL getManifestUrl(final File file)
079: throws MalformedURLException {
080: if (file.isDirectory()) {
081: File result = new File(file, "plugin.xml"); //$NON-NLS-1$
082: if (result.isFile()) {
083: return IoUtil.file2url(result);
084: }
085: result = new File(file, "plugin-fragment.xml"); //$NON-NLS-1$
086: if (result.isFile()) {
087: return IoUtil.file2url(result);
088: }
089: result = new File(file, "META-INF" + File.separator //$NON-NLS-1$
090: + "plugin.xml"); //$NON-NLS-1$
091: if (result.isFile()) {
092: return IoUtil.file2url(result);
093: }
094: result = new File(file, "META-INF" + File.separator //$NON-NLS-1$
095: + "plugin-fragment.xml"); //$NON-NLS-1$
096: if (result.isFile()) {
097: return IoUtil.file2url(result);
098: }
099: return null;
100: }
101: if (!file.isFile()) {
102: return null;
103: }
104: URL url = new URL("jar:" //$NON-NLS-1$
105: + IoUtil.file2url(file).toExternalForm()
106: + "!/plugin.xml"); //$NON-NLS-1$
107: if (IoUtil.isResourceExists(url)) {
108: return url;
109: }
110: url = new URL("jar:" //$NON-NLS-1$
111: + IoUtil.file2url(file).toExternalForm()
112: + "!/plugin-fragment.xml"); //$NON-NLS-1$
113: if (IoUtil.isResourceExists(url)) {
114: return url;
115: }
116: url = new URL("jar:" //$NON-NLS-1$
117: + IoUtil.file2url(file).toExternalForm()
118: + "!/META-INF/plugin.xml"); //$NON-NLS-1$
119: if (IoUtil.isResourceExists(url)) {
120: return url;
121: }
122: url = new URL("jar:" //$NON-NLS-1$
123: + IoUtil.file2url(file).toExternalForm()
124: + "!/META-INF/plugin-fragment.xml"); //$NON-NLS-1$
125: if (IoUtil.isResourceExists(url)) {
126: return url;
127: }
128: return null;
129: }
130:
131: private final URL context;
132: private final URL manifest;
133:
134: /**
135: * Creates a new plug-in location from a given context an manifest URL's.
136: * @param aContext plug-in context URL
137: * @param aManifest plug-in manifest URL
138: */
139: public StandardPluginLocation(final URL aContext,
140: final URL aManifest) {
141: if (aContext == null) {
142: throw new NullPointerException("context"); //$NON-NLS-1$
143: }
144: if (aManifest == null) {
145: throw new NullPointerException("manifest"); //$NON-NLS-1$
146: }
147: context = aContext;
148: manifest = aManifest;
149: }
150:
151: /**
152: * Creates a new plug-in location from a jar or a zip file or a folder. This
153: * plug-in manifest file path specified is relative to the root directory of
154: * the jar or zip file or given folder.
155: * @param file the plug-in zip file or plug-in folder
156: * @param manifestPath the relative manifest path
157: * @throws MalformedURLException if the plug-in URL's couldn't be created
158: */
159: public StandardPluginLocation(final File file,
160: final String manifestPath) throws MalformedURLException {
161: if (file.isDirectory()) {
162: context = IoUtil.file2url(file);
163: } else {
164: context = new URL("jar:" //$NON-NLS-1$
165: + IoUtil.file2url(file).toExternalForm() + "!/"); //$NON-NLS-1$
166: }
167: manifest = new URL(context, manifestPath.startsWith("/") //$NON-NLS-1$
168: ? manifestPath.substring(1)
169: : manifestPath);
170: }
171:
172: /**
173: * @see org.java.plugin.PluginManager.PluginLocation#getManifestLocation()
174: */
175: public URL getManifestLocation() {
176: return manifest;
177: }
178:
179: /**
180: * @see org.java.plugin.PluginManager.PluginLocation#getContextLocation()
181: */
182: public URL getContextLocation() {
183: return context;
184: }
185:
186: /**
187: * @see java.lang.Object#toString()
188: */
189: @Override
190: public String toString() {
191: return context.toString();
192: }
193: }
|