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

Last change on this file since 12627 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 8.2 KB
Line 
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;
12import java.nio.file.Files;
13import java.nio.file.StandardCopyOption;
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;
24import org.openstreetmap.josm.tools.HttpClient;
25import org.openstreetmap.josm.tools.Logging;
26import org.xml.sax.SAXException;
27
28/**
29 * Asynchronous task for downloading a collection of plugins.
30 *
31 * When the task is finished {@link #getDownloadedPlugins()} replies the list of downloaded plugins
32 * and {@link #getFailedPlugins()} replies the list of failed plugins.
33 * @since 2817
34 */
35public class PluginDownloadTask extends PleaseWaitRunnable {
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";
42
43 private final Collection<PluginInformation> toUpdate = new LinkedList<>();
44 private final Collection<PluginInformation> failed = new LinkedList<>();
45 private final Collection<PluginInformation> downloaded = new LinkedList<>();
46 private Exception lastException;
47 private boolean canceled;
48 private HttpClient downloadConnection;
49
50 /**
51 * Creates the download task
52 *
53 * @param parent the parent component relative to which the {@link org.openstreetmap.josm.gui.PleaseWaitDialog} is displayed
54 * @param toUpdate a collection of plugin descriptions for plugins to update/download. Must not be null.
55 * @param title the title to display in the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
56 * @throws IllegalArgumentException if toUpdate is null
57 */
58 public PluginDownloadTask(Component parent, Collection<PluginInformation> toUpdate, String title) {
59 super(parent, title == null ? "" : title, false /* don't ignore exceptions */);
60 CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
61 this.toUpdate.addAll(toUpdate);
62 }
63
64 /**
65 * Creates the task
66 *
67 * @param monitor a progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null
68 * @param toUpdate a collection of plugin descriptions for plugins to update/download. Must not be null.
69 * @param title the title to display in the {@link org.openstreetmap.josm.gui.PleaseWaitDialog}
70 * @throws IllegalArgumentException if toUpdate is null
71 */
72 public PluginDownloadTask(ProgressMonitor monitor, Collection<PluginInformation> toUpdate, String title) {
73 super(title, monitor == null ? NullProgressMonitor.INSTANCE : monitor, false /* don't ignore exceptions */);
74 CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
75 this.toUpdate.addAll(toUpdate);
76 }
77
78 /**
79 * Sets the collection of plugins to update.
80 *
81 * @param toUpdate the collection of plugins to update. Must not be null.
82 * @throws IllegalArgumentException if toUpdate is null
83 */
84 public void setPluginsToDownload(Collection<PluginInformation> toUpdate) {
85 CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
86 this.toUpdate.clear();
87 this.toUpdate.addAll(toUpdate);
88 }
89
90 @Override
91 protected void cancel() {
92 this.canceled = true;
93 synchronized (this) {
94 if (downloadConnection != null) {
95 downloadConnection.disconnect();
96 }
97 }
98 }
99
100 @Override
101 protected void finish() {
102 // Do nothing. Error/success feedback is managed in PluginPreference.notifyDownloadResults()
103 }
104
105 protected void download(PluginInformation pi, File file) throws PluginDownloadException {
106 if (pi.mainversion > Version.getInstance().getVersion()) {
107 ExtendedDialog dialog = new ExtendedDialog(
108 progressMonitor.getWindowParent(),
109 tr("Skip download"),
110 tr("Download Plugin"), tr("Skip Download")
111 );
112 dialog.setContent(tr("JOSM version {0} required for plugin {1}.", pi.mainversion, pi.name));
113 dialog.setButtonIcons("download", "cancel");
114 if (dialog.showDialog().getValue() != 1)
115 throw new PluginDownloadException(tr("Download skipped"));
116 }
117 try {
118 if (pi.downloadlink == null) {
119 String msg = tr("Cannot download plugin ''{0}''. Its download link is not known. Skipping download.", pi.name);
120 Logging.warn(msg);
121 throw new PluginDownloadException(msg);
122 }
123 URL url = new URL(pi.downloadlink);
124 synchronized (this) {
125 downloadConnection = HttpClient.create(url).setAccept(PLUGIN_MIME_TYPES);
126 downloadConnection.connect();
127 }
128 try (InputStream in = downloadConnection.getResponse().getContent()) {
129 Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
130 }
131 } catch (MalformedURLException e) {
132 String msg = tr("Cannot download plugin ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.",
133 pi.name, pi.downloadlink);
134 Logging.warn(msg);
135 throw new PluginDownloadException(msg, e);
136 } catch (IOException e) {
137 if (canceled)
138 return;
139 throw new PluginDownloadException(e);
140 } finally {
141 synchronized (this) {
142 downloadConnection = null;
143 }
144 }
145 }
146
147 @Override
148 protected void realRun() throws SAXException, IOException {
149 File pluginDir = Main.pref.getPluginsDirectory();
150 if (!pluginDir.exists() && !pluginDir.mkdirs()) {
151 String message = tr("Failed to create plugin directory ''{0}''", pluginDir.toString());
152 lastException = new PluginDownloadException(message);
153 Logging.error(message);
154 failed.addAll(toUpdate);
155 return;
156 }
157 getProgressMonitor().setTicksCount(toUpdate.size());
158 for (PluginInformation d : toUpdate) {
159 if (canceled)
160 return;
161 String message = tr("Downloading Plugin {0}...", d.name);
162 Logging.info(message);
163 progressMonitor.subTask(message);
164 progressMonitor.worked(1);
165 File pluginFile = new File(pluginDir, d.name + ".jar.new");
166 try {
167 download(d, pluginFile);
168 } catch (PluginDownloadException e) {
169 lastException = e;
170 Logging.error(e);
171 failed.add(d);
172 continue;
173 }
174 downloaded.add(d);
175 }
176 PluginHandler.installDownloadedPlugins(false);
177 }
178
179 /**
180 * Replies true if the task was canceled by the user
181 *
182 * @return <code>true</code> if the task was stopped by the user
183 */
184 public boolean isCanceled() {
185 return canceled;
186 }
187
188 /**
189 * Replies the list of plugins whose download has failed.
190 *
191 * @return the list of plugins whose download has failed
192 */
193 public Collection<PluginInformation> getFailedPlugins() {
194 return failed;
195 }
196
197 /**
198 * Replies the list of successfully downloaded plugins.
199 *
200 * @return the list of successfully downloaded plugins
201 */
202 public Collection<PluginInformation> getDownloadedPlugins() {
203 return downloaded;
204 }
205
206 /**
207 * Replies the last exception that occured during download, or {@code null}.
208 * @return the last exception that occured during download, or {@code null}
209 * @since 9621
210 */
211 public Exception getLastException() {
212 return lastException;
213 }
214}
Note: See TracBrowser for help on using the repository browser.