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

Last change on this file since 13193 was 13193, checked in by Don-vip, 8 months ago

see #15310 - deprecate Plugin.copy(String, String)

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.FileNotFoundException;
8import java.io.IOException;
9import java.io.InputStream;
10import java.net.URL;
11import java.net.URLClassLoader;
12import java.nio.file.Files;
13import java.nio.file.StandardCopyOption;
14import java.security.AccessController;
15import java.security.PrivilegedAction;
16import java.util.List;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.gui.MapFrame;
20import org.openstreetmap.josm.gui.MapFrameListener;
21import org.openstreetmap.josm.gui.download.DownloadSelection;
22import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
23import org.openstreetmap.josm.spi.preferences.Config;
24import org.openstreetmap.josm.spi.preferences.IBaseDirectories;
25import org.openstreetmap.josm.tools.Logging;
26import 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 */
49public 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     * @deprecated without replacement
178     */
179    @Deprecated
180    public void copy(String from, String to) throws IOException {
181        String pluginDirName = getPluginDir();
182        File pluginDir = new File(pluginDirName);
183        if (!pluginDir.exists()) {
184            Utils.mkDirs(pluginDir);
185        }
186        try (InputStream in = getClass().getResourceAsStream(from)) {
187            if (in == null) {
188                throw new IOException("Resource not found: "+from);
189            }
190            Files.copy(in, new File(pluginDirName, to).toPath(), StandardCopyOption.REPLACE_EXISTING);
191        }
192    }
193
194    /**
195     * Get a class loader for loading resources from the plugin jar.
196     *
197     * This can be used to avoid getting a file from another plugin that
198     * happens to have a file with the same file name and path.
199     *
200     * Usage: Instead of
201     *   getClass().getResource("/resources/pluginProperties.properties");
202     * write
203     *   getPluginResourceClassLoader().getResource("resources/pluginProperties.properties");
204     *
205     * (Note the missing leading "/".)
206     * @return a class loader for loading resources from the plugin jar
207     */
208    public ClassLoader getPluginResourceClassLoader() {
209        File pluginDir = Main.pref.getPluginsDirectory();
210        File pluginJar = new File(pluginDir, info.name + ".jar");
211        final URL pluginJarUrl = Utils.fileToURL(pluginJar);
212        return AccessController.doPrivileged((PrivilegedAction<ClassLoader>)
213                () -> new URLClassLoader(new URL[] {pluginJarUrl}, Plugin.class.getClassLoader()));
214    }
215}
Note: See TracBrowser for help on using the repository browser.