You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2013/01/04 21:13:10 UTC

svn commit: r1429082 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/intercept/ main/java/org/apache/webbeans/proxy/ test/java/org/apache/webbeans/newtests/interceptors/resolution/ test/resources/org/apache/webbeans/newtests/i...

Author: struberg
Date: Fri Jan  4 20:13:10 2013
New Revision: 1429082

URL: http://svn.apache.org/viewvc?rev=1429082&view=rev
Log:
OWB-344 more interceptor resolution

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionTest.java
    openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/resolution/
    openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionTest.xml
      - copied, changed from r1428645, openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/lifecycle/LifecycleTest.xml
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolution.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/WebBeansInterceptorConfig.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolution.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolution.java?rev=1429082&r1=1429081&r2=1429082&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolution.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolution.java Fri Jan  4 20:13:10 2013
@@ -28,6 +28,7 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -58,17 +59,15 @@ public class InterceptorResolution
 
     public BeanInterceptorInfo calculateInterceptorInfo(AnnotatedType annotatedType)
     {
-        BeanInterceptorInfo interceptorInfo = new BeanInterceptorInfo();
-
         List<AnnotatedMethod> interceptableAnnotatedMethods = getInterceptableAnnotatedMethods(annotatedType);
 
         InterceptorUtil interceptorUtils = webBeansContext.getInterceptorUtil();
         AnnotationManager annotationManager = webBeansContext.getAnnotationManager();
         BeanManager bm = webBeansContext.getBeanManagerImpl();
 
-        List<Interceptor> classLevelCdiInterceptors = new ArrayList<Interceptor>();
         List<Interceptor> classLevelEjbInterceptors = new ArrayList<Interceptor>();
 
+        Map<Method, List<Annotation>> methodInterceptorBindings = new HashMap<Method, List<Annotation>>();
 
         // pick up CDI interceptors from a class level
         //X TODO should work but can surely be improved!
@@ -78,21 +77,27 @@ public class InterceptorResolution
         //X TODO pick up EJB interceptors from a class level
         //X TODO pick up the decorators
 
-        // iterate over all methods and build up the interceptor stack
+        // iterate over all methods and build up the CDI interceptor stack
         for (AnnotatedMethod interceptableAnnotatedMethod : interceptableAnnotatedMethods)
         {
-            Set<Annotation> methodInterceptorBindings
-                    = annotationManager.getInterceptorAnnotations(AnnotationUtil.getAnnotationsFromSet(interceptableAnnotatedMethod.getAnnotations()));
-
             List<Annotation> cummulatedInterceptorBindings = new ArrayList<Annotation>();
-            cummulatedInterceptorBindings.addAll(methodInterceptorBindings);
+            cummulatedInterceptorBindings.addAll(
+                    annotationManager.getInterceptorAnnotations(AnnotationUtil.getAnnotationsFromSet(interceptableAnnotatedMethod.getAnnotations())));
+
             cummulatedInterceptorBindings.addAll(classInterceptorBindings);
 
+            // we collect all interceptor binding annotations on all the class.
+            classInterceptorBindings.addAll(cummulatedInterceptorBindings);
+
+            methodInterceptorBindings.put(interceptableAnnotatedMethod.getJavaMember(), cummulatedInterceptorBindings);
         }
 
         //X TODO sort the CDI interceptors
 
-        return interceptorInfo;
+        Set<Interceptor> classLevelCdiInterceptors = new HashSet<Interceptor>();
+
+
+        return new BeanInterceptorInfo(null, null, null);
     }
 
     /**
@@ -126,6 +131,13 @@ public class InterceptorResolution
      */
     public static class BeanInterceptorInfo
     {
+        public BeanInterceptorInfo(Set<Decorator<?>> decorators, Set<Interceptor<?>> interceptors, MethodInterceptorInfo[] methodsInfo)
+        {
+            this.decorators = decorators;
+            this.interceptors = interceptors;
+            this.methodsInfo = methodsInfo;
+        }
+
         /**
          * All the Interceptor Beans which are active on this class somewhere.
          * This is only used to create the Interceptor instances.
@@ -141,9 +153,22 @@ public class InterceptorResolution
          * For each method which is either decorated or intercepted we keep an entry.
          * If there is no entry then the method has neither a decorator nor an interceptor.
          */
-        private Map<Method, MethodInterceptorInfo> methodsInfo = new HashMap<Method, MethodInterceptorInfo>();
+        private MethodInterceptorInfo[] methodsInfo = null;
+
+        public Set<Decorator<?>> getDecorators()
+        {
+            return decorators;
+        }
 
+        public Set<Interceptor<?>> getInterceptors()
+        {
+            return interceptors;
+        }
 
+        public MethodInterceptorInfo[] getMethodsInfo()
+        {
+            return methodsInfo;
+        }
     }
 
     /**
@@ -160,6 +185,7 @@ public class InterceptorResolution
             this.methodEjbInterceptors = methodEjbInterceptors;
         }
 
+        private Method            method;
         private InterceptionType  interceptionType;
         private List<Interceptor> methodEjbInterceptors = null;
         private List<Interceptor> methodCdiInterceptors = null;

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java?rev=1429082&r1=1429081&r2=1429082&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorsManager.java Fri Jan  4 20:13:10 2013
@@ -29,6 +29,9 @@ import org.apache.webbeans.container.Bea
 import org.apache.webbeans.exception.WebBeansConfigurationException;
 import org.apache.webbeans.util.Asserts;
 
+/**
+ * This class keeps all the enabled interceptor classes of a certain BeanManager.
+ */
 public class InterceptorsManager
 {
     private List<Class<?>> enabledInterceptors = new CopyOnWriteArrayList<Class<?>>();
@@ -42,6 +45,9 @@ public class InterceptorsManager
         manager = webBeansContext.getBeanManagerImpl();
     }
 
+    /**
+     * Add a certain class to the enabled interceptors list.
+     */
     public void addNewInterceptor(Class<?> interceptorClazz)
     {
         Asserts.nullCheckForClass(interceptorClazz, "interceptorClazz can not be null");
@@ -52,20 +58,27 @@ public class InterceptorsManager
         }
     }
 
+    /**
+     * Helper to compare the order of different interceptor classes
+     */
     public int compare(Class<?> src, Class<?> target)
     {
         Asserts.assertNotNull(src, "src parameter can not be  null");
         Asserts.assertNotNull(target, "target parameter can not be null");
 
         int srcIndex = enabledInterceptors.indexOf(src);
-        int targetIndex = enabledInterceptors.indexOf(target);
+        if (srcIndex == -1)
+        {
+            throw new IllegalArgumentException(src.getName() + " is not an enabled interceptor!");
+        }
 
-        if (srcIndex == -1 || targetIndex == -1)
+        int targetIndex = enabledInterceptors.indexOf(target);
+        if (targetIndex == -1)
         {
-            throw new IllegalArgumentException("One of the compare class of the list : [" + src.getName() + "," + target.getName() + "]"
-                                               + " is not contained in the enabled interceptors list!");
+            throw new IllegalArgumentException(target.getName() + " is not an enabled interceptor!");
         }
 
+
         if (srcIndex == targetIndex)
         {
             return 0;
@@ -80,6 +93,9 @@ public class InterceptorsManager
         }
     }
 
+    /**
+     * Check if the given interceptor class is in the list of enabled interceptors.
+     */
     public boolean isInterceptorEnabled(Class<?> interceptorClazz)
     {
         Asserts.nullCheckForClass(interceptorClazz, "interceptorClazz can not be null");
@@ -93,7 +109,7 @@ public class InterceptorsManager
         {
             AnnotatedType<?> annotatedType = webBeansContext.getAnnotatedElementFactory().newAnnotatedType(interceptorClass);
 
-            //Validate decorator classes
+            // Validate decorator classes
             if(!annotatedType.isAnnotationPresent(Interceptor.class) && !manager.containsCustomInterceptorClass(interceptorClass))
             {
                 throw new WebBeansConfigurationException("Given class : " + interceptorClass + " is not a interceptor class");

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/WebBeansInterceptorConfig.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/WebBeansInterceptorConfig.java?rev=1429082&r1=1429081&r2=1429082&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/WebBeansInterceptorConfig.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/WebBeansInterceptorConfig.java Fri Jan  4 20:13:10 2013
@@ -20,7 +20,6 @@ package org.apache.webbeans.intercept;
 
 import org.apache.webbeans.annotation.AnnotationManager;
 import org.apache.webbeans.component.AbstractInjectionTargetBean;
-import org.apache.webbeans.component.AbstractOwbBean;
 import org.apache.webbeans.config.OWBLogConst;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.config.inheritance.IBeanInheritedMetaData;
@@ -139,7 +138,6 @@ public final class WebBeansInterceptorCo
      */
     public void configure(AbstractInjectionTargetBean<?> component, List<InterceptorData> stack)
     {
-        Class<?> clazz = ((AbstractOwbBean<?>)component).getReturnType();
         AnnotatedType<?> annotatedType = component.getAnnotatedType();
         Set<Annotation> annotations = annotatedType.getAnnotations();
 
@@ -195,8 +193,7 @@ public final class WebBeansInterceptorCo
             }
         }
 
-        anns = new Annotation[bindingTypeSet.size()];
-        anns = bindingTypeSet.toArray(anns);
+        anns = bindingTypeSet.toArray(new Annotation[bindingTypeSet.size()]);
 
         //Spec Section 9.5.2
         for(Annotation checkAnn : anns)

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java?rev=1429082&r1=1429081&r2=1429082&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/InterceptorDecoratorProxyFactory.java Fri Jan  4 20:13:10 2013
@@ -52,7 +52,7 @@ public class InterceptorDecoratorProxyFa
     public static final String FIELD_PROXIED_INSTANCE = "owbIntDecProxiedInstance";
 
     /** the name of the field which stores the Interceptor + Decorator stack InterceptorHandler */
-    public static final String FIELD_INTERCEPTOR_HANDLER = "owbDecoratorHandler";
+    public static final String FIELD_INTERCEPTOR_HANDLER = "owbIntDecHandler";
 
     //X TODO add caching of created proxy classes. This is needed to prevent class loading clashes.
     //X a generated proxy cannot easily get redefined later!

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionTest.java?rev=1429082&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionTest.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionTest.java Fri Jan  4 20:13:10 2013
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.newtests.interceptors.resolution;
+
+import javax.enterprise.inject.spi.AnnotatedType;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.webbeans.intercept.InterceptorResolution;
+import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.apache.webbeans.newtests.interceptors.factory.beans.ClassInterceptedClass;
+import org.apache.webbeans.test.component.intercept.webbeans.bindings.Transactional;
+
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test interceptor resolution.
+ */
+public class InterceptorResolutionTest  extends AbstractUnitTest
+{
+
+    @Test
+    public void testClassLevelInterceptors() throws Exception
+    {
+        Collection<String> beanXmls = new ArrayList<String>();
+        beanXmls.add(getXmlPath(this.getClass().getPackage().getName(), this.getClass().getSimpleName()));
+
+        Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
+        beanClasses.add(ClassInterceptedClass.class);
+        beanClasses.add(Transactional.class);
+
+        startContainer(beanClasses, null);
+
+        InterceptorResolution ir = new InterceptorResolution(getWebBeansContext());
+        AnnotatedType<ClassInterceptedClass> annotatedType = getBeanManager().createAnnotatedType(ClassInterceptedClass.class);
+
+        InterceptorResolution.BeanInterceptorInfo interceptorInfo = ir.calculateInterceptorInfo(annotatedType);
+        Assert.assertNotNull(interceptorInfo);
+
+/*X TODO implement!
+        Assert.assertNotNull(interceptorInfo.getInterceptors());
+        Assert.assertEquals(1, interceptorInfo.getInterceptors().size());
+
+        Assert.assertNull(interceptorInfo.getDecorators());
+
+        Assert.assertNotNull(interceptorInfo.getMethodsInfo());
+*/
+
+
+        shutDownContainer();
+    }
+}

Copied: openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionTest.xml (from r1428645, openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/lifecycle/LifecycleTest.xml)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionTest.xml?p2=openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionTest.xml&p1=openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/lifecycle/LifecycleTest.xml&r1=1428645&r2=1429082&rev=1429082&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/lifecycle/LifecycleTest.xml (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/interceptors/resolution/InterceptorResolutionTest.xml Fri Jan  4 20:13:10 2013
@@ -19,6 +19,6 @@ under the License.
 -->
 <beans>
    <interceptors>
-      <class>org.apache.webbeans.newtests.interceptors.lifecycle.LifecycleInterceptor</class>
+      <class>org.apache.webbeans.test.component.intercept.webbeans.bindings.Transactional</class>
    </interceptors>
 </beans>