[3083] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.plugins;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.awt.Component;
|
---|
| 7 | import java.io.File;
|
---|
| 8 | import java.io.IOException;
|
---|
| 9 | import java.io.InputStream;
|
---|
| 10 | import java.net.MalformedURLException;
|
---|
| 11 | import java.net.URL;
|
---|
[9280] | 12 | import java.nio.file.Files;
|
---|
| 13 | import java.nio.file.StandardCopyOption;
|
---|
[3083] | 14 | import java.util.Collection;
|
---|
| 15 | import java.util.LinkedList;
|
---|
| 16 |
|
---|
| 17 | import org.openstreetmap.josm.Main;
|
---|
| 18 | import org.openstreetmap.josm.data.Version;
|
---|
| 19 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
| 20 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
| 21 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
| 22 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
| 23 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[9168] | 24 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
[3083] | 25 | import org.xml.sax.SAXException;
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Asynchronous task for downloading a collection of plugins.
|
---|
[3530] | 29 | *
|
---|
[5266] | 30 | * When the task is finished {@link #getDownloadedPlugins()} replies the list of downloaded plugins
|
---|
| 31 | * and {@link #getFailedPlugins()} replies the list of failed plugins.
|
---|
[9621] | 32 | * @since 2817
|
---|
[3083] | 33 | */
|
---|
[9059] | 34 | public class PluginDownloadTask extends PleaseWaitRunnable {
|
---|
[6867] | 35 |
|
---|
| 36 | /**
|
---|
| 37 | * The accepted MIME types sent in the HTTP Accept header.
|
---|
| 38 | * @since 6867
|
---|
| 39 | */
|
---|
| 40 | public static final String PLUGIN_MIME_TYPES = "application/java-archive, application/zip; q=0.9, application/octet-stream; q=0.5";
|
---|
[7509] | 41 |
|
---|
[7005] | 42 | private final Collection<PluginInformation> toUpdate = new LinkedList<>();
|
---|
| 43 | private final Collection<PluginInformation> failed = new LinkedList<>();
|
---|
| 44 | private final Collection<PluginInformation> downloaded = new LinkedList<>();
|
---|
[9621] | 45 | private Exception lastException;
|
---|
[3083] | 46 | private boolean canceled;
|
---|
[9309] | 47 | private HttpClient downloadConnection;
|
---|
[3083] | 48 |
|
---|
| 49 | /**
|
---|
| 50 | * Creates the download task
|
---|
[3530] | 51 | *
|
---|
[5836] | 52 | * @param parent the parent component relative to which the {@link org.openstreetmap.josm.gui.PleaseWaitDialog} is displayed
|
---|
[3083] | 53 | * @param toUpdate a collection of plugin descriptions for plugins to update/download. Must not be null.
|
---|
[5836] | 54 | * @param title the title to display in the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
|
---|
[8291] | 55 | * @throws IllegalArgumentException if toUpdate is null
|
---|
[3083] | 56 | */
|
---|
[8291] | 57 | public PluginDownloadTask(Component parent, Collection<PluginInformation> toUpdate, String title) {
|
---|
[3083] | 58 | super(parent, title == null ? "" : title, false /* don't ignore exceptions */);
|
---|
| 59 | CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
|
---|
[3090] | 60 | this.toUpdate.addAll(toUpdate);
|
---|
[3083] | 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Creates the task
|
---|
[3530] | 65 | *
|
---|
[5266] | 66 | * @param monitor a progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null
|
---|
[3083] | 67 | * @param toUpdate a collection of plugin descriptions for plugins to update/download. Must not be null.
|
---|
[5836] | 68 | * @param title the title to display in the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
|
---|
[8291] | 69 | * @throws IllegalArgumentException if toUpdate is null
|
---|
[3083] | 70 | */
|
---|
| 71 | public PluginDownloadTask(ProgressMonitor monitor, Collection<PluginInformation> toUpdate, String title) {
|
---|
[8510] | 72 | super(title, monitor == null ? NullProgressMonitor.INSTANCE : monitor, false /* don't ignore exceptions */);
|
---|
[3083] | 73 | CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
|
---|
[3090] | 74 | this.toUpdate.addAll(toUpdate);
|
---|
[3083] | 75 | }
|
---|
| 76 |
|
---|
[3090] | 77 | /**
|
---|
| 78 | * Sets the collection of plugins to update.
|
---|
[3530] | 79 | *
|
---|
[3090] | 80 | * @param toUpdate the collection of plugins to update. Must not be null.
|
---|
[8291] | 81 | * @throws IllegalArgumentException if toUpdate is null
|
---|
[3090] | 82 | */
|
---|
[8291] | 83 | public void setPluginsToDownload(Collection<PluginInformation> toUpdate) {
|
---|
[3090] | 84 | CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
|
---|
| 85 | this.toUpdate.clear();
|
---|
| 86 | this.toUpdate.addAll(toUpdate);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[6883] | 89 | @Override
|
---|
| 90 | protected void cancel() {
|
---|
[3083] | 91 | this.canceled = true;
|
---|
[8510] | 92 | synchronized (this) {
|
---|
[3083] | 93 | if (downloadConnection != null) {
|
---|
| 94 | downloadConnection.disconnect();
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[6883] | 99 | @Override
|
---|
[9621] | 100 | protected void finish() {
|
---|
| 101 | // Do nothing. Error/success feedback is managed in PluginPreference.notifyDownloadResults()
|
---|
| 102 | }
|
---|
[3083] | 103 |
|
---|
[8510] | 104 | protected void download(PluginInformation pi, File file) throws PluginDownloadException {
|
---|
[3083] | 105 | if (pi.mainversion > Version.getInstance().getVersion()) {
|
---|
| 106 | ExtendedDialog dialog = new ExtendedDialog(
|
---|
[6048] | 107 | progressMonitor.getWindowParent(),
|
---|
[3083] | 108 | tr("Skip download"),
|
---|
| 109 | new String[] {
|
---|
| 110 | tr("Download Plugin"),
|
---|
| 111 | tr("Skip Download") }
|
---|
| 112 | );
|
---|
| 113 | dialog.setContent(tr("JOSM version {0} required for plugin {1}.", pi.mainversion, pi.name));
|
---|
[8443] | 114 | dialog.setButtonIcons(new String[] {"download", "cancel"});
|
---|
[3083] | 115 | dialog.showDialog();
|
---|
| 116 | int answer = dialog.getValue();
|
---|
| 117 | if (answer != 1)
|
---|
[3332] | 118 | throw new PluginDownloadException(tr("Download skipped"));
|
---|
[3083] | 119 | }
|
---|
| 120 | try {
|
---|
| 121 | if (pi.downloadlink == null) {
|
---|
[6248] | 122 | String msg = tr("Cannot download plugin ''{0}''. Its download link is not known. Skipping download.", pi.name);
|
---|
| 123 | Main.warn(msg);
|
---|
[3083] | 124 | throw new PluginDownloadException(msg);
|
---|
| 125 | }
|
---|
| 126 | URL url = new URL(pi.downloadlink);
|
---|
[8510] | 127 | synchronized (this) {
|
---|
[9168] | 128 | downloadConnection = HttpClient.create(url)
|
---|
[9309] | 129 | .setAccept(PLUGIN_MIME_TYPES);
|
---|
| 130 | downloadConnection.connect();
|
---|
[3083] | 131 | }
|
---|
[9309] | 132 | try (InputStream in = downloadConnection.getResponse().getContent()) {
|
---|
[9280] | 133 | Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
---|
[3083] | 134 | }
|
---|
[6248] | 135 | } catch (MalformedURLException e) {
|
---|
[8394] | 136 | String msg = tr("Cannot download plugin ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.",
|
---|
| 137 | pi.name, pi.downloadlink);
|
---|
[6248] | 138 | Main.warn(msg);
|
---|
[8394] | 139 | throw new PluginDownloadException(msg, e);
|
---|
[3083] | 140 | } catch (IOException e) {
|
---|
| 141 | if (canceled)
|
---|
| 142 | return;
|
---|
| 143 | throw new PluginDownloadException(e);
|
---|
| 144 | } finally {
|
---|
[8510] | 145 | synchronized (this) {
|
---|
[3083] | 146 | downloadConnection = null;
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[6883] | 151 | @Override
|
---|
| 152 | protected void realRun() throws SAXException, IOException {
|
---|
[3083] | 153 | File pluginDir = Main.pref.getPluginsDirectory();
|
---|
[6883] | 154 | if (!pluginDir.exists() && !pluginDir.mkdirs()) {
|
---|
[9621] | 155 | String message = tr("Failed to create plugin directory ''{0}''", pluginDir.toString());
|
---|
| 156 | lastException = new PluginDownloadException(message);
|
---|
| 157 | Main.error(message);
|
---|
[6883] | 158 | failed.addAll(toUpdate);
|
---|
| 159 | return;
|
---|
[3083] | 160 | }
|
---|
| 161 | getProgressMonitor().setTicksCount(toUpdate.size());
|
---|
| 162 | for (PluginInformation d : toUpdate) {
|
---|
[9621] | 163 | if (canceled)
|
---|
| 164 | return;
|
---|
[8938] | 165 | String message = tr("Downloading Plugin {0}...", d.name);
|
---|
| 166 | Main.info(message);
|
---|
| 167 | progressMonitor.subTask(message);
|
---|
[3083] | 168 | progressMonitor.worked(1);
|
---|
| 169 | File pluginFile = new File(pluginDir, d.name + ".jar.new");
|
---|
| 170 | try {
|
---|
| 171 | download(d, pluginFile);
|
---|
[8510] | 172 | } catch (PluginDownloadException e) {
|
---|
[9621] | 173 | lastException = e;
|
---|
[6643] | 174 | Main.error(e);
|
---|
[3083] | 175 | failed.add(d);
|
---|
| 176 | continue;
|
---|
| 177 | }
|
---|
| 178 | downloaded.add(d);
|
---|
| 179 | }
|
---|
| 180 | PluginHandler.installDownloadedPlugins(false);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /**
|
---|
[4310] | 184 | * Replies true if the task was canceled by the user
|
---|
[3530] | 185 | *
|
---|
[5836] | 186 | * @return <code>true</code> if the task was stopped by the user
|
---|
[3083] | 187 | */
|
---|
| 188 | public boolean isCanceled() {
|
---|
| 189 | return canceled;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /**
|
---|
[8017] | 193 | * Replies the list of plugins whose download has failed.
|
---|
[3530] | 194 | *
|
---|
[8017] | 195 | * @return the list of plugins whose download has failed
|
---|
[3083] | 196 | */
|
---|
| 197 | public Collection<PluginInformation> getFailedPlugins() {
|
---|
| 198 | return failed;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /**
|
---|
[8017] | 202 | * Replies the list of successfully downloaded plugins.
|
---|
[3530] | 203 | *
|
---|
[8017] | 204 | * @return the list of successfully downloaded plugins
|
---|
[3083] | 205 | */
|
---|
| 206 | public Collection<PluginInformation> getDownloadedPlugins() {
|
---|
| 207 | return downloaded;
|
---|
| 208 | }
|
---|
[9621] | 209 |
|
---|
| 210 | /**
|
---|
| 211 | * Replies the last exception that occured during download, or {@code null}.
|
---|
| 212 | * @return the last exception that occured during download, or {@code null}
|
---|
| 213 | * @since 9621
|
---|
| 214 | */
|
---|
| 215 | public Exception getLastException() {
|
---|
| 216 | return lastException;
|
---|
| 217 | }
|
---|
[3083] | 218 | }
|
---|