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 2012/02/15 18:28:17 UTC

git commit: DELTASPIKE-61 global alternatives optimized for weld

Updated Branches:
  refs/heads/master bb2e9e1b7 -> 7bf27dccd


DELTASPIKE-61 global alternatives optimized for weld


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

Branch: refs/heads/master
Commit: 7bf27dccdb1e0836d5dc55eb004c4bf9edf51e83
Parents: bb2e9e1
Author: gpetracek <gp...@apache.org>
Authored: Wed Feb 15 18:26:16 2012 +0100
Committer: gpetracek <gp...@apache.org>
Committed: Wed Feb 15 18:26:16 2012 +0100

----------------------------------------------------------------------
 .../core/impl/exclude/ExcludeExtension.java        |  119 ++++++++++++++-
 1 files changed, 116 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/7bf27dcc/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/exclude/ExcludeExtension.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/exclude/ExcludeExtension.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/exclude/ExcludeExtension.java
index 5533b1c..403cf97 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/exclude/ExcludeExtension.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/exclude/ExcludeExtension.java
@@ -56,6 +56,8 @@ public class ExcludeExtension implements Extension, Deactivatable
 {
     private static final Logger LOG = Logger.getLogger(ExcludeExtension.class.getName());
 
+    private static Boolean isOwbDetected = null;
+
     private Boolean isActivated = null;
     private Boolean isGlobalAlternativeActivated = null;
 
@@ -81,7 +83,16 @@ public class ExcludeExtension implements Extension, Deactivatable
         //we need to do it before the exclude logic to keep the @Exclude support for global alternatives
         if (this.isGlobalAlternativeActivated)
         {
-            activateGlobalAlternatives(processAnnotatedType, beanManager);
+            detectCdiImplementation();
+
+            if (isOwbDetected)
+            {
+                activateGlobalAlternativesOwb(processAnnotatedType, beanManager);
+            }
+            else
+            {
+                activateGlobalAlternativesWeld(processAnnotatedType, beanManager);
+            }
         }
 
         if (!this.isActivated)
@@ -116,13 +127,96 @@ public class ExcludeExtension implements Extension, Deactivatable
         evalExcludeWithExpression(processAnnotatedType, exclude);
     }
 
+    private void activateGlobalAlternativesWeld(ProcessAnnotatedType<Object> processAnnotatedType,
+        BeanManager beanManager)
+    {
+        Class<Object> currentBean = processAnnotatedType.getAnnotatedType().getJavaClass();
+
+        if (currentBean.isInterface())
+        {
+            return;
+        }
+
+        Set<Class> beanBaseTypes = resolveBeanTypes(currentBean);
+
+        boolean isAlternativeBeanImplementation = currentBean.isAnnotationPresent(Alternative.class);
+
+        List<Annotation> qualifiersOfCurrentBean =
+                resolveQualifiers(processAnnotatedType.getAnnotatedType().getAnnotations(), beanManager);
+
+        String configuredBeanName;
+        List<Annotation> qualifiersOfConfiguredBean;
+        Class<Object> alternativeBeanClass;
+        Set<Annotation> alternativeBeanAnnotations;
+
+        for (Class currentType : beanBaseTypes)
+        {
+            alternativeBeanAnnotations = new HashSet<Annotation>();
+
+            configuredBeanName = ConfigResolver.getPropertyValue(currentType.getName());
+            if (configuredBeanName != null && configuredBeanName.length() > 0)
+            {
+                alternativeBeanClass = ClassUtils.tryToLoadClassForName(configuredBeanName);
+
+                if (alternativeBeanClass == null)
+                {
+                    throw new IllegalStateException("Can't find class " + configuredBeanName + " which is configured" +
+                            " for " + currentType.getName());
+                }
+
+                //check that the configured class is an alternative
+                if (!alternativeBeanClass.isAnnotationPresent(Alternative.class))
+                {
+                    //we have to continue because other classes can be configured as well
+                    continue;
+                }
+
+                alternativeBeanAnnotations.addAll(Arrays.asList(alternativeBeanClass.getAnnotations()));
+                qualifiersOfConfiguredBean = resolveQualifiers(alternativeBeanAnnotations, beanManager);
+            }
+            else
+            {
+                continue;
+            }
+
+            if (isAlternativeBeanImplementation) //current bean is annotated with @Alternative
+            {
+                if (doQualifiersMatch(qualifiersOfCurrentBean, qualifiersOfConfiguredBean))
+                {
+                    AnnotatedTypeBuilder<Object> annotatedTypeBuilder
+                        = new AnnotatedTypeBuilder<Object>().readFromType(processAnnotatedType.getAnnotatedType());
+
+                    annotatedTypeBuilder.removeFromClass(Alternative.class);
+                    processAnnotatedType.setAnnotatedType(annotatedTypeBuilder.create());
+                    return;
+                }
+            }
+            else //current bean is the original implementation
+            {
+                if (doQualifiersMatch(qualifiersOfCurrentBean, qualifiersOfConfiguredBean))
+                {
+                    //veto this original implementation because the alternative will be added
+                    processAnnotatedType.veto();
+                    return;
+                }
+            }
+        }
+    }
+
+    //see OWB-643
     //just #veto the original implementation and remove @Alternative from the ProcessAnnotatedType of
     // the configured alternative doesn't work with OWB (due to OWB-643)
-    private void activateGlobalAlternatives(ProcessAnnotatedType<Object> processAnnotatedType, BeanManager beanManager)
+    private void activateGlobalAlternativesOwb(ProcessAnnotatedType<Object> processAnnotatedType,
+        BeanManager beanManager)
     {
         //the current bean is the bean with a potential global alternative
         Class<Object> currentBean = processAnnotatedType.getAnnotatedType().getJavaClass();
 
+        if (currentBean.isInterface())
+        {
+            return;
+        }
+
         Set<Class> beanBaseTypes = resolveBeanTypes(currentBean);
 
         boolean isAlternativeBeanImplementation = currentBean.isAnnotationPresent(Alternative.class);
@@ -134,7 +228,7 @@ public class ExcludeExtension implements Extension, Deactivatable
         List<Annotation> qualifiersOfConfiguredBean;
         Class<Object> alternativeBeanClass;
         Set<Annotation> alternativeBeanAnnotations;
-        
+
         for (Class currentType : beanBaseTypes)
         {
             alternativeBeanAnnotations = new HashSet<Annotation>();
@@ -399,4 +493,23 @@ public class ExcludeExtension implements Extension, Deactivatable
             isGlobalAlternativeActivated = ClassDeactivation.isActivated(GlobalAlternative.class);
         }
     }
+
+    private void detectCdiImplementation()
+    {
+        if (isOwbDetected == null)
+        {
+            isOwbDetected = false;
+
+            RuntimeException runtimeException = new RuntimeException();
+
+            for (StackTraceElement element : runtimeException.getStackTrace())
+            {
+                if (element.toString().contains("org.apache.webbeans."))
+                {
+                    isOwbDetected = true;
+                    break;
+                }
+            }
+        }
+    }
 }