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

Last change on this file since 6388 was 6248, checked in by Don-vip, 11 years ago

Rework console output:

  • new log level "error"
  • Replace nearly all calls to system.out and system.err to Main.(error|warn|info|debug)
  • Remove some unnecessary debug output
  • Some messages are modified (removal of "Info", "Warning", "Error" from the message itself -> notable i18n impact but limited to console error messages not seen by the majority of users, so that's ok)
  • Property svn:eol-style set to native
File size: 9.5 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.openstreetmap.josm.tools.ImageProvider;
21import org.openstreetmap.josm.tools.Utils;
22import org.xml.sax.SAXException;
23
24/**
25 * This is an asynchronous task for reading plugin information from the files
26 * in the local plugin repositories.
27 *
28 * It scans the files in the local plugins repository (see {@link org.openstreetmap.josm.data.Preferences#getPluginsDirectory()}
29 * and extracts plugin information from three kind of files:
30 * <ul>
31 * <li>.jar files, assuming that they represent plugin jars</li>
32 * <li>.jar.new files, assuming that these are downloaded but not yet installed plugins</li>
33 * <li>cached lists of available plugins, downloaded for instance from
34 * <a href="http://josm.openstreetmap.de/plugins">http://josm.openstreetmap.de/plugins</a></li>
35 * </ul>
36 *
37 */
38public class ReadLocalPluginInformationTask extends PleaseWaitRunnable {
39 private Map<String, PluginInformation> availablePlugins;
40 private boolean canceled;
41
42 public ReadLocalPluginInformationTask() {
43 super(tr("Reading local plugin information.."), false);
44 availablePlugins = new HashMap<String, PluginInformation>();
45 }
46
47 public ReadLocalPluginInformationTask(ProgressMonitor monitor) {
48 super(tr("Reading local plugin information.."),monitor, false);
49 availablePlugins = new HashMap<String, PluginInformation>();
50 }
51
52 @Override
53 protected void cancel() {
54 canceled = true;
55 }
56
57 @Override
58 protected void finish() {}
59
60 protected void processJarFile(File f, String pluginName) throws PluginException{
61 PluginInformation info = new PluginInformation(
62 f,
63 pluginName
64 );
65 if (!availablePlugins.containsKey(info.getName())) {
66 info.updateLocalInfo(info);
67 availablePlugins.put(info.getName(), info);
68 } else {
69 PluginInformation current = availablePlugins.get(info.getName());
70 current.updateFromJar(info);
71 }
72 }
73
74 protected void scanSiteCacheFiles(ProgressMonitor monitor, File pluginsDirectory) {
75 File[] siteCacheFiles = pluginsDirectory.listFiles(
76 new FilenameFilter() {
77 @Override
78 public boolean accept(File dir, String name) {
79 return name.matches("^([0-9]+-)?site.*\\.txt$");
80 }
81 }
82 );
83 if (siteCacheFiles == null || siteCacheFiles.length == 0)
84 return;
85 monitor.subTask(tr("Processing plugin site cache files..."));
86 monitor.setTicksCount(siteCacheFiles.length);
87 for (File f: siteCacheFiles) {
88 String fname = f.getName();
89 monitor.setCustomText(tr("Processing file ''{0}''", fname));
90 try {
91 processLocalPluginInformationFile(f);
92 } catch(PluginListParseException e) {
93 Main.warn(tr("Failed to scan file ''{0}'' for plugin information. Skipping.", fname));
94 e.printStackTrace();
95 }
96 monitor.worked(1);
97 }
98 }
99
100 protected void scanIconCacheFiles(ProgressMonitor monitor, File pluginsDirectory) {
101 File[] siteCacheFiles = pluginsDirectory.listFiles(
102 new FilenameFilter() {
103 @Override
104 public boolean accept(File dir, String name) {
105 return name.matches("^([0-9]+-)?site.*plugin-icons\\.zip$");
106 }
107 }
108 );
109 if (siteCacheFiles == null || siteCacheFiles.length == 0)
110 return;
111 monitor.subTask(tr("Processing plugin site cache icon files..."));
112 monitor.setTicksCount(siteCacheFiles.length);
113 for (File f: siteCacheFiles) {
114 String fname = f.getName();
115 monitor.setCustomText(tr("Processing file ''{0}''", fname));
116 for (PluginInformation pi : availablePlugins.values()) {
117 if (pi.icon == null && pi.iconPath != null) {
118 pi.icon = new ImageProvider(pi.name+".jar/"+pi.iconPath)
119 .setArchive(f)
120 .setMaxWidth(24)
121 .setMaxHeight(24)
122 .setOptional(true).get();
123 }
124 }
125 monitor.worked(1);
126 }
127 }
128
129 protected void scanPluginFiles(ProgressMonitor monitor, File pluginsDirectory) {
130 File[] pluginFiles = pluginsDirectory.listFiles(
131 new FilenameFilter() {
132 @Override
133 public boolean accept(File dir, String name) {
134 return name.endsWith(".jar") || name.endsWith(".jar.new");
135 }
136 }
137 );
138 if (pluginFiles == null || pluginFiles.length == 0)
139 return;
140 monitor.subTask(tr("Processing plugin files..."));
141 monitor.setTicksCount(pluginFiles.length);
142 for (File f: pluginFiles) {
143 String fname = f.getName();
144 monitor.setCustomText(tr("Processing file ''{0}''", fname));
145 try {
146 if (fname.endsWith(".jar")) {
147 String pluginName = fname.substring(0, fname.length() - 4);
148 processJarFile(f, pluginName);
149 } else if (fname.endsWith(".jar.new")) {
150 String pluginName = fname.substring(0, fname.length() - 8);
151 processJarFile(f, pluginName);
152 }
153 } catch (PluginException e){
154 Main.warn("PluginException: "+e.getMessage());
155 Main.warn(tr("Failed to scan file ''{0}'' for plugin information. Skipping.", fname));
156 }
157 monitor.worked(1);
158 }
159 }
160
161 protected void scanLocalPluginRepository(ProgressMonitor monitor, File pluginsDirectory) {
162 if (pluginsDirectory == null) return;
163 try {
164 monitor.beginTask("");
165 scanSiteCacheFiles(monitor, pluginsDirectory);
166 scanIconCacheFiles(monitor, pluginsDirectory);
167 scanPluginFiles(monitor, pluginsDirectory);
168 } finally {
169 monitor.setCustomText("");
170 monitor.finishTask();
171 }
172 }
173
174 protected void processLocalPluginInformationFile(File file) throws PluginListParseException{
175 FileInputStream fin = null;
176 try {
177 fin = new FileInputStream(file);
178 List<PluginInformation> pis = new PluginListParser().parse(fin);
179 for (PluginInformation pi : pis) {
180 // we always keep plugin information from a plugin site because it
181 // includes information not available in the plugin jars Manifest, i.e.
182 // the download link or localized descriptions
183 //
184 availablePlugins.put(pi.name, pi);
185 }
186 } catch(IOException e) {
187 throw new PluginListParseException(e);
188 } finally {
189 Utils.close(fin);
190 }
191 }
192
193 protected void analyseInProcessPlugins() {
194 for (PluginProxy proxy : PluginHandler.pluginList) {
195 PluginInformation info = proxy.getPluginInformation();
196 if (canceled)return;
197 if (!availablePlugins.containsKey(info.name)) {
198 availablePlugins.put(info.name, info);
199 } else {
200 availablePlugins.get(info.name).localversion = info.localversion;
201 }
202 }
203 }
204
205 protected void filterOldPlugins() {
206 for (PluginHandler.DeprecatedPlugin p : PluginHandler.DEPRECATED_PLUGINS) {
207 if (canceled)return;
208 if (availablePlugins.containsKey(p.name)) {
209 availablePlugins.remove(p.name);
210 }
211 }
212 }
213
214 @Override
215 protected void realRun() throws SAXException, IOException, OsmTransferException {
216 Collection<String> pluginLocations = PluginInformation.getPluginLocations();
217 getProgressMonitor().setTicksCount(pluginLocations.size() + 2);
218 if (canceled) return;
219 for (String location : pluginLocations) {
220 scanLocalPluginRepository(
221 getProgressMonitor().createSubTaskMonitor(1, false),
222 new File(location)
223 );
224 getProgressMonitor().worked(1);
225 if (canceled)return;
226 }
227 analyseInProcessPlugins();
228 getProgressMonitor().worked(1);
229 if (canceled)return;
230 filterOldPlugins();
231 getProgressMonitor().worked(1);
232 }
233
234 /**
235 * Replies information about available plugins detected by this task.
236 *
237 * @return information about available plugins detected by this task.
238 */
239 public List<PluginInformation> getAvailablePlugins() {
240 return new ArrayList<PluginInformation>(availablePlugins.values());
241 }
242
243 /**
244 * Replies true if the task was canceled by the user
245 *
246 * @return true if the task was canceled by the user
247 */
248 public boolean isCanceled() {
249 return canceled;
250 }
251}
Note: See TracBrowser for help on using the repository browser.