Bladeren bron

Add a separate ServiceManager implementation.

pull/415/head
Chris Smith 9 jaren geleden
bovenliggende
commit
3214f6f141

+ 5
- 2
src/com/dmdirc/Main.java Bestand weergeven

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

+ 6
- 9
src/com/dmdirc/ParserFactory.java Bestand weergeven

@@ -31,8 +31,8 @@ import com.dmdirc.parser.common.MyInfo;
31 31
 import com.dmdirc.parser.interfaces.Parser;
32 32
 import com.dmdirc.parser.interfaces.ProtocolDescription;
33 33
 import com.dmdirc.plugins.NoSuchProviderException;
34
-import com.dmdirc.plugins.PluginManager;
35 34
 import com.dmdirc.plugins.Service;
35
+import com.dmdirc.plugins.ServiceManager;
36 36
 import com.dmdirc.plugins.ServiceProvider;
37 37
 
38 38
 import java.net.URI;
@@ -57,22 +57,19 @@ public class ParserFactory {
57 57
     /** The name of the server domain. */
58 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 62
     /** The event bus to post events to. */
63 63
     private final DMDircMBassador eventBus;
64 64
 
65 65
     /**
66 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 68
     @Inject
72 69
     public ParserFactory(
73
-            final PluginManager pluginManager,
70
+            final ServiceManager serviceManager,
74 71
             final DMDircMBassador eventBus) {
75
-        this.pluginManager = pluginManager;
72
+        this.serviceManager = serviceManager;
76 73
         this.eventBus = eventBus;
77 74
     }
78 75
 
@@ -208,7 +205,7 @@ public class ParserFactory {
208 205
         final String scheme = address.getScheme() == null ? "irc" : address.getScheme();
209 206
 
210 207
         try {
211
-            final Service service = pluginManager.getService("parser", scheme);
208
+            final Service service = serviceManager.getService("parser", scheme);
212 209
 
213 210
             if (service != null && !service.getProviders().isEmpty()) {
214 211
                 final ServiceProvider provider = service.getProviders().get(0);

+ 6
- 12
src/com/dmdirc/commandparser/commands/global/NewServer.java Bestand weergeven

@@ -34,7 +34,7 @@ import com.dmdirc.config.profiles.ProfileManager;
34 34
 import com.dmdirc.interfaces.CommandController;
35 35
 import com.dmdirc.interfaces.Connection;
36 36
 import com.dmdirc.interfaces.ConnectionFactory;
37
-import com.dmdirc.plugins.PluginManager;
37
+import com.dmdirc.plugins.ServiceManager;
38 38
 import com.dmdirc.ui.input.AdditionalTabTargets;
39 39
 import com.dmdirc.util.InvalidURIException;
40 40
 import com.dmdirc.util.URIParser;
@@ -56,8 +56,8 @@ public class NewServer extends Command implements IntelligentCommand {
56 56
             CommandType.TYPE_GLOBAL);
57 57
     /** The factory to use to construct servers. */
58 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 61
     /** Manager to use to find profiles. */
62 62
     private final ProfileManager profileManager;
63 63
     /** The parser to use for user input. */
@@ -65,23 +65,17 @@ public class NewServer extends Command implements IntelligentCommand {
65 65
 
66 66
     /**
67 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 69
     @Inject
76 70
     public NewServer(
77 71
             final CommandController controller,
78 72
             final ConnectionFactory connectionFactory,
79
-            final PluginManager pluginManager,
73
+            final ServiceManager serviceManager,
80 74
             final ProfileManager profileManager,
81 75
             final URIParser uriParser) {
82 76
         super(controller);
83 77
         this.connectionFactory = connectionFactory;
84
-        this.pluginManager = pluginManager;
78
+        this.serviceManager = serviceManager;
85 79
         this.profileManager = profileManager;
86 80
         this.uriParser = uriParser;
87 81
     }
@@ -111,7 +105,7 @@ public class NewServer extends Command implements IntelligentCommand {
111 105
         final AdditionalTabTargets res = new AdditionalTabTargets();
112 106
 
113 107
         if (arg == 0) {
114
-            res.addAll(pluginManager.getServicesByType("parser").stream()
108
+            res.addAll(serviceManager.getServicesByType("parser").stream()
115 109
                     .map(parserType -> parserType.getName() + "://").collect(Collectors.toList()));
116 110
         }
117 111
         res.excludeAll();

+ 6
- 15
src/com/dmdirc/config/prefs/PreferencesDialogModel.java Bestand weergeven

@@ -27,8 +27,8 @@ import com.dmdirc.events.ClientPrefsClosedEvent;
27 27
 import com.dmdirc.events.ClientPrefsOpenedEvent;
28 28
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
29 29
 import com.dmdirc.interfaces.config.ConfigProvider;
30
-import com.dmdirc.plugins.PluginManager;
31 30
 import com.dmdirc.plugins.Service;
31
+import com.dmdirc.plugins.ServiceManager;
32 32
 import com.dmdirc.util.collections.ListenerList;
33 33
 import com.dmdirc.util.validators.NumericalValidator;
34 34
 import com.dmdirc.util.validators.OptionalValidator;
@@ -62,22 +62,13 @@ public class PreferencesDialogModel {
62 62
     private final AggregateConfigProvider configManager;
63 63
     /** Identity to write settings to. */
64 64
     private final ConfigProvider identity;
65
-    /** Plugin manager. */
66
-    private final PluginManager pluginManager;
65
+    /** Service manager. */
66
+    private final ServiceManager serviceManager;
67 67
     /** Event bus to post events on. */
68 68
     private final DMDircMBassador eventBus;
69 69
 
70 70
     /**
71 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 73
     @Inject
83 74
     public PreferencesDialogModel(final PreferencesInterface pluginPanel,
@@ -86,7 +77,7 @@ public class PreferencesDialogModel {
86 77
             final PreferencesInterface urlHandlerPanel,
87 78
             final AggregateConfigProvider configManager,
88 79
             final ConfigProvider identity,
89
-            final PluginManager pluginManager,
80
+            final ServiceManager serviceManager,
90 81
             final DMDircMBassador eventBus) {
91 82
         this.pluginPanel = pluginPanel;
92 83
         this.themePanel = themePanel;
@@ -94,7 +85,7 @@ public class PreferencesDialogModel {
94 85
         this.urlHandlerPanel = urlHandlerPanel;
95 86
         this.configManager = configManager;
96 87
         this.identity = identity;
97
-        this.pluginManager = pluginManager;
88
+        this.serviceManager = serviceManager;
98 89
         this.eventBus = eventBus;
99 90
 
100 91
         addDefaultCategories();
@@ -235,7 +226,7 @@ public class PreferencesDialogModel {
235 226
         final PreferencesCategory category = new PreferencesCategory("Tab Completion", "");
236 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 230
             taboptions.put(service.getName(), service.getName());
240 231
         }
241 232
 

+ 5
- 2
src/com/dmdirc/plugins/CorePluginHelper.java Bestand weergeven

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

+ 8
- 4
src/com/dmdirc/plugins/PluginInfo.java Bestand weergeven

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

+ 6
- 102
src/com/dmdirc/plugins/PluginManager.java Bestand weergeven

@@ -56,7 +56,7 @@ import net.engio.mbassy.listener.Handler;
56 56
 /**
57 57
  * Searches for and manages plugins and services.
58 58
  */
59
-public class PluginManager implements ServiceManager {
59
+public class PluginManager {
60 60
 
61 61
     /** List of known plugins' file names to their corresponding {@link PluginInfo} objects. */
62 62
     private final Map<String, PluginInfo> knownPlugins = new HashMap<>();
@@ -70,14 +70,14 @@ public class PluginManager implements ServiceManager {
70 70
     private final UpdateManager updateManager;
71 71
     /** A provider of initialisers for plugin injectors. */
72 72
     private final Provider<PluginInjectorInitialiser> initialiserProvider;
73
-    /** Map of services. */
74
-    private final Map<String, Map<String, Service>> services = new HashMap<>();
75 73
     /** Global ClassLoader used by plugins from this manager. */
76 74
     private final GlobalClassLoader globalClassLoader;
77 75
     /** The graph to pass to plugins for DI purposes. */
78 76
     private final ObjectGraph objectGraph;
79 77
     /** Event bus to pass to plugin info for plugin loaded events. */
80 78
     private final DMDircMBassador eventBus;
79
+    /** The service manager to use. */
80
+    private final ServiceManager serviceManager;
81 81
 
82 82
     /**
83 83
      * Creates a new instance of PluginManager.
@@ -91,12 +91,14 @@ public class PluginManager implements ServiceManager {
91 91
      */
92 92
     public PluginManager(
93 93
             final DMDircMBassador eventBus,
94
+            final ServiceManager serviceManager,
94 95
             final IdentityController identityController,
95 96
             final UpdateManager updateManager,
96 97
             final Provider<PluginInjectorInitialiser> initialiserProvider,
97 98
             final ObjectGraph objectGraph,
98 99
             final String directory) {
99 100
         this.identityController = identityController;
101
+        this.serviceManager = serviceManager;
100 102
         this.updateManager = updateManager;
101 103
         this.initialiserProvider = initialiserProvider;
102 104
         this.directory = directory;
@@ -116,104 +118,6 @@ public class PluginManager implements ServiceManager {
116 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 122
      * Autoloads plugins.
219 123
      */
@@ -255,7 +159,7 @@ public class PluginManager implements ServiceManager {
255 159
                             + "!/META-INF/plugin.config"),
256 160
                     Paths.get(directory, filename));
257 161
             metadata.load();
258
-            final PluginInfo pluginInfo = new PluginInfo(this, directory, metadata,
162
+            final PluginInfo pluginInfo = new PluginInfo(this, serviceManager, directory, metadata,
259 163
                     initialiserProvider, eventBus,
260 164
                     identityController, objectGraph);
261 165
             final PluginInfo existing = getPluginInfoByName(metadata.getName());

+ 5
- 4
src/com/dmdirc/plugins/PluginModule.java Bestand weergeven

@@ -52,10 +52,11 @@ public class PluginModule {
52 52
             final UpdateManager updateManager,
53 53
             final Provider<PluginInjectorInitialiser> initialiserProvider,
54 54
             final ObjectGraph objectGraph,
55
+            final ServiceManager serviceManager,
55 56
             final CorePluginHelper pluginHelper,
56 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 60
         final CorePluginExtractor extractor = new CorePluginExtractor(manager, directory, eventBus);
60 61
         pluginHelper.checkBundledPlugins(extractor, manager,
61 62
                 identityController.getGlobalConfiguration());
@@ -73,8 +74,8 @@ public class PluginModule {
73 74
     }
74 75
 
75 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 81
     @Provides

+ 144
- 0
src/com/dmdirc/plugins/ServiceManagerImpl.java Bestand weergeven

@@ -0,0 +1,144 @@
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 Bestand weergeven

@@ -29,7 +29,7 @@ import com.dmdirc.config.profiles.ProfileManager;
29 29
 import com.dmdirc.interfaces.CommandController;
30 30
 import com.dmdirc.interfaces.Connection;
31 31
 import com.dmdirc.interfaces.ConnectionFactory;
32
-import com.dmdirc.plugins.PluginManager;
32
+import com.dmdirc.plugins.ServiceManager;
33 33
 import com.dmdirc.util.URIParser;
34 34
 
35 35
 import java.net.URI;
@@ -56,7 +56,7 @@ public class NewServerTest {
56 56
     @Mock private ProfileManager profileManager;
57 57
     @Mock private Profile identity;
58 58
     @Mock private FrameContainer container;
59
-    @Mock private PluginManager pluginManager;
59
+    @Mock private ServiceManager serviceManager;
60 60
     @Mock private ConnectionFactory factory;
61 61
     @Mock private Connection connection;
62 62
     private NewServer command;
@@ -66,7 +66,7 @@ public class NewServerTest {
66 66
         when(factory.createServer(any(URI.class), any(Profile.class))).thenReturn(connection);
67 67
         when(profileManager.getProfiles()).thenReturn(
68 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 72
     @Test

+ 13
- 13
test/com/dmdirc/config/prefs/PreferencesDialogModelTest.java Bestand weergeven

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

Laden…
Annuleren
Opslaan