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