1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileOutputStream;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.io.OutputStream;
|
---|
12 | import java.net.HttpURLConnection;
|
---|
13 | import java.net.MalformedURLException;
|
---|
14 | import java.net.URL;
|
---|
15 | import java.util.Collection;
|
---|
16 | import java.util.LinkedList;
|
---|
17 | import java.util.logging.Logger;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.Version;
|
---|
21 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
22 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
23 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
24 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
25 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
26 | import 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 | */
|
---|
36 | public 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 = new LinkedList<PluginInformation>();
|
---|
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.addAll(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.addAll(toUpdate);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Sets the collection of plugins to update.
|
---|
77 | *
|
---|
78 | * @param toUpdate the collection of plugins to update. Must not be null.
|
---|
79 | * @throws IllegalArgumentException thrown if toUpdate is null
|
---|
80 | */
|
---|
81 | public void setPluginsToDownload(Collection<PluginInformation> toUpdate) throws IllegalArgumentException{
|
---|
82 | CheckParameterUtil.ensureParameterNotNull(toUpdate, "toUpdate");
|
---|
83 | this.toUpdate.clear();
|
---|
84 | this.toUpdate.addAll(toUpdate);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override protected void cancel() {
|
---|
88 | this.canceled = true;
|
---|
89 | synchronized(this) {
|
---|
90 | if (downloadConnection != null) {
|
---|
91 | downloadConnection.disconnect();
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override protected void finish() {}
|
---|
97 |
|
---|
98 | protected void download(PluginInformation pi, File file) throws PluginDownloadException{
|
---|
99 | if (pi.mainversion > Version.getInstance().getVersion()) {
|
---|
100 | ExtendedDialog dialog = new ExtendedDialog(
|
---|
101 | Main.parent,
|
---|
102 | tr("Skip download"),
|
---|
103 | new String[] {
|
---|
104 | tr("Download Plugin"),
|
---|
105 | tr("Skip Download") }
|
---|
106 | );
|
---|
107 | dialog.setContent(tr("JOSM version {0} required for plugin {1}.", pi.mainversion, pi.name));
|
---|
108 | dialog.setButtonIcons(new String[] { "download.png", "cancel.png" });
|
---|
109 | dialog.showDialog();
|
---|
110 | int answer = dialog.getValue();
|
---|
111 | if (answer != 1)
|
---|
112 | throw new PluginDownloadException(tr("Download skipped"));
|
---|
113 | }
|
---|
114 | OutputStream out = null;
|
---|
115 | InputStream in = null;
|
---|
116 | try {
|
---|
117 | if (pi.downloadlink == null) {
|
---|
118 | String msg = tr("Warning: Cannot download plugin ''{0}''. Its download link is not known. Skipping download.", pi.name);
|
---|
119 | System.err.println(msg);
|
---|
120 | throw new PluginDownloadException(msg);
|
---|
121 | }
|
---|
122 | URL url = new URL(pi.downloadlink);
|
---|
123 | synchronized(this) {
|
---|
124 | downloadConnection = (HttpURLConnection)url.openConnection();
|
---|
125 | downloadConnection.setRequestProperty("Cache-Control", "no-cache");
|
---|
126 | downloadConnection.setRequestProperty("User-Agent",Version.getInstance().getAgentString());
|
---|
127 | downloadConnection.setRequestProperty("Host", url.getHost());
|
---|
128 | downloadConnection.connect();
|
---|
129 | }
|
---|
130 | in = downloadConnection.getInputStream();
|
---|
131 | out = new FileOutputStream(file);
|
---|
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 | out.close();
|
---|
137 | in.close();
|
---|
138 | } catch(MalformedURLException e) {
|
---|
139 | String msg = tr("Warning: Cannot download plugin ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.", pi.name, pi.downloadlink);
|
---|
140 | System.err.println(msg);
|
---|
141 | throw new PluginDownloadException(msg);
|
---|
142 | } catch (IOException e) {
|
---|
143 | if (canceled)
|
---|
144 | return;
|
---|
145 | throw new PluginDownloadException(e);
|
---|
146 | } finally {
|
---|
147 | if (in != null) {
|
---|
148 | try {
|
---|
149 | in.close();
|
---|
150 | } catch(IOException e) { /* ignore */}
|
---|
151 | }
|
---|
152 | synchronized(this) {
|
---|
153 | downloadConnection = null;
|
---|
154 | }
|
---|
155 | if (out != null) {
|
---|
156 | try {
|
---|
157 | out.close();
|
---|
158 | } catch(IOException e) { /* ignore */}
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override protected void realRun() throws SAXException, IOException {
|
---|
164 | File pluginDir = Main.pref.getPluginsDirectory();
|
---|
165 | if (!pluginDir.exists()) {
|
---|
166 | if (!pluginDir.mkdirs()) {
|
---|
167 | lastException = new PluginDownloadException(tr("Failed to create plugin directory ''{0}''", pluginDir.toString()));
|
---|
168 | failed.addAll(toUpdate);
|
---|
169 | return;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | getProgressMonitor().setTicksCount(toUpdate.size());
|
---|
173 | for (PluginInformation d : toUpdate) {
|
---|
174 | if (canceled) return;
|
---|
175 | progressMonitor.subTask(tr("Downloading Plugin {0}...", d.name));
|
---|
176 | progressMonitor.worked(1);
|
---|
177 | File pluginFile = new File(pluginDir, d.name + ".jar.new");
|
---|
178 | try {
|
---|
179 | download(d, pluginFile);
|
---|
180 | } catch(PluginDownloadException e) {
|
---|
181 | e.printStackTrace();
|
---|
182 | failed.add(d);
|
---|
183 | continue;
|
---|
184 | }
|
---|
185 | downloaded.add(d);
|
---|
186 | }
|
---|
187 | PluginHandler.installDownloadedPlugins(false);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Replies true if the task was cancelled by the user
|
---|
192 | *
|
---|
193 | * @return
|
---|
194 | */
|
---|
195 | public boolean isCanceled() {
|
---|
196 | return canceled;
|
---|
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> getFailedPlugins() {
|
---|
205 | return failed;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Replies the list of plugins whose download has failed
|
---|
210 | *
|
---|
211 | * @return the list of plugins whose download has failed
|
---|
212 | */
|
---|
213 | public Collection<PluginInformation> getDownloadedPlugins() {
|
---|
214 | return downloaded;
|
---|
215 | }
|
---|
216 | }
|
---|