Browse Source

Introduce Dagger.

Start off with the simple stuff, just providing unitialised managers
and having Main do all the wiring. Update the tests to match.

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

+ 90
- 0
src/com/dmdirc/ClientModule.java View File

@@ -0,0 +1,90 @@
1
+/*
2
+ * Copyright (c) 2006-2013 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;
24
+
25
+import com.dmdirc.actions.ActionManager;
26
+import com.dmdirc.config.IdentityManager;
27
+import com.dmdirc.interfaces.IdentityController;
28
+
29
+import javax.inject.Singleton;
30
+
31
+import dagger.Module;
32
+import dagger.Provides;
33
+
34
+/**
35
+ * Provides dependencies for the client.
36
+ */
37
+@Module(injects = Main.class)
38
+public class ClientModule {
39
+
40
+    /**
41
+     * Provides an identity manager for the client.
42
+     *
43
+     * @return An unitialised {@link IdentityManager}.
44
+     */
45
+    @Provides
46
+    @Singleton
47
+    public IdentityManager getIdentityManager() {
48
+        final IdentityManager identityManager = new IdentityManager();
49
+        IdentityManager.setIdentityManager(identityManager);
50
+        identityManager.loadVersionIdentity();
51
+        return identityManager;
52
+    }
53
+
54
+    /**
55
+     * Provides an identity controller.
56
+     *
57
+     * @param manager The identity manager to use as a controller.
58
+     * @return An identity controller to use.
59
+     */
60
+    @Provides
61
+    public IdentityController getIdentityController(final IdentityManager manager) {
62
+        return manager;
63
+    }
64
+
65
+    /**
66
+     * Provides a parser factory.
67
+     *
68
+     * @return A parser factory for use in the client.
69
+     */
70
+    @Provides
71
+    public ParserFactory getParserFactory() {
72
+        return new ParserFactory(Main.mainInstance.getPluginManager());
73
+    }
74
+
75
+    /**
76
+     * Provides an action manager.
77
+     *
78
+     * @param serverManager The server manager to use to iterate servers.
79
+     * @param identityController The identity controller to use to look up settings.
80
+     * @return An unitialised action manager.
81
+     */
82
+    @Provides
83
+    @Singleton
84
+    public ActionManager getActionManager(final ServerManager serverManager, final IdentityController identityController) {
85
+        final ActionManager actionManager = new ActionManager(serverManager, identityController);
86
+        ActionManager.setActionManager(actionManager);
87
+        return actionManager;
88
+    }
89
+
90
+}

+ 30
- 25
src/com/dmdirc/Main.java View File

@@ -57,7 +57,9 @@ import java.util.Map;
57 57
 import java.util.Timer;
58 58
 import java.util.TimerTask;
59 59
 
60
-import javax.inject.Provider;
60
+import javax.inject.Inject;
61
+
62
+import dagger.ObjectGraph;
61 63
 
62 64
 /**
63 65
  * Main class, handles initialisation.
@@ -70,6 +72,15 @@ public class Main implements LifecycleController {
70 72
     /** The UI to use for the client. */
71 73
     private final Collection<UIController> CONTROLLERS = new HashSet<>();
72 74
 
75
+    /** The identity manager the client will use. */
76
+    private final IdentityManager identityManager;
77
+
78
+    /** The server manager the client will use. */
79
+    private final ServerManager serverManager;
80
+
81
+    /** The action manager the client will use. */
82
+    private final ActionManager actionManager;
83
+
73 84
     /** The config dir to use for the client. */
74 85
     private String configdir;
75 86
 
@@ -80,8 +91,22 @@ public class Main implements LifecycleController {
80 91
     /** Instance of pluginmanager used by this Main. */
81 92
     protected PluginManager pluginManager;
82 93
 
83
-    /** Our ServerManager. */
84
-    protected ServerManager serverManager;
94
+    /**
95
+     * Creates a new instance of {@link Main}.
96
+     *
97
+     * @param identityManager The identity manager the client will use.
98
+     * @param serverManager The server manager the client will use.
99
+     * @param actionManager The action manager the client will use.
100
+     */
101
+    @Inject
102
+    public Main(
103
+            final IdentityManager identityManager,
104
+            final ServerManager serverManager,
105
+            final ActionManager actionManager) {
106
+        this.identityManager = identityManager;
107
+        this.serverManager = serverManager;
108
+        this.actionManager = actionManager;
109
+    }
85 110
 
86 111
     /**
87 112
      * Entry procedure.
@@ -91,7 +116,8 @@ public class Main implements LifecycleController {
91 116
     @SuppressWarnings("PMD.AvoidCatchingThrowable")
92 117
     public static void main(final String[] args) {
93 118
         try {
94
-            mainInstance = new Main();
119
+            ObjectGraph graph = ObjectGraph.create(new ClientModule());
120
+            mainInstance = graph.get(Main.class);
95 121
             mainInstance.init(args);
96 122
         } catch (Throwable ex) {
97 123
             Logger.appError(ErrorLevel.FATAL, "Exception while initialising",
@@ -107,15 +133,6 @@ public class Main implements LifecycleController {
107 133
     public void init(final String[] args) {
108 134
         Thread.setDefaultUncaughtExceptionHandler(new DMDircExceptionHandler());
109 135
 
110
-        final IdentityManager identityManager = new IdentityManager();
111
-        IdentityManager.setIdentityManager(identityManager);
112
-        identityManager.loadVersionIdentity();
113
-
114
-        serverManager = new ServerManager(new ParserFactoryProvider(), identityManager);
115
-
116
-        final ActionManager actionManager = new ActionManager(serverManager, identityManager);
117
-        ActionManager.setActionManager(actionManager);
118
-
119 136
         final CommandLineParser clp = new CommandLineParser(this, args);
120 137
 
121 138
         try {
@@ -510,16 +527,4 @@ public class Main implements LifecycleController {
510 527
         }
511 528
     }
512 529
 
513
-    /**
514
-     * Temporary class to lazily provide {@link ParserFactory}s.
515
-     */
516
-    private class ParserFactoryProvider implements Provider<ParserFactory> {
517
-
518
-        /** {@inheritDoc} */
519
-        @Override
520
-        public ParserFactory get() {
521
-            return new ParserFactory(pluginManager);
522
-        }
523
-    }
524
-
525 530
 }

+ 4
- 0
src/com/dmdirc/ServerManager.java View File

@@ -41,12 +41,15 @@ import java.util.List;
41 41
 import java.util.Set;
42 42
 import java.util.concurrent.CopyOnWriteArraySet;
43 43
 
44
+import javax.inject.Inject;
44 45
 import javax.inject.Provider;
46
+import javax.inject.Singleton;
45 47
 
46 48
 /**
47 49
  * The ServerManager maintains a list of all servers, and provides methods to
48 50
  * search or iterate over them.
49 51
  */
52
+@Singleton
50 53
 public class ServerManager implements ServerFactory {
51 54
 
52 55
     /** All servers that currently exist. */
@@ -64,6 +67,7 @@ public class ServerManager implements ServerFactory {
64 67
      * @param parserFactoryProvider The provider of {@link ParserFactory}s to give to servers.
65 68
      * @param identityController The identity controller to use to find profiles.
66 69
      */
70
+    @Inject
67 71
     public ServerManager(
68 72
             final Provider<ParserFactory> parserFactoryProvider,
69 73
             final IdentityController identityController) {

+ 25
- 16
test/com/dmdirc/TestMain.java View File

@@ -6,29 +6,31 @@ import com.dmdirc.config.IdentityManager;
6 6
 import com.dmdirc.config.InvalidIdentityFileException;
7 7
 import com.dmdirc.plugins.PluginManager;
8 8
 
9
-import javax.inject.Provider;
10
-import org.mockito.Mock;
11
-import org.mockito.MockitoAnnotations;
9
+import static org.mockito.Mockito.*;
12 10
 
13 11
 /**
14 12
  * Main subclass to init things needed for testing.
15 13
  */
16 14
 public class TestMain extends Main {
15
+
17 16
     private static Main instance;
18 17
 
19
-    @Mock private Provider<ParserFactory> parserFactoryProvider;
18
+    private final IdentityManager identityManager;
19
+    private final ServerManager serverManager;
20
+    private final ActionManager actionManager;
20 21
 
21
-    public TestMain() { }
22
+    public TestMain(final IdentityManager identityManager,
23
+            final ServerManager serverManager,
24
+            final ActionManager actionManager) {
25
+        super(identityManager, serverManager, actionManager);
26
+        this.identityManager = identityManager;
27
+        this.serverManager = serverManager;
28
+        this.actionManager = actionManager;
29
+    }
22 30
 
23 31
     /** {@inheritDoc} */
24 32
     @Override
25 33
     public void init(final String[] args) {
26
-        MockitoAnnotations.initMocks(this);
27
-
28
-        // TODO: Tests probably shouldn't rely on a config dir... Who knows
29
-        //       what the user has done with their config.
30
-        IdentityManager.setIdentityManager(new IdentityManager());
31
-        IdentityManager.getIdentityManager().loadVersionIdentity();
32 34
         try {
33 35
             IdentityManager.getIdentityManager().initialise(getConfigDir());
34 36
         } catch (InvalidIdentityFileException ex) {
@@ -37,10 +39,6 @@ public class TestMain extends Main {
37 39
             // DON'T do anything to the user's configuration... (so no calls
38 40
             // to handleInvalidConfigFile(); here)
39 41
         }
40
-        serverManager = new ServerManager(
41
-                parserFactoryProvider,
42
-                IdentityManager.getIdentityManager());
43
-        ActionManager.setActionManager(new ActionManager(serverManager, IdentityManager.getIdentityManager()));
44 42
 
45 43
         final String fs = System.getProperty("file.separator");
46 44
         final String pluginDirectory = getConfigDir() + "plugins" + fs;
@@ -63,7 +61,18 @@ public class TestMain extends Main {
63 61
      */
64 62
     public static Main getTestMain() {
65 63
         if (instance == null) {
66
-            instance = new TestMain();
64
+            // TODO: Tests probably shouldn't rely on a config dir... Who knows
65
+            //       what the user has done with their config.
66
+            final IdentityManager identityManager = new IdentityManager();
67
+            IdentityManager.setIdentityManager(identityManager);
68
+            IdentityManager.getIdentityManager().loadVersionIdentity();
69
+
70
+            final ServerManager serverManager = mock(ServerManager.class);
71
+
72
+            final ActionManager actionManager = new ActionManager(serverManager, identityManager);
73
+            ActionManager.setActionManager(actionManager);
74
+
75
+            instance = new TestMain(identityManager, serverManager, actionManager);
67 76
             instance.init(new String[0]);
68 77
         }
69 78
         return instance;

Loading…
Cancel
Save