source: josm/trunk/src/org/openstreetmap/josm/plugins/PluginProxy.java@ 12517

Last change on this file since 12517 was 12322, checked in by bastiK, 7 years ago

fixed #14901 - restrict plugin classpath

Separate class loader for each plugin instead of unified class loader.

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins;
3
4import java.util.List;
5
6import org.openstreetmap.josm.Main;
7import org.openstreetmap.josm.gui.MapFrame;
8import org.openstreetmap.josm.gui.download.DownloadSelection;
9import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
10import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
11
12/**
13 * Helper class for the JOSM system to communicate with the plugin.
14 *
15 * This class should be of no interest for sole plugin writer.
16 *
17 * @author Immanuel.Scholz
18 */
19public class PluginProxy extends Plugin {
20
21 /**
22 * The plugin.
23 */
24 private final Object plugin;
25 private final PluginClassLoader classLoader;
26
27 /**
28 * Constructs a new {@code PluginProxy}.
29 * @param plugin the plugin
30 * @param info the associated plugin info
31 * @param classLoader the class loader for the plugin
32 * @since 12322
33 */
34 public PluginProxy(Object plugin, PluginInformation info, PluginClassLoader classLoader) {
35 super(info);
36 this.plugin = plugin;
37 this.classLoader = classLoader;
38 }
39
40 /**
41 * Get the plugin object.
42 * @return the plugin object
43 * @since 12322
44 */
45 public Object getPlugin() {
46 return plugin;
47 }
48
49 /**
50 * Get the class loader for the plugin.
51 * @return the plugin class loader
52 * @since 12322
53 */
54 public PluginClassLoader getClassLoader() {
55 return classLoader;
56 }
57
58 private void handlePluginException(Exception e) {
59 PluginHandler.pluginLoadingExceptions.put(getPluginInformation().name, e);
60 BugReportExceptionHandler.handleException(new PluginException(this, getPluginInformation().name, e));
61 }
62
63 @Override
64 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
65 try {
66 plugin.getClass().getMethod("mapFrameInitialized", MapFrame.class, MapFrame.class).invoke(plugin, oldFrame, newFrame);
67 } catch (NoSuchMethodException e) {
68 Main.trace(e);
69 Main.debug("Plugin "+plugin+" does not define mapFrameInitialized");
70 } catch (ReflectiveOperationException | IllegalArgumentException e) {
71 handlePluginException(e);
72 }
73 }
74
75 @Override
76 public PreferenceSetting getPreferenceSetting() {
77 try {
78 return (PreferenceSetting) plugin.getClass().getMethod("getPreferenceSetting").invoke(plugin);
79 } catch (NoSuchMethodException e) {
80 Main.trace(e);
81 Main.debug("Plugin "+plugin+" does not define getPreferenceSetting");
82 return null;
83 } catch (ReflectiveOperationException | IllegalArgumentException e) {
84 handlePluginException(e);
85 }
86 return null;
87 }
88
89 @Override
90 public void addDownloadSelection(List<DownloadSelection> list) {
91 try {
92 plugin.getClass().getMethod("addDownloadSelection", List.class).invoke(plugin, list);
93 } catch (NoSuchMethodException e) {
94 Main.trace(e);
95 Main.debug("Plugin "+plugin+" does not define addDownloadSelection");
96 } catch (ReflectiveOperationException | IllegalArgumentException e) {
97 handlePluginException(e);
98 }
99 }
100}
Note: See TracBrowser for help on using the repository browser.