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.jar.JarInputStream;
|
---|
9 | import java.util.jar.Manifest;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Encapsulate general information about a plugin. This information is available
|
---|
13 | * without the need of loading any class from the plugin jar file.
|
---|
14 | *
|
---|
15 | * @author imi
|
---|
16 | */
|
---|
17 | public class PluginInformation {
|
---|
18 | public final File file;
|
---|
19 | public final String name;
|
---|
20 | public final String className;
|
---|
21 | public final String description;
|
---|
22 |
|
---|
23 | public PluginInformation(File file) {
|
---|
24 | this.file = file;
|
---|
25 | name = file.getName().substring(0, file.getName().length()-4);
|
---|
26 | try {
|
---|
27 | JarInputStream jar = new JarInputStream(new FileInputStream(file));
|
---|
28 | Manifest manifest = jar.getManifest();
|
---|
29 | className = manifest.getMainAttributes().getValue("Plugin-Class");
|
---|
30 | description = manifest.getMainAttributes().getValue("Plugin-Description");
|
---|
31 | jar.close();
|
---|
32 | } catch (IOException e) {
|
---|
33 | throw new PluginException(null, name, e);
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Load and instantiate the plugin
|
---|
39 | */
|
---|
40 | public PluginProxy load() {
|
---|
41 | try {
|
---|
42 | ClassLoader loader = URLClassLoader.newInstance(
|
---|
43 | new URL[]{new URL(getURLString())},
|
---|
44 | getClass().getClassLoader());
|
---|
45 | Object plugin = Class.forName(className, true, loader).newInstance();
|
---|
46 | return new PluginProxy(plugin, this);
|
---|
47 | } catch (Exception e) {
|
---|
48 | throw new PluginException(null, name, e);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | private String getURLString() {
|
---|
53 | if (System.getProperty("os.name").startsWith("Windows"))
|
---|
54 | return "file:/"+file.getAbsolutePath();
|
---|
55 | return "file://"+file.getAbsolutePath();
|
---|
56 | }
|
---|
57 | }
|
---|