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.setMarkedForReplication(true);
}
if (stackUnwound && root.isMarkedForReplication())
{
clusteredCache.replicate(root);
}
if (ctx != root && ctx.isMarkedForReplication())
{
// 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);
}