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