[jboss-cvs] JBossAS SVN: r93943 - in projects/interceptors/trunk/src: main/java/org/jboss/interceptor/proxy and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Sep 23 03:38:39 EDT 2009


Author: marius.bogoevici
Date: 2009-09-23 03:38:38 -0400 (Wed, 23 Sep 2009)
New Revision: 93943

Added:
   projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModelBuilder.java
Modified:
   projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModel.java
   projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java
   projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionChain.java
   projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreatorImpl.java
   projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/SimpleInterceptionHandler.java
   projects/interceptors/trunk/src/main/java/org/jboss/interceptor/registry/InterceptorRegistry.java
   projects/interceptors/trunk/src/main/java/org/jboss/interceptor/util/InterceptionUtils.java
   projects/interceptors/trunk/src/test/java/org/jboss/interceptors/InterceptionTest.java
Log:
Adding an API for creating models, changing tests. Some bug fixes. Assume target class as interceptor.

Modified: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModel.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModel.java	2009-09-23 07:18:35 UTC (rev 93942)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModel.java	2009-09-23 07:38:38 UTC (rev 93943)
@@ -24,7 +24,7 @@
 /**
  * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
  */
-public interface InterceptionModel
+public interface InterceptionModel<T>
 {
 
    /**
@@ -44,4 +44,6 @@
     */
    public Set<Class<?>> getAllInterceptors();
 
+   public T getInterceptedEntity();
+
 }

Added: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModelBuilder.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModelBuilder.java	                        (rev 0)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModelBuilder.java	2009-09-23 07:38:38 UTC (rev 93943)
@@ -0,0 +1,98 @@
+/*
+ * 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.model;
+
+import static org.jboss.interceptor.model.InterceptionType.*;
+
+import java.lang.reflect.Method;
+
+/**
+ * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
+ */
+public class InterceptionModelBuilder<T>
+{
+
+   private InterceptionModelImpl interceptionModel;
+
+   private T interceptedEntity;
+
+   private InterceptionModelBuilder(T interceptedEntity)
+   {
+      this.interceptedEntity = interceptedEntity;
+      this.interceptionModel = new InterceptionModelImpl(interceptedEntity);
+   }
+
+   public static <T> InterceptionModelBuilder<T> newBuilderFor(T entity)
+   {
+      return new InterceptionModelBuilder(entity);
+   }
+
+   public T getInterceptedEntity()
+   {
+      return interceptedEntity;
+   }
+
+   public InterceptionModel build()
+   {
+      return interceptionModel;
+   }
+
+   public MethodInterceptorDescriptor interceptAroundInvoke(Method m)
+   {
+      return new MethodInterceptorDescriptor(m, InterceptionType.AROUND_INVOKE);
+   }
+
+   public MethodInterceptorDescriptor interceptPostConstruct()
+   {
+      return new MethodInterceptorDescriptor(null, POST_CONSTRUCT);
+   }
+
+   public MethodInterceptorDescriptor interceptPreDestroy()
+   {
+      return new MethodInterceptorDescriptor(null, PRE_DESTROY);
+   }
+
+   public MethodInterceptorDescriptor interceptPrePassivate()
+   {
+      return new MethodInterceptorDescriptor(null, PRE_PASSIVATE);
+   }
+
+   public MethodInterceptorDescriptor interceptPostActivate()
+   {
+      return new MethodInterceptorDescriptor(null, POST_ACTIVATE);
+   }
+
+   public final class MethodInterceptorDescriptor
+   {
+      private Method method;
+
+      private InterceptionType interceptionType;
+
+      public MethodInterceptorDescriptor(Method m, InterceptionType interceptionType)
+      {
+         this.method = m;
+         this.interceptionType = interceptionType;
+      }
+
+      public void with(Class<?>... clazzes)
+      {
+         InterceptionModelBuilder.this.interceptionModel.appendInterceptors(interceptionType, method, clazzes);
+      }
+   }
+
+}

Modified: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java	2009-09-23 07:18:35 UTC (rev 93942)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/model/InterceptionModelImpl.java	2009-09-23 07:38:38 UTC (rev 93943)
@@ -17,13 +17,15 @@
 
 package org.jboss.interceptor.model;
 
+import org.jboss.interceptor.proxy.InterceptorException;
+
 import java.lang.reflect.Method;
 import java.util.*;
 
 /**
  * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
  */
-public class InterceptionModelImpl implements InterceptionModel
+public class InterceptionModelImpl<T> implements InterceptionModel<T>
 {
 
    private Map<InterceptionType, List<Class<?>>> lifecycleInterceptors = new HashMap<InterceptionType, List<Class<?>>>();
@@ -32,7 +34,13 @@
 
    private Set<Class<?>> allInterceptors = new LinkedHashSet<Class<?>>();
 
+   private T interceptedEntity;
 
+   public InterceptionModelImpl(T interceptedEntity)
+   {
+      this.interceptedEntity = interceptedEntity;
+   }
+
    public List<Class<?>> getInterceptors(InterceptionType interceptionType, Method method)
    {
       if (interceptionType.isLifecycleCallback() && method != null)
@@ -58,20 +66,47 @@
       return Collections.unmodifiableSet(allInterceptors);
    }
 
-   public void setInterceptors(InterceptionType interceptionType, Method method, List<Class<?>> interceptors)
+   public T getInterceptedEntity()
    {
+      return this.interceptedEntity;
+   }
+
+   public void appendInterceptors(InterceptionType interceptionType, Method method, Class<?>... interceptors)
+   {
       if (interceptionType.isLifecycleCallback())
       {
-         lifecycleInterceptors.put(interceptionType, interceptors);
+         List<Class<?>> interceptorsList = lifecycleInterceptors.get(interceptionType);
+         if (interceptorsList == null)
+         {
+            interceptorsList = new ArrayList<Class<?>>();
+            lifecycleInterceptors.put(interceptionType, interceptorsList);
+         }
+         appendInterceptorClassesToList(interceptionType, interceptorsList, interceptors);
       } else
       {
          if (null == methodBoundInterceptors.get(interceptionType))
          {
             methodBoundInterceptors.put(interceptionType, new HashMap<Method, List<Class<?>>>());
          }
-         methodBoundInterceptors.get(interceptionType).put(method, interceptors);
+         List<Class<?>> interceptorsList = methodBoundInterceptors.get(interceptionType).get(method);
+         if (interceptorsList == null)
+         {
+            interceptorsList = new ArrayList<Class<?>>();
+            methodBoundInterceptors.get(interceptionType).put(method, interceptorsList);
+         }
+         appendInterceptorClassesToList(interceptionType, interceptorsList, interceptors);
       }
-      allInterceptors.addAll(interceptors);
+      for (Class<?> interceptorClass: interceptors )
+          allInterceptors.add(interceptorClass);
    }
 
+   private void appendInterceptorClassesToList(InterceptionType interceptionType, List<Class<?>> interceptorsList, Class<?>... interceptors)
+   {
+      for (Class<?> clazz: interceptors)
+         if (interceptorsList.contains(clazz))
+            throw new InterceptorException("Duplicate interceptor class definition when binding" + clazz.getName() + " on " + interceptionType.name());
+      else
+            interceptorsList.add(clazz);
+   }
+
 }

Modified: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionChain.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionChain.java	2009-09-23 07:18:35 UTC (rev 93942)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptionChain.java	2009-09-23 07:38:38 UTC (rev 93943)
@@ -56,7 +56,7 @@
 
    public Object invokeNext(InvocationContext invocationContext) {
 
-      if (currentPosition < interceptorClasses.size())
+      if (hasNext())
       {
          return interceptionHandlerMap.get(interceptorClasses.get(currentPosition++)).invoke(target, interceptionType, invocationContext);
       }
@@ -78,4 +78,10 @@
       }
    }
 
+   public boolean hasNext()
+   {
+      return currentPosition < interceptorClasses.size();
+   }
+
+
 }

Modified: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreatorImpl.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreatorImpl.java	2009-09-23 07:18:35 UTC (rev 93942)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreatorImpl.java	2009-09-23 07:38:38 UTC (rev 93943)
@@ -19,9 +19,11 @@
 
 import org.jboss.interceptor.registry.InterceptorRegistry;
 import org.jboss.interceptor.model.InterceptionType;
+import static org.jboss.interceptor.util.InterceptionUtils.isAroundInvokeInterceptionCandidate;
 import javassist.util.proxy.ProxyFactory;
 import javassist.util.proxy.MethodHandler;
 
+import javax.interceptor.AroundInvoke;
 import java.lang.reflect.Method;
 import java.util.*;
 
@@ -33,11 +35,11 @@
    public static final String POST_CONSTRUCT = "lifecycle_mixin_$$_postConstruct";
    public static final String PRE_DESTROY = "lifecycle_mixin_$$_preDestroy";
 
-   private InterceptorRegistry<Class> interceptorRegistry;
+   private InterceptorRegistry<Class<?>> interceptorRegistry;
 
    private InterceptionHandlerFactory interceptionHandlerFactory;
 
-   public InterceptorProxyCreatorImpl(InterceptorRegistry<Class> interceptorRegistry, InterceptionHandlerFactory interceptionHandlerFactory)
+   public InterceptorProxyCreatorImpl(InterceptorRegistry<Class<?>> interceptorRegistry, InterceptionHandlerFactory interceptionHandlerFactory)
    {
       this.interceptorRegistry = interceptorRegistry;
       this.interceptionHandlerFactory = interceptionHandlerFactory;
@@ -114,7 +116,7 @@
       private Map<Class<?>, InterceptionHandler> interceptorHandlerInstances = new HashMap<Class<?>, InterceptionHandler>();
       private Class<?> targetClazz;
 
-      public InstanceProxifyingMethodHandler(Object target, Class<?> targetClass, InterceptorRegistry registry)
+      public InstanceProxifyingMethodHandler(Object target, Class<?> targetClass, InterceptorRegistry<Class<?>> registry)
       {
          if (target == null)
             this.target = this;
@@ -130,25 +132,18 @@
 
          for (Class<?> interceptorClazz : registry.getInterceptionModel(this.targetClazz).getAllInterceptors())
          {
-            if (!interceptorClazz.equals(targetClazz))
-               interceptorHandlerInstances.put(interceptorClazz, interceptionHandlerFactory.createForClass(interceptorClazz));
+            interceptorHandlerInstances.put(interceptorClazz, interceptionHandlerFactory.createForClass(interceptorClazz));
          }
 
          interceptorHandlerInstances.put(targetClazz, new SimpleInterceptionHandler(target, targetClazz));
       }
 
-//      public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
-//      {
-//         List<Class<?>> interceptorClasses = registry.getInterceptionModel(targetClazz).getInterceptors(InterceptionType.AROUND_INVOKE, thisMethod);
-//         List<Class<?>> interceptorClassesForMethod = interceptorClasses == null ? new ArrayList<Class<?>>() : interceptorClasses;
-//         InterceptionChain chain = new InterceptionChain(interceptorClassesForMethod, InterceptionType.AROUND_INVOKE, target, thisMethod, args, interceptorHandlerInstances);
-//         return chain.invokeNext(new InterceptorInvocationContext(chain, target, thisMethod, args));
-//      }
-
       public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
       {
          if (!thisMethod.getDeclaringClass().equals(LifecycleMixin.class))
          {
+            if (!isAroundInvokeInterceptionCandidate(thisMethod))
+               return proceed.invoke(self, args);
             return executeInterception(thisMethod, args, InterceptionType.AROUND_INVOKE);
          } else
          {
@@ -164,10 +159,15 @@
          return null;
       }
 
+
+
       private Object executeInterception(Method thisMethod, Object[] args, InterceptionType interceptionType)
       {
          List<Class<?>> interceptorClasses = registry.getInterceptionModel(targetClazz).getInterceptors(interceptionType, thisMethod);
          List<Class<?>> interceptorClassesForMethod = interceptorClasses == null ? new ArrayList<Class<?>>() : interceptorClasses;
+         //assume the list is immutable
+         interceptorClassesForMethod = new ArrayList(interceptorClassesForMethod);
+         interceptorClassesForMethod.add(targetClazz);
          InterceptionChain chain = new InterceptionChain(interceptorClassesForMethod, interceptionType, target, thisMethod, args, interceptorHandlerInstances);
          return chain.invokeNext(new InterceptorInvocationContext(chain, target, thisMethod, args));
       }
@@ -180,9 +180,8 @@
       private Map<Class<?>, InterceptionHandler> interceptorHandlerInstances = new HashMap<Class<?>, InterceptionHandler>();
       private Class<?> targetClazz;
 
-      public AutoProxifiedMethodHandler(Class<?> targetClazz, InterceptorRegistry registry)
+      public AutoProxifiedMethodHandler(Class<?> targetClazz, InterceptorRegistry<Class<?>> registry)
       {
-
          if (targetClazz == null)
             throw new IllegalArgumentException("Target class must not be null");
 
@@ -197,8 +196,11 @@
 
       public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
       {
+         // do not intercept interceptor methods
          if (!thisMethod.getDeclaringClass().equals(LifecycleMixin.class))
          {
+            if (thisMethod.getAnnotation(AroundInvoke.class) != null)
+               return proceed.invoke(self, args);
             return executeInterception(self, thisMethod, proceed, args, InterceptionType.AROUND_INVOKE);
          } else
          {
@@ -216,11 +218,22 @@
 
       private Object executeInterception(Object self, Method thisMethod, Method proceed, Object[] args, InterceptionType interceptionType)
       {
+         addSelfAsInterceptorHandler(self);
          List<Class<?>> interceptorClasses = registry.getInterceptionModel(targetClazz).getInterceptors(interceptionType, thisMethod);
          List<Class<?>> interceptorClassesForMethod = interceptorClasses == null ? new ArrayList<Class<?>>() : interceptorClasses;
+         //assume the list is immutable and always consider target class as interception targets
+         interceptorClassesForMethod = new ArrayList(interceptorClassesForMethod);
+         interceptorClassesForMethod.add(targetClazz);
          InterceptionChain chain = new InterceptionChain(interceptorClassesForMethod, interceptionType, self, proceed, args, interceptorHandlerInstances);
          return chain.invokeNext(new InterceptorInvocationContext(chain, self, proceed, args));
       }
+
+      private void addSelfAsInterceptorHandler(Object self)
+      {
+         if (!interceptorHandlerInstances.containsKey(targetClazz))
+            interceptorHandlerInstances.put(targetClazz, new SimpleInterceptionHandler(self, targetClazz));
+      }
+
    }
 
    public interface LifecycleMixin
@@ -230,3 +243,5 @@
       public void lifecycle_mixin_$$_preDestroy();
    }
 }
+
+

Modified: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/SimpleInterceptionHandler.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/SimpleInterceptionHandler.java	2009-09-23 07:18:35 UTC (rev 93942)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/proxy/SimpleInterceptionHandler.java	2009-09-23 07:38:38 UTC (rev 93943)
@@ -88,7 +88,10 @@
    {
       try
       {
-         return interceptorMethods.get(interceptionType).invoke(interceptorInstance, new Object[]{invocationContext});
+         if (interceptorMethods.get(interceptionType) != null)
+            return interceptorMethods.get(interceptionType).invoke(interceptorInstance, new Object[]{invocationContext});
+         else
+            return null;
       } catch (IllegalAccessException e)
       {
          throw new RuntimeException((e));

Modified: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/registry/InterceptorRegistry.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/registry/InterceptorRegistry.java	2009-09-23 07:18:35 UTC (rev 93942)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/registry/InterceptorRegistry.java	2009-09-23 07:38:38 UTC (rev 93943)
@@ -29,14 +29,14 @@
  */
 public class InterceptorRegistry<T>
 {
-   private Map<T, InterceptionModel> interceptionModelMap = new HashMap<T, InterceptionModel>();
+   private Map<T, InterceptionModel<T>> interceptionModelMap = new HashMap<T, InterceptionModel<T>>();
 
-   public void registerInterceptionModel(T interceptedEntity, InterceptionModel interceptionModel)
+   public void registerInterceptionModel(T interceptedEntity, InterceptionModel<T> interceptionModel)
    {
       this.interceptionModelMap.put(interceptedEntity, interceptionModel);
    }
 
-   public InterceptionModel getInterceptionModel(T interceptedEntity)
+   public InterceptionModel<T> getInterceptionModel(T interceptedEntity)
    {
       return this.interceptionModelMap.get(interceptedEntity);
    }

Modified: projects/interceptors/trunk/src/main/java/org/jboss/interceptor/util/InterceptionUtils.java
===================================================================
--- projects/interceptors/trunk/src/main/java/org/jboss/interceptor/util/InterceptionUtils.java	2009-09-23 07:18:35 UTC (rev 93942)
+++ projects/interceptors/trunk/src/main/java/org/jboss/interceptor/util/InterceptionUtils.java	2009-09-23 07:38:38 UTC (rev 93943)
@@ -19,22 +19,35 @@
 
 import org.jboss.interceptor.proxy.InterceptorProxyCreatorImpl;
 
+import javax.interceptor.AroundInvoke;
+import java.lang.reflect.Method;
+
 /**
  * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
  */
 public class InterceptionUtils
 {
-   public static void executePostConstruct(Object proxy) {
-      if (proxy instanceof InterceptorProxyCreatorImpl.LifecycleMixin) {
+   public static void executePostConstruct(Object proxy)
+   {
+      if (proxy instanceof InterceptorProxyCreatorImpl.LifecycleMixin)
+      {
          InterceptorProxyCreatorImpl.LifecycleMixin lifecycleMixin = (InterceptorProxyCreatorImpl.LifecycleMixin) proxy;
          lifecycleMixin.lifecycle_mixin_$$_postConstruct();
       }
    }
 
-   public static void executePredestroy(Object proxy) {
-      if (proxy instanceof InterceptorProxyCreatorImpl.LifecycleMixin) {
+   public static void executePredestroy(Object proxy)
+   {
+      if (proxy instanceof InterceptorProxyCreatorImpl.LifecycleMixin)
+      {
          InterceptorProxyCreatorImpl.LifecycleMixin lifecycleMixin = (InterceptorProxyCreatorImpl.LifecycleMixin) proxy;
          lifecycleMixin.lifecycle_mixin_$$_preDestroy();
       }
    }
+
+   public static boolean isAroundInvokeInterceptionCandidate(Method thisMethod)
+   {
+      // just a provisory implementation
+      return thisMethod.getAnnotation(AroundInvoke.class) == null;
+   }
 }

Modified: projects/interceptors/trunk/src/test/java/org/jboss/interceptors/InterceptionTest.java
===================================================================
--- projects/interceptors/trunk/src/test/java/org/jboss/interceptors/InterceptionTest.java	2009-09-23 07:18:35 UTC (rev 93942)
+++ projects/interceptors/trunk/src/test/java/org/jboss/interceptors/InterceptionTest.java	2009-09-23 07:38:38 UTC (rev 93943)
@@ -23,6 +23,7 @@
 import org.jboss.interceptor.registry.InterceptorRegistry;
 import org.jboss.interceptor.model.InterceptionModelImpl;
 import org.jboss.interceptor.model.InterceptionType;
+import org.jboss.interceptor.model.InterceptionModelBuilder;
 import org.jboss.interceptor.util.InterceptionUtils;
 import org.junit.Test;
 import org.junit.Before;
@@ -37,79 +38,119 @@
 /**
  * @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
  */
-public class InterceptionTest {
-    private static final String TEAM_NAME = "Ajax Amsterdam";
+public class InterceptionTest
+{
+   private static final String TEAM_NAME = "Ajax Amsterdam";
 
-    private String[] expectedLoggedValues = {
-    "org.jboss.interceptors.InterceptionTest$MyFirstInterceptor_postConstruct",
-            "org.jboss.interceptors.InterceptionTest$MyFirstInterceptor_aroundInvokeBefore",
-            "org.jboss.interceptors.InterceptionTest$MySecondInterceptor_aroundInvokeBefore",
-            "org.jboss.interceptors.FootballTeam_aroundInvokeBefore",
-            "org.jboss.interceptors.FootballTeam_getName",
-            "org.jboss.interceptors.FootballTeam_aroundInvokeAfter",
-            "org.jboss.interceptors.InterceptionTest$MySecondInterceptor_aroundInvokeAfter",
-            "org.jboss.interceptors.InterceptionTest$MyFirstInterceptor_aroundInvokeAfter",
-            "org.jboss.interceptors.InterceptionTest$MySecondInterceptor_preDestroy"
+   private String[] expectedLoggedValues = {
+         "org.jboss.interceptors.InterceptionTest$MyFirstInterceptor_postConstruct",
+         "org.jboss.interceptors.InterceptionTest$MyFirstInterceptor_aroundInvokeBefore",
+         "org.jboss.interceptors.InterceptionTest$MySecondInterceptor_aroundInvokeBefore",
+         "org.jboss.interceptors.FootballTeam_aroundInvokeBefore",
+         "org.jboss.interceptors.FootballTeam_getName",
+         "org.jboss.interceptors.FootballTeam_aroundInvokeAfter",
+         "org.jboss.interceptors.InterceptionTest$MySecondInterceptor_aroundInvokeAfter",
+         "org.jboss.interceptors.InterceptionTest$MyFirstInterceptor_aroundInvokeAfter",
+         "org.jboss.interceptors.InterceptionTest$MySecondInterceptor_preDestroy"
 
-    };
+   };
+   private InterceptorRegistry<Class<?>> interceptorRegistry;
+   private InterceptionModelBuilder builder;
+   private InterceptorProxyCreator interceptorProxyCreator;
 
-    @Before
-    public void resetLog() {
-        InterceptorTestLogger.reset();
-    }
+   @Before
+   public void resetLogAndSetupClasses() throws Exception
+   {
+      InterceptorTestLogger.reset();
+      interceptorRegistry = new InterceptorRegistry<Class<?>>();
 
-    @Test
-    public void testInterception() throws Exception {
+      builder = InterceptionModelBuilder.newBuilderFor(FootballTeam.class);
 
-        InterceptorRegistry<Class> interceptorRegistry = new InterceptorRegistry<Class>();
-        InterceptionModelImpl interceptionModel = new InterceptionModelImpl();
-        interceptionModel.setInterceptors(InterceptionType.AROUND_INVOKE, FootballTeam.class.getMethod("getName"), Arrays.asList(new Class<?>[]{MyFirstInterceptor.class, MySecondInterceptor.class, FootballTeam.class}));
-        interceptionModel.setInterceptors(InterceptionType.POST_CONSTRUCT, null, Arrays.asList(new Class<?>[]{MyFirstInterceptor.class}));
-        interceptionModel.setInterceptors(InterceptionType.PRE_DESTROY, null, Arrays.asList(new Class<?>[]{MySecondInterceptor.class}));
-        interceptorRegistry.registerInterceptionModel(FootballTeam.class, interceptionModel);
+      builder.interceptAroundInvoke(FootballTeam.class.getMethod("getName")).with(MyFirstInterceptor.class, MySecondInterceptor.class);
+      builder.interceptPostConstruct().with(MyFirstInterceptor.class);
+      builder.interceptPreDestroy().with(MySecondInterceptor.class);
+      interceptorRegistry.registerInterceptionModel(FootballTeam.class, builder.build());
 
-        InterceptorProxyCreator interceptorProxyCreator = new InterceptorProxyCreatorImpl(interceptorRegistry, new SimpleInterceptionHandlerFactory());
-        //SoccerTeam proxy = interceptorProxyCreator.createInstrumentedInstance(FootballTeam.class, new Class<?>[]{String.class}, new Object[]{"Poli Timisoara"});
-        FootballTeam proxy = interceptorProxyCreator.createProxyFromInstance(new FootballTeam(TEAM_NAME), FootballTeam.class);
-        InterceptionUtils.executePostConstruct(proxy);
-        Assert.assertEquals(TEAM_NAME, proxy.getName());
-        InterceptionUtils.executePredestroy(proxy);
-        Assert.assertArrayEquals(expectedLoggedValues, InterceptorTestLogger.getLog().toArray());
+      interceptorProxyCreator = new InterceptorProxyCreatorImpl(interceptorRegistry, new SimpleInterceptionHandlerFactory());
 
-    }
+   }
 
+   @Test
+   public void testInterceptionWithInstrumentedClass() throws Exception
+   {
 
-    public static class MyFirstInterceptor {
+      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);
 
-        @AroundInvoke
-        public Object doAround2(InvocationContext invocationContext) throws Exception {
-            InterceptorTestLogger.add(MyFirstInterceptor.class,"aroundInvokeBefore");
-            Object result = invocationContext.proceed();
-            InterceptorTestLogger.add(MyFirstInterceptor.class, "aroundInvokeAfter");
-            return result;
-        }
+   }
 
-        @PostConstruct
-        public Object doAfterConstruction(InvocationContext invocationContext) throws Exception {
-            InterceptorTestLogger.add(MyFirstInterceptor.class, "postConstruct");
-            return invocationContext.proceed();
-        }
-    }
 
-    public static class MySecondInterceptor extends MyFirstInterceptor {
-        @AroundInvoke
-        public Object doAround(InvocationContext invocationContext) throws Exception {
-            InterceptorTestLogger.add(MySecondInterceptor.class, "aroundInvokeBefore");
-            Object result = invocationContext.proceed();
-            InterceptorTestLogger.add(MySecondInterceptor.class, "aroundInvokeAfter");
-            return result;
-        }
+   @Test
+   public void testInterceptionWithProxifiedObject() throws Exception
+   {
+      FootballTeam proxy = interceptorProxyCreator.createProxyFromInstance(new FootballTeam(TEAM_NAME), FootballTeam.class);
+      executeAssertionsOnProxy(proxy);
 
-        @PreDestroy
-        public Object doneHere(InvocationContext invocationContext) throws Exception {
-            InterceptorTestLogger.add(MySecondInterceptor.class, "preDestroy");
-            return invocationContext.proceed();
-        }
-    }
+   }
+
+   private void executeAssertionsOnProxy(FootballTeam proxy)
+   {
+      InterceptionUtils.executePostConstruct(proxy);
+      Assert.assertEquals(TEAM_NAME, proxy.getName());
+      InterceptionUtils.executePredestroy(proxy);
+      Object[] logValues = InterceptorTestLogger.getLog().toArray();
+      Assert.assertArrayEquals(iterateAndDisplay(logValues), expectedLoggedValues, logValues);
+   }
+
+   private String iterateAndDisplay(Object[] logValues)
+   {
+      StringBuffer buffer = new StringBuffer();
+      for (Object logValue: logValues)
+      {
+         buffer.append(logValue.toString() + "\n");
+      }
+      return buffer.toString();
+   }
+
+
+   public static class MyFirstInterceptor
+   {
+
+      @AroundInvoke
+      public Object doAround2(InvocationContext invocationContext) throws Exception
+      {
+         InterceptorTestLogger.add(MyFirstInterceptor.class, "aroundInvokeBefore");
+         Object result = invocationContext.proceed();
+         InterceptorTestLogger.add(MyFirstInterceptor.class, "aroundInvokeAfter");
+         return result;
+      }
+
+      @PostConstruct
+      public Object doAfterConstruction(InvocationContext invocationContext) throws Exception
+      {
+         InterceptorTestLogger.add(MyFirstInterceptor.class, "postConstruct");
+         return invocationContext.proceed();
+      }
+   }
+
+   public static class MySecondInterceptor extends MyFirstInterceptor
+   {
+      @AroundInvoke
+      public Object doAround(InvocationContext invocationContext) throws Exception
+      {
+         InterceptorTestLogger.add(MySecondInterceptor.class, "aroundInvokeBefore");
+         Object result = invocationContext.proceed();
+         InterceptorTestLogger.add(MySecondInterceptor.class, "aroundInvokeAfter");
+         return result;
+      }
+
+      @PreDestroy
+      public Object doneHere(InvocationContext invocationContext) throws Exception
+      {
+         InterceptorTestLogger.add(MySecondInterceptor.class, "preDestroy");
+         return invocationContext.proceed();
+      }
+   }
 }
 




More information about the jboss-cvs-commits mailing list