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

Last change on this file since 2990 was 2990, checked in by jttt, 14 years ago

Fix some eclipse warnings

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