Browse Source

PluginManager shouldn't keep a reference to Main.

Change-Id: I91d4f63ffc2ec54942342f7fae8be9c2e036039f
Reviewed-on: http://gerrit.dmdirc.com/2673
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8rc1
Chris Smith 10 years ago
parent
commit
15d78e5e0b

+ 1
- 1
src/com/dmdirc/Main.java View File

@@ -125,7 +125,7 @@ public class Main implements LifecycleController {
125 125
 
126 126
         MessageSinkManager.getManager().loadDefaultSinks();
127 127
 
128
-        pluginManager = new PluginManager(IdentityManager.getIdentityManager(), this);
128
+        pluginManager = new PluginManager(IdentityManager.getIdentityManager());
129 129
         checkBundledPlugins(pluginManager, IdentityManager.getIdentityManager().getGlobalConfiguration());
130 130
 
131 131
         ThemeManager.loadThemes();

+ 13
- 27
src/com/dmdirc/plugins/PluginInfo.java View File

@@ -79,20 +79,20 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
79 79
     /** Is this plugin only loaded temporarily? */
80 80
     private boolean tempLoaded;
81 81
     /** List of classes this plugin has. */
82
-    private final List<String> myClasses = new ArrayList<String>();
82
+    private final List<String> myClasses = new ArrayList<>();
83 83
     /** Last Error Message. */
84 84
     @Getter
85 85
     private String lastError = "No Error";
86 86
     /** Are we trying to load? */
87 87
     private boolean isLoading;
88 88
     /** List of services we provide. */
89
-    private final List<Service> provides = new ArrayList<Service>();
89
+    private final List<Service> provides = new ArrayList<>();
90 90
     /** List of children of this plugin. */
91
-    private final List<PluginInfo> children = new ArrayList<PluginInfo>();
91
+    private final List<PluginInfo> children = new ArrayList<>();
92 92
     /** Map of exports. */
93
-    private final Map<String, ExportInfo> exports = new HashMap<String, ExportInfo>();
93
+    private final Map<String, ExportInfo> exports = new HashMap<>();
94 94
     /** List of identities. */
95
-    private final List<Identity> identities = new ArrayList<Identity>();
95
+    private final List<Identity> identities = new ArrayList<>();
96 96
 
97 97
     /**
98 98
      * Create a new PluginInfo.
@@ -164,13 +164,14 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
164 164
             injector.addParameter(parentPlugin);
165 165
         }
166 166
 
167
+        // TODO: This should be switched to using a full DI framework.
167 168
         injector.addParameter(PluginManager.class, metaData.getManager());
168 169
         injector.addParameter(ActionManager.getActionManager());
169 170
         injector.addParameter(PluginInfo.class, this);
170
-        injector.addParameter(Main.class, metaData.getManager().getMain());
171
+        injector.addParameter(Main.class, Main.mainInstance);
171 172
         injector.addParameter(PluginMetaData.class, metaData);
172 173
         injector.addParameter(IdentityManager.getIdentityManager());
173
-        injector.addParameter(ServerManager.class, metaData.getManager().getMain().getServerManager());
174
+        injector.addParameter(ServerManager.class, Main.mainInstance.getServerManager());
174 175
         injector.addParameter(CommandManager.getCommandManager());
175 176
         injector.addParameter(MessageSinkManager.class, MessageSinkManager.getManager());
176 177
         injector.addParameter(WindowManager.class, WindowManager.getWindowManager());
@@ -188,8 +189,7 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
188 189
      * @throws IOException if there is an error with the ResourceManager.
189 190
      */
190 191
     public Map<String, InputStream> getLicenceStreams() throws IOException {
191
-        final TreeMap<String, InputStream> licences =
192
-                new TreeMap<String, InputStream>(String.CASE_INSENSITIVE_ORDER);
192
+        final TreeMap<String, InputStream> licences = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
193 193
         licences.putAll(getResourceManager().getResourcesStartingWithAsInputStreams(
194 194
                 "META-INF/licences/"));
195 195
         return licences;
@@ -400,7 +400,7 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
400 400
     @Override
401 401
     public List<Service> getServices() {
402 402
         synchronized (provides) {
403
-            return new ArrayList<Service>(provides);
403
+            return new ArrayList<>(provides);
404 404
         }
405 405
     }
406 406
 
@@ -560,12 +560,7 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
560 560
 
561 561
             try {
562 562
                 plugin.onLoad();
563
-            } catch (LinkageError e) {
564
-                lastError = "Error in onLoad for " + metaData.getName() + ":"
565
-                        + e.getMessage();
566
-                Logger.userError(ErrorLevel.MEDIUM, lastError, e);
567
-                unloadPlugin();
568
-            } catch (Exception e) {
563
+            } catch (LinkageError | Exception e) {
569 564
                 lastError = "Error in onLoad for " + metaData.getName() + ":"
570 565
                         + e.getMessage();
571 566
                 Logger.userError(ErrorLevel.MEDIUM, lastError, e);
@@ -706,12 +701,7 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
706 701
                         if (!tempLoaded) {
707 702
                             try {
708 703
                                 plugin.onLoad();
709
-                            } catch (LinkageError e) {
710
-                                lastError = "Error in onLoad for "
711
-                                        + metaData.getName() + ":" + e.getMessage();
712
-                                Logger.userError(ErrorLevel.MEDIUM, lastError, e);
713
-                                unloadPlugin();
714
-                            } catch (Exception e) {
704
+                            } catch (LinkageError | Exception e) {
715 705
                                 lastError = "Error in onLoad for "
716 706
                                         + metaData.getName() + ":" + e.getMessage();
717 707
                                 Logger.userError(ErrorLevel.MEDIUM, lastError, e);
@@ -794,11 +784,7 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
794 784
                 // Now unload ourself
795 785
                 try {
796 786
                     plugin.onUnload();
797
-                } catch (Exception e) {
798
-                    lastError = "Error in onUnload for " + metaData.getName()
799
-                            + ":" + e + " - " + e.getMessage();
800
-                    Logger.userError(ErrorLevel.MEDIUM, lastError, e);
801
-                } catch (LinkageError e) {
787
+                } catch (Exception | LinkageError e) {
802 788
                     lastError = "Error in onUnload for " + metaData.getName()
803 789
                             + ":" + e + " - " + e.getMessage();
804 790
                     Logger.userError(ErrorLevel.MEDIUM, lastError, e);

+ 23
- 38
src/com/dmdirc/plugins/PluginManager.java View File

@@ -22,7 +22,6 @@
22 22
 
23 23
 package com.dmdirc.plugins;
24 24
 
25
-import com.dmdirc.Main;
26 25
 import com.dmdirc.actions.ActionManager;
27 26
 import com.dmdirc.actions.CoreActionType;
28 27
 import com.dmdirc.config.IdentityManager;
@@ -54,30 +53,28 @@ import java.util.Map;
54 53
 public class PluginManager implements ActionListener, ServiceManager {
55 54
 
56 55
     /** List of known plugins' file names to their corresponding {@link PluginInfo} objects. */
57
-    private final Map<String, PluginInfo> knownPlugins = new HashMap<String, PluginInfo>();
56
+    private final Map<String, PluginInfo> knownPlugins = new HashMap<>();
58 57
 
59 58
     /** Set of known plugins' metadata. */
60
-    private final Collection<PluginMetaData> plugins = new HashSet<PluginMetaData>();
59
+    private final Collection<PluginMetaData> plugins = new HashSet<>();
61 60
 
62 61
     /** Directory where plugins are stored. */
63 62
     private final String myDir;
64 63
 
65 64
     /** Map of services. */
66
-    private final Map<String, Map<String, Service>> services = new HashMap<String, Map<String, Service>>();
67
-
68
-    /** Parent. */
69
-    private final Main main;
65
+    private final Map<String, Map<String, Service>> services = new HashMap<>();
70 66
 
71 67
     /** Global ClassLoader used by plugins from this manager. */
72 68
     private final GlobalClassLoader globalClassLoader;
73 69
 
74 70
     /**
75 71
      * Creates a new instance of PluginManager.
72
+     *
73
+     * @param identityManager The identity manager to use for configuration options.
76 74
      */
77
-    public PluginManager(final IdentityManager identityManager, final Main main) {
75
+    public PluginManager(final IdentityManager identityManager) {
78 76
         final String fs = System.getProperty("file.separator");
79 77
         myDir = identityManager.getConfigDir() + "plugins" + fs;
80
-        this.main = main;
81 78
         this.globalClassLoader = new GlobalClassLoader(this);
82 79
         ActionManager.getActionManager().registerListener(this,
83 80
                 CoreActionType.CLIENT_PREFS_OPENED,
@@ -94,17 +91,6 @@ public class PluginManager implements ActionListener, ServiceManager {
94 91
         return globalClassLoader;
95 92
     }
96 93
 
97
-    /**
98
-     * Get the instance of Main that owns this.
99
-     *
100
-     * @return main that owns this.
101
-     * @Deprecated Global state is bad.
102
-     */
103
-    @Deprecated
104
-    public Main getMain() {
105
-        return main;
106
-    }
107
-
108 94
     /** {@inheritDoc} */
109 95
     @Override
110 96
     public Service getService(final String type, final String name) {
@@ -126,7 +112,7 @@ public class PluginManager implements ActionListener, ServiceManager {
126 112
                 return service;
127 113
             }
128 114
         } else if (create) {
129
-            final Map<String, Service> map = new HashMap<String, Service>();
115
+            final Map<String, Service> map = new HashMap<>();
130 116
             final Service service = new Service(type, name);
131 117
             map.put(name, service);
132 118
             services.put(type, map);
@@ -190,17 +176,17 @@ public class PluginManager implements ActionListener, ServiceManager {
190 176
         // Find the type first
191 177
         if (services.containsKey(type)) {
192 178
             final Map<String, Service> map = services.get(type);
193
-            return new ArrayList<Service>(map.values());
179
+            return new ArrayList<>(map.values());
194 180
         }
195 181
 
196
-        return new ArrayList<Service>();
182
+        return new ArrayList<>();
197 183
     }
198 184
 
199 185
     /** {@inheritDoc} */
200 186
     @Override
201 187
     public List<Service> getAllServices() {
202 188
         // Find the type first
203
-        final List<Service> allServices = new ArrayList<Service>();
189
+        final List<Service> allServices = new ArrayList<>();
204 190
         for (Map<String, Service> map : services.values()) {
205 191
             allServices.addAll(map.values());
206 192
         }
@@ -411,7 +397,7 @@ public class PluginManager implements ActionListener, ServiceManager {
411 397
         synchronized (plugins) {
412 398
             plugins.removeAll(newPlugins);
413 399
 
414
-            for (PluginMetaData oldPlugin : new HashSet<PluginMetaData>(plugins)) {
400
+            for (PluginMetaData oldPlugin : new HashSet<>(plugins)) {
415 401
                 delPlugin(oldPlugin.getRelativeFilename());
416 402
             }
417 403
 
@@ -428,7 +414,7 @@ public class PluginManager implements ActionListener, ServiceManager {
428 414
      * available updates.
429 415
      */
430 416
     public void applyUpdates() {
431
-        final Deque<File> dirs = new LinkedList<File>();
417
+        final Deque<File> dirs = new LinkedList<>();
432 418
 
433 419
         dirs.add(new File(myDir));
434 420
 
@@ -454,11 +440,10 @@ public class PluginManager implements ActionListener, ServiceManager {
454 440
      * @return A list of all installed or known plugins
455 441
      */
456 442
     public Collection<PluginMetaData> getAllPlugins() {
457
-        final Collection<PluginMetaData> res
458
-                = new HashSet<PluginMetaData>(plugins.size());
443
+        final Collection<PluginMetaData> res = new HashSet<>(plugins.size());
459 444
 
460
-        final Deque<File> dirs = new LinkedList<File>();
461
-        final Collection<String> pluginPaths = new LinkedList<String>();
445
+        final Deque<File> dirs = new LinkedList<>();
446
+        final Collection<String> pluginPaths = new LinkedList<>();
462 447
 
463 448
         dirs.add(new File(myDir));
464 449
 
@@ -471,9 +456,9 @@ public class PluginManager implements ActionListener, ServiceManager {
471 456
             }
472 457
         }
473 458
 
474
-        final MapList<String, String> newServices = new MapList<String, String>();
475
-        final Map<String, PluginMetaData> newPluginsByName = new HashMap<String, PluginMetaData>();
476
-        final Map<String, PluginMetaData> newPluginsByPath = new HashMap<String, PluginMetaData>();
459
+        final MapList<String, String> newServices = new MapList<>();
460
+        final Map<String, PluginMetaData> newPluginsByName = new HashMap<>();
461
+        final Map<String, PluginMetaData> newPluginsByPath = new HashMap<>();
477 462
 
478 463
         // Initialise all of our metadata objects
479 464
         for (String target : pluginPaths) {
@@ -554,7 +539,7 @@ public class PluginManager implements ActionListener, ServiceManager {
554 539
      * @return Collection<PluginInfo> of known plugins.
555 540
      */
556 541
     public Collection<PluginInfo> getPluginInfos() {
557
-        return new ArrayList<PluginInfo>(knownPlugins.values());
542
+        return new ArrayList<>(knownPlugins.values());
558 543
     }
559 544
 
560 545
     /** {@inheritDoc} */
@@ -568,10 +553,10 @@ public class PluginManager implements ActionListener, ServiceManager {
568 553
                 if (pi.isLoaded() || pi.isTempLoaded()) {
569 554
                     try {
570 555
                         pi.getPlugin().showConfig((PreferencesDialogModel) arguments[0]);
571
-                    } catch (LinkageError le) {
572
-                        Logger.userError(ErrorLevel.MEDIUM, "Error with plugin (" + pi.getMetaData().getFriendlyName() + "), unable to show config (" + le + ")", le);
573
-                    } catch (Exception ex) {
574
-                        Logger.userError(ErrorLevel.MEDIUM, "Error with plugin (" + pi.getMetaData().getFriendlyName() + "), unable to show config (" + ex + ")", ex);
556
+                    } catch (LinkageError | Exception le) {
557
+                        Logger.userError(ErrorLevel.MEDIUM,
558
+                                "Error with plugin (" + pi.getMetaData().getFriendlyName()
559
+                                + "), unable to show config (" + le + ")", le);
575 560
                     }
576 561
                 }
577 562
             }

+ 1
- 1
test/com/dmdirc/TestMain.java View File

@@ -40,7 +40,7 @@ public class TestMain extends Main {
40 40
                 parserFactoryProvider,
41 41
                 IdentityManager.getIdentityManager());
42 42
         ActionManager.initActionManager(serverManager, IdentityManager.getIdentityManager());
43
-        pluginManager = new PluginManager(IdentityManager.getIdentityManager(), this);
43
+        pluginManager = new PluginManager(IdentityManager.getIdentityManager());
44 44
         pluginManager.refreshPlugins();
45 45
         CommandManager.initCommandManager(IdentityManager.getIdentityManager(), serverManager);
46 46
 

Loading…
Cancel
Save