Author: struberg
Date: Sat Jan 19 21:35:58 2013
New Revision: 1435712

URL: http://svn.apache.org/viewvc?rev=1435712&view=rev
Log:
OWB-344 streamline lifecycle interceptor handling

Modified:
    
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
    
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/WebBeansInterceptorConfig.java
    
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorProxyChainTest.java

Modified: 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java?rev=1435712&r1=1435711&r2=1435712&view=diff
==============================================================================
--- 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
 (original)
+++ 
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorResolutionService.java
 Sat Jan 19 21:35:58 2013
@@ -51,6 +51,7 @@ import java.util.logging.Logger;
 import org.apache.webbeans.annotation.AnnotationManager;
 import org.apache.webbeans.component.SelfInterceptorBean;
 import org.apache.webbeans.component.creation.SelfInterceptorBeanBuilder;
+import org.apache.webbeans.config.OpenWebBeansConfiguration;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.exception.WebBeansConfigurationException;
 import org.apache.webbeans.logger.WebBeansLoggerFacade;
@@ -74,6 +75,12 @@ public class InterceptorResolutionServic
     private final Class<? extends Annotation> postActivateClass;
     private final Class<? extends Annotation> aroundTimeoutClass;
 
+    /**
+     * Enforcing that interceptor callbacks should not be
+     * able to throw checked exceptions is configurable
+     */
+    private static volatile Boolean enforceCheckedException;
+
 
     public InterceptorResolutionService(WebBeansContext webBeansContext)
     {
@@ -133,9 +140,7 @@ public class InterceptorResolutionServic
         // iterate over all methods and build up the interceptor/decorator 
stack
         for (AnnotatedMethod annotatedMethod : interceptableAnnotatedMethods)
         {
-            // this probably needs some more fine tuning if a method is both 
lifecycle and used as business invocation.
-            Set<InterceptionType> interceptionTypes = 
collectInterceptionTypes(annotatedMethod);
-            BusinessMethodInterceptorInfo methodInterceptorInfo = new 
BusinessMethodInterceptorInfo(interceptionTypes);
+            BusinessMethodInterceptorInfo methodInterceptorInfo = new 
BusinessMethodInterceptorInfo();
 
             calculateEjbMethodInterceptors(methodInterceptorInfo, 
allUsedEjbInterceptors, classLevelEjbInterceptors, annotatedMethod);
 
@@ -187,6 +192,26 @@ public class InterceptorResolutionServic
                                        nonInterceptedMethods, 
lifecycleMethodInterceptorInfos);
     }
 
+    /**
+     * Lifycycle methods like {@link javax.annotation.PostConstruct} and
+     * {@link javax.annotation.PreDestroy} must not define a checked Exception
+     * regarding to the spec. But this is often unnecessary restrictive so we
+     * allow to disable this check application wide.
+     *
+     * @return <code>true</code> if the spec rule of having no checked 
exception should be enforced
+     */
+    private boolean isNoCheckedExceptionEnforced()
+    {
+        if (enforceCheckedException == null)
+        {
+            enforceCheckedException = 
Boolean.parseBoolean(webBeansContext.getOpenWebBeansConfiguration().
+                    
getProperty(OpenWebBeansConfiguration.INTERCEPTOR_FORCE_NO_CHECKED_EXCEPTIONS, 
"true"));
+        }
+
+        return enforceCheckedException.booleanValue();
+    }
+
+
     private void addCdiClassLifecycleInterceptors(Set<Annotation> 
classInterceptorBindings, Set<Interceptor<?>> allUsedCdiInterceptors)
     {
         if (classInterceptorBindings.size() > 0)
@@ -218,22 +243,22 @@ public class InterceptorResolutionServic
     private void addLifecycleMethods(Map<InterceptionType, 
LifecycleMethodInfo> lifecycleMethodInterceptorInfos,
                                      AnnotatedType<?> annotatedType,
                                      InterceptionType interceptionType,
-                                     Class<? extends Annotation> annotation,
+                                     Class<? extends Annotation> 
lifeycleAnnotation,
                                      Set<Interceptor<?>> 
allUsedCdiInterceptors,
                                      Set<Interceptor<?>> 
allUsedEjbInterceptors,
                                      List<Interceptor<?>> 
classLevelEjbInterceptors,
                                      Set<Annotation> classInterceptorBindings,
                                      boolean parentFirst)
     {
-        Set<InterceptionType> it = new HashSet<InterceptionType>();
-        it.add(interceptionType);
         List<AnnotatedMethod<?>> foundMethods = new 
ArrayList<AnnotatedMethod<?>>();
-        BusinessMethodInterceptorInfo methodInterceptorInfo = new 
BusinessMethodInterceptorInfo(it);
+        BusinessMethodInterceptorInfo methodInterceptorInfo = new 
BusinessMethodInterceptorInfo();
 
-        List<AnnotatedMethod<?>> lifecycleMethodCandidates = 
webBeansContext.getInterceptorUtil().getLifecycleMethods(annotatedType, 
annotation, parentFirst);
+        List<AnnotatedMethod<?>> lifecycleMethodCandidates = 
webBeansContext.getInterceptorUtil().getLifecycleMethods(annotatedType, 
lifeycleAnnotation, parentFirst);
 
         for (AnnotatedMethod<?> lifecycleMethod : lifecycleMethodCandidates)
         {
+            verifyLifecycleMethod(lifeycleAnnotation, lifecycleMethod);
+
             if (lifecycleMethod.getParameters().size() == 0)
             {
                 foundMethods.add(lifecycleMethod);
@@ -441,63 +466,50 @@ public class InterceptorResolutionServic
         allUsedCdiInterceptors.addAll(methodInterceptors);
     }
 
-
     /**
-     * Determine the {@link InterceptionType} of the given AnnotatedMethod
-     * of an intercepted method.
-     * An empty list means that this is an AroundInvoke method.
-     */
-    private Set<InterceptionType> collectInterceptionTypes(AnnotatedMethod 
interceptableAnnotatedMethod)
-    {
-        Set<InterceptionType> interceptionTypes = new 
HashSet<InterceptionType>();
-        for (Annotation annotation : 
interceptableAnnotatedMethod.getAnnotations())
-        {
-            if (annotation instanceof PostConstruct)
-            {
-                verifyLifecycleMethodParameters(annotation, 
interceptableAnnotatedMethod);
-                interceptionTypes.add(InterceptionType.POST_CONSTRUCT);
-            }
-            if (annotation instanceof PreDestroy)
-            {
-                verifyLifecycleMethodParameters(annotation, 
interceptableAnnotatedMethod);
-                interceptionTypes.add(InterceptionType.PRE_DESTROY);
-            }
-            if (null != ejbPlugin && 
prePassivateClass.isAssignableFrom(annotation.getClass()))
-            {
-                verifyLifecycleMethodParameters(annotation, 
interceptableAnnotatedMethod);
-                interceptionTypes.add(InterceptionType.PRE_PASSIVATE);
-            }
-            if (null != ejbPlugin && 
postActivateClass.isAssignableFrom(annotation.getClass()))
-            {
-                verifyLifecycleMethodParameters(annotation, 
interceptableAnnotatedMethod);
-                interceptionTypes.add(InterceptionType.POST_ACTIVATE);
-            }
-            if (null != ejbPlugin && 
aroundTimeoutClass.isAssignableFrom(annotation.getClass()))
-            {
-                verifyLifecycleMethodParameters(annotation, 
interceptableAnnotatedMethod);
-                interceptionTypes.add(InterceptionType.AROUND_TIMEOUT);
-            }
-        }
-
-        return interceptionTypes;
-    }
-
-    /**
-     * Check that the given lifecycle method either has
-     * no parameter at all (standard case), or
-     * exactly one InvocationContext parameter (self-interception)
+     * Check that the given lifecycle method has:
+     * <ul>
+     *     <li>either has no parameter at all (standard case), or</li>
+     *     <li>has exactly one InvocationContext parameter 
(self-interception)</li>
+     *     <li>has no return value</li>
+     * </ul>
+     *
      * @param annotatedMethod
      */
-    private void verifyLifecycleMethodParameters(Annotation 
lifecycleAnnotation, AnnotatedMethod annotatedMethod)
+    private <T> void verifyLifecycleMethod(Class<? extends Annotation> 
lifecycleAnnotation, AnnotatedMethod<T> annotatedMethod)
     {
-        List<AnnotatedParameter<?>> params = annotatedMethod.getParameters();
+        List<AnnotatedParameter<T>> params = annotatedMethod.getParameters();
         if (params.size() > 0 && (params.size() > 1 || 
!params.get(0).getBaseType().equals(InvocationContext.class)))
         {
-            throw new WebBeansConfigurationException(lifecycleAnnotation + " 
LifecycleMethod "
+            throw new 
WebBeansConfigurationException(lifecycleAnnotation.getName() + " 
LifecycleMethod "
                                                      + 
annotatedMethod.getJavaMember()
                                                      + " must either have no 
parameter or InvocationContext but has:"
                                                      + 
Arrays.toString(annotatedMethod.getJavaMember().getParameterTypes()));
         }
+
+        if (!annotatedMethod.getJavaMember().getReturnType().equals(Void.TYPE))
+        {
+            throw new WebBeansConfigurationException("@" + 
lifecycleAnnotation.getName()
+                    + " annotated method : " + 
annotatedMethod.getJavaMember().getName()
+                    + " in class : " + 
annotatedMethod.getDeclaringType().getJavaClass().getName()
+                    + " must return void type");
+        }
+
+        if (isNoCheckedExceptionEnforced() && 
ClassUtil.isMethodHasCheckedException(annotatedMethod.getJavaMember()))
+        {
+            throw new WebBeansConfigurationException("@" + 
lifecycleAnnotation.getName()
+                    + " annotated method : " + 
annotatedMethod.getJavaMember().getName()
+                    + " in class : " + 
annotatedMethod.getDeclaringType().getJavaClass().getName()
+                    + " can not throw any checked exception");
+        }
+
+        if (Modifier.isStatic(annotatedMethod.getJavaMember().getModifiers()))
+        {
+            throw new WebBeansConfigurationException("@" + 
lifecycleAnnotation.getName()
+                    + " annotated method : " + 
annotatedMethod.getJavaMember().getName()
+                    + " in class : " + 
annotatedMethod.getDeclaringType().getJavaClass().getName()
+                    + " can not be static");
+        }
     }
 
     /**
@@ -641,19 +653,8 @@ public class InterceptorResolutionServic
         private Interceptor<?>[] cdiInterceptors = null;
         private LinkedHashMap<Decorator<?>, Method> methodDecorators = null;
 
-        /**
-         * lifecycle methods can serve multiple intercepton types :/
-         */
-        private Set<InterceptionType> interceptionTypes;
-
-        public BusinessMethodInterceptorInfo(Set<InterceptionType> 
interceptionTypes)
-        {
-            this.interceptionTypes = interceptionTypes;
-        }
-
-        public Set<InterceptionType> getInterceptionTypes()
+        public BusinessMethodInterceptorInfo()
         {
-            return interceptionTypes;
         }
 
         /**

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=1435712&r1=1435711&r2=1435712&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
 Sat Jan 19 21:35:58 2013
@@ -114,12 +114,6 @@ public final class WebBeansInterceptorCo
                 {
                     methodInterceptors.put(interceptedMethod, 
Collections.EMPTY_LIST);
                 }
-
-                // empty InterceptionType -> AROUND_INVOKE
-                if (!mii.getInterceptionTypes().isEmpty())
-                {
-                    nonBusinessMethods.add(interceptedMethod);
-                }
             }
 
             List<Interceptor<?>> postConstructInterceptors

Modified: 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorProxyChainTest.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorProxyChainTest.java?rev=1435712&r1=1435711&r2=1435712&view=diff
==============================================================================
--- 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorProxyChainTest.java
 (original)
+++ 
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/interceptors/resolution/InterceptorProxyChainTest.java
 Sat Jan 19 21:35:58 2013
@@ -110,11 +110,6 @@ public class InterceptorProxyChainTest e
             {
                 methodInterceptors.put(interceptedMethod, activeInterceptors);
             }
-
-            if (!mii.getInterceptionTypes().isEmpty())
-            {
-                nonBusinessMethods.add(interceptedMethod);
-            }
         }
 
         // step 2.
@@ -156,7 +151,7 @@ public class InterceptorProxyChainTest e
         //X this is for creating the NormalScoping Proxy which is now separate
         proxyInstance = createNormalScopingProxy(classLoader, 
ClassMultiInterceptedClass.class, proxyInstance);
 
-        //X performBenchmarkOn(proxyInstance);
+        performBenchmarkOn(proxyInstance);
 
         shutDownContainer();
     }


Reply via email to