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.

ParserDebugManager.java 5.9KB

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