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

Last change on this file since 4153 was 4087, checked in by bastiK, 13 years ago

PaintVisitor refactoring, includes hook for external MapRenderers (author: Gubaer)

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