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