You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by be...@apache.org on 2010/02/16 16:35:19 UTC

svn commit: r910563 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/decorator/ main/java/org/apache/webbeans/intercept/ test/java/org/apache/webbeans/newtests/decorators/multiple/ test/java/org/apache/webbeans/newtests/decorat...

Author: bergmark
Date: Tue Feb 16 15:35:18 2010
New Revision: 910563

URL: http://svn.apache.org/viewvc?rev=910563&view=rev
Log:
[OWB-260] Add Interceptor to the end of the IterceptorStack when both Interceptors and Decorators apply to a bean.

Added:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/decorator/WebBeansDecoratorInterceptor.java   (with props)
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/MyIntercept.java   (with props)
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/OutputInterceptor.java   (with props)
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/DecoratorAndInterceptorStackTests.java   (with props)
    openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/decorators/tests/DecoratorAndInterceptorStack.xml   (with props)
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/OutputProvider.java

Added: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/decorator/WebBeansDecoratorInterceptor.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/decorator/WebBeansDecoratorInterceptor.java?rev=910563&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/decorator/WebBeansDecoratorInterceptor.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/decorator/WebBeansDecoratorInterceptor.java Tue Feb 16 15:35:18 2010
@@ -0,0 +1,45 @@
+/*
+ * 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.decorator;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InvocationContext;
+
+@Interceptor
+public class WebBeansDecoratorInterceptor
+{
+
+    private DelegateHandler delegate;
+    private Object instance;
+
+    public WebBeansDecoratorInterceptor(DelegateHandler delegate, Object instance)
+    {
+        this.delegate = delegate;
+        this.instance = instance;
+    }
+
+    /**
+     * Acts as the entry point into the Decorator stack when this Interceptor is
+     * added to the end of the InterceptorStack.
+     * 
+     * @throws Exception
+     */
+    @AroundInvoke
+    public Object invokeDecorators(InvocationContext ctx) throws Exception
+    {
+        return delegate.invoke(instance, ctx.getMethod(), ctx.getMethod(), ctx.getParameters());
+    }
+
+}

Propchange: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/decorator/WebBeansDecoratorInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java?rev=910563&r1=910562&r2=910563&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java Tue Feb 16 15:35:18 2010
@@ -40,6 +40,7 @@
 import org.apache.webbeans.decorator.DelegateHandler;
 import org.apache.webbeans.decorator.WebBeansDecorator;
 import org.apache.webbeans.decorator.WebBeansDecoratorConfig;
+import org.apache.webbeans.decorator.WebBeansDecoratorInterceptor;
 import org.apache.webbeans.exception.WebBeansConfigurationException;
 import org.apache.webbeans.exception.WebBeansException;
 import org.apache.webbeans.intercept.ejb.EJBInterceptorConfig;
@@ -151,85 +152,87 @@
 
     public Object invoke(Object instance, Method method, Method proceed, Object[] arguments) throws Exception
     {
-        Object result = null;        
-        boolean interceptorRun = false;
-        
         try
         {
-            if(bean instanceof InjectionTargetBean)
+            if (bean instanceof InjectionTargetBean)
             {
-                InjectionTargetBean<?> injectionTarget = (InjectionTargetBean<?>)this.bean;
-                
-                //toString is supported but no other object method names!!!
+                InjectionTargetBean<?> injectionTarget = (InjectionTargetBean<?>) this.bean;
+
+                // toString is supported but no other object method names!!!
                 if ((!ClassUtil.isObjectMethod(method.getName()) || method.getName().equals("toString")) && InterceptorUtil.isWebBeansBusinessMethod(method))
                 {
-                    
+
                     DelegateHandler delegateHandler = null;
                     List<Object> decorators = null;
-                    
-                    if(injectionTarget.getDecoratorStack().size() > 0)
+
+                    if (injectionTarget.getDecoratorStack().size() > 0)
                     {
                         ProxyFactory delegateFactory = JavassistProxyFactory.createProxyFactory(bean);
                         delegateHandler = new DelegateHandler();
                         delegateFactory.setHandler(delegateHandler);
                         Object delegate = delegateFactory.createClass().newInstance();
-                            
-                        //Gets component decorator stack
+
+                        // Gets component decorator stack
                         decorators = WebBeansDecoratorConfig.getDecoratorStack(injectionTarget, instance, delegate);
-                        
+
                         delegateHandler.setDecorators(decorators);
                     }
-               
+
                     // Run around invoke chain
                     List<InterceptorData> stack = injectionTarget.getInterceptorStack();
-                    
+
                     List<InterceptorData> temp = new ArrayList<InterceptorData>(stack);
-                    
-                    //Filter both EJB and WebBeans interceptors
+
+                    // Filter both EJB and WebBeans interceptors
                     filterCommonInterceptorStackList(temp, method);
-                    
-                    //Call Around Invokes
+
+                    // If there are both interceptors and decorators, add hook
+                    // point to the end of the interceptor stack.
+                    if (decorators != null && temp.size() > 0)
+                    {
+                        WebBeansDecoratorInterceptor lastInterceptor = new WebBeansDecoratorInterceptor(delegateHandler, instance);
+                        InterceptorDataImpl data = new InterceptorDataImpl(true);
+                        data.setInterceptorInstance(lastInterceptor);
+                        data.setAroundInvoke(lastInterceptor.getClass().getDeclaredMethods()[0]);
+                        temp.add(data);
+                    }
+
+                    // Call Around Invokes
                     if (WebBeansUtil.isContainsInterceptorMethod(temp, InterceptorType.AROUND_INVOKE))
                     {
-                        result = callAroundInvokes(method, arguments, WebBeansUtil.getInterceptorMethods(temp, InterceptorType.AROUND_INVOKE));
-                        interceptorRun = true;
+                        return callAroundInvokes(method, arguments, WebBeansUtil.getInterceptorMethods(temp, InterceptorType.AROUND_INVOKE));
                     }
-                    
-                    //If there are Decorators, allow the delegate handler to manage the stack
-                    if(decorators != null)
+
+                    // If there are Decorators, allow the delegate handler to
+                    // manage the stack
+                    if (decorators != null)
                     {
                         return delegateHandler.invoke(instance, method, proceed, arguments);
                     }
-                   
                 }
 
-                if(interceptorRun)
-                {
-                    return result;
-                }
-                
                 if (!method.isAccessible())
+
                 {
                     method.setAccessible(true);
                 }
-                
+
             }
-            
             return method.invoke(instance, arguments);
-            
-        }catch(InvocationTargetException e)
+        }
+        catch (InvocationTargetException e)
         {
             Throwable target = e.getTargetException();
-            if(Exception.class.isAssignableFrom(target.getClass()))
+            if (Exception.class.isAssignableFrom(target.getClass()))
             {
-                throw (Exception)target;
+                throw (Exception) target;
             }
             else
             {
                 throw e;
             }
         }
-        
+
     }
 
     protected abstract <T> Object callAroundInvokes(Method proceed, Object[] arguments, List<InterceptorData> stack) throws Exception;

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/MyIntercept.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/MyIntercept.java?rev=910563&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/MyIntercept.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/MyIntercept.java Tue Feb 16 15:35:18 2010
@@ -0,0 +1,29 @@
+/*
+ * 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.decorators.multiple;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import javax.interceptor.InterceptorBinding;
+
+@Inherited
+@InterceptorBinding
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface MyIntercept {}
+

Propchange: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/MyIntercept.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/OutputInterceptor.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/OutputInterceptor.java?rev=910563&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/OutputInterceptor.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/OutputInterceptor.java Tue Feb 16 15:35:18 2010
@@ -0,0 +1,32 @@
+/*
+ * 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.decorators.multiple;
+
+import javax.inject.Inject;
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InvocationContext;
+
+@MyIntercept @Interceptor
+public class OutputInterceptor {
+	
+	@Inject RequestStringBuilder rb;
+	
+	@AroundInvoke
+	public Object myHook(InvocationContext ctx) throws Exception{
+		rb.addOutput("OutputInterceptor\n");
+		return ctx.proceed();
+	}
+
+}

Propchange: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/OutputInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/OutputProvider.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/OutputProvider.java?rev=910563&r1=910562&r2=910563&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/OutputProvider.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/multiple/OutputProvider.java Tue Feb 16 15:35:18 2010
@@ -25,6 +25,7 @@
     @Inject
     RequestStringBuilder rsb = null;
 
+    @MyIntercept
     public String getOutput()
     {
 

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/DecoratorAndInterceptorStackTests.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/DecoratorAndInterceptorStackTests.java?rev=910563&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/DecoratorAndInterceptorStackTests.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/DecoratorAndInterceptorStackTests.java Tue Feb 16 15:35:18 2010
@@ -0,0 +1,72 @@
+/*
+ * 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.decorators.tests;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.enterprise.inject.Default;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.util.AnnotationLiteral;
+
+import junit.framework.Assert;
+
+import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.apache.webbeans.newtests.decorators.multiple.Decorator1;
+import org.apache.webbeans.newtests.decorators.multiple.Decorator2;
+import org.apache.webbeans.newtests.decorators.multiple.IOutputProvider;
+import org.apache.webbeans.newtests.decorators.multiple.MyIntercept;
+import org.apache.webbeans.newtests.decorators.multiple.OutputInterceptor;
+import org.apache.webbeans.newtests.decorators.multiple.OutputProvider;
+import org.apache.webbeans.newtests.decorators.multiple.RequestStringBuilder;
+import org.junit.Test;
+
+public class DecoratorAndInterceptorStackTests extends AbstractUnitTest
+{
+
+    public static final String PACKAGE_NAME = DecoratorAndInterceptorStackTests.class.getPackage().getName();
+
+    @Test
+    public void testDecoratorStack()
+    {
+        Collection<Class<?>> classes = new ArrayList<Class<?>>();
+        classes.add(Decorator1.class);
+        classes.add(Decorator2.class);
+        classes.add(IOutputProvider.class);
+        classes.add(OutputProvider.class);
+        classes.add(RequestStringBuilder.class);
+        classes.add(MyIntercept.class);
+        classes.add(OutputInterceptor.class);
+
+        Collection<URL> xmls = new ArrayList<URL>();
+        xmls.add(getXMLUrl(PACKAGE_NAME, "DecoratorAndInterceptorStack"));
+
+        startContainer(classes, xmls);
+
+        Bean<?> bean = getBeanManager().getBeans(OutputProvider.class, new AnnotationLiteral<Default>()
+        {
+        }).iterator().next();
+        Object instance = getBeanManager().getReference(bean, OutputProvider.class, getBeanManager().createCreationalContext(bean));
+
+        OutputProvider outputProvider = (OutputProvider) instance;
+
+        Assert.assertTrue(outputProvider != null);
+
+        String result = outputProvider.getOutput();
+        System.out.println(result);
+        // Verify that the Interceptors and Decorators were called in order, and in a stack.
+        Assert.assertTrue(result.equalsIgnoreCase("OutputInterceptor\nDecorator1\nDecorator2\nOutputProvider\n"));
+    }
+}

Propchange: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/decorators/tests/DecoratorAndInterceptorStackTests.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/decorators/tests/DecoratorAndInterceptorStack.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/decorators/tests/DecoratorAndInterceptorStack.xml?rev=910563&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/decorators/tests/DecoratorAndInterceptorStack.xml (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/decorators/tests/DecoratorAndInterceptorStack.xml Tue Feb 16 15:35:18 2010
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+<beans>
+   <interceptors>
+		<class>org.apache.webbeans.newtests.decorators.multiple.OutputInterceptor</class>
+	</interceptors>
+   <decorators>
+      <class>org.apache.webbeans.newtests.decorators.multiple.Decorator1</class>
+      <class>org.apache.webbeans.newtests.decorators.multiple.Decorator2</class>
+   </decorators>
+</beans>
\ No newline at end of file

Propchange: openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/decorators/tests/DecoratorAndInterceptorStack.xml
------------------------------------------------------------------------------
    svn:eol-style = native