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

Last change on this file since 2610 was 2557, checked in by stoecker, 14 years ago

fixed old file delete

  • Property svn:eol-style set to native
File size: 8.6 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%<?plugins=>"};
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 LinkedList<String> sitenames = new LinkedList<String>();
109 for (String site : getSites()) {
110 try {
111 String filesite = site.replaceAll("%<(.*)>", "");
112 /* replace %<x> with empty string or x=plugins (separated with comma) */
113 String pl = Main.pref.getCollectionAsString("plugins");
114 if(pl != null && pl.length() != 0)
115 site = site.replaceAll("%<(.*)>", "$1"+pl);
116 else
117 site = filesite;
118 BufferedReader r = new BufferedReader(new InputStreamReader(new URL(site).openStream(), "utf-8"));
119 new File(Main.pref.getPreferencesDir()+"plugins").mkdir();
120 String sname = count + "-site-" + filesite.replaceAll("[/:\\\\ <>|]", "_") + ".txt";
121 sitenames.add(sname);
122 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
123 new FileOutputStream(new File(Main.pref.getPluginsDirFile(), sname)), "utf-8"));
124 for (String line = r.readLine(); line != null; line = r.readLine()) {
125 out.append(line+"\n");
126 }
127 r.close();
128 out.close();
129 count++;
130 } catch (IOException x) {
131 }
132 }
133 /* remove old files */
134 File[] pluginFiles = Main.pref.getPluginsDirFile().listFiles();
135 if (pluginFiles != null) {
136 for (File f : pluginFiles) {
137 if (!f.isFile())
138 continue;
139 String fname = f.getName();
140 if(fname.endsWith(".jar"))
141 {
142 for(String s : PluginHandler.oldplugins)
143 {
144 if(fname.equals(s+".jar"))
145 {
146 System.out.println(tr("Delete old plugin {0}",fname));
147 f.delete();
148 }
149 }
150 }
151 else if(!fname.endsWith(".jar.new") && !sitenames.contains(fname))
152 {
153 System.out.println(tr("Delete old plugin file {0}",fname));
154 f.delete();
155 }
156 }
157 }
158 return count;
159 }
160
161 private static boolean download(PluginInformation pd, File file) {
162 if(pd.mainversion > Version.getInstance().getVersion())
163 {
164 ExtendedDialog dialog = new ExtendedDialog(
165 Main.parent,
166 tr("Skip download"),
167 new String[] {tr("Download Plugin"), tr("Skip Download")}
168 );
169 dialog.setContent(tr("JOSM version {0} required for plugin {1}.", pd.mainversion, pd.name));
170 dialog.setButtonIcons(new String[] {"download.png", "cancel.png"});
171 dialog.showDialog();
172 int answer = dialog.getValue();
173 if (answer != 1)
174 return false;
175 }
176
177 try {
178 InputStream in = new URL(pd.downloadlink).openStream();
179 OutputStream out = new FileOutputStream(file);
180 byte[] buffer = new byte[8192];
181 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
182 out.write(buffer, 0, read);
183 }
184 out.close();
185 in.close();
186 new PluginInformation(file);
187 return true;
188 } catch (Exception e) {
189 e.printStackTrace();
190 }
191 file.delete(); /* cleanup */
192 return false;
193 }
194
195 public static void update(Collection<PluginInformation> update, boolean restart) {
196 Main.worker.execute(new UpdateTask(update, true, restart));
197 }
198
199 public Collection<PluginInformation> download(Collection<PluginInformation> download) {
200 // Execute task in current thread instead of executing it in other thread and waiting for result
201 // Waiting for result is not a good idea because the waiting thread will probably be either EDT
202 // or worker thread. Blocking one of these threads will cause deadlock
203 UpdateTask t = new UpdateTask(download, false, true);
204 t.run();
205 return t.failed;
206 }
207
208 public static boolean moveUpdatedPlugins() {
209 File pluginDir = Main.pref.getPluginsDirFile();
210 boolean ok = true;
211 if (pluginDir.exists() && pluginDir.isDirectory() && pluginDir.canWrite()) {
212 final File[] files = pluginDir.listFiles(new FilenameFilter() {
213 public boolean accept(File dir, String name) {
214 return name.endsWith(".new");
215 }});
216 for (File updatedPlugin : files) {
217 final String filePath = updatedPlugin.getPath();
218 File plugin = new File(filePath.substring(0, filePath.length() - 4));
219 ok = (plugin.delete() || !plugin.exists()) && updatedPlugin.renameTo(plugin) && ok;
220 }
221 }
222 return ok;
223 }
224}
Note: See TracBrowser for help on using the repository browser.