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.

CallbackManager.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2006-2015 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.parser.common;
  23. import com.dmdirc.parser.events.ParserErrorEvent;
  24. import com.dmdirc.parser.events.ParserEvent;
  25. import com.dmdirc.parser.interfaces.Parser;
  26. import com.google.common.base.Throwables;
  27. import java.util.Date;
  28. import net.engio.mbassy.bus.MBassador;
  29. import net.engio.mbassy.bus.config.BusConfiguration;
  30. import net.engio.mbassy.bus.config.Feature;
  31. /**
  32. * Parser Callback Manager.
  33. * Manages adding/removing/calling callbacks.
  34. */
  35. public class CallbackManager extends MBassador<ParserEvent> {
  36. private final Object errorHandlerLock = new Object();
  37. private final Parser parser;
  38. public CallbackManager(final Parser parser) {
  39. super(new BusConfiguration().addFeature(Feature.SyncPubSub.Default())
  40. .addFeature(Feature.AsynchronousHandlerInvocation.Default(1, 1)).addFeature(
  41. Feature.AsynchronousMessageDispatch.Default()
  42. .setNumberOfMessageDispatchers(1)));
  43. this.parser = parser;
  44. setupErrorHandler();
  45. }
  46. @SuppressWarnings("TypeMayBeWeakened")
  47. public CallbackManager(final BusConfiguration configuration, final Parser parser) {
  48. super(configuration);
  49. setupErrorHandler();
  50. this.parser = parser;
  51. }
  52. @Override
  53. public void publish(final ParserEvent message) {
  54. // TODO: HACKY REMOVE
  55. if (message.getParser() == null) {
  56. message.setParser(parser);
  57. }
  58. if (message.getDate() == null) {
  59. message.setDate(new Date());
  60. }
  61. // TODO: HACKY REMOVE
  62. super.publish(message);
  63. }
  64. @SuppressWarnings({
  65. "ThrowableResultOfMethodCallIgnored",
  66. "CallToPrintStackTrace",
  67. "UseOfSystemOutOrSystemErr"
  68. })
  69. private void setupErrorHandler() {
  70. addErrorHandler(e -> {
  71. if (Thread.holdsLock(errorHandlerLock)) {
  72. // ABORT ABORT ABORT - we're publishing an error on the same thread we just tried
  73. // to publish an error on. Something in the error reporting pipeline must be
  74. // breaking, so don't try adding any more errors.
  75. System.err.println("ERROR: Error when reporting error");
  76. e.getCause().printStackTrace();
  77. return;
  78. }
  79. synchronized (errorHandlerLock) {
  80. final Throwable error = e.getCause().getCause();
  81. publish(new ParserErrorEvent(parser, new Date(),
  82. new ParserError(ParserError.ERROR_EXCEPTION,
  83. error.getMessage(), Throwables.getStackTraceAsString(error))));
  84. }
  85. });
  86. }
  87. }