|
@@ -39,6 +39,15 @@ import com.dmdirc.util.validators.ValidationResponse;
|
39
|
39
|
import java.io.File;
|
40
|
40
|
import java.io.IOException;
|
41
|
41
|
import java.io.InputStream;
|
|
42
|
+import java.nio.file.DirectoryStream;
|
|
43
|
+import java.nio.file.FileSystem;
|
|
44
|
+import java.nio.file.FileSystems;
|
|
45
|
+import java.nio.file.FileVisitResult;
|
|
46
|
+import java.nio.file.Files;
|
|
47
|
+import java.nio.file.Path;
|
|
48
|
+import java.nio.file.Paths;
|
|
49
|
+import java.nio.file.SimpleFileVisitor;
|
|
50
|
+import java.nio.file.attribute.BasicFileAttributes;
|
42
|
51
|
import java.util.ArrayList;
|
43
|
52
|
import java.util.Collection;
|
44
|
53
|
import java.util.Collections;
|
|
@@ -97,6 +106,8 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
|
97
|
106
|
private final List<ConfigProvider> configProviders = new ArrayList<>();
|
98
|
107
|
/** Event bus to post plugin loaded events to. */
|
99
|
108
|
private final DMDircMBassador eventBus;
|
|
109
|
+ /** File system for the plugin's jar. */
|
|
110
|
+ private final FileSystem pluginFilesystem;
|
100
|
111
|
|
101
|
112
|
/**
|
102
|
113
|
* Create a new PluginInfo.
|
|
@@ -110,6 +121,7 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
|
110
|
121
|
* @throws PluginException if there is an error loading the Plugin
|
111
|
122
|
*/
|
112
|
123
|
public PluginInfo(
|
|
124
|
+ final String pluginDirectory,
|
113
|
125
|
final PluginMetaData metadata,
|
114
|
126
|
final Provider<PluginInjectorInitialiser> injectorInitialiser,
|
115
|
127
|
final DMDircMBassador eventBus,
|
|
@@ -122,16 +134,20 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
|
122
|
134
|
this.filename = new File(metadata.getPluginUrl().getPath()).getName();
|
123
|
135
|
this.metaData = metadata;
|
124
|
136
|
|
125
|
|
- final ResourceManager res;
|
126
|
|
-
|
127
|
137
|
try {
|
128
|
|
- res = getResourceManager();
|
|
138
|
+ pluginFilesystem = FileSystems.newFileSystem(Paths.get(pluginDirectory, filename), null);
|
|
139
|
+ } catch (IOException ex) {
|
|
140
|
+ lastError = "Error loading filesystem: " + ex.getMessage();
|
|
141
|
+ throw new PluginException("Plugin " + filename + " failed to load. " + lastError, ex);
|
|
142
|
+ }
|
|
143
|
+ try {
|
|
144
|
+ // TODO: Stop using ResourceManager
|
|
145
|
+ getResourceManager();
|
129
|
146
|
} catch (IOException ioe) {
|
130
|
147
|
lastError = "Error with resourcemanager: " + ioe.getMessage();
|
131
|
148
|
throw new PluginException("Plugin " + filename + " failed to load. " + lastError, ioe);
|
132
|
149
|
}
|
133
|
|
-
|
134
|
|
- updateClassList(res);
|
|
150
|
+ updateClassList();
|
135
|
151
|
|
136
|
152
|
if (!myClasses.contains(metadata.getMainClass())) {
|
137
|
153
|
lastError = "main class file (" + metadata.getMainClass() + ") not found in jar.";
|
|
@@ -164,17 +180,25 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
|
164
|
180
|
|
165
|
181
|
/**
|
166
|
182
|
* Updates the list of known classes within this plugin from the specified resource manager.
|
167
|
|
- *
|
168
|
|
- * @param res Resource manager to use to read the plugin contents.
|
169
|
183
|
*/
|
170
|
|
- private void updateClassList(final ResourceManager res) {
|
|
184
|
+ private void updateClassList() throws PluginException {
|
171
|
185
|
myClasses.clear();
|
|
186
|
+ try {
|
|
187
|
+ Files.walkFileTree(pluginFilesystem.getPath("/"), new SimpleFileVisitor<Path>() {
|
172
|
188
|
|
173
|
|
- for (final String classfilename : res.getResourcesStartingWith("")) {
|
174
|
|
- final String classname = classfilename.replace('/', '.');
|
175
|
|
- if (classname.endsWith(".class")) {
|
176
|
|
- myClasses.add(classname.substring(0, classname.length() - 6));
|
177
|
|
- }
|
|
189
|
+ @Override
|
|
190
|
+ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) {
|
|
191
|
+ if (file.getFileName().toString().endsWith(".class")) {
|
|
192
|
+ final String classname = file.toAbsolutePath().toString().replace('/',
|
|
193
|
+ '.');
|
|
194
|
+ myClasses.add(classname.substring(1, classname.length() - 6));
|
|
195
|
+ }
|
|
196
|
+ return FileVisitResult.CONTINUE;
|
|
197
|
+ }
|
|
198
|
+ });
|
|
199
|
+ } catch (IOException ex) {
|
|
200
|
+ lastError = "Error loading classes: " + ex.getMessage();
|
|
201
|
+ throw new PluginException("Plugin " + filename + " failed to load. " + lastError, ex);
|
178
|
202
|
}
|
179
|
203
|
}
|
180
|
204
|
|
|
@@ -242,8 +266,15 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
|
242
|
266
|
*/
|
243
|
267
|
public Map<String, InputStream> getLicenceStreams() throws IOException {
|
244
|
268
|
final TreeMap<String, InputStream> licences = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
245
|
|
- licences.putAll(getResourceManager().getResourcesStartingWithAsInputStreams(
|
246
|
|
- "META-INF/licences/"));
|
|
269
|
+ if (!Files.exists(pluginFilesystem.getPath("/META-INF/licenses/"))) {
|
|
270
|
+ return licences;
|
|
271
|
+ }
|
|
272
|
+ try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(
|
|
273
|
+ pluginFilesystem.getPath("/META-INF/licenses/"))) {
|
|
274
|
+ for (Path path : directoryStream) {
|
|
275
|
+ licences.put(path.getFileName().toString(), Files.newInputStream(path));
|
|
276
|
+ }
|
|
277
|
+ }
|
247
|
278
|
return licences;
|
248
|
279
|
}
|
249
|
280
|
|
|
@@ -366,18 +397,11 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
|
366
|
397
|
* Called when the plugin is updated using the updater. Reloads metaData and updates the list of
|
367
|
398
|
* files.
|
368
|
399
|
*/
|
369
|
|
- public void pluginUpdated() {
|
370
|
|
- try {
|
371
|
|
- // Force a new resourcemanager just in case.
|
372
|
|
- updateClassList(getResourceManager(true));
|
373
|
|
-
|
|
400
|
+ public void pluginUpdated() throws PluginException {
|
|
401
|
+ updateClassList();
|
374
|
402
|
updateMetaData();
|
375
|
403
|
updateProvides();
|
376
|
404
|
getDefaults();
|
377
|
|
- } catch (IOException ioe) {
|
378
|
|
- eventBus.publish(new UserErrorEvent(ErrorLevel.MEDIUM, ioe,
|
379
|
|
- "There was an error updating " + metaData.getName(), ""));
|
380
|
|
- }
|
381
|
405
|
}
|
382
|
406
|
|
383
|
407
|
/**
|