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

Last change on this file since 9062 was 9059, checked in by Don-vip, 8 years ago

checkstyle

  • 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.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 boolean canceled;
47 private HttpURLConnection 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
102 protected void download(PluginInformation pi, File file) throws PluginDownloadException {
103 if (pi.mainversion > Version.getInstance().getVersion()) {
104 ExtendedDialog dialog = new ExtendedDialog(
105 progressMonitor.getWindowParent(),
106 tr("Skip download"),
107 new String[] {
108 tr("Download Plugin"),
109 tr("Skip Download") }
110 );
111 dialog.setContent(tr("JOSM version {0} required for plugin {1}.", pi.mainversion, pi.name));
112 dialog.setButtonIcons(new String[] {"download", "cancel"});
113 dialog.showDialog();
114 int answer = dialog.getValue();
115 if (answer != 1)
116 throw new PluginDownloadException(tr("Download skipped"));
117 }
118 try {
119 if (pi.downloadlink == null) {
120 String msg = tr("Cannot download plugin ''{0}''. Its download link is not known. Skipping download.", pi.name);
121 Main.warn(msg);
122 throw new PluginDownloadException(msg);
123 }
124 URL url = new URL(pi.downloadlink);
125 synchronized (this) {
126 downloadConnection = CachedFile.connectFollowingRedirect(url, PLUGIN_MIME_TYPES, null);
127 }
128 try (
129 InputStream in = downloadConnection.getInputStream();
130 OutputStream out = new FileOutputStream(file)
131 ) {
132 byte[] buffer = new byte[8192];
133 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
134 out.write(buffer, 0, read);
135 }
136 }
137 } catch (MalformedURLException e) {
138 String msg = tr("Cannot download plugin ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.",
139 pi.name, pi.downloadlink);
140 Main.warn(msg);
141 throw new PluginDownloadException(msg, e);
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 String message = tr("Downloading Plugin {0}...", d.name);
165 Main.info(message);
166 progressMonitor.subTask(message);
167 progressMonitor.worked(1);
168 File pluginFile = new File(pluginDir, d.name + ".jar.new");
169 try {
170 download(d, pluginFile);
171 } catch (PluginDownloadException e) {
172 Main.error(e);
173 failed.add(d);
174 continue;
175 }
176 downloaded.add(d);
177 }
178 PluginHandler.installDownloadedPlugins(false);
179 }
180
181 /**
182 * Replies true if the task was canceled by the user
183 *
184 * @return <code>true</code> if the task was stopped by the user
185 */
186 public boolean isCanceled() {
187 return canceled;
188 }
189
190 /**
191 * Replies the list of plugins whose download has failed.
192 *
193 * @return the list of plugins whose download has failed
194 */
195 public Collection<PluginInformation> getFailedPlugins() {
196 return failed;
197 }
198
199 /**
200 * Replies the list of successfully downloaded plugins.
201 *
202 * @return the list of successfully downloaded plugins
203 */
204 public Collection<PluginInformation> getDownloadedPlugins() {
205 return downloaded;
206 }
207}
Note: See TracBrowser for help on using the repository browser.