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

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

fix some Sonar issues

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