1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.FileNotFoundException;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.net.URL;
|
---|
11 | import java.net.URLClassLoader;
|
---|
12 | import java.nio.file.Files;
|
---|
13 | import java.nio.file.StandardCopyOption;
|
---|
14 | import java.security.AccessController;
|
---|
15 | import java.security.PrivilegedAction;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
20 | import org.openstreetmap.josm.gui.MapFrameListener;
|
---|
21 | import org.openstreetmap.josm.gui.download.DownloadSelection;
|
---|
22 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
23 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
24 | import org.openstreetmap.josm.spi.preferences.IBaseDirectories;
|
---|
25 | import org.openstreetmap.josm.tools.Logging;
|
---|
26 | import org.openstreetmap.josm.tools.Utils;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * For all purposes of loading dynamic resources, the Plugin's class loader should be used
|
---|
30 | * (or else, the plugin jar will not be within the class path).
|
---|
31 | *
|
---|
32 | * A plugin may subclass this abstract base class (but it is optional).
|
---|
33 | *
|
---|
34 | * The actual implementation of this class is optional, as all functions will be called
|
---|
35 | * via reflection. This is to be able to change this interface without the need of
|
---|
36 | * recompiling or even breaking the plugins. If your class does not provide a
|
---|
37 | * function here (or does provide a function with a mismatching signature), it will not
|
---|
38 | * be called. That simple.
|
---|
39 | *
|
---|
40 | * Or in other words: See this base class as an documentation of what automatic callbacks
|
---|
41 | * are provided (you can register yourself to more callbacks in your plugin class
|
---|
42 | * constructor).
|
---|
43 | *
|
---|
44 | * Subclassing Plugin and overriding some functions makes it easy for you to keep sync
|
---|
45 | * with the correct actual plugin architecture of JOSM.
|
---|
46 | *
|
---|
47 | * @author Immanuel.Scholz
|
---|
48 | */
|
---|
49 | public abstract class Plugin implements MapFrameListener {
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * This is the info available for this plugin. You can access this from your
|
---|
53 | * constructor.
|
---|
54 | *
|
---|
55 | * (The actual implementation to request the info from a static variable
|
---|
56 | * is a bit hacky, but it works).
|
---|
57 | */
|
---|
58 | private PluginInformation info;
|
---|
59 |
|
---|
60 | private final IBaseDirectories pluginBaseDirectories = new PluginBaseDirectories();
|
---|
61 |
|
---|
62 | private class PluginBaseDirectories implements IBaseDirectories {
|
---|
63 | private File preferencesDir;
|
---|
64 | private File cacheDir;
|
---|
65 | private File userdataDir;
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public File getPreferencesDirectory(boolean createIfMissing) {
|
---|
69 | if (preferencesDir == null) {
|
---|
70 | preferencesDir = Config.getDirs().getPreferencesDirectory(createIfMissing).toPath()
|
---|
71 | .resolve("plugins").resolve(info.name).toFile();
|
---|
72 | }
|
---|
73 | if (createIfMissing && !preferencesDir.exists() && !preferencesDir.mkdirs()) {
|
---|
74 | Logging.error(tr("Failed to create missing plugin preferences directory: {0}", preferencesDir.getAbsoluteFile()));
|
---|
75 | }
|
---|
76 | return preferencesDir;
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public File getUserDataDirectory(boolean createIfMissing) {
|
---|
81 | if (userdataDir == null) {
|
---|
82 | userdataDir = Config.getDirs().getUserDataDirectory(createIfMissing).toPath()
|
---|
83 | .resolve("plugins").resolve(info.name).toFile();
|
---|
84 | }
|
---|
85 | if (createIfMissing && !userdataDir.exists() && !userdataDir.mkdirs()) {
|
---|
86 | Logging.error(tr("Failed to create missing plugin user data directory: {0}", userdataDir.getAbsoluteFile()));
|
---|
87 | }
|
---|
88 | return userdataDir;
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public File getCacheDirectory(boolean createIfMissing) {
|
---|
93 | if (cacheDir == null) {
|
---|
94 | cacheDir = Config.getDirs().getCacheDirectory(createIfMissing).toPath()
|
---|
95 | .resolve("plugins").resolve(info.name).toFile();
|
---|
96 | }
|
---|
97 | if (createIfMissing && !cacheDir.exists() && !cacheDir.mkdirs()) {
|
---|
98 | Logging.error(tr("Failed to create missing plugin cache directory: {0}", cacheDir.getAbsoluteFile()));
|
---|
99 | }
|
---|
100 | return cacheDir;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Creates the plugin
|
---|
106 | *
|
---|
107 | * @param info the plugin information describing the plugin.
|
---|
108 | */
|
---|
109 | public Plugin(PluginInformation info) {
|
---|
110 | this.info = info;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Replies the plugin information object for this plugin
|
---|
115 | *
|
---|
116 | * @return the plugin information object
|
---|
117 | */
|
---|
118 | public PluginInformation getPluginInformation() {
|
---|
119 | return info;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Sets the plugin information object for this plugin
|
---|
124 | *
|
---|
125 | * @param info the plugin information object
|
---|
126 | */
|
---|
127 | public void setPluginInformation(PluginInformation info) {
|
---|
128 | this.info = info;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Get the directories where this plugin can store various files.
|
---|
133 | * @return the directories where this plugin can store files
|
---|
134 | * @since 13007
|
---|
135 | */
|
---|
136 | public IBaseDirectories getPluginDirs() {
|
---|
137 | return pluginBaseDirectories;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * @return The directory for the plugin to store all kind of stuff.
|
---|
142 | * @deprecated (since 13007) to get the same directory as this method, use {@code getPluginDirs().getUserDataDirectory(false)}.
|
---|
143 | * However, for files that can be characterized as cache or preferences, you are encouraged to use the appropriate
|
---|
144 | * {@link IBaseDirectories} method from {@link #getPluginDirs()}.
|
---|
145 | */
|
---|
146 | @Deprecated
|
---|
147 | public String getPluginDir() {
|
---|
148 | return new File(Main.pref.getPluginsDirectory(), info.name).getPath();
|
---|
149 | }
|
---|
150 |
|
---|
151 | @Override
|
---|
152 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {}
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Called in the preferences dialog to create a preferences page for the plugin,
|
---|
156 | * if any available.
|
---|
157 | * @return the preferences dialog, or {@code null}
|
---|
158 | */
|
---|
159 | public PreferenceSetting getPreferenceSetting() {
|
---|
160 | return null;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Called in the download dialog to give the plugin a chance to modify the list
|
---|
165 | * of bounding box selectors.
|
---|
166 | * @param list list of bounding box selectors
|
---|
167 | */
|
---|
168 | public void addDownloadSelection(List<DownloadSelection> list) {}
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Copies the resource 'from' to the file in the plugin directory named 'to'.
|
---|
172 | * @param from source file
|
---|
173 | * @param to target file
|
---|
174 | * @throws FileNotFoundException if the file exists but is a directory rather than a regular file,
|
---|
175 | * does not exist but cannot be created, or cannot be opened for any other reason
|
---|
176 | * @throws IOException if any other I/O error occurs
|
---|
177 | */
|
---|
178 | public void copy(String from, String to) throws IOException {
|
---|
179 | String pluginDirName = getPluginDir();
|
---|
180 | File pluginDir = new File(pluginDirName);
|
---|
181 | if (!pluginDir.exists()) {
|
---|
182 | Utils.mkDirs(pluginDir);
|
---|
183 | }
|
---|
184 | try (InputStream in = getClass().getResourceAsStream(from)) {
|
---|
185 | if (in == null) {
|
---|
186 | throw new IOException("Resource not found: "+from);
|
---|
187 | }
|
---|
188 | Files.copy(in, new File(pluginDirName, to).toPath(), StandardCopyOption.REPLACE_EXISTING);
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Get a class loader for loading resources from the plugin jar.
|
---|
194 | *
|
---|
195 | * This can be used to avoid getting a file from another plugin that
|
---|
196 | * happens to have a file with the same file name and path.
|
---|
197 | *
|
---|
198 | * Usage: Instead of
|
---|
199 | * getClass().getResource("/resources/pluginProperties.properties");
|
---|
200 | * write
|
---|
201 | * getPluginResourceClassLoader().getResource("resources/pluginProperties.properties");
|
---|
202 | *
|
---|
203 | * (Note the missing leading "/".)
|
---|
204 | * @return a class loader for loading resources from the plugin jar
|
---|
205 | */
|
---|
206 | public ClassLoader getPluginResourceClassLoader() {
|
---|
207 | File pluginDir = Main.pref.getPluginsDirectory();
|
---|
208 | File pluginJar = new File(pluginDir, info.name + ".jar");
|
---|
209 | final URL pluginJarUrl = Utils.fileToURL(pluginJar);
|
---|
210 | return AccessController.doPrivileged((PrivilegedAction<ClassLoader>)
|
---|
211 | () -> new URLClassLoader(new URL[] {pluginJarUrl}, Plugin.class.getClassLoader()));
|
---|
212 | }
|
---|
213 | }
|
---|