Procházet zdrojové kódy

Inject the CommandLoader.

Create a proper LifecycleController implementation that can be
injected, move the interface to the interfaces package, and
then inject the CommandLoader instead of creating it in Main.

Change-Id: I4236b4668301f585544c8c6a9f17c39771290ea5
Reviewed-on: http://gerrit.dmdirc.com/2688
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.8rc1
Chris Smith před 10 roky
rodič
revize
b37b954647

+ 12
- 0
src/com/dmdirc/ClientModule.java Zobrazit soubor

@@ -27,6 +27,7 @@ import com.dmdirc.commandline.CommandLineOptionsModule;
27 27
 import com.dmdirc.config.IdentityManager;
28 28
 import com.dmdirc.interfaces.ActionController;
29 29
 import com.dmdirc.interfaces.IdentityController;
30
+import com.dmdirc.interfaces.LifecycleController;
30 31
 
31 32
 import javax.inject.Singleton;
32 33
 
@@ -100,4 +101,15 @@ public class ClientModule {
100 101
         return actionManager;
101 102
     }
102 103
 
104
+    /**
105
+     * Provides a lifecycle controller.
106
+     *
107
+     * @param controller The concrete implementation to use.
108
+     * @return The lifecycle controller the app should use.
109
+     */
110
+    @Provides
111
+    public LifecycleController getLifecycleController(final SystemLifecycleController controller) {
112
+        return controller;
113
+    }
114
+
103 115
 }

+ 33
- 7
src/com/dmdirc/Main.java Zobrazit soubor

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc;
24 24
 
25
+import com.dmdirc.interfaces.LifecycleController;
25 26
 import com.dmdirc.actions.ActionManager;
26 27
 import com.dmdirc.actions.CoreActionType;
27 28
 import com.dmdirc.commandline.CommandLineOptionsModule;
@@ -90,6 +91,9 @@ public class Main implements LifecycleController {
90 91
     /** The plugin manager the client will use. */
91 92
     private final PluginManager pluginManager;
92 93
 
94
+    /** The command loader to use to initialise the command manager. */
95
+    private final CommandLoader commandLoader;
96
+
93 97
     /** The config dir to use for the client. */
94 98
     private final String configdir;
95 99
 
@@ -105,6 +109,7 @@ public class Main implements LifecycleController {
105 109
      * @param actionManager The action manager the client will use.
106 110
      * @param commandLineParser The command-line parser used for this instance.
107 111
      * @param pluginManager The plugin manager the client will use.
112
+     * @param commandLoader The command loader to use to initialise the command manager.
108 113
      * @param configDir The base configuration directory to use.
109 114
      */
110 115
     @Inject
@@ -114,12 +119,14 @@ public class Main implements LifecycleController {
114 119
             final ActionManager actionManager,
115 120
             final CommandLineParser commandLineParser,
116 121
             final PluginManager pluginManager,
122
+            final CommandLoader commandLoader,
117 123
             @Directory(DirectoryType.BASE) final String configDir) {
118 124
         this.identityManager = identityManager;
119 125
         this.serverManager = serverManager;
120 126
         this.actionManager = actionManager;
121 127
         this.commandLineParser = commandLineParser;
122 128
         this.pluginManager = pluginManager;
129
+        this.commandLoader = commandLoader;
123 130
         this.configdir = configDir;
124 131
     }
125 132
 
@@ -167,8 +174,7 @@ public class Main implements LifecycleController {
167 174
 
168 175
         commandLineParser.applySettings(identityManager.getGlobalConfigIdentity());
169 176
 
170
-        new CommandLoader(this, serverManager, pluginManager, identityManager)
171
-                .loadCommands(CommandManager.initCommandManager(identityManager, serverManager));
177
+        commandLoader.loadCommands(CommandManager.initCommandManager(identityManager, serverManager));
172 178
 
173 179
         for (String service : new String[]{"ui", "tabcompletion", "parser"}) {
174 180
             ensureExists(pluginManager, service);
@@ -400,27 +406,47 @@ public class Main implements LifecycleController {
400 406
         }
401 407
     }
402 408
 
403
-    /** {@inheritDoc} */
409
+    /**
410
+     * {@inheritDoc}
411
+     *
412
+     * @deprecated Use a proper {@link LifecycleController}.
413
+     */
404 414
     @Override
415
+    @Deprecated
405 416
     public void quit() {
406 417
         quit(0);
407 418
     }
408 419
 
409
-    /** {@inheritDoc} */
420
+    /**
421
+     * {@inheritDoc}
422
+     *
423
+     * @deprecated Use a proper {@link LifecycleController}.
424
+     */
410 425
     @Override
426
+    @Deprecated
411 427
     public void quit(final int exitCode) {
412
-        quit(IdentityManager.getIdentityManager().getGlobalConfiguration().getOption("general",
428
+        quit(identityManager.getGlobalConfiguration().getOption("general",
413 429
                 "closemessage"), exitCode);
414 430
     }
415 431
 
416
-    /** {@inheritDoc} */
432
+    /**
433
+     * {@inheritDoc}
434
+     *
435
+     * @deprecated Use a proper {@link LifecycleController}.
436
+     */
417 437
     @Override
438
+    @Deprecated
418 439
     public void quit(final String reason) {
419 440
         quit(reason, 0);
420 441
     }
421 442
 
422
-    /** {@inheritDoc} */
443
+    /**
444
+     * {@inheritDoc}
445
+     *
446
+     * @deprecated Use a proper {@link LifecycleController}.
447
+     */
423 448
     @Override
449
+    @Deprecated
424 450
     public void quit(final String reason, final int exitCode) {
425 451
         serverManager.disconnectAll(reason);
426 452
 

+ 76
- 0
src/com/dmdirc/SystemLifecycleController.java Zobrazit soubor

@@ -0,0 +1,76 @@
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.interfaces.IdentityController;
26
+import com.dmdirc.interfaces.LifecycleController;
27
+
28
+import javax.inject.Inject;
29
+
30
+/**
31
+ * Simple {@link LifecycleController} implementation that calls {@link System#exit(int)}.
32
+ */
33
+public class SystemLifecycleController implements LifecycleController {
34
+
35
+    /** Controller to retrieve settings from. */
36
+    private final IdentityController identityController;
37
+
38
+    /** Manager to use to disconnect servers. */
39
+    private final ServerManager serverManager;
40
+
41
+    @Inject
42
+    public SystemLifecycleController(
43
+            final IdentityController identityController,
44
+            final ServerManager serverManager) {
45
+        this.identityController = identityController;
46
+        this.serverManager = serverManager;
47
+    }
48
+
49
+    /** {@inheritDoc} */
50
+    @Override
51
+    public void quit() {
52
+        quit(0);
53
+    }
54
+
55
+    /** {@inheritDoc} */
56
+    @Override
57
+    public void quit(final int exitCode) {
58
+        quit(identityController.getGlobalConfiguration()
59
+                .getOption("general", "closemessage"), exitCode);
60
+    }
61
+
62
+    /** {@inheritDoc} */
63
+    @Override
64
+    public void quit(final String reason) {
65
+        quit(reason, 0);
66
+    }
67
+
68
+    /** {@inheritDoc} */
69
+    @Override
70
+    public void quit(final String reason, final int exitCode) {
71
+        serverManager.disconnectAll(reason);
72
+
73
+        System.exit(exitCode);
74
+    }
75
+
76
+}

+ 4
- 1
src/com/dmdirc/commandparser/CommandLoader.java Zobrazit soubor

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.commandparser;
24 24
 
25
-import com.dmdirc.LifecycleController;
25
+import com.dmdirc.interfaces.LifecycleController;
26 26
 import com.dmdirc.ServerManager;
27 27
 import com.dmdirc.commandparser.commands.channel.Ban;
28 28
 import com.dmdirc.commandparser.commands.channel.Cycle;
@@ -71,6 +71,8 @@ import com.dmdirc.config.IdentityManager;
71 71
 import com.dmdirc.interfaces.CommandController;
72 72
 import com.dmdirc.plugins.PluginManager;
73 73
 
74
+import javax.inject.Inject;
75
+
74 76
 /**
75 77
  * Facilitates loading of core commands into a {@link CommandManager}.
76 78
  */
@@ -96,6 +98,7 @@ public class CommandLoader {
96 98
      * @param pluginManager The plugin manager to pass to plugin-dependent commands.
97 99
      * @param identityManager The identity manager to pass to config-related commands.
98 100
      */
101
+    @Inject
99 102
     public CommandLoader(
100 103
             final LifecycleController lifecycleController,
101 104
             final ServerManager serverManager,

+ 1
- 1
src/com/dmdirc/commandparser/commands/global/Exit.java Zobrazit soubor

@@ -23,7 +23,7 @@
23 23
 package com.dmdirc.commandparser.commands.global;
24 24
 
25 25
 import com.dmdirc.FrameContainer;
26
-import com.dmdirc.LifecycleController;
26
+import com.dmdirc.interfaces.LifecycleController;
27 27
 import com.dmdirc.commandparser.BaseCommandInfo;
28 28
 import com.dmdirc.commandparser.CommandArguments;
29 29
 import com.dmdirc.commandparser.CommandInfo;

src/com/dmdirc/LifecycleController.java → src/com/dmdirc/interfaces/LifecycleController.java Zobrazit soubor

@@ -20,7 +20,7 @@
20 20
  * SOFTWARE.
21 21
  */
22 22
 
23
-package com.dmdirc;
23
+package com.dmdirc.interfaces;
24 24
 
25 25
 /**
26 26
  * Provides methods that control the lifecycle of the application.

+ 2
- 1
test/com/dmdirc/TestMain.java Zobrazit soubor

@@ -30,7 +30,8 @@ public class TestMain extends Main {
30 30
             final CommandLineParser commandLineParser,
31 31
             final PluginManager pluginManager,
32 32
             final String configDir) {
33
-        super(identityManager, serverManager, actionManager, commandLineParser, pluginManager, configDir);
33
+        super(identityManager, serverManager, actionManager, commandLineParser,
34
+                pluginManager, null, configDir);
34 35
         this.identityManager = identityManager;
35 36
         this.serverManager = serverManager;
36 37
         this.actionManager = actionManager;

+ 1
- 1
test/com/dmdirc/commandparser/commands/HelpTest.java Zobrazit soubor

@@ -21,7 +21,7 @@
21 21
  */
22 22
 package com.dmdirc.commandparser.commands;
23 23
 
24
-import com.dmdirc.LifecycleController;
24
+import com.dmdirc.interfaces.LifecycleController;
25 25
 import com.dmdirc.ServerManager;
26 26
 import com.dmdirc.TestMain;
27 27
 import com.dmdirc.commandparser.CommandInfo;

Načítá se…
Zrušit
Uložit