Guest User

MessageBundleExtension

a guest
May 10th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.10 KB | None | 0 0
  1.  
  2. import java.lang.reflect.Method;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Collection;
  6. import java.util.HashSet;
  7. import java.util.List;
  8.  
  9. import javax.enterprise.event.Observes;
  10. import javax.enterprise.inject.spi.AfterBeanDiscovery;
  11. import javax.enterprise.inject.spi.AfterDeploymentValidation;
  12. import javax.enterprise.inject.spi.AnnotatedType;
  13. import javax.enterprise.inject.spi.Bean;
  14. import javax.enterprise.inject.spi.BeanManager;
  15. import javax.enterprise.inject.spi.Extension;
  16. import javax.enterprise.inject.spi.ProcessAnnotatedType;
  17.  
  18. import org.apache.deltaspike.core.api.message.Message;
  19. import org.apache.deltaspike.core.api.message.MessageContext;
  20. import org.apache.deltaspike.core.api.message.annotation.MessageBundle;
  21. import org.apache.deltaspike.core.api.message.annotation.MessageTemplate;
  22. import org.apache.deltaspike.core.util.bean.BeanBuilder;
  23.  
  24. public class MessageBundleExtension implements Extension {
  25.     private final Collection<AnnotatedType<?>> messageBundleTypes = new HashSet<AnnotatedType<?>>();
  26.     private final List<String> deploymentErrors = new ArrayList<String>();
  27.  
  28.     @SuppressWarnings("rawtypes")
  29.     protected void detectInterfaces(
  30.             @Observes ProcessAnnotatedType processAnnotatedType) {
  31.         AnnotatedType<?> type = processAnnotatedType.getAnnotatedType();
  32.  
  33.         if (type.isAnnotationPresent(MessageBundle.class)) {
  34.             if (validateMessageBundle(type.getJavaClass())) {
  35.                 messageBundleTypes.add(type);
  36.             }
  37.         }
  38.     }
  39.  
  40.     /**
  41.      * @return <code>true</code> if all is well
  42.      */
  43.     private boolean validateMessageBundle(Class<?> currentClass) {
  44.         boolean ok = true;
  45.  
  46.         // sanity check: annotated class must be an Interface
  47.         if (!currentClass.isInterface()) {
  48.             deploymentErrors
  49.                     .add("@MessageBundle must only be used on Interfaces, but got used on class "
  50.                             + currentClass.getName());
  51.             return false;
  52.         }
  53.  
  54.         for (Method currentMethod : currentClass.getDeclaredMethods()) {
  55.             if (!currentMethod.isAnnotationPresent(MessageTemplate.class)) {
  56.                 continue;
  57.             }
  58.  
  59.             if (String.class.isAssignableFrom(currentMethod.getReturnType())) {
  60.                 continue;
  61.             }
  62.  
  63.             if (Message.class.isAssignableFrom(currentMethod.getReturnType())) {
  64.                 ok |= validateMessageContextAwareMethod(currentMethod);
  65.             } else {
  66.                 deploymentErrors.add(currentMethod.getReturnType().getName()
  67.                         + " isn't supported. Details: "
  68.                         + currentMethod.getDeclaringClass().getName() + "#"
  69.                         + currentMethod.getName() + " only "
  70.                         + String.class.getName() + " or "
  71.                         + Message.class.getName());
  72.                 ok = false;
  73.             }
  74.         }
  75.  
  76.         return ok;
  77.     }
  78.  
  79.     private boolean validateMessageContextAwareMethod(Method currentMethod) {
  80.         for (Class<?> currentParameterType : currentMethod.getParameterTypes()) {
  81.             if (MessageContext.class.isAssignableFrom(currentParameterType)) {
  82.                 return true;
  83.             }
  84.         }
  85.  
  86.         deploymentErrors.add("No " + MessageContext.class.getName()
  87.                 + " parameter found at: "
  88.                 + currentMethod.getDeclaringClass().getName() + "#"
  89.                 + currentMethod.getName()
  90.                 + ". That is required for return-type "
  91.                 + Message.class.getName());
  92.         return false;
  93.     }
  94.  
  95.     protected void installMessageBundleProducerBeans(
  96.             @Observes AfterBeanDiscovery abd, BeanManager beanManager) {
  97.         if (!deploymentErrors.isEmpty()) {
  98.             abd.addDefinitionError(new IllegalArgumentException(
  99.                     "The following MessageBundle problems where found: "
  100.                             + Arrays.toString(deploymentErrors.toArray())));
  101.             return;
  102.         }
  103.  
  104.         for (AnnotatedType<?> type : messageBundleTypes) {
  105.             abd.addBean(createMessageBundleBean(type, beanManager));
  106.         }
  107.     }
  108.  
  109.     private static <T> Bean<T> createMessageBundleBean(
  110.             AnnotatedType<T> annotatedType, BeanManager beanManager) {
  111.  
  112.         Class<T> beanClass = annotatedType.getJavaClass();
  113.  
  114.         BeanBuilder<T> beanBuilder = new BeanBuilder<T>(beanManager)
  115.                 .readFromType(annotatedType)
  116.                 .passivationCapable(true)
  117.                 .beanLifecycle(new MessageBundleBeanLifecycle<T>(beanClass));
  118.  
  119.         return beanBuilder.create();
  120.     }
  121.  
  122.     protected void cleanup(
  123.             @Observes AfterDeploymentValidation afterDeploymentValidation) {
  124.         messageBundleTypes.clear();
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment