[jboss-cvs] JBossAS SVN: r95091 - in projects/interceptors/trunk/jboss-interceptor/src: main/java/org/jboss/interceptor/util and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Oct 19 02:23:52 EDT 2009
Author: marius.bogoevici
Date: 2009-10-19 02:23:52 -0400 (Mon, 19 Oct 2009)
New Revision: 95091
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/InterceptionModelImpl.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
Log:
Add support for ignoring global interceptors.
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 06:20:40 UTC (rev 95090)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionModelBuilder.java 2009-10-19 06:23:52 UTC (rev 95091)
@@ -82,6 +82,11 @@
return new MethodInterceptorDescriptor(null, POST_ACTIVATE);
}
+ public void ignoreGlobalInterceptors(Method m)
+ {
+ this.interceptionModel.setIgnoresGlobals(m, true);
+ }
+
public final class MethodInterceptorDescriptor
{
private Method method;
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 06:20:40 UTC (rev 95090)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java 2009-10-19 06:23:52 UTC (rev 95091)
@@ -32,6 +32,8 @@
private Map<InterceptionType, Map<MethodHolder, List<I>>> methodBoundInterceptors = new HashMap<InterceptionType, Map<MethodHolder, List<I>>>();
+ private Set<MethodHolder> methodsIgnoringGlobals = new HashSet<MethodHolder>();
+
private Set<I> allInterceptors = new LinkedHashSet<I>();
private T interceptedEntity;
@@ -44,26 +46,32 @@
public List<I> getInterceptors(InterceptionType interceptionType, Method method)
{
if (interceptionType.isLifecycleCallback() && method != null)
+ {
throw new IllegalArgumentException("On a lifecycle callback, the associated method must be null");
+ }
if (!interceptionType.isLifecycleCallback() && method == null)
+ {
throw new IllegalArgumentException("Around-invoke and around-timeout interceptors are defined for a given method");
+ }
if (interceptionType.isLifecycleCallback())
{
if (globalInterceptors.containsKey(interceptionType))
+ {
return globalInterceptors.get(interceptionType);
+ }
}
else
{
ArrayList<I> returnedInterceptors = new ArrayList<I>();
- if (globalInterceptors.containsKey(interceptionType))
+ if (!methodsIgnoringGlobals.contains(methodHolder(method)) && globalInterceptors.containsKey(interceptionType))
{
returnedInterceptors.addAll(globalInterceptors.get(interceptionType));
}
- if (methodBoundInterceptors.containsKey(interceptionType) && methodBoundInterceptors.get(interceptionType).containsKey(MethodHolder.of(method, true)))
+ if (methodBoundInterceptors.containsKey(interceptionType) && methodBoundInterceptors.get(interceptionType).containsKey(methodHolder(method)))
{
- returnedInterceptors.addAll(methodBoundInterceptors.get(interceptionType).get(MethodHolder.of(method, true)));
+ returnedInterceptors.addAll(methodBoundInterceptors.get(interceptionType).get(methodHolder(method)));
}
return returnedInterceptors;
}
@@ -80,6 +88,18 @@
return this.interceptedEntity;
}
+ public void setIgnoresGlobals(Method method, boolean ignoresGlobals)
+ {
+ if (ignoresGlobals)
+ {
+ methodsIgnoringGlobals.add(methodHolder(method));
+ }
+ else
+ {
+ methodsIgnoringGlobals.remove(methodHolder(method));
+ }
+ }
+
public void appendInterceptors(InterceptionType interceptionType, Method method, I... interceptors)
{
if (null == method)
@@ -91,18 +111,23 @@
globalInterceptors.put(interceptionType, interceptorsList);
}
appendInterceptorClassesToList(interceptionType, interceptorsList, interceptors);
- } else
+ }
+ else
{
if (null == methodBoundInterceptors.get(interceptionType))
{
methodBoundInterceptors.put(interceptionType, new HashMap<MethodHolder, List<I>>());
}
- List<I> interceptorsList = methodBoundInterceptors.get(interceptionType).get(MethodHolder.of(method, true));
+ List<I> interceptorsList = methodBoundInterceptors.get(interceptionType).get(methodHolder(method));
if (interceptorsList == null)
{
interceptorsList = new ArrayList<I>();
- methodBoundInterceptors.get(interceptionType).put(MethodHolder.of(method, true), interceptorsList);
+ methodBoundInterceptors.get(interceptionType).put(methodHolder(method), interceptorsList);
}
+ if (globalInterceptors.containsKey(interceptionType))
+ {
+ validateDuplicateInterceptors(interceptionType, globalInterceptors.get(interceptionType), interceptors);
+ }
appendInterceptorClassesToList(interceptionType, interceptorsList, interceptors);
}
allInterceptors.addAll(Arrays.asList(interceptors));
@@ -110,14 +135,27 @@
private void appendInterceptorClassesToList(InterceptionType interceptionType, List<I> interceptorsList, I... interceptors)
{
- for (I interceptor: interceptors)
+ validateDuplicateInterceptors(interceptionType, interceptorsList, interceptors);
+ interceptorsList.addAll(Arrays.asList(interceptors));
+ }
+
+ private void validateDuplicateInterceptors(InterceptionType interceptionType, List<I> interceptorsList, I[] interceptors)
+ {
+ for (I interceptor : interceptors)
+ {
if (interceptorsList.contains(interceptor))
+ {
if (interceptionType != null)
- throw new InterceptorException("Duplicate interceptor class definition when binding" + interceptor + " on " + interceptionType.name());
- else
- throw new InterceptorException("Duplicate interceptor class definition when binding" + interceptor + " as general interceptor");
- else
- interceptorsList.add(interceptor);
+ {
+ throw new InterceptorException("Duplicate interceptor class definition when binding" + interceptor + " on " + interceptionType.name());
+ }
+ }
+ }
}
+ private static MethodHolder methodHolder(Method method)
+ {
+ return MethodHolder.of(method, 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 06:20:40 UTC (rev 95090)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/InterceptionUtils.java 2009-10-19 06:23:52 UTC (rev 95091)
@@ -91,6 +91,8 @@
// just a provisory implementation - any method which is not an interceptor method
// is an interception candidate
int modifiers = method.getModifiers();
+ if (Modifier.isStatic(modifiers))
+ return false;
for (InterceptionType interceptionType : InterceptionTypeRegistry.getSupportedInterceptionTypes())
{
if (method.getAnnotation(InterceptionTypeRegistry.getAnnotationClass(interceptionType)) != null)
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 06:20:40 UTC (rev 95090)
+++ projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java 2009-10-19 06:23:52 UTC (rev 95091)
@@ -32,9 +32,7 @@
import org.jboss.interceptors.proxy.InterceptorTestLogger;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.Test;
-import org.junit.Ignore;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@@ -60,6 +58,16 @@
"org.jboss.interceptors.proxy.InterceptionTest$MySecondInterceptor_preDestroy"
};
+ private String[] expectedLoggedValuesWithGlobalsIgnored = {
+ "org.jboss.interceptors.proxy.InterceptionTest$MyFirstInterceptor_postConstruct",
+ "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$MySecondInterceptor_preDestroy"
+ };
+
private String[] expectedLoggedValuesOnSerialization = {
"org.jboss.interceptors.proxy.FootballTeam_prePassivating",
"org.jboss.interceptors.proxy.FootballTeam_postActivating",
@@ -107,10 +115,23 @@
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);
+ }
+
+ public void resetLogAndSetupClassesWithGlobalsIgnored() 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);
+ builder.ignoreGlobalInterceptors(FootballTeam.class.getMethod("getName"));
interceptionModel = builder.build();
this.interceptorRegistry = new InterceptorRegistry<Class<?>, Class<?>>();
this.interceptorRegistry.registerInterceptionModel(FootballTeam.class, interceptionModel);
@@ -126,6 +147,8 @@
InterceptionUtils.executePostConstruct(proxy);
Assert.assertEquals(TEAM_NAME, proxy.getName());
InterceptionUtils.executePredestroy(proxy);
+ Object[] logValues = InterceptorTestLogger.getLog().toArray();
+ Assert.assertArrayEquals(iterateAndDisplay(logValues), expectedLoggedValues, logValues);
}
@Test
@@ -146,9 +169,24 @@
InterceptionUtils.executePostConstruct(proxy);
Assert.assertEquals(TEAM_NAME, proxy.getName());
InterceptionUtils.executePredestroy(proxy);
+ Object[] logValues = InterceptorTestLogger.getLog().toArray();
+ Assert.assertArrayEquals(iterateAndDisplay(logValues), expectedLoggedValues, logValues);
+
}
+ @Test
+ public void testInterceptionWithGlobalsIgnored() throws Exception
+ {
+ resetLogAndSetupClassesWithGlobalsIgnored();
+ 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);
+ Object[] logValues = InterceptorTestLogger.getLog().toArray();
+ Assert.assertArrayEquals(iterateAndDisplay(logValues), expectedLoggedValuesWithGlobalsIgnored, logValues);
+ }
+
@Test
public void testInterceptionWithSerializedProxy() throws Exception
{
More information about the jboss-cvs-commits
mailing list