001package edu.pdx.cs410J.grader.poa;
002
003import com.google.common.eventbus.EventBus;
004import com.google.common.eventbus.Subscribe;
005import com.google.common.eventbus.SubscriberExceptionContext;
006import com.google.common.eventbus.SubscriberExceptionHandler;
007
008public class EventBusThatPublishesUnhandledExceptionEvents extends EventBus {
009
010  /** We have to use this hokey second event bus because the
011   * SubscriberExceptionHandler (which posts the UnhandledExceptionEvent)
012   * must be created in the constructor before the bus, itself, is
013   * fully initialized and available for use. */
014  private static final EventBus errorMessageBus = new EventBus("Error Messages");
015
016  public EventBusThatPublishesUnhandledExceptionEvents() {
017    super(new SubscriberExceptionHandler() {
018      @Override
019      public void handleException(Throwable throwable, SubscriberExceptionContext subscriberExceptionContext) {
020        postUncaughtExceptionEvent(throwable);
021      }
022    });
023
024    errorMessageBus.register(this);
025  }
026
027  @Subscribe
028  public void postUncaughtExceptionEventToErrorMessageBus(UnhandledExceptionEvent event) {
029    this.post(event);
030  }
031
032  private static void postUncaughtExceptionEvent(Throwable throwable) {
033    errorMessageBus.post(new UnhandledExceptionEvent(throwable));
034  }
035}