Ver código fonte

More plugin tidying

Change-Id: I6eb475bf19450c09a6be9cfb48c12a57022d302f
Reviewed-on: http://gerrit.dmdirc.com/2066
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.6.6b1
Chris Smith 13 anos atrás
pai
commit
5c3b4a5383

+ 3
- 0
src/com/dmdirc/plugins/Plugin.java Ver arquivo

@@ -75,7 +75,10 @@ public interface Plugin {
75 75
      * Sets the associated plugin info for this plugin.
76 76
      *
77 77
      * @param pluginInfo Associated plugin info
78
+     * @deprecated PluginInfo should be obtained using a constructor parameter,
79
+     * if required
78 80
      */
81
+    @Deprecated
79 82
     void setPluginInfo(final PluginInfo pluginInfo);
80 83
 
81 84
     /**

+ 29
- 56
src/com/dmdirc/plugins/PluginInfo.java Ver arquivo

@@ -97,18 +97,6 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
97 97
 
98 98
         ResourceManager res;
99 99
 
100
-        // Check for updates.
101
-        if (new File(getFullFilename() + ".update").exists() && new File(getFullFilename()).delete()) {
102
-            new File(getFullFilename() + ".update").renameTo(new File(getFullFilename()));
103
-
104
-            updateMetaData();
105
-        }
106
-
107
-        if (metadata.hasErrors()) {
108
-            throw new PluginException("Plugin " + filename + " has metadata "
109
-                    + "errors: " + metadata.getErrors());
110
-        }
111
-
112 100
         try {
113 101
             res = getResourceManager();
114 102
         } catch (IOException ioe) {
@@ -116,22 +104,30 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
116 104
             throw new PluginException("Plugin " + filename + " failed to load. " + lastError, ioe);
117 105
         }
118 106
 
119
-        final String mainClass = metadata.getMainClass().replace('.', '/') + ".class";
120
-        if (!res.resourceExists(mainClass)) {
121
-            lastError = "main class file (" + mainClass + ") not found in jar.";
107
+        updateClassList(res);
108
+
109
+        if (!myClasses.contains(metadata.getMainClass())) {
110
+            lastError = "main class file (" + metadata.getMainClass() + ") not found in jar.";
122 111
             throw new PluginException("Plugin " + filename + " failed to load. " + lastError);
123 112
         }
124 113
 
114
+        updateProvides();
115
+        getDefaults();
116
+    }
117
+
118
+    /**
119
+     * Updates the list of known classes within this plugin from the specified
120
+     * resource manager.
121
+     */
122
+    private void updateClassList(final ResourceManager res) {
123
+        myClasses.clear();
124
+
125 125
         for (final String classfilename : res.getResourcesStartingWith("")) {
126
-            String classname = classfilename.replace('/', '.');
127
-            if (classname.matches("^.*\\.class$")) {
128
-                classname = classname.replaceAll("\\.class$", "");
129
-                myClasses.add(classname);
126
+            final String classname = classfilename.replace('/', '.');
127
+            if (classname.endsWith(".class")) {
128
+                myClasses.add(classname.substring(0, classname.length() - 6));
130 129
             }
131 130
         }
132
-
133
-        updateProvides();
134
-        getDefaults();
135 131
     }
136 132
 
137 133
     /**
@@ -297,21 +293,14 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
297 293
     public void pluginUpdated() {
298 294
         try {
299 295
             // Force a new resourcemanager just incase.
300
-            final ResourceManager res = getResourceManager(true);
301
-
302
-            myClasses.clear();
303
-            for (final String classfilename : res.getResourcesStartingWith("")) {
304
-                String classname = classfilename.replace('/', '.');
305
-                if (classname.matches("^.*\\.class$")) {
306
-                    classname = classname.replaceAll("\\.class$", "");
307
-                    myClasses.add(classname);
308
-                }
309
-            }
296
+            updateClassList(getResourceManager(true));
297
+
310 298
             updateMetaData();
311 299
             updateProvides();
312 300
             getDefaults();
313 301
         } catch (IOException ioe) {
314
-            Logger.userError(ErrorLevel.MEDIUM, "There was an error updating "+getName(), ioe);
302
+            Logger.userError(ErrorLevel.MEDIUM, "There was an error updating "
303
+                    + metadata.getName(), ioe);
315 304
         }
316 305
     }
317 306
 
@@ -573,7 +562,7 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
573 562
 
574 563
             final Class<?> clazz = classloader.loadClass(classname);
575 564
             if (clazz == null) {
576
-                lastError = "Class '"+classname+"' was not able to load.";
565
+                lastError = "Class '" + classname + "' was not able to load.";
577 566
                 return;
578 567
             }
579 568
 
@@ -824,23 +813,11 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
824 813
      * @return List of all persistent classes in this plugin
825 814
      */
826 815
     public Collection<String> getPersistentClasses() {
827
-        final Collection<String> result = new ArrayList<String>();
828
-
829 816
         if (isPersistent()) {
830
-            try {
831
-                final ResourceManager res = getResourceManager();
832
-
833
-                for (final String resourceFilename : res.getResourcesStartingWith("")) {
834
-                    if (resourceFilename.matches("^.*\\.class$")) {
835
-                        result.add(resourceFilename.replaceAll("\\.class$", "").replace('/', '.'));
836
-                    }
837
-                }
838
-            } catch (IOException e) {
839
-                // Jar no longer exists?
840
-            }
817
+            return getClassList();
818
+        } else {
819
+            return metadata.getPersistentClasses();
841 820
         }
842
-
843
-        return metadata.getPersistentClasses();
844 821
     }
845 822
 
846 823
     /**
@@ -850,11 +827,7 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
850 827
      * @return true if file (or whole plugin) is persistent, else false
851 828
      */
852 829
     public boolean isPersistent(final String classname) {
853
-        if (isPersistent()) {
854
-            return true;
855
-        } else {
856
-            return metadata.getPersistentClasses().contains(classname);
857
-        }
830
+        return isPersistent() || metadata.getPersistentClasses().contains(classname);
858 831
     }
859 832
 
860 833
     /**
@@ -941,7 +914,7 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
941 914
      */
942 915
     @Override
943 916
     public String toString() {
944
-        return getNiceName() + " - " + filename;
917
+        return metadata.getFriendlyName() + " - " + filename;
945 918
     }
946 919
 
947 920
     /** {@inheritDoc} */
@@ -992,7 +965,7 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
992 965
      *
993 966
      * @param name Name of the service to check
994 967
      *
995
-     * @return true iif the plugin exports the service
968
+     * @return true iff the plugin exports the service
996 969
      */
997 970
     public boolean hasExportedService(final String name) {
998 971
         return exports.containsKey(name);

+ 25
- 0
src/com/dmdirc/plugins/PluginManager.java Ver arquivo

@@ -414,6 +414,8 @@ public class PluginManager implements ActionListener {
414 414
      * Refreshes the list of known plugins.
415 415
      */
416 416
     public void refreshPlugins() {
417
+        applyUpdates();
418
+
417 419
         final Collection<PluginMetaData> newPlugins = getAllPlugins();
418 420
 
419 421
         for (PluginMetaData plugin : newPlugins) {
@@ -436,6 +438,29 @@ public class PluginManager implements ActionListener {
436 438
                 CoreActionType.PLUGIN_REFRESH, null, this);
437 439
     }
438 440
 
441
+    /**
442
+     * Recursively scans the plugin directory and attempts to apply any
443
+     * available updates.
444
+     */
445
+    public void applyUpdates() {
446
+        final Deque<File> dirs = new LinkedList<File>();
447
+
448
+        dirs.add(new File(myDir));
449
+
450
+        while (!dirs.isEmpty()) {
451
+            final File dir = dirs.pop();
452
+            if (dir.isDirectory()) {
453
+                dirs.addAll(Arrays.asList(dir.listFiles()));
454
+            } else if (dir.isFile() && dir.getName().endsWith(".jar")) {
455
+                final File update = new File(dir.getAbsolutePath() + ".update");
456
+
457
+                if (update.exists() && dir.delete()) {
458
+                    update.renameTo(dir);
459
+                }
460
+            }
461
+        }
462
+    }
463
+
439 464
     /**
440 465
      * Retrieves a list of all installed plugins.
441 466
      * Any file under the main plugin directory (~/.DMDirc/plugins or similar)

Carregando…
Cancelar
Salvar