KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > standard > StandardPluginLocation


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.standard;
20
21 import java.io.File JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Locale JavaDoc;
25
26 import org.java.plugin.PluginManager.PluginLocation;
27 import org.java.plugin.util.IoUtil;
28
29 /**
30  * A standard implementation of plug-in location interface. It may be used to
31  * create plug-in locations from JAR or ZIP files of plug-in folders, or from
32  * any URL pointers.
33  * <p>
34  * Inspired by Per Cederberg.
35  *
36  * @version $Id: StandardPluginLocation.java,v 1.1 2006/04/09 14:41:50 ddimon Exp $
37  */

38 public class StandardPluginLocation implements PluginLocation {
39     /**
40      * Creates plug-in location from a given file and checks that all required
41      * resources are available. Before creating location object, this method
42      * probes given ZIP file of folder for presence of any of the following
43      * files:
44      * <ul>
45      * <li>/plugin.xml</li>
46      * <li>/plugin-fragment.xml</li>
47      * <li>/META-INF/plugin.xml</li>
48      * <li>/META-INF/plugin-fragment.xml</li>
49      * </ul>
50      * If any of those files present, a new plug-in location object created and
51      * returned.
52      * @param file plug-in JAR or ZIP file or plug-in folder
53      * @return created new plug-in location or <code>null</code> if given file
54      * doesn't points to a valid plug-in file or folder
55      * @throws MalformedURLException if the plug-in URL's couldn't be created
56      */

57     public static PluginLocation create(final File JavaDoc file)
58             throws MalformedURLException JavaDoc {
59         if (file.isDirectory()) {
60             URL JavaDoc manifestUrl = getManifestUrl(file);
61             return (manifestUrl == null) ? null
62                     : new StandardPluginLocation(
63                             IoUtil.file2url(file), manifestUrl);
64         }
65         String JavaDoc fileName = file.getName().toLowerCase(Locale.getDefault());
66         if (!fileName.endsWith(".jar") //$NON-NLS-1$
67
&& !fileName.endsWith(".zip")) { //$NON-NLS-1$
68
return null;
69         }
70         URL JavaDoc manifestUrl = getManifestUrl(file);
71         return (manifestUrl == null) ? null
72                 : new StandardPluginLocation(new URL JavaDoc("jar:" //$NON-NLS-1$
73
+ IoUtil.file2url(file).toExternalForm()
74                         + "!/"), manifestUrl); //$NON-NLS-1$
75
}
76
77     private static URL JavaDoc getManifestUrl(final File JavaDoc file)
78             throws MalformedURLException JavaDoc {
79         if (file.isDirectory()) {
80             File JavaDoc result = new File JavaDoc(file, "plugin.xml"); //$NON-NLS-1$
81
if (result.isFile()) {
82                 return IoUtil.file2url(result);
83             }
84             result = new File JavaDoc(file, "plugin-fragment.xml"); //$NON-NLS-1$
85
if (result.isFile()) {
86                 return IoUtil.file2url(result);
87             }
88             result = new File JavaDoc(file, "META-INF" + File.separator //$NON-NLS-1$
89
+ "plugin.xml"); //$NON-NLS-1$
90
if (result.isFile()) {
91                 return IoUtil.file2url(result);
92             }
93             result = new File JavaDoc(file, "META-INF" + File.separator //$NON-NLS-1$
94
+ "plugin-fragment.xml"); //$NON-NLS-1$
95
if (result.isFile()) {
96                 return IoUtil.file2url(result);
97             }
98             return null;
99         }
100         if (!file.isFile()) {
101             return null;
102         }
103         URL JavaDoc url = new URL JavaDoc("jar:" //$NON-NLS-1$
104
+ IoUtil.file2url(file).toExternalForm()
105                 + "!/plugin.xml"); //$NON-NLS-1$
106
if (IoUtil.isResourceExists(url)) {
107             return url;
108         }
109         url = new URL JavaDoc("jar:" //$NON-NLS-1$
110
+ IoUtil.file2url(file).toExternalForm()
111                 + "!/plugin-fragment.xml"); //$NON-NLS-1$
112
if (IoUtil.isResourceExists(url)) {
113             return url;
114         }
115         url = new URL JavaDoc("jar:" //$NON-NLS-1$
116
+ IoUtil.file2url(file).toExternalForm()
117                 + "!/META-INF/plugin.xml"); //$NON-NLS-1$
118
if (IoUtil.isResourceExists(url)) {
119             return url;
120         }
121         url = new URL JavaDoc("jar:" //$NON-NLS-1$
122
+ IoUtil.file2url(file).toExternalForm()
123                 + "!/META-INF/plugin-fragment.xml"); //$NON-NLS-1$
124
if (IoUtil.isResourceExists(url)) {
125             return url;
126         }
127         return null;
128     }
129     
130     private final URL JavaDoc context;
131     private final URL JavaDoc manifest;
132
133     /**
134      * Creates a new plug-in location from a given context an manifest URL's.
135      * @param aContext plug-in context URL
136      * @param aManifest plug-in manifest URL
137      */

138     public StandardPluginLocation(final URL JavaDoc aContext, final URL JavaDoc aManifest) {
139         if (aContext == null) {
140             throw new NullPointerException JavaDoc("context"); //$NON-NLS-1$
141
}
142         if (aManifest == null) {
143             throw new NullPointerException JavaDoc("manifest"); //$NON-NLS-1$
144
}
145         context = aContext;
146         manifest = aManifest;
147     }
148     
149     /**
150      * Creates a new plug-in location from a jar or a zip file or a folder. This
151      * plug-in manifest file path specified is relative to the root directory of
152      * the jar or zip file or given folder.
153      * @param file the plug-in zip file or plug-in folder
154      * @param manifestPath the relative manifest path
155      * @throws MalformedURLException if the plug-in URL's couldn't be created
156      */

157     public StandardPluginLocation(final File JavaDoc file, final String JavaDoc manifestPath)
158             throws MalformedURLException JavaDoc {
159         if (file.isDirectory()) {
160             context = IoUtil.file2url(file);
161         } else {
162             context = new URL JavaDoc("jar:" //$NON-NLS-1$
163
+ IoUtil.file2url(file).toExternalForm() + "!/"); //$NON-NLS-1$
164
}
165         manifest = new URL JavaDoc(context, manifestPath.startsWith("/") //$NON-NLS-1$
166
? manifestPath.substring(1) : manifestPath);
167     }
168
169     /**
170      * @see org.java.plugin.PluginManager.PluginLocation#getManifestLocation()
171      */

172     public URL JavaDoc getManifestLocation() {
173         return manifest;
174     }
175
176     /**
177      * @see org.java.plugin.PluginManager.PluginLocation#getContextLocation()
178      */

179     public URL JavaDoc getContextLocation() {
180         return context;
181     }
182
183     /**
184      * @see java.lang.Object#toString()
185      */

186     public String JavaDoc toString() {
187         return context.toString();
188     }
189 }
190
Popular Tags