Browse Source

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 years ago
parent
commit
5c3b4a5383

+ 3
- 0
src/com/dmdirc/plugins/Plugin.java View File

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

+ 29
- 56
src/com/dmdirc/plugins/PluginInfo.java View File

97
 
97
 
98
         ResourceManager res;
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
         try {
100
         try {
113
             res = getResourceManager();
101
             res = getResourceManager();
114
         } catch (IOException ioe) {
102
         } catch (IOException ioe) {
116
             throw new PluginException("Plugin " + filename + " failed to load. " + lastError, ioe);
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
             throw new PluginException("Plugin " + filename + " failed to load. " + lastError);
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
         for (final String classfilename : res.getResourcesStartingWith("")) {
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
     public void pluginUpdated() {
293
     public void pluginUpdated() {
298
         try {
294
         try {
299
             // Force a new resourcemanager just incase.
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
             updateMetaData();
298
             updateMetaData();
311
             updateProvides();
299
             updateProvides();
312
             getDefaults();
300
             getDefaults();
313
         } catch (IOException ioe) {
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
 
562
 
574
             final Class<?> clazz = classloader.loadClass(classname);
563
             final Class<?> clazz = classloader.loadClass(classname);
575
             if (clazz == null) {
564
             if (clazz == null) {
576
-                lastError = "Class '"+classname+"' was not able to load.";
565
+                lastError = "Class '" + classname + "' was not able to load.";
577
                 return;
566
                 return;
578
             }
567
             }
579
 
568
 
824
      * @return List of all persistent classes in this plugin
813
      * @return List of all persistent classes in this plugin
825
      */
814
      */
826
     public Collection<String> getPersistentClasses() {
815
     public Collection<String> getPersistentClasses() {
827
-        final Collection<String> result = new ArrayList<String>();
828
-
829
         if (isPersistent()) {
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
      * @return true if file (or whole plugin) is persistent, else false
827
      * @return true if file (or whole plugin) is persistent, else false
851
      */
828
      */
852
     public boolean isPersistent(final String classname) {
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
      */
914
      */
942
     @Override
915
     @Override
943
     public String toString() {
916
     public String toString() {
944
-        return getNiceName() + " - " + filename;
917
+        return metadata.getFriendlyName() + " - " + filename;
945
     }
918
     }
946
 
919
 
947
     /** {@inheritDoc} */
920
     /** {@inheritDoc} */
992
      *
965
      *
993
      * @param name Name of the service to check
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
     public boolean hasExportedService(final String name) {
970
     public boolean hasExportedService(final String name) {
998
         return exports.containsKey(name);
971
         return exports.containsKey(name);

+ 25
- 0
src/com/dmdirc/plugins/PluginManager.java View File

414
      * Refreshes the list of known plugins.
414
      * Refreshes the list of known plugins.
415
      */
415
      */
416
     public void refreshPlugins() {
416
     public void refreshPlugins() {
417
+        applyUpdates();
418
+
417
         final Collection<PluginMetaData> newPlugins = getAllPlugins();
419
         final Collection<PluginMetaData> newPlugins = getAllPlugins();
418
 
420
 
419
         for (PluginMetaData plugin : newPlugins) {
421
         for (PluginMetaData plugin : newPlugins) {
436
                 CoreActionType.PLUGIN_REFRESH, null, this);
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
      * Retrieves a list of all installed plugins.
465
      * Retrieves a list of all installed plugins.
441
      * Any file under the main plugin directory (~/.DMDirc/plugins or similar)
466
      * Any file under the main plugin directory (~/.DMDirc/plugins or similar)

Loading…
Cancel
Save