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