openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1537421 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/intercept/ test/java/org/apache/webbeans/newtests/proxy/
Date Thu, 31 Oct 2013 09:57:51 GMT
Author: rmannibucau
Date: Thu Oct 31 09:57:51 2013
New Revision: 1537421

URL: http://svn.apache.org/r1537421
Log:
serializing self interceptor more particularly than other interceptorssince the bean doesn't
exist as a bean in the bm

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/proxy/InnerClassProxyTest.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/DefaultInterceptorHandler.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/proxy/InterceptorProxySerializationTest.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/DefaultInterceptorHandler.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/DefaultInterceptorHandler.java?rev=1537421&r1=1537420&r2=1537421&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/DefaultInterceptorHandler.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/DefaultInterceptorHandler.java
Thu Oct 31 09:57:51 2013
@@ -18,13 +18,18 @@
  */
 package org.apache.webbeans.intercept;
 
+import org.apache.webbeans.component.InjectionTargetBean;
+import org.apache.webbeans.component.SelfInterceptorBean;
+import org.apache.webbeans.component.WebBeansType;
 import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.portable.InjectionTargetImpl;
 import org.apache.webbeans.proxy.InterceptorHandler;
 import org.apache.webbeans.util.ExceptionUtil;
 import org.apache.webbeans.util.WebBeansUtil;
 
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.InjectionTarget;
 import javax.enterprise.inject.spi.InterceptionType;
 import javax.enterprise.inject.spi.Interceptor;
 import java.io.Externalizable;
@@ -42,6 +47,8 @@ import java.util.Map;
 
 public class DefaultInterceptorHandler<T> implements InterceptorHandler, Externalizable
 {
+    private static final String SELF_KEY = "SELF_INTERCEPTOR";
+
     /**
      * The native contextual instance target instance.
      * This is the unproxies and undecorated instance.
@@ -180,8 +187,10 @@ public class DefaultInterceptorHandler<T
         for (final Map.Entry<Interceptor<?>, ?> entry : instances.entrySet())
         {
             final Interceptor<?> key = entry.getKey();
-            serializeInterceptor(out, key);
-            out.writeObject(entry.getValue());
+            if (serializeInterceptor(out, key))
+            {
+                out.writeObject(entry.getValue());
+            }
         }
 
         out.writeInt(interceptors.size());
@@ -223,9 +232,16 @@ public class DefaultInterceptorHandler<T
         final Map<Interceptor<?>, Object> tmpInstances = new HashMap<Interceptor<?>,
Object>();
         for (int i = 0; i < instancesSize; i++)
         {
-            final Interceptor<?> bean = (Interceptor<?>) beanManager.getPassivationCapableBean(in.readUTF());
-            final Object value = in.readObject();
-            tmpInstances.put(bean, value);
+            final Interceptor<?> interceptor = readInterceptor(in.readUTF(), beanManager);
+            if (!SelfInterceptorBean.class.isInstance(interceptor))
+            {
+                final Object value = in.readObject();
+                tmpInstances.put(interceptor, value);
+            }
+            else
+            {
+                tmpInstances.put(interceptor, target);
+            }
         }
         instances = tmpInstances;
 
@@ -250,7 +266,7 @@ public class DefaultInterceptorHandler<T
             final List<Interceptor<?>> interceptorList = new ArrayList<Interceptor<?>>(interceptorListSize);
             for (int j = 0; j < interceptorListSize; j++)
             {
-                interceptorList.add((Interceptor<?>) beanManager.getPassivationCapableBean(in.readUTF()));
+                interceptorList.add(readInterceptor(in.readUTF(), beanManager));
             }
             interceptors.put(method, interceptorList);
         }
@@ -258,13 +274,52 @@ public class DefaultInterceptorHandler<T
         beanPassivationId = in.readUTF();
     }
 
-    private static void serializeInterceptor(final ObjectOutput out, final Interceptor<?>
key) throws IOException
+    /**
+     * @return false if the interceptor value can be ignored
+     */
+    private static boolean serializeInterceptor(final ObjectOutput out, final Interceptor<?>
key) throws IOException
     {
+        if (SelfInterceptorBean.class.isInstance(key))
+        {
+            final String beanName = WebBeansUtil.getPassivationId(key)
+                .replace(WebBeansType.INTERCEPTOR.name(), WebBeansType.MANAGED.name());
+            out.writeUTF(SELF_KEY + beanName);
+            return false;
+        }
+
         final String id = WebBeansUtil.getPassivationId(key);
         if (id == null)
         {
             throw new NotSerializableException(key + " is not serializable");
         }
         out.writeUTF(id);
+        return true;
+    }
+
+    private static Interceptor<?> readInterceptor(final String id, final BeanManager
beanManager) throws IOException
+    {
+        if (id.startsWith(SELF_KEY))
+        {
+            final Bean<?> bean = beanManager.getPassivationCapableBean(id.substring(SELF_KEY.length()));
+            if (InjectionTargetBean.class.isInstance(bean))
+            {
+                final InjectionTarget<?> it = InjectionTargetBean.class.cast(bean).getInjectionTarget();
+                if (InjectionTargetImpl.class.isInstance(it))
+                {
+                    final InterceptorResolutionService.BeanInterceptorInfo info = InjectionTargetImpl.class.cast(it)
+                                                                                        
       .getInterceptorInfo();
+                    return info.getSelfInterceptorBean();
+                }
+                else
+                {
+                    throw new NotSerializableException("Can't find self interceptor");
+                }
+            }
+            else
+            {
+                throw new NotSerializableException("Can't find self interceptor");
+            }
+        }
+        return (Interceptor<?>) beanManager.getPassivationCapableBean(id);
     }
 }

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/proxy/InnerClassProxyTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/proxy/InnerClassProxyTest.java?rev=1537421&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/proxy/InnerClassProxyTest.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/proxy/InnerClassProxyTest.java
Thu Oct 31 09:57:51 2013
@@ -0,0 +1,99 @@
+/*
+ * 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.proxy;
+
+import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.junit.Test;
+
+import javax.inject.Inject;
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InterceptorBinding;
+import javax.interceptor.InvocationContext;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+public class InnerClassProxyTest extends AbstractUnitTest {
+    @Inject
+    private Foo foo;
+
+    @Test
+    public void proxy() throws IllegalAccessException, InstantiationException {
+        addInterceptor(InInterceptor.class);
+        startContainer(Arrays.asList(Foo.class, Inner.class), Collections.<String>emptyList(),
true);
+        assertNotNull(foo);
+        try {
+            assertEquals("ok", foo.bar(false));
+        } catch (final Exception e) {
+            fail();
+        }
+        try {
+            foo.bar(true);
+            fail();
+        } catch (final Exception e) {
+            // no-op: ok
+        }
+        shutDownContainer();
+    }
+
+    @In
+    public static class Foo {
+        public String bar(final boolean b) throws Inner {
+            if (b) {
+                throw new Inner("fail");
+            }
+            return "ok";
+        }
+    }
+
+    @Interceptor
+    @In
+    public static class InInterceptor
+    {
+        @AroundInvoke
+        public Object invoke(InvocationContext context) throws Exception
+        {
+            return context.proceed();
+        }
+    }
+
+    @InterceptorBinding
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target( { ElementType.TYPE, ElementType.METHOD })
+    public static @interface In {
+    }
+
+    public static class Inner extends RuntimeException {
+        public Inner() {
+            // no-op
+        }
+
+        public Inner(final String message) {
+            super(message);
+        }
+    }
+}

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/proxy/InterceptorProxySerializationTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/proxy/InterceptorProxySerializationTest.java?rev=1537421&r1=1537420&r2=1537421&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/proxy/InterceptorProxySerializationTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/proxy/InterceptorProxySerializationTest.java
Thu Oct 31 09:57:51 2013
@@ -21,6 +21,7 @@ package org.apache.webbeans.newtests.pro
 import org.apache.webbeans.newtests.AbstractUnitTest;
 import org.junit.Test;
 
+import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import javax.interceptor.AroundInvoke;
 import javax.interceptor.Interceptor;
@@ -32,6 +33,7 @@ import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 import java.util.ArrayList;
+import java.util.Arrays;
 
 import static org.apache.webbeans.newtests.util.Serializations.deserialize;
 import static org.apache.webbeans.newtests.util.Serializations.serialize;
@@ -43,11 +45,14 @@ public class InterceptorProxySerializati
     @Inject
     private Intercepted client;
 
+    @Inject
+    private AutoIntercepted auto;
+
     @Test
     public void testProxyMappingConfig() throws Exception
     {
         addInterceptor(IBInterceptor.class);
-        startContainer(new ArrayList<Class<?>>() {{ add(Intercepted.class); }},
null, true);
+        startContainer(Arrays.<Class<?>>asList(Intercepted.class, AutoIntercepted.class),
null, true);
 
         try
         {
@@ -67,6 +72,35 @@ public class InterceptorProxySerializati
         }
     }
 
+    @Test
+    public void testSerializableEvenIfAutoIntercepted() throws Exception
+    {
+        addInterceptor(IBInterceptor.class);
+        startContainer(Arrays.<Class<?>>asList(Intercepted.class, AutoIntercepted.class),
null, true);
+
+        try
+        {
+            AutoIntercepted.called = false;
+            auto.touch();
+            assertTrue(AutoIntercepted.called);
+
+            final AutoIntercepted deserializeInit = AutoIntercepted.class.cast(deserialize(serialize(auto)));
+
+            AutoIntercepted.called = false;
+            auto.touch();
+            assertTrue(AutoIntercepted.called);
+
+            final AutoIntercepted deserializeState = AutoIntercepted.class.cast(deserialize(serialize(deserializeInit)));
+            AutoIntercepted.called = false;
+            auto.touch();
+            assertTrue(AutoIntercepted.called);
+        }
+        finally
+        {
+            shutDownContainer();
+        }
+    }
+
     @InterceptorBinding
     @Target({ElementType.METHOD, ElementType.TYPE})
     @Retention(RetentionPolicy.RUNTIME)
@@ -117,4 +151,18 @@ public class InterceptorProxySerializati
             return false;
         }
     }
+
+    @IB
+    public static class AutoIntercepted implements Serializable
+    {
+        public static boolean called = false;
+
+        @AroundInvoke
+        public Object auto(final InvocationContext ic)  throws Exception {
+            called = true;
+            return ic.proceed();
+        }
+
+        public void touch() {}
+    }
 }



Mime
View raw message