Created
March 25, 2014 12:22
-
-
Save mpellegrini/9760787 to your computer and use it in GitHub Desktop.
An example CDI Extension that observes all events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.enterprise.event.Observes; | |
import javax.enterprise.inject.spi.AfterBeanDiscovery; | |
import javax.enterprise.inject.spi.AfterDeploymentValidation; | |
import javax.enterprise.inject.spi.BeforeBeanDiscovery; | |
import javax.enterprise.inject.spi.BeforeShutdown; | |
import javax.enterprise.inject.spi.Extension; | |
import javax.enterprise.inject.spi.ProcessAnnotatedType; | |
import javax.enterprise.inject.spi.ProcessBean; | |
import javax.enterprise.inject.spi.ProcessInjectionTarget; | |
import javax.enterprise.inject.spi.ProcessProducer; | |
/** | |
* | |
*/ | |
@SuppressWarnings("CdiManagedBeanInconsistencyInspection") | |
public class DebugExtension implements Extension { | |
private static final Logger LOGGER = LoggerFactory.getLogger(DebugExtension.class); | |
public DebugExtension() { | |
} | |
/** | |
* The container must fire an event before it begins the bean discovery process. | |
* | |
* @param bbd {@code BeforeBeanDiscovery} CDI Event | |
*/ | |
@SuppressWarnings("UnusedDeclaration") | |
private void beforeBeanDiscovery(@Observes final BeforeBeanDiscovery bbd) { | |
LOGGER.info("BeforeBeanDiscovery"); | |
} | |
/** | |
* The container must fire a second event when it has fully completed the | |
* bean discovery process, validated that there are no definition errors | |
* relating to the discovered beans, and registered Bean and ObserverMethod | |
* objects for the discovered beans, but before detecting deployment | |
* problems. | |
* | |
* @param abd {@code AfterBeanDiscovery} CDI Event | |
*/ | |
@SuppressWarnings("UnusedDeclaration") | |
private void afterBeanDiscovery(@Observes final AfterBeanDiscovery abd) { | |
LOGGER.info("AfterBeanDiscovery"); | |
} | |
/** | |
* The container must fire a third event after it has validated that there | |
* are no deployment problems and before creating contexts or processing | |
* requests. | |
* | |
* @param adb {@code AfterDeploymentValidation} CDI Event | |
*/ | |
@SuppressWarnings("UnusedDeclaration") | |
private void afterDeploymentValidation(@Observes final AfterDeploymentValidation adb) { | |
LOGGER.info("AfterDeploymentValidation"); | |
} | |
/** | |
* The container must fire a final event after it has finished processing | |
* requests and destroyed all contexts. | |
* | |
* @param bs {@code BeforeShutdown} CDI Event | |
*/ | |
@SuppressWarnings("UnusedDeclaration") | |
private void beforeShutdown(@Observes final BeforeShutdown bs) { | |
LOGGER.info("BeforeShutdown"); | |
} | |
/** | |
* The container must fire an event for each Java class or interface it | |
* discovers in a bean archive, before it reads the declared annotations. | |
* | |
* @param pat {@code ProcessAnnotatedType} CDI Event | |
* @param <T> A {@code Class} | |
*/ | |
@SuppressWarnings("UnusedDeclaration") | |
private <T> void processAnnotatedType(@Observes final ProcessAnnotatedType<T> pat) { | |
LOGGER.info("processAnnotatedType " + pat.getAnnotatedType().getJavaClass().getSimpleName()); | |
} | |
/** | |
* The container must fire an event for every Java EE component class | |
* supporting injection that may be instantiated by the container at | |
* runtime, including every managed bean declared using @ManagedBean, EJB | |
* session or message-driven bean, enabled bean, enabled interceptor or | |
* enabled decorator. | |
* | |
* @param pit {@code ProcessInjectionTarget} CDI Event | |
* @param <T> the managed bean class, session bean class or Java EE component class supporting injection. | |
*/ | |
@SuppressWarnings("UnusedDeclaration") | |
private <T> void processInjectionTarget(@Observes final ProcessInjectionTarget<T> pit) { | |
LOGGER.info("ProcessInjectionTarget " + pit.getAnnotatedType().getJavaClass().getSimpleName()); | |
} | |
/** | |
* The container must fire an event for each producer method or field of | |
* each enabled bean, including resources | |
* | |
* @param pp {@code ProcessProducer} CDI Event | |
* @param <T> The bean class of the bean that declares the producer method or field | |
* @param <X> The return type of the producer method or the type of the producer field. | |
*/ | |
@SuppressWarnings("UnusedDeclaration") | |
private <T, X> void processProducer(@Observes final ProcessProducer<T, X> pp) { | |
LOGGER.info("ProcessProducer " + pp.getAnnotatedMember().getDeclaringType().getJavaClass().getSimpleName()); | |
} | |
/** | |
* The container must fire an event for each enabled bean, interceptor or | |
* decorator deployed in a bean archive, before registering the Bean | |
* object. No event is fired for any @New qualified bean, defined in | |
* Section 3.12, “@New qualified beans”. | |
* | |
* @param pb {@code ProcessBean} CDI Event | |
* @param <X> | |
*/ | |
@SuppressWarnings("UnusedDeclaration") | |
private <X> void processBean(@Observes final ProcessBean<X> pb) { | |
LOGGER.info("ProcessBean " + pb.getBean().getBeanClass().getSimpleName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment