
package de.ivu.elect.jee.background;

import java.io.Serializable;

import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
@NotifyOnSuccess
public class NotifyOnSuccessInterceptor implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient BeanManager beanManager;

  @AroundInvoke
  @SuppressWarnings({"rawtypes", "unchecked"})
  public Object secure(InvocationContext ctx) throws Exception {
    Object result = ctx.proceed();
    beanManager.fireEvent(ctx.getMethod().getAnnotation(NotifyOnSuccess.class).value().newInstance());
    return result;
  }
}