Browse Source

Start moving away from ResourceManager.

Change-Id: I5738618fc319defaefa6ab7427ce4c0447e90272
Reviewed-on: http://gerrit.dmdirc.com/4023
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes 9 years ago
parent
commit
1b7af58027

+ 1
- 1
src/com/dmdirc/plugins/CorePluginExtractor.java View File

@@ -105,7 +105,7 @@ public class CorePluginExtractor {
105 105
                         plugin.pluginUpdated();
106 106
                     }
107 107
                 }
108
-            } catch (IOException ex) {
108
+            } catch (PluginException | IOException ex) {
109 109
                 eventBus.publish(new UserErrorEvent(ErrorLevel.LOW, ex,
110 110
                         "Failed to extract plugins", ""));
111 111
             }

+ 48
- 24
src/com/dmdirc/plugins/PluginInfo.java View File

@@ -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
     /**

+ 2
- 1
src/com/dmdirc/plugins/PluginManager.java View File

@@ -254,7 +254,8 @@ public class PluginManager implements ServiceManager {
254 254
                             + "!/META-INF/plugin.config"),
255 255
                     new URL("file:" + directory + filename));
256 256
             metadata.load();
257
-            final PluginInfo pluginInfo = new PluginInfo(metadata, initialiserProvider, eventBus,
257
+            final PluginInfo pluginInfo = new PluginInfo(directory, metadata,
258
+                    initialiserProvider, eventBus,
258 259
                     identityController, objectGraph);
259 260
             final PluginInfo existing = getPluginInfoByName(metadata.getName());
260 261
             if (existing != null) {

+ 6
- 1
src/com/dmdirc/updater/components/PluginComponent.java View File

@@ -23,6 +23,7 @@
23 23
 package com.dmdirc.updater.components;
24 24
 
25 25
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
26
+import com.dmdirc.plugins.PluginException;
26 27
 import com.dmdirc.plugins.PluginInfo;
27 28
 import com.dmdirc.updater.UpdateComponent;
28 29
 import com.dmdirc.updater.Version;
@@ -116,7 +117,11 @@ public class PluginComponent implements UpdateComponent {
116 117
             new File(path).renameTo(newTarget);
117 118
             returnCode = true;
118 119
         } else {
119
-            plugin.pluginUpdated();
120
+            try {
121
+                plugin.pluginUpdated();
122
+            } catch (PluginException ex) {
123
+                returnCode = true;
124
+            }
120 125
         }
121 126
 
122 127
         return returnCode;

Loading…
Cancel
Save