source: josm/trunk/src/org/openstreetmap/josm/plugins/PluginDownloader.java@ 2491

Last change on this file since 2491 was 2372, checked in by stoecker, 14 years ago

fix #3391 - update plugins after josm update

  • Property svn:eol-style set to native
File size: 7.2 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2/**
3 *
4 */
5package org.openstreetmap.josm.plugins;
6
7import static org.openstreetmap.josm.tools.I18n.tr;
8import static org.openstreetmap.josm.tools.I18n.trn;
9
10import java.io.BufferedReader;
11import java.io.BufferedWriter;
12import java.io.File;
13import java.io.FileOutputStream;
14import java.io.FilenameFilter;
15import java.io.IOException;
16import java.io.InputStream;
17import java.io.InputStreamReader;
18import java.io.OutputStream;
19import java.io.OutputStreamWriter;
20import java.net.URL;
21import java.util.Arrays;
22import java.util.Collection;
23import java.util.LinkedList;
24
25import javax.swing.JOptionPane;
26
27import org.openstreetmap.josm.Main;
28import org.openstreetmap.josm.actions.AboutAction;
29import org.openstreetmap.josm.data.Version;
30import org.openstreetmap.josm.gui.ExtendedDialog;
31import org.openstreetmap.josm.gui.PleaseWaitRunnable;
32import org.xml.sax.SAXException;
33
34public class PluginDownloader {
35
36 private static final class UpdateTask extends PleaseWaitRunnable {
37 private final Collection<PluginInformation> toUpdate;
38 public final Collection<PluginInformation> failed = new LinkedList<PluginInformation>();
39 private String errors = "";
40 private int count = 0;
41 private boolean restart;
42
43 private UpdateTask(Collection<PluginInformation> toUpdate, boolean up, boolean restart) {
44 super(up ? tr("Update Plugins") : tr("Download Plugins"));
45 this.toUpdate = toUpdate;
46 this.restart = restart;
47 }
48
49 @Override protected void cancel() {
50 finish();
51 }
52
53 @Override protected void finish() {
54 if (errors.length() > 0) {
55 JOptionPane.showMessageDialog(
56 Main.parent,
57 tr("There were problems with the following plugins:\n\n {0}",errors),
58 tr("Error"),
59 JOptionPane.ERROR_MESSAGE
60 );
61 } else {
62 String txt = trn("{0} Plugin successfully downloaded.", "{0} Plugins successfully downloaded.", count, count);
63 if(restart)
64 txt += "\n"+tr("Please restart JOSM.");
65
66 JOptionPane.showMessageDialog(
67 Main.parent,
68 txt,
69 tr("Information"),
70 JOptionPane.INFORMATION_MESSAGE
71 );
72 }
73 }
74
75 @Override protected void realRun() throws SAXException, IOException {
76 File pluginDir = Main.pref.getPluginsDirFile();
77 if (!pluginDir.exists()) {
78 pluginDir.mkdirs();
79 }
80 progressMonitor.setTicksCount(toUpdate.size());
81 for (PluginInformation d : toUpdate) {
82 progressMonitor.subTask(tr("Downloading Plugin {0}...", d.name));
83 progressMonitor.worked(1);
84 File pluginFile = new File(pluginDir, d.name + ".jar.new");
85 if(download(d, pluginFile)) {
86 count++;
87 } else
88 {
89 errors += d.name + "\n";
90 failed.add(d);
91 }
92 }
93 PluginDownloader.moveUpdatedPlugins();
94 }
95 }
96
97 private final static String[] pluginSites = {"http://josm.openstreetmap.de/plugin"};
98
99 public static Collection<String> getSites() {
100 return Main.pref.getCollection("pluginmanager.sites", Arrays.asList(pluginSites));
101 }
102 public static void setSites(Collection<String> c) {
103 Main.pref.putCollection("pluginmanager.sites", c);
104 }
105
106 public static int downloadDescription() {
107 int count = 0;
108 for (String site : getSites()) {
109 /* TODO: remove old site files (everything except .jar) */
110 try {
111 BufferedReader r = new BufferedReader(new InputStreamReader(new URL(site).openStream(), "utf-8"));
112 new File(Main.pref.getPreferencesDir()+"plugins").mkdir();
113 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
114 new FileOutputStream(new File(Main.pref.getPluginsDirFile(),
115 count + "-site-" + site.replaceAll("[/:\\\\ <>|]", "_") + ".txt")), "utf-8"));
116 for (String line = r.readLine(); line != null; line = r.readLine()) {
117 out.append(line+"\n");
118 }
119 r.close();
120 out.close();
121 count++;
122 } catch (IOException x) {
123 }
124 }
125 return count;
126 }
127
128 private static boolean download(PluginInformation pd, File file) {
129 if(pd.mainversion > Version.getInstance().getVersion())
130 {
131 ExtendedDialog dialog = new ExtendedDialog(
132 Main.parent,
133 tr("Skip download"),
134 new String[] {tr("Download Plugin"), tr("Skip Download")}
135 );
136 dialog.setContent(tr("JOSM version {0} required for plugin {1}.", pd.mainversion, pd.name));
137 dialog.setButtonIcons(new String[] {"download.png", "cancel.png"});
138 dialog.showDialog();
139 int answer = dialog.getValue();
140 if (answer != 1)
141 return false;
142 }
143
144 try {
145 InputStream in = new URL(pd.downloadlink).openStream();
146 OutputStream out = new FileOutputStream(file);
147 byte[] buffer = new byte[8192];
148 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
149 out.write(buffer, 0, read);
150 }
151 out.close();
152 in.close();
153 new PluginInformation(file);
154 return true;
155 } catch (Exception e) {
156 e.printStackTrace();
157 }
158 file.delete(); /* cleanup */
159 return false;
160 }
161
162 public static void update(Collection<PluginInformation> update, boolean restart) {
163 Main.worker.execute(new UpdateTask(update, true, restart));
164 }
165
166 public Collection<PluginInformation> download(Collection<PluginInformation> download) {
167 // Execute task in current thread instead of executing it in other thread and waiting for result
168 // Waiting for result is not a good idea because the waiting thread will probably be either EDT
169 // or worker thread. Blocking one of these threads will cause deadlock
170 UpdateTask t = new UpdateTask(download, false, true);
171 t.run();
172 return t.failed;
173 }
174
175 public static boolean moveUpdatedPlugins() {
176 File pluginDir = Main.pref.getPluginsDirFile();
177 boolean ok = true;
178 if (pluginDir.exists() && pluginDir.isDirectory() && pluginDir.canWrite()) {
179 final File[] files = pluginDir.listFiles(new FilenameFilter() {
180 public boolean accept(File dir, String name) {
181 return name.endsWith(".new");
182 }});
183 for (File updatedPlugin : files) {
184 final String filePath = updatedPlugin.getPath();
185 File plugin = new File(filePath.substring(0, filePath.length() - 4));
186 ok = (plugin.delete() || !plugin.exists()) && updatedPlugin.renameTo(plugin) && ok;
187 }
188 }
189 return ok;
190 }
191}
Note: See TracBrowser for help on using the repository browser.