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

Last change on this file since 7122 was 7033, checked in by Don-vip, 10 years ago

see #8465 - global use of try-with-resources, according to

  • Property svn:eol-style set to native
File size: 7.9 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.FileOutputStream;
9import java.io.IOException;
10import java.io.InputStream;
11import java.io.OutputStream;
12import java.net.HttpURLConnection;
13import java.net.MalformedURLException;
14import java.net.URL;
15import java.util.Collection;
16import java.util.LinkedList;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.Version;
20import org.openstreetmap.josm.gui.ExtendedDialog;
21import org.openstreetmap.josm.gui.PleaseWaitRunnable;
22import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
23import org.openstreetmap.josm.gui.progress.ProgressMonitor;
24import org.openstreetmap.josm.io.MirroredInputStream;
25import org.openstreetmap.josm.tools.CheckParameterUtil;
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 *
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 HttpURLConnection 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 thrown if toUpdate is null
57 */
58 public PluginDownloadTask(Component parent, Collection<PluginInformation> toUpdate, String title) throws IllegalArgumentException{
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 thrown 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 thrown if toUpdate is null
83 */
84 public void setPluginsToDownload(Collection<PluginInformation> toUpdate) throws IllegalArgumentException{
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
103 protected void download(PluginInformation pi, File file) throws PluginDownloadException{
104 if (pi.mainversion > Version.getInstance().getVersion()) {
105 ExtendedDialog dialog = new ExtendedDialog(
106 progressMonitor.getWindowParent(),
107 tr("Skip download"),
108 new String[] {
109 tr("Download Plugin"),
110 tr("Skip Download") }
111 );
112 dialog.setContent(tr("JOSM version {0} required for plugin {1}.", pi.mainversion, pi.name));
113 dialog.setButtonIcons(new String[] { "download.png", "cancel.png" });
114 dialog.showDialog();
115 int answer = dialog.getValue();
116 if (answer != 1)
117 throw new PluginDownloadException(tr("Download skipped"));
118 }
119 try {
120 if (pi.downloadlink == null) {
121 String msg = tr("Cannot download plugin ''{0}''. Its download link is not known. Skipping download.", pi.name);
122 Main.warn(msg);
123 throw new PluginDownloadException(msg);
124 }
125 URL url = new URL(pi.downloadlink);
126 synchronized(this) {
127 downloadConnection = MirroredInputStream.connectFollowingRedirect(url, PLUGIN_MIME_TYPES);
128 }
129 try (
130 InputStream in = downloadConnection.getInputStream();
131 OutputStream out = new FileOutputStream(file)
132 ) {
133 byte[] buffer = new byte[8192];
134 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
135 out.write(buffer, 0, read);
136 }
137 }
138 } catch (MalformedURLException e) {
139 String msg = tr("Cannot download plugin ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.", pi.name, pi.downloadlink);
140 Main.warn(msg);
141 throw new PluginDownloadException(msg);
142 } catch (IOException e) {
143 if (canceled)
144 return;
145 throw new PluginDownloadException(e);
146 } finally {
147 synchronized(this) {
148 downloadConnection = null;
149 }
150 }
151 }
152
153 @Override
154 protected void realRun() throws SAXException, IOException {
155 File pluginDir = Main.pref.getPluginsDirectory();
156 if (!pluginDir.exists() && !pluginDir.mkdirs()) {
157 lastException = new PluginDownloadException(tr("Failed to create plugin directory ''{0}''", pluginDir.toString()));
158 failed.addAll(toUpdate);
159 return;
160 }
161 getProgressMonitor().setTicksCount(toUpdate.size());
162 for (PluginInformation d : toUpdate) {
163 if (canceled) return;
164 progressMonitor.subTask(tr("Downloading Plugin {0}...", d.name));
165 progressMonitor.worked(1);
166 File pluginFile = new File(pluginDir, d.name + ".jar.new");
167 try {
168 download(d, pluginFile);
169 } catch(PluginDownloadException e) {
170 Main.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 successfully downloaded plugins
190 *
191 * @return the list of successfully downloaded plugins
192 */
193 public Collection<PluginInformation> getFailedPlugins() {
194 return failed;
195 }
196
197 /**
198 * Replies the list of plugins whose download has failed
199 *
200 * @return the list of plugins whose download has failed
201 */
202 public Collection<PluginInformation> getDownloadedPlugins() {
203 return downloaded;
204 }
205}
Note: See TracBrowser for help on using the repository browser.