You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2013/06/12 20:43:59 UTC

git commit: DELTASPIKE-380 improved error-handling

Updated Branches:
  refs/heads/master 6d79d1fcd -> 776bfcff0


DELTASPIKE-380 improved error-handling


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

Branch: refs/heads/master
Commit: 776bfcff0e36b1d8338a12f3144885e430296566
Parents: 6d79d1f
Author: gpetracek <gp...@apache.org>
Authored: Wed Jun 12 20:41:37 2013 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Wed Jun 12 20:41:37 2013 +0200

----------------------------------------------------------------------
 .../impl/PartialBeanBindingExtension.java       | 37 +++++++++++---------
 .../partialbean/impl/PartialBeanLifecycle.java  | 21 ++++++++++-
 2 files changed, 40 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/776bfcff/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/PartialBeanBindingExtension.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/PartialBeanBindingExtension.java b/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/PartialBeanBindingExtension.java
index 140a819..18973a2 100644
--- a/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/PartialBeanBindingExtension.java
+++ b/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/PartialBeanBindingExtension.java
@@ -37,7 +37,6 @@ import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Modifier;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Set;
 import java.util.logging.Logger;
 
 public class PartialBeanBindingExtension implements Extension, Deactivatable
@@ -78,8 +77,7 @@ public class PartialBeanBindingExtension implements Extension, Deactivatable
         }
         else if (InvocationHandler.class.isAssignableFrom(beanClass))
         {
-            validateInvocationHandler(
-                    beanClass, bindingAnnotationClass, pat.getAnnotatedType().getAnnotations(), beanManager);
+            validateInvocationHandler(beanClass, bindingAnnotationClass);
 
             this.partialBeanHandlers.put(bindingAnnotationClass, (Class<? extends InvocationHandler>) beanClass);
         }
@@ -106,20 +104,13 @@ public class PartialBeanBindingExtension implements Extension, Deactivatable
 
         for (Map.Entry<Class<?>, Class<? extends Annotation>> partialBeanEntry : this.partialBeans.entrySet())
         {
-            Bean partialBean = createPartialBean(partialBeanEntry.getKey(), partialBeanEntry.getValue(), beanManager);
+            Bean partialBean = createPartialBean(
+                    partialBeanEntry.getKey(), partialBeanEntry.getValue(), afterBeanDiscovery, beanManager);
 
             if (partialBean != null)
             {
                 afterBeanDiscovery.addBean(partialBean);
             }
-            else
-            {
-                afterBeanDiscovery.addDefinitionError(new IllegalStateException("A class which implements " +
-                        InvocationHandler.class.getName() + " and is annotated with @" +
-                        partialBeanEntry.getValue().getName() + " is needed as a handler for " +
-                        partialBeanEntry.getKey().getName() + ". See the documentation about @" +
-                        PartialBeanBinding.class.getName() + "."));
-            }
         }
 
         this.partialBeans.clear();
@@ -128,21 +119,35 @@ public class PartialBeanBindingExtension implements Extension, Deactivatable
 
     protected <T> Bean<T> createPartialBean(Class<T> beanClass,
                                             Class<? extends Annotation> bindingAnnotationClass,
-                                            BeanManager beanManager)
+                                            AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager)
     {
         Class<? extends InvocationHandler> invocationHandlerClass = partialBeanHandlers.get(bindingAnnotationClass);
 
         if (invocationHandlerClass == null)
         {
+            afterBeanDiscovery.addDefinitionError(new IllegalStateException("A class which implements " +
+                    InvocationHandler.class.getName() + " and is annotated with @" +
+                    bindingAnnotationClass.getName() + " is needed as a handler for " +
+                    beanClass.getName() + ". See the documentation about @" +
+                    PartialBeanBinding.class.getName() + "."));
+
             return null;
         }
 
         AnnotatedType<T> annotatedType = new AnnotatedTypeBuilder<T>().readFromType(beanClass).create();
 
+        PartialBeanLifecycle beanLifecycle =
+                new PartialBeanLifecycle(beanClass, invocationHandlerClass, afterBeanDiscovery, beanManager);
+
+        if (!beanLifecycle.isValid())
+        {
+            return null;
+        }
+
         BeanBuilder<T> beanBuilder = new BeanBuilder<T>(beanManager)
                 .readFromType(annotatedType)
                 .passivationCapable(true)
-                .beanLifecycle(new PartialBeanLifecycle(beanClass, invocationHandlerClass, beanManager));
+                .beanLifecycle(beanLifecycle);
 
         return beanBuilder.create();
     }
@@ -161,9 +166,7 @@ public class PartialBeanBindingExtension implements Extension, Deactivatable
     }
 
     protected <X> void validateInvocationHandler(Class<X> beanClass,
-                                                 Class<? extends Annotation> bindingAnnotationClass,
-                                                 Set<Annotation> annotations,
-                                                 BeanManager beanManager)
+                                                 Class<? extends Annotation> bindingAnnotationClass)
     {
         Class<? extends InvocationHandler> alreadyFoundHandler = this.partialBeanHandlers.get(bindingAnnotationClass);
         if (alreadyFoundHandler != null)

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/776bfcff/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/PartialBeanLifecycle.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/PartialBeanLifecycle.java b/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/PartialBeanLifecycle.java
index 65af217..d6a5891 100644
--- a/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/PartialBeanLifecycle.java
+++ b/deltaspike/modules/partial-bean/impl/src/main/java/org/apache/deltaspike/partialbean/impl/PartialBeanLifecycle.java
@@ -27,6 +27,7 @@ import org.apache.deltaspike.core.util.metadata.builder.ContextualLifecycle;
 
 import javax.enterprise.context.Dependent;
 import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.InjectionTarget;
@@ -48,8 +49,11 @@ class PartialBeanLifecycle<T, H extends InvocationHandler> implements Contextual
     private final Class<H> handlerClass;
     private CreationalContext<?> creationalContextOfDependentHandler;
     private final boolean isInterfaceMode;
+    private final boolean valid;
 
-    PartialBeanLifecycle(Class<T> partialBeanClass, Class<H> handlerClass, BeanManager beanManager)
+
+    PartialBeanLifecycle(Class<T> partialBeanClass, Class<H> handlerClass,
+                         AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager)
     {
         this.handlerClass = handlerClass;
 
@@ -70,6 +74,15 @@ class PartialBeanLifecycle<T, H extends InvocationHandler> implements Contextual
             {
                 Object proxyFactory = ClassUtils.tryToInstantiateClassForName("javassist.util.proxy.ProxyFactory");
 
+                if (proxyFactory == null)
+                {
+                    afterBeanDiscovery.addDefinitionError(new IllegalStateException(
+                        "For using abstract classes as partial beans," +
+                                "it's needed to add the lib 'javassist' to the classpath."));
+                    partialBeanProxyClass = null;
+                    this.valid = false;
+                    return;
+                }
                 Method setSuperclassMethod = proxyFactory.getClass().getDeclaredMethod("setSuperclass", Class.class);
                 setSuperclassMethod.invoke(proxyFactory, partialBeanClass);
 
@@ -93,6 +106,7 @@ class PartialBeanLifecycle<T, H extends InvocationHandler> implements Contextual
             }
         });
          */
+        this.valid = true;
     }
 
     public T create(Bean bean, CreationalContext creationalContext)
@@ -190,4 +204,9 @@ class PartialBeanLifecycle<T, H extends InvocationHandler> implements Contextual
         */
         creationalContext.release();
     }
+
+    boolean isValid()
+    {
+        return valid;
+    }
 }