Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.HashSet;
- import java.util.List;
- import javax.enterprise.event.Observes;
- import javax.enterprise.inject.spi.AfterBeanDiscovery;
- import javax.enterprise.inject.spi.AfterDeploymentValidation;
- import javax.enterprise.inject.spi.AnnotatedType;
- import javax.enterprise.inject.spi.Bean;
- import javax.enterprise.inject.spi.BeanManager;
- import javax.enterprise.inject.spi.Extension;
- import javax.enterprise.inject.spi.ProcessAnnotatedType;
- import org.apache.deltaspike.core.api.message.Message;
- import org.apache.deltaspike.core.api.message.MessageContext;
- import org.apache.deltaspike.core.api.message.annotation.MessageBundle;
- import org.apache.deltaspike.core.api.message.annotation.MessageTemplate;
- import org.apache.deltaspike.core.util.bean.BeanBuilder;
- public class MessageBundleExtension implements Extension {
- private final Collection<AnnotatedType<?>> messageBundleTypes = new HashSet<AnnotatedType<?>>();
- private final List<String> deploymentErrors = new ArrayList<String>();
- @SuppressWarnings("rawtypes")
- protected void detectInterfaces(
- @Observes ProcessAnnotatedType processAnnotatedType) {
- AnnotatedType<?> type = processAnnotatedType.getAnnotatedType();
- if (type.isAnnotationPresent(MessageBundle.class)) {
- if (validateMessageBundle(type.getJavaClass())) {
- messageBundleTypes.add(type);
- }
- }
- }
- /**
- * @return <code>true</code> if all is well
- */
- private boolean validateMessageBundle(Class<?> currentClass) {
- boolean ok = true;
- // sanity check: annotated class must be an Interface
- if (!currentClass.isInterface()) {
- deploymentErrors
- .add("@MessageBundle must only be used on Interfaces, but got used on class "
- + currentClass.getName());
- return false;
- }
- for (Method currentMethod : currentClass.getDeclaredMethods()) {
- if (!currentMethod.isAnnotationPresent(MessageTemplate.class)) {
- continue;
- }
- if (String.class.isAssignableFrom(currentMethod.getReturnType())) {
- continue;
- }
- if (Message.class.isAssignableFrom(currentMethod.getReturnType())) {
- ok |= validateMessageContextAwareMethod(currentMethod);
- } else {
- deploymentErrors.add(currentMethod.getReturnType().getName()
- + " isn't supported. Details: "
- + currentMethod.getDeclaringClass().getName() + "#"
- + currentMethod.getName() + " only "
- + String.class.getName() + " or "
- + Message.class.getName());
- ok = false;
- }
- }
- return ok;
- }
- private boolean validateMessageContextAwareMethod(Method currentMethod) {
- for (Class<?> currentParameterType : currentMethod.getParameterTypes()) {
- if (MessageContext.class.isAssignableFrom(currentParameterType)) {
- return true;
- }
- }
- deploymentErrors.add("No " + MessageContext.class.getName()
- + " parameter found at: "
- + currentMethod.getDeclaringClass().getName() + "#"
- + currentMethod.getName()
- + ". That is required for return-type "
- + Message.class.getName());
- return false;
- }
- protected void installMessageBundleProducerBeans(
- @Observes AfterBeanDiscovery abd, BeanManager beanManager) {
- if (!deploymentErrors.isEmpty()) {
- abd.addDefinitionError(new IllegalArgumentException(
- "The following MessageBundle problems where found: "
- + Arrays.toString(deploymentErrors.toArray())));
- return;
- }
- for (AnnotatedType<?> type : messageBundleTypes) {
- abd.addBean(createMessageBundleBean(type, beanManager));
- }
- }
- private static <T> Bean<T> createMessageBundleBean(
- AnnotatedType<T> annotatedType, BeanManager beanManager) {
- Class<T> beanClass = annotatedType.getJavaClass();
- BeanBuilder<T> beanBuilder = new BeanBuilder<T>(beanManager)
- .readFromType(annotatedType)
- .passivationCapable(true)
- .beanLifecycle(new MessageBundleBeanLifecycle<T>(beanClass));
- return beanBuilder.create();
- }
- protected void cleanup(
- @Observes AfterDeploymentValidation afterDeploymentValidation) {
- messageBundleTypes.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment