1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.FileInputStream;
|
---|
8 | import java.io.FilenameFilter;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Map;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
18 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
20 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
21 | import 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 | */
|
---|
37 | public 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 | 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 static 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)
|
---|
140 | return;
|
---|
141 | if (monitor == null)
|
---|
142 | monitor = NullProgressMonitor.INSTANCE;
|
---|
143 | try {
|
---|
144 | monitor.beginTask("");
|
---|
145 | scanSiteCacheFiles(monitor, pluginsDirectory);
|
---|
146 | scanPluginFiles(monitor, pluginsDirectory);
|
---|
147 | } finally {
|
---|
148 | monitor.setCustomText("");
|
---|
149 | monitor.finishTask();
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | protected void processLocalPluginInformationFile(File file) throws PluginListParseException {
|
---|
154 | try (FileInputStream fin = new FileInputStream(file)) {
|
---|
155 | List<PluginInformation> pis = new PluginListParser().parse(fin);
|
---|
156 | for (PluginInformation pi : pis) {
|
---|
157 | // we always keep plugin information from a plugin site because it
|
---|
158 | // includes information not available in the plugin jars Manifest, i.e.
|
---|
159 | // the download link or localized descriptions
|
---|
160 | //
|
---|
161 | availablePlugins.put(pi.name, pi);
|
---|
162 | }
|
---|
163 | } catch (IOException e) {
|
---|
164 | throw new PluginListParseException(e);
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | protected void analyseInProcessPlugins() {
|
---|
169 | for (PluginProxy proxy : PluginHandler.pluginList) {
|
---|
170 | PluginInformation info = proxy.getPluginInformation();
|
---|
171 | if (canceled) return;
|
---|
172 | if (!availablePlugins.containsKey(info.name)) {
|
---|
173 | availablePlugins.put(info.name, info);
|
---|
174 | } else {
|
---|
175 | availablePlugins.get(info.name).localversion = info.localversion;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | protected void filterOldPlugins() {
|
---|
181 | for (PluginHandler.DeprecatedPlugin p : PluginHandler.DEPRECATED_PLUGINS) {
|
---|
182 | if (canceled) return;
|
---|
183 | if (availablePlugins.containsKey(p.name)) {
|
---|
184 | availablePlugins.remove(p.name);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Override
|
---|
190 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
191 | Collection<String> pluginLocations = PluginInformation.getPluginLocations();
|
---|
192 | getProgressMonitor().setTicksCount(pluginLocations.size() + 2);
|
---|
193 | if (canceled) return;
|
---|
194 | for (String location : pluginLocations) {
|
---|
195 | scanLocalPluginRepository(
|
---|
196 | getProgressMonitor().createSubTaskMonitor(1, false),
|
---|
197 | new File(location)
|
---|
198 | );
|
---|
199 | getProgressMonitor().worked(1);
|
---|
200 | if (canceled) return;
|
---|
201 | }
|
---|
202 | analyseInProcessPlugins();
|
---|
203 | getProgressMonitor().worked(1);
|
---|
204 | if (canceled) return;
|
---|
205 | filterOldPlugins();
|
---|
206 | getProgressMonitor().worked(1);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Replies information about available plugins detected by this task.
|
---|
211 | *
|
---|
212 | * @return information about available plugins detected by this task.
|
---|
213 | */
|
---|
214 | public List<PluginInformation> getAvailablePlugins() {
|
---|
215 | return new ArrayList<>(availablePlugins.values());
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Replies true if the task was canceled by the user
|
---|
220 | *
|
---|
221 | * @return true if the task was canceled by the user
|
---|
222 | */
|
---|
223 | public boolean isCanceled() {
|
---|
224 | return canceled;
|
---|
225 | }
|
---|
226 | }
|
---|