1 package net.avcompris.commons3.notifier.impl; 2 3 import static com.google.common.base.Preconditions.checkNotNull; 4 5 import javax.annotation.Nullable; 6 7 import org.apache.commons.lang3.NotImplementedException; 8 9 import net.avcompris.commons3.api.User; 10 import net.avcompris.commons3.api.exception.ServiceException; 11 import net.avcompris.commons3.notifier.utils.NotificationUtils; 12 import net.avcompris.commons3.triggered.ErrorTriggeredAction; 13 import net.avcompris.commons3.triggered.impl.AbstractErrorTriggeredImpl; 14 15 public final class ErrorTriggeredInMemory extends AbstractErrorTriggeredImpl { 16 17 private final String name; 18 19 @Nullable 20 private User threadUser; 21 22 public ErrorTriggeredInMemory(final String name) { 23 24 checkNotNull(name, "name"); 25 26 this.name = name; 27 28 ErrorNotifierInMemory.getInstance().register(this); 29 } 30 31 public ErrorTriggeredInMemory(final Class<?> clazz) { 32 33 this(checkNotNull(clazz, "clazz").getSimpleName()); 34 } 35 36 @Override 37 public String getName() { 38 39 return name; 40 } 41 42 void trigger(@Nullable final String correlationId, @Nullable final String username, final Throwable throwable) 43 throws ServiceException { 44 45 checkNotNull(throwable, "throwable"); 46 47 final User threadUserCopy = threadUser; 48 49 if (threadUserCopy != null && !threadUserCopy.getUsername().contentEquals(username)) { 50 51 throw new NotImplementedException("user: " + username + ", threadUser: " + threadUserCopy.getUsername()); 52 } 53 54 final ErrorTriggeredAction action = getAction(); 55 56 if (action == null) { 57 58 return; 59 } 60 61 action.execute(correlationId, username, NotificationUtils.toErrorNotification(throwable)); 62 } 63 64 @Override 65 @Nullable 66 public User getThreadUser() { 67 68 return threadUser; 69 } 70 71 @Override 72 public void setThreadUser(@Nullable final String correlationId, @Nullable final User user) { 73 74 threadUser = user; 75 } 76 }