source: josm/trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java@ 12323

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

see #14901 - some cleanup

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