1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Font;
|
---|
10 | import java.awt.GraphicsEnvironment;
|
---|
11 | import java.awt.GridBagConstraints;
|
---|
12 | import java.awt.GridBagLayout;
|
---|
13 | import java.awt.Insets;
|
---|
14 | import java.awt.event.ActionEvent;
|
---|
15 | import java.io.File;
|
---|
16 | import java.io.FilenameFilter;
|
---|
17 | import java.io.IOException;
|
---|
18 | import java.net.URL;
|
---|
19 | import java.net.URLClassLoader;
|
---|
20 | import java.security.AccessController;
|
---|
21 | import java.security.PrivilegedAction;
|
---|
22 | import java.util.ArrayList;
|
---|
23 | import java.util.Arrays;
|
---|
24 | import java.util.Collection;
|
---|
25 | import java.util.Collections;
|
---|
26 | import java.util.Comparator;
|
---|
27 | import java.util.HashMap;
|
---|
28 | import java.util.HashSet;
|
---|
29 | import java.util.Iterator;
|
---|
30 | import java.util.LinkedList;
|
---|
31 | import java.util.List;
|
---|
32 | import java.util.Locale;
|
---|
33 | import java.util.Map;
|
---|
34 | import java.util.Map.Entry;
|
---|
35 | import java.util.Set;
|
---|
36 | import java.util.TreeSet;
|
---|
37 | import java.util.concurrent.ExecutionException;
|
---|
38 | import java.util.concurrent.FutureTask;
|
---|
39 | import java.util.concurrent.TimeUnit;
|
---|
40 | import java.util.jar.JarFile;
|
---|
41 | import java.util.stream.Collectors;
|
---|
42 |
|
---|
43 | import javax.swing.AbstractAction;
|
---|
44 | import javax.swing.BorderFactory;
|
---|
45 | import javax.swing.Box;
|
---|
46 | import javax.swing.JButton;
|
---|
47 | import javax.swing.JCheckBox;
|
---|
48 | import javax.swing.JLabel;
|
---|
49 | import javax.swing.JOptionPane;
|
---|
50 | import javax.swing.JPanel;
|
---|
51 | import javax.swing.JScrollPane;
|
---|
52 | import javax.swing.UIManager;
|
---|
53 |
|
---|
54 | import org.openstreetmap.josm.Main;
|
---|
55 | import org.openstreetmap.josm.actions.RestartAction;
|
---|
56 | import org.openstreetmap.josm.data.Version;
|
---|
57 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
58 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
59 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
60 | import org.openstreetmap.josm.gui.download.DownloadSelection;
|
---|
61 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
62 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
63 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
64 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
65 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
66 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
67 | import org.openstreetmap.josm.io.OfflineAccessException;
|
---|
68 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
69 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
70 | import org.openstreetmap.josm.tools.GBC;
|
---|
71 | import org.openstreetmap.josm.tools.I18n;
|
---|
72 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
73 | import org.openstreetmap.josm.tools.Logging;
|
---|
74 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
75 | import org.openstreetmap.josm.tools.Utils;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * PluginHandler is basically a collection of static utility functions used to bootstrap
|
---|
79 | * and manage the loaded plugins.
|
---|
80 | * @since 1326
|
---|
81 | */
|
---|
82 | public final class PluginHandler {
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Deprecated plugins that are removed on start
|
---|
86 | */
|
---|
87 | static final Collection<DeprecatedPlugin> DEPRECATED_PLUGINS;
|
---|
88 | static {
|
---|
89 | String inCore = tr("integrated into main program");
|
---|
90 |
|
---|
91 | DEPRECATED_PLUGINS = Arrays.asList(
|
---|
92 | new DeprecatedPlugin("mappaint", inCore),
|
---|
93 | new DeprecatedPlugin("unglueplugin", inCore),
|
---|
94 | new DeprecatedPlugin("lang-de", inCore),
|
---|
95 | new DeprecatedPlugin("lang-en_GB", inCore),
|
---|
96 | new DeprecatedPlugin("lang-fr", inCore),
|
---|
97 | new DeprecatedPlugin("lang-it", inCore),
|
---|
98 | new DeprecatedPlugin("lang-pl", inCore),
|
---|
99 | new DeprecatedPlugin("lang-ro", inCore),
|
---|
100 | new DeprecatedPlugin("lang-ru", inCore),
|
---|
101 | new DeprecatedPlugin("ewmsplugin", inCore),
|
---|
102 | new DeprecatedPlugin("ywms", inCore),
|
---|
103 | new DeprecatedPlugin("tways-0.2", inCore),
|
---|
104 | new DeprecatedPlugin("geotagged", inCore),
|
---|
105 | new DeprecatedPlugin("landsat", tr("replaced by new {0} plugin", "lakewalker")),
|
---|
106 | new DeprecatedPlugin("namefinder", inCore),
|
---|
107 | new DeprecatedPlugin("waypoints", inCore),
|
---|
108 | new DeprecatedPlugin("slippy_map_chooser", inCore),
|
---|
109 | new DeprecatedPlugin("tcx-support", tr("replaced by new {0} plugin", "dataimport")),
|
---|
110 | new DeprecatedPlugin("usertools", inCore),
|
---|
111 | new DeprecatedPlugin("AgPifoJ", inCore),
|
---|
112 | new DeprecatedPlugin("utilsplugin", inCore),
|
---|
113 | new DeprecatedPlugin("ghost", inCore),
|
---|
114 | new DeprecatedPlugin("validator", inCore),
|
---|
115 | new DeprecatedPlugin("multipoly", inCore),
|
---|
116 | new DeprecatedPlugin("multipoly-convert", inCore),
|
---|
117 | new DeprecatedPlugin("remotecontrol", inCore),
|
---|
118 | new DeprecatedPlugin("imagery", inCore),
|
---|
119 | new DeprecatedPlugin("slippymap", inCore),
|
---|
120 | new DeprecatedPlugin("wmsplugin", inCore),
|
---|
121 | new DeprecatedPlugin("ParallelWay", inCore),
|
---|
122 | new DeprecatedPlugin("dumbutils", tr("replaced by new {0} plugin", "utilsplugin2")),
|
---|
123 | new DeprecatedPlugin("ImproveWayAccuracy", inCore),
|
---|
124 | new DeprecatedPlugin("Curves", tr("replaced by new {0} plugin", "utilsplugin2")),
|
---|
125 | new DeprecatedPlugin("epsg31287", inCore),
|
---|
126 | new DeprecatedPlugin("licensechange", tr("no longer required")),
|
---|
127 | new DeprecatedPlugin("restart", inCore),
|
---|
128 | new DeprecatedPlugin("wayselector", inCore),
|
---|
129 | new DeprecatedPlugin("openstreetbugs", inCore),
|
---|
130 | new DeprecatedPlugin("nearclick", tr("no longer required")),
|
---|
131 | new DeprecatedPlugin("notes", inCore),
|
---|
132 | new DeprecatedPlugin("mirrored_download", inCore),
|
---|
133 | new DeprecatedPlugin("ImageryCache", inCore),
|
---|
134 | new DeprecatedPlugin("commons-imaging", tr("replaced by new {0} plugin", "apache-commons")),
|
---|
135 | new DeprecatedPlugin("missingRoads", tr("replaced by new {0} plugin", "ImproveOsm")),
|
---|
136 | new DeprecatedPlugin("trafficFlowDirection", tr("replaced by new {0} plugin", "ImproveOsm")),
|
---|
137 | new DeprecatedPlugin("kendzi3d-jogl", tr("replaced by new {0} plugin", "jogl")),
|
---|
138 | new DeprecatedPlugin("josm-geojson", tr("replaced by new {0} plugin", "geojson")),
|
---|
139 | new DeprecatedPlugin("proj4j", inCore),
|
---|
140 | new DeprecatedPlugin("OpenStreetView", tr("replaced by new {0} plugin", "OpenStreetCam")),
|
---|
141 | new DeprecatedPlugin("imageryadjust", inCore)
|
---|
142 | );
|
---|
143 | }
|
---|
144 |
|
---|
145 | private PluginHandler() {
|
---|
146 | // Hide default constructor for utils classes
|
---|
147 | }
|
---|
148 |
|
---|
149 | static final class PluginInformationAction extends AbstractAction {
|
---|
150 | private final PluginInformation info;
|
---|
151 |
|
---|
152 | PluginInformationAction(PluginInformation info) {
|
---|
153 | super(tr("Information"));
|
---|
154 | this.info = info;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Returns plugin information text.
|
---|
159 | * @return plugin information text
|
---|
160 | */
|
---|
161 | public String getText() {
|
---|
162 | StringBuilder b = new StringBuilder();
|
---|
163 | for (Entry<String, String> e : info.attr.entrySet()) {
|
---|
164 | b.append(e.getKey());
|
---|
165 | b.append(": ");
|
---|
166 | b.append(e.getValue());
|
---|
167 | b.append('\n');
|
---|
168 | }
|
---|
169 | return b.toString();
|
---|
170 | }
|
---|
171 |
|
---|
172 | @Override
|
---|
173 | public void actionPerformed(ActionEvent event) {
|
---|
174 | String text = getText();
|
---|
175 | JosmTextArea a = new JosmTextArea(10, 40);
|
---|
176 | a.setEditable(false);
|
---|
177 | a.setText(text);
|
---|
178 | a.setCaretPosition(0);
|
---|
179 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
180 | JOptionPane.showMessageDialog(Main.parent, new JScrollPane(a), tr("Plugin information"),
|
---|
181 | JOptionPane.INFORMATION_MESSAGE);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Description of a deprecated plugin
|
---|
188 | */
|
---|
189 | public static class DeprecatedPlugin implements Comparable<DeprecatedPlugin> {
|
---|
190 | /** Plugin name */
|
---|
191 | public final String name;
|
---|
192 | /** Short explanation about deprecation, can be {@code null} */
|
---|
193 | public final String reason;
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Constructs a new {@code DeprecatedPlugin} with a given reason.
|
---|
197 | * @param name The plugin name
|
---|
198 | * @param reason The reason about deprecation
|
---|
199 | */
|
---|
200 | public DeprecatedPlugin(String name, String reason) {
|
---|
201 | this.name = name;
|
---|
202 | this.reason = reason;
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | public int hashCode() {
|
---|
207 | final int prime = 31;
|
---|
208 | int result = prime + ((name == null) ? 0 : name.hashCode());
|
---|
209 | return prime * result + ((reason == null) ? 0 : reason.hashCode());
|
---|
210 | }
|
---|
211 |
|
---|
212 | @Override
|
---|
213 | public boolean equals(Object obj) {
|
---|
214 | if (this == obj)
|
---|
215 | return true;
|
---|
216 | if (obj == null)
|
---|
217 | return false;
|
---|
218 | if (getClass() != obj.getClass())
|
---|
219 | return false;
|
---|
220 | DeprecatedPlugin other = (DeprecatedPlugin) obj;
|
---|
221 | if (name == null) {
|
---|
222 | if (other.name != null)
|
---|
223 | return false;
|
---|
224 | } else if (!name.equals(other.name))
|
---|
225 | return false;
|
---|
226 | if (reason == null) {
|
---|
227 | if (other.reason != null)
|
---|
228 | return false;
|
---|
229 | } else if (!reason.equals(other.reason))
|
---|
230 | return false;
|
---|
231 | return true;
|
---|
232 | }
|
---|
233 |
|
---|
234 | @Override
|
---|
235 | public int compareTo(DeprecatedPlugin o) {
|
---|
236 | int d = name.compareTo(o.name);
|
---|
237 | if (d == 0)
|
---|
238 | d = reason.compareTo(o.reason);
|
---|
239 | return d;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * ClassLoader that makes the addURL method of URLClassLoader public.
|
---|
245 | *
|
---|
246 | * Like URLClassLoader, but allows to add more URLs after construction.
|
---|
247 | */
|
---|
248 | public static class DynamicURLClassLoader extends URLClassLoader {
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Constructs a new {@code DynamicURLClassLoader}.
|
---|
252 | * @param urls the URLs from which to load classes and resources
|
---|
253 | * @param parent the parent class loader for delegation
|
---|
254 | */
|
---|
255 | public DynamicURLClassLoader(URL[] urls, ClassLoader parent) {
|
---|
256 | super(urls, parent);
|
---|
257 | }
|
---|
258 |
|
---|
259 | @Override
|
---|
260 | public void addURL(URL url) {
|
---|
261 | super.addURL(url);
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * List of unmaintained plugins. Not really up-to-date as the vast majority of plugins are not maintained after a few months, sadly...
|
---|
267 | */
|
---|
268 | static final List<String> UNMAINTAINED_PLUGINS = Collections.unmodifiableList(Arrays.asList(
|
---|
269 | "NanoLog", // See https://trac.openstreetmap.org/changeset/29404/subversion
|
---|
270 | "irsrectify", // See https://trac.openstreetmap.org/changeset/29404/subversion
|
---|
271 | "surveyor2", // See https://trac.openstreetmap.org/changeset/29404/subversion
|
---|
272 | "gpsbabelgui",
|
---|
273 | "Intersect_way",
|
---|
274 | "ContourOverlappingMerge", // See #11202, #11518, https://github.com/bularcasergiu/ContourOverlappingMerge/issues/1
|
---|
275 | "LaneConnector", // See #11468, #11518, https://github.com/TrifanAdrian/LanecConnectorPlugin/issues/1
|
---|
276 | "Remove.redundant.points" // See #11468, #11518, https://github.com/bularcasergiu/RemoveRedundantPoints (not even created an issue...)
|
---|
277 | ));
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Default time-based update interval, in days (pluginmanager.time-based-update.interval)
|
---|
281 | */
|
---|
282 | public static final int DEFAULT_TIME_BASED_UPDATE_INTERVAL = 30;
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * All installed and loaded plugins (resp. their main classes)
|
---|
286 | */
|
---|
287 | static final Collection<PluginProxy> pluginList = new LinkedList<>();
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * All exceptions that occured during plugin loading
|
---|
291 | */
|
---|
292 | static final Map<String, Throwable> pluginLoadingExceptions = new HashMap<>();
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Class loader to locate resources from plugins.
|
---|
296 | * @see #getJoinedPluginResourceCL()
|
---|
297 | */
|
---|
298 | private static DynamicURLClassLoader joinedPluginResourceCL;
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Add here all ClassLoader whose resource should be searched.
|
---|
302 | */
|
---|
303 | private static final List<ClassLoader> sources = new LinkedList<>();
|
---|
304 | static {
|
---|
305 | try {
|
---|
306 | sources.add(ClassLoader.getSystemClassLoader());
|
---|
307 | sources.add(org.openstreetmap.josm.gui.MainApplication.class.getClassLoader());
|
---|
308 | } catch (SecurityException ex) {
|
---|
309 | Logging.debug(ex);
|
---|
310 | sources.add(ImageProvider.class.getClassLoader());
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | private static PluginDownloadTask pluginDownloadTask;
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Returns the list of currently installed and loaded plugins.
|
---|
318 | * @return the list of currently installed and loaded plugins
|
---|
319 | * @since 10982
|
---|
320 | */
|
---|
321 | public static List<PluginInformation> getPlugins() {
|
---|
322 | return pluginList.stream().map(PluginProxy::getPluginInformation).collect(Collectors.toList());
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Returns all ClassLoaders whose resource should be searched.
|
---|
327 | * @return all ClassLoaders whose resource should be searched
|
---|
328 | */
|
---|
329 | public static Collection<ClassLoader> getResourceClassLoaders() {
|
---|
330 | return Collections.unmodifiableCollection(sources);
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Removes deprecated plugins from a collection of plugins. Modifies the
|
---|
335 | * collection <code>plugins</code>.
|
---|
336 | *
|
---|
337 | * Also notifies the user about removed deprecated plugins
|
---|
338 | *
|
---|
339 | * @param parent The parent Component used to display warning popup
|
---|
340 | * @param plugins the collection of plugins
|
---|
341 | */
|
---|
342 | static void filterDeprecatedPlugins(Component parent, Collection<String> plugins) {
|
---|
343 | Set<DeprecatedPlugin> removedPlugins = new TreeSet<>();
|
---|
344 | for (DeprecatedPlugin depr : DEPRECATED_PLUGINS) {
|
---|
345 | if (plugins.contains(depr.name)) {
|
---|
346 | plugins.remove(depr.name);
|
---|
347 | Main.pref.removeFromCollection("plugins", depr.name);
|
---|
348 | removedPlugins.add(depr);
|
---|
349 | }
|
---|
350 | }
|
---|
351 | if (removedPlugins.isEmpty())
|
---|
352 | return;
|
---|
353 |
|
---|
354 | // notify user about removed deprecated plugins
|
---|
355 | //
|
---|
356 | StringBuilder sb = new StringBuilder(32);
|
---|
357 | sb.append("<html>")
|
---|
358 | .append(trn(
|
---|
359 | "The following plugin is no longer necessary and has been deactivated:",
|
---|
360 | "The following plugins are no longer necessary and have been deactivated:",
|
---|
361 | removedPlugins.size()))
|
---|
362 | .append("<ul>");
|
---|
363 | for (DeprecatedPlugin depr: removedPlugins) {
|
---|
364 | sb.append("<li>").append(depr.name);
|
---|
365 | if (depr.reason != null) {
|
---|
366 | sb.append(" (").append(depr.reason).append(')');
|
---|
367 | }
|
---|
368 | sb.append("</li>");
|
---|
369 | }
|
---|
370 | sb.append("</ul></html>");
|
---|
371 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
372 | JOptionPane.showMessageDialog(
|
---|
373 | parent,
|
---|
374 | sb.toString(),
|
---|
375 | tr("Warning"),
|
---|
376 | JOptionPane.WARNING_MESSAGE
|
---|
377 | );
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * Removes unmaintained plugins from a collection of plugins. Modifies the
|
---|
383 | * collection <code>plugins</code>. Also removes the plugin from the list
|
---|
384 | * of plugins in the preferences, if necessary.
|
---|
385 | *
|
---|
386 | * Asks the user for every unmaintained plugin whether it should be removed.
|
---|
387 | * @param parent The parent Component used to display warning popup
|
---|
388 | *
|
---|
389 | * @param plugins the collection of plugins
|
---|
390 | */
|
---|
391 | static void filterUnmaintainedPlugins(Component parent, Collection<String> plugins) {
|
---|
392 | for (String unmaintained : UNMAINTAINED_PLUGINS) {
|
---|
393 | if (!plugins.contains(unmaintained)) {
|
---|
394 | continue;
|
---|
395 | }
|
---|
396 | String msg = tr("<html>Loading of the plugin \"{0}\" was requested."
|
---|
397 | + "<br>This plugin is no longer developed and very likely will produce errors."
|
---|
398 | +"<br>It should be disabled.<br>Delete from preferences?</html>",
|
---|
399 | Utils.escapeReservedCharactersHTML(unmaintained));
|
---|
400 | if (confirmDisablePlugin(parent, msg, unmaintained)) {
|
---|
401 | Main.pref.removeFromCollection("plugins", unmaintained);
|
---|
402 | plugins.remove(unmaintained);
|
---|
403 | }
|
---|
404 | }
|
---|
405 | }
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Checks whether the locally available plugins should be updated and
|
---|
409 | * asks the user if running an update is OK. An update is advised if
|
---|
410 | * JOSM was updated to a new version since the last plugin updates or
|
---|
411 | * if the plugins were last updated a long time ago.
|
---|
412 | *
|
---|
413 | * @param parent the parent component relative to which the confirmation dialog
|
---|
414 | * is to be displayed
|
---|
415 | * @return true if a plugin update should be run; false, otherwise
|
---|
416 | */
|
---|
417 | public static boolean checkAndConfirmPluginUpdate(Component parent) {
|
---|
418 | if (!checkOfflineAccess()) {
|
---|
419 | Logging.info(tr("{0} not available (offline mode)", tr("Plugin update")));
|
---|
420 | return false;
|
---|
421 | }
|
---|
422 | String message = null;
|
---|
423 | String togglePreferenceKey = null;
|
---|
424 | int v = Version.getInstance().getVersion();
|
---|
425 | if (Config.getPref().getInt("pluginmanager.version", 0) < v) {
|
---|
426 | message =
|
---|
427 | "<html>"
|
---|
428 | + tr("You updated your JOSM software.<br>"
|
---|
429 | + "To prevent problems the plugins should be updated as well.<br><br>"
|
---|
430 | + "Update plugins now?"
|
---|
431 | )
|
---|
432 | + "</html>";
|
---|
433 | togglePreferenceKey = "pluginmanager.version-based-update.policy";
|
---|
434 | } else {
|
---|
435 | long tim = System.currentTimeMillis();
|
---|
436 | long last = Config.getPref().getLong("pluginmanager.lastupdate", 0);
|
---|
437 | Integer maxTime = Config.getPref().getInt("pluginmanager.time-based-update.interval", DEFAULT_TIME_BASED_UPDATE_INTERVAL);
|
---|
438 | long d = TimeUnit.MILLISECONDS.toDays(tim - last);
|
---|
439 | if ((last <= 0) || (maxTime <= 0)) {
|
---|
440 | Config.getPref().put("pluginmanager.lastupdate", Long.toString(tim));
|
---|
441 | } else if (d > maxTime) {
|
---|
442 | message =
|
---|
443 | "<html>"
|
---|
444 | + tr("Last plugin update more than {0} days ago.", d)
|
---|
445 | + "</html>";
|
---|
446 | togglePreferenceKey = "pluginmanager.time-based-update.policy";
|
---|
447 | }
|
---|
448 | }
|
---|
449 | if (message == null) return false;
|
---|
450 |
|
---|
451 | UpdatePluginsMessagePanel pnlMessage = new UpdatePluginsMessagePanel();
|
---|
452 | pnlMessage.setMessage(message);
|
---|
453 | pnlMessage.initDontShowAgain(togglePreferenceKey);
|
---|
454 |
|
---|
455 | // check whether automatic update at startup was disabled
|
---|
456 | //
|
---|
457 | String policy = Config.getPref().get(togglePreferenceKey, "ask").trim().toLowerCase(Locale.ENGLISH);
|
---|
458 | switch(policy) {
|
---|
459 | case "never":
|
---|
460 | if ("pluginmanager.version-based-update.policy".equals(togglePreferenceKey)) {
|
---|
461 | Logging.info(tr("Skipping plugin update after JOSM upgrade. Automatic update at startup is disabled."));
|
---|
462 | } else if ("pluginmanager.time-based-update.policy".equals(togglePreferenceKey)) {
|
---|
463 | Logging.info(tr("Skipping plugin update after elapsed update interval. Automatic update at startup is disabled."));
|
---|
464 | }
|
---|
465 | return false;
|
---|
466 |
|
---|
467 | case "always":
|
---|
468 | if ("pluginmanager.version-based-update.policy".equals(togglePreferenceKey)) {
|
---|
469 | Logging.info(tr("Running plugin update after JOSM upgrade. Automatic update at startup is enabled."));
|
---|
470 | } else if ("pluginmanager.time-based-update.policy".equals(togglePreferenceKey)) {
|
---|
471 | Logging.info(tr("Running plugin update after elapsed update interval. Automatic update at startup is disabled."));
|
---|
472 | }
|
---|
473 | return true;
|
---|
474 |
|
---|
475 | case "ask":
|
---|
476 | break;
|
---|
477 |
|
---|
478 | default:
|
---|
479 | Logging.warn(tr("Unexpected value ''{0}'' for preference ''{1}''. Assuming value ''ask''.", policy, togglePreferenceKey));
|
---|
480 | }
|
---|
481 |
|
---|
482 | ButtonSpec[] options = new ButtonSpec[] {
|
---|
483 | new ButtonSpec(
|
---|
484 | tr("Update plugins"),
|
---|
485 | ImageProvider.get("dialogs", "refresh"),
|
---|
486 | tr("Click to update the activated plugins"),
|
---|
487 | null /* no specific help context */
|
---|
488 | ),
|
---|
489 | new ButtonSpec(
|
---|
490 | tr("Skip update"),
|
---|
491 | ImageProvider.get("cancel"),
|
---|
492 | tr("Click to skip updating the activated plugins"),
|
---|
493 | null /* no specific help context */
|
---|
494 | )
|
---|
495 | };
|
---|
496 |
|
---|
497 | int ret = HelpAwareOptionPane.showOptionDialog(
|
---|
498 | parent,
|
---|
499 | pnlMessage,
|
---|
500 | tr("Update plugins"),
|
---|
501 | JOptionPane.WARNING_MESSAGE,
|
---|
502 | null,
|
---|
503 | options,
|
---|
504 | options[0],
|
---|
505 | ht("/Preferences/Plugins#AutomaticUpdate")
|
---|
506 | );
|
---|
507 |
|
---|
508 | if (pnlMessage.isRememberDecision()) {
|
---|
509 | switch(ret) {
|
---|
510 | case 0:
|
---|
511 | Config.getPref().put(togglePreferenceKey, "always");
|
---|
512 | break;
|
---|
513 | case JOptionPane.CLOSED_OPTION:
|
---|
514 | case 1:
|
---|
515 | Config.getPref().put(togglePreferenceKey, "never");
|
---|
516 | break;
|
---|
517 | default: // Do nothing
|
---|
518 | }
|
---|
519 | } else {
|
---|
520 | Config.getPref().put(togglePreferenceKey, "ask");
|
---|
521 | }
|
---|
522 | return ret == 0;
|
---|
523 | }
|
---|
524 |
|
---|
525 | private static boolean checkOfflineAccess() {
|
---|
526 | if (Main.isOffline(OnlineResource.ALL)) {
|
---|
527 | return false;
|
---|
528 | }
|
---|
529 | if (Main.isOffline(OnlineResource.JOSM_WEBSITE)) {
|
---|
530 | for (String updateSite : Main.pref.getPluginSites()) {
|
---|
531 | try {
|
---|
532 | OnlineResource.JOSM_WEBSITE.checkOfflineAccess(updateSite, Main.getJOSMWebsite());
|
---|
533 | } catch (OfflineAccessException e) {
|
---|
534 | Logging.trace(e);
|
---|
535 | return false;
|
---|
536 | }
|
---|
537 | }
|
---|
538 | }
|
---|
539 | return true;
|
---|
540 | }
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * Alerts the user if a plugin required by another plugin is missing, and offer to download them & restart JOSM
|
---|
544 | *
|
---|
545 | * @param parent The parent Component used to display error popup
|
---|
546 | * @param plugin the plugin
|
---|
547 | * @param missingRequiredPlugin the missing required plugin
|
---|
548 | */
|
---|
549 | private static void alertMissingRequiredPlugin(Component parent, String plugin, Set<String> missingRequiredPlugin) {
|
---|
550 | StringBuilder sb = new StringBuilder(48);
|
---|
551 | sb.append("<html>")
|
---|
552 | .append(trn("Plugin {0} requires a plugin which was not found. The missing plugin is:",
|
---|
553 | "Plugin {0} requires {1} plugins which were not found. The missing plugins are:",
|
---|
554 | missingRequiredPlugin.size(),
|
---|
555 | Utils.escapeReservedCharactersHTML(plugin),
|
---|
556 | missingRequiredPlugin.size()))
|
---|
557 | .append(Utils.joinAsHtmlUnorderedList(missingRequiredPlugin))
|
---|
558 | .append("</html>");
|
---|
559 | ButtonSpec[] specs = new ButtonSpec[] {
|
---|
560 | new ButtonSpec(
|
---|
561 | tr("Download and restart"),
|
---|
562 | ImageProvider.get("restart"),
|
---|
563 | trn("Click to download missing plugin and restart JOSM",
|
---|
564 | "Click to download missing plugins and restart JOSM",
|
---|
565 | missingRequiredPlugin.size()),
|
---|
566 | null /* no specific help text */
|
---|
567 | ),
|
---|
568 | new ButtonSpec(
|
---|
569 | tr("Continue"),
|
---|
570 | ImageProvider.get("ok"),
|
---|
571 | trn("Click to continue without this plugin",
|
---|
572 | "Click to continue without these plugins",
|
---|
573 | missingRequiredPlugin.size()),
|
---|
574 | null /* no specific help text */
|
---|
575 | )
|
---|
576 | };
|
---|
577 | if (0 == HelpAwareOptionPane.showOptionDialog(
|
---|
578 | parent,
|
---|
579 | sb.toString(),
|
---|
580 | tr("Error"),
|
---|
581 | JOptionPane.ERROR_MESSAGE,
|
---|
582 | null, /* no special icon */
|
---|
583 | specs,
|
---|
584 | specs[0],
|
---|
585 | ht("/Plugin/Loading#MissingRequiredPlugin"))) {
|
---|
586 | downloadRequiredPluginsAndRestart(parent, missingRequiredPlugin);
|
---|
587 | }
|
---|
588 | }
|
---|
589 |
|
---|
590 | private static void downloadRequiredPluginsAndRestart(final Component parent, final Set<String> missingRequiredPlugin) {
|
---|
591 | // Update plugin list
|
---|
592 | final ReadRemotePluginInformationTask pluginInfoDownloadTask = new ReadRemotePluginInformationTask(
|
---|
593 | Main.pref.getOnlinePluginSites());
|
---|
594 | MainApplication.worker.submit(pluginInfoDownloadTask);
|
---|
595 |
|
---|
596 | // Continuation
|
---|
597 | MainApplication.worker.submit(() -> {
|
---|
598 | // Build list of plugins to download
|
---|
599 | Set<PluginInformation> toDownload = new HashSet<>(pluginInfoDownloadTask.getAvailablePlugins());
|
---|
600 | toDownload.removeIf(info -> !missingRequiredPlugin.contains(info.getName()));
|
---|
601 | // Check if something has still to be downloaded
|
---|
602 | if (!toDownload.isEmpty()) {
|
---|
603 | // download plugins
|
---|
604 | final PluginDownloadTask task = new PluginDownloadTask(parent, toDownload, tr("Download plugins"));
|
---|
605 | MainApplication.worker.submit(task);
|
---|
606 | MainApplication.worker.submit(() -> {
|
---|
607 | // restart if some plugins have been downloaded
|
---|
608 | if (!task.getDownloadedPlugins().isEmpty()) {
|
---|
609 | // update plugin list in preferences
|
---|
610 | Set<String> plugins = new HashSet<>(Config.getPref().getList("plugins"));
|
---|
611 | for (PluginInformation plugin : task.getDownloadedPlugins()) {
|
---|
612 | plugins.add(plugin.name);
|
---|
613 | }
|
---|
614 | Config.getPref().putList("plugins", new ArrayList<>(plugins));
|
---|
615 | // restart
|
---|
616 | try {
|
---|
617 | RestartAction.restartJOSM();
|
---|
618 | } catch (IOException e) {
|
---|
619 | Logging.error(e);
|
---|
620 | }
|
---|
621 | } else {
|
---|
622 | Logging.warn("No plugin downloaded, restart canceled");
|
---|
623 | }
|
---|
624 | });
|
---|
625 | } else {
|
---|
626 | Logging.warn("No plugin to download, operation canceled");
|
---|
627 | }
|
---|
628 | });
|
---|
629 | }
|
---|
630 |
|
---|
631 | private static void alertJOSMUpdateRequired(Component parent, String plugin, int requiredVersion) {
|
---|
632 | HelpAwareOptionPane.showOptionDialog(
|
---|
633 | parent,
|
---|
634 | tr("<html>Plugin {0} requires JOSM version {1}. The current JOSM version is {2}.<br>"
|
---|
635 | +"You have to update JOSM in order to use this plugin.</html>",
|
---|
636 | plugin, Integer.toString(requiredVersion), Version.getInstance().getVersionString()
|
---|
637 | ),
|
---|
638 | tr("Warning"),
|
---|
639 | JOptionPane.WARNING_MESSAGE,
|
---|
640 | ht("/Plugin/Loading#JOSMUpdateRequired")
|
---|
641 | );
|
---|
642 | }
|
---|
643 |
|
---|
644 | /**
|
---|
645 | * Checks whether all preconditions for loading the plugin <code>plugin</code> are met. The
|
---|
646 | * current JOSM version must be compatible with the plugin and no other plugins this plugin
|
---|
647 | * depends on should be missing.
|
---|
648 | *
|
---|
649 | * @param parent The parent Component used to display error popup
|
---|
650 | * @param plugins the collection of all loaded plugins
|
---|
651 | * @param plugin the plugin for which preconditions are checked
|
---|
652 | * @return true, if the preconditions are met; false otherwise
|
---|
653 | */
|
---|
654 | public static boolean checkLoadPreconditions(Component parent, Collection<PluginInformation> plugins, PluginInformation plugin) {
|
---|
655 |
|
---|
656 | // make sure the plugin is compatible with the current JOSM version
|
---|
657 | //
|
---|
658 | int josmVersion = Version.getInstance().getVersion();
|
---|
659 | if (plugin.localmainversion > josmVersion && josmVersion != Version.JOSM_UNKNOWN_VERSION) {
|
---|
660 | alertJOSMUpdateRequired(parent, plugin.name, plugin.localmainversion);
|
---|
661 | return false;
|
---|
662 | }
|
---|
663 |
|
---|
664 | // Add all plugins already loaded (to include early plugins when checking late ones)
|
---|
665 | Collection<PluginInformation> allPlugins = new HashSet<>(plugins);
|
---|
666 | for (PluginProxy proxy : pluginList) {
|
---|
667 | allPlugins.add(proxy.getPluginInformation());
|
---|
668 | }
|
---|
669 |
|
---|
670 | return checkRequiredPluginsPreconditions(parent, allPlugins, plugin, true);
|
---|
671 | }
|
---|
672 |
|
---|
673 | /**
|
---|
674 | * Checks if required plugins preconditions for loading the plugin <code>plugin</code> are met.
|
---|
675 | * No other plugins this plugin depends on should be missing.
|
---|
676 | *
|
---|
677 | * @param parent The parent Component used to display error popup. If parent is
|
---|
678 | * null, the error popup is suppressed
|
---|
679 | * @param plugins the collection of all loaded plugins
|
---|
680 | * @param plugin the plugin for which preconditions are checked
|
---|
681 | * @param local Determines if the local or up-to-date plugin dependencies are to be checked.
|
---|
682 | * @return true, if the preconditions are met; false otherwise
|
---|
683 | * @since 5601
|
---|
684 | */
|
---|
685 | public static boolean checkRequiredPluginsPreconditions(Component parent, Collection<PluginInformation> plugins,
|
---|
686 | PluginInformation plugin, boolean local) {
|
---|
687 |
|
---|
688 | String requires = local ? plugin.localrequires : plugin.requires;
|
---|
689 |
|
---|
690 | // make sure the dependencies to other plugins are not broken
|
---|
691 | //
|
---|
692 | if (requires != null) {
|
---|
693 | Set<String> pluginNames = new HashSet<>();
|
---|
694 | for (PluginInformation pi: plugins) {
|
---|
695 | pluginNames.add(pi.name);
|
---|
696 | }
|
---|
697 | Set<String> missingPlugins = new HashSet<>();
|
---|
698 | List<String> requiredPlugins = local ? plugin.getLocalRequiredPlugins() : plugin.getRequiredPlugins();
|
---|
699 | for (String requiredPlugin : requiredPlugins) {
|
---|
700 | if (!pluginNames.contains(requiredPlugin)) {
|
---|
701 | missingPlugins.add(requiredPlugin);
|
---|
702 | }
|
---|
703 | }
|
---|
704 | if (!missingPlugins.isEmpty()) {
|
---|
705 | if (parent != null) {
|
---|
706 | alertMissingRequiredPlugin(parent, plugin.name, missingPlugins);
|
---|
707 | }
|
---|
708 | return false;
|
---|
709 | }
|
---|
710 | }
|
---|
711 | return true;
|
---|
712 | }
|
---|
713 |
|
---|
714 | /**
|
---|
715 | * Method to get the (now obsolete) class loader for loading plugin code.
|
---|
716 | *
|
---|
717 | * @return the class loader
|
---|
718 | * @deprecated There is no longer a unified plugin class loader. Use {@link PluginProxy#classLoader}
|
---|
719 | * to get the class loader for each plugin. Or <code>PluginClass.class.getClassLoader()</code>
|
---|
720 | * to access the class loader from within the plugin.
|
---|
721 | */
|
---|
722 | @Deprecated
|
---|
723 | public static synchronized DynamicURLClassLoader getPluginClassLoader() {
|
---|
724 | return getJoinedPluginResourceCL();
|
---|
725 | }
|
---|
726 |
|
---|
727 | /**
|
---|
728 | * Get class loader to locate resources from plugins.
|
---|
729 | *
|
---|
730 | * It joins URLs of all plugins, to find images, etc.
|
---|
731 | * (Not for loading Java classes - each plugin has a separate {@link PluginClassLoader}
|
---|
732 | * for that purpose.)
|
---|
733 | * @return class loader to locate resources from plugins
|
---|
734 | */
|
---|
735 | private static synchronized DynamicURLClassLoader getJoinedPluginResourceCL() {
|
---|
736 | if (joinedPluginResourceCL == null) {
|
---|
737 | joinedPluginResourceCL = AccessController.doPrivileged((PrivilegedAction<DynamicURLClassLoader>)
|
---|
738 | () -> new DynamicURLClassLoader(new URL[0], PluginHandler.class.getClassLoader()));
|
---|
739 | sources.add(0, joinedPluginResourceCL);
|
---|
740 | }
|
---|
741 | return joinedPluginResourceCL;
|
---|
742 | }
|
---|
743 |
|
---|
744 | /**
|
---|
745 | * Add more plugins to the joined plugin resource class loader.
|
---|
746 | *
|
---|
747 | * @param plugins the plugins to add
|
---|
748 | */
|
---|
749 | private static void extendJoinedPluginResourceCL(Collection<PluginInformation> plugins) {
|
---|
750 | // iterate all plugins and collect all libraries of all plugins:
|
---|
751 | File pluginDir = Main.pref.getPluginsDirectory();
|
---|
752 | DynamicURLClassLoader cl = getJoinedPluginResourceCL();
|
---|
753 |
|
---|
754 | for (PluginInformation info : plugins) {
|
---|
755 | if (info.libraries == null) {
|
---|
756 | continue;
|
---|
757 | }
|
---|
758 | for (URL libUrl : info.libraries) {
|
---|
759 | cl.addURL(libUrl);
|
---|
760 | }
|
---|
761 | File pluginJar = new File(pluginDir, info.name + ".jar");
|
---|
762 | I18n.addTexts(pluginJar);
|
---|
763 | URL pluginJarUrl = Utils.fileToURL(pluginJar);
|
---|
764 | cl.addURL(pluginJarUrl);
|
---|
765 | }
|
---|
766 | }
|
---|
767 |
|
---|
768 | /**
|
---|
769 | * Loads and instantiates the plugin described by <code>plugin</code> using
|
---|
770 | * the class loader <code>pluginClassLoader</code>.
|
---|
771 | *
|
---|
772 | * @param parent The parent component to be used for the displayed dialog
|
---|
773 | * @param plugin the plugin
|
---|
774 | * @param pluginClassLoader the plugin class loader
|
---|
775 | */
|
---|
776 | private static void loadPlugin(Component parent, PluginInformation plugin, PluginClassLoader pluginClassLoader) {
|
---|
777 | String msg = tr("Could not load plugin {0}. Delete from preferences?", plugin.name);
|
---|
778 | try {
|
---|
779 | Class<?> klass = plugin.loadClass(pluginClassLoader);
|
---|
780 | if (klass != null) {
|
---|
781 | Logging.info(tr("loading plugin ''{0}'' (version {1})", plugin.name, plugin.localversion));
|
---|
782 | PluginProxy pluginProxy = plugin.load(klass, pluginClassLoader);
|
---|
783 | pluginList.add(pluginProxy);
|
---|
784 | MainApplication.addAndFireMapFrameListener(pluginProxy);
|
---|
785 | }
|
---|
786 | msg = null;
|
---|
787 | } catch (PluginException e) {
|
---|
788 | pluginLoadingExceptions.put(plugin.name, e);
|
---|
789 | Logging.error(e);
|
---|
790 | if (e.getCause() instanceof ClassNotFoundException) {
|
---|
791 | msg = tr("<html>Could not load plugin {0} because the plugin<br>main class ''{1}'' was not found.<br>"
|
---|
792 | + "Delete from preferences?</html>", Utils.escapeReservedCharactersHTML(plugin.name), plugin.className);
|
---|
793 | }
|
---|
794 | } catch (RuntimeException e) { // NOPMD
|
---|
795 | pluginLoadingExceptions.put(plugin.name, e);
|
---|
796 | Logging.error(e);
|
---|
797 | }
|
---|
798 | if (msg != null && confirmDisablePlugin(parent, msg, plugin.name)) {
|
---|
799 | Main.pref.removeFromCollection("plugins", plugin.name);
|
---|
800 | }
|
---|
801 | }
|
---|
802 |
|
---|
803 | /**
|
---|
804 | * Loads the plugin in <code>plugins</code> from locally available jar files into memory.
|
---|
805 | *
|
---|
806 | * @param parent The parent component to be used for the displayed dialog
|
---|
807 | * @param plugins the list of plugins
|
---|
808 | * @param monitor the progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null.
|
---|
809 | */
|
---|
810 | public static void loadPlugins(Component parent, Collection<PluginInformation> plugins, ProgressMonitor monitor) {
|
---|
811 | if (monitor == null) {
|
---|
812 | monitor = NullProgressMonitor.INSTANCE;
|
---|
813 | }
|
---|
814 | try {
|
---|
815 | monitor.beginTask(tr("Loading plugins ..."));
|
---|
816 | monitor.subTask(tr("Checking plugin preconditions..."));
|
---|
817 | List<PluginInformation> toLoad = new LinkedList<>();
|
---|
818 | for (PluginInformation pi: plugins) {
|
---|
819 | if (checkLoadPreconditions(parent, plugins, pi)) {
|
---|
820 | toLoad.add(pi);
|
---|
821 | }
|
---|
822 | }
|
---|
823 | // sort the plugins according to their "staging" equivalence class. The
|
---|
824 | // lower the value of "stage" the earlier the plugin should be loaded.
|
---|
825 | //
|
---|
826 | toLoad.sort(Comparator.comparingInt(o -> o.stage));
|
---|
827 | if (toLoad.isEmpty())
|
---|
828 | return;
|
---|
829 |
|
---|
830 | Map<PluginInformation, PluginClassLoader> classLoaders = new HashMap<>();
|
---|
831 | for (PluginInformation info : toLoad) {
|
---|
832 | PluginClassLoader cl = AccessController.doPrivileged((PrivilegedAction<PluginClassLoader>)
|
---|
833 | () -> new PluginClassLoader(
|
---|
834 | info.libraries.toArray(new URL[0]),
|
---|
835 | PluginHandler.class.getClassLoader(),
|
---|
836 | null));
|
---|
837 | classLoaders.put(info, cl);
|
---|
838 | }
|
---|
839 |
|
---|
840 | // resolve dependencies
|
---|
841 | for (PluginInformation info : toLoad) {
|
---|
842 | PluginClassLoader cl = classLoaders.get(info);
|
---|
843 | DEPENDENCIES:
|
---|
844 | for (String depName : info.getRequiredPlugins()) {
|
---|
845 | for (PluginInformation depInfo : toLoad) {
|
---|
846 | if (depInfo.getName().equals(depName)) {
|
---|
847 | cl.addDependency(classLoaders.get(depInfo));
|
---|
848 | continue DEPENDENCIES;
|
---|
849 | }
|
---|
850 | }
|
---|
851 | for (PluginProxy proxy : pluginList) {
|
---|
852 | if (proxy.getPluginInformation().getName().equals(depName)) {
|
---|
853 | cl.addDependency(proxy.getClassLoader());
|
---|
854 | continue DEPENDENCIES;
|
---|
855 | }
|
---|
856 | }
|
---|
857 | throw new AssertionError("unable to find dependency " + depName + " for plugin " + info.getName());
|
---|
858 | }
|
---|
859 | }
|
---|
860 |
|
---|
861 | extendJoinedPluginResourceCL(toLoad);
|
---|
862 | monitor.setTicksCount(toLoad.size());
|
---|
863 | for (PluginInformation info : toLoad) {
|
---|
864 | monitor.setExtraText(tr("Loading plugin ''{0}''...", info.name));
|
---|
865 | loadPlugin(parent, info, classLoaders.get(info));
|
---|
866 | monitor.worked(1);
|
---|
867 | }
|
---|
868 | } finally {
|
---|
869 | monitor.finishTask();
|
---|
870 | }
|
---|
871 | }
|
---|
872 |
|
---|
873 | /**
|
---|
874 | * Loads plugins from <code>plugins</code> which have the flag {@link PluginInformation#early} set to true.
|
---|
875 | *
|
---|
876 | * @param parent The parent component to be used for the displayed dialog
|
---|
877 | * @param plugins the collection of plugins
|
---|
878 | * @param monitor the progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null.
|
---|
879 | */
|
---|
880 | public static void loadEarlyPlugins(Component parent, Collection<PluginInformation> plugins, ProgressMonitor monitor) {
|
---|
881 | List<PluginInformation> earlyPlugins = new ArrayList<>(plugins.size());
|
---|
882 | for (PluginInformation pi: plugins) {
|
---|
883 | if (pi.early) {
|
---|
884 | earlyPlugins.add(pi);
|
---|
885 | }
|
---|
886 | }
|
---|
887 | loadPlugins(parent, earlyPlugins, monitor);
|
---|
888 | }
|
---|
889 |
|
---|
890 | /**
|
---|
891 | * Loads plugins from <code>plugins</code> which have the flag {@link PluginInformation#early} set to false.
|
---|
892 | *
|
---|
893 | * @param parent The parent component to be used for the displayed dialog
|
---|
894 | * @param plugins the collection of plugins
|
---|
895 | * @param monitor the progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null.
|
---|
896 | */
|
---|
897 | public static void loadLatePlugins(Component parent, Collection<PluginInformation> plugins, ProgressMonitor monitor) {
|
---|
898 | List<PluginInformation> latePlugins = new ArrayList<>(plugins.size());
|
---|
899 | for (PluginInformation pi: plugins) {
|
---|
900 | if (!pi.early) {
|
---|
901 | latePlugins.add(pi);
|
---|
902 | }
|
---|
903 | }
|
---|
904 | loadPlugins(parent, latePlugins, monitor);
|
---|
905 | }
|
---|
906 |
|
---|
907 | /**
|
---|
908 | * Loads locally available plugin information from local plugin jars and from cached
|
---|
909 | * plugin lists.
|
---|
910 | *
|
---|
911 | * @param monitor the progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null.
|
---|
912 | * @return the list of locally available plugin information
|
---|
913 | *
|
---|
914 | */
|
---|
915 | private static Map<String, PluginInformation> loadLocallyAvailablePluginInformation(ProgressMonitor monitor) {
|
---|
916 | if (monitor == null) {
|
---|
917 | monitor = NullProgressMonitor.INSTANCE;
|
---|
918 | }
|
---|
919 | try {
|
---|
920 | ReadLocalPluginInformationTask task = new ReadLocalPluginInformationTask(monitor);
|
---|
921 | try {
|
---|
922 | task.run();
|
---|
923 | } catch (RuntimeException e) { // NOPMD
|
---|
924 | Logging.error(e);
|
---|
925 | return null;
|
---|
926 | }
|
---|
927 | Map<String, PluginInformation> ret = new HashMap<>();
|
---|
928 | for (PluginInformation pi: task.getAvailablePlugins()) {
|
---|
929 | ret.put(pi.name, pi);
|
---|
930 | }
|
---|
931 | return ret;
|
---|
932 | } finally {
|
---|
933 | monitor.finishTask();
|
---|
934 | }
|
---|
935 | }
|
---|
936 |
|
---|
937 | private static void alertMissingPluginInformation(Component parent, Collection<String> plugins) {
|
---|
938 | StringBuilder sb = new StringBuilder();
|
---|
939 | sb.append("<html>")
|
---|
940 | .append(trn("JOSM could not find information about the following plugin:",
|
---|
941 | "JOSM could not find information about the following plugins:",
|
---|
942 | plugins.size()))
|
---|
943 | .append(Utils.joinAsHtmlUnorderedList(plugins))
|
---|
944 | .append(trn("The plugin is not going to be loaded.",
|
---|
945 | "The plugins are not going to be loaded.",
|
---|
946 | plugins.size()))
|
---|
947 | .append("</html>");
|
---|
948 | HelpAwareOptionPane.showOptionDialog(
|
---|
949 | parent,
|
---|
950 | sb.toString(),
|
---|
951 | tr("Warning"),
|
---|
952 | JOptionPane.WARNING_MESSAGE,
|
---|
953 | ht("/Plugin/Loading#MissingPluginInfos")
|
---|
954 | );
|
---|
955 | }
|
---|
956 |
|
---|
957 | /**
|
---|
958 | * Builds the set of plugins to load. Deprecated and unmaintained plugins are filtered
|
---|
959 | * out. This involves user interaction. This method displays alert and confirmation
|
---|
960 | * messages.
|
---|
961 | *
|
---|
962 | * @param parent The parent component to be used for the displayed dialog
|
---|
963 | * @param monitor the progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null.
|
---|
964 | * @return the set of plugins to load (as set of plugin names)
|
---|
965 | */
|
---|
966 | public static List<PluginInformation> buildListOfPluginsToLoad(Component parent, ProgressMonitor monitor) {
|
---|
967 | if (monitor == null) {
|
---|
968 | monitor = NullProgressMonitor.INSTANCE;
|
---|
969 | }
|
---|
970 | try {
|
---|
971 | monitor.beginTask(tr("Determining plugins to load..."));
|
---|
972 | Set<String> plugins = new HashSet<>(Config.getPref().getList("plugins", new LinkedList<String>()));
|
---|
973 | Logging.debug("Plugins list initialized to {0}", plugins);
|
---|
974 | String systemProp = System.getProperty("josm.plugins");
|
---|
975 | if (systemProp != null) {
|
---|
976 | plugins.addAll(Arrays.asList(systemProp.split(",")));
|
---|
977 | Logging.debug("josm.plugins system property set to '{0}'. Plugins list is now {1}", systemProp, plugins);
|
---|
978 | }
|
---|
979 | monitor.subTask(tr("Removing deprecated plugins..."));
|
---|
980 | filterDeprecatedPlugins(parent, plugins);
|
---|
981 | monitor.subTask(tr("Removing unmaintained plugins..."));
|
---|
982 | filterUnmaintainedPlugins(parent, plugins);
|
---|
983 | Logging.debug("Plugins list is finally set to {0}", plugins);
|
---|
984 | Map<String, PluginInformation> infos = loadLocallyAvailablePluginInformation(monitor.createSubTaskMonitor(1, false));
|
---|
985 | List<PluginInformation> ret = new LinkedList<>();
|
---|
986 | if (infos != null) {
|
---|
987 | for (Iterator<String> it = plugins.iterator(); it.hasNext();) {
|
---|
988 | String plugin = it.next();
|
---|
989 | if (infos.containsKey(plugin)) {
|
---|
990 | ret.add(infos.get(plugin));
|
---|
991 | it.remove();
|
---|
992 | }
|
---|
993 | }
|
---|
994 | }
|
---|
995 | if (!plugins.isEmpty()) {
|
---|
996 | alertMissingPluginInformation(parent, plugins);
|
---|
997 | }
|
---|
998 | return ret;
|
---|
999 | } finally {
|
---|
1000 | monitor.finishTask();
|
---|
1001 | }
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | private static void alertFailedPluginUpdate(Component parent, Collection<PluginInformation> plugins) {
|
---|
1005 | StringBuilder sb = new StringBuilder(128);
|
---|
1006 | sb.append("<html>")
|
---|
1007 | .append(trn(
|
---|
1008 | "Updating the following plugin has failed:",
|
---|
1009 | "Updating the following plugins has failed:",
|
---|
1010 | plugins.size()))
|
---|
1011 | .append("<ul>");
|
---|
1012 | for (PluginInformation pi: plugins) {
|
---|
1013 | sb.append("<li>").append(Utils.escapeReservedCharactersHTML(pi.name)).append("</li>");
|
---|
1014 | }
|
---|
1015 | sb.append("</ul>")
|
---|
1016 | .append(trn(
|
---|
1017 | "Please open the Preference Dialog after JOSM has started and try to update it manually.",
|
---|
1018 | "Please open the Preference Dialog after JOSM has started and try to update them manually.",
|
---|
1019 | plugins.size()))
|
---|
1020 | .append("</html>");
|
---|
1021 | HelpAwareOptionPane.showOptionDialog(
|
---|
1022 | parent,
|
---|
1023 | sb.toString(),
|
---|
1024 | tr("Plugin update failed"),
|
---|
1025 | JOptionPane.ERROR_MESSAGE,
|
---|
1026 | ht("/Plugin/Loading#FailedPluginUpdated")
|
---|
1027 | );
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | private static Set<PluginInformation> findRequiredPluginsToDownload(
|
---|
1031 | Collection<PluginInformation> pluginsToUpdate, List<PluginInformation> allPlugins, Set<PluginInformation> pluginsToDownload) {
|
---|
1032 | Set<PluginInformation> result = new HashSet<>();
|
---|
1033 | for (PluginInformation pi : pluginsToUpdate) {
|
---|
1034 | for (String name : pi.getRequiredPlugins()) {
|
---|
1035 | try {
|
---|
1036 | PluginInformation installedPlugin = PluginInformation.findPlugin(name);
|
---|
1037 | if (installedPlugin == null) {
|
---|
1038 | // New required plugin is not installed, find its PluginInformation
|
---|
1039 | PluginInformation reqPlugin = null;
|
---|
1040 | for (PluginInformation pi2 : allPlugins) {
|
---|
1041 | if (pi2.getName().equals(name)) {
|
---|
1042 | reqPlugin = pi2;
|
---|
1043 | break;
|
---|
1044 | }
|
---|
1045 | }
|
---|
1046 | // Required plugin is known but not already on download list
|
---|
1047 | if (reqPlugin != null && !pluginsToDownload.contains(reqPlugin)) {
|
---|
1048 | result.add(reqPlugin);
|
---|
1049 | }
|
---|
1050 | }
|
---|
1051 | } catch (PluginException e) {
|
---|
1052 | Logging.warn(tr("Failed to find plugin {0}", name));
|
---|
1053 | Logging.error(e);
|
---|
1054 | }
|
---|
1055 | }
|
---|
1056 | }
|
---|
1057 | return result;
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | /**
|
---|
1061 | * Updates the plugins in <code>plugins</code>.
|
---|
1062 | *
|
---|
1063 | * @param parent the parent component for message boxes
|
---|
1064 | * @param pluginsWanted the collection of plugins to update. Updates all plugins if {@code null}
|
---|
1065 | * @param monitor the progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null.
|
---|
1066 | * @param displayErrMsg if {@code true}, a blocking error message is displayed in case of I/O exception.
|
---|
1067 | * @return the list of plugins to load
|
---|
1068 | * @throws IllegalArgumentException if plugins is null
|
---|
1069 | */
|
---|
1070 | public static Collection<PluginInformation> updatePlugins(Component parent,
|
---|
1071 | Collection<PluginInformation> pluginsWanted, ProgressMonitor monitor, boolean displayErrMsg) {
|
---|
1072 | Collection<PluginInformation> plugins = null;
|
---|
1073 | pluginDownloadTask = null;
|
---|
1074 | if (monitor == null) {
|
---|
1075 | monitor = NullProgressMonitor.INSTANCE;
|
---|
1076 | }
|
---|
1077 | try {
|
---|
1078 | monitor.beginTask("");
|
---|
1079 |
|
---|
1080 | // try to download the plugin lists
|
---|
1081 | ReadRemotePluginInformationTask task1 = new ReadRemotePluginInformationTask(
|
---|
1082 | monitor.createSubTaskMonitor(1, false),
|
---|
1083 | Main.pref.getOnlinePluginSites(), displayErrMsg
|
---|
1084 | );
|
---|
1085 | task1.run();
|
---|
1086 | List<PluginInformation> allPlugins = task1.getAvailablePlugins();
|
---|
1087 |
|
---|
1088 | try {
|
---|
1089 | plugins = buildListOfPluginsToLoad(parent, monitor.createSubTaskMonitor(1, false));
|
---|
1090 | // If only some plugins have to be updated, filter the list
|
---|
1091 | if (pluginsWanted != null && !pluginsWanted.isEmpty()) {
|
---|
1092 | final Collection<String> pluginsWantedName = Utils.transform(pluginsWanted, piw -> piw.name);
|
---|
1093 | plugins = SubclassFilteredCollection.filter(plugins, pi -> pluginsWantedName.contains(pi.name));
|
---|
1094 | }
|
---|
1095 | } catch (RuntimeException e) { // NOPMD
|
---|
1096 | Logging.warn(tr("Failed to download plugin information list"));
|
---|
1097 | Logging.error(e);
|
---|
1098 | // don't abort in case of error, continue with downloading plugins below
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | // filter plugins which actually have to be updated
|
---|
1102 | Collection<PluginInformation> pluginsToUpdate = new ArrayList<>();
|
---|
1103 | if (plugins != null) {
|
---|
1104 | for (PluginInformation pi: plugins) {
|
---|
1105 | if (pi.isUpdateRequired()) {
|
---|
1106 | pluginsToUpdate.add(pi);
|
---|
1107 | }
|
---|
1108 | }
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | if (!pluginsToUpdate.isEmpty()) {
|
---|
1112 |
|
---|
1113 | Set<PluginInformation> pluginsToDownload = new HashSet<>(pluginsToUpdate);
|
---|
1114 |
|
---|
1115 | if (allPlugins != null) {
|
---|
1116 | // Updated plugins may need additional plugin dependencies currently not installed
|
---|
1117 | //
|
---|
1118 | Set<PluginInformation> additionalPlugins = findRequiredPluginsToDownload(pluginsToUpdate, allPlugins, pluginsToDownload);
|
---|
1119 | pluginsToDownload.addAll(additionalPlugins);
|
---|
1120 |
|
---|
1121 | // Iterate on required plugins, if they need themselves another plugins (i.e A needs B, but B needs C)
|
---|
1122 | while (!additionalPlugins.isEmpty()) {
|
---|
1123 | // Install the additional plugins to load them later
|
---|
1124 | if (plugins != null)
|
---|
1125 | plugins.addAll(additionalPlugins);
|
---|
1126 | additionalPlugins = findRequiredPluginsToDownload(additionalPlugins, allPlugins, pluginsToDownload);
|
---|
1127 | pluginsToDownload.addAll(additionalPlugins);
|
---|
1128 | }
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | // try to update the locally installed plugins
|
---|
1132 | pluginDownloadTask = new PluginDownloadTask(
|
---|
1133 | monitor.createSubTaskMonitor(1, false),
|
---|
1134 | pluginsToDownload,
|
---|
1135 | tr("Update plugins")
|
---|
1136 | );
|
---|
1137 |
|
---|
1138 | try {
|
---|
1139 | pluginDownloadTask.run();
|
---|
1140 | } catch (RuntimeException e) { // NOPMD
|
---|
1141 | Logging.error(e);
|
---|
1142 | alertFailedPluginUpdate(parent, pluginsToUpdate);
|
---|
1143 | return plugins;
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | // Update Plugin info for downloaded plugins
|
---|
1147 | refreshLocalUpdatedPluginInfo(pluginDownloadTask.getDownloadedPlugins());
|
---|
1148 |
|
---|
1149 | // notify user if downloading a locally installed plugin failed
|
---|
1150 | if (!pluginDownloadTask.getFailedPlugins().isEmpty()) {
|
---|
1151 | alertFailedPluginUpdate(parent, pluginDownloadTask.getFailedPlugins());
|
---|
1152 | return plugins;
|
---|
1153 | }
|
---|
1154 | }
|
---|
1155 | } finally {
|
---|
1156 | monitor.finishTask();
|
---|
1157 | }
|
---|
1158 | if (pluginsWanted == null) {
|
---|
1159 | // if all plugins updated, remember the update because it was successful
|
---|
1160 | Config.getPref().putInt("pluginmanager.version", Version.getInstance().getVersion());
|
---|
1161 | Config.getPref().put("pluginmanager.lastupdate", Long.toString(System.currentTimeMillis()));
|
---|
1162 | }
|
---|
1163 | return plugins;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | /**
|
---|
1167 | * Ask the user for confirmation that a plugin shall be disabled.
|
---|
1168 | *
|
---|
1169 | * @param parent The parent component to be used for the displayed dialog
|
---|
1170 | * @param reason the reason for disabling the plugin
|
---|
1171 | * @param name the plugin name
|
---|
1172 | * @return true, if the plugin shall be disabled; false, otherwise
|
---|
1173 | */
|
---|
1174 | public static boolean confirmDisablePlugin(Component parent, String reason, String name) {
|
---|
1175 | ButtonSpec[] options = new ButtonSpec[] {
|
---|
1176 | new ButtonSpec(
|
---|
1177 | tr("Disable plugin"),
|
---|
1178 | ImageProvider.get("dialogs", "delete"),
|
---|
1179 | tr("Click to delete the plugin ''{0}''", name),
|
---|
1180 | null /* no specific help context */
|
---|
1181 | ),
|
---|
1182 | new ButtonSpec(
|
---|
1183 | tr("Keep plugin"),
|
---|
1184 | ImageProvider.get("cancel"),
|
---|
1185 | tr("Click to keep the plugin ''{0}''", name),
|
---|
1186 | null /* no specific help context */
|
---|
1187 | )
|
---|
1188 | };
|
---|
1189 | return 0 == HelpAwareOptionPane.showOptionDialog(
|
---|
1190 | parent,
|
---|
1191 | reason,
|
---|
1192 | tr("Disable plugin"),
|
---|
1193 | JOptionPane.WARNING_MESSAGE,
|
---|
1194 | null,
|
---|
1195 | options,
|
---|
1196 | options[0],
|
---|
1197 | null // FIXME: add help topic
|
---|
1198 | );
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | /**
|
---|
1202 | * Returns the plugin of the specified name.
|
---|
1203 | * @param name The plugin name
|
---|
1204 | * @return The plugin of the specified name, if installed and loaded, or {@code null} otherwise.
|
---|
1205 | */
|
---|
1206 | public static Object getPlugin(String name) {
|
---|
1207 | for (PluginProxy plugin : pluginList) {
|
---|
1208 | if (plugin.getPluginInformation().name.equals(name))
|
---|
1209 | return plugin.getPlugin();
|
---|
1210 | }
|
---|
1211 | return null;
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | /**
|
---|
1215 | * Returns the plugin class loader for the plugin of the specified name.
|
---|
1216 | * @param name The plugin name
|
---|
1217 | * @return The plugin class loader for the plugin of the specified name, if
|
---|
1218 | * installed and loaded, or {@code null} otherwise.
|
---|
1219 | * @since 12323
|
---|
1220 | */
|
---|
1221 | public static PluginClassLoader getPluginClassLoader(String name) {
|
---|
1222 | for (PluginProxy plugin : pluginList) {
|
---|
1223 | if (plugin.getPluginInformation().name.equals(name))
|
---|
1224 | return plugin.getClassLoader();
|
---|
1225 | }
|
---|
1226 | return null;
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | public static void addDownloadSelection(List<DownloadSelection> downloadSelections) {
|
---|
1230 | for (PluginProxy p : pluginList) {
|
---|
1231 | p.addDownloadSelection(downloadSelections);
|
---|
1232 | }
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 | public static Collection<PreferenceSettingFactory> getPreferenceSetting() {
|
---|
1236 | Collection<PreferenceSettingFactory> settings = new ArrayList<>();
|
---|
1237 | for (PluginProxy plugin : pluginList) {
|
---|
1238 | settings.add(new PluginPreferenceFactory(plugin));
|
---|
1239 | }
|
---|
1240 | return settings;
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | /**
|
---|
1244 | * Installs downloaded plugins. Moves files with the suffix ".jar.new" to the corresponding
|
---|
1245 | * ".jar" files.
|
---|
1246 | *
|
---|
1247 | * If {@code dowarn} is true, this methods emits warning messages on the console if a downloaded
|
---|
1248 | * but not yet installed plugin .jar can't be be installed. If {@code dowarn} is false, the
|
---|
1249 | * installation of the respective plugin is silently skipped.
|
---|
1250 | *
|
---|
1251 | * @param dowarn if true, warning messages are displayed; false otherwise
|
---|
1252 | */
|
---|
1253 | public static void installDownloadedPlugins(boolean dowarn) {
|
---|
1254 | File pluginDir = Main.pref.getPluginsDirectory();
|
---|
1255 | if (!pluginDir.exists() || !pluginDir.isDirectory() || !pluginDir.canWrite())
|
---|
1256 | return;
|
---|
1257 |
|
---|
1258 | final File[] files = pluginDir.listFiles((FilenameFilter) (dir, name) -> name.endsWith(".jar.new"));
|
---|
1259 | if (files == null)
|
---|
1260 | return;
|
---|
1261 |
|
---|
1262 | for (File updatedPlugin : files) {
|
---|
1263 | final String filePath = updatedPlugin.getPath();
|
---|
1264 | File plugin = new File(filePath.substring(0, filePath.length() - 4));
|
---|
1265 | String pluginName = updatedPlugin.getName().substring(0, updatedPlugin.getName().length() - 8);
|
---|
1266 | if (plugin.exists() && !plugin.delete() && dowarn) {
|
---|
1267 | Logging.warn(tr("Failed to delete outdated plugin ''{0}''.", plugin.toString()));
|
---|
1268 | Logging.warn(tr("Failed to install already downloaded plugin ''{0}''. " +
|
---|
1269 | "Skipping installation. JOSM is still going to load the old plugin version.",
|
---|
1270 | pluginName));
|
---|
1271 | continue;
|
---|
1272 | }
|
---|
1273 | try {
|
---|
1274 | // Check the plugin is a valid and accessible JAR file before installing it (fix #7754)
|
---|
1275 | new JarFile(updatedPlugin).close();
|
---|
1276 | } catch (IOException e) {
|
---|
1277 | if (dowarn) {
|
---|
1278 | Logging.log(Logging.LEVEL_WARN, tr("Failed to install plugin ''{0}'' from temporary download file ''{1}''. {2}",
|
---|
1279 | plugin.toString(), updatedPlugin.toString(), e.getLocalizedMessage()), e);
|
---|
1280 | }
|
---|
1281 | continue;
|
---|
1282 | }
|
---|
1283 | // Install plugin
|
---|
1284 | if (!updatedPlugin.renameTo(plugin) && dowarn) {
|
---|
1285 | Logging.warn(tr("Failed to install plugin ''{0}'' from temporary download file ''{1}''. Renaming failed.",
|
---|
1286 | plugin.toString(), updatedPlugin.toString()));
|
---|
1287 | Logging.warn(tr("Failed to install already downloaded plugin ''{0}''. " +
|
---|
1288 | "Skipping installation. JOSM is still going to load the old plugin version.",
|
---|
1289 | pluginName));
|
---|
1290 | }
|
---|
1291 | }
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | /**
|
---|
1295 | * Determines if the specified file is a valid and accessible JAR file.
|
---|
1296 | * @param jar The file to check
|
---|
1297 | * @return true if file can be opened as a JAR file.
|
---|
1298 | * @since 5723
|
---|
1299 | */
|
---|
1300 | public static boolean isValidJar(File jar) {
|
---|
1301 | if (jar != null && jar.exists() && jar.canRead()) {
|
---|
1302 | try {
|
---|
1303 | new JarFile(jar).close();
|
---|
1304 | } catch (IOException e) {
|
---|
1305 | Logging.warn(e);
|
---|
1306 | return false;
|
---|
1307 | }
|
---|
1308 | return true;
|
---|
1309 | } else if (jar != null) {
|
---|
1310 | Logging.warn("Invalid jar file ''"+jar+"'' (exists: "+jar.exists()+", canRead: "+jar.canRead()+')');
|
---|
1311 | }
|
---|
1312 | return false;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | /**
|
---|
1316 | * Replies the updated jar file for the given plugin name.
|
---|
1317 | * @param name The plugin name to find.
|
---|
1318 | * @return the updated jar file for the given plugin name. null if not found or not readable.
|
---|
1319 | * @since 5601
|
---|
1320 | */
|
---|
1321 | public static File findUpdatedJar(String name) {
|
---|
1322 | File pluginDir = Main.pref.getPluginsDirectory();
|
---|
1323 | // Find the downloaded file. We have tried to install the downloaded plugins
|
---|
1324 | // (PluginHandler.installDownloadedPlugins). This succeeds depending on the platform.
|
---|
1325 | File downloadedPluginFile = new File(pluginDir, name + ".jar.new");
|
---|
1326 | if (!isValidJar(downloadedPluginFile)) {
|
---|
1327 | downloadedPluginFile = new File(pluginDir, name + ".jar");
|
---|
1328 | if (!isValidJar(downloadedPluginFile)) {
|
---|
1329 | return null;
|
---|
1330 | }
|
---|
1331 | }
|
---|
1332 | return downloadedPluginFile;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | /**
|
---|
1336 | * Refreshes the given PluginInformation objects with new contents read from their corresponding jar file.
|
---|
1337 | * @param updatedPlugins The PluginInformation objects to update.
|
---|
1338 | * @since 5601
|
---|
1339 | */
|
---|
1340 | public static void refreshLocalUpdatedPluginInfo(Collection<PluginInformation> updatedPlugins) {
|
---|
1341 | if (updatedPlugins == null) return;
|
---|
1342 | for (PluginInformation pi : updatedPlugins) {
|
---|
1343 | File downloadedPluginFile = findUpdatedJar(pi.name);
|
---|
1344 | if (downloadedPluginFile == null) {
|
---|
1345 | continue;
|
---|
1346 | }
|
---|
1347 | try {
|
---|
1348 | pi.updateFromJar(new PluginInformation(downloadedPluginFile, pi.name));
|
---|
1349 | } catch (PluginException e) {
|
---|
1350 | Logging.error(e);
|
---|
1351 | }
|
---|
1352 | }
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | private static int askUpdateDisableKeepPluginAfterException(PluginProxy plugin) {
|
---|
1356 | final ButtonSpec[] options = new ButtonSpec[] {
|
---|
1357 | new ButtonSpec(
|
---|
1358 | tr("Update plugin"),
|
---|
1359 | ImageProvider.get("dialogs", "refresh"),
|
---|
1360 | tr("Click to update the plugin ''{0}''", plugin.getPluginInformation().name),
|
---|
1361 | null /* no specific help context */
|
---|
1362 | ),
|
---|
1363 | new ButtonSpec(
|
---|
1364 | tr("Disable plugin"),
|
---|
1365 | ImageProvider.get("dialogs", "delete"),
|
---|
1366 | tr("Click to disable the plugin ''{0}''", plugin.getPluginInformation().name),
|
---|
1367 | null /* no specific help context */
|
---|
1368 | ),
|
---|
1369 | new ButtonSpec(
|
---|
1370 | tr("Keep plugin"),
|
---|
1371 | ImageProvider.get("cancel"),
|
---|
1372 | tr("Click to keep the plugin ''{0}''", plugin.getPluginInformation().name),
|
---|
1373 | null /* no specific help context */
|
---|
1374 | )
|
---|
1375 | };
|
---|
1376 |
|
---|
1377 | final StringBuilder msg = new StringBuilder(256);
|
---|
1378 | msg.append("<html>")
|
---|
1379 | .append(tr("An unexpected exception occurred that may have come from the ''{0}'' plugin.",
|
---|
1380 | Utils.escapeReservedCharactersHTML(plugin.getPluginInformation().name)))
|
---|
1381 | .append("<br>");
|
---|
1382 | if (plugin.getPluginInformation().author != null) {
|
---|
1383 | msg.append(tr("According to the information within the plugin, the author is {0}.",
|
---|
1384 | Utils.escapeReservedCharactersHTML(plugin.getPluginInformation().author)))
|
---|
1385 | .append("<br>");
|
---|
1386 | }
|
---|
1387 | msg.append(tr("Try updating to the newest version of this plugin before reporting a bug."))
|
---|
1388 | .append("</html>");
|
---|
1389 |
|
---|
1390 | try {
|
---|
1391 | FutureTask<Integer> task = new FutureTask<>(() -> HelpAwareOptionPane.showOptionDialog(
|
---|
1392 | Main.parent,
|
---|
1393 | msg.toString(),
|
---|
1394 | tr("Update plugins"),
|
---|
1395 | JOptionPane.QUESTION_MESSAGE,
|
---|
1396 | null,
|
---|
1397 | options,
|
---|
1398 | options[0],
|
---|
1399 | ht("/ErrorMessages#ErrorInPlugin")
|
---|
1400 | ));
|
---|
1401 | GuiHelper.runInEDT(task);
|
---|
1402 | return task.get();
|
---|
1403 | } catch (InterruptedException | ExecutionException e) {
|
---|
1404 | Logging.warn(e);
|
---|
1405 | }
|
---|
1406 | return -1;
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | /**
|
---|
1410 | * Replies the plugin which most likely threw the exception <code>ex</code>.
|
---|
1411 | *
|
---|
1412 | * @param ex the exception
|
---|
1413 | * @return the plugin; null, if the exception probably wasn't thrown from a plugin
|
---|
1414 | */
|
---|
1415 | private static PluginProxy getPluginCausingException(Throwable ex) {
|
---|
1416 | PluginProxy err = null;
|
---|
1417 | StackTraceElement[] stack = ex.getStackTrace();
|
---|
1418 | // remember the error position, as multiple plugins may be involved, we search the topmost one
|
---|
1419 | int pos = stack.length;
|
---|
1420 | for (PluginProxy p : pluginList) {
|
---|
1421 | String baseClass = p.getPluginInformation().className;
|
---|
1422 | baseClass = baseClass.substring(0, baseClass.lastIndexOf('.'));
|
---|
1423 | for (int elpos = 0; elpos < pos; ++elpos) {
|
---|
1424 | if (stack[elpos].getClassName().startsWith(baseClass)) {
|
---|
1425 | pos = elpos;
|
---|
1426 | err = p;
|
---|
1427 | }
|
---|
1428 | }
|
---|
1429 | }
|
---|
1430 | return err;
|
---|
1431 | }
|
---|
1432 |
|
---|
1433 | /**
|
---|
1434 | * Checks whether the exception <code>e</code> was thrown by a plugin. If so,
|
---|
1435 | * conditionally updates or deactivates the plugin, but asks the user first.
|
---|
1436 | *
|
---|
1437 | * @param e the exception
|
---|
1438 | * @return plugin download task if the plugin has been updated to a newer version, {@code null} if it has been disabled or kept as it
|
---|
1439 | */
|
---|
1440 | public static PluginDownloadTask updateOrdisablePluginAfterException(Throwable e) {
|
---|
1441 | PluginProxy plugin = null;
|
---|
1442 | // Check for an explicit problem when calling a plugin function
|
---|
1443 | if (e instanceof PluginException) {
|
---|
1444 | plugin = ((PluginException) e).plugin;
|
---|
1445 | }
|
---|
1446 | if (plugin == null) {
|
---|
1447 | plugin = getPluginCausingException(e);
|
---|
1448 | }
|
---|
1449 | if (plugin == null)
|
---|
1450 | // don't know what plugin threw the exception
|
---|
1451 | return null;
|
---|
1452 |
|
---|
1453 | Set<String> plugins = new HashSet<>(Config.getPref().getList("plugins"));
|
---|
1454 | final PluginInformation pluginInfo = plugin.getPluginInformation();
|
---|
1455 | if (!plugins.contains(pluginInfo.name))
|
---|
1456 | // plugin not activated ? strange in this context but anyway, don't bother
|
---|
1457 | // the user with dialogs, skip conditional deactivation
|
---|
1458 | return null;
|
---|
1459 |
|
---|
1460 | switch (askUpdateDisableKeepPluginAfterException(plugin)) {
|
---|
1461 | case 0:
|
---|
1462 | // update the plugin
|
---|
1463 | updatePlugins(Main.parent, Collections.singleton(pluginInfo), null, true);
|
---|
1464 | return pluginDownloadTask;
|
---|
1465 | case 1:
|
---|
1466 | // deactivate the plugin
|
---|
1467 | plugins.remove(plugin.getPluginInformation().name);
|
---|
1468 | Config.getPref().putList("plugins", new ArrayList<>(plugins));
|
---|
1469 | GuiHelper.runInEDTAndWait(() -> JOptionPane.showMessageDialog(
|
---|
1470 | Main.parent,
|
---|
1471 | tr("The plugin has been removed from the configuration. Please restart JOSM to unload the plugin."),
|
---|
1472 | tr("Information"),
|
---|
1473 | JOptionPane.INFORMATION_MESSAGE
|
---|
1474 | ));
|
---|
1475 | return null;
|
---|
1476 | default:
|
---|
1477 | // user doesn't want to deactivate the plugin
|
---|
1478 | return null;
|
---|
1479 | }
|
---|
1480 | }
|
---|
1481 |
|
---|
1482 | /**
|
---|
1483 | * Returns the list of loaded plugins as a {@code String} to be displayed in status report. Useful for bug reports.
|
---|
1484 | * @return The list of loaded plugins
|
---|
1485 | */
|
---|
1486 | public static Collection<String> getBugReportInformation() {
|
---|
1487 | final Collection<String> pl = new TreeSet<>(Config.getPref().getList("plugins", new LinkedList<>()));
|
---|
1488 | for (final PluginProxy pp : pluginList) {
|
---|
1489 | PluginInformation pi = pp.getPluginInformation();
|
---|
1490 | pl.remove(pi.name);
|
---|
1491 | pl.add(pi.name + " (" + (pi.localversion != null && !pi.localversion.isEmpty()
|
---|
1492 | ? pi.localversion : "unknown") + ')');
|
---|
1493 | }
|
---|
1494 | return pl;
|
---|
1495 | }
|
---|
1496 |
|
---|
1497 | /**
|
---|
1498 | * Returns the list of loaded plugins as a {@code JPanel} to be displayed in About dialog.
|
---|
1499 | * @return The list of loaded plugins (one "line" of Swing components per plugin)
|
---|
1500 | */
|
---|
1501 | public static JPanel getInfoPanel() {
|
---|
1502 | JPanel pluginTab = new JPanel(new GridBagLayout());
|
---|
1503 | for (final PluginProxy p : pluginList) {
|
---|
1504 | final PluginInformation info = p.getPluginInformation();
|
---|
1505 | String name = info.name
|
---|
1506 | + (info.version != null && !info.version.isEmpty() ? " Version: " + info.version : "");
|
---|
1507 | pluginTab.add(new JLabel(name), GBC.std());
|
---|
1508 | pluginTab.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
|
---|
1509 | pluginTab.add(new JButton(new PluginInformationAction(info)), GBC.eol());
|
---|
1510 |
|
---|
1511 | JosmTextArea description = new JosmTextArea(info.description == null ? tr("no description available")
|
---|
1512 | : info.description);
|
---|
1513 | description.setEditable(false);
|
---|
1514 | description.setFont(new JLabel().getFont().deriveFont(Font.ITALIC));
|
---|
1515 | description.setLineWrap(true);
|
---|
1516 | description.setWrapStyleWord(true);
|
---|
1517 | description.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
|
---|
1518 | description.setBackground(UIManager.getColor("Panel.background"));
|
---|
1519 | description.setCaretPosition(0);
|
---|
1520 |
|
---|
1521 | pluginTab.add(description, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
1522 | }
|
---|
1523 | return pluginTab;
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | /**
|
---|
1527 | * Returns the set of deprecated and unmaintained plugins.
|
---|
1528 | * @return set of deprecated and unmaintained plugins names.
|
---|
1529 | * @since 8938
|
---|
1530 | */
|
---|
1531 | public static Set<String> getDeprecatedAndUnmaintainedPlugins() {
|
---|
1532 | Set<String> result = new HashSet<>(DEPRECATED_PLUGINS.size() + UNMAINTAINED_PLUGINS.size());
|
---|
1533 | for (DeprecatedPlugin dp : DEPRECATED_PLUGINS) {
|
---|
1534 | result.add(dp.name);
|
---|
1535 | }
|
---|
1536 | result.addAll(UNMAINTAINED_PLUGINS);
|
---|
1537 | return result;
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 | private static class UpdatePluginsMessagePanel extends JPanel {
|
---|
1541 | private final JMultilineLabel lblMessage = new JMultilineLabel("");
|
---|
1542 | private final JCheckBox cbDontShowAgain = new JCheckBox(
|
---|
1543 | tr("Do not ask again and remember my decision (go to Preferences->Plugins to change it later)"));
|
---|
1544 |
|
---|
1545 | UpdatePluginsMessagePanel() {
|
---|
1546 | build();
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 | protected final void build() {
|
---|
1550 | setLayout(new GridBagLayout());
|
---|
1551 | GridBagConstraints gc = new GridBagConstraints();
|
---|
1552 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
1553 | gc.fill = GridBagConstraints.BOTH;
|
---|
1554 | gc.weightx = 1.0;
|
---|
1555 | gc.weighty = 1.0;
|
---|
1556 | gc.insets = new Insets(5, 5, 5, 5);
|
---|
1557 | add(lblMessage, gc);
|
---|
1558 | lblMessage.setFont(lblMessage.getFont().deriveFont(Font.PLAIN));
|
---|
1559 |
|
---|
1560 | gc.gridy = 1;
|
---|
1561 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
1562 | gc.weighty = 0.0;
|
---|
1563 | add(cbDontShowAgain, gc);
|
---|
1564 | cbDontShowAgain.setFont(cbDontShowAgain.getFont().deriveFont(Font.PLAIN));
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | public void setMessage(String message) {
|
---|
1568 | lblMessage.setText(message);
|
---|
1569 | }
|
---|
1570 |
|
---|
1571 | public void initDontShowAgain(String preferencesKey) {
|
---|
1572 | String policy = Config.getPref().get(preferencesKey, "ask");
|
---|
1573 | policy = policy.trim().toLowerCase(Locale.ENGLISH);
|
---|
1574 | cbDontShowAgain.setSelected(!"ask".equals(policy));
|
---|
1575 | }
|
---|
1576 |
|
---|
1577 | public boolean isRememberDecision() {
|
---|
1578 | return cbDontShowAgain.isSelected();
|
---|
1579 | }
|
---|
1580 | }
|
---|
1581 | }
|
---|