source: josm/trunk/src/org/openstreetmap/josm/plugins/Plugin.java@ 5987

Last change on this file since 5987 was 5987, checked in by Don-vip, 11 years ago

Better error messages when loading plugins

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.plugins;
3
4import java.io.File;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.net.URL;
10import java.net.URLClassLoader;
11import java.util.List;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.gui.MapFrame;
15import org.openstreetmap.josm.gui.MapFrameListener;
16import org.openstreetmap.josm.gui.download.DownloadSelection;
17import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
18import org.openstreetmap.josm.tools.Utils;
19
20/**
21 * For all purposes of loading dynamic resources, the Plugin's class loader should be used
22 * (or else, the plugin jar will not be within the class path).
23 *
24 * A plugin may subclass this abstract base class (but it is optional).
25 *
26 * The actual implementation of this class is optional, as all functions will be called
27 * via reflection. This is to be able to change this interface without the need of
28 * recompiling or even breaking the plugins. If your class does not provide a
29 * function here (or does provide a function with a mismatching signature), it will not
30 * be called. That simple.
31 *
32 * Or in other words: See this base class as an documentation of what automatic callbacks
33 * are provided (you can register yourself to more callbacks in your plugin class
34 * constructor).
35 *
36 * Subclassing Plugin and overriding some functions makes it easy for you to keep sync
37 * with the correct actual plugin architecture of JOSM.
38 *
39 * @author Immanuel.Scholz
40 */
41public abstract class Plugin implements MapFrameListener {
42
43 /**
44 * This is the info available for this plugin. You can access this from your
45 * constructor.
46 *
47 * (The actual implementation to request the info from a static variable
48 * is a bit hacky, but it works).
49 */
50 private PluginInformation info = null;
51
52 /**
53 * Creates the plugin
54 *
55 * @param info the plugin information describing the plugin.
56 */
57 public Plugin(PluginInformation info) {
58 this.info = info;
59 }
60
61 /**
62 * Replies the plugin information object for this plugin
63 *
64 * @return the plugin information object
65 */
66 public PluginInformation getPluginInformation() {
67 return info;
68 }
69
70 /**
71 * Sets the plugin information object for this plugin
72 *
73 * @param info the plugin information object
74 */
75 public void setPluginInformation(PluginInformation info) {
76 this.info = info;
77 }
78
79 /**
80 * @return The directory for the plugin to store all kind of stuff.
81 */
82 public String getPluginDir() {
83 return new File(Main.pref.getPluginsDirectory(), info.name).getPath();
84 }
85
86 @Override
87 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {}
88
89 /**
90 * Called in the preferences dialog to create a preferences page for the plugin,
91 * if any available.
92 */
93 public PreferenceSetting getPreferenceSetting() { return null; }
94
95 /**
96 * Called in the download dialog to give the plugin a chance to modify the list
97 * of bounding box selectors.
98 */
99 public void addDownloadSelection(List<DownloadSelection> list) {}
100
101 /**
102 * Copies the resource 'from' to the file in the plugin directory named 'to'.
103 */
104 public void copy(String from, String to) throws FileNotFoundException, IOException {
105 String pluginDirName = getPluginDir();
106 File pluginDir = new File(pluginDirName);
107 if (!pluginDir.exists()) {
108 pluginDir.mkdirs();
109 }
110 FileOutputStream out = null;
111 InputStream in = null;
112 try {
113 out = new FileOutputStream(new File(pluginDirName, to));
114 in = getClass().getResourceAsStream(from);
115 if (in == null) {
116 throw new IOException("Resource not found: "+from);
117 }
118 byte[] buffer = new byte[8192];
119 for(int len = in.read(buffer); len > 0; len = in.read(buffer)) {
120 out.write(buffer, 0, len);
121 }
122 } finally {
123 Utils.close(in);
124 Utils.close(out);
125 }
126 }
127
128 /**
129 * Get a class loader for loading resources from the plugin jar.
130 *
131 * This can be used to avoid getting a file from another plugin that
132 * happens to have a file with the same file name and path.
133 *
134 * Usage: Instead of
135 * getClass().getResource("/resources/pluginProperties.properties");
136 * write
137 * getPluginResourceClassLoader().getResource("resources/pluginProperties.properties");
138 *
139 * (Note the missing leading "/".)
140 */
141 public ClassLoader getPluginResourceClassLoader() {
142 File pluginDir = Main.pref.getPluginsDirectory();
143 File pluginJar = new File(pluginDir, info.name + ".jar");
144 URL pluginJarUrl = PluginInformation.fileToURL(pluginJar);
145 URLClassLoader pluginClassLoader = new URLClassLoader(new URL[] { pluginJarUrl } , Main.class.getClassLoader());
146 return pluginClassLoader;
147 }
148}
Note: See TracBrowser for help on using the repository browser.