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

Last change on this file since 8471 was 8443, checked in by Don-vip, 9 years ago

remove extra whitespaces

  • Property svn:eol-style set to native
File size: 7.8 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.CachedFile;
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 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
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", "cancel"});
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 = CachedFile.connectFollowingRedirect(url, PLUGIN_MIME_TYPES, null);
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.",
140 pi.name, pi.downloadlink);
141 Main.warn(msg);
142 throw new PluginDownloadException(msg, e);
143 } catch (IOException e) {
144 if (canceled)
145 return;
146 throw new PluginDownloadException(e);
147 } finally {
148 synchronized(this) {
149 downloadConnection = null;
150 }
151 }
152 }
153
154 @Override
155 protected void realRun() throws SAXException, IOException {
156 File pluginDir = Main.pref.getPluginsDirectory();
157 if (!pluginDir.exists() && !pluginDir.mkdirs()) {
158 /*lastException =*/ new PluginDownloadException(tr("Failed to create plugin directory ''{0}''", pluginDir.toString()));
159 failed.addAll(toUpdate);
160 return;
161 }
162 getProgressMonitor().setTicksCount(toUpdate.size());
163 for (PluginInformation d : toUpdate) {
164 if (canceled) return;
165 progressMonitor.subTask(tr("Downloading Plugin {0}...", d.name));
166 progressMonitor.worked(1);
167 File pluginFile = new File(pluginDir, d.name + ".jar.new");
168 try {
169 download(d, pluginFile);
170 } catch(PluginDownloadException e) {
171 Main.error(e);
172 failed.add(d);
173 continue;
174 }
175 downloaded.add(d);
176 }
177 PluginHandler.installDownloadedPlugins(false);
178 }
179
180 /**
181 * Replies true if the task was canceled by the user
182 *
183 * @return <code>true</code> if the task was stopped by the user
184 */
185 public boolean isCanceled() {
186 return canceled;
187 }
188
189 /**
190 * Replies the list of plugins whose download has failed.
191 *
192 * @return the list of plugins whose download has failed
193 */
194 public Collection<PluginInformation> getFailedPlugins() {
195 return failed;
196 }
197
198 /**
199 * Replies the list of successfully downloaded plugins.
200 *
201 * @return the list of successfully downloaded plugins
202 */
203 public Collection<PluginInformation> getDownloadedPlugins() {
204 return downloaded;
205 }
206}
Note: See TracBrowser for help on using the repository browser.