[jbosscache-commits] JBoss Cache SVN: r5640 - in core/trunk/src: main/java/org/jboss/cache/invocation and 1 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Wed Apr 23 07:14:17 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-04-23 07:14:17 -0400 (Wed, 23 Apr 2008)
New Revision: 5640

Added:
   core/trunk/src/test/java/org/jboss/cache/interceptors/LegacyInterceptorTest.java
Modified:
   core/trunk/src/main/java/org/jboss/cache/interceptors/Interceptor.java
   core/trunk/src/main/java/org/jboss/cache/invocation/InterceptorChain.java
Log:
Compatibility with legacy interceptors

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/Interceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/Interceptor.java	2008-04-23 10:33:06 UTC (rev 5639)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/Interceptor.java	2008-04-23 11:14:17 UTC (rev 5640)
@@ -22,20 +22,11 @@
 package org.jboss.cache.interceptors;
 
 import org.apache.commons.logging.Log;
-import org.jboss.cache.CacheException;
 import org.jboss.cache.CacheSPI;
 import org.jboss.cache.Fqn;
 import org.jboss.cache.InvocationContext;
 import org.jboss.cache.NodeSPI;
 import org.jboss.cache.commands.CacheCommand;
-import org.jboss.cache.commands.cachedata.*;
-import org.jboss.cache.commands.channel.BlockChannelCommand;
-import org.jboss.cache.commands.channel.UnblockChannelCommand;
-import org.jboss.cache.commands.remote.*;
-import org.jboss.cache.commands.tx.CommitCommand;
-import org.jboss.cache.commands.tx.OptimisticPrepareCommand;
-import org.jboss.cache.commands.tx.PrepareCommand;
-import org.jboss.cache.commands.tx.RollbackCommand;
 import org.jboss.cache.config.Configuration;
 import org.jboss.cache.factories.annotations.Inject;
 import org.jboss.cache.factories.annotations.Start;
@@ -52,26 +43,15 @@
  */
 public abstract class Interceptor extends ChainedInterceptor implements InterceptorMBean
 {
-   protected Interceptor next = null;
    protected CacheSPI<?, ?> cache;
    protected Log log = null;
    protected Configuration configuration;
    private boolean statsEnabled = false;
    protected boolean trace;
 
-
-   public void setNext(Interceptor i)
-   {
-      next = i;
-   }
-
-   public Interceptor getNext()
-   {
-      return next;
-   }
-
    public void setCache(CacheSPI cache)
    {
+      this.cache = cache;
    }
 
    @Start
@@ -105,7 +85,8 @@
     */
    public Object nextInterceptor(InvocationContext ctx) throws Throwable
    {
-      return next.invoke(ctx);
+//      return getNext().invoke(ctx);
+      return invokeNextInterceptor(ctx, null);
    }
 
    public boolean getStatisticsEnabled()
@@ -180,105 +161,13 @@
 
    public Object handleDefault(InvocationContext ctx, CacheCommand command) throws Throwable
    {
-      if (command != null) //call originated from a command
+      if (command != null) //call originated from a command's accept() method.
       {
          return invoke(ctx);
       }
-      //this means that another Interceptor called this method, we have to dispatch the call to the appropriate method
-      int methodId = ctx.getMethodCall().getId();
-      switch (methodId)
-      {
 
-         case PutDataMapCommand.ERASE_METHOD_ID:
-         case PutDataMapCommand.ERASE_VERSIONED_METHOD_ID:
-         case PutDataMapCommand.METHOD_ID:
-         case PutDataMapCommand.VERSIONED_METHOD_ID:
-            return next.handlePutDataMapCommand(ctx, (PutDataMapCommand) command);
-
-         case PutKeyValueCommand.METHOD_ID:
-         case PutKeyValueCommand.VERSIONED_METHOD_ID:
-         case PutKeyValueCommand.PUT_FOR_EXT_READ_METHOD_ID:
-         case PutKeyValueCommand.PUT_FOR_EXT_READ_VERSIONED_METHOD_ID:
-            return next.handlePutKeyValueCommand(ctx, (PutKeyValueCommand) command);
-
-         case RemoveNodeCommand.METHOD_ID:
-         case RemoveNodeCommand.VERSIONED_METHOD_ID:
-            return next.handleRemoveNodeCommand(ctx, (RemoveNodeCommand) command);
-
-         case RemoveDataCommand.METHOD_ID:
-         case RemoveDataCommand.VERSIONED_METHOD_ID:
-            return next.handleRemoveDataCommand(ctx, (RemoveDataCommand) command);
-
-         case EvictNodeCommand.METHOD_ID:
-         case EvictNodeCommand.VERSIONED_METHOD_ID:
-            return next.handleEvictFqnCommand(ctx, (EvictNodeCommand) command);
-
-         case InvalidateCommand.METHOD_ID:
-            return next.handleInvalidateCommand(ctx, (InvalidateCommand) command);
-
-         case RemoveKeyCommand.METHOD_ID:
-         case RemoveKeyCommand.VERSIONED_METHOD_ID:
-            return next.handleRemoveKeyCommand(ctx, (RemoveKeyCommand) command);
-
-         case GetDataMapCommand.METHOD_ID:
-            return next.handleGetDataMapCommand(ctx, (GetDataMapCommand) command);
-
-         case RemoteExistsNodeCommand.METHOD_ID:
-            return next.handleExistsNodeCommand(ctx, (RemoteExistsNodeCommand) command);
-
-         case GetKeyValueCommand.METHOD_ID:
-            return next.handleGetKeyValueCommand(ctx, (GetKeyValueCommand) command);
-
-         case GetNodeCommand.METHOD_ID:
-            return next.handleGetNodeCommand(ctx, (GetNodeCommand) command);
-
-         case GetKeysCommand.METHOD_ID:
-            return next.handleGetKeysCommand(ctx, (GetKeysCommand) command);
-
-         case GetChildrenNamesCommand.METHOD_ID:
-            return next.handleGetChildrenNamesCommand(ctx, (GetChildrenNamesCommand) command);
-
-         case MoveCommand.METHOD_ID:
-            return next.handleMoveCommand(ctx, (MoveCommand) command);
-
-         case GravitateDataCommand.METHOD_ID:
-            return next.handleGravitateDataCommand(ctx, (GravitateDataCommand) command);
-
-         case PrepareCommand.METHOD_ID:
-            return next.handlePrepareCommand(ctx, (PrepareCommand) command);
-
-         case RollbackCommand.METHOD_ID:
-            return next.handleRollbackCommand(ctx, (RollbackCommand) command);
-
-         case CommitCommand.METHOD_ID:
-            return next.handleCommitCommand(ctx, (CommitCommand) command);
-
-         case OptimisticPrepareCommand.METHOD_ID:
-            return next.handleOptimisticPrepareCommand(ctx, (OptimisticPrepareCommand) command);
-
-         case ReplicateCommand.SINGLE_METHOD_ID:
-         case ReplicateCommand.MULTIPLE_METHOD_ID:
-            throw new CacheException("Replicate calls should never be passed up the interceptor chain!");
-
-         case AnnounceBuddyPoolNameCommand.METHOD_ID:
-            return next.handleAnnounceBuddyPoolName(ctx, (AnnounceBuddyPoolNameCommand) command);
-
-         case RemoveFromBuddyGroupCommand.METHOD_ID:
-            return next.handleRemoveFromBuddyGroupCommand(ctx, (RemoveFromBuddyGroupCommand) command);
-
-         case AssignToBuddyGroupCommand.METHOD_ID:
-            return next.handleAssignToBuddyGroupCommand(ctx, (AssignToBuddyGroupCommand) command);
-
-         case DataGravitationCleanupCommand.METHOD_ID:
-            return next.handleDataGravitationCleanupCommand(ctx, (DataGravitationCleanupCommand) command);
-
-         case BlockChannelCommand.METHOD_ID:
-            return next.handleBlockChannelCommand(ctx, (BlockChannelCommand) command);
-
-         case UnblockChannelCommand.METHOD_ID:
-            return next.handleUnblockChannelCommand(ctx, (UnblockChannelCommand) command);
-
-      }
-      return null;
+      //this means that another Interceptor called this method, we have to dispatch the call to the appropriate method.  Probably called directly using super.invoke().
+      command = ctx.getExecutingCommand();
+      return command.accept(ctx, getNext());
    }
 }

Modified: core/trunk/src/main/java/org/jboss/cache/invocation/InterceptorChain.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/invocation/InterceptorChain.java	2008-04-23 10:33:06 UTC (rev 5639)
+++ core/trunk/src/main/java/org/jboss/cache/invocation/InterceptorChain.java	2008-04-23 11:14:17 UTC (rev 5640)
@@ -5,8 +5,10 @@
 import org.jboss.cache.CacheException;
 import org.jboss.cache.InvocationContext;
 import org.jboss.cache.commands.CacheCommand;
+import org.jboss.cache.commands.functional.MarshallableCommand;
 import org.jboss.cache.factories.annotations.Inject;
 import org.jboss.cache.interceptors.base.ChainedInterceptor;
+import org.jboss.cache.marshall.MethodCall;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -227,9 +229,12 @@
    /**
     * Walks the command through the interceptor chain. The received ctx is being passed in.
     */
+   @SuppressWarnings("deprecated")
    public Object invoke(InvocationContext ctx, CacheCommand command)
    {
       ctx.setExecutingCommand(command);
+      // ensure legacy MethodCall is available for interceptor implementations.
+      ctx.setMethodCall(createMethodCall((MarshallableCommand) command));
       try
       {
          return command.accept(ctx, firstInChain);
@@ -248,6 +253,15 @@
       }
    }
 
+   @SuppressWarnings("deprecated")
+   private MethodCall createMethodCall(MarshallableCommand command)
+   {
+      MethodCall call = new MethodCall();
+      call.setMethodId(command.getCommandId());
+      call.setArgs(command.getParameters());
+      return call;
+   }
+
    /**
     * Similar to {@link #invoke(org.jboss.cache.InvocationContext, org.jboss.cache.commands.CacheCommand)}, but
     * constructs a invocation context on the fly, using {@link InvocationContextContainer#get()}

Added: core/trunk/src/test/java/org/jboss/cache/interceptors/LegacyInterceptorTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/interceptors/LegacyInterceptorTest.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/interceptors/LegacyInterceptorTest.java	2008-04-23 11:14:17 UTC (rev 5640)
@@ -0,0 +1,108 @@
+package org.jboss.cache.interceptors;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.InvocationContext;
+import org.jboss.cache.commands.cachedata.PutKeyValueCommand;
+import org.jboss.cache.misc.TestingUtil;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+
+/**
+ * This is to test that "old-style" interceptors from 2.0.x and 2.1.x will work with the new interceptor structure.
+ *
+ * @author Manik Surtani (<a href="mailto:manik at jboss.org">manik at jboss.org</a>)
+ * @since 2.2.0
+ */
+ at Test(groups = "functional")
+public class LegacyInterceptorTest
+{
+   Cache cache;
+   static CountDownLatch interceptorResumeLatch, interceptorInvokedLatch;
+   TestInterceptor testInterceptor;
+   Executor testRunner;
+
+   @BeforeTest
+   public void setUp()
+   {
+      cache = new DefaultCacheFactory().createCache();
+      testInterceptor = new TestInterceptor();
+
+      ((CacheSPI) cache).addInterceptor(testInterceptor, TxInterceptor.class);
+      testRunner = Executors.newSingleThreadExecutor();
+   }
+
+   @BeforeMethod
+   public void createLatches()
+   {
+      interceptorResumeLatch = new CountDownLatch(1);
+      interceptorInvokedLatch = new CountDownLatch(1);
+   }
+
+   @AfterTest
+   public void tearDown()
+   {
+      TestingUtil.killCaches(cache);
+   }
+
+   public void testPut() throws Exception
+   {
+      testRunner.execute(new Runnable()
+      {
+         public void run()
+         {
+            cache.put("/a", "k", "v");
+         }
+      });
+
+      interceptorInvokedLatch.await();
+
+      // check that the context on the test interceptor is correct.
+      InvocationContext ctx = testInterceptor.ctx;
+      interceptorResumeLatch.countDown();
+
+      assert ctx.getMethodCall().getMethodId() == PutKeyValueCommand.METHOD_ID;
+      assert ctx.getMethodCall().getArgs()[0] == null; // gtx
+      assert ctx.getMethodCall().getArgs()[1].equals(Fqn.fromString("/a")); // fqn
+      assert ctx.getMethodCall().getArgs()[2].equals("k"); // key
+      assert ctx.getMethodCall().getArgs()[3].equals("v"); // value
+      assert ctx.getMethodCall().getArgs()[4] == Boolean.FALSE; //last boolean value
+   }
+
+   public static class TestInterceptor extends Interceptor
+   {
+      InvocationContext ctx;
+
+      @Override
+      public Object invoke(InvocationContext ctx) throws Throwable
+      {
+         if (ctx.isOriginLocal())
+         {
+            // copy the context so tests can inspect it
+            this.ctx = ctx;
+
+            // signal to the test that this has been invoked.
+            interceptorInvokedLatch.countDown();
+
+            // wait for tests to finish
+            interceptorResumeLatch.await();
+
+            // wipe class-level context variable
+            this.ctx = null;
+         }
+
+         // the "old-style" of passing up the interceptor chain
+         return super.invoke(ctx);
+      }
+   }
+
+}
+




More information about the jbosscache-commits mailing list