Browse Source

Migrate ParserDebug plugin.

Change-Id: I661cc835878a13701363e9911b4f4cc23c83ae24
Reviewed-on: http://gerrit.dmdirc.com/1297
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
tags/0.6.4
Chris Smith 14 years ago
parent
commit
c81cec1b12

+ 112
- 0
src/com/dmdirc/addons/parserdebug/DebugPlugin.java View File

@@ -0,0 +1,112 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.ActionManager;
27
+import com.dmdirc.actions.CoreActionType;
28
+import com.dmdirc.actions.interfaces.ActionType;
29
+import com.dmdirc.commandparser.CommandManager;
30
+import com.dmdirc.interfaces.ActionListener;
31
+import com.dmdirc.parser.interfaces.Parser;
32
+import com.dmdirc.parser.interfaces.callbacks.DebugInfoListener;
33
+import com.dmdirc.plugins.Plugin;
34
+
35
+import java.util.ArrayList;
36
+import java.util.Date;
37
+import java.util.HashMap;
38
+
39
+/**
40
+ * This causes parser debugging to be spammed to the console.
41
+ *
42
+ * @author Shane 'Dataforce' McCormack
43
+ */
44
+public final class DebugPlugin extends Plugin implements DebugInfoListener, ActionListener {
45
+
46
+    /** The ParserDebugCommand we created. */
47
+    private ParserDebugCommand command = null;
48
+
49
+    /** Map of parsers registered. */
50
+    protected final HashMap<Parser,DebugWindow> registeredParsers
51
+            = new HashMap<Parser,DebugWindow>();
52
+
53
+    /**
54
+     * Creates a new instance of the Debug Plugin.
55
+     */
56
+    public DebugPlugin() { super(); }
57
+    
58
+    /** {@inheritDoc} */
59
+    @Override
60
+    public void onLoad() {
61
+        ActionManager.addListener(this, CoreActionType.SERVER_DISCONNECTED);
62
+        command = new ParserDebugCommand(this);
63
+    }
64
+    
65
+    /** {@inheritDoc} */
66
+    @Override
67
+    public void onUnload() {
68
+        CommandManager.unregisterCommand(command);
69
+        final ArrayList<DebugWindow> windowList = new ArrayList<DebugWindow>();
70
+        for (Parser parser : registeredParsers.keySet()) {
71
+            try {
72
+                parser.getCallbackManager().delCallback(DebugInfoListener.class, this);
73
+                DebugWindow window = registeredParsers.get(parser);
74
+                windowList.add(window);
75
+            } catch (Exception e) { }
76
+        }
77
+        for (DebugWindow window : windowList) {
78
+            window.close();
79
+        }
80
+        registeredParsers.clear();
81
+    }
82
+    
83
+    /** {@inheritDoc} */
84
+    @Override
85
+    public void onDebugInfo(final Parser parser, final Date date, final int level, final String data) {
86
+        DebugWindow window = registeredParsers.get(parser);
87
+        if (window != null) {
88
+            window.addLine(String.format("[%d] %s%n", level, data), true);
89
+        }
90
+    }
91
+    
92
+    /** {@inheritDoc} */
93
+    @Override
94
+    public void processEvent(final ActionType type, final StringBuffer format, final Object... arguments) {
95
+        if (type == CoreActionType.SERVER_DISCONNECTED) {
96
+            final Server thisServer = (Server)arguments[0];
97
+            final Parser parser = thisServer.getParser();
98
+            if (registeredParsers.containsKey(parser)) {
99
+                try {
100
+                    DebugWindow window = registeredParsers.get(parser);
101
+                    registeredParsers.remove(parser);
102
+                    window.unsetParser();
103
+                    parser.getCallbackManager().delCallback(DebugInfoListener.class, this);
104
+                    window.addLine("======================", true);
105
+                    window.addLine("No Longer Monitoring: "+parser+" (Server Disconnected)", true);
106
+                    window.addLine("======================", true);
107
+                } catch (Exception e) { }
108
+            }
109
+        }
110
+    }
111
+}
112
+

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

@@ -0,0 +1,115 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.FrameContainer;
26
+import com.dmdirc.Server;
27
+import com.dmdirc.parser.interfaces.Parser;
28
+import com.dmdirc.parser.interfaces.callbacks.DebugInfoListener;
29
+import com.dmdirc.ui.WindowManager;
30
+import com.dmdirc.ui.interfaces.Window;
31
+
32
+/**
33
+ * This class is used to show the parser debug in a window
34
+ *
35
+ * @author Shane 'Dataforce' McCormack
36
+ */
37
+public class DebugWindow extends FrameContainer<Window> {
38
+
39
+    /** The plugin that owns this window */
40
+    protected DebugPlugin plugin;
41
+    /** The parser this window is debugging */
42
+    protected Parser parser;
43
+    /** The Server window we are a child of */
44
+    protected Server server;
45
+    
46
+    /**
47
+     * Creates a new instance of DebugWindow.
48
+     *
49
+     * @param plugin The plugin that owns this window
50
+     * @param title The title of this window
51
+     * @param parser The parser this plugin is debugging
52
+     * @param server The Server window this is a child of
53
+     */
54
+    public DebugWindow(final DebugPlugin plugin, final String title, final Parser parser, final Server server) {
55
+        super("raw", "Parser Debug", title, Window.class, server.getConfigManager());
56
+        this.plugin = plugin;
57
+        this.parser = parser;
58
+        this.server = server;
59
+
60
+        WindowManager.addWindow(server, this);
61
+    }
62
+       
63
+    /**
64
+     * Returns the server instance associated with this container.
65
+     *
66
+     * @return the associated server connection
67
+     */
68
+    @Override
69
+    public Server getServer() {
70
+        return server;
71
+    }
72
+    
73
+    /**
74
+     * Set the parser to null to stop us holding onto parsers when the server
75
+     * connection is closed.
76
+     */
77
+    public void unsetParser() {
78
+        addLine("======================", true);
79
+        addLine("Unset parser: "+parser, true);
80
+        parser = null;
81
+    }
82
+    
83
+
84
+    /**
85
+     * Closes this container (and it's associated frame).
86
+     */
87
+    @Override
88
+    public void windowClosing() {
89
+        // 1: Make the window non-visible
90
+        for (Window window : getWindows()) {
91
+            window.setVisible(false);
92
+        }
93
+        
94
+        // 2: Remove any callbacks or listeners
95
+        if (parser != null) {
96
+            parser.getCallbackManager().delCallback(DebugInfoListener.class, plugin);
97
+        }
98
+        
99
+        // 3: Trigger any actions neccessary
100
+        // 4: Trigger action for the window closing
101
+        // 5: Inform any parents that the window is closing
102
+        
103
+        // 6: Remove the window from the window manager
104
+        WindowManager.removeWindow(this);
105
+    }
106
+
107
+    /** {@inheritDoc} */
108
+    @Override
109
+    public void windowClosed() {
110
+        // 7: Remove any references to the window and parents
111
+        this.parser = null;
112
+        this.server = null;
113
+        this.plugin = null;
114
+    }
115
+}

+ 133
- 0
src/com/dmdirc/addons/parserdebug/ParserDebugCommand.java View File

@@ -0,0 +1,133 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.FrameContainer;
26
+import com.dmdirc.commandparser.CommandArguments;
27
+import com.dmdirc.commandparser.CommandInfo;
28
+import com.dmdirc.commandparser.CommandManager;
29
+import com.dmdirc.commandparser.CommandType;
30
+import com.dmdirc.commandparser.commands.Command;
31
+import com.dmdirc.commandparser.commands.CommandOptions;
32
+import com.dmdirc.commandparser.commands.context.CommandContext;
33
+import com.dmdirc.commandparser.commands.context.ServerCommandContext;
34
+import com.dmdirc.parser.interfaces.Parser;
35
+import com.dmdirc.parser.interfaces.callbacks.DebugInfoListener;
36
+
37
+/**
38
+ * The ParserDebug Command allows controlling of which parsers spam debug info.
39
+ *
40
+ * @author Shane "Dataforce" Mc Cormack
41
+ */
42
+@CommandOptions(allowOffline=false)
43
+public final class ParserDebugCommand extends Command implements CommandInfo {
44
+    /** My Plugin */
45
+    final DebugPlugin myPlugin;
46
+
47
+    /**
48
+     * Creates a new instance of ParserDebugCommand.
49
+     *
50
+     * @param plugin Plugin that owns this command
51
+     */
52
+    public ParserDebugCommand(final DebugPlugin plugin) {
53
+        super();
54
+        myPlugin = plugin;
55
+        CommandManager.registerCommand(this);
56
+    }
57
+    
58
+    /** {@inheritDoc} */
59
+    @Override
60
+    public CommandType getType() {
61
+        return CommandType.TYPE_SERVER;
62
+    }
63
+
64
+    /**
65
+     * Executes this command.
66
+     *
67
+     * @param origin The framecontainer in which this command was issued
68
+     * @param commandArgs The user supplied arguments
69
+     * @param context The Context of this command execution
70
+     */
71
+    @Override
72
+    public void execute(final FrameContainer<?> origin, final CommandArguments commandArgs, final CommandContext context) {
73
+        final String[] args = commandArgs.getArguments();
74
+        final boolean isSilent = commandArgs.isSilent();
75
+  
76
+        Parser parser = ((ServerCommandContext) context).getServer().getParser();
77
+        
78
+        if (parser == null) {
79
+            sendLine(origin, isSilent, FORMAT_ERROR, "Unable to get a parser for this window.");
80
+            return;
81
+        }
82
+        if (myPlugin.registeredParsers.containsKey(parser)) {
83
+            try {
84
+                parser.getCallbackManager().delCallback(DebugInfoListener.class, myPlugin);
85
+                DebugWindow window = myPlugin.registeredParsers.get(parser);
86
+                window.addLine("======================", true);
87
+                window.addLine("No Longer Monitoring: "+parser+" (User Requested)", true);
88
+                window.addLine("======================", true);
89
+                myPlugin.registeredParsers.remove(parser);
90
+                sendLine(origin, isSilent, FORMAT_OUTPUT, "Removing callback ok");
91
+            } catch (Exception e) {
92
+                sendLine(origin, isSilent, FORMAT_ERROR, "Removing callback failed");
93
+            }
94
+        } else {
95
+            try {
96
+                parser.getCallbackManager().addCallback(DebugInfoListener.class, myPlugin);
97
+                DebugWindow window = new DebugWindow(myPlugin, "Parser Debug", parser, origin.getServer());
98
+                myPlugin.registeredParsers.put(parser, window);
99
+                sendLine(origin, isSilent, FORMAT_OUTPUT, "Adding callback ok");
100
+                window.addLine("======================", true);
101
+                window.addLine("Started Monitoring: "+parser, true);
102
+                window.addLine("======================", true);
103
+            } catch (Exception e) {
104
+                sendLine(origin, isSilent, FORMAT_ERROR, "Adding callback failed");
105
+            }
106
+        }
107
+    }
108
+
109
+    /**
110
+     * Returns this command's name.
111
+     *
112
+     * @return The name of this command
113
+     */
114
+    @Override
115
+    public String getName() { return "parserdebug"; }
116
+    
117
+    /**
118
+     * Returns whether or not this command should be shown in help messages.
119
+     *
120
+     * @return True iff the command should be shown, false otherwise
121
+     */
122
+    @Override
123
+    public boolean showInHelp() { return true; }
124
+    
125
+    /**
126
+     * Returns a string representing the help message for this command.
127
+     *
128
+     * @return the help message for this command
129
+     */
130
+    @Override
131
+    public String getHelp() { return "parserdebug - Enables/Disables hooks for onDebugInfo for the parser that owns this window"; }
132
+}
133
+

+ 17
- 0
src/com/dmdirc/addons/parserdebug/plugin.config View File

@@ -0,0 +1,17 @@
1
+keysections:
2
+  metadata
3
+  version
4
+
5
+metadata:
6
+  mainclass=com.dmdirc.addons.parserdebug.DebugPlugin
7
+  author=Shane <shane@dmdirc.com>
8
+  description=Allows viewing of parser debugging. (Hacky, likely to break stuff!)
9
+  name=parserdebug
10
+  nicename=Parser Debug Plugin
11
+
12
+version:
13
+  number=3
14
+  friendly=0.3
15
+
16
+provides:
17
+  parserdebug command

Loading…
Cancel
Save