Ignore:
Timestamp:
2014-02-01T02:15:45+01:00 (11 years ago)
Author:
Don-vip
Message:

fix #9406 - Verify for new versions of plugins on crashes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java

    r6643 r6797  
    3232import java.util.Set;
    3333import java.util.TreeSet;
     34import java.util.concurrent.Callable;
    3435import java.util.concurrent.ExecutionException;
    3536import java.util.concurrent.ExecutorService;
    3637import java.util.concurrent.Executors;
    3738import java.util.concurrent.Future;
     39import java.util.concurrent.FutureTask;
    3840import java.util.jar.JarFile;
    3941
     
    5860import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    5961import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     62import org.openstreetmap.josm.gui.util.GuiHelper;
    6063import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
    6164import org.openstreetmap.josm.gui.widgets.JosmTextArea;
    62 import org.openstreetmap.josm.tools.CheckParameterUtil;
    6365import org.openstreetmap.josm.tools.GBC;
    6466import org.openstreetmap.josm.tools.I18n;
     
    183185     * List of unmaintained plugins. Not really up-to-date as the vast majority of plugins are not really maintained after a few months, sadly...
    184186     */
    185     final public static String [] UNMAINTAINED_PLUGINS = new String[] {"gpsbabelgui", "Intersect_way"};
     187    public static final String [] UNMAINTAINED_PLUGINS = new String[] {"gpsbabelgui", "Intersect_way"};
    186188
    187189    /**
     
    208210        }
    209211    }
     212   
     213    private static PluginDownloadTask pluginDownloadTask = null;
    210214
    211215    public static Collection<ClassLoader> getResourceClassLoaders() {
     
    819823     *
    820824     * @param parent the parent component for message boxes
    821      * @param plugins the collection of plugins to update. Must not be null.
     825     * @param pluginsWanted the collection of plugins to update. Updates all plugins if {@code null}
    822826     * @param monitor the progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null.
    823827     * @throws IllegalArgumentException thrown if plugins is null
    824828     */
    825     public static List<PluginInformation> updatePlugins(Component parent,
    826             List<PluginInformation> plugins, ProgressMonitor monitor)
    827             throws IllegalArgumentException{
    828         CheckParameterUtil.ensureParameterNotNull(plugins, "plugins");
     829    public static Collection<PluginInformation> updatePlugins(Component parent,
     830            Collection<PluginInformation> pluginsWanted, ProgressMonitor monitor)
     831            throws IllegalArgumentException {
     832        Collection<PluginInformation> plugins = null;
     833        pluginDownloadTask = null;
    829834        if (monitor == null) {
    830835            monitor = NullProgressMonitor.INSTANCE;
     
    847852                allPlugins = task1.getAvailablePlugins();
    848853                plugins = buildListOfPluginsToLoad(parent,monitor.createSubTaskMonitor(1, false));
     854                // If only some plugins have to be updated, filter the list
     855                if (pluginsWanted != null && !pluginsWanted.isEmpty()) {
     856                    for (Iterator<PluginInformation> it = plugins.iterator(); it.hasNext();) {
     857                        PluginInformation pi = it.next();
     858                        boolean found = false;
     859                        for (PluginInformation piw : pluginsWanted) {
     860                            if (pi.name.equals(piw.name)) {
     861                                found = true;
     862                                break;
     863                            }
     864                        }
     865                        if (!found) {
     866                            it.remove();
     867                        }
     868                    }
     869                }
    849870            } catch (ExecutionException e) {
    850871                Main.warn(tr("Failed to download plugin information list")+": ExecutionException");
     
    886907                // try to update the locally installed plugins
    887908                //
    888                 PluginDownloadTask task2 = new PluginDownloadTask(
     909                pluginDownloadTask = new PluginDownloadTask(
    889910                        monitor.createSubTaskMonitor(1,false),
    890911                        pluginsToDownload,
     
    892913                );
    893914
    894                 future = service.submit(task2);
     915                future = service.submit(pluginDownloadTask);
    895916                try {
    896917                    future.get();
     
    907928                // Update Plugin info for downloaded plugins
    908929                //
    909                 refreshLocalUpdatedPluginInfo(task2.getDownloadedPlugins());
     930                refreshLocalUpdatedPluginInfo(pluginDownloadTask.getDownloadedPlugins());
    910931
    911932                // notify user if downloading a locally installed plugin failed
    912933                //
    913                 if (! task2.getFailedPlugins().isEmpty()) {
    914                     alertFailedPluginUpdate(parent, task2.getFailedPlugins());
     934                if (! pluginDownloadTask.getFailedPlugins().isEmpty()) {
     935                    alertFailedPluginUpdate(parent, pluginDownloadTask.getFailedPlugins());
    915936                    return plugins;
    916937                }
     
    919940            monitor.finishTask();
    920941        }
    921         // remember the update because it was successful
    922         //
    923         Main.pref.putInteger("pluginmanager.version", Version.getInstance().getVersion());
    924         Main.pref.put("pluginmanager.lastupdate", Long.toString(System.currentTimeMillis()));
     942        if (pluginsWanted == null) {
     943            // if all plugins updated, remember the update because it was successful
     944            //
     945            Main.pref.putInteger("pluginmanager.version", Version.getInstance().getVersion());
     946            Main.pref.put("pluginmanager.lastupdate", Long.toString(System.currentTimeMillis()));
     947        }
    925948        return plugins;
    926949    }
     
    10941117    }
    10951118
    1096     private static boolean confirmDeactivatingPluginAfterException(PluginProxy plugin) {
    1097         ButtonSpec [] options = new ButtonSpec[] {
     1119    private static int askUpdateDisableKeepPluginAfterException(PluginProxy plugin) {
     1120        final ButtonSpec[] options = new ButtonSpec[] {
     1121                new ButtonSpec(
     1122                        tr("Update plugin"),
     1123                        ImageProvider.get("dialogs", "refresh"),
     1124                        tr("Click to update the plugin ''{0}''", plugin.getPluginInformation().name),
     1125                        null /* no specific help context */
     1126                ),
    10981127                new ButtonSpec(
    10991128                        tr("Disable plugin"),
     
    11101139        };
    11111140
    1112         StringBuffer msg = new StringBuffer();
     1141        final StringBuffer msg = new StringBuffer();
    11131142        msg.append("<html>");
    11141143        msg.append(tr("An unexpected exception occurred that may have come from the ''{0}'' plugin.", plugin.getPluginInformation().name));
    11151144        msg.append("<br>");
    1116         if(plugin.getPluginInformation().author != null) {
     1145        if (plugin.getPluginInformation().author != null) {
    11171146            msg.append(tr("According to the information within the plugin, the author is {0}.", plugin.getPluginInformation().author));
    11181147            msg.append("<br>");
    11191148        }
    11201149        msg.append(tr("Try updating to the newest version of this plugin before reporting a bug."));
    1121         msg.append("<br>");
    1122         msg.append(tr("Should the plugin be disabled?"));
    11231150        msg.append("</html>");
    11241151
    1125         int ret = HelpAwareOptionPane.showOptionDialog(
    1126                 Main.parent,
    1127                 msg.toString(),
    1128                 tr("Update plugins"),
    1129                 JOptionPane.QUESTION_MESSAGE,
    1130                 null,
    1131                 options,
    1132                 options[0],
    1133                 ht("/ErrorMessages#ErrorInPlugin")
    1134         );
    1135         return ret == 0;
     1152        try {
     1153            FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
     1154                @Override
     1155                public Integer call() {
     1156                    return HelpAwareOptionPane.showOptionDialog(
     1157                            Main.parent,
     1158                            msg.toString(),
     1159                            tr("Update plugins"),
     1160                            JOptionPane.QUESTION_MESSAGE,
     1161                            null,
     1162                            options,
     1163                            options[0],
     1164                            ht("/ErrorMessages#ErrorInPlugin")
     1165                    );
     1166                }
     1167            });
     1168            GuiHelper.runInEDT(task);
     1169            return task.get();
     1170        } catch (InterruptedException e) {
     1171            Main.warn(e);
     1172        } catch (ExecutionException e) {
     1173            Main.warn(e);
     1174        }
     1175        return -1;
    11361176    }
    11371177
     
    11631203    /**
    11641204     * Checks whether the exception <code>e</code> was thrown by a plugin. If so,
    1165      * conditionally deactivates the plugin, but asks the user first.
     1205     * conditionally updates or deactivates the plugin, but asks the user first.
    11661206     *
    11671207     * @param e the exception
    1168      */
    1169     public static void disablePluginAfterException(Throwable e) {
     1208     * @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
     1209     */
     1210    public static PluginDownloadTask updateOrdisablePluginAfterException(Throwable e) {
    11701211        PluginProxy plugin = null;
    11711212        // Check for an explicit problem when calling a plugin function
     
    11781219        if (plugin == null)
    11791220            // don't know what plugin threw the exception
    1180             return;
     1221            return null;
    11811222
    11821223        Set<String> plugins = new HashSet<String>(
    11831224                Main.pref.getCollection("plugins",Collections.<String> emptySet())
    11841225        );
    1185         if (! plugins.contains(plugin.getPluginInformation().name))
     1226        final PluginInformation pluginInfo = plugin.getPluginInformation();
     1227        if (! plugins.contains(pluginInfo.name))
    11861228            // plugin not activated ? strange in this context but anyway, don't bother
    11871229            // the user with dialogs, skip conditional deactivation
    1188             return;
    1189 
    1190         if (!confirmDeactivatingPluginAfterException(plugin))
     1230            return null;
     1231
     1232        switch (askUpdateDisableKeepPluginAfterException(plugin)) {
     1233        case 0:
     1234            // update the plugin
     1235            updatePlugins(Main.parent, Collections.singleton(pluginInfo), null);
     1236            return pluginDownloadTask;
     1237        case 1:
     1238            // deactivate the plugin
     1239            plugins.remove(plugin.getPluginInformation().name);
     1240            Main.pref.putCollection("plugins", plugins);
     1241            GuiHelper.runInEDTAndWait(new Runnable() {
     1242                @Override
     1243                public void run() {
     1244                    JOptionPane.showMessageDialog(
     1245                            Main.parent,
     1246                            tr("The plugin has been removed from the configuration. Please restart JOSM to unload the plugin."),
     1247                            tr("Information"),
     1248                            JOptionPane.INFORMATION_MESSAGE
     1249                    );
     1250                }
     1251            });
     1252            return null;
     1253        default:
    11911254            // user doesn't want to deactivate the plugin
    1192             return;
    1193 
    1194         // deactivate the plugin
    1195         plugins.remove(plugin.getPluginInformation().name);
    1196         Main.pref.putCollection("plugins", plugins);
    1197         JOptionPane.showMessageDialog(
    1198                 Main.parent,
    1199                 tr("The plugin has been removed from the configuration. Please restart JOSM to unload the plugin."),
    1200                 tr("Information"),
    1201                 JOptionPane.INFORMATION_MESSAGE
    1202         );
    1203         return;
     1255            return null;
     1256        }
    12041257    }
    12051258
Note: See TracChangeset for help on using the changeset viewer.