|
@@ -0,0 +1,215 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2015 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.plugins;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.DMDircMBassador;
|
|
26
|
+import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
|
|
27
|
+import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
|
|
28
|
+import com.dmdirc.events.UserErrorEvent;
|
|
29
|
+import com.dmdirc.logger.ErrorLevel;
|
|
30
|
+
|
|
31
|
+import com.google.common.collect.ArrayListMultimap;
|
|
32
|
+import com.google.common.collect.Multimap;
|
|
33
|
+
|
|
34
|
+import java.io.IOException;
|
|
35
|
+import java.net.MalformedURLException;
|
|
36
|
+import java.net.URL;
|
|
37
|
+import java.nio.file.FileVisitOption;
|
|
38
|
+import java.nio.file.Files;
|
|
39
|
+import java.nio.file.Path;
|
|
40
|
+import java.util.ArrayList;
|
|
41
|
+import java.util.Collection;
|
|
42
|
+import java.util.Collections;
|
|
43
|
+import java.util.Map;
|
|
44
|
+import java.util.Objects;
|
|
45
|
+import java.util.concurrent.CopyOnWriteArrayList;
|
|
46
|
+import java.util.function.Function;
|
|
47
|
+import java.util.stream.Collectors;
|
|
48
|
+
|
|
49
|
+import javax.annotation.Nullable;
|
|
50
|
+import javax.inject.Inject;
|
|
51
|
+import javax.inject.Singleton;
|
|
52
|
+
|
|
53
|
+/**
|
|
54
|
+ * Locates and tracks plugin files on disk.
|
|
55
|
+ */
|
|
56
|
+@Singleton
|
|
57
|
+public class PluginFileHandler {
|
|
58
|
+
|
|
59
|
+ private final Path directory;
|
|
60
|
+ private final DMDircMBassador eventBus;
|
|
61
|
+
|
|
62
|
+ private final Collection<PluginMetaData> knownPlugins = new CopyOnWriteArrayList<>();
|
|
63
|
+
|
|
64
|
+ @Inject
|
|
65
|
+ public PluginFileHandler(
|
|
66
|
+ @Directory(DirectoryType.PLUGINS) final Path directory,
|
|
67
|
+ final DMDircMBassador eventBus) {
|
|
68
|
+ this.directory = directory;
|
|
69
|
+ this.eventBus = eventBus;
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ /**
|
|
73
|
+ * Finds all plugin files on disk, loads their metadata, and validates them.
|
|
74
|
+ *
|
|
75
|
+ * @param manager The plugin manager to pass to new metadata instances.
|
|
76
|
+ * @return Collection of valid plugins.
|
|
77
|
+ */
|
|
78
|
+ public Collection<PluginMetaData> refresh(final PluginManager manager) {
|
|
79
|
+ final Collection<PluginMetaData> metadata = findAllPlugins(manager);
|
|
80
|
+
|
|
81
|
+ // Deal with plugins that had errors
|
|
82
|
+ metadata.stream().filter(PluginMetaData::hasErrors).forEach(this::reportErrors);
|
|
83
|
+ metadata.removeIf(PluginMetaData::hasErrors);
|
|
84
|
+
|
|
85
|
+ final Collection<PluginMetaData> newPlugins = getValidPlugins(metadata);
|
|
86
|
+
|
|
87
|
+ knownPlugins.clear();
|
|
88
|
+ knownPlugins.addAll(newPlugins);
|
|
89
|
+
|
|
90
|
+ return newPlugins;
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ /**
|
|
94
|
+ * Gets the collection of known plugins.
|
|
95
|
+ *
|
|
96
|
+ * @return A cached collection of plugins (no disk I/O will be performed).
|
|
97
|
+ */
|
|
98
|
+ public Collection<PluginMetaData> getKnownPlugins() {
|
|
99
|
+ return Collections.unmodifiableCollection(knownPlugins);
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ /**
|
|
103
|
+ * Finds all plugins on disk with loadable metadata.
|
|
104
|
+ *
|
|
105
|
+ * @param manager The plugin manager to pass to new metadata instances.
|
|
106
|
+ * @return Collection of all plugins with loadable metadata.
|
|
107
|
+ */
|
|
108
|
+ private Collection<PluginMetaData> findAllPlugins(final PluginManager manager) {
|
|
109
|
+ try {
|
|
110
|
+ return Files.walk(directory, FileVisitOption.FOLLOW_LINKS)
|
|
111
|
+ .filter(p -> p.getFileName().toString().endsWith(".jar"))
|
|
112
|
+ .map(Path::toAbsolutePath)
|
|
113
|
+ .map(path -> this.getMetaData(path, manager)).filter(Objects::nonNull)
|
|
114
|
+ .collect(Collectors.toList());
|
|
115
|
+ } catch (IOException ex) {
|
|
116
|
+ eventBus.publish(new UserErrorEvent(ErrorLevel.HIGH, ex,
|
|
117
|
+ "Unable to read plugin directory", ""));
|
|
118
|
+ return Collections.emptyList();
|
|
119
|
+ }
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ /**
|
|
123
|
+ * Attempts to get the metadata for a plugin at the specified path.
|
|
124
|
+ *
|
|
125
|
+ * @param path The path of the plugin to get metadata from.
|
|
126
|
+ * @param manager The plugin manager to pass to new metadata instances.
|
|
127
|
+ * @return The metadata if it could be created, {@code null} otherwise.
|
|
128
|
+ */
|
|
129
|
+ @Nullable
|
|
130
|
+ private PluginMetaData getMetaData(final Path path, final PluginManager manager) {
|
|
131
|
+ try {
|
|
132
|
+ final PluginMetaData metaData = new PluginMetaData(
|
|
133
|
+ manager, new URL("jar:file:" + path + "!/META-INF/plugin.config"), path);
|
|
134
|
+ metaData.load();
|
|
135
|
+ return metaData;
|
|
136
|
+ } catch (MalformedURLException ex) {
|
|
137
|
+ eventBus.publish(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
|
|
138
|
+ "Error creating URL for plugin " + path + ": " + ex.getMessage(), ""));
|
|
139
|
+ return null;
|
|
140
|
+ }
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ /**
|
|
144
|
+ * Reports any errors present in the metadata to the event bus.
|
|
145
|
+ *
|
|
146
|
+ * @param pluginMetaData The metadata to read errors from.
|
|
147
|
+ */
|
|
148
|
+ private void reportErrors(final PluginMetaData pluginMetaData) {
|
|
149
|
+ eventBus.publish(new UserErrorEvent(ErrorLevel.MEDIUM, null,
|
|
150
|
+ "Error reading plugin metadata for plugin " + pluginMetaData.getPluginPath() + ": "
|
|
151
|
+ + pluginMetaData.getErrors(), ""));
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ /**
|
|
155
|
+ * Gets all services defined for the given plugins.
|
|
156
|
+ *
|
|
157
|
+ * @param metadata The metadata to retrieve services from.
|
|
158
|
+ * @return A multimap of service types to their implementations.
|
|
159
|
+ */
|
|
160
|
+ private Multimap<String, String> getServices(final Collection<PluginMetaData> metadata) {
|
|
161
|
+ final Multimap<String, String> services = ArrayListMultimap.create();
|
|
162
|
+
|
|
163
|
+ // Normal services
|
|
164
|
+ metadata.stream()
|
|
165
|
+ .map(PluginMetaData::getServices)
|
|
166
|
+ .flatMap(Collection::stream)
|
|
167
|
+ .map(service -> service.split(" ", 2))
|
|
168
|
+ .forEach(pair -> services.put(pair[1], pair[0]));
|
|
169
|
+
|
|
170
|
+ // Exports
|
|
171
|
+ metadata.stream()
|
|
172
|
+ .map(PluginMetaData::getExports)
|
|
173
|
+ .flatMap(Collection::stream)
|
|
174
|
+ .map(exportName -> exportName.split(" "))
|
|
175
|
+ .map(parts -> parts.length > 4 ? parts[4] : parts[0])
|
|
176
|
+ .forEach(name -> services.put("export", name));
|
|
177
|
+
|
|
178
|
+ return services;
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ /**
|
|
182
|
+ * Validates each plugin uses a {@link PluginMetaDataValidator}, and returns those found to
|
|
183
|
+ * be valid.
|
|
184
|
+ *
|
|
185
|
+ * @param metadata The collection of metadata to validate.
|
|
186
|
+ * @return The collection of metadata that passed validation.
|
|
187
|
+ */
|
|
188
|
+ private Collection<PluginMetaData> getValidPlugins(
|
|
189
|
+ final Collection<PluginMetaData> metadata) {
|
|
190
|
+ // Collect a map by name
|
|
191
|
+ final Map<String, PluginMetaData> metaDataByName = metadata.stream()
|
|
192
|
+ .collect(Collectors.toMap(PluginMetaData::getName, Function.identity()));
|
|
193
|
+
|
|
194
|
+ // Collect up services
|
|
195
|
+ final Multimap<String, String> services = getServices(metadata);
|
|
196
|
+
|
|
197
|
+ // Validate each in turn
|
|
198
|
+ final Collection<PluginMetaData> res = new ArrayList<>();
|
|
199
|
+ for (PluginMetaData target : metadata) {
|
|
200
|
+ final PluginMetaDataValidator validator = new PluginMetaDataValidator(target);
|
|
201
|
+ final Collection<String> results = validator.validate(metaDataByName, services);
|
|
202
|
+
|
|
203
|
+ if (results.isEmpty()) {
|
|
204
|
+ res.add(target);
|
|
205
|
+ } else {
|
|
206
|
+ eventBus.publish(new UserErrorEvent(ErrorLevel.MEDIUM, null,
|
|
207
|
+ "Plugin validation failed for " + target.getPluginPath() + ": " + results,
|
|
208
|
+ ""));
|
|
209
|
+ }
|
|
210
|
+ }
|
|
211
|
+
|
|
212
|
+ return res;
|
|
213
|
+ }
|
|
214
|
+
|
|
215
|
+}
|