[3083] | 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 |
|
---|
[6248] | 16 | import org.openstreetmap.josm.Main;
|
---|
[3083] | 17 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
| 18 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
| 19 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
| 20 | import 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.
|
---|
[3530] | 25 | *
|
---|
[5836] | 26 | * It scans the files in the local plugins repository (see {@link org.openstreetmap.josm.data.Preferences#getPluginsDirectory()}
|
---|
[3083] | 27 | * and extracts plugin information from three kind of files:
|
---|
| 28 | * <ul>
|
---|
[5723] | 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>
|
---|
[3083] | 31 | * <li>cached lists of available plugins, downloaded for instance from
|
---|
[7655] | 32 | * <a href="https://josm.openstreetmap.de/pluginicons">https://josm.openstreetmap.de/pluginicons</a></li>
|
---|
[3083] | 33 | * </ul>
|
---|
| 34 | *
|
---|
| 35 | */
|
---|
| 36 | public class ReadLocalPluginInformationTask extends PleaseWaitRunnable {
|
---|
| 37 | private Map<String, PluginInformation> availablePlugins;
|
---|
| 38 | private boolean canceled;
|
---|
| 39 |
|
---|
[6906] | 40 | /**
|
---|
| 41 | * Constructs a new {@code ReadLocalPluginInformationTask}.
|
---|
| 42 | */
|
---|
[3083] | 43 | public ReadLocalPluginInformationTask() {
|
---|
| 44 | super(tr("Reading local plugin information.."), false);
|
---|
[7005] | 45 | availablePlugins = new HashMap<>();
|
---|
[3083] | 46 | }
|
---|
| 47 |
|
---|
| 48 | public ReadLocalPluginInformationTask(ProgressMonitor monitor) {
|
---|
[8510] | 49 | super(tr("Reading local plugin information.."), monitor, false);
|
---|
[7005] | 50 | availablePlugins = new HashMap<>();
|
---|
[3083] | 51 | }
|
---|
| 52 |
|
---|
| 53 | @Override
|
---|
| 54 | protected void cancel() {
|
---|
| 55 | canceled = true;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Override
|
---|
| 59 | protected void finish() {}
|
---|
| 60 |
|
---|
[8510] | 61 | protected void processJarFile(File f, String pluginName) throws PluginException {
|
---|
[3083] | 62 | PluginInformation info = new PluginInformation(
|
---|
| 63 | f,
|
---|
| 64 | pluginName
|
---|
| 65 | );
|
---|
| 66 | if (!availablePlugins.containsKey(info.getName())) {
|
---|
[5601] | 67 | info.updateLocalInfo(info);
|
---|
[3083] | 68 | availablePlugins.put(info.getName(), info);
|
---|
| 69 | } else {
|
---|
[3874] | 70 | PluginInformation current = availablePlugins.get(info.getName());
|
---|
[5601] | 71 | current.updateFromJar(info);
|
---|
[3083] | 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[6906] | 75 | private File[] listFiles(File pluginsDirectory, final String regex) {
|
---|
| 76 | return pluginsDirectory.listFiles(
|
---|
[3083] | 77 | new FilenameFilter() {
|
---|
[6084] | 78 | @Override
|
---|
[3083] | 79 | public boolean accept(File dir, String name) {
|
---|
[6906] | 80 | return name.matches(regex);
|
---|
[3083] | 81 | }
|
---|
| 82 | }
|
---|
| 83 | );
|
---|
[6906] | 84 | }
|
---|
| 85 |
|
---|
| 86 | protected void scanSiteCacheFiles(ProgressMonitor monitor, File pluginsDirectory) {
|
---|
| 87 | File[] siteCacheFiles = listFiles(pluginsDirectory, "^([0-9]+-)?site.*\\.txt$");
|
---|
[3083] | 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);
|
---|
[8510] | 97 | } catch (PluginListParseException e) {
|
---|
[6248] | 98 | Main.warn(tr("Failed to scan file ''{0}'' for plugin information. Skipping.", fname));
|
---|
[6643] | 99 | Main.error(e);
|
---|
[3083] | 100 | }
|
---|
| 101 | monitor.worked(1);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
[3330] | 104 |
|
---|
[3083] | 105 | protected void scanPluginFiles(ProgressMonitor monitor, File pluginsDirectory) {
|
---|
| 106 | File[] pluginFiles = pluginsDirectory.listFiles(
|
---|
| 107 | new FilenameFilter() {
|
---|
[6084] | 108 | @Override
|
---|
[3083] | 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 | }
|
---|
[8510] | 129 | } catch (PluginException e) {
|
---|
[6248] | 130 | Main.warn("PluginException: "+e.getMessage());
|
---|
| 131 | Main.warn(tr("Failed to scan file ''{0}'' for plugin information. Skipping.", fname));
|
---|
[3083] | 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 |
|
---|
[8510] | 149 | protected void processLocalPluginInformationFile(File file) throws PluginListParseException {
|
---|
[7033] | 150 | try (FileInputStream fin = new FileInputStream(file)) {
|
---|
[3083] | 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 | }
|
---|
[8510] | 159 | } catch (IOException e) {
|
---|
[3083] | 160 | throw new PluginListParseException(e);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | protected void analyseInProcessPlugins() {
|
---|
| 165 | for (PluginProxy proxy : PluginHandler.pluginList) {
|
---|
| 166 | PluginInformation info = proxy.getPluginInformation();
|
---|
[8510] | 167 | if (canceled) return;
|
---|
[3083] | 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() {
|
---|
[3730] | 177 | for (PluginHandler.DeprecatedPlugin p : PluginHandler.DEPRECATED_PLUGINS) {
|
---|
[8510] | 178 | if (canceled) return;
|
---|
[3730] | 179 | if (availablePlugins.containsKey(p.name)) {
|
---|
| 180 | availablePlugins.remove(p.name);
|
---|
[3083] | 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);
|
---|
[3287] | 189 | if (canceled) return;
|
---|
[3130] | 190 | for (String location : pluginLocations) {
|
---|
| 191 | scanLocalPluginRepository(
|
---|
| 192 | getProgressMonitor().createSubTaskMonitor(1, false),
|
---|
| 193 | new File(location)
|
---|
| 194 | );
|
---|
| 195 | getProgressMonitor().worked(1);
|
---|
[8510] | 196 | if (canceled) return;
|
---|
[3130] | 197 | }
|
---|
[3083] | 198 | analyseInProcessPlugins();
|
---|
| 199 | getProgressMonitor().worked(1);
|
---|
[8510] | 200 | if (canceled) return;
|
---|
[3083] | 201 | filterOldPlugins();
|
---|
| 202 | getProgressMonitor().worked(1);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /**
|
---|
| 206 | * Replies information about available plugins detected by this task.
|
---|
[3530] | 207 | *
|
---|
[3083] | 208 | * @return information about available plugins detected by this task.
|
---|
| 209 | */
|
---|
| 210 | public List<PluginInformation> getAvailablePlugins() {
|
---|
[7005] | 211 | return new ArrayList<>(availablePlugins.values());
|
---|
[3083] | 212 | }
|
---|
| 213 |
|
---|
| 214 | /**
|
---|
[4310] | 215 | * Replies true if the task was canceled by the user
|
---|
[3530] | 216 | *
|
---|
[4310] | 217 | * @return true if the task was canceled by the user
|
---|
[3083] | 218 | */
|
---|
| 219 | public boolean isCanceled() {
|
---|
| 220 | return canceled;
|
---|
| 221 | }
|
---|
| 222 | }
|
---|