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

Last change on this file since 9917 was 9645, checked in by Don-vip, 8 years ago

fix findbugs issue RV_RETURN_VALUE_IGNORED_BAD_PRACTICE for java.io.File.mkdirs

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