[jboss-cvs] JBossAS SVN: r95087 - in projects/interceptors/trunk: jboss-interceptor and 4 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Oct 19 01:17:42 EDT 2009
Author: marius.bogoevici
Date: 2009-10-19 01:17:42 -0400 (Mon, 19 Oct 2009)
New Revision: 95087
Modified:
projects/interceptors/trunk/jboss-interceptor/pom.xml
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionModelBuilder.java
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/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/InterceptionChain.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/InterceptionUtils.java
projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java
projects/interceptors/trunk/pom.xml
Log:
Add support for registering global interceptors.
Modified: projects/interceptors/trunk/jboss-interceptor/pom.xml
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/pom.xml 2009-10-19 04:05:45 UTC (rev 95086)
+++ projects/interceptors/trunk/jboss-interceptor/pom.xml 2009-10-19 05:17:42 UTC (rev 95087)
@@ -32,7 +32,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
- <optional>true</optional>
+ <!--<optional>true</optional>-->
</dependency>
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionModelBuilder.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionModelBuilder.java 2009-10-19 04:05:45 UTC (rev 95086)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionModelBuilder.java 2009-10-19 05:17:42 UTC (rev 95087)
@@ -52,6 +52,11 @@
return interceptionModel;
}
+ public MethodInterceptorDescriptor interceptAll()
+ {
+ return new MethodInterceptorDescriptor(null, InterceptionType.values());
+ }
+
public MethodInterceptorDescriptor interceptAroundInvoke(Method m)
{
return new MethodInterceptorDescriptor(m, InterceptionType.AROUND_INVOKE);
@@ -81,17 +86,20 @@
{
private Method method;
- private InterceptionType interceptionType;
+ private InterceptionType[] interceptionTypes;
- public MethodInterceptorDescriptor(Method m, InterceptionType interceptionType)
+ public MethodInterceptorDescriptor(Method m, InterceptionType... interceptionType)
{
this.method = m;
- this.interceptionType = interceptionType;
+ this.interceptionTypes = interceptionType;
}
public void with(I... clazzes)
{
- InterceptionModelBuilder.this.interceptionModel.appendInterceptors(interceptionType, method, clazzes);
+ for (InterceptionType interceptionType: interceptionTypes)
+ {
+ InterceptionModelBuilder.this.interceptionModel.appendInterceptors(interceptionType, method, clazzes);
+ }
}
}
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-19 04:05:45 UTC (rev 95086)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java 2009-10-19 05:17:42 UTC (rev 95087)
@@ -28,7 +28,7 @@
public class InterceptionModelImpl<T, I> implements InterceptionModel<T, I>
{
- private Map<InterceptionType, List<I>> lifecycleInterceptors = new HashMap<InterceptionType, List<I>>();
+ private Map<InterceptionType, List<I>> globalInterceptors = new HashMap<InterceptionType, List<I>>();
private Map<InterceptionType, Map<MethodHolder, List<I>>> methodBoundInterceptors = new HashMap<InterceptionType, Map<MethodHolder, List<I>>>();
@@ -51,13 +51,21 @@
if (interceptionType.isLifecycleCallback())
{
- if (lifecycleInterceptors.containsKey(interceptionType))
- return lifecycleInterceptors.get(interceptionType);
+ if (globalInterceptors.containsKey(interceptionType))
+ return globalInterceptors.get(interceptionType);
}
else
{
- if (methodBoundInterceptors.containsKey(interceptionType) && methodBoundInterceptors.get(interceptionType).containsKey(new MethodHolder(method, true)))
- return methodBoundInterceptors.get(interceptionType).get(new MethodHolder(method, true));
+ ArrayList<I> returnedInterceptors = new ArrayList<I>();
+ if (globalInterceptors.containsKey(interceptionType))
+ {
+ returnedInterceptors.addAll(globalInterceptors.get(interceptionType));
+ }
+ if (methodBoundInterceptors.containsKey(interceptionType) && methodBoundInterceptors.get(interceptionType).containsKey(MethodHolder.of(method, true)))
+ {
+ returnedInterceptors.addAll(methodBoundInterceptors.get(interceptionType).get(MethodHolder.of(method, true)));
+ }
+ return returnedInterceptors;
}
return Collections.EMPTY_LIST;
}
@@ -74,13 +82,13 @@
public void appendInterceptors(InterceptionType interceptionType, Method method, I... interceptors)
{
- if (interceptionType.isLifecycleCallback())
+ if (null == method)
{
- List<I> interceptorsList = lifecycleInterceptors.get(interceptionType);
+ List<I> interceptorsList = globalInterceptors.get(interceptionType);
if (interceptorsList == null)
{
interceptorsList = new ArrayList<I>();
- lifecycleInterceptors.put(interceptionType, interceptorsList);
+ globalInterceptors.put(interceptionType, interceptorsList);
}
appendInterceptorClassesToList(interceptionType, interceptorsList, interceptors);
} else
@@ -89,11 +97,11 @@
{
methodBoundInterceptors.put(interceptionType, new HashMap<MethodHolder, List<I>>());
}
- List<I> interceptorsList = methodBoundInterceptors.get(interceptionType).get(new MethodHolder(method, true));
+ List<I> interceptorsList = methodBoundInterceptors.get(interceptionType).get(MethodHolder.of(method, true));
if (interceptorsList == null)
{
interceptorsList = new ArrayList<I>();
- methodBoundInterceptors.get(interceptionType).put(new MethodHolder(method, true), interceptorsList);
+ methodBoundInterceptors.get(interceptionType).put(MethodHolder.of(method, true), interceptorsList);
}
appendInterceptorClassesToList(interceptionType, interceptorsList, interceptors);
}
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-19 04:05:45 UTC (rev 95086)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptorClassMetadataImpl.java 2009-10-19 05:17:42 UTC (rev 95087)
@@ -74,14 +74,14 @@
detectedInterceptorTypes.add(interceptionType);
// add method in the list - if it is there already, it means that it has been added by a subclass
ReflectionUtils.ensureAccessible(method);
- if (!foundMethods.contains(new MethodHolder(method, false)))
+ if (!foundMethods.contains(MethodHolder.of(method, false)))
{
methodMap.get(interceptionType).add(0, method);
hasInterceptorMethods = true;
}
}
}
- foundMethods.add(new MethodHolder(method, false));
+ foundMethods.add(MethodHolder.of(method, false));
}
currentClass = currentClass.getSuperclass();
} while (!Object.class.equals(currentClass));
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-19 04:05:45 UTC (rev 95086)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/MethodHolder.java 2009-10-19 05:17:42 UTC (rev 95087)
@@ -36,8 +36,13 @@
private Class<?> declaringClass;
- public MethodHolder(Method method, boolean withDeclaringClass)
+ public static MethodHolder of(Method method, boolean withDeclaringClass)
{
+ return new MethodHolder(method, withDeclaringClass);
+ }
+
+ private MethodHolder(Method method, boolean withDeclaringClass)
+ {
this.methodName = method.getName();
this.parameterTypes = method.getParameterTypes();
if (withDeclaringClass)
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptionChain.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptionChain.java 2009-10-19 04:05:45 UTC (rev 95086)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptionChain.java 2009-10-19 05:17:42 UTC (rev 95087)
@@ -30,7 +30,7 @@
/**
* @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
*/
-public class InterceptionChain<I>
+public class InterceptionChain
{
private final Logger log = LoggerFactory.getLogger(InterceptionChain.class);
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-19 04:05:45 UTC (rev 95086)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorMethodHandler.java 2009-10-19 05:17:42 UTC (rev 95087)
@@ -76,11 +76,11 @@
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
{
ReflectionUtils.ensureAccessible(thisMethod);
- if (getInterceptionStack().contains(new MethodHolder(thisMethod, true)))
+ if (getInterceptionStack().contains(MethodHolder.of(thisMethod, true)))
return thisMethod.invoke(target, args);
try
{
- getInterceptionStack().add(new MethodHolder(thisMethod, true));
+ getInterceptionStack().add(MethodHolder.of(thisMethod, true));
if (!thisMethod.getDeclaringClass().equals(LifecycleMixin.class))
{
@@ -100,7 +100,7 @@
return null;
} finally
{
- getInterceptionStack().remove(new MethodHolder(thisMethod, true));
+ getInterceptionStack().remove(MethodHolder.of(thisMethod, true));
}
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/InterceptionUtils.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/InterceptionUtils.java 2009-10-19 04:05:45 UTC (rev 95086)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/InterceptionUtils.java 2009-10-19 05:17:42 UTC (rev 95087)
@@ -88,7 +88,8 @@
*/
public static boolean isInterceptionCandidate(Method method)
{
- // just a provisory implementation
+ // just a provisory implementation - any method which is not an interceptor method
+ // is an interception candidate
int modifiers = method.getModifiers();
for (InterceptionType interceptionType : InterceptionTypeRegistry.getSupportedInterceptionTypes())
{
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-19 04:05:45 UTC (rev 95086)
+++ projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java 2009-10-19 05:17:42 UTC (rev 95087)
@@ -74,8 +74,7 @@
private InterceptionModel<Class<?>, Class<?>> interceptionModel;
private InterceptorRegistry<Class<?>, Class<?>> interceptorRegistry;
- @Before
- public void resetLogAndSetupClasses() throws Exception
+ public void resetLogAndSetupClassesForMethod() throws Exception
{
InterceptorTestLogger.reset();
@@ -90,21 +89,39 @@
}
- @Test
- @Ignore
- public void testInterceptionWithInstrumentedClass() throws Exception
+ public void resetLogAndSetupClassesGlobally() throws Exception
{
+ InterceptorTestLogger.reset();
- //FootballTeam proxy = interceptorProxyCreator.createInstrumentedInstance(FootballTeam.class, new Class<?>[]{String.class}, new Object[]{TEAM_NAME});
- //FootballTeam proxy = interceptorProxyCreator.createProxyFromInstance(new FootballTeam(TEAM_NAME), FootballTeam.class);
- //executeAssertionsOnProxy(proxy);
+ InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(FootballTeam.class, (Class) Class.class);
+ builder.interceptAll().with(MyFirstInterceptor.class, MySecondInterceptor.class);
+ interceptionModel = builder.build();
+ this.interceptorRegistry = new InterceptorRegistry<Class<?>, Class<?>>();
+ this.interceptorRegistry.registerInterceptionModel(FootballTeam.class, interceptionModel);
+
}
+ public void resetLogAndSetupClassesMixed() throws Exception
+ {
+ InterceptorTestLogger.reset();
+ InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(FootballTeam.class, (Class) Class.class);
+
+ builder.interceptAll().with(MyFirstInterceptor.class);
+ builder.interceptPreDestroy().with(MySecondInterceptor.class);
+ builder.interceptAroundInvoke(FootballTeam.class.getMethod("getName")).with(MySecondInterceptor.class);
+ interceptionModel = builder.build();
+ this.interceptorRegistry = new InterceptorRegistry<Class<?>, Class<?>>();
+ this.interceptorRegistry.registerInterceptionModel(FootballTeam.class, interceptionModel);
+
+ }
+
+
@Test
- public void testInterceptionWithProxifiedObject() throws Exception
+ public void testInterceptionWithMethodRegisteredInterceptors() throws Exception
{
+ resetLogAndSetupClassesForMethod();
FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
InterceptionUtils.executePostConstruct(proxy);
Assert.assertEquals(TEAM_NAME, proxy.getName());
@@ -112,8 +129,30 @@
}
@Test
+ public void testInterceptionWithGlobalInterceptors() throws Exception
+ {
+ resetLogAndSetupClassesGlobally();
+ FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
+ InterceptionUtils.executePostConstruct(proxy);
+ Assert.assertEquals(TEAM_NAME, proxy.getName());
+ InterceptionUtils.executePredestroy(proxy);
+ }
+
+ @Test
+ public void testInterceptionWithMixedInterceptors() throws Exception
+ {
+ resetLogAndSetupClassesMixed();
+ FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
+ InterceptionUtils.executePostConstruct(proxy);
+ Assert.assertEquals(TEAM_NAME, proxy.getName());
+ InterceptionUtils.executePredestroy(proxy);
+ }
+
+
+ @Test
public void testInterceptionWithSerializedProxy() throws Exception
{
+ resetLogAndSetupClassesForMethod();
FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new ObjectOutputStream(baos).writeObject(proxy);
Modified: projects/interceptors/trunk/pom.xml
===================================================================
--- projects/interceptors/trunk/pom.xml 2009-10-19 04:05:45 UTC (rev 95086)
+++ projects/interceptors/trunk/pom.xml 2009-10-19 05:17:42 UTC (rev 95087)
@@ -217,8 +217,10 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${version.slf4j}</version>
+<!--
<scope>test</scope>
<optional>true</optional>
+-->
</dependency>
More information about the jboss-cvs-commits
mailing list