Browse Source

Inject updater components.

Remove some singleton refs from them.

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

+ 43
- 2
src/com/dmdirc/ClientModule.java View File

@@ -54,8 +54,13 @@ import com.dmdirc.ui.core.components.StatusBarManager;
54 54
 import com.dmdirc.ui.messages.ColourManager;
55 55
 import com.dmdirc.ui.themes.ThemeManager;
56 56
 import com.dmdirc.updater.UpdateChecker;
57
+import com.dmdirc.updater.UpdateComponent;
57 58
 import com.dmdirc.updater.Version;
59
+import com.dmdirc.updater.components.ClientComponent;
60
+import com.dmdirc.updater.components.DefaultsComponent;
58 61
 import com.dmdirc.updater.components.LauncherComponent;
62
+import com.dmdirc.updater.components.ModeAliasesComponent;
63
+import com.dmdirc.updater.manager.DMDircUpdateManager;
59 64
 import com.dmdirc.updater.manager.UpdateManager;
60 65
 
61 66
 import java.awt.GraphicsEnvironment;
@@ -283,12 +288,15 @@ public class ClientModule {
283 288
      * Gets an update manager for the client.
284 289
      *
285 290
      * @param commandLineParser CLI parser to use to find launcher version.
291
+     * @param updateManager The underlying update manager.
286 292
      * @return The update manager to use.
287 293
      */
288 294
     @Provides
289 295
     @Singleton
290
-    public UpdateManager getUpdateManager(final CommandLineParser commandLineParser) {
291
-        UpdateChecker.init();
296
+    public UpdateManager getUpdateManager(
297
+            final CommandLineParser commandLineParser,
298
+            final DMDircUpdateManager updateManager) {
299
+        UpdateChecker.init(updateManager);
292 300
         final UpdateManager manager = UpdateChecker.getManager();
293 301
 
294 302
         if (commandLineParser.getLauncherVersion() != null) {
@@ -411,6 +419,39 @@ public class ClientModule {
411 419
         return identityManager;
412 420
     }
413 421
 
422
+    /**
423
+     * Provides a client component for the updater component set.
424
+     *
425
+     * @param component The client component to provide.
426
+     * @return The component entry in the set.
427
+     */
428
+    @Provides(type = Provides.Type.SET)
429
+    public UpdateComponent getClientComponent(final ClientComponent component) {
430
+        return component;
431
+    }
432
+
433
+    /**
434
+     * Provides a mode aliases component for the updater component set.
435
+     *
436
+     * @param component The mode aliases component to provide.
437
+     * @return The component entry in the set.
438
+     */
439
+    @Provides(type = Provides.Type.SET)
440
+    public UpdateComponent getModeAliasesComponent(final ModeAliasesComponent component) {
441
+        return component;
442
+    }
443
+
444
+    /**
445
+     * Provides a defaults component for the updater component set.
446
+     *
447
+     * @param component The defaults component to provide.
448
+     * @return The component entry in the set.
449
+     */
450
+    @Provides(type = Provides.Type.SET)
451
+    public UpdateComponent getDefaultsComponent(final DefaultsComponent component) {
452
+        return component;
453
+    }
454
+
414 455
     /**
415 456
      * Called when the global config cannot be loaded due to an error. This
416 457
      * method informs the user of the problem and installs a new default config

+ 2
- 5
src/com/dmdirc/updater/UpdateChecker.java View File

@@ -27,7 +27,6 @@ import com.dmdirc.config.IdentityManager;
27 27
 import com.dmdirc.logger.ErrorLevel;
28 28
 import com.dmdirc.logger.Logger;
29 29
 import com.dmdirc.updater.manager.CachingUpdateManager;
30
-import com.dmdirc.updater.manager.DMDircUpdateManager;
31 30
 import com.dmdirc.updater.manager.UpdateStatus;
32 31
 
33 32
 import java.util.Date;
@@ -106,10 +105,8 @@ public final class UpdateChecker implements Runnable {
106 105
      *
107 106
      * @param main Parent Main class
108 107
      */
109
-    public static void init() {
110
-        manager = new DMDircUpdateManager(IdentityManager.getIdentityManager()
111
-                .getGlobalConfiguration(), IdentityManager.getIdentityManager().getConfigDir(), 3);
112
-
108
+    public static void init(final CachingUpdateManager manager) {
109
+        UpdateChecker.manager = manager;
113 110
         initTimer();
114 111
     }
115 112
 

+ 26
- 5
src/com/dmdirc/updater/components/ClientComponent.java View File

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.updater.components;
24 24
 
25
-import com.dmdirc.config.IdentityManager;
25
+import com.dmdirc.interfaces.IdentityController;
26 26
 import com.dmdirc.ui.StatusMessage;
27 27
 import com.dmdirc.ui.core.components.StatusBarManager;
28 28
 import com.dmdirc.updater.UpdateComponent;
@@ -31,11 +31,33 @@ import com.dmdirc.util.resourcemanager.DMDircResourceManager;
31 31
 
32 32
 import java.io.File;
33 33
 
34
+import javax.inject.Inject;
35
+
34 36
 /**
35 37
  * Represents the client component, which covers the core client resources.
36 38
  */
37 39
 public class ClientComponent implements UpdateComponent {
38 40
 
41
+    /** The controller to read settings from. */
42
+    private final IdentityController identityController;
43
+
44
+    /** The manager to add status bar messages to. */
45
+    private final StatusBarManager statusBarManager;
46
+
47
+    /**
48
+     * Creates a new instance of {@link ClientComponent}.
49
+     *
50
+     * @param identityController The controller to read settings from.
51
+     * @param statusBarManager The manager to add status bar messages to.
52
+     */
53
+    @Inject
54
+    public ClientComponent(
55
+            final IdentityController identityController,
56
+            final StatusBarManager statusBarManager) {
57
+        this.identityController = identityController;
58
+        this.statusBarManager = statusBarManager;
59
+    }
60
+
39 61
     /** {@inheritDoc} */
40 62
     @Override
41 63
     public String getName() {
@@ -99,8 +121,7 @@ public class ClientComponent implements UpdateComponent {
99 121
     /** {@inheritDoc} */
100 122
     @Override
101 123
     public String getFriendlyVersion() {
102
-        return IdentityManager.getIdentityManager().getGlobalConfiguration()
103
-                .getOption("version", "version");
124
+        return identityController.getGlobalConfiguration().getOption("version", "version");
104 125
     }
105 126
 
106 127
     /** {@inheritDoc} */
@@ -120,8 +141,8 @@ public class ClientComponent implements UpdateComponent {
120 141
             // @deprecated Should be removed when updater UI changes are
121 142
             // implemented.
122 143
             final String message = this.getManualInstructions(path);
123
-            StatusBarManager.getStatusBarManager().setMessage(
124
-                    new StatusMessage(message, IdentityManager.getIdentityManager().getGlobalConfiguration()));
144
+            statusBarManager.setMessage(new StatusMessage(message,
145
+                    identityController.getGlobalConfiguration()));
125 146
         }
126 147
 
127 148
         return true;

+ 19
- 5
src/com/dmdirc/updater/components/DefaultsComponent.java View File

@@ -23,7 +23,7 @@
23 23
 package com.dmdirc.updater.components;
24 24
 
25 25
 import com.dmdirc.config.ConfigManager;
26
-import com.dmdirc.config.IdentityManager;
26
+import com.dmdirc.interfaces.IdentityController;
27 27
 import com.dmdirc.updater.UpdateComponent;
28 28
 import com.dmdirc.updater.Version;
29 29
 import com.dmdirc.util.resourcemanager.ZipResourceManager;
@@ -31,11 +31,26 @@ import com.dmdirc.util.resourcemanager.ZipResourceManager;
31 31
 import java.io.File;
32 32
 import java.io.IOException;
33 33
 
34
+import javax.inject.Inject;
35
+
34 36
 /**
35 37
  * Represents the default identities.
36 38
  */
37 39
 public class DefaultsComponent implements UpdateComponent {
38 40
 
41
+    /** The controller to read settings from. */
42
+    private final IdentityController identityController;
43
+
44
+    /**
45
+     * Creates a new instance of {@link DefaultsComponent}.
46
+     *
47
+     * @param identityController The controller to read settings from.
48
+     */
49
+    @Inject
50
+    public DefaultsComponent(final IdentityController identityController) {
51
+        this.identityController = identityController;
52
+    }
53
+
39 54
     /** {@inheritDoc} */
40 55
     @Override
41 56
     public String getName() {
@@ -57,8 +72,7 @@ public class DefaultsComponent implements UpdateComponent {
57 72
     /** {@inheritDoc} */
58 73
     @Override
59 74
     public Version getVersion() {
60
-        final ConfigManager globalConfig = IdentityManager.getIdentityManager()
61
-                .getGlobalConfiguration();
75
+        final ConfigManager globalConfig = identityController.getGlobalConfiguration();
62 76
 
63 77
         if (globalConfig.hasOptionString("identity", "defaultsversion")) {
64 78
             return new Version(globalConfig.getOption("identity",
@@ -95,9 +109,9 @@ public class DefaultsComponent implements UpdateComponent {
95 109
     public boolean doInstall(final String path) throws IOException {
96 110
         final ZipResourceManager ziprm = ZipResourceManager.getInstance(path);
97 111
 
98
-        ziprm.extractResources("", IdentityManager.getIdentityManager().getIdentityDirectory());
112
+        ziprm.extractResources("", identityController.getIdentityDirectory());
99 113
 
100
-        IdentityManager.getIdentityManager().loadUserIdentities();
114
+        identityController.loadUserIdentities();
101 115
 
102 116
         new File(path).delete();
103 117
 

+ 19
- 5
src/com/dmdirc/updater/components/ModeAliasesComponent.java View File

@@ -23,7 +23,7 @@
23 23
 package com.dmdirc.updater.components;
24 24
 
25 25
 import com.dmdirc.config.ConfigManager;
26
-import com.dmdirc.config.IdentityManager;
26
+import com.dmdirc.interfaces.IdentityController;
27 27
 import com.dmdirc.updater.UpdateComponent;
28 28
 import com.dmdirc.updater.Version;
29 29
 import com.dmdirc.util.resourcemanager.ZipResourceManager;
@@ -31,11 +31,26 @@ import com.dmdirc.util.resourcemanager.ZipResourceManager;
31 31
 import java.io.File;
32 32
 import java.io.IOException;
33 33
 
34
+import javax.inject.Inject;
35
+
34 36
 /**
35 37
  * Represents the mode alias identities.
36 38
  */
37 39
 public class ModeAliasesComponent implements UpdateComponent {
38 40
 
41
+    /** The controller to read settings from. */
42
+    private final IdentityController identityController;
43
+
44
+    /**
45
+     * Creates a new instance of {@link DefaultsComponent}.
46
+     *
47
+     * @param identityController The controller to read settings from.
48
+     */
49
+    @Inject
50
+    public ModeAliasesComponent(final IdentityController identityController) {
51
+        this.identityController = identityController;
52
+    }
53
+
39 54
     /** {@inheritDoc} */
40 55
     @Override
41 56
     public String getName() {
@@ -57,8 +72,7 @@ public class ModeAliasesComponent implements UpdateComponent {
57 72
     /** {@inheritDoc} */
58 73
     @Override
59 74
     public Version getVersion() {
60
-        final ConfigManager globalConfig = IdentityManager.getIdentityManager()
61
-                .getGlobalConfiguration();
75
+        final ConfigManager globalConfig = identityController.getGlobalConfiguration();
62 76
 
63 77
         if (globalConfig.hasOptionString("identity", "modealiasversion")) {
64 78
             return new Version(globalConfig.getOption("identity",
@@ -95,9 +109,9 @@ public class ModeAliasesComponent implements UpdateComponent {
95 109
     public boolean doInstall(final String path) throws IOException {
96 110
         final ZipResourceManager ziprm = ZipResourceManager.getInstance(path);
97 111
 
98
-        ziprm.extractResources("", IdentityManager.getIdentityManager().getIdentityDirectory());
112
+        ziprm.extractResources("", identityController.getIdentityDirectory());
99 113
 
100
-        IdentityManager.getIdentityManager().loadUserIdentities();
114
+        identityController.loadUserIdentities();
101 115
 
102 116
         new File(path).delete();
103 117
 

+ 22
- 21
src/com/dmdirc/updater/manager/DMDircUpdateManager.java View File

@@ -23,21 +23,22 @@
23 23
 package com.dmdirc.updater.manager;
24 24
 
25 25
 import com.dmdirc.config.ConfigBinding;
26
-import com.dmdirc.config.ConfigManager;
26
+import com.dmdirc.interfaces.IdentityController;
27 27
 import com.dmdirc.updater.UpdateChannel;
28
+import com.dmdirc.updater.UpdateComponent;
28 29
 import com.dmdirc.updater.checking.DMDircCheckStrategy;
29 30
 import com.dmdirc.updater.checking.NaiveConsolidator;
30
-import com.dmdirc.updater.components.ClientComponent;
31
-import com.dmdirc.updater.components.DefaultsComponent;
32
-import com.dmdirc.updater.components.ModeAliasesComponent;
33 31
 import com.dmdirc.updater.installing.LegacyInstallationStrategy;
34 32
 import com.dmdirc.updater.retrieving.DownloadRetrievalStrategy;
35 33
 
34
+import java.util.Set;
36 35
 import java.util.concurrent.LinkedBlockingQueue;
37 36
 import java.util.concurrent.ThreadFactory;
38 37
 import java.util.concurrent.ThreadPoolExecutor;
39 38
 import java.util.concurrent.TimeUnit;
40 39
 
40
+import javax.inject.Inject;
41
+
41 42
 import lombok.extern.slf4j.Slf4j;
42 43
 
43 44
 /**
@@ -46,35 +47,35 @@ import lombok.extern.slf4j.Slf4j;
46 47
 @Slf4j
47 48
 public class DMDircUpdateManager extends CachingUpdateManagerImpl {
48 49
 
50
+    /** Number of threads to use. */
51
+    private static final int THREAD_COUNT = 3;
52
+
49 53
     /** The default check strategy we use. */
50 54
     private final DMDircCheckStrategy checkStrategy;
51 55
 
52 56
     /**
53 57
      * Creates a new instance of the update manager.
54 58
      *
55
-     * @param configManager The config manager to use to retrieve channel info
56
-     * @param tempFolder Temporary folder to use for downloading updates
57
-     * @param threads The number of threads to use for updating
59
+     * @param identityController The controller to use for config information.
60
+     * @param components The default components to add to the manager.
58 61
      */
59
-    public DMDircUpdateManager(final ConfigManager configManager,
60
-            final String tempFolder, final int threads) {
61
-        super(new ThreadPoolExecutor(threads, threads, 1, TimeUnit.MINUTES,
62
+    @Inject
63
+    public DMDircUpdateManager(
64
+            final IdentityController identityController,
65
+            final Set<UpdateComponent> components) {
66
+        super(new ThreadPoolExecutor(THREAD_COUNT, THREAD_COUNT, 1, TimeUnit.MINUTES,
62 67
                 new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory()),
63 68
                 new NaiveConsolidator(),
64
-                new ConfigComponentPolicy(configManager));
65
-
66
-        // @deprecated See ClientComponent
67
-        addComponent(new ClientComponent());
68
-        addComponent(new ModeAliasesComponent());
69
-        addComponent(new DefaultsComponent());
70
-
69
+                new ConfigComponentPolicy(identityController.getGlobalConfiguration()));
71 70
         checkStrategy = new DMDircCheckStrategy(UpdateChannel.STABLE);
72
-
73
-        configManager.getBinder().bind(this, DMDircUpdateManager.class);
74
-
71
+        identityController.getGlobalConfiguration().getBinder().bind(this, DMDircUpdateManager.class);
75 72
         addCheckStrategy(checkStrategy);
76
-        addRetrievalStrategy(new DownloadRetrievalStrategy(tempFolder));
73
+        addRetrievalStrategy(new DownloadRetrievalStrategy(identityController.getConfigDir()));
77 74
         addInstallationStrategy(new LegacyInstallationStrategy());
75
+
76
+        for (UpdateComponent component : components) {
77
+            addComponent(component);
78
+        }
78 79
     }
79 80
 
80 81
     /**

Loading…
Cancel
Save