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

Last change on this file since 8470 was 7033, checked in by Don-vip, 10 years ago

see #8465 - global use of try-with-resources, according to

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