You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DebugPlugin.java 4.4KB

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