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/04/27 22:17:10 UTC

git commit: DELTASPIKE-353 restrict global alternatives to weld 1.x

Updated Branches:
  refs/heads/master c75b22937 -> 208585811


DELTASPIKE-353 restrict global alternatives to weld 1.x


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

Branch: refs/heads/master
Commit: 208585811d5443133d77e008caa59589ed6ac1db
Parents: c75b229
Author: gpetracek <gp...@apache.org>
Authored: Sat Apr 27 22:12:29 2013 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Sat Apr 27 22:14:42 2013 +0200

----------------------------------------------------------------------
 .../impl/exclude/extension/ExcludeExtension.java   |   74 ++++++++++++---
 1 files changed, 60 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/20858581/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/exclude/extension/ExcludeExtension.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/exclude/extension/ExcludeExtension.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/exclude/extension/ExcludeExtension.java
index aed8f97..88a5972 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/exclude/extension/ExcludeExtension.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/exclude/extension/ExcludeExtension.java
@@ -41,11 +41,14 @@ import javax.enterprise.inject.spi.ProcessAnnotatedType;
 import javax.enterprise.util.Nonbinding;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -59,14 +62,14 @@ public class ExcludeExtension implements Extension, Deactivatable
 {
     private static final Logger LOG = Logger.getLogger(ExcludeExtension.class.getName());
 
-    private static Boolean isWeldDetected = false;
+    private static Boolean isWeld1Detected = false;
 
     private boolean isActivated = true;
     private boolean isGlobalAlternativeActivated = true;
     private boolean isCustomProjectStageBeanFilterActivated = true;
 
     @SuppressWarnings("UnusedDeclaration")
-    protected void init(@Observes BeforeBeanDiscovery beforeBeanDiscovery)
+    protected void init(@Observes BeforeBeanDiscovery beforeBeanDiscovery, BeanManager beanManager)
     {
         isActivated =
                 ClassDeactivationUtils.isActivated(getClass());
@@ -77,7 +80,7 @@ public class ExcludeExtension implements Extension, Deactivatable
         isCustomProjectStageBeanFilterActivated =
                 ClassDeactivationUtils.isActivated(CustomProjectStageBeanFilter.class);
 
-        isWeldDetected = isWeld();
+        isWeld1Detected = isWeld1(beanManager);
     }
 
     /**
@@ -98,12 +101,9 @@ public class ExcludeExtension implements Extension, Deactivatable
     protected void vetoBeans(@Observes ProcessAnnotatedType processAnnotatedType, BeanManager beanManager)
     {
         //we need to do it before the exclude logic to keep the @Exclude support for global alternatives
-        if (isGlobalAlternativeActivated)
+        if (isGlobalAlternativeActivated && isWeld1Detected)
         {
-            if (isWeldDetected)
-            {
-                activateGlobalAlternativesWeld(processAnnotatedType, beanManager);
-            }
+            activateGlobalAlternativesWeld1(processAnnotatedType, beanManager);
         }
 
         if (isCustomProjectStageBeanFilterActivated)
@@ -158,8 +158,8 @@ public class ExcludeExtension implements Extension, Deactivatable
 
 
 
-    private void activateGlobalAlternativesWeld(ProcessAnnotatedType processAnnotatedType,
-        BeanManager beanManager)
+    private void activateGlobalAlternativesWeld1(ProcessAnnotatedType processAnnotatedType,
+                                                 BeanManager beanManager)
     {
         Class<Object> currentBean = processAnnotatedType.getAnnotatedType().getJavaClass();
 
@@ -442,13 +442,18 @@ public class ExcludeExtension implements Extension, Deactivatable
                 processAnnotatedType.getAnnotatedType().getJavaClass());
     }
 
-    private boolean isWeld()
+    private boolean isWeld1(BeanManager beanManager)
     {
-        IllegalStateException runtimeException = new IllegalStateException();
+        if (beanManager.getClass().getName().startsWith("org.apache"))
+        {
+            return false;
+        }
 
-        for (StackTraceElement element : runtimeException.getStackTrace())
+        if (beanManager.getClass().getName().startsWith("org.jboss.weld"))
         {
-            if (element.toString().contains("org.jboss.weld"))
+            String version = getJarVersion(beanManager.getClass());
+
+            if (version != null && version.startsWith("1."))
             {
                 return true;
             }
@@ -456,4 +461,45 @@ public class ExcludeExtension implements Extension, Deactivatable
 
         return false;
     }
+
+    private static String getJarVersion(Class targetClass)
+    {
+        String manifestFileLocation = getManifestFileLocationOfClass(targetClass);
+
+        try
+        {
+            return new Manifest(new URL(manifestFileLocation).openStream())
+                    //weld doesn't use IMPLEMENTATION_VERSION
+                    .getMainAttributes().getValue(Attributes.Name.SPECIFICATION_VERSION);
+        }
+        catch (Exception e)
+        {
+            return null;
+        }
+    }
+
+    private static String getManifestFileLocationOfClass(Class targetClass)
+    {
+        String manifestFileLocation;
+
+        try
+        {
+            manifestFileLocation = getManifestLocation(targetClass);
+        }
+        catch (Exception e)
+        {
+            //in this case we have a proxy
+            manifestFileLocation = getManifestLocation(targetClass.getSuperclass());
+        }
+        return manifestFileLocation;
+    }
+
+    private static String getManifestLocation(Class targetClass)
+    {
+        String classFilePath = targetClass.getCanonicalName().replace('.', '/') + ".class";
+        String manifestFilePath = "/META-INF/MANIFEST.MF";
+
+        String classLocation = targetClass.getResource(targetClass.getSimpleName() + ".class").toString();
+        return classLocation.substring(0, classLocation.indexOf(classFilePath) - 1) + manifestFilePath;
+    }
 }