1 | package org.openstreetmap.josm.plugins;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.net.URL;
|
---|
7 | import java.net.URLClassLoader;
|
---|
8 | import java.util.Map;
|
---|
9 | import java.util.TreeMap;
|
---|
10 | import java.util.jar.Attributes;
|
---|
11 | import java.util.jar.JarInputStream;
|
---|
12 | import java.util.jar.Manifest;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Encapsulate general information about a plugin. This information is available
|
---|
16 | * without the need of loading any class from the plugin jar file.
|
---|
17 | *
|
---|
18 | * @author imi
|
---|
19 | */
|
---|
20 | public class PluginInformation {
|
---|
21 | public final File file;
|
---|
22 | public final String name;
|
---|
23 | public final String className;
|
---|
24 | public final String description;
|
---|
25 | public final boolean early;
|
---|
26 | public final String author;
|
---|
27 |
|
---|
28 | public final Map<String, String> attr = new TreeMap<String, String>();
|
---|
29 |
|
---|
30 | public PluginInformation(File file) {
|
---|
31 | this.file = file;
|
---|
32 | name = file.getName().substring(0, file.getName().length()-4);
|
---|
33 | try {
|
---|
34 | JarInputStream jar = new JarInputStream(new FileInputStream(file));
|
---|
35 | Manifest manifest = jar.getManifest();
|
---|
36 | Attributes attr = manifest.getMainAttributes();
|
---|
37 | className = attr.getValue("Plugin-Class");
|
---|
38 | description = attr.getValue("Plugin-Description");
|
---|
39 | early = Boolean.parseBoolean(attr.getValue("Plugin-Early"));
|
---|
40 | author = attr.getValue("Author");
|
---|
41 | for (Object o : attr.keySet())
|
---|
42 | this.attr.put(o.toString(), attr.getValue(o.toString()));
|
---|
43 | jar.close();
|
---|
44 | } catch (IOException e) {
|
---|
45 | throw new PluginException(null, name, e);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Load and instantiate the plugin
|
---|
51 | */
|
---|
52 | public PluginProxy load(Class<?> klass) {
|
---|
53 | try {
|
---|
54 | return new PluginProxy(klass.newInstance(), this);
|
---|
55 | } catch (Exception e) {
|
---|
56 | throw new PluginException(null, name, e);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Load the class of the plugin
|
---|
62 | */
|
---|
63 | public Class<?> loadClass() {
|
---|
64 | try {
|
---|
65 | ClassLoader loader = URLClassLoader.newInstance(
|
---|
66 | new URL[]{new URL(getURLString())},
|
---|
67 | getClass().getClassLoader());
|
---|
68 | Class<?> realClass = Class.forName(className, true, loader);
|
---|
69 | return realClass;
|
---|
70 | } catch (Exception e) {
|
---|
71 | throw new PluginException(null, name, e);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private String getURLString() {
|
---|
76 | if (System.getProperty("os.name").startsWith("Windows"))
|
---|
77 | return "file:/"+file.getAbsolutePath();
|
---|
78 | return "file://"+file.getAbsolutePath();
|
---|
79 | }
|
---|
80 | }
|
---|