浏览代码

Add a separate ServiceManager implementation.

pull/415/head
Chris Smith 9 年前
父节点
当前提交
3214f6f141

+ 5
- 2
src/com/dmdirc/Main.java 查看文件

89
     private final Set<CommandDetails> commands;
89
     private final Set<CommandDetails> commands;
90
     /** Mode alias reporter to use. */
90
     /** Mode alias reporter to use. */
91
     private final ModeAliasReporter reporter;
91
     private final ModeAliasReporter reporter;
92
+    private final ServiceManager serviceManager;
92
 
93
 
93
     /**
94
     /**
94
      * Creates a new instance of {@link Main}.
95
      * Creates a new instance of {@link Main}.
106
             final Set<Migrator> migrators,
107
             final Set<Migrator> migrators,
107
             final DMDircMBassador eventBus,
108
             final DMDircMBassador eventBus,
108
             final Set<CommandDetails> commands,
109
             final Set<CommandDetails> commands,
109
-            final ModeAliasReporter reporter) {
110
+            final ModeAliasReporter reporter,
111
+            final ServiceManager serviceManager) {
110
         this.identityManager = identityManager;
112
         this.identityManager = identityManager;
111
         this.connectionManager = connectionManager;
113
         this.connectionManager = connectionManager;
112
         this.commandLineParser = commandLineParser;
114
         this.commandLineParser = commandLineParser;
119
         this.eventBus = eventBus;
121
         this.eventBus = eventBus;
120
         this.commands = commands;
122
         this.commands = commands;
121
         this.reporter = reporter;
123
         this.reporter = reporter;
124
+        this.serviceManager = serviceManager;
122
     }
125
     }
123
 
126
 
124
     /**
127
     /**
161
         migrators.stream().filter(Migrator::needsMigration).forEach(Migrator::migrate);
164
         migrators.stream().filter(Migrator::needsMigration).forEach(Migrator::migrate);
162
         commands.forEach(c -> commandManager.registerCommand(c.getCommand(), c.getInfo()));
165
         commands.forEach(c -> commandManager.registerCommand(c.getCommand(), c.getInfo()));
163
 
166
 
164
-        loadUIs(pluginManager);
167
+        loadUIs(serviceManager);
165
 
168
 
166
         doFirstRun();
169
         doFirstRun();
167
 
170
 

+ 6
- 9
src/com/dmdirc/ParserFactory.java 查看文件

31
 import com.dmdirc.parser.interfaces.Parser;
31
 import com.dmdirc.parser.interfaces.Parser;
32
 import com.dmdirc.parser.interfaces.ProtocolDescription;
32
 import com.dmdirc.parser.interfaces.ProtocolDescription;
33
 import com.dmdirc.plugins.NoSuchProviderException;
33
 import com.dmdirc.plugins.NoSuchProviderException;
34
-import com.dmdirc.plugins.PluginManager;
35
 import com.dmdirc.plugins.Service;
34
 import com.dmdirc.plugins.Service;
35
+import com.dmdirc.plugins.ServiceManager;
36
 import com.dmdirc.plugins.ServiceProvider;
36
 import com.dmdirc.plugins.ServiceProvider;
37
 
37
 
38
 import java.net.URI;
38
 import java.net.URI;
57
     /** The name of the server domain. */
57
     /** The name of the server domain. */
58
     private static final String DOMAIN_SERVER = "server";
58
     private static final String DOMAIN_SERVER = "server";
59
 
59
 
60
-    /** PluginManager used by this ParserFactory */
61
-    private final PluginManager pluginManager;
60
+    /** ServiceManager used by this ParserFactory */
61
+    private final ServiceManager serviceManager;
62
     /** The event bus to post events to. */
62
     /** The event bus to post events to. */
63
     private final DMDircMBassador eventBus;
63
     private final DMDircMBassador eventBus;
64
 
64
 
65
     /**
65
     /**
66
      * Creates a new instance of {@link ParserFactory}.
66
      * Creates a new instance of {@link ParserFactory}.
67
-     *
68
-     * @param pluginManager The plugin manager used by this factory.
69
-     * @param eventBus      The event bus to post events to.
70
      */
67
      */
71
     @Inject
68
     @Inject
72
     public ParserFactory(
69
     public ParserFactory(
73
-            final PluginManager pluginManager,
70
+            final ServiceManager serviceManager,
74
             final DMDircMBassador eventBus) {
71
             final DMDircMBassador eventBus) {
75
-        this.pluginManager = pluginManager;
72
+        this.serviceManager = serviceManager;
76
         this.eventBus = eventBus;
73
         this.eventBus = eventBus;
77
     }
74
     }
78
 
75
 
208
         final String scheme = address.getScheme() == null ? "irc" : address.getScheme();
205
         final String scheme = address.getScheme() == null ? "irc" : address.getScheme();
209
 
206
 
210
         try {
207
         try {
211
-            final Service service = pluginManager.getService("parser", scheme);
208
+            final Service service = serviceManager.getService("parser", scheme);
212
 
209
 
213
             if (service != null && !service.getProviders().isEmpty()) {
210
             if (service != null && !service.getProviders().isEmpty()) {
214
                 final ServiceProvider provider = service.getProviders().get(0);
211
                 final ServiceProvider provider = service.getProviders().get(0);

+ 6
- 12
src/com/dmdirc/commandparser/commands/global/NewServer.java 查看文件

34
 import com.dmdirc.interfaces.CommandController;
34
 import com.dmdirc.interfaces.CommandController;
35
 import com.dmdirc.interfaces.Connection;
35
 import com.dmdirc.interfaces.Connection;
36
 import com.dmdirc.interfaces.ConnectionFactory;
36
 import com.dmdirc.interfaces.ConnectionFactory;
37
-import com.dmdirc.plugins.PluginManager;
37
+import com.dmdirc.plugins.ServiceManager;
38
 import com.dmdirc.ui.input.AdditionalTabTargets;
38
 import com.dmdirc.ui.input.AdditionalTabTargets;
39
 import com.dmdirc.util.InvalidURIException;
39
 import com.dmdirc.util.InvalidURIException;
40
 import com.dmdirc.util.URIParser;
40
 import com.dmdirc.util.URIParser;
56
             CommandType.TYPE_GLOBAL);
56
             CommandType.TYPE_GLOBAL);
57
     /** The factory to use to construct servers. */
57
     /** The factory to use to construct servers. */
58
     private final ConnectionFactory connectionFactory;
58
     private final ConnectionFactory connectionFactory;
59
-    /** Plugin manager to query for available services. */
60
-    private final PluginManager pluginManager;
59
+    /** Service manager to query for available services. */
60
+    private final ServiceManager serviceManager;
61
     /** Manager to use to find profiles. */
61
     /** Manager to use to find profiles. */
62
     private final ProfileManager profileManager;
62
     private final ProfileManager profileManager;
63
     /** The parser to use for user input. */
63
     /** The parser to use for user input. */
65
 
65
 
66
     /**
66
     /**
67
      * Creates a new newserver command which will use the specified factory to construct servers.
67
      * Creates a new newserver command which will use the specified factory to construct servers.
68
-     *
69
-     * @param controller         The controller to use for command information.
70
-     * @param connectionFactory      The factory to use to construct servers.
71
-     * @param pluginManager      The plugin manager to use to query available services.
72
-     * @param profileManager     Manager to use to find profiles.
73
-     * @param uriParser          The parser to use for user input.
74
      */
68
      */
75
     @Inject
69
     @Inject
76
     public NewServer(
70
     public NewServer(
77
             final CommandController controller,
71
             final CommandController controller,
78
             final ConnectionFactory connectionFactory,
72
             final ConnectionFactory connectionFactory,
79
-            final PluginManager pluginManager,
73
+            final ServiceManager serviceManager,
80
             final ProfileManager profileManager,
74
             final ProfileManager profileManager,
81
             final URIParser uriParser) {
75
             final URIParser uriParser) {
82
         super(controller);
76
         super(controller);
83
         this.connectionFactory = connectionFactory;
77
         this.connectionFactory = connectionFactory;
84
-        this.pluginManager = pluginManager;
78
+        this.serviceManager = serviceManager;
85
         this.profileManager = profileManager;
79
         this.profileManager = profileManager;
86
         this.uriParser = uriParser;
80
         this.uriParser = uriParser;
87
     }
81
     }
111
         final AdditionalTabTargets res = new AdditionalTabTargets();
105
         final AdditionalTabTargets res = new AdditionalTabTargets();
112
 
106
 
113
         if (arg == 0) {
107
         if (arg == 0) {
114
-            res.addAll(pluginManager.getServicesByType("parser").stream()
108
+            res.addAll(serviceManager.getServicesByType("parser").stream()
115
                     .map(parserType -> parserType.getName() + "://").collect(Collectors.toList()));
109
                     .map(parserType -> parserType.getName() + "://").collect(Collectors.toList()));
116
         }
110
         }
117
         res.excludeAll();
111
         res.excludeAll();

+ 6
- 15
src/com/dmdirc/config/prefs/PreferencesDialogModel.java 查看文件

27
 import com.dmdirc.events.ClientPrefsOpenedEvent;
27
 import com.dmdirc.events.ClientPrefsOpenedEvent;
28
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
28
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
29
 import com.dmdirc.interfaces.config.ConfigProvider;
29
 import com.dmdirc.interfaces.config.ConfigProvider;
30
-import com.dmdirc.plugins.PluginManager;
31
 import com.dmdirc.plugins.Service;
30
 import com.dmdirc.plugins.Service;
31
+import com.dmdirc.plugins.ServiceManager;
32
 import com.dmdirc.util.collections.ListenerList;
32
 import com.dmdirc.util.collections.ListenerList;
33
 import com.dmdirc.util.validators.NumericalValidator;
33
 import com.dmdirc.util.validators.NumericalValidator;
34
 import com.dmdirc.util.validators.OptionalValidator;
34
 import com.dmdirc.util.validators.OptionalValidator;
62
     private final AggregateConfigProvider configManager;
62
     private final AggregateConfigProvider configManager;
63
     /** Identity to write settings to. */
63
     /** Identity to write settings to. */
64
     private final ConfigProvider identity;
64
     private final ConfigProvider identity;
65
-    /** Plugin manager. */
66
-    private final PluginManager pluginManager;
65
+    /** Service manager. */
66
+    private final ServiceManager serviceManager;
67
     /** Event bus to post events on. */
67
     /** Event bus to post events on. */
68
     private final DMDircMBassador eventBus;
68
     private final DMDircMBassador eventBus;
69
 
69
 
70
     /**
70
     /**
71
      * Creates a new instance of PreferencesDialogModel.
71
      * Creates a new instance of PreferencesDialogModel.
72
-     *
73
-     * @param pluginPanel     UI specific plugin panel
74
-     * @param themePanel      UI specific theme panel
75
-     * @param updatesPanel    UI specific updates panel
76
-     * @param urlHandlerPanel UI specific URL panel
77
-     * @param configManager   Config manager to read settings from
78
-     * @param identity        Identity to write settings to
79
-     * @param pluginManager   Plugin manager to retrieve plugins from
80
-     * @param eventBus        Event bus to post events on
81
      */
72
      */
82
     @Inject
73
     @Inject
83
     public PreferencesDialogModel(final PreferencesInterface pluginPanel,
74
     public PreferencesDialogModel(final PreferencesInterface pluginPanel,
86
             final PreferencesInterface urlHandlerPanel,
77
             final PreferencesInterface urlHandlerPanel,
87
             final AggregateConfigProvider configManager,
78
             final AggregateConfigProvider configManager,
88
             final ConfigProvider identity,
79
             final ConfigProvider identity,
89
-            final PluginManager pluginManager,
80
+            final ServiceManager serviceManager,
90
             final DMDircMBassador eventBus) {
81
             final DMDircMBassador eventBus) {
91
         this.pluginPanel = pluginPanel;
82
         this.pluginPanel = pluginPanel;
92
         this.themePanel = themePanel;
83
         this.themePanel = themePanel;
94
         this.urlHandlerPanel = urlHandlerPanel;
85
         this.urlHandlerPanel = urlHandlerPanel;
95
         this.configManager = configManager;
86
         this.configManager = configManager;
96
         this.identity = identity;
87
         this.identity = identity;
97
-        this.pluginManager = pluginManager;
88
+        this.serviceManager = serviceManager;
98
         this.eventBus = eventBus;
89
         this.eventBus = eventBus;
99
 
90
 
100
         addDefaultCategories();
91
         addDefaultCategories();
235
         final PreferencesCategory category = new PreferencesCategory("Tab Completion", "");
226
         final PreferencesCategory category = new PreferencesCategory("Tab Completion", "");
236
         final Map<String, String> taboptions = new HashMap<>();
227
         final Map<String, String> taboptions = new HashMap<>();
237
 
228
 
238
-        for (Service service : pluginManager.getServicesByType("tabcompletion")) {
229
+        for (Service service : serviceManager.getServicesByType("tabcompletion")) {
239
             taboptions.put(service.getName(), service.getName());
230
             taboptions.put(service.getName(), service.getName());
240
         }
231
         }
241
 
232
 

+ 5
- 2
src/com/dmdirc/plugins/CorePluginHelper.java 查看文件

34
 @Singleton
34
 @Singleton
35
 public class CorePluginHelper {
35
 public class CorePluginHelper {
36
 
36
 
37
+    private final ServiceManager serviceManager;
38
+
37
     @Inject
39
     @Inject
38
-    public CorePluginHelper() {
40
+    public CorePluginHelper(final ServiceManager serviceManager) {
41
+        this.serviceManager = serviceManager;
39
     }
42
     }
40
 
43
 
41
     /**
44
     /**
51
             final CorePluginExtractor corePluginExtractor,
54
             final CorePluginExtractor corePluginExtractor,
52
             final PluginManager pm,
55
             final PluginManager pm,
53
             final String serviceType) {
56
             final String serviceType) {
54
-        if (pm.getServicesByType(serviceType).isEmpty()) {
57
+        if (serviceManager.getServicesByType(serviceType).isEmpty()) {
55
             corePluginExtractor.extractCorePlugins(serviceType + "_");
58
             corePluginExtractor.extractCorePlugins(serviceType + "_");
56
             pm.refreshPlugins();
59
             pm.refreshPlugins();
57
         }
60
         }

+ 8
- 4
src/com/dmdirc/plugins/PluginInfo.java 查看文件

72
     private final PluginMetaData metaData;
72
     private final PluginMetaData metaData;
73
     /** The manager to use to look up other plugins. */
73
     /** The manager to use to look up other plugins. */
74
     private final PluginManager pluginManager;
74
     private final PluginManager pluginManager;
75
+    /** The manager to register services with. */
76
+    private final ServiceManager serviceManager;
75
     /** The initialiser to use for the injector. */
77
     /** The initialiser to use for the injector. */
76
     private final Provider<PluginInjectorInitialiser> injectorInitialiser;
78
     private final Provider<PluginInjectorInitialiser> injectorInitialiser;
77
     /** The object graph to pass to plugins for DI purposes. */
79
     /** The object graph to pass to plugins for DI purposes. */
118
      */
120
      */
119
     public PluginInfo(
121
     public PluginInfo(
120
             final PluginManager pluginManager,
122
             final PluginManager pluginManager,
123
+            final ServiceManager serviceManager,
121
             final String pluginDirectory,
124
             final String pluginDirectory,
122
             final PluginMetaData metadata,
125
             final PluginMetaData metadata,
123
             final Provider<PluginInjectorInitialiser> injectorInitialiser,
126
             final Provider<PluginInjectorInitialiser> injectorInitialiser,
125
             final IdentityController identityController,
128
             final IdentityController identityController,
126
             final ObjectGraph objectGraph) throws PluginException {
129
             final ObjectGraph objectGraph) throws PluginException {
127
         this.pluginManager = pluginManager;
130
         this.pluginManager = pluginManager;
131
+        this.serviceManager = serviceManager;
128
         this.injectorInitialiser = injectorInitialiser;
132
         this.injectorInitialiser = injectorInitialiser;
129
         this.objectGraph = objectGraph;
133
         this.objectGraph = objectGraph;
130
         this.eventBus = eventBus;
134
         this.eventBus = eventBus;
375
                 final String type = bits.length > 1 ? bits[1] : "misc";
379
                 final String type = bits.length > 1 ? bits[1] : "misc";
376
 
380
 
377
                 if (!"any".equalsIgnoreCase(name) && !"export".equalsIgnoreCase(type)) {
381
                 if (!"any".equalsIgnoreCase(name) && !"export".equalsIgnoreCase(type)) {
378
-                    final Service service = pluginManager.getService(type, name, true);
382
+                    final Service service = serviceManager.getService(type, name, true);
379
                     synchronized (provides) {
383
                     synchronized (provides) {
380
                         service.addProvider(this);
384
                         service.addProvider(this);
381
                         provides.add(service);
385
                         provides.add(service);
507
                 Service best = null;
511
                 Service best = null;
508
                 boolean found = false;
512
                 boolean found = false;
509
 
513
 
510
-                for (Service service : pluginManager.getServicesByType(parts[1])) {
514
+                for (Service service : serviceManager.getServicesByType(parts[1])) {
511
                     if (service.isActive()) {
515
                     if (service.isActive()) {
512
                         found = true;
516
                         found = true;
513
                         break;
517
                         break;
524
                     return false;
528
                     return false;
525
                 }
529
                 }
526
             } else {
530
             } else {
527
-                final Service service = pluginManager.getService(parts[1], parts[0]);
531
+                final Service service = serviceManager.getService(parts[1], parts[0]);
528
 
532
 
529
                 if (service == null || !service.activate()) {
533
                 if (service == null || !service.activate()) {
530
                     return false;
534
                     return false;
933
                 final String serviceName = bits.length > 4 ? bits[4] : bits[0];
937
                 final String serviceName = bits.length > 4 ? bits[4] : bits[0];
934
 
938
 
935
                 // Add a provides for this
939
                 // Add a provides for this
936
-                final Service service = pluginManager.getService("export", serviceName, true);
940
+                final Service service = serviceManager.getService("export", serviceName, true);
937
                 synchronized (provides) {
941
                 synchronized (provides) {
938
                     service.addProvider(this);
942
                     service.addProvider(this);
939
                     provides.add(service);
943
                     provides.add(service);

+ 6
- 102
src/com/dmdirc/plugins/PluginManager.java 查看文件

56
 /**
56
 /**
57
  * Searches for and manages plugins and services.
57
  * Searches for and manages plugins and services.
58
  */
58
  */
59
-public class PluginManager implements ServiceManager {
59
+public class PluginManager {
60
 
60
 
61
     /** List of known plugins' file names to their corresponding {@link PluginInfo} objects. */
61
     /** List of known plugins' file names to their corresponding {@link PluginInfo} objects. */
62
     private final Map<String, PluginInfo> knownPlugins = new HashMap<>();
62
     private final Map<String, PluginInfo> knownPlugins = new HashMap<>();
70
     private final UpdateManager updateManager;
70
     private final UpdateManager updateManager;
71
     /** A provider of initialisers for plugin injectors. */
71
     /** A provider of initialisers for plugin injectors. */
72
     private final Provider<PluginInjectorInitialiser> initialiserProvider;
72
     private final Provider<PluginInjectorInitialiser> initialiserProvider;
73
-    /** Map of services. */
74
-    private final Map<String, Map<String, Service>> services = new HashMap<>();
75
     /** Global ClassLoader used by plugins from this manager. */
73
     /** Global ClassLoader used by plugins from this manager. */
76
     private final GlobalClassLoader globalClassLoader;
74
     private final GlobalClassLoader globalClassLoader;
77
     /** The graph to pass to plugins for DI purposes. */
75
     /** The graph to pass to plugins for DI purposes. */
78
     private final ObjectGraph objectGraph;
76
     private final ObjectGraph objectGraph;
79
     /** Event bus to pass to plugin info for plugin loaded events. */
77
     /** Event bus to pass to plugin info for plugin loaded events. */
80
     private final DMDircMBassador eventBus;
78
     private final DMDircMBassador eventBus;
79
+    /** The service manager to use. */
80
+    private final ServiceManager serviceManager;
81
 
81
 
82
     /**
82
     /**
83
      * Creates a new instance of PluginManager.
83
      * Creates a new instance of PluginManager.
91
      */
91
      */
92
     public PluginManager(
92
     public PluginManager(
93
             final DMDircMBassador eventBus,
93
             final DMDircMBassador eventBus,
94
+            final ServiceManager serviceManager,
94
             final IdentityController identityController,
95
             final IdentityController identityController,
95
             final UpdateManager updateManager,
96
             final UpdateManager updateManager,
96
             final Provider<PluginInjectorInitialiser> initialiserProvider,
97
             final Provider<PluginInjectorInitialiser> initialiserProvider,
97
             final ObjectGraph objectGraph,
98
             final ObjectGraph objectGraph,
98
             final String directory) {
99
             final String directory) {
99
         this.identityController = identityController;
100
         this.identityController = identityController;
101
+        this.serviceManager = serviceManager;
100
         this.updateManager = updateManager;
102
         this.updateManager = updateManager;
101
         this.initialiserProvider = initialiserProvider;
103
         this.initialiserProvider = initialiserProvider;
102
         this.directory = directory;
104
         this.directory = directory;
116
         return globalClassLoader;
118
         return globalClassLoader;
117
     }
119
     }
118
 
120
 
119
-    @Override
120
-    public Service getService(final String type, final String name) {
121
-        return getService(type, name, false);
122
-    }
123
-
124
-    @Override
125
-    public Service getService(final String type, final String name, final boolean create) {
126
-        // Find the type first
127
-        if (services.containsKey(type)) {
128
-            final Map<String, Service> map = services.get(type);
129
-            // Now the name
130
-            if (map.containsKey(name)) {
131
-                return map.get(name);
132
-            } else if (create) {
133
-                final Service service = new Service(type, name);
134
-                map.put(name, service);
135
-                return service;
136
-            }
137
-        } else if (create) {
138
-            final Map<String, Service> map = new HashMap<>();
139
-            final Service service = new Service(type, name);
140
-            map.put(name, service);
141
-            services.put(type, map);
142
-            return service;
143
-        }
144
-
145
-        return null;
146
-    }
147
-
148
-    @Override
149
-    public ServiceProvider getServiceProvider(final String type, final String name) throws
150
-            NoSuchProviderException {
151
-        final Service service = getService(type, name);
152
-        if (service != null) {
153
-            ServiceProvider provider = service.getActiveProvider();
154
-            if (provider != null) {
155
-                return provider;
156
-            } else {
157
-                // Try to activate the service then try again.
158
-                service.activate();
159
-                provider = service.getActiveProvider();
160
-                if (provider != null) {
161
-                    return provider;
162
-                }
163
-            }
164
-        }
165
-
166
-        throw new NoSuchProviderException("No provider found for: " + type + "->" + name);
167
-    }
168
-
169
-    @Override
170
-    public ServiceProvider getServiceProvider(final String type, final List<String> names,
171
-            final boolean fallback) throws NoSuchProviderException {
172
-        for (final String name : names) {
173
-            final ServiceProvider provider = getServiceProvider(type, name);
174
-            if (provider != null) {
175
-                return provider;
176
-            }
177
-        }
178
-
179
-        if (fallback) {
180
-            final List<Service> servicesType = getServicesByType(type);
181
-            if (!servicesType.isEmpty()) {
182
-                final Service service = servicesType.get(0);
183
-                return getServiceProvider(type, service.getName());
184
-            }
185
-        }
186
-
187
-        throw new NoSuchProviderException("No provider found for " + type + "from the given list");
188
-    }
189
-
190
-    @Override
191
-    public ExportedService getExportedService(final String name) {
192
-        return getServiceProvider("export", name).getExportedService(name);
193
-    }
194
-
195
-    @Override
196
-    public List<Service> getServicesByType(final String type) {
197
-        // Find the type first
198
-        if (services.containsKey(type)) {
199
-            final Map<String, Service> map = services.get(type);
200
-            return new ArrayList<>(map.values());
201
-        }
202
-
203
-        return new ArrayList<>();
204
-    }
205
-
206
-    @Override
207
-    public List<Service> getAllServices() {
208
-        // Find the type first
209
-        final List<Service> allServices = new ArrayList<>();
210
-        for (Map<String, Service> map : services.values()) {
211
-            allServices.addAll(map.values());
212
-        }
213
-
214
-        return allServices;
215
-    }
216
-
217
     /**
121
     /**
218
      * Autoloads plugins.
122
      * Autoloads plugins.
219
      */
123
      */
255
                             + "!/META-INF/plugin.config"),
159
                             + "!/META-INF/plugin.config"),
256
                     Paths.get(directory, filename));
160
                     Paths.get(directory, filename));
257
             metadata.load();
161
             metadata.load();
258
-            final PluginInfo pluginInfo = new PluginInfo(this, directory, metadata,
162
+            final PluginInfo pluginInfo = new PluginInfo(this, serviceManager, directory, metadata,
259
                     initialiserProvider, eventBus,
163
                     initialiserProvider, eventBus,
260
                     identityController, objectGraph);
164
                     identityController, objectGraph);
261
             final PluginInfo existing = getPluginInfoByName(metadata.getName());
165
             final PluginInfo existing = getPluginInfoByName(metadata.getName());

+ 5
- 4
src/com/dmdirc/plugins/PluginModule.java 查看文件

52
             final UpdateManager updateManager,
52
             final UpdateManager updateManager,
53
             final Provider<PluginInjectorInitialiser> initialiserProvider,
53
             final Provider<PluginInjectorInitialiser> initialiserProvider,
54
             final ObjectGraph objectGraph,
54
             final ObjectGraph objectGraph,
55
+            final ServiceManager serviceManager,
55
             final CorePluginHelper pluginHelper,
56
             final CorePluginHelper pluginHelper,
56
             @Directory(DirectoryType.PLUGINS) final String directory) {
57
             @Directory(DirectoryType.PLUGINS) final String directory) {
57
-        final PluginManager manager = new PluginManager(eventBus, identityController,
58
-                updateManager, initialiserProvider, objectGraph, directory);
58
+        final PluginManager manager = new PluginManager(eventBus, serviceManager,
59
+                identityController, updateManager, initialiserProvider, objectGraph, directory);
59
         final CorePluginExtractor extractor = new CorePluginExtractor(manager, directory, eventBus);
60
         final CorePluginExtractor extractor = new CorePluginExtractor(manager, directory, eventBus);
60
         pluginHelper.checkBundledPlugins(extractor, manager,
61
         pluginHelper.checkBundledPlugins(extractor, manager,
61
                 identityController.getGlobalConfiguration());
62
                 identityController.getGlobalConfiguration());
73
     }
74
     }
74
 
75
 
75
     @Provides
76
     @Provides
76
-    public ServiceManager getServiceManager(final PluginManager pluginManager) {
77
-        return pluginManager;
77
+    public ServiceManager getServiceManager(final ServiceManagerImpl serviceManager) {
78
+        return serviceManager;
78
     }
79
     }
79
 
80
 
80
     @Provides
81
     @Provides

+ 144
- 0
src/com/dmdirc/plugins/ServiceManagerImpl.java 查看文件

1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.plugins;
24
+
25
+import java.util.ArrayList;
26
+import java.util.HashMap;
27
+import java.util.List;
28
+import java.util.Map;
29
+
30
+import javax.inject.Inject;
31
+import javax.inject.Singleton;
32
+
33
+/**
34
+ * Implementation of {@link ServiceManager}.
35
+ */
36
+@Singleton
37
+public class ServiceManagerImpl implements ServiceManager {
38
+
39
+    /** Map of services. */
40
+    private final Map<String, Map<String, Service>> services = new HashMap<>();
41
+
42
+    @Inject
43
+    public ServiceManagerImpl() {
44
+    }
45
+
46
+    @Override
47
+    public Service getService(final String type, final String name) {
48
+        return getService(type, name, false);
49
+    }
50
+
51
+    @Override
52
+    public Service getService(final String type, final String name, final boolean create) {
53
+        // Find the type first
54
+        if (services.containsKey(type)) {
55
+            final Map<String, Service> map = services.get(type);
56
+            // Now the name
57
+            if (map.containsKey(name)) {
58
+                return map.get(name);
59
+            } else if (create) {
60
+                final Service service = new Service(type, name);
61
+                map.put(name, service);
62
+                return service;
63
+            }
64
+        } else if (create) {
65
+            final Map<String, Service> map = new HashMap<>();
66
+            final Service service = new Service(type, name);
67
+            map.put(name, service);
68
+            services.put(type, map);
69
+            return service;
70
+        }
71
+
72
+        return null;
73
+    }
74
+
75
+    @Override
76
+    public ServiceProvider getServiceProvider(final String type, final String name) throws
77
+            NoSuchProviderException {
78
+        final Service service = getService(type, name);
79
+        if (service != null) {
80
+            ServiceProvider provider = service.getActiveProvider();
81
+            if (provider != null) {
82
+                return provider;
83
+            } else {
84
+                // Try to activate the service then try again.
85
+                service.activate();
86
+                provider = service.getActiveProvider();
87
+                if (provider != null) {
88
+                    return provider;
89
+                }
90
+            }
91
+        }
92
+
93
+        throw new NoSuchProviderException("No provider found for: " + type + "->" + name);
94
+    }
95
+
96
+    @Override
97
+    public ServiceProvider getServiceProvider(final String type, final List<String> names,
98
+            final boolean fallback) throws NoSuchProviderException {
99
+        for (final String name : names) {
100
+            final ServiceProvider provider = getServiceProvider(type, name);
101
+            if (provider != null) {
102
+                return provider;
103
+            }
104
+        }
105
+
106
+        if (fallback) {
107
+            final List<Service> servicesType = getServicesByType(type);
108
+            if (!servicesType.isEmpty()) {
109
+                final Service service = servicesType.get(0);
110
+                return getServiceProvider(type, service.getName());
111
+            }
112
+        }
113
+
114
+        throw new NoSuchProviderException("No provider found for " + type + "from the given list");
115
+    }
116
+
117
+    @Override
118
+    public ExportedService getExportedService(final String name) {
119
+        return getServiceProvider("export", name).getExportedService(name);
120
+    }
121
+
122
+    @Override
123
+    public List<Service> getServicesByType(final String type) {
124
+        // Find the type first
125
+        if (services.containsKey(type)) {
126
+            final Map<String, Service> map = services.get(type);
127
+            return new ArrayList<>(map.values());
128
+        }
129
+
130
+        return new ArrayList<>();
131
+    }
132
+
133
+    @Override
134
+    public List<Service> getAllServices() {
135
+        // Find the type first
136
+        final List<Service> allServices = new ArrayList<>();
137
+        for (Map<String, Service> map : services.values()) {
138
+            allServices.addAll(map.values());
139
+        }
140
+
141
+        return allServices;
142
+    }
143
+
144
+}

+ 3
- 3
test/com/dmdirc/commandparser/commands/global/NewServerTest.java 查看文件

29
 import com.dmdirc.interfaces.CommandController;
29
 import com.dmdirc.interfaces.CommandController;
30
 import com.dmdirc.interfaces.Connection;
30
 import com.dmdirc.interfaces.Connection;
31
 import com.dmdirc.interfaces.ConnectionFactory;
31
 import com.dmdirc.interfaces.ConnectionFactory;
32
-import com.dmdirc.plugins.PluginManager;
32
+import com.dmdirc.plugins.ServiceManager;
33
 import com.dmdirc.util.URIParser;
33
 import com.dmdirc.util.URIParser;
34
 
34
 
35
 import java.net.URI;
35
 import java.net.URI;
56
     @Mock private ProfileManager profileManager;
56
     @Mock private ProfileManager profileManager;
57
     @Mock private Profile identity;
57
     @Mock private Profile identity;
58
     @Mock private FrameContainer container;
58
     @Mock private FrameContainer container;
59
-    @Mock private PluginManager pluginManager;
59
+    @Mock private ServiceManager serviceManager;
60
     @Mock private ConnectionFactory factory;
60
     @Mock private ConnectionFactory factory;
61
     @Mock private Connection connection;
61
     @Mock private Connection connection;
62
     private NewServer command;
62
     private NewServer command;
66
         when(factory.createServer(any(URI.class), any(Profile.class))).thenReturn(connection);
66
         when(factory.createServer(any(URI.class), any(Profile.class))).thenReturn(connection);
67
         when(profileManager.getProfiles()).thenReturn(
67
         when(profileManager.getProfiles()).thenReturn(
68
                 Collections.singletonList(identity));
68
                 Collections.singletonList(identity));
69
-        command = new NewServer(controller, factory, pluginManager, profileManager, new URIParser());
69
+        command = new NewServer(controller, factory, serviceManager, profileManager, new URIParser());
70
     }
70
     }
71
 
71
 
72
     @Test
72
     @Test

+ 13
- 13
test/com/dmdirc/config/prefs/PreferencesDialogModelTest.java 查看文件

26
 import com.dmdirc.events.ClientPrefsClosedEvent;
26
 import com.dmdirc.events.ClientPrefsClosedEvent;
27
 import com.dmdirc.events.ClientPrefsOpenedEvent;
27
 import com.dmdirc.events.ClientPrefsOpenedEvent;
28
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
28
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
29
-import com.dmdirc.plugins.PluginManager;
30
 import com.dmdirc.plugins.Service;
29
 import com.dmdirc.plugins.Service;
30
+import com.dmdirc.plugins.ServiceManager;
31
 
31
 
32
 import java.util.ArrayList;
32
 import java.util.ArrayList;
33
 import java.util.List;
33
 import java.util.List;
51
 public class PreferencesDialogModelTest {
51
 public class PreferencesDialogModelTest {
52
 
52
 
53
     @Mock private DMDircMBassador eventBus;
53
     @Mock private DMDircMBassador eventBus;
54
-    @Mock private PluginManager pluginManager;
54
+    @Mock private ServiceManager serviceManager;
55
 
55
 
56
     @Before
56
     @Before
57
     public void setup() {
57
     public void setup() {
60
         when(tabcompleter.getName()).thenReturn("tabber");
60
         when(tabcompleter.getName()).thenReturn("tabber");
61
         services.add(tabcompleter);
61
         services.add(tabcompleter);
62
 
62
 
63
-        when(pluginManager.getServicesByType("tabcompletion")).thenReturn(services);
63
+        when(serviceManager.getServicesByType("tabcompletion")).thenReturn(services);
64
     }
64
     }
65
 
65
 
66
     @Test
66
     @Test
68
         final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
68
         final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
69
         when(cm.getOption("domain", "option")).thenReturn("fallback");
69
         when(cm.getOption("domain", "option")).thenReturn("fallback");
70
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
70
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
71
-                null, null, cm, null, pluginManager, eventBus);
71
+                null, null, cm, null, serviceManager, eventBus);
72
         assertNotNull(pm.getCategory("General"));
72
         assertNotNull(pm.getCategory("General"));
73
         assertNotNull(pm.getCategory("Connection"));
73
         assertNotNull(pm.getCategory("Connection"));
74
         assertNotNull(pm.getCategory("Messages"));
74
         assertNotNull(pm.getCategory("Messages"));
85
         when(cm.getOption("domain", "option")).thenReturn("fallback");
85
         when(cm.getOption("domain", "option")).thenReturn("fallback");
86
         final PreferencesCategory category = mock(PreferencesCategory.class);
86
         final PreferencesCategory category = mock(PreferencesCategory.class);
87
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
87
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
88
-                null, null, cm, null, pluginManager, eventBus);
88
+                null, null, cm, null, serviceManager, eventBus);
89
         pm.addCategory(category);
89
         pm.addCategory(category);
90
         pm.dismiss();
90
         pm.dismiss();
91
 
91
 
100
         when(cm.getOption("domain", "option")).thenReturn("fallback");
100
         when(cm.getOption("domain", "option")).thenReturn("fallback");
101
 
101
 
102
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
102
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
103
-                null, null, cm, null, pluginManager, eventBus);
103
+                null, null, cm, null, serviceManager, eventBus);
104
         pm.addCategory(category);
104
         pm.addCategory(category);
105
         assertFalse(pm.save());
105
         assertFalse(pm.save());
106
 
106
 
115
         when(cm.getOption("domain", "option")).thenReturn("fallback");
115
         when(cm.getOption("domain", "option")).thenReturn("fallback");
116
 
116
 
117
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
117
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
118
-                null, null, cm, null, pluginManager, eventBus);
118
+                null, null, cm, null, serviceManager, eventBus);
119
         pm.addCategory(category);
119
         pm.addCategory(category);
120
         assertTrue(pm.save());
120
         assertTrue(pm.save());
121
 
121
 
128
         when(cm.getOption("domain", "option")).thenReturn("fallback");
128
         when(cm.getOption("domain", "option")).thenReturn("fallback");
129
 
129
 
130
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
130
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
131
-                null, null, cm, null, pluginManager, eventBus);
131
+                null, null, cm, null, serviceManager, eventBus);
132
         assertNull(pm.getCategory("unittest123"));
132
         assertNull(pm.getCategory("unittest123"));
133
     }
133
     }
134
 
134
 
138
         when(cm.getOption("domain", "option")).thenReturn("fallback");
138
         when(cm.getOption("domain", "option")).thenReturn("fallback");
139
 
139
 
140
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
140
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
141
-                null, null, cm, null, pluginManager, eventBus);
141
+                null, null, cm, null, serviceManager, eventBus);
142
         assertNotNull(pm.getCategories());
142
         assertNotNull(pm.getCategories());
143
         assertFalse(pm.getCategories().isEmpty());
143
         assertFalse(pm.getCategories().isEmpty());
144
 
144
 
153
         when(cm.getOption("domain", "option")).thenReturn("fallback");
153
         when(cm.getOption("domain", "option")).thenReturn("fallback");
154
 
154
 
155
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
155
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
156
-                null, null, cm, null, pluginManager, eventBus);
156
+                null, null, cm, null, serviceManager, eventBus);
157
         final PreferencesInterface tpi = mock(PreferencesInterface.class);
157
         final PreferencesInterface tpi = mock(PreferencesInterface.class);
158
 
158
 
159
         pm.registerSaveListener(tpi);
159
         pm.registerSaveListener(tpi);
166
         final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
166
         final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
167
         when(cm.getOption("domain", "option")).thenReturn("fallback");
167
         when(cm.getOption("domain", "option")).thenReturn("fallback");
168
 
168
 
169
-        new PreferencesDialogModel(null, null, null, null, cm, null, pluginManager, eventBus);
169
+        new PreferencesDialogModel(null, null, null, null, cm, null, serviceManager, eventBus);
170
 
170
 
171
         verify(eventBus).publish(isA(ClientPrefsOpenedEvent.class));
171
         verify(eventBus).publish(isA(ClientPrefsOpenedEvent.class));
172
     }
172
     }
176
         final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
176
         final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
177
         when(cm.getOption("domain", "option")).thenReturn("fallback");
177
         when(cm.getOption("domain", "option")).thenReturn("fallback");
178
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
178
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
179
-                null, null, cm, null, pluginManager, eventBus);
179
+                null, null, cm, null, serviceManager, eventBus);
180
         pm.close();
180
         pm.close();
181
 
181
 
182
         verify(eventBus).publishAsync(isA(ClientPrefsClosedEvent.class));
182
         verify(eventBus).publishAsync(isA(ClientPrefsClosedEvent.class));
188
         when(cm.getOption("domain", "option")).thenReturn("fallback");
188
         when(cm.getOption("domain", "option")).thenReturn("fallback");
189
 
189
 
190
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
190
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
191
-                null, null, cm, null, pluginManager, eventBus);
191
+                null, null, cm, null, serviceManager, eventBus);
192
         final PreferencesCategory category = mock(PreferencesCategory.class);
192
         final PreferencesCategory category = mock(PreferencesCategory.class);
193
         final PreferencesInterface tpi = mock(PreferencesInterface.class);
193
         final PreferencesInterface tpi = mock(PreferencesInterface.class);
194
         when(category.hasObject()).thenReturn(true);
194
         when(category.hasObject()).thenReturn(true);

正在加载...
取消
保存