source: josm/trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java@ 13426

Last change on this file since 13426 was 13294, checked in by Don-vip, 6 years ago

fix #15554 - fix plugin classloader after plugin update

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