[jboss-cvs] JBossAS SVN: r94661 - in projects/interceptors/trunk/jboss-interceptor/src: main/java/org/jboss/interceptor/proxy and 2 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Oct 12 02:57:55 EDT 2009
Author: marius.bogoevici
Date: 2009-10-12 02:57:54 -0400 (Mon, 12 Oct 2009)
New Revision: 94661
Added:
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/AbstractClassInterceptionHandler.java
Modified:
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionTypeRegistry.java
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptorClassMetadataImpl.java
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/MethodHolder.java
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/DirectClassInterceptionHandler.java
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorMethodHandler.java
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/ReflectionUtils.java
projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/FootballTeam.java
projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java
Log:
Adding support for serialization and interception of postconstruct and predestroy.
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java 2009-10-12 06:54:22 UTC (rev 94660)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java 2009-10-12 06:57:54 UTC (rev 94661)
@@ -30,7 +30,7 @@
private Map<InterceptionType, List<I>> lifecycleInterceptors = new HashMap<InterceptionType, List<I>>();
- private Map<InterceptionType, Map<Method, List<I>>> methodBoundInterceptors = new HashMap<InterceptionType, Map<Method, List<I>>>();
+ private Map<InterceptionType, Map<MethodHolder, List<I>>> methodBoundInterceptors = new HashMap<InterceptionType, Map<MethodHolder, List<I>>>();
private Set<I> allInterceptors = new LinkedHashSet<I>();
@@ -56,8 +56,8 @@
}
else
{
- if (methodBoundInterceptors.containsKey(interceptionType) && methodBoundInterceptors.get(interceptionType).containsKey(method))
- return methodBoundInterceptors.get(interceptionType).get(method);
+ if (methodBoundInterceptors.containsKey(interceptionType) && methodBoundInterceptors.get(interceptionType).containsKey(new MethodHolder(method, true)))
+ return methodBoundInterceptors.get(interceptionType).get(new MethodHolder(method, true));
}
return Collections.EMPTY_LIST;
}
@@ -87,13 +87,13 @@
{
if (null == methodBoundInterceptors.get(interceptionType))
{
- methodBoundInterceptors.put(interceptionType, new HashMap<Method, List<I>>());
+ methodBoundInterceptors.put(interceptionType, new HashMap<MethodHolder, List<I>>());
}
List<I> interceptorsList = methodBoundInterceptors.get(interceptionType).get(method);
if (interceptorsList == null)
{
interceptorsList = new ArrayList<I>();
- methodBoundInterceptors.get(interceptionType).put(method, interceptorsList);
+ methodBoundInterceptors.get(interceptionType).put(new MethodHolder(method, true), interceptorsList);
}
appendInterceptorClassesToList(interceptionType, interceptorsList, interceptors);
}
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionTypeRegistry.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionTypeRegistry.java 2009-10-12 06:54:22 UTC (rev 94660)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionTypeRegistry.java 2009-10-12 06:57:54 UTC (rev 94661)
@@ -25,6 +25,8 @@
import java.util.HashMap;
import java.lang.annotation.Annotation;
+import org.jboss.interceptor.util.ReflectionUtils;
+
/**
* @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
*/
@@ -42,7 +44,7 @@
{
try
{
- interceptionAnnotationClasses.put(interceptionType, (Class<? extends Annotation>) Class.forName(interceptionType.getAnnotationClassName()));
+ interceptionAnnotationClasses.put(interceptionType, (Class<? extends Annotation>) ReflectionUtils.classForName(interceptionType.getAnnotationClassName()));
} catch (Exception e)
{
LOG.warn("Class '" + interceptionType.getAnnotationClassName() + "' not found, interception based on it is not enabled" );
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptorClassMetadataImpl.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptorClassMetadataImpl.java 2009-10-12 06:54:22 UTC (rev 94660)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptorClassMetadataImpl.java 2009-10-12 06:57:54 UTC (rev 94661)
@@ -19,16 +19,20 @@
import org.jboss.interceptor.util.InterceptionUtils;
import org.jboss.interceptor.util.ReflectionUtils;
+import org.jboss.interceptor.registry.InterceptorClassMetadataRegistry;
+import org.jboss.interceptor.InterceptorException;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.lang.reflect.Method;
import java.util.*;
+import java.io.Serializable;
/**
* @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
*/
-public class InterceptorClassMetadataImpl implements InterceptorClassMetadata
+public class InterceptorClassMetadataImpl implements InterceptorClassMetadata, Serializable
{
private Log log = LogFactory.getLog(InterceptorClassMetadataImpl.class);
@@ -85,4 +89,33 @@
return methods == null ? Collections.EMPTY_LIST : methods;
}
+ private Object writeReplace()
+ {
+ return new InterceptorClassMetadataSerializationProxy(getInterceptorClass().getName());
+ }
+
+ static class InterceptorClassMetadataSerializationProxy implements Serializable
+ {
+ private String className;
+
+ InterceptorClassMetadataSerializationProxy(String className)
+ {
+ this.className = className;
+ }
+
+ private Object readResolve()
+ {
+ try
+ {
+ return InterceptorClassMetadataRegistry.getRegistry().getInterceptorClassMetadata(ReflectionUtils.classForName(className));
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new InterceptorException("Failed to deserialize the interceptor class metadata", e);
+ }
+ }
+
+ }
+
+
}
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/MethodHolder.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/MethodHolder.java 2009-10-12 06:54:22 UTC (rev 94660)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/MethodHolder.java 2009-10-12 06:57:54 UTC (rev 94661)
@@ -19,11 +19,15 @@
import java.lang.reflect.Method;
import java.util.Arrays;
+import java.io.Serializable;
+import org.jboss.interceptor.util.ReflectionUtils;
+import org.jboss.interceptor.InterceptorException;
+
/**
* @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
*/
-public class MethodHolder
+public class MethodHolder implements Serializable
{
private String methodName;
@@ -40,6 +44,13 @@
this.declaringClass = method.getDeclaringClass();
}
+ private MethodHolder(String methodName, Class<?>[] parameterTypes, Class<?> declaringClass)
+ {
+ this.methodName = methodName;
+ this.parameterTypes = parameterTypes;
+ this.declaringClass = declaringClass;
+ }
+
@Override
public boolean equals(Object o)
{
@@ -64,4 +75,58 @@
result = 31 * result + (declaringClass != null ? declaringClass.hashCode() : 0);
return result;
}
+
+ private Object writeReplace()
+ {
+ return new MethodHolderSerializationProxy(this);
+ }
+
+ static class MethodHolderSerializationProxy implements Serializable
+ {
+ private String className;
+ private String methodName;
+ private String parameterClassNames[];
+
+ MethodHolderSerializationProxy(MethodHolder methodHolder)
+ {
+ className = methodHolder.declaringClass != null? methodHolder.declaringClass.getName() : null;
+ methodName = methodHolder.methodName;
+ if (methodHolder.parameterTypes != null)
+ {
+ parameterClassNames = new String[methodHolder.parameterTypes.length];
+ int i = 0;
+ for (Class<?> parameterType: methodHolder.parameterTypes)
+ {
+ parameterClassNames[i++] = parameterType.getName();
+ }
+ }
+ }
+
+ private Object readResolve()
+ {
+
+ try
+ {
+ Class<?>[] parameterTypes = null;
+ if (parameterClassNames != null)
+ {
+ parameterTypes = new Class<?>[parameterClassNames.length];
+ for (int i = 0; i<parameterClassNames.length; i++)
+ {
+ parameterTypes[i] = ReflectionUtils.classForName(parameterClassNames[i]);
+ }
+ }
+ Class<?> declaringClass = null;
+ if (className != null)
+ {
+ declaringClass = ReflectionUtils.classForName(className);
+ }
+ return new MethodHolder(methodName, parameterTypes, declaringClass);
+ }
+ catch (ClassNotFoundException e)
+ {
+ throw new InterceptorException("Error while deserializing intercepted instance", e);
+ }
+ }
+ }
}
Added: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/AbstractClassInterceptionHandler.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/AbstractClassInterceptionHandler.java (rev 0)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/AbstractClassInterceptionHandler.java 2009-10-12 06:57:54 UTC (rev 94661)
@@ -0,0 +1,152 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed 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.jboss.interceptor.proxy;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Queue;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.io.Serializable;
+
+import javax.interceptor.InvocationContext;
+
+import org.jboss.interceptor.util.ReflectionUtils;
+import org.jboss.interceptor.InterceptorException;
+import org.jboss.interceptor.registry.InterceptorClassMetadataRegistry;
+import org.jboss.interceptor.model.InterceptionType;
+import org.jboss.interceptor.model.InterceptorClassMetadata;
+
+/**
+ * @author Marius Bogoevici
+ */
+public abstract class AbstractClassInterceptionHandler implements InterceptionHandler, Serializable
+{
+ private InterceptorClassMetadata interceptorMetadata;
+ private Class<?> clazz;
+
+ public abstract Object getInterceptorInstance();
+
+ protected AbstractClassInterceptionHandler(Class<?> clazz)
+ {
+ this.clazz = clazz;
+ this.interceptorMetadata = InterceptorClassMetadataRegistry.getRegistry().getInterceptorClassMetadata(this.clazz);
+ }
+
+ public Object invoke(Object target, InterceptionType interceptionType, InvocationContext invocationContext) throws Exception
+ {
+ List<Method> methods = interceptorMetadata.getInterceptorMethods(interceptionType);
+ if (methods != null)
+ {
+ DelegatingInvocationContext delegatingInvocationContext = new DelegatingInvocationContext(invocationContext, getInterceptorInstance(), methods);
+ return delegatingInvocationContext.proceed();
+ } else
+ throw new InterceptorException(target.toString() + " was requested to perform " + interceptionType.name() + " but no such method is defined on it");
+ }
+
+ public InterceptorClassMetadata getInterceptorMetadata()
+ {
+ return interceptorMetadata;
+ }
+
+ public Class<?> getClazz()
+ {
+ return clazz;
+ }
+
+ public boolean handles(Class<?> clazz)
+ {
+ return this.clazz.equals(clazz);
+ }
+
+ public class DelegatingInvocationContext implements InvocationContext
+ {
+
+ private InvocationContext delegateInvocationContext;
+
+ private Object targetObject;
+
+ private Queue<Method> invocationQueue;
+
+ public DelegatingInvocationContext(InvocationContext delegateInvocationContext, Object targetObject, List<Method> methods)
+ {
+ this.delegateInvocationContext = delegateInvocationContext;
+ this.targetObject = targetObject;
+ this.invocationQueue = new ConcurrentLinkedQueue<Method>(methods);
+ }
+
+ public Map<String, Object> getContextData()
+ {
+ return delegateInvocationContext.getContextData();
+ }
+
+ public Method getMethod()
+ {
+ return delegateInvocationContext.getMethod();
+ }
+
+ public Object[] getParameters()
+ {
+ return delegateInvocationContext.getParameters();
+ }
+
+ public Object getTarget()
+ {
+ return delegateInvocationContext.getTarget();
+ }
+
+ public Object proceed() throws Exception
+ {
+ if (!invocationQueue.isEmpty())
+ {
+
+ Method interceptorMethod = invocationQueue.remove();
+ ReflectionUtils.ensureAccessible(interceptorMethod);
+ try
+ {
+ if (interceptorMethod.getParameterTypes().length == 0)
+ return interceptorMethod.invoke(targetObject);
+ else
+ return interceptorMethod.invoke(targetObject, this);
+ }
+ catch (InvocationTargetException e)
+ {
+ if (e.getCause() instanceof Exception)
+ throw (Exception)e.getCause();
+ else
+ throw new InterceptorException(e);
+ }
+ } else
+ {
+ return delegateInvocationContext.proceed();
+ }
+ }
+
+ public void setParameters(Object[] params)
+ {
+ delegateInvocationContext.setParameters(params);
+ }
+
+ public Object getTimer()
+ {
+ return delegateInvocationContext.getTimer();
+ }
+
+ }
+}
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/DirectClassInterceptionHandler.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/DirectClassInterceptionHandler.java 2009-10-12 06:54:22 UTC (rev 94660)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/DirectClassInterceptionHandler.java 2009-10-12 06:57:54 UTC (rev 94661)
@@ -17,50 +17,29 @@
package org.jboss.interceptor.proxy;
-import org.jboss.interceptor.model.InterceptionType;
-import org.jboss.interceptor.model.InterceptorClassMetadata;
import org.jboss.interceptor.registry.InterceptorClassMetadataRegistry;
import org.jboss.interceptor.InterceptorException;
-import org.jboss.interceptor.util.ReflectionUtils;
-import javax.interceptor.InvocationContext;
-import java.lang.reflect.Method;
-import java.lang.reflect.InvocationTargetException;
-import java.util.List;
-import java.util.Map;
-import java.util.Queue;
-import java.util.concurrent.ConcurrentLinkedQueue;
-
/**
* @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
*/
-public class DirectClassInterceptionHandler<I> implements InterceptionHandler
+public class DirectClassInterceptionHandler<I> extends AbstractClassInterceptionHandler
{
private final Object interceptorInstance;
- private InterceptorClassMetadata interceptorMetadata;
- private Class<?> clazz;
-
public DirectClassInterceptionHandler(Object interceptorInstance, Class<?> clazz)
{
- if (interceptorInstance == null)
- throw new IllegalArgumentException("Interceptor instance cannot be null");
-
- this.clazz = (clazz == null) ? interceptorInstance.getClass() : clazz;
+ super((clazz == null) ? interceptorInstance.getClass() : clazz);
this.interceptorInstance = interceptorInstance;
- this.interceptorMetadata = InterceptorClassMetadataRegistry.getRegistry().getInterceptorClassMetadata(this.clazz);
-
}
public DirectClassInterceptionHandler(Class<?> simpleInterceptorClass)
{
-
+ super(simpleInterceptorClass);
if (simpleInterceptorClass == null)
throw new IllegalArgumentException("Class must not be null");
-
- this.clazz = simpleInterceptorClass;
try
{
this.interceptorInstance = simpleInterceptorClass.newInstance();
@@ -68,101 +47,14 @@
{
throw new InterceptorException("Cannot create interceptor instance:", e);
}
- this.interceptorMetadata = InterceptorClassMetadataRegistry.getRegistry().getInterceptorClassMetadata(this.clazz);
}
- public Object invoke(Object target, InterceptionType interceptionType, InvocationContext invocationContext) throws Exception
+ public Object getInterceptorInstance()
{
- List<Method> methods = interceptorMetadata.getInterceptorMethods(interceptionType);
- if (methods != null)
- {
- DelegatingInvocationContext delegatingInvocationContext = new DelegatingInvocationContext(invocationContext, interceptorInstance, methods);
- return delegatingInvocationContext.proceed();
- } else
- throw new InterceptorException(target.toString() + " was requested to perform " + interceptionType.name() + " but no such method is defined on it");
+ return interceptorInstance;
}
- public boolean handles(Class<?> clazz)
- {
- return this.clazz.equals(clazz);
- }
-
- public class DelegatingInvocationContext implements InvocationContext
- {
-
- private InvocationContext delegateInvocationContext;
-
- private Object targetObject;
-
- private Queue<Method> invocationQueue;
-
- public DelegatingInvocationContext(InvocationContext delegateInvocationContext, Object targetObject, List<Method> methods)
- {
- this.delegateInvocationContext = delegateInvocationContext;
- this.targetObject = targetObject;
- this.invocationQueue = new ConcurrentLinkedQueue<Method>(methods);
- }
-
- public Map<String, Object> getContextData()
- {
- return delegateInvocationContext.getContextData();
- }
-
- public Method getMethod()
- {
- return delegateInvocationContext.getMethod();
- }
-
- public Object[] getParameters()
- {
- return delegateInvocationContext.getParameters();
- }
-
- public Object getTarget()
- {
- return delegateInvocationContext.getTarget();
- }
-
- public Object proceed() throws Exception
- {
- if (!invocationQueue.isEmpty())
- {
-
- Method interceptorMethod = invocationQueue.remove();
- ReflectionUtils.ensureAccessible(interceptorMethod);
- try
- {
- if (interceptorMethod.getParameterTypes().length == 0)
- return interceptorMethod.invoke(targetObject);
- else
- return interceptorMethod.invoke(targetObject, this);
- }
- catch (InvocationTargetException e)
- {
- if (e.getCause() instanceof Exception)
- throw (Exception)e.getCause();
- else
- throw new InterceptorException(e);
- }
- } else
- {
- return delegateInvocationContext.proceed();
- }
- }
-
- public void setParameters(Object[] params)
- {
- delegateInvocationContext.setParameters(params);
- }
-
- public Object getTimer()
- {
- return delegateInvocationContext.getTimer();
- }
-
- }
-
}
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorMethodHandler.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorMethodHandler.java 2009-10-12 06:54:22 UTC (rev 94660)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorMethodHandler.java 2009-10-12 06:57:54 UTC (rev 94661)
@@ -1,6 +1,9 @@
package org.jboss.interceptor.proxy;
import java.io.Serializable;
+import java.io.ObjectOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
@@ -132,4 +135,31 @@
InterceptionChain chain = new InterceptionChain(interceptionHandlers, interceptionType, target, thisMethod, args);
return chain.invokeNext(new InterceptorInvocationContext(chain, target, thisMethod, args));
}
+
+ private void writeObject(ObjectOutputStream objectOutputStream) throws IOException
+ {
+ try
+ {
+ executeInterception(null, null, InterceptionType.PRE_PASSIVATE);
+ objectOutputStream.defaultWriteObject();
+ }
+ catch (Throwable throwable)
+ {
+ throw new IOException("Error while serializing class", throwable);
+ }
+ }
+
+ private void readObject(ObjectInputStream objectInputStream) throws IOException
+ {
+ try
+ {
+ objectInputStream.defaultReadObject();
+ executeInterception(null, null, InterceptionType.POST_ACTIVATE);
+ }
+ catch (Throwable throwable)
+ {
+ throw new IOException("Error while deserializing class", throwable);
+ }
+ }
+
}
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/ReflectionUtils.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/ReflectionUtils.java 2009-10-12 06:54:22 UTC (rev 94660)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/ReflectionUtils.java 2009-10-12 06:57:54 UTC (rev 94661)
@@ -32,4 +32,16 @@
method.setAccessible(true);
}
}
+
+ public static Class<?> classForName(String className) throws ClassNotFoundException
+ {
+ if (Thread.currentThread().getContextClassLoader() != null)
+ {
+ return Thread.currentThread().getContextClassLoader().loadClass(className);
+ }
+ else
+ {
+ return Class.forName(className);
+ }
+ }
}
Modified: projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/FootballTeam.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/FootballTeam.java 2009-10-12 06:54:22 UTC (rev 94660)
+++ projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/FootballTeam.java 2009-10-12 06:57:54 UTC (rev 94661)
@@ -17,15 +17,20 @@
package org.jboss.interceptors.proxy;
+import java.io.Serializable;
+
import org.jboss.interceptors.proxy.InterceptorTestLogger;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
+import javax.ejb.PrePassivate;
+import javax.ejb.PostActivate;
/**
* @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
*/
-public class FootballTeam {
+public class FootballTeam implements Serializable
+{
private String teamName;
@@ -43,6 +48,18 @@
return teamName;
}
+ @PrePassivate
+ public void beforePassivating()
+ {
+ InterceptorTestLogger.add(FootballTeam.class, "prePassivating");
+ }
+
+ @PostActivate
+ public void afterActivating()
+ {
+ InterceptorTestLogger.add(FootballTeam.class, "postActivating");
+ }
+
@AroundInvoke
public Object itsMe(InvocationContext invocationContext) throws Exception {
InterceptorTestLogger.add(FootballTeam.class, "aroundInvokeBefore");
Modified: projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java 2009-10-12 06:54:22 UTC (rev 94660)
+++ projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java 2009-10-12 06:57:54 UTC (rev 94661)
@@ -17,15 +17,20 @@
package org.jboss.interceptors.proxy;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.Serializable;
+
import org.jboss.interceptor.model.InterceptionModelBuilder;
import org.jboss.interceptor.model.InterceptionModel;
-import org.jboss.interceptor.proxy.InterceptorProxyCreator;
-import org.jboss.interceptor.proxy.InterceptorProxyCreatorImpl;
import org.jboss.interceptor.proxy.DirectClassInterceptionHandlerFactory;
import org.jboss.interceptor.registry.InterceptorRegistry;
import org.jboss.interceptor.util.InterceptionUtils;
import org.jboss.interceptors.proxy.FootballTeam;
import org.jboss.interceptors.proxy.InterceptorTestLogger;
+
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -53,17 +58,28 @@
"org.jboss.interceptors.proxy.InterceptionTest$MySecondInterceptor_aroundInvokeAfter",
"org.jboss.interceptors.proxy.InterceptionTest$MyFirstInterceptor_aroundInvokeAfter",
"org.jboss.interceptors.proxy.InterceptionTest$MySecondInterceptor_preDestroy"
+ };
+ private String[] expectedLoggedValuesOnSerialization = {
+ "org.jboss.interceptors.proxy.FootballTeam_prePassivating",
+ "org.jboss.interceptors.proxy.FootballTeam_postActivating",
+ "org.jboss.interceptors.proxy.InterceptionTest$MyFirstInterceptor_aroundInvokeBefore",
+ "org.jboss.interceptors.proxy.InterceptionTest$MySecondInterceptor_aroundInvokeBefore",
+ "org.jboss.interceptors.proxy.FootballTeam_aroundInvokeBefore",
+ "org.jboss.interceptors.proxy.FootballTeam_getName",
+ "org.jboss.interceptors.proxy.FootballTeam_aroundInvokeAfter",
+ "org.jboss.interceptors.proxy.InterceptionTest$MySecondInterceptor_aroundInvokeAfter",
+ "org.jboss.interceptors.proxy.InterceptionTest$MyFirstInterceptor_aroundInvokeAfter",
};
- private InterceptionModel<Class<?>,Class<?>> interceptionModel;
- private InterceptorRegistry<Class<?>,Class<?>> interceptorRegistry;
+ private InterceptionModel<Class<?>, Class<?>> interceptionModel;
+ private InterceptorRegistry<Class<?>, Class<?>> interceptorRegistry;
@Before
public void resetLogAndSetupClasses() throws Exception
{
InterceptorTestLogger.reset();
- InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(FootballTeam.class, (Class)Class.class);
+ InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(FootballTeam.class, (Class) Class.class);
builder.interceptAroundInvoke(FootballTeam.class.getMethod("getName")).with(MyFirstInterceptor.class, MySecondInterceptor.class);
builder.interceptPostConstruct().with(MyFirstInterceptor.class);
@@ -90,22 +106,27 @@
public void testInterceptionWithProxifiedObject() throws Exception
{
FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
- executeAssertionsOnProxy(proxy);
+ InterceptionUtils.executePostConstruct(proxy);
+ Assert.assertEquals(TEAM_NAME, proxy.getName());
+ InterceptionUtils.executePredestroy(proxy);
}
- private void executeAssertionsOnProxy(FootballTeam proxy)
+ @Test
+ public void testInterceptionWithSerializedProxy() throws Exception
{
- InterceptionUtils.executePostConstruct(proxy);
+ FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ new ObjectOutputStream(baos).writeObject(proxy);
+ proxy = (FootballTeam) new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
Assert.assertEquals(TEAM_NAME, proxy.getName());
- InterceptionUtils.executePredestroy(proxy);
Object[] logValues = InterceptorTestLogger.getLog().toArray();
- Assert.assertArrayEquals(iterateAndDisplay(logValues), expectedLoggedValues, logValues);
+ Assert.assertArrayEquals(iterateAndDisplay(logValues), expectedLoggedValuesOnSerialization, logValues);
}
private String iterateAndDisplay(Object[] logValues)
{
StringBuffer buffer = new StringBuffer();
- for (Object logValue: logValues)
+ for (Object logValue : logValues)
{
buffer.append(logValue.toString()).append("\n");
}
@@ -113,7 +134,7 @@
}
- public static class MyFirstInterceptor
+ public static class MyFirstInterceptor implements Serializable
{
@AroundInvoke
More information about the jboss-cvs-commits
mailing list