package org.fenixedu.bennu.signals;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.SubscriberExceptionContext;
import com.google.common.eventbus.SubscriberExceptionHandler;

class SignalEventBus extends EventBus {

    private SignalLoggingSubscriberExceptionHandler exceptionHandler;
    private boolean throwsExceptions = false;

    private SignalEventBus(SignalLoggingSubscriberExceptionHandler handler) {
        super(handler);
        exceptionHandler = handler;
    }

    public SignalEventBus(String identifier) {
        this(new SignalLoggingSubscriberExceptionHandler(identifier));
    }

    public SignalEventBus(String identifier, boolean throwsExceptions) {
        this(new SignalLoggingSubscriberExceptionHandler(identifier));
        this.throwsExceptions = throwsExceptions;
    }

    public void emit(Object event) throws RuntimeException {
        super.post(event);
        Throwable e = exceptionHandler.getException();
        if (e != null) {
            exceptionHandler.clear();
            if (throwsExceptions) {
                throw new RuntimeException("Exceptions while emiting signals", e);
            }
        }
    }

    private static final class SignalLoggingSubscriberExceptionHandler implements SubscriberExceptionHandler {

        private final Logger logger;
        private final String identifier;

        public Throwable exception;

        public SignalLoggingSubscriberExceptionHandler(String identifier) {
            logger = Logger.getLogger(SignalEventBus.class.getName() + "." + checkNotNull(identifier));
            this.identifier = identifier;
        }

        @Override
        public void handleException(Throwable exception, SubscriberExceptionContext context) {
            logger.log(Level.SEVERE,
                    "Could not emit signal: " + context.getSubscriber() + " to " + context.getSubscriberMethod(),
                    exception.getCause());
            this.exception = exception;
        }

        public Throwable getException() {
            return exception;
        }

        public void clear() {
            exception = null;
        }
    }

}

@@ -41,130 +41,56 @@
 ort 
-static com.google.common.base.Preconditions.checkNotNull;%0A%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger
+org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0Ai
@@ -306,24 +306,119 @@
     private 
+static final Logger logger = LoggerFactory.getLogger(SignalEventBus.class);%0A%0A    private final 
 SignalLoggin
@@ -1453,45 +1453,8 @@
  %7B%0A%0A
-        private final Logger logger;%0A
     
@@ -1490,16 +1490,16 @@
 tifier;%0A
+
 %0A       
@@ -1608,112 +1608,8 @@
 ) %7B%0A
-            logger = Logger.getLogger(SignalEventBus.class.getName() + %22.%22 + checkNotNull(identifier));%0A
     
@@ -1793,46 +1793,14 @@
 ger.
-log(Level.SEVERE,%0A                    
+error(
 %22Cou
@@ -1887,17 +1887,22 @@
 Method()
-,
+ + %22(%22
 %0A       
@@ -1918,28 +1918,37 @@
     
-exception.getCause()
++ identifier + %22)%22, exception
 );%0A 

Use the correct logger on the event bus