[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;
|
---|
[10123] | 18 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
[3083] | 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.
|
---|
[3530] | 26 | *
|
---|
[5836] | 27 | * It scans the files in the local plugins repository (see {@link org.openstreetmap.josm.data.Preferences#getPluginsDirectory()}
|
---|
[3083] | 28 | * and extracts plugin information from three kind of files:
|
---|
| 29 | * <ul>
|
---|
[5723] | 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>
|
---|
[3083] | 32 | * <li>cached lists of available plugins, downloaded for instance from
|
---|
[7655] | 33 | * <a href="https://josm.openstreetmap.de/pluginicons">https://josm.openstreetmap.de/pluginicons</a></li>
|
---|
[3083] | 34 | * </ul>
|
---|
| 35 | *
|
---|
| 36 | */
|
---|
| 37 | public class ReadLocalPluginInformationTask extends PleaseWaitRunnable {
|
---|
[9078] | 38 | private final Map<String, PluginInformation> availablePlugins;
|
---|
[3083] | 39 | private boolean canceled;
|
---|
| 40 |
|
---|
[6906] | 41 | /**
|
---|
| 42 | * Constructs a new {@code ReadLocalPluginInformationTask}.
|
---|
| 43 | */
|
---|
[3083] | 44 | public ReadLocalPluginInformationTask() {
|
---|
| 45 | super(tr("Reading local plugin information.."), false);
|
---|
[7005] | 46 | availablePlugins = new HashMap<>();
|
---|
[3083] | 47 | }
|
---|
| 48 |
|
---|
[10616] | 49 | /**
|
---|
| 50 | * Constructs a new {@code ReadLocalPluginInformationTask}.
|
---|
| 51 | * @param monitor progress monitor
|
---|
| 52 | */
|
---|
[3083] | 53 | public ReadLocalPluginInformationTask(ProgressMonitor monitor) {
|
---|
[8510] | 54 | super(tr("Reading local plugin information.."), monitor, false);
|
---|
[7005] | 55 | availablePlugins = new HashMap<>();
|
---|
[3083] | 56 | }
|
---|
| 57 |
|
---|
| 58 | @Override
|
---|
| 59 | protected void cancel() {
|
---|
| 60 | canceled = true;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | @Override
|
---|
[10173] | 64 | protected void finish() {
|
---|
| 65 | // Do nothing
|
---|
| 66 | }
|
---|
[3083] | 67 |
|
---|
[8510] | 68 | protected void processJarFile(File f, String pluginName) throws PluginException {
|
---|
[3083] | 69 | PluginInformation info = new PluginInformation(
|
---|
| 70 | f,
|
---|
| 71 | pluginName
|
---|
| 72 | );
|
---|
| 73 | if (!availablePlugins.containsKey(info.getName())) {
|
---|
[5601] | 74 | info.updateLocalInfo(info);
|
---|
[3083] | 75 | availablePlugins.put(info.getName(), info);
|
---|
| 76 | } else {
|
---|
[3874] | 77 | PluginInformation current = availablePlugins.get(info.getName());
|
---|
[5601] | 78 | current.updateFromJar(info);
|
---|
[3083] | 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[10043] | 82 | private static File[] listFiles(File pluginsDirectory, final String regex) {
|
---|
[10616] | 83 | return pluginsDirectory.listFiles((FilenameFilter) (dir, name) -> name.matches(regex));
|
---|
[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(
|
---|
[10616] | 107 | (FilenameFilter) (dir, name) -> name.endsWith(".jar") || name.endsWith(".jar.new")
|
---|
[3083] | 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 | }
|
---|
[8510] | 124 | } catch (PluginException e) {
|
---|
[10616] | 125 | Main.warn(e, "PluginException: ");
|
---|
[6248] | 126 | Main.warn(tr("Failed to scan file ''{0}'' for plugin information. Skipping.", fname));
|
---|
[3083] | 127 | }
|
---|
| 128 | monitor.worked(1);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[10965] | 132 | protected void scanLocalPluginRepository(ProgressMonitor progressMonitor, File pluginsDirectory) {
|
---|
[10123] | 133 | if (pluginsDirectory == null)
|
---|
| 134 | return;
|
---|
[10965] | 135 | ProgressMonitor monitor = progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE;
|
---|
[3083] | 136 | try {
|
---|
| 137 | monitor.beginTask("");
|
---|
| 138 | scanSiteCacheFiles(monitor, pluginsDirectory);
|
---|
| 139 | scanPluginFiles(monitor, pluginsDirectory);
|
---|
| 140 | } finally {
|
---|
| 141 | monitor.setCustomText("");
|
---|
| 142 | monitor.finishTask();
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[8510] | 146 | protected void processLocalPluginInformationFile(File file) throws PluginListParseException {
|
---|
[7033] | 147 | try (FileInputStream fin = new FileInputStream(file)) {
|
---|
[3083] | 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 | }
|
---|
[8510] | 156 | } catch (IOException e) {
|
---|
[3083] | 157 | throw new PluginListParseException(e);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | protected void analyseInProcessPlugins() {
|
---|
| 162 | for (PluginProxy proxy : PluginHandler.pluginList) {
|
---|
| 163 | PluginInformation info = proxy.getPluginInformation();
|
---|
[8510] | 164 | if (canceled) return;
|
---|
[3083] | 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() {
|
---|
[3730] | 174 | for (PluginHandler.DeprecatedPlugin p : PluginHandler.DEPRECATED_PLUGINS) {
|
---|
[8510] | 175 | if (canceled) return;
|
---|
[3730] | 176 | if (availablePlugins.containsKey(p.name)) {
|
---|
| 177 | availablePlugins.remove(p.name);
|
---|
[3083] | 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);
|
---|
[3287] | 186 | if (canceled) return;
|
---|
[3130] | 187 | for (String location : pluginLocations) {
|
---|
| 188 | scanLocalPluginRepository(
|
---|
| 189 | getProgressMonitor().createSubTaskMonitor(1, false),
|
---|
| 190 | new File(location)
|
---|
| 191 | );
|
---|
| 192 | getProgressMonitor().worked(1);
|
---|
[8510] | 193 | if (canceled) return;
|
---|
[3130] | 194 | }
|
---|
[3083] | 195 | analyseInProcessPlugins();
|
---|
| 196 | getProgressMonitor().worked(1);
|
---|
[8510] | 197 | if (canceled) return;
|
---|
[3083] | 198 | filterOldPlugins();
|
---|
| 199 | getProgressMonitor().worked(1);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | /**
|
---|
| 203 | * Replies information about available plugins detected by this task.
|
---|
[3530] | 204 | *
|
---|
[3083] | 205 | * @return information about available plugins detected by this task.
|
---|
| 206 | */
|
---|
| 207 | public List<PluginInformation> getAvailablePlugins() {
|
---|
[7005] | 208 | return new ArrayList<>(availablePlugins.values());
|
---|
[3083] | 209 | }
|
---|
| 210 |
|
---|
| 211 | /**
|
---|
[4310] | 212 | * Replies true if the task was canceled by the user
|
---|
[3530] | 213 | *
|
---|
[4310] | 214 | * @return true if the task was canceled by the user
|
---|
[3083] | 215 | */
|
---|
| 216 | public boolean isCanceled() {
|
---|
| 217 | return canceled;
|
---|
| 218 | }
|
---|
| 219 | }
|
---|