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

Last change on this file since 7956 was 7655, checked in by stoecker, 9 years ago

support data: style URL's for icon in plugin list, see #10581, drop old style loading

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