Pārlūkot izejas kodu

Add interface for things that want to quit.

Allows us to pass in a sensible reference to things like the /exit
command, instead of a Main object or calling a method statically.

Change-Id: I3a794339cbf0c2052ff1d7bf377dacb6171bf1f7
Reviewed-on: http://gerrit.dmdirc.com/2662
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8rc1
Chris Smith 10 gadus atpakaļ
vecāks
revīzija
1afcf20413

+ 59
- 0
src/com/dmdirc/LifecycleController.java Parādīt failu

@@ -0,0 +1,59 @@
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
+/**
26
+ * Provides methods that control the lifecycle of the application.
27
+ */
28
+public interface LifecycleController {
29
+
30
+    /**
31
+     * Quits the client nicely, with the default closing message.
32
+     */
33
+    void quit();
34
+
35
+    /**
36
+     * Quits the client nicely, with the default closing message.
37
+     *
38
+     * @param exitCode This is the exit code that will be returned to the
39
+     *                  operating system when the client exits
40
+     */
41
+    void quit(final int exitCode);
42
+
43
+    /**
44
+     * Quits the client nicely.
45
+     *
46
+     * @param reason The quit reason to send
47
+     */
48
+    void quit(final String reason);
49
+
50
+    /**
51
+     * Quits the client nicely.
52
+     *
53
+     * @param reason The quit reason to send
54
+     * @param exitCode This is the exit code that will be returned to the
55
+     *                  operating system when the client exits
56
+     */
57
+    void quit(final String reason, final int exitCode);
58
+
59
+}

+ 10
- 23
src/com/dmdirc/Main.java Parādīt failu

@@ -60,7 +60,7 @@ import java.util.TimerTask;
60 60
 /**
61 61
  * Main class, handles initialisation.
62 62
  */
63
-public class Main {
63
+public class Main implements LifecycleController {
64 64
 
65 65
     /** Feedback nag delay. */
66 66
     private final int FEEDBACK_DELAY = 30 * 60 * 1000;
@@ -128,7 +128,7 @@ public class Main {
128 128
 
129 129
         clp.applySettings();
130 130
 
131
-        new CommandLoader(serverManager, pluginManager)
131
+        new CommandLoader(this, serverManager, pluginManager)
132 132
                 .loadCommands(CommandManager.initCommandManager(IdentityManager.getIdentityManager(), serverManager));
133 133
 
134 134
         for (String service : new String[]{"ui", "tabcompletion", "parser"}) {
@@ -366,40 +366,27 @@ public class Main {
366 366
         }
367 367
     }
368 368
 
369
-    /**
370
-     * Quits the client nicely, with the default closing message.
371
-     */
369
+    /** {@inheritDoc} */
370
+    @Override
372 371
     public void quit() {
373 372
         quit(0);
374 373
     }
375 374
 
376
-    /**
377
-     * Quits the client nicely, with the default closing message.
378
-     *
379
-     * @param exitCode This is the exit code that will be returned to the
380
-     *                  operating system when the client exits
381
-     */
375
+    /** {@inheritDoc} */
376
+    @Override
382 377
     public void quit(final int exitCode) {
383 378
         quit(IdentityManager.getIdentityManager().getGlobalConfiguration().getOption("general",
384 379
                 "closemessage"), exitCode);
385 380
     }
386 381
 
387
-    /**
388
-     * Quits the client nicely.
389
-     *
390
-     * @param reason The quit reason to send
391
-     */
382
+    /** {@inheritDoc} */
383
+    @Override
392 384
     public void quit(final String reason) {
393 385
         quit(reason, 0);
394 386
     }
395 387
 
396
-    /**
397
-     * Quits the client nicely.
398
-     *
399
-     * @param reason The quit reason to send
400
-     * @param exitCode This is the exit code that will be returned to the
401
-     *                  operating system when the client exits
402
-     */
388
+    /** {@inheritDoc} */
389
+    @Override
403 390
     public void quit(final String reason, final int exitCode) {
404 391
         serverManager.disconnectAll(reason);
405 392
 

+ 8
- 1
src/com/dmdirc/commandparser/CommandLoader.java Parādīt failu

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.commandparser;
24 24
 
25
+import com.dmdirc.LifecycleController;
25 26
 import com.dmdirc.ServerManager;
26 27
 import com.dmdirc.commandparser.commands.channel.Ban;
27 28
 import com.dmdirc.commandparser.commands.channel.Cycle;
@@ -74,6 +75,9 @@ import com.dmdirc.plugins.PluginManager;
74 75
  */
75 76
 public class CommandLoader {
76 77
 
78
+    /** Controller to give to commands that want to start/stop the app. */
79
+    private final LifecycleController lifecycleController;
80
+
77 81
     /** Server manager to give to server-related commands. */
78 82
     private final ServerManager serverManager;
79 83
 
@@ -83,12 +87,15 @@ public class CommandLoader {
83 87
     /**
84 88
      * Creates a new instance of {@link CommandLoader}.
85 89
      *
90
+     * @param lifecycleController Controller to give to commands that want to start/stop the app.
86 91
      * @param serverManager The server manager to pass to server-related commands.
87 92
      * @param pluginManager The plugin manager to pass to plugin-dependent commands.
88 93
      */
89 94
     public CommandLoader(
95
+            final LifecycleController lifecycleController,
90 96
             final ServerManager serverManager,
91 97
             final PluginManager pluginManager) {
98
+        this.lifecycleController = lifecycleController;
92 99
         this.serverManager = serverManager;
93 100
         this.pluginManager = pluginManager;
94 101
     }
@@ -144,7 +151,7 @@ public class CommandLoader {
144 151
         manager.registerCommand(new AllServers(serverManager), AllServers.INFO);
145 152
         manager.registerCommand(new Clear(), Clear.INFO);
146 153
         manager.registerCommand(new Echo(), Echo.INFO);
147
-        manager.registerCommand(new Exit(), Exit.INFO);
154
+        manager.registerCommand(new Exit(lifecycleController), Exit.INFO);
148 155
         manager.registerCommand(new Help(), Help.INFO);
149 156
         manager.registerCommand(new Ifplugin(pluginManager), Ifplugin.INFO);
150 157
         manager.registerCommand(new NewServer(serverManager, pluginManager), NewServer.INFO);

+ 14
- 2
src/com/dmdirc/commandparser/commands/global/Exit.java Parādīt failu

@@ -23,7 +23,7 @@
23 23
 package com.dmdirc.commandparser.commands.global;
24 24
 
25 25
 import com.dmdirc.FrameContainer;
26
-import com.dmdirc.Main;
26
+import com.dmdirc.LifecycleController;
27 27
 import com.dmdirc.commandparser.BaseCommandInfo;
28 28
 import com.dmdirc.commandparser.CommandArguments;
29 29
 import com.dmdirc.commandparser.CommandInfo;
@@ -43,11 +43,23 @@ public class Exit extends Command {
43 43
             "exit [reason] - exits the client",
44 44
             CommandType.TYPE_GLOBAL);
45 45
 
46
+    /** The controller to use to quit the app. */
47
+    private final LifecycleController controller;
48
+
49
+    /**
50
+     * Creates a new instance of the {@link Exit} command.
51
+     *
52
+     * @param controller The controller to use to quit the app.
53
+     */
54
+    public Exit(final LifecycleController controller) {
55
+        this.controller = controller;
56
+    }
57
+
46 58
     /** {@inheritDoc} */
47 59
     @Override
48 60
     public void execute(final FrameContainer origin,
49 61
             final CommandArguments args, final CommandContext context) {
50
-        Main.mainInstance.quit(args.getArguments().length > 0 ? args.getArgumentsAsString()
62
+        controller.quit(args.getArguments().length > 0 ? args.getArgumentsAsString()
51 63
                 : origin.getConfigManager().getOption("general", "closemessage"));
52 64
     }
53 65
 

+ 2
- 0
test/com/dmdirc/commandparser/commands/HelpTest.java Parādīt failu

@@ -21,6 +21,7 @@
21 21
  */
22 22
 package com.dmdirc.commandparser.commands;
23 23
 
24
+import com.dmdirc.LifecycleController;
24 25
 import com.dmdirc.ServerManager;
25 26
 import com.dmdirc.TestMain;
26 27
 import com.dmdirc.commandparser.CommandInfo;
@@ -61,6 +62,7 @@ public class HelpTest {
61 62
 
62 63
         TestMain.getTestMain();
63 64
         new CommandLoader(
65
+                mock(LifecycleController.class),
64 66
                 mock(ServerManager.class),
65 67
                 mock(PluginManager.class)).loadCommands(CommandManager.getCommandManager());
66 68
 

Notiek ielāde…
Atcelt
Saglabāt