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

Last change on this file since 12225 was 10965, checked in by Don-vip, 8 years ago

sonar - squid:S1226 - Method parameters, caught exceptions and foreach variables should not be reassigned

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