You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by st...@apache.org on 2012/06/15 10:09:11 UTC

[1/2] git commit: DELTASPIKE-200 handle MessageBundle problems as DefinitionError

Updated Branches:
  refs/heads/master 04f7d1736 -> 01d4f0cdf


DELTASPIKE-200 handle MessageBundle problems as DefinitionError


Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/01d4f0cd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/01d4f0cd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/01d4f0cd

Branch: refs/heads/master
Commit: 01d4f0cdf356f7b7159f518872dfe6d117cf3456
Parents: 57c9f71
Author: Mark Struberg <st...@apache.org>
Authored: Fri Jun 15 10:03:00 2012 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Fri Jun 15 10:03:00 2012 +0200

----------------------------------------------------------------------
 .../core/impl/message/MessageBundleExtension.java  |   49 ++++++++++----
 .../broken/BrokenMessageBundleOnClassTest.java     |    5 +-
 2 files changed, 39 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/01d4f0cd/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/message/MessageBundleExtension.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/message/MessageBundleExtension.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/message/MessageBundleExtension.java
index ffae84c..11d3bd5 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/message/MessageBundleExtension.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/message/MessageBundleExtension.java
@@ -19,8 +19,11 @@
 package org.apache.deltaspike.core.impl.message;
 
 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;
@@ -52,6 +55,8 @@ public class MessageBundleExtension implements Extension, Deactivatable
     private final Collection<AnnotatedType<?>> messageBundleTypes = new HashSet<AnnotatedType<?>>();
     private Bean<Object> bundleProducerBean;
 
+    private List<String> deploymentErrors = new ArrayList<String>();
+
     private Boolean isActivated = null;
 
     @SuppressWarnings("UnusedDeclaration")
@@ -72,19 +77,26 @@ public class MessageBundleExtension implements Extension, Deactivatable
 
         if (type.isAnnotationPresent(MessageBundle.class))
         {
-            validateMessageBundle(type.getJavaClass());
-
-            messageBundleTypes.add(type);
+            if (validateMessageBundle(type.getJavaClass()))
+            {
+                messageBundleTypes.add(type);
+            }
         }
     }
 
-    private void validateMessageBundle(Class<?> currentClass)
+    /**
+     * @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())
         {
-            throw new IllegalStateException("@MessageBundle must only be used on Interfaces, but got used on class " +
+            deploymentErrors.add("@MessageBundle must only be used on Interfaces, but got used on class " +
                     currentClass.getName());
+            return false;
         }
 
         for (Method currentMethod : currentClass.getDeclaredMethods())
@@ -101,31 +113,34 @@ public class MessageBundleExtension implements Extension, Deactivatable
 
             if (Message.class.isAssignableFrom(currentMethod.getReturnType()))
             {
-                validateMessageContextAwareMethod(currentMethod);
+                ok |= validateMessageContextAwareMethod(currentMethod);
             }
             else
             {
-                throw new IllegalStateException(
-                        currentMethod.getReturnType().getName() + " isn't supported. Details: " +
+                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 void validateMessageContextAwareMethod(Method currentMethod)
+    private boolean validateMessageContextAwareMethod(Method currentMethod)
     {
         for (Class currentParameterType : currentMethod.getParameterTypes())
         {
             if (MessageContext.class.isAssignableFrom(currentParameterType))
             {
-                return;
+                return true;
             }
         }
 
-        throw new IllegalStateException("No " + MessageContext.class.getName() + " parameter found at: " +
+        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;
     }
 
     /**
@@ -181,11 +196,17 @@ public class MessageBundleExtension implements Extension, Deactivatable
     }
 
     @SuppressWarnings("UnusedDeclaration")
-    protected void installMessageBundleProducerBeans(@Observes AfterBeanDiscovery event, BeanManager beanManager)
+    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())));
+        }
+
         for (AnnotatedType<?> type : messageBundleTypes)
         {
-            event.addBean(createMessageBundleBean(bundleProducerBean, type, beanManager));
+            abd.addBean(createMessageBundleBean(bundleProducerBean, type, beanManager));
         }
     }
 
@@ -201,7 +222,7 @@ public class MessageBundleExtension implements Extension, Deactivatable
     }
 
     @SuppressWarnings("UnusedDeclaration")
-    protected void cleanup(@Observes AfterDeploymentValidation event)
+    protected void cleanup(@Observes AfterDeploymentValidation afterDeploymentValidation)
     {
         messageBundleTypes.clear();
     }

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/01d4f0cd/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleOnClassTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleOnClassTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleOnClassTest.java
index 435f21f..66cbfe2 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleOnClassTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleOnClassTest.java
@@ -26,11 +26,14 @@ import org.apache.deltaspike.test.category.SeCategory;
 import org.apache.deltaspike.test.util.ArchiveUtils;
 import org.jboss.arquillian.container.test.api.Deployment;
 import org.jboss.arquillian.container.test.api.ShouldThrowException;
+//X import org.jboss.arquillian.junit.Arquillian;
 import org.jboss.shrinkwrap.api.ShrinkWrap;
 import org.jboss.shrinkwrap.api.asset.EmptyAsset;
 import org.jboss.shrinkwrap.api.spec.JavaArchive;
 import org.jboss.shrinkwrap.api.spec.WebArchive;
 import org.junit.experimental.categories.Category;
+//X import org.junit.Test;
+//X import org.junit.runner.RunWith;
 
 import static org.junit.Assert.assertEquals;
 
@@ -72,7 +75,7 @@ public class BrokenMessageBundleOnClassTest
                         MessageBundleExtension.class);
     }
 
-    //X @Test
+    //X     @Test
     public void testSimpleMessage()
     {
         assertEquals("Welcome to DeltaSpike", messageBundle.welcomeToDeltaSpike());