source: josm/trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java@ 3530

Last change on this file since 3530 was 3530, checked in by stoecker, 14 years ago

fix array preferences

  • Property svn:eol-style set to native
File size: 15.9 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.plugins;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Image;
7import java.io.File;
8import java.io.FileInputStream;
9import java.io.IOException;
10import java.io.InputStream;
11import java.lang.reflect.Constructor;
12import java.lang.reflect.InvocationTargetException;
13import java.net.MalformedURLException;
14import java.net.URL;
15import java.util.ArrayList;
16import java.util.Collection;
17import java.util.LinkedList;
18import java.util.List;
19import java.util.Map;
20import java.util.TreeMap;
21import java.util.jar.Attributes;
22import java.util.jar.JarInputStream;
23import java.util.jar.Manifest;
24import javax.swing.ImageIcon;
25
26import org.openstreetmap.josm.Main;
27import org.openstreetmap.josm.data.Version;
28import org.openstreetmap.josm.tools.ImageProvider;
29import org.openstreetmap.josm.tools.LanguageInfo;
30
31/**
32 * Encapsulate general information about a plugin. This information is available
33 * without the need of loading any class from the plugin jar file.
34 *
35 * @author imi
36 */
37public class PluginInformation {
38 public File file = null;
39 public String name = null;
40 public int mainversion = 0;
41 public String className = null;
42 public boolean oldmode = false;
43 public String requires = null;
44 public String link = null;
45 public String description = null;
46 public boolean early = false;
47 public String author = null;
48 public int stage = 50;
49 public String version = null;
50 public String localversion = null;
51 public String downloadlink = null;
52 public String iconPath;
53 public ImageIcon icon;
54 public List<URL> libraries = new LinkedList<URL>();
55 public final Map<String, String> attr = new TreeMap<String, String>();
56
57 /**
58 * Creates a plugin information object by reading the plugin information from
59 * the manifest in the plugin jar.
60 *
61 * The plugin name is derived from the file name.
62 *
63 * @param file the plugin jar file
64 * @throws PluginException if reading the manifest fails
65 */
66 public PluginInformation(File file) throws PluginException{
67 this(file, file.getName().substring(0, file.getName().length()-4));
68 }
69
70 /**
71 * Creates a plugin information object for the plugin with name {@code name}.
72 * Information about the plugin is extracted from the maifest file in the the plugin jar
73 * {@code file}.
74 * @param file the plugin jar
75 * @param name the plugin name
76 * @throws PluginException thrown if reading the manifest file fails
77 */
78 public PluginInformation(File file, String name) throws PluginException{
79 this.name = name;
80 this.file = file;
81 JarInputStream jar = null;
82 try {
83 jar = new JarInputStream(new FileInputStream(file));
84 Manifest manifest = jar.getManifest();
85 if (manifest == null)
86 throw new PluginException(name, tr("The plugin file ''{0}'' does not include a Manifest.", file.toString()));
87 scanManifest(manifest, false);
88 libraries.add(0, fileToURL(file));
89 } catch (IOException e) {
90 throw new PluginException(name, e);
91 } finally {
92 if (jar != null) {
93 try {
94 jar.close();
95 } catch(IOException e) { /* ignore */ }
96 }
97 }
98 }
99
100 /**
101 * Creates a plugin information object by reading plugin information in Manifest format
102 * from the input stream {@code manifestStream}.
103 *
104 * @param manifestStream the stream to read the manifest from
105 * @param name the plugin name
106 * @param url the download URL for the plugin
107 * @throws PluginException thrown if the plugin information can't be read from the input stream
108 */
109 public PluginInformation(InputStream manifestStream, String name, String url) throws PluginException {
110 this.name = name;
111 try {
112 Manifest manifest = new Manifest();
113 manifest.read(manifestStream);
114 if(url != null) {
115 downloadlink = url;
116 }
117 scanManifest(manifest, url != null);
118 } catch (IOException e) {
119 throw new PluginException(name, e);
120 }
121 }
122
123 /**
124 * Updates the plugin information of this plugin information object with the
125 * plugin information in a plugin information object retrieved from a plugin
126 * update site.
127 *
128 * @param other the plugin information object retrieved from the update
129 * site
130 */
131 public void updateFromPluginSite(PluginInformation other) {
132 this.mainversion = other.mainversion;
133 this.className = other.className;
134 this.requires = other.requires;
135 this.link = other.link;
136 this.description = other.description;
137 this.early = other.early;
138 this.author = other.author;
139 this.stage = other.stage;
140 this.version = other.version;
141 this.downloadlink = other.downloadlink;
142 this.icon = other.icon;
143 this.iconPath = other.iconPath;
144 this.libraries = other.libraries;
145 this.attr.clear();
146 this.attr.putAll(other.attr);
147 }
148
149 private void scanManifest(Manifest manifest, boolean oldcheck){
150 String lang = LanguageInfo.getLanguageCodeManifest();
151 Attributes attr = manifest.getMainAttributes();
152 className = attr.getValue("Plugin-Class");
153 String s = attr.getValue(lang+"Plugin-Link");
154 if(s == null) {
155 s = attr.getValue("Plugin-Link");
156 }
157 link = s;
158 requires = attr.getValue("Plugin-Requires");
159 s = attr.getValue(lang+"Plugin-Description");
160 if(s == null)
161 {
162 s = attr.getValue("Plugin-Description");
163 if(s != null) {
164 s = tr(s);
165 }
166 }
167 description = s;
168 early = Boolean.parseBoolean(attr.getValue("Plugin-Early"));
169 String stageStr = attr.getValue("Plugin-Stage");
170 stage = stageStr == null ? 50 : Integer.parseInt(stageStr);
171 version = attr.getValue("Plugin-Version");
172 try { mainversion = Integer.parseInt(attr.getValue("Plugin-Mainversion")); }
173 catch(NumberFormatException e) {}
174 author = attr.getValue("Author");
175 iconPath = attr.getValue("Plugin-Icon");
176 if (iconPath != null && file != null) {
177 // extract icon from the plugin jar file
178 icon = ImageProvider.getIfAvailable(null, null, null, iconPath, file);
179 }
180 if(oldcheck && mainversion > Version.getInstance().getVersion())
181 {
182 int myv = Version.getInstance().getVersion();
183 for(Map.Entry<Object, Object> entry : attr.entrySet())
184 {
185 try {
186 String key = ((Attributes.Name)entry.getKey()).toString();
187 if(key.endsWith("_Plugin-Url"))
188 {
189 int mv = Integer.parseInt(key.substring(0,key.length()-11));
190 if(mv <= myv && (mv > mainversion || mainversion > myv))
191 {
192 String v = (String)entry.getValue();
193 int i = v.indexOf(";");
194 if(i > 0)
195 {
196 downloadlink = v.substring(i+1);
197 mainversion = mv;
198 version = v.substring(0,i);
199 oldmode = true;
200 }
201 }
202 }
203 }
204 catch(Exception e) { e.printStackTrace(); }
205 }
206 }
207
208 String classPath = attr.getValue(Attributes.Name.CLASS_PATH);
209 if (classPath != null) {
210 for (String entry : classPath.split(" ")) {
211 File entryFile;
212 if (new File(entry).isAbsolute()) {
213 entryFile = new File(entry);
214 } else {
215 entryFile = new File(file.getParent(), entry);
216 }
217
218 libraries.add(fileToURL(entryFile));
219 }
220 }
221 for (Object o : attr.keySet()) {
222 this.attr.put(o.toString(), attr.getValue(o.toString()));
223 }
224 }
225
226 /**
227 * Replies the description as HTML document, including a link to a web page with
228 * more information, provided such a link is available.
229 *
230 * @return the description as HTML document
231 */
232 public String getDescriptionAsHtml() {
233 StringBuilder sb = new StringBuilder();
234 sb.append("<html><body>");
235 sb.append(description == null ? tr("no description available") : description);
236 if (link != null) {
237 sb.append(" <a href=\"").append(link).append("\">").append(tr("More info...")).append("</a>");
238 }
239 sb.append("</body></html>");
240 return sb.toString();
241 }
242
243 /**
244 * Load and instantiate the plugin
245 *
246 * @param the plugin class
247 * @return the instantiated and initialized plugin
248 */
249 public PluginProxy load(Class<?> klass) throws PluginException{
250 try {
251 Constructor<?> c = klass.getConstructor(PluginInformation.class);
252 Object plugin = c.newInstance(this);
253 return new PluginProxy(plugin, this);
254 } catch(NoSuchMethodException e) {
255 throw new PluginException(name, e);
256 } catch(IllegalAccessException e) {
257 throw new PluginException(name, e);
258 } catch (InstantiationException e) {
259 throw new PluginException(name, e);
260 } catch(InvocationTargetException e) {
261 throw new PluginException(name, e);
262 }
263 }
264
265 /**
266 * Load the class of the plugin
267 *
268 * @param classLoader the class loader to use
269 * @return the loaded class
270 */
271 public Class<?> loadClass(ClassLoader classLoader) throws PluginException {
272 if (className == null)
273 return null;
274 try{
275 Class<?> realClass = Class.forName(className, true, classLoader);
276 return realClass;
277 } catch (ClassNotFoundException e) {
278 throw new PluginException(name, e);
279 } catch(ClassCastException e) {
280 throw new PluginException(name, e);
281 }
282 }
283
284 public static URL fileToURL(File f) {
285 try {
286 return f.toURI().toURL();
287 } catch (MalformedURLException ex) {
288 return null;
289 }
290 }
291
292 /**
293 * Try to find a plugin after some criterias. Extract the plugin-information
294 * from the plugin and return it. The plugin is searched in the following way:
295 *
296 *<li>first look after an MANIFEST.MF in the package org.openstreetmap.josm.plugins.<plugin name>
297 * (After removing all fancy characters from the plugin name).
298 * If found, the plugin is loaded using the bootstrap classloader.
299 *<li>If not found, look for a jar file in the user specific plugin directory
300 * (~/.josm/plugins/<plugin name>.jar)
301 *<li>If not found and the environment variable JOSM_RESOURCES + "/plugins/" exist, look there.
302 *<li>Try for the java property josm.resources + "/plugins/" (set via java -Djosm.plugins.path=...)
303 *<li>If the environment variable ALLUSERSPROFILE and APPDATA exist, look in
304 * ALLUSERSPROFILE/<the last stuff from APPDATA>/JOSM/plugins.
305 * (*sic* There is no easy way under Windows to get the All User's application
306 * directory)
307 *<li>Finally, look in some typical unix paths:<ul>
308 * <li>/usr/local/share/josm/plugins/
309 * <li>/usr/local/lib/josm/plugins/
310 * <li>/usr/share/josm/plugins/
311 * <li>/usr/lib/josm/plugins/
312 *
313 * If a plugin class or jar file is found earlier in the list but seem not to
314 * be working, an PluginException is thrown rather than continuing the search.
315 * This is so JOSM can detect broken user-provided plugins and do not go silently
316 * ignore them.
317 *
318 * The plugin is not initialized. If the plugin is a .jar file, it is not loaded
319 * (only the manifest is extracted). In the classloader-case, the class is
320 * bootstraped (e.g. static {} - declarations will run. However, nothing else is done.
321 *
322 * @param pluginName The name of the plugin (in all lowercase). E.g. "lang-de"
323 * @return Information about the plugin or <code>null</code>, if the plugin
324 * was nowhere to be found.
325 * @throws PluginException In case of broken plugins.
326 */
327 public static PluginInformation findPlugin(String pluginName) throws PluginException {
328 String name = pluginName;
329 name = name.replaceAll("[-. ]", "");
330 InputStream manifestStream = PluginInformation.class.getResourceAsStream("/org/openstreetmap/josm/plugins/"+name+"/MANIFEST.MF");
331 if (manifestStream != null)
332 return new PluginInformation(manifestStream, pluginName, null);
333
334 Collection<String> locations = getPluginLocations();
335
336 for (String s : locations) {
337 File pluginFile = new File(s, pluginName + ".jar");
338 if (pluginFile.exists()) {
339 PluginInformation info = new PluginInformation(pluginFile);
340 return info;
341 }
342 }
343 return null;
344 }
345
346 public static Collection<String> getPluginLocations() {
347 Collection<String> locations = Main.pref.getAllPossiblePreferenceDirs();
348 Collection<String> all = new ArrayList<String>(locations.size());
349 for (String s : locations) {
350 all.add(s+"plugins");
351 }
352 return all;
353 }
354
355 /**
356 * Replies true if the plugin with the given information is most likely outdated with
357 * respect to the referenceVersion.
358 *
359 * @param referenceVersion the reference version. Can be null if we don't know a
360 * reference version
361 *
362 * @return true, if the plugin needs to be updated; false, otherweise
363 */
364 public boolean isUpdateRequired(String referenceVersion) {
365 if (this.downloadlink == null) return false;
366 if (this.version == null && referenceVersion!= null)
367 return true;
368 if (this.version != null && !this.version.equals(referenceVersion))
369 return true;
370 return false;
371 }
372
373 /**
374 * Replies true if this this plugin should be updated/downloaded because either
375 * it is not available locally (its local version is null) or its local version is
376 * older than the available version on the server.
377 *
378 * @return true if the plugin should be updated
379 */
380 public boolean isUpdateRequired() {
381 if (this.downloadlink == null) return false;
382 if (this.localversion == null) return true;
383 return isUpdateRequired(this.localversion);
384 }
385
386 protected boolean matches(String filter, String value) {
387 if (filter == null) return true;
388 if (value == null) return false;
389 return value.toLowerCase().contains(filter.toLowerCase());
390 }
391
392 /**
393 * Replies true if either the name, the description, or the version match (case insensitive)
394 * one of the words in filter. Replies true if filter is null.
395 *
396 * @param filter the filter expression
397 * @return true if this plugin info matches with the filter
398 */
399 public boolean matches(String filter) {
400 if (filter == null) return true;
401 String words[] = filter.split("\\s+");
402 for (String word: words) {
403 if (matches(word, name)
404 || matches(word, description)
405 || matches(word, version)
406 || matches(word, localversion))
407 return true;
408 }
409 return false;
410 }
411
412 /**
413 * Replies the name of the plugin
414 */
415 public String getName() {
416 return name;
417 }
418
419 /**
420 * Sets the name
421 * @param name
422 */
423 public void setName(String name) {
424 this.name = name;
425 }
426
427 public ImageIcon getScaledIcon() {
428 if (icon == null)
429 return null;
430 return new ImageIcon(icon.getImage().getScaledInstance(24, 24, Image.SCALE_SMOOTH));
431 }
432}
Note: See TracBrowser for help on using the repository browser.