source: josm/trunk/src/org/openstreetmap/josm/plugins/ReadLocalPluginInformationTask.java@ 3318

Last change on this file since 3318 was 3287, checked in by stoecker, 14 years ago

fix #4706 and fix #5066 - plugin handling

  • Property svn:eol-style set to native
File size: 8.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.FilenameFilter;
9import java.io.IOException;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.HashMap;
13import java.util.List;
14import java.util.Map;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.gui.PleaseWaitRunnable;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19import org.openstreetmap.josm.io.OsmTransferException;
20import org.xml.sax.SAXException;
21
22/**
23 * This is an asynchronous task for reading plugin information from the files
24 * in the local plugin repositories.
25 *
26 * It scans the files in the local plugins repository (see {@see Preferences#getPluginsDirectory()}
27 * and extracts plugin information from three kind of files:
28 * <ul>
29 * <li>.jar-files, assuming that they represent plugin jars</li>
30 * <li>.jar.new-files, assuming that these are downloaded but not yet installed plugins</li>
31 * <li>cached lists of available plugins, downloaded for instance from
32 * <a href="http://josm.openstreetmap.de/plugins">http://josm.openstreetmap.de/plugins</a></li>
33 * </ul>
34 *
35 */
36public class ReadLocalPluginInformationTask extends PleaseWaitRunnable {
37 private Map<String, PluginInformation> availablePlugins;
38 private boolean canceled;
39
40 public ReadLocalPluginInformationTask() {
41 super(tr("Reading local plugin information.."), false);
42 availablePlugins = new HashMap<String, PluginInformation>();
43 }
44
45 public ReadLocalPluginInformationTask(ProgressMonitor monitor) {
46 super(tr("Reading local plugin information.."),monitor, false);
47 availablePlugins = new HashMap<String, PluginInformation>();
48 }
49
50 @Override
51 protected void cancel() {
52 canceled = true;
53 }
54
55 @Override
56 protected void finish() {}
57
58 protected void processJarFile(File f, String pluginName) throws PluginException{
59 PluginInformation info = new PluginInformation(
60 f,
61 pluginName
62 );
63 if (!availablePlugins.containsKey(info.getName())) {
64 info.localversion = info.version;
65 availablePlugins.put(info.getName(), info);
66 } else {
67 availablePlugins.get(info.getName()).localversion = info.version;
68 }
69 }
70
71 protected void scanSiteCacheFiles(ProgressMonitor monitor, File pluginsDirectory) {
72 File[] siteCacheFiles = pluginsDirectory.listFiles(
73 new FilenameFilter() {
74 public boolean accept(File dir, String name) {
75 return name.matches("^([0-9]+-)?site.*\\.txt$");
76 }
77 }
78 );
79 if (siteCacheFiles == null || siteCacheFiles.length == 0)
80 return;
81 monitor.subTask(tr("Processing plugin site cache files..."));
82 monitor.setTicksCount(siteCacheFiles.length);
83 for (File f: siteCacheFiles) {
84 String fname = f.getName();
85 monitor.setCustomText(tr("Processing file ''{0}''", fname));
86 try {
87 processLocalPluginInformationFile(f);
88 } catch(PluginListParseException e) {
89 System.err.println(tr("Warning: Failed to scan file ''{0}'' for plugin information. Skipping.", fname));
90 e.printStackTrace();
91 }
92 monitor.worked(1);
93 }
94 }
95
96 protected void scanPluginFiles(ProgressMonitor monitor, File pluginsDirectory) {
97 File[] pluginFiles = pluginsDirectory.listFiles(
98 new FilenameFilter() {
99 public boolean accept(File dir, String name) {
100 return name.endsWith(".jar") || name.endsWith(".jar.new");
101 }
102 }
103 );
104 if (pluginFiles == null || pluginFiles.length == 0)
105 return;
106 monitor.subTask(tr("Processing plugin files..."));
107 monitor.setTicksCount(pluginFiles.length);
108 for (File f: pluginFiles) {
109 String fname = f.getName();
110 monitor.setCustomText(tr("Processing file ''{0}''", fname));
111 try {
112 if (fname.endsWith(".jar")) {
113 String pluginName = fname.substring(0, fname.length() - 4);
114 processJarFile(f, pluginName);
115 } else if (fname.endsWith(".jar.new")) {
116 String pluginName = fname.substring(0, fname.length() - 8);
117 processJarFile(f, pluginName);
118 }
119 } catch(PluginException e){
120 System.err.println(tr("Warning: Failed to scan file ''{0}'' for plugin information. Skipping.", fname));
121 e.printStackTrace();
122 }
123 monitor.worked(1);
124 }
125 }
126
127 protected void scanLocalPluginRepository(ProgressMonitor monitor, File pluginsDirectory) {
128 if (pluginsDirectory == null) return;
129 try {
130 monitor.beginTask("");
131 scanSiteCacheFiles(monitor, pluginsDirectory);
132 scanPluginFiles(monitor, pluginsDirectory);
133 } finally {
134 monitor.setCustomText("");
135 monitor.finishTask();
136 }
137 }
138
139 protected void processLocalPluginInformationFile(File file) throws PluginListParseException{
140 FileInputStream fin = null;
141 try {
142 fin = new FileInputStream(file);
143 List<PluginInformation> pis = new PluginListParser().parse(fin);
144 for (PluginInformation pi : pis) {
145 // we always keep plugin information from a plugin site because it
146 // includes information not available in the plugin jars Manifest, i.e.
147 // the download link or localized descriptions
148 //
149 availablePlugins.put(pi.name, pi);
150 }
151 } catch(IOException e) {
152 throw new PluginListParseException(e);
153 } finally {
154 if (fin != null) {
155 try {
156 fin.close();
157 } catch(IOException e){ /* ignore */}
158 }
159 }
160 }
161
162 protected void analyseInProcessPlugins() {
163 for (PluginProxy proxy : PluginHandler.pluginList) {
164 PluginInformation info = proxy.getPluginInformation();
165 if (canceled)return;
166 if (!availablePlugins.containsKey(info.name)) {
167 availablePlugins.put(info.name, info);
168 } else {
169 availablePlugins.get(info.name).localversion = info.localversion;
170 }
171 }
172 }
173
174 protected void filterOldPlugins() {
175 for (String p : PluginHandler.DEPRECATED_PLUGINS) {
176 if (canceled)return;
177 if (availablePlugins.containsKey(p)) {
178 availablePlugins.remove(p);
179 }
180 }
181 }
182
183 @Override
184 protected void realRun() throws SAXException, IOException, OsmTransferException {
185 Collection<String> pluginLocations = PluginInformation.getPluginLocations();
186 getProgressMonitor().setTicksCount(pluginLocations.size() + 2);
187 if (canceled) return;
188 for (String location : pluginLocations) {
189 scanLocalPluginRepository(
190 getProgressMonitor().createSubTaskMonitor(1, false),
191 new File(location)
192 );
193 getProgressMonitor().worked(1);
194 if (canceled)return;
195 }
196 analyseInProcessPlugins();
197 getProgressMonitor().worked(1);
198 if (canceled)return;
199 filterOldPlugins();
200 getProgressMonitor().worked(1);
201 }
202
203 /**
204 * Replies information about available plugins detected by this task.
205 *
206 * @return information about available plugins detected by this task.
207 */
208 public List<PluginInformation> getAvailablePlugins() {
209 return new ArrayList<PluginInformation>(availablePlugins.values());
210 }
211
212 /**
213 * Replies true if the task was cancelled by the user
214 *
215 * @return true if the task was cancelled by the user
216 */
217 public boolean isCanceled() {
218 return canceled;
219 }
220}
Note: See TracBrowser for help on using the repository browser.