FileDocCategorySizeDatePackage
StatefulReplicationInterceptor.javaAPI DocJBoss 4.2.16173Fri Jul 13 20:53:48 BST 2007org.jboss.ejb3.cache

StatefulReplicationInterceptor

public class StatefulReplicationInterceptor extends Object implements org.jboss.aop.advice.Interceptor
Replicate SFSB if it is modified.
author
Bill Burke
author
Brian Stansberry
version
$Revision: 60643 $

Fields Summary
private static final ThreadLocal
replicationContext
Constructors Summary
Methods Summary
public java.lang.StringgetName()

   
     
   
      return this.getClass().getName();
   
public java.lang.Objectinvoke(org.jboss.aop.joinpoint.Invocation invocation)

      // Find the ultimate parent context for the tree of SFSBs the target
      // bean is part of.  This "tree" could just be the bean itself, or
      // a multi-layer tree of nested SFSBs.
      StatefulContainerInvocation ejbInv = (StatefulContainerInvocation) invocation;
      StatefulBeanContext ctx = (StatefulBeanContext) ejbInv.getBeanContext();
      StatefulBeanContext root = ctx.getUltimateContainedIn();
      
      // Find out if the ultimate parent is clustered
      boolean clustered = false;
      StatefulContainer container = (StatefulContainer) root.getContainer();
      ClusteredStatefulCache clusteredCache = null;
      if (container.getCache() instanceof ClusteredStatefulCache)
      {
         clustered = true;
         clusteredCache = (ClusteredStatefulCache) container.getCache();
      }
      
      // Track nested calls to this tree so we know when the outer call
      // returns -- that's when we replicate
      if (clustered)         
         pushCallStack(root);
      
      boolean stackUnwound = false;
      Object rtn = null;
      try
      {
         rtn = invocation.invokeNext();
      }
      finally
      {
         stackUnwound = (clustered && isCallStackUnwound(root));
      }


      // We only replicate if the ultimate parent is clustered
      // TODO should we fail somehow during bean creation otherwise??
      boolean mustReplicate = clustered;
      
      // If the bean implements Optimized, we call isModified() even
      // if we know we won't replicate, as the bean might be expecting 
      // us to call the method
      Object obj = invocation.getTargetObject();
      if (obj instanceof Optimized)
      {
         if (((Optimized) obj).isModified() == false)
         {
            mustReplicate = false;
         }
      }
      
      if (mustReplicate)
      {
         // Mark the bean for replication. If the call stack is not
         // unwound yet this will tell the outer caller the tree is 
         // dirty even if the outer bean's isModified() returns false
         root.markedForReplication = true;
      }
      
      if (stackUnwound && root.markedForReplication)
      {
         clusteredCache.replicate(root);
      }
      
      if (ctx != root && ctx.markedForReplication)
      {
         // ctx is a ProxiedStatefulBeanContext that may have failed over
         // and needs to invalidate any remote nodes that hold stale refs
         // to their delegate. So we replicate it.
         container = (StatefulContainer) ctx.getContainer();
         StatefulCache cache = container.getCache();
         if (cache instanceof ClusteredStatefulCache)
         {
            clusteredCache = (ClusteredStatefulCache) cache;
            clusteredCache.replicate(ctx);
         }
         else
         {
            // not replicable
            ctx.markedForReplication = false;
         }
      }
      
      return rtn;
   
private static booleanisCallStackUnwound(org.jboss.ejb3.stateful.StatefulBeanContext ctx)

      Map<StatefulBeanContext, Stack<Boolean>> map = replicationContext.get();
      if (map == null)
      {
         throw new IllegalStateException("replicationContext contains no Map");
      }
      Stack<Boolean> callStack = map.get(ctx);
      if (callStack == null)
      {
         throw new IllegalStateException("replicationContext contains no call stack");
      }
      
      callStack.pop();
      boolean unwound = (callStack.size() == 0);
      
      if (unwound)
      {
         map.remove(ctx);
         if (map.size() == 0)
         {
            replicationContext.set(null);
         }
      }
      
      return unwound;
   
private static voidpushCallStack(org.jboss.ejb3.stateful.StatefulBeanContext ctx)

      Stack<Boolean> callStack = null;
      Map<StatefulBeanContext, Stack<Boolean>> map = replicationContext.get();
      if (map == null)
      {
         map = new HashMap<StatefulBeanContext, Stack<Boolean>>();
         replicationContext.set(map);
      }
      else
      {
         callStack = map.get(ctx);
      }

      if (callStack == null)
      {
         callStack = new Stack<Boolean>();
         map.put(ctx, callStack);
      }
      
      callStack.push(Boolean.TRUE);