Browse Source

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 11 years ago
parent
commit
1afcf20413

+ 59
- 0
src/com/dmdirc/LifecycleController.java View File

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 View File

60
 /**
60
 /**
61
  * Main class, handles initialisation.
61
  * Main class, handles initialisation.
62
  */
62
  */
63
-public class Main {
63
+public class Main implements LifecycleController {
64
 
64
 
65
     /** Feedback nag delay. */
65
     /** Feedback nag delay. */
66
     private final int FEEDBACK_DELAY = 30 * 60 * 1000;
66
     private final int FEEDBACK_DELAY = 30 * 60 * 1000;
128
 
128
 
129
         clp.applySettings();
129
         clp.applySettings();
130
 
130
 
131
-        new CommandLoader(serverManager, pluginManager)
131
+        new CommandLoader(this, serverManager, pluginManager)
132
                 .loadCommands(CommandManager.initCommandManager(IdentityManager.getIdentityManager(), serverManager));
132
                 .loadCommands(CommandManager.initCommandManager(IdentityManager.getIdentityManager(), serverManager));
133
 
133
 
134
         for (String service : new String[]{"ui", "tabcompletion", "parser"}) {
134
         for (String service : new String[]{"ui", "tabcompletion", "parser"}) {
366
         }
366
         }
367
     }
367
     }
368
 
368
 
369
-    /**
370
-     * Quits the client nicely, with the default closing message.
371
-     */
369
+    /** {@inheritDoc} */
370
+    @Override
372
     public void quit() {
371
     public void quit() {
373
         quit(0);
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
     public void quit(final int exitCode) {
377
     public void quit(final int exitCode) {
383
         quit(IdentityManager.getIdentityManager().getGlobalConfiguration().getOption("general",
378
         quit(IdentityManager.getIdentityManager().getGlobalConfiguration().getOption("general",
384
                 "closemessage"), exitCode);
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
     public void quit(final String reason) {
384
     public void quit(final String reason) {
393
         quit(reason, 0);
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
     public void quit(final String reason, final int exitCode) {
390
     public void quit(final String reason, final int exitCode) {
404
         serverManager.disconnectAll(reason);
391
         serverManager.disconnectAll(reason);
405
 
392
 

+ 8
- 1
src/com/dmdirc/commandparser/CommandLoader.java View File

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

+ 14
- 2
src/com/dmdirc/commandparser/commands/global/Exit.java View File

23
 package com.dmdirc.commandparser.commands.global;
23
 package com.dmdirc.commandparser.commands.global;
24
 
24
 
25
 import com.dmdirc.FrameContainer;
25
 import com.dmdirc.FrameContainer;
26
-import com.dmdirc.Main;
26
+import com.dmdirc.LifecycleController;
27
 import com.dmdirc.commandparser.BaseCommandInfo;
27
 import com.dmdirc.commandparser.BaseCommandInfo;
28
 import com.dmdirc.commandparser.CommandArguments;
28
 import com.dmdirc.commandparser.CommandArguments;
29
 import com.dmdirc.commandparser.CommandInfo;
29
 import com.dmdirc.commandparser.CommandInfo;
43
             "exit [reason] - exits the client",
43
             "exit [reason] - exits the client",
44
             CommandType.TYPE_GLOBAL);
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
     /** {@inheritDoc} */
58
     /** {@inheritDoc} */
47
     @Override
59
     @Override
48
     public void execute(final FrameContainer origin,
60
     public void execute(final FrameContainer origin,
49
             final CommandArguments args, final CommandContext context) {
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
                 : origin.getConfigManager().getOption("general", "closemessage"));
63
                 : origin.getConfigManager().getOption("general", "closemessage"));
52
     }
64
     }
53
 
65
 

+ 2
- 0
test/com/dmdirc/commandparser/commands/HelpTest.java View File

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

Loading…
Cancel
Save