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

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

sonar - squid:S3878 - Arrays should not be created for varargs parameters

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