Browse Source

DI the parser debug plugin.

Change-Id: I74bf46c25c17dd0297e65421ea726a301afbef46
Reviewed-on: http://gerrit.dmdirc.com/3218
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.8
Greg Holmes 10 years ago
parent
commit
849591c40d

+ 13
- 89
src/com/dmdirc/addons/parserdebug/DebugPlugin.java View File

@@ -22,113 +22,37 @@
22 22
 
23 23
 package com.dmdirc.addons.parserdebug;
24 24
 
25
-import com.dmdirc.actions.CoreActionType;
26
-import com.dmdirc.interfaces.ActionController;
27
-import com.dmdirc.interfaces.ActionListener;
28
-import com.dmdirc.interfaces.CommandController;
29
-import com.dmdirc.interfaces.Connection;
30
-import com.dmdirc.interfaces.actions.ActionType;
31
-import com.dmdirc.parser.interfaces.Parser;
32
-import com.dmdirc.parser.interfaces.callbacks.DebugInfoListener;
25
+import com.dmdirc.plugins.PluginInfo;
33 26
 import com.dmdirc.plugins.implementations.BaseCommandPlugin;
34
-import com.dmdirc.ui.WindowManager;
35
-import com.dmdirc.util.URLBuilder;
36 27
 
37
-import java.util.ArrayList;
38
-import java.util.Date;
39
-import java.util.HashMap;
40
-import java.util.Map;
28
+import dagger.ObjectGraph;
41 29
 
42 30
 /**
43
- * This causes parser debugging to be spammed to the console.
31
+ * This causes parser debugging to be output to the a debug window.
44 32
  */
45
-public final class DebugPlugin extends BaseCommandPlugin implements
46
-        DebugInfoListener, ActionListener {
33
+public final class DebugPlugin extends BaseCommandPlugin {
47 34
 
48
-    /** Map of parsers registered. */
49
-    protected final Map<Parser, DebugWindow> registeredParsers = new HashMap<>();
50 35
     /** The action controller to use. */
51
-    private final ActionController actionController;
36
+    private ParserDebugManager manager;
52 37
 
53
-    /**
54
-     * Creates a new instance of this plugin.
55
-     *
56
-     * @param actionController  The action controller to register listeners with
57
-     * @param commandController Command controller to register commands
58
-     * @param windowManager     Window Manager
59
-     * @param urlBuilder        The URL builder to use when finding icons.
60
-     */
61
-    public DebugPlugin(
62
-            final ActionController actionController,
63
-            final CommandController commandController,
64
-            final WindowManager windowManager,
65
-            final URLBuilder urlBuilder) {
66
-        super(commandController);
67
-
68
-        this.actionController = actionController;
69
-
70
-        registerCommand(new ParserDebugCommand(commandController, this, windowManager, urlBuilder),
71
-                ParserDebugCommand.INFO);
38
+    @Override
39
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
40
+        super.load(pluginInfo, graph);
41
+        setObjectGraph(graph.plus(new ParserDebugModule()));
42
+        registerCommand(ParserDebugCommand.class, ParserDebugCommand.INFO);
43
+        manager = getObjectGraph().get(ParserDebugManager.class);
72 44
     }
73 45
 
74
-    /** {@inheritDoc} */
75 46
     @Override
76 47
     public void onLoad() {
77
-        actionController.registerListener(this, CoreActionType.SERVER_DISCONNECTED);
48
+        manager.addActionListener();
78 49
         super.onLoad();
79 50
     }
80 51
 
81
-    /** {@inheritDoc} */
82 52
     @Override
83 53
     public void onUnload() {
84
-        actionController.unregisterListener(this);
85
-
86
-        final ArrayList<DebugWindow> windowList = new ArrayList<>();
87
-        for (Parser parser : registeredParsers.keySet()) {
88
-            try {
89
-                parser.getCallbackManager().delCallback(DebugInfoListener.class, this);
90
-                final DebugWindow window = registeredParsers.get(parser);
91
-                windowList.add(window);
92
-            } catch (Exception e) {
93
-            }
94
-        }
95
-        for (DebugWindow window : windowList) {
96
-            window.close();
97
-        }
98
-        registeredParsers.clear();
54
+        manager.removeActionListener();
99 55
         super.onUnload();
100 56
     }
101 57
 
102
-    /** {@inheritDoc} */
103
-    @Override
104
-    public void onDebugInfo(final Parser parser, final Date date, final int level, final String data) {
105
-        final DebugWindow window = registeredParsers.get(parser);
106
-        if (window != null) {
107
-            window.addLine(String.format("[%d] %s%n", level, data), true);
108
-        }
109
-    }
110
-
111
-    /** {@inheritDoc} */
112
-    @Override
113
-    public void processEvent(final ActionType type, final StringBuffer format,
114
-            final Object... arguments) {
115
-        if (type == CoreActionType.SERVER_DISCONNECTED) {
116
-            final Connection connection = (Connection) arguments[0];
117
-            final Parser parser = connection.getParser();
118
-            if (registeredParsers.containsKey(parser)) {
119
-                try {
120
-                    final DebugWindow window = registeredParsers.get(parser);
121
-                    registeredParsers.remove(parser);
122
-                    window.unsetParser();
123
-                    parser.getCallbackManager().delCallback(DebugInfoListener.class, this);
124
-                    window.addLine("======================", true);
125
-                    window.addLine("No Longer Monitoring: " + parser + " (Server Disconnected)",
126
-                            true);
127
-                    window.addLine("======================", true);
128
-                } catch (Exception e) {
129
-                }
130
-            }
131
-        }
132
-    }
133
-
134 58
 }

+ 11
- 11
src/com/dmdirc/addons/parserdebug/DebugWindow.java View File

@@ -37,33 +37,33 @@ import java.util.Arrays;
37 37
  */
38 38
 public class DebugWindow extends FrameContainer {
39 39
 
40
-    /** The plugin that owns this window */
41
-    protected final DebugPlugin plugin;
42
-    /** The parser this window is debugging */
40
+    /** The debug listener for this window. */
41
+    protected final DebugInfoListener listener;
42
+    /** The parser this window is debugging. */
43 43
     protected Parser parser;
44
-    /** The connection we're operating on */
45
-    protected final Connection connection;
44
+    /** The server we're operating on. */
45
+    protected final Server server;
46 46
 
47 47
     /**
48 48
      * Creates a new instance of DebugWindow.
49 49
      *
50
-     * @param plugin     The plugin that owns this window
50
+     * @param listener   The debug listener for this window
51 51
      * @param title      The title of this window
52 52
      * @param parser     The parser this plugin is debugging
53 53
      * @param server     The Server window this is a child of
54 54
      * @param urlBuilder The URL builder to use when finding icons.
55 55
      */
56 56
     public DebugWindow(
57
-            final DebugPlugin plugin,
57
+            final DebugInfoListener listener,
58 58
             final String title,
59 59
             final Parser parser,
60 60
             final Server server,
61 61
             final URLBuilder urlBuilder) {
62 62
         super("raw", "Parser Debug", title, server.getConfigManager(), urlBuilder,
63 63
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier()));
64
-        this.plugin = plugin;
64
+        this.listener = listener;
65 65
         this.parser = parser;
66
-        this.connection = server;
66
+        this.server = server;
67 67
     }
68 68
 
69 69
     /**
@@ -73,7 +73,7 @@ public class DebugWindow extends FrameContainer {
73 73
      */
74 74
     @Override
75 75
     public Connection getConnection() {
76
-        return connection;
76
+        return server;
77 77
     }
78 78
 
79 79
     /**
@@ -94,7 +94,7 @@ public class DebugWindow extends FrameContainer {
94 94
 
95 95
         // Remove any callbacks or listeners
96 96
         if (parser != null) {
97
-            parser.getCallbackManager().delCallback(DebugInfoListener.class, plugin);
97
+            parser.getCallbackManager().delCallback(DebugInfoListener.class, listener);
98 98
         }
99 99
     }
100 100
 

+ 16
- 40
src/com/dmdirc/addons/parserdebug/ParserDebugCommand.java View File

@@ -33,9 +33,8 @@ import com.dmdirc.commandparser.commands.context.CommandContext;
33 33
 import com.dmdirc.commandparser.commands.context.ServerCommandContext;
34 34
 import com.dmdirc.interfaces.CommandController;
35 35
 import com.dmdirc.parser.interfaces.Parser;
36
-import com.dmdirc.parser.interfaces.callbacks.DebugInfoListener;
37
-import com.dmdirc.ui.WindowManager;
38
-import com.dmdirc.util.URLBuilder;
36
+
37
+import javax.inject.Inject;
39 38
 
40 39
 /**
41 40
  * The ParserDebug Command allows controlling of which parsers spam debug info.
@@ -48,35 +47,27 @@ public final class ParserDebugCommand extends Command {
48 47
             "parserdebug", "parserdebug - Enables/Disables hooks for "
49 48
             + "onDebugInfo for the parser that owns this window",
50 49
             CommandType.TYPE_SERVER);
51
-    /** My Plugin */
52
-    final DebugPlugin myPlugin;
53
-    /** Window management. */
54
-    private final WindowManager windowManager;
55
-    private final URLBuilder urlBuilder;
50
+    /** Parser debug manager. */
51
+    final ParserDebugManager parserDebugManager;
56 52
 
57 53
     /**
58 54
      * Creates a new instance of ParserDebugCommand.
59 55
      *
60
-     * @param controller    The controller to use for command information.
61
-     * @param plugin        Plugin that owns this command
62
-     * @param windowManager Window management
63
-     * @param urlBuilder    The URL builder to use when finding icons.
56
+     * @param controller         The controller to use for command information.
57
+     * @param parserDebugManager Parser debug manager
64 58
      */
59
+    @Inject
65 60
     public ParserDebugCommand(
66 61
             final CommandController controller,
67
-            final DebugPlugin plugin,
68
-            final WindowManager windowManager,
69
-            final URLBuilder urlBuilder) {
62
+            final ParserDebugManager parserDebugManager) {
70 63
         super(controller);
71
-        myPlugin = plugin;
72
-        this.windowManager = windowManager;
73
-        this.urlBuilder = urlBuilder;
64
+        this.parserDebugManager = parserDebugManager;
74 65
     }
75 66
 
76 67
     /**
77 68
      * Executes this command.
78 69
      *
79
-     * @param origin      The framecontainer in which this command was issued
70
+     * @param origin      The frame container in which this command was issued
80 71
      * @param commandArgs The user supplied arguments
81 72
      * @param context     The Context of this command execution
82 73
      */
@@ -85,37 +76,22 @@ public final class ParserDebugCommand extends Command {
85 76
             final CommandContext context) {
86 77
         final boolean isSilent = commandArgs.isSilent();
87 78
 
88
-        final Parser parser = ((ServerCommandContext) context).getServer()
89
-                .getParser();
79
+        final Parser parser = ((ServerCommandContext) context).getServer().getParser();
90 80
 
91 81
         if (parser == null) {
92 82
             sendLine(origin, isSilent, FORMAT_ERROR, "Unable to get a parser for this window.");
93 83
             return;
94 84
         }
95
-        if (myPlugin.registeredParsers.containsKey(parser)) {
96
-            try {
97
-                parser.getCallbackManager().delCallback(DebugInfoListener.class, myPlugin);
98
-                final DebugWindow window = myPlugin.registeredParsers.get(parser);
99
-                window.addLine("======================", true);
100
-                window.addLine("No Longer Monitoring: " + parser + " (User Requested)", true);
101
-                window.addLine("======================", true);
102
-                myPlugin.registeredParsers.remove(parser);
85
+        if (parserDebugManager.containsParser(parser)) {
86
+            if (parserDebugManager.removeParser(parser, false)) {
103 87
                 sendLine(origin, isSilent, FORMAT_OUTPUT, "Removing callback ok");
104
-            } catch (Exception e) {
88
+            } else {
105 89
                 sendLine(origin, isSilent, FORMAT_ERROR, "Removing callback failed");
106 90
             }
107 91
         } else {
108
-            try {
109
-                parser.getCallbackManager().addCallback(DebugInfoListener.class, myPlugin);
110
-                final DebugWindow window = new DebugWindow(myPlugin,
111
-                        "Parser Debug", parser, (Server) origin.getConnection(), urlBuilder);
112
-                windowManager.addWindow((Server) origin.getConnection(), window);
113
-                myPlugin.registeredParsers.put(parser, window);
92
+            if (parserDebugManager.addParser(parser, (Server) origin.getConnection())) {
114 93
                 sendLine(origin, isSilent, FORMAT_OUTPUT, "Adding callback ok");
115
-                window.addLine("======================", true);
116
-                window.addLine("Started Monitoring: " + parser, true);
117
-                window.addLine("======================", true);
118
-            } catch (Exception e) {
94
+            } else {
119 95
                 sendLine(origin, isSilent, FORMAT_ERROR, "Adding callback failed");
120 96
             }
121 97
         }

+ 168
- 0
src/com/dmdirc/addons/parserdebug/ParserDebugManager.java View File

@@ -0,0 +1,168 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.addons.parserdebug;
24
+
25
+import com.dmdirc.Server;
26
+import com.dmdirc.actions.CoreActionType;
27
+import com.dmdirc.interfaces.ActionController;
28
+import com.dmdirc.interfaces.ActionListener;
29
+import com.dmdirc.interfaces.Connection;
30
+import com.dmdirc.interfaces.actions.ActionType;
31
+import com.dmdirc.parser.common.CallbackNotFoundException;
32
+import com.dmdirc.parser.interfaces.Parser;
33
+import com.dmdirc.parser.interfaces.callbacks.DebugInfoListener;
34
+import com.dmdirc.ui.WindowManager;
35
+import com.dmdirc.util.URLBuilder;
36
+
37
+import java.util.Date;
38
+import java.util.HashMap;
39
+import java.util.Map;
40
+
41
+import javax.inject.Inject;
42
+
43
+public class ParserDebugManager implements ActionListener, DebugInfoListener {
44
+
45
+    /** Map of parsers registered. */
46
+    protected final Map<Parser, DebugWindow> registeredParsers;
47
+    /** Action controller. */
48
+    private final ActionController actionController;
49
+    /** URL Builder. */
50
+    private final URLBuilder urlBuilder;
51
+    /** Window manager. */
52
+    private final WindowManager windowManager;
53
+
54
+    @Inject
55
+    public ParserDebugManager(final ActionController actionController,
56
+            final URLBuilder urlBuilder,
57
+            final WindowManager windowManager) {
58
+        this.actionController = actionController;
59
+        this.urlBuilder = urlBuilder;
60
+        this.windowManager = windowManager;
61
+        registeredParsers = new HashMap<>();
62
+    }
63
+
64
+    /**
65
+     * Adds action listener.
66
+     */
67
+    public void addActionListener() {
68
+        actionController.registerListener(this, CoreActionType.SERVER_DISCONNECTED);
69
+    }
70
+
71
+    /**
72
+     * Remove action listener.
73
+     */
74
+    public void removeActionListener() {
75
+        actionController.unregisterListener(this);
76
+    }
77
+
78
+    /**
79
+     * Are we listener on the specified parser?
80
+     *
81
+     * @param parser Parser to check
82
+     *
83
+     * @return true if we're listening
84
+     */
85
+    public boolean containsParser(final Parser parser) {
86
+        return registeredParsers.containsKey(parser);
87
+    }
88
+
89
+    /**
90
+     * Adds a parser to this manager, adding any required call backs.
91
+     *
92
+     * @param parser Parser to add
93
+     * @param server The server associated with the parser
94
+     *
95
+     * @return Whether we added the parser without error
96
+     */
97
+    public boolean addParser(final Parser parser, final Server server) {
98
+        try {
99
+            parser.getCallbackManager().addCallback(DebugInfoListener.class, this);
100
+            final DebugWindow window = new DebugWindow(this, "Parser Debug", parser,
101
+                    server, urlBuilder);
102
+            windowManager.addWindow(server, window);
103
+            registeredParsers.put(parser, window);
104
+            window.addLine("======================", true);
105
+            window.addLine("Started Monitoring: " + parser, true);
106
+            window.addLine("======================", true);
107
+            return true;
108
+        } catch (CallbackNotFoundException ex) {
109
+            return false;
110
+        }
111
+    }
112
+
113
+    /**
114
+     * Removes the parser from this manager, removing any call backs as required.
115
+     *
116
+     * @param parser Parser to add
117
+     * @param close  Close debug window?
118
+     *
119
+     * @return Whether removed the parser without error
120
+     */
121
+    public boolean removeParser(final Parser parser, final boolean close) {
122
+        try {
123
+            parser.getCallbackManager().delCallback(DebugInfoListener.class, this);
124
+            final DebugWindow window = registeredParsers.get(parser);
125
+            window.addLine("======================", true);
126
+            window.addLine("No Longer Monitoring: " + parser + " (User Requested)", true);
127
+            window.addLine("======================", true);
128
+            if (close) {
129
+                window.close();
130
+            }
131
+            registeredParsers.remove(parser);
132
+            return true;
133
+        } catch (CallbackNotFoundException ex) {
134
+            return false;
135
+        }
136
+    }
137
+
138
+    /**
139
+     * Removes all parser listeners and closes.
140
+     *
141
+     * @param close Close debug windows?
142
+     */
143
+    public void removeAllParserListeners(final boolean close) {
144
+        for (Parser parser : registeredParsers.keySet()) {
145
+            removeParser(parser, close);
146
+        }
147
+    }
148
+
149
+    @Override
150
+    public void processEvent(final ActionType type, final StringBuffer format,
151
+            final Object... arguments) {
152
+        if (type == CoreActionType.SERVER_DISCONNECTED) {
153
+            final Parser parser = ((Connection) arguments[0]).getParser();
154
+            if (registeredParsers.containsKey(parser)) {
155
+                removeParser(parser, false);
156
+            }
157
+        }
158
+    }
159
+
160
+    @Override
161
+    public void onDebugInfo(final Parser parser, final Date date, final int level, final String data) {
162
+        final DebugWindow window = registeredParsers.get(parser);
163
+        if (window != null) {
164
+            window.addLine(String.format("[%d] %s%n", level, data), true);
165
+        }
166
+    }
167
+
168
+}

+ 32
- 0
src/com/dmdirc/addons/parserdebug/ParserDebugModule.java View File

@@ -0,0 +1,32 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.addons.parserdebug;
24
+
25
+import com.dmdirc.ClientModule;
26
+
27
+import dagger.Module;
28
+
29
+@Module(injects = {ParserDebugCommand.class, ParserDebugManager.class}, addsTo = ClientModule.class)
30
+public class ParserDebugModule {
31
+
32
+}

Loading…
Cancel
Save