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

Last change on this file since 8811 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 8.1 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 {@link org.openstreetmap.josm.data.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="https://josm.openstreetmap.de/pluginicons">https://josm.openstreetmap.de/pluginicons</a></li>
33 * </ul>
34 *
35 */
36public class ReadLocalPluginInformationTask extends PleaseWaitRunnable {
37 private Map<String, PluginInformation> availablePlugins;
38 private boolean canceled;
39
40 /**
41 * Constructs a new {@code ReadLocalPluginInformationTask}.
42 */
43 public ReadLocalPluginInformationTask() {
44 super(tr("Reading local plugin information.."), false);
45 availablePlugins = new HashMap<>();
46 }
47
48 public ReadLocalPluginInformationTask(ProgressMonitor monitor) {
49 super(tr("Reading local plugin information.."), monitor, false);
50 availablePlugins = new HashMap<>();
51 }
52
53 @Override
54 protected void cancel() {
55 canceled = true;
56 }
57
58 @Override
59 protected void finish() {}
60
61 protected void processJarFile(File f, String pluginName) throws PluginException {
62 PluginInformation info = new PluginInformation(
63 f,
64 pluginName
65 );
66 if (!availablePlugins.containsKey(info.getName())) {
67 info.updateLocalInfo(info);
68 availablePlugins.put(info.getName(), info);
69 } else {
70 PluginInformation current = availablePlugins.get(info.getName());
71 current.updateFromJar(info);
72 }
73 }
74
75 private File[] listFiles(File pluginsDirectory, final String regex) {
76 return pluginsDirectory.listFiles(
77 new FilenameFilter() {
78 @Override
79 public boolean accept(File dir, String name) {
80 return name.matches(regex);
81 }
82 }
83 );
84 }
85
86 protected void scanSiteCacheFiles(ProgressMonitor monitor, File pluginsDirectory) {
87 File[] siteCacheFiles = listFiles(pluginsDirectory, "^([0-9]+-)?site.*\\.txt$");
88 if (siteCacheFiles == null || siteCacheFiles.length == 0)
89 return;
90 monitor.subTask(tr("Processing plugin site cache files..."));
91 monitor.setTicksCount(siteCacheFiles.length);
92 for (File f: siteCacheFiles) {
93 String fname = f.getName();
94 monitor.setCustomText(tr("Processing file ''{0}''", fname));
95 try {
96 processLocalPluginInformationFile(f);
97 } catch (PluginListParseException e) {
98 Main.warn(tr("Failed to scan file ''{0}'' for plugin information. Skipping.", fname));
99 Main.error(e);
100 }
101 monitor.worked(1);
102 }
103 }
104
105 protected void scanPluginFiles(ProgressMonitor monitor, File pluginsDirectory) {
106 File[] pluginFiles = pluginsDirectory.listFiles(
107 new FilenameFilter() {
108 @Override
109 public boolean accept(File dir, String name) {
110 return name.endsWith(".jar") || name.endsWith(".jar.new");
111 }
112 }
113 );
114 if (pluginFiles == null || pluginFiles.length == 0)
115 return;
116 monitor.subTask(tr("Processing plugin files..."));
117 monitor.setTicksCount(pluginFiles.length);
118 for (File f: pluginFiles) {
119 String fname = f.getName();
120 monitor.setCustomText(tr("Processing file ''{0}''", fname));
121 try {
122 if (fname.endsWith(".jar")) {
123 String pluginName = fname.substring(0, fname.length() - 4);
124 processJarFile(f, pluginName);
125 } else if (fname.endsWith(".jar.new")) {
126 String pluginName = fname.substring(0, fname.length() - 8);
127 processJarFile(f, pluginName);
128 }
129 } catch (PluginException e) {
130 Main.warn("PluginException: "+e.getMessage());
131 Main.warn(tr("Failed to scan file ''{0}'' for plugin information. Skipping.", fname));
132 }
133 monitor.worked(1);
134 }
135 }
136
137 protected void scanLocalPluginRepository(ProgressMonitor monitor, File pluginsDirectory) {
138 if (pluginsDirectory == null) return;
139 try {
140 monitor.beginTask("");
141 scanSiteCacheFiles(monitor, pluginsDirectory);
142 scanPluginFiles(monitor, pluginsDirectory);
143 } finally {
144 monitor.setCustomText("");
145 monitor.finishTask();
146 }
147 }
148
149 protected void processLocalPluginInformationFile(File file) throws PluginListParseException {
150 try (FileInputStream fin = new FileInputStream(file)) {
151 List<PluginInformation> pis = new PluginListParser().parse(fin);
152 for (PluginInformation pi : pis) {
153 // we always keep plugin information from a plugin site because it
154 // includes information not available in the plugin jars Manifest, i.e.
155 // the download link or localized descriptions
156 //
157 availablePlugins.put(pi.name, pi);
158 }
159 } catch (IOException e) {
160 throw new PluginListParseException(e);
161 }
162 }
163
164 protected void analyseInProcessPlugins() {
165 for (PluginProxy proxy : PluginHandler.pluginList) {
166 PluginInformation info = proxy.getPluginInformation();
167 if (canceled) return;
168 if (!availablePlugins.containsKey(info.name)) {
169 availablePlugins.put(info.name, info);
170 } else {
171 availablePlugins.get(info.name).localversion = info.localversion;
172 }
173 }
174 }
175
176 protected void filterOldPlugins() {
177 for (PluginHandler.DeprecatedPlugin p : PluginHandler.DEPRECATED_PLUGINS) {
178 if (canceled) return;
179 if (availablePlugins.containsKey(p.name)) {
180 availablePlugins.remove(p.name);
181 }
182 }
183 }
184
185 @Override
186 protected void realRun() throws SAXException, IOException, OsmTransferException {
187 Collection<String> pluginLocations = PluginInformation.getPluginLocations();
188 getProgressMonitor().setTicksCount(pluginLocations.size() + 2);
189 if (canceled) return;
190 for (String location : pluginLocations) {
191 scanLocalPluginRepository(
192 getProgressMonitor().createSubTaskMonitor(1, false),
193 new File(location)
194 );
195 getProgressMonitor().worked(1);
196 if (canceled) return;
197 }
198 analyseInProcessPlugins();
199 getProgressMonitor().worked(1);
200 if (canceled) return;
201 filterOldPlugins();
202 getProgressMonitor().worked(1);
203 }
204
205 /**
206 * Replies information about available plugins detected by this task.
207 *
208 * @return information about available plugins detected by this task.
209 */
210 public List<PluginInformation> getAvailablePlugins() {
211 return new ArrayList<>(availablePlugins.values());
212 }
213
214 /**
215 * Replies true if the task was canceled by the user
216 *
217 * @return true if the task was canceled by the user
218 */
219 public boolean isCanceled() {
220 return canceled;
221 }
222}
Note: See TracBrowser for help on using the repository browser.