[jbosscache-commits] JBoss Cache SVN: r7194 - core/trunk/src/main/java/org/jboss/cache/interceptors.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Nov 25 06:08:03 EST 2008


Author: manik.surtani at jboss.com
Date: 2008-11-25 06:08:02 -0500 (Tue, 25 Nov 2008)
New Revision: 7194

Modified:
   core/trunk/src/main/java/org/jboss/cache/interceptors/ActivationInterceptor.java
   core/trunk/src/main/java/org/jboss/cache/interceptors/LegacyActivationInterceptor.java
Log:
Passivation efficiency improvements

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/ActivationInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/ActivationInterceptor.java	2008-11-24 22:07:40 UTC (rev 7193)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/ActivationInterceptor.java	2008-11-25 11:08:02 UTC (rev 7194)
@@ -32,13 +32,7 @@
 import org.jboss.cache.commands.read.GetNodeCommand;
 import org.jboss.cache.commands.tx.OptimisticPrepareCommand;
 import org.jboss.cache.commands.tx.PrepareCommand;
-import org.jboss.cache.commands.write.ClearDataCommand;
-import org.jboss.cache.commands.write.MoveCommand;
-import org.jboss.cache.commands.write.PutDataMapCommand;
-import org.jboss.cache.commands.write.PutForExternalReadCommand;
-import org.jboss.cache.commands.write.PutKeyValueCommand;
-import org.jboss.cache.commands.write.RemoveKeyCommand;
-import org.jboss.cache.commands.write.RemoveNodeCommand;
+import org.jboss.cache.commands.write.*;
 import org.jboss.cache.factories.annotations.Inject;
 import org.jboss.cache.factories.annotations.Start;
 import org.jboss.cache.jmx.annotations.ManagedAttribute;
@@ -48,11 +42,7 @@
 
 import javax.transaction.SystemException;
 import javax.transaction.TransactionManager;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
@@ -63,366 +53,308 @@
  * @author <a href="mailto:{hmesha at novell.com}">{Hany Mesha}</a>
  * @version $Id$
  */
-public class ActivationInterceptor extends CacheLoaderInterceptor
-{
+public class ActivationInterceptor extends CacheLoaderInterceptor {
 
-   protected TransactionManager txMgr = null;
-   private long activations = 0;
-   ActivationModificationsBuilder builder;
+    protected TransactionManager txMgr = null;
+    private long activations = 0;
+    ActivationModificationsBuilder builder;
 
-   /**
-    * List<Transaction> that we have registered for
-    */
-   protected ConcurrentHashMap transactions = new ConcurrentHashMap(16);
-   protected static final Object NULL = new Object();
+    /**
+     * List<Transaction> that we have registered for
+     */
+    protected ConcurrentHashMap transactions = new ConcurrentHashMap(16);
+    protected static final Object NULL = new Object();
 
-   public ActivationInterceptor()
-   {
-      isActivation = true;
-      useCacheStore = false;
-   }
+    public ActivationInterceptor() {
+        isActivation = true;
+        useCacheStore = false;
+    }
 
-   @Inject
-   public void injectTransactionManager(TransactionManager txMgr)
-   {
-      this.txMgr = txMgr;
-   }
+    @Inject
+    public void injectTransactionManager(TransactionManager txMgr) {
+        this.txMgr = txMgr;
+    }
 
-   @Start
-   public void createModificationsBuilder()
-   {
-      builder = new ActivationModificationsBuilder();
-   }
+    @Start
+    public void createModificationsBuilder() {
+        builder = new ActivationModificationsBuilder();
+    }
 
-   @Override
-   public Object visitClearDataCommand(InvocationContext ctx, ClearDataCommand command) throws Throwable
-   {
-      Object returnValue = super.visitClearDataCommand(ctx, command);
-      if (trace)
-         log.trace("This is a remove data operation; removing the data from the loader, no activation processing needed.");
-      loader.removeData(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitClearDataCommand(InvocationContext ctx, ClearDataCommand command) throws Throwable {
+        Object returnValue = super.visitClearDataCommand(ctx, command);
+        if (trace)
+            log.trace("This is a remove data operation; removing the data from the loader, no activation processing needed.");
+        loader.removeData(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitRemoveNodeCommand(InvocationContext ctx, RemoveNodeCommand command) throws Throwable
-   {
-      Object returnValue = super.visitRemoveNodeCommand(ctx, command);
-      if (trace)
-         log.trace("This is a remove operation; removing the node from the loader, no activation processing needed.");
-      loader.remove(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitRemoveNodeCommand(InvocationContext ctx, RemoveNodeCommand command) throws Throwable {
+        Object returnValue = super.visitRemoveNodeCommand(ctx, command);
+        if (trace)
+            log.trace("This is a remove operation; removing the node from the loader, no activation processing needed.");
+        loader.remove(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitGetChildrenNamesCommand(InvocationContext ctx, GetChildrenNamesCommand command) throws Throwable
-   {
-      Object returnValue = super.visitGetChildrenNamesCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitGetChildrenNamesCommand(InvocationContext ctx, GetChildrenNamesCommand command) throws Throwable {
+        Object returnValue = super.visitGetChildrenNamesCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitGetKeysCommand(InvocationContext ctx, GetKeysCommand command) throws Throwable
-   {
-      Object returnValue = super.visitGetKeysCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitGetKeysCommand(InvocationContext ctx, GetKeysCommand command) throws Throwable {
+        Object returnValue = super.visitGetKeysCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitGetNodeCommand(InvocationContext ctx, GetNodeCommand command) throws Throwable
-   {
-      Object returnValue = super.visitGetNodeCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitGetNodeCommand(InvocationContext ctx, GetNodeCommand command) throws Throwable {
+        Object returnValue = super.visitGetNodeCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command) throws Throwable
-   {
-      Object returnValue = super.visitGetKeyValueCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command) throws Throwable {
+        Object returnValue = super.visitGetKeyValueCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitPutForExternalReadCommand(InvocationContext ctx, PutForExternalReadCommand command) throws Throwable
-   {
-      return visitPutKeyValueCommand(ctx, command);
-   }
+    @Override
+    public Object visitPutForExternalReadCommand(InvocationContext ctx, PutForExternalReadCommand command) throws Throwable {
+        return visitPutKeyValueCommand(ctx, command);
+    }
 
-   @Override
-   public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable
-   {
-      Object returnValue = super.visitPutKeyValueCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable {
+        Object returnValue = super.visitPutKeyValueCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitPutDataMapCommand(InvocationContext ctx, PutDataMapCommand command) throws Throwable
-   {
-      Object returnValue = super.visitPutDataMapCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitPutDataMapCommand(InvocationContext ctx, PutDataMapCommand command) throws Throwable {
+        Object returnValue = super.visitPutDataMapCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitRemoveKeyCommand(InvocationContext ctx, RemoveKeyCommand command) throws Throwable
-   {
-      Object returnValue = super.visitRemoveKeyCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitRemoveKeyCommand(InvocationContext ctx, RemoveKeyCommand command) throws Throwable {
+        Object returnValue = super.visitRemoveKeyCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitMoveCommand(InvocationContext ctx, MoveCommand command) throws Throwable
-   {
-      Object returnValue = super.visitMoveCommand(ctx, command);
-      if (trace)
-         log.trace("This is a move operation; removing the FROM node from the loader, no activation processing needed.");
-      loader.remove(command.getFqn());
-      removeNodeFromCacheLoader(command.getFqn().getParent());
-      removeNodeFromCacheLoader(command.getTo());
-      return returnValue;
-   }
+    @Override
+    public Object visitMoveCommand(InvocationContext ctx, MoveCommand command) throws Throwable {
+        Object returnValue = super.visitMoveCommand(ctx, command);
+        if (trace)
+            log.trace("This is a move operation; removing the FROM node from the loader, no activation processing needed.");
+        loader.remove(command.getFqn());
+        removeNodeFromCacheLoader(command.getFqn().getParent());
+        removeNodeFromCacheLoader(command.getTo());
+        return returnValue;
+    }
 
 
-   /**
-    * Remove the node from the cache loader if it exists in memory,
-    * its attributes have been initialized, its children have been loaded,
-    * AND it was found in the cache loader (nodeLoaded = true).
-    * Then notify the listeners that the node has been activated.
-    */
-   private void removeNodeFromCacheLoader(Fqn fqn) throws Throwable
-   {
-      if (fqn == null) return;
-      InternalNode n;
-      if (((n = dataContainer.peekInternalNode(fqn, true)) != null) && n.isDataLoaded() && loader.exists(fqn))
-      {
-         // node not null and attributes have been loaded?
-         if (n.hasChildren())
-         {
-            boolean result = childrenLoaded(n);
-            if (result)
-            {
-               log.debug("children all initialized");
-               remove(fqn);
+    /**
+     * Remove the node from the cache loader if it exists in memory,
+     * its attributes have been initialized, its children have been loaded,
+     * AND it was found in the cache loader (nodeLoaded = true).
+     */
+    private void removeNodeFromCacheLoader(Fqn fqn) throws Throwable {
+        if (fqn == null) return;
+        InternalNode n;
+        if (((n = dataContainer.peekInternalNode(fqn, true)) != null) && n.isDataLoaded()) {
+            // node not null and attributes have been loaded?
+            if (n.hasChildren()) {
+                boolean result = childrenLoaded(n);
+                if (result) {
+                    log.debug("children all initialized");
+                    remove(fqn);
+                }
+            } else if (loaderNoChildren(fqn)) {
+                if (log.isDebugEnabled()) log.debug("no children " + n);
+                remove(fqn);
             }
-         }
-         else if (loaderNoChildren(fqn))
-         {
-            if (log.isDebugEnabled()) log.debug("no children " + n);
-            remove(fqn);
-         }
-      }
-   }
+        }
+    }
 
-   private boolean childrenLoaded(InternalNode<?, ?> node)
-   {
-      if (!node.isChildrenLoaded())
-      {
-         return false;
-      }
-      for (InternalNode child : node.getChildren())
-      {
-         if (!child.isDataLoaded())
-         {
+    private boolean childrenLoaded(InternalNode<?, ?> node) {
+        if (!node.isChildrenLoaded()) {
             return false;
-         }
+        }
+        for (InternalNode child : node.getChildren()) {
+            if (!child.isDataLoaded()) {
+                return false;
+            }
 
-         if (child.hasChildren())
-         {
-            if (!childrenLoaded(child))
-               return false;
-         }
-         else if (!loaderNoChildren(child.getFqn()))
-         {
-            return false;
-         }
-      }
-      return true;
+            if (child.hasChildren()) {
+                if (!childrenLoaded(child))
+                    return false;
+            } else if (!loaderNoChildren(child.getFqn())) {
+                return false;
+            }
+        }
+        return true;
 
-   }
+    }
 
-   @Override
-   public Object visitOptimisticPrepareCommand(InvocationContext ctx, OptimisticPrepareCommand command) throws Throwable
-   {
-      Object retval = invokeNextInterceptor(ctx, command);
-      if (inTransaction())
-      {
-         prepareCacheLoader(ctx);
-      }
-      return retval;
-   }
+    @Override
+    public Object visitOptimisticPrepareCommand(InvocationContext ctx, OptimisticPrepareCommand command) throws Throwable {
+        Object retval = invokeNextInterceptor(ctx, command);
+        if (inTransaction()) {
+            prepareCacheLoader(ctx);
+        }
+        return retval;
+    }
 
-   private boolean inTransaction() throws SystemException
-   {
-      return txMgr != null && txMgr.getTransaction() != null;
-   }
+    private boolean inTransaction() throws SystemException {
+        return txMgr != null && txMgr.getTransaction() != null;
+    }
 
-   @Override
-   public Object visitPrepareCommand(InvocationContext ctx, PrepareCommand command) throws Throwable
-   {
-      Object retval = invokeNextInterceptor(ctx, command);
-      if (inTransaction())
-      {
-         prepareCacheLoader(ctx);
-      }
-      return retval;
-   }
+    @Override
+    public Object visitPrepareCommand(InvocationContext ctx, PrepareCommand command) throws Throwable {
+        Object retval = invokeNextInterceptor(ctx, command);
+        if (inTransaction()) {
+            prepareCacheLoader(ctx);
+        }
+        return retval;
+    }
 
-   private void remove(Fqn fqn) throws Exception
-   {
-      loader.remove(fqn);
-      if (getStatisticsEnabled()) activations++;
-   }
+    private void remove(Fqn fqn) throws Exception {
+        loader.remove(fqn);
+        if (getStatisticsEnabled()) activations++;
+    }
 
-   /**
-    * Returns true if the loader indicates no children for this node.
-    * Return false on error.
-    */
-   private boolean loaderNoChildren(Fqn fqn)
-   {
-      try
-      {
-         Set childrenNames = loader.getChildrenNames(fqn);
-         return (childrenNames == null);
-      }
-      catch (Exception e)
-      {
-         log.error("failed getting the children names for " + fqn + " from the cache loader", e);
-         return false;
-      }
-   }
+    /**
+     * Returns true if the loader indicates no children for this node.
+     * Return false on error.
+     */
+    private boolean loaderNoChildren(Fqn fqn) {
+        try {
+            Set childrenNames = loader.getChildrenNames(fqn);
+            return (childrenNames == null);
+        }
+        catch (Exception e) {
+            log.error("failed getting the children names for " + fqn + " from the cache loader", e);
+            return false;
+        }
+    }
 
-   private void prepareCacheLoader(InvocationContext ctx) throws Throwable
-   {
-      GlobalTransaction gtx = ctx.getGlobalTransaction();
-      TransactionContext tCtx = ctx.getTransactionContext();
-      if (tCtx == null)
-      {
-         throw new Exception("tCtx for transaction " + gtx + " not found in transaction table");
-      }
-      List<Modification> cacheLoaderModifications = new ArrayList<Modification>();
+    private void prepareCacheLoader(InvocationContext ctx) throws Throwable {
+        GlobalTransaction gtx = ctx.getGlobalTransaction();
+        TransactionContext tCtx = ctx.getTransactionContext();
+        if (tCtx == null) {
+            throw new Exception("tCtx for transaction " + gtx + " not found in transaction table");
+        }
+        List<Modification> cacheLoaderModifications = new ArrayList<Modification>();
 
-      builder.visitCollection(ctx, tCtx.getModifications());
-      if (cacheLoaderModifications.size() > 0)
-      {
-         loader.prepare(gtx, cacheLoaderModifications, false);
-      }
-   }
+        builder.visitCollection(ctx, tCtx.getModifications());
+        if (cacheLoaderModifications.size() > 0) {
+            loader.prepare(gtx, cacheLoaderModifications, false);
+        }
+    }
 
-   public class ActivationModificationsBuilder extends AbstractVisitor
-   {
+    public class ActivationModificationsBuilder extends AbstractVisitor {
 
-      private List<Modification> cacheLoaderModifications = new ArrayList<Modification>();
+        private List<Modification> cacheLoaderModifications = new ArrayList<Modification>();
 
-      private int txActs = 0;
+        private int txActs = 0;
 
-      @Override
-      public Object visitRemoveNodeCommand(InvocationContext ctx, RemoveNodeCommand removeNodeCommand) throws Throwable
-      {
-         Modification mod = new Modification(Modification.ModificationType.REMOVE_NODE, removeNodeCommand.getFqn());
-         cacheLoaderModifications.add(mod);
-         return null;
-      }
+        @Override
+        public Object visitRemoveNodeCommand(InvocationContext ctx, RemoveNodeCommand removeNodeCommand) throws Throwable {
+            Modification mod = new Modification(Modification.ModificationType.REMOVE_NODE, removeNodeCommand.getFqn());
+            cacheLoaderModifications.add(mod);
+            return null;
+        }
 
-      @Override
-      public Object visitPutDataMapCommand(InvocationContext ctx, PutDataMapCommand command) throws Throwable
-      {
-         Fqn fqn = command.getFqn();
-         handlePutCommand(ctx, fqn);
-         return null;
-      }
+        @Override
+        public Object visitPutDataMapCommand(InvocationContext ctx, PutDataMapCommand command) throws Throwable {
+            Fqn fqn = command.getFqn();
+            handlePutCommand(ctx, fqn);
+            return null;
+        }
 
-      @Override
-      public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable
-      {
-         Fqn fqn = command.getFqn();
-         handlePutCommand(ctx, fqn);
-         return null;
-      }
+        @Override
+        public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable {
+            Fqn fqn = command.getFqn();
+            handlePutCommand(ctx, fqn);
+            return null;
+        }
 
-      // On the way out, remove the node from the cache loader.
-      // Only remove the node if it exists in memory, its attributes have
-      // been initialized, its children have been loaded
-      // AND it was found in the cache loader (nodeLoaded = true).
-      // Then notify the listeners that the node has been activated.
-      private void handlePutCommand(InvocationContext ctx, Fqn fqn)
-            throws Exception
-      {
-         if (fqn != null && dataContainer.exists(fqn) && loader.exists(fqn))
-         {
-            InternalNode n = dataContainer.peekInternalNode(fqn, true);// don't load
-            // node not null and attributes have been loaded?
-            if (n != null && n.isDataLoaded())
-            {
-               // has children?
-               boolean result = childrenLoaded(n);
-               if (n.hasChildren() && result)
-               {
-                  // children have been loaded, remove the node
-                  addRemoveMod(ctx, cacheLoaderModifications, fqn, n.getData());
-                  txActs++;
-               }
-               // doesn't have children, check the cache loader
-               else if (loaderNoChildren(fqn))
-               {
-                  addRemoveMod(ctx, cacheLoaderModifications, fqn, n.getData());
-                  txActs++;
-               }
+        // On the way out, remove the node from the cache loader.
+        // Only remove the node if it exists in memory, its attributes have
+        // been initialized, its children have been loaded
+        // AND it was found in the cache loader (nodeLoaded = true).
+        // Then notify the listeners that the node has been activated.
+        private void handlePutCommand(InvocationContext ctx, Fqn fqn)
+                throws Exception {
+            if (fqn != null && dataContainer.exists(fqn) && loader.exists(fqn)) {
+                InternalNode n = dataContainer.peekInternalNode(fqn, true);// don't load
+                // node not null and attributes have been loaded?
+                if (n != null && n.isDataLoaded()) {
+                    // has children?
+                    boolean result = childrenLoaded(n);
+                    if (n.hasChildren() && result) {
+                        // children have been loaded, remove the node
+                        addRemoveMod(ctx, cacheLoaderModifications, fqn, n.getData());
+                        txActs++;
+                    }
+                    // doesn't have children, check the cache loader
+                    else if (loaderNoChildren(fqn)) {
+                        addRemoveMod(ctx, cacheLoaderModifications, fqn, n.getData());
+                        txActs++;
+                    }
+                }
             }
-         }
-      }
+        }
 
-      private boolean loaderNoChildren(Fqn fqn) throws Exception
-      {
-         return loader.getChildrenNames(fqn) != null;
-      }
+        private boolean loaderNoChildren(Fqn fqn) throws Exception {
+            return loader.getChildrenNames(fqn) != null;
+        }
 
-      private void addRemoveMod(InvocationContext ctx, List<Modification> l, Fqn fqn, Map data)
-      {
-         Modification mod = new Modification(Modification.ModificationType.REMOVE_NODE, fqn);
-         l.add(mod);
-         notifier.notifyNodeActivated(fqn, false, data, ctx);
-      }
+        private void addRemoveMod(InvocationContext ctx, List<Modification> l, Fqn fqn, Map data) {
+            Modification mod = new Modification(Modification.ModificationType.REMOVE_NODE, fqn);
+            l.add(mod);
+            notifier.notifyNodeActivated(fqn, false, data, ctx);
+        }
 
-      public List<Modification> getCacheLoaderModifications()
-      {
-         return cacheLoaderModifications;
-      }
+        public List<Modification> getCacheLoaderModifications() {
+            return cacheLoaderModifications;
+        }
 
-      public int getTxActs()
-      {
-         return txActs;
-      }
+        public int getTxActs() {
+            return txActs;
+        }
 
-   }
+    }
 
-   @ManagedAttribute(description = "number of cache node activations")
-   public long getActivations()
-   {
-      return activations;
-   }
+    @ManagedAttribute(description = "number of cache node activations")
+    public long getActivations() {
+        return activations;
+    }
 
-   @ManagedOperation
-   public void resetStatistics()
-   {
-      super.resetStatistics();
-      activations = 0;
-   }
+    @ManagedOperation
+    public void resetStatistics() {
+        super.resetStatistics();
+        activations = 0;
+    }
 
-   @ManagedOperation
-   public Map<String, Object> dumpStatistics()
-   {
-      Map<String, Object> retval = super.dumpStatistics();
-      if (retval == null)
-      {
-         retval = new HashMap<String, Object>();
-      }
-      retval.put("Activations", activations);
-      return retval;
-   }
+    @ManagedOperation
+    public Map<String, Object> dumpStatistics() {
+        Map<String, Object> retval = super.dumpStatistics();
+        if (retval == null) {
+            retval = new HashMap<String, Object>();
+        }
+        retval.put("Activations", activations);
+        return retval;
+    }
 }

Modified: core/trunk/src/main/java/org/jboss/cache/interceptors/LegacyActivationInterceptor.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/interceptors/LegacyActivationInterceptor.java	2008-11-24 22:07:40 UTC (rev 7193)
+++ core/trunk/src/main/java/org/jboss/cache/interceptors/LegacyActivationInterceptor.java	2008-11-25 11:08:02 UTC (rev 7194)
@@ -32,13 +32,7 @@
 import org.jboss.cache.commands.read.GetNodeCommand;
 import org.jboss.cache.commands.tx.OptimisticPrepareCommand;
 import org.jboss.cache.commands.tx.PrepareCommand;
-import org.jboss.cache.commands.write.ClearDataCommand;
-import org.jboss.cache.commands.write.MoveCommand;
-import org.jboss.cache.commands.write.PutDataMapCommand;
-import org.jboss.cache.commands.write.PutForExternalReadCommand;
-import org.jboss.cache.commands.write.PutKeyValueCommand;
-import org.jboss.cache.commands.write.RemoveKeyCommand;
-import org.jboss.cache.commands.write.RemoveNodeCommand;
+import org.jboss.cache.commands.write.*;
 import org.jboss.cache.factories.annotations.Inject;
 import org.jboss.cache.factories.annotations.Start;
 import org.jboss.cache.jmx.annotations.ManagedOperation;
@@ -47,11 +41,7 @@
 
 import javax.transaction.SystemException;
 import javax.transaction.TransactionManager;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
@@ -64,364 +54,306 @@
  * @deprecated will be removed along with optimistic and pessimistic locking.
  */
 @Deprecated
-public class LegacyActivationInterceptor extends LegacyCacheLoaderInterceptor
-{
+public class LegacyActivationInterceptor extends LegacyCacheLoaderInterceptor {
 
-   protected TransactionManager txMgr = null;
-   private long activations = 0;
-   ActivationModificationsBuilder builder;
+    protected TransactionManager txMgr = null;
+    private long activations = 0;
+    ActivationModificationsBuilder builder;
 
-   /**
-    * List<Transaction> that we have registered for
-    */
-   protected ConcurrentHashMap transactions = new ConcurrentHashMap(16);
-   protected static final Object NULL = new Object();
+    /**
+     * List<Transaction> that we have registered for
+     */
+    protected ConcurrentHashMap transactions = new ConcurrentHashMap(16);
+    protected static final Object NULL = new Object();
 
-   public LegacyActivationInterceptor()
-   {
-      isActivation = true;
-      useCacheStore = false;
-   }
+    public LegacyActivationInterceptor() {
+        isActivation = true;
+        useCacheStore = false;
+    }
 
-   @Inject
-   public void injectTransactionManager(TransactionManager txMgr)
-   {
-      this.txMgr = txMgr;
-   }
+    @Inject
+    public void injectTransactionManager(TransactionManager txMgr) {
+        this.txMgr = txMgr;
+    }
 
-   @Start
-   public void createModificationsBuilder()
-   {
-      builder = new ActivationModificationsBuilder();
-   }
+    @Start
+    public void createModificationsBuilder() {
+        builder = new ActivationModificationsBuilder();
+    }
 
-   @Override
-   public Object visitClearDataCommand(InvocationContext ctx, ClearDataCommand command) throws Throwable
-   {
-      Object returnValue = super.visitClearDataCommand(ctx, command);
-      if (trace)
-         log.trace("This is a remove data operation; removing the data from the loader, no activation processing needed.");
-      loader.removeData(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitClearDataCommand(InvocationContext ctx, ClearDataCommand command) throws Throwable {
+        Object returnValue = super.visitClearDataCommand(ctx, command);
+        if (trace)
+            log.trace("This is a remove data operation; removing the data from the loader, no activation processing needed.");
+        loader.removeData(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitRemoveNodeCommand(InvocationContext ctx, RemoveNodeCommand command) throws Throwable
-   {
-      Object returnValue = super.visitRemoveNodeCommand(ctx, command);
-      if (trace)
-         log.trace("This is a remove operation; removing the node from the loader, no activation processing needed.");
-      loader.remove(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitRemoveNodeCommand(InvocationContext ctx, RemoveNodeCommand command) throws Throwable {
+        Object returnValue = super.visitRemoveNodeCommand(ctx, command);
+        if (trace)
+            log.trace("This is a remove operation; removing the node from the loader, no activation processing needed.");
+        loader.remove(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitGetChildrenNamesCommand(InvocationContext ctx, GetChildrenNamesCommand command) throws Throwable
-   {
-      Object returnValue = super.visitGetChildrenNamesCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitGetChildrenNamesCommand(InvocationContext ctx, GetChildrenNamesCommand command) throws Throwable {
+        Object returnValue = super.visitGetChildrenNamesCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitGetKeysCommand(InvocationContext ctx, GetKeysCommand command) throws Throwable
-   {
-      Object returnValue = super.visitGetKeysCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitGetKeysCommand(InvocationContext ctx, GetKeysCommand command) throws Throwable {
+        Object returnValue = super.visitGetKeysCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitGetNodeCommand(InvocationContext ctx, GetNodeCommand command) throws Throwable
-   {
-      Object returnValue = super.visitGetNodeCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitGetNodeCommand(InvocationContext ctx, GetNodeCommand command) throws Throwable {
+        Object returnValue = super.visitGetNodeCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command) throws Throwable
-   {
-      Object returnValue = super.visitGetKeyValueCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitGetKeyValueCommand(InvocationContext ctx, GetKeyValueCommand command) throws Throwable {
+        Object returnValue = super.visitGetKeyValueCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitPutForExternalReadCommand(InvocationContext ctx, PutForExternalReadCommand command) throws Throwable
-   {
-      return visitPutKeyValueCommand(ctx, command);
-   }
+    @Override
+    public Object visitPutForExternalReadCommand(InvocationContext ctx, PutForExternalReadCommand command) throws Throwable {
+        return visitPutKeyValueCommand(ctx, command);
+    }
 
-   @Override
-   public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable
-   {
-      Object returnValue = super.visitPutKeyValueCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable {
+        Object returnValue = super.visitPutKeyValueCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitPutDataMapCommand(InvocationContext ctx, PutDataMapCommand command) throws Throwable
-   {
-      Object returnValue = super.visitPutDataMapCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitPutDataMapCommand(InvocationContext ctx, PutDataMapCommand command) throws Throwable {
+        Object returnValue = super.visitPutDataMapCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitRemoveKeyCommand(InvocationContext ctx, RemoveKeyCommand command) throws Throwable
-   {
-      Object returnValue = super.visitRemoveKeyCommand(ctx, command);
-      removeNodeFromCacheLoader(command.getFqn());
-      return returnValue;
-   }
+    @Override
+    public Object visitRemoveKeyCommand(InvocationContext ctx, RemoveKeyCommand command) throws Throwable {
+        Object returnValue = super.visitRemoveKeyCommand(ctx, command);
+        removeNodeFromCacheLoader(command.getFqn());
+        return returnValue;
+    }
 
-   @Override
-   public Object visitMoveCommand(InvocationContext ctx, MoveCommand command) throws Throwable
-   {
-      Object returnValue = super.visitMoveCommand(ctx, command);
-      if (trace)
-         log.trace("This is a move operation; removing the FROM node from the loader, no activation processing needed.");
-      loader.remove(command.getFqn());
-      removeNodeFromCacheLoader(command.getFqn().getParent());
-      removeNodeFromCacheLoader(command.getTo());
-      return returnValue;
-   }
+    @Override
+    public Object visitMoveCommand(InvocationContext ctx, MoveCommand command) throws Throwable {
+        Object returnValue = super.visitMoveCommand(ctx, command);
+        if (trace)
+            log.trace("This is a move operation; removing the FROM node from the loader, no activation processing needed.");
+        loader.remove(command.getFqn());
+        removeNodeFromCacheLoader(command.getFqn().getParent());
+        removeNodeFromCacheLoader(command.getTo());
+        return returnValue;
+    }
 
 
-   /**
-    * Remove the node from the cache loader if it exists in memory,
-    * its attributes have been initialized, its children have been loaded,
-    * AND it was found in the cache loader (nodeLoaded = true).
-    * Then notify the listeners that the node has been activated.
-    */
-   private void removeNodeFromCacheLoader(Fqn fqn) throws Throwable
-   {
-      NodeSPI n;
-      if (((n = dataContainer.peek(fqn, true, false)) != null) && n.isDataLoaded() && loader.exists(fqn))
-      {
-         // node not null and attributes have been loaded?
-         if (!n.getChildrenDirect().isEmpty())
-         {
-            boolean result = childrenLoaded(n);
-            if (result)
-            {
-               log.debug("children all initialized");
-               remove(fqn);
+    /**
+     * Remove the node from the cache loader if it exists in memory,
+     * its attributes have been initialized, its children have been loaded,
+     * AND it was found in the cache loader (nodeLoaded = true).
+     */
+    private void removeNodeFromCacheLoader(Fqn fqn) throws Throwable {
+        NodeSPI n;
+        if (((n = dataContainer.peek(fqn, true, false)) != null) && n.isDataLoaded()) {
+            // node not null and attributes have been loaded?
+            if (!n.getChildrenDirect().isEmpty()) {
+                boolean result = childrenLoaded(n);
+                if (result) {
+                    log.debug("children all initialized");
+                    remove(fqn);
+                }
+            } else if (loaderNoChildren(fqn)) {
+                if (log.isDebugEnabled()) log.debug("no children " + n);
+                remove(fqn);
             }
-         }
-         else if (loaderNoChildren(fqn))
-         {
-            if (log.isDebugEnabled()) log.debug("no children " + n);
-            remove(fqn);
-         }
-      }
-   }
+        }
+    }
 
-   private boolean childrenLoaded(NodeSPI<?, ?> node)
-   {
-      if (!node.isChildrenLoaded())
-      {
-         return false;
-      }
-      for (NodeSPI child : node.getChildrenDirect())
-      {
-         if (!child.isDataLoaded())
-         {
+    private boolean childrenLoaded(NodeSPI<?, ?> node) {
+        if (!node.isChildrenLoaded()) {
             return false;
-         }
+        }
+        for (NodeSPI child : node.getChildrenDirect()) {
+            if (!child.isDataLoaded()) {
+                return false;
+            }
 
-         if (child.getChildrenDirect().size() > 0)
-         {
-            if (!childrenLoaded(child))
-               return false;
-         }
-         else if (!loaderNoChildren(child.getFqn()))
-         {
-            return false;
-         }
-      }
-      return true;
+            if (child.getChildrenDirect().size() > 0) {
+                if (!childrenLoaded(child))
+                    return false;
+            } else if (!loaderNoChildren(child.getFqn())) {
+                return false;
+            }
+        }
+        return true;
 
-   }
+    }
 
-   @Override
-   public Object visitOptimisticPrepareCommand(InvocationContext ctx, OptimisticPrepareCommand command) throws Throwable
-   {
-      Object retval = invokeNextInterceptor(ctx, command);
-      if (inTransaction())
-      {
-         prepareCacheLoader(ctx);
-      }
-      return retval;
-   }
+    @Override
+    public Object visitOptimisticPrepareCommand(InvocationContext ctx, OptimisticPrepareCommand command) throws Throwable {
+        Object retval = invokeNextInterceptor(ctx, command);
+        if (inTransaction()) {
+            prepareCacheLoader(ctx);
+        }
+        return retval;
+    }
 
-   private boolean inTransaction() throws SystemException
-   {
-      return txMgr != null && txMgr.getTransaction() != null;
-   }
+    private boolean inTransaction() throws SystemException {
+        return txMgr != null && txMgr.getTransaction() != null;
+    }
 
-   @Override
-   public Object visitPrepareCommand(InvocationContext ctx, PrepareCommand command) throws Throwable
-   {
-      Object retval = invokeNextInterceptor(ctx, command);
-      if (inTransaction())
-      {
-         prepareCacheLoader(ctx);
-      }
-      return retval;
-   }
+    @Override
+    public Object visitPrepareCommand(InvocationContext ctx, PrepareCommand command) throws Throwable {
+        Object retval = invokeNextInterceptor(ctx, command);
+        if (inTransaction()) {
+            prepareCacheLoader(ctx);
+        }
+        return retval;
+    }
 
-   private void remove(Fqn fqn) throws Exception
-   {
-      loader.remove(fqn);
-      if (getStatisticsEnabled()) activations++;
-   }
+    private void remove(Fqn fqn) throws Exception {
+        loader.remove(fqn);
+        if (getStatisticsEnabled()) activations++;
+    }
 
-   /**
-    * Returns true if the loader indicates no children for this node.
-    * Return false on error.
-    */
-   private boolean loaderNoChildren(Fqn fqn)
-   {
-      try
-      {
-         Set childrenNames = loader.getChildrenNames(fqn);
-         return (childrenNames == null);
-      }
-      catch (Exception e)
-      {
-         log.error("failed getting the children names for " + fqn + " from the cache loader", e);
-         return false;
-      }
-   }
+    /**
+     * Returns true if the loader indicates no children for this node.
+     * Return false on error.
+     */
+    private boolean loaderNoChildren(Fqn fqn) {
+        try {
+            Set childrenNames = loader.getChildrenNames(fqn);
+            return (childrenNames == null);
+        }
+        catch (Exception e) {
+            log.error("failed getting the children names for " + fqn + " from the cache loader", e);
+            return false;
+        }
+    }
 
-   public long getActivations()
-   {
-      return activations;
-   }
+    public long getActivations() {
+        return activations;
+    }
 
-   private void prepareCacheLoader(InvocationContext ctx) throws Throwable
-   {
-      GlobalTransaction gtx = ctx.getGlobalTransaction();
-      TransactionContext tCtx = ctx.getTransactionContext();
-      if (tCtx == null)
-      {
-         throw new Exception("tCtx for transaction " + gtx + " not found in transaction table");
-      }
-      List<Modification> cacheLoaderModifications = new ArrayList<Modification>();
+    private void prepareCacheLoader(InvocationContext ctx) throws Throwable {
+        GlobalTransaction gtx = ctx.getGlobalTransaction();
+        TransactionContext tCtx = ctx.getTransactionContext();
+        if (tCtx == null) {
+            throw new Exception("tCtx for transaction " + gtx + " not found in transaction table");
+        }
+        List<Modification> cacheLoaderModifications = new ArrayList<Modification>();
 
-      builder.visitCollection(ctx, tCtx.getModifications());
-      if (cacheLoaderModifications.size() > 0)
-      {
-         loader.prepare(gtx, cacheLoaderModifications, false);
-      }
-   }
+        builder.visitCollection(ctx, tCtx.getModifications());
+        if (cacheLoaderModifications.size() > 0) {
+            loader.prepare(gtx, cacheLoaderModifications, false);
+        }
+    }
 
-   public class ActivationModificationsBuilder extends AbstractVisitor
-   {
+    public class ActivationModificationsBuilder extends AbstractVisitor {
 
-      private List<Modification> cacheLoaderModifications = new ArrayList<Modification>();
+        private List<Modification> cacheLoaderModifications = new ArrayList<Modification>();
 
-      private int txActs = 0;
+        private int txActs = 0;
 
-      @Override
-      public Object visitRemoveNodeCommand(InvocationContext ctx, RemoveNodeCommand removeNodeCommand) throws Throwable
-      {
-         Modification mod = new Modification(Modification.ModificationType.REMOVE_NODE, removeNodeCommand.getFqn());
-         cacheLoaderModifications.add(mod);
-         return null;
-      }
+        @Override
+        public Object visitRemoveNodeCommand(InvocationContext ctx, RemoveNodeCommand removeNodeCommand) throws Throwable {
+            Modification mod = new Modification(Modification.ModificationType.REMOVE_NODE, removeNodeCommand.getFqn());
+            cacheLoaderModifications.add(mod);
+            return null;
+        }
 
-      @Override
-      public Object visitPutDataMapCommand(InvocationContext ctx, PutDataMapCommand command) throws Throwable
-      {
-         Fqn fqn = command.getFqn();
-         handlePutCommand(ctx, fqn);
-         return null;
-      }
+        @Override
+        public Object visitPutDataMapCommand(InvocationContext ctx, PutDataMapCommand command) throws Throwable {
+            Fqn fqn = command.getFqn();
+            handlePutCommand(ctx, fqn);
+            return null;
+        }
 
-      @Override
-      public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable
-      {
-         Fqn fqn = command.getFqn();
-         handlePutCommand(ctx, fqn);
-         return null;
-      }
+        @Override
+        public Object visitPutKeyValueCommand(InvocationContext ctx, PutKeyValueCommand command) throws Throwable {
+            Fqn fqn = command.getFqn();
+            handlePutCommand(ctx, fqn);
+            return null;
+        }
 
-      // On the way out, remove the node from the cache loader.
-      // Only remove the node if it exists in memory, its attributes have
-      // been initialized, its children have been loaded
-      // AND it was found in the cache loader (nodeLoaded = true).
-      // Then notify the listeners that the node has been activated.
-      private void handlePutCommand(InvocationContext ctx, Fqn fqn)
-            throws Exception
-      {
-         if (fqn != null && dataContainer.peek(fqn, false, false) != null && loader.exists(fqn))
-         {
-            NodeSPI n = dataContainer.peek(fqn, true, false);// don't load
-            // node not null and attributes have been loaded?
-            if (n != null && n.isDataLoaded())
-            {
-               // has children?
-               boolean result = childrenLoaded(n);
-               if (!n.getChildrenDirect().isEmpty() && result)
-               {
-                  // children have been loaded, remove the node
-                  addRemoveMod(ctx, cacheLoaderModifications, fqn, n.getDataDirect());
-                  txActs++;
-               }
-               // doesn't have children, check the cache loader
-               else if (loaderNoChildren(fqn))
-               {
-                  addRemoveMod(ctx, cacheLoaderModifications, fqn, n.getDataDirect());
-                  txActs++;
-               }
+        // On the way out, remove the node from the cache loader.
+        // Only remove the node if it exists in memory, its attributes have
+        // been initialized, its children have been loaded
+        // AND it was found in the cache loader (nodeLoaded = true).
+        // Then notify the listeners that the node has been activated.
+        private void handlePutCommand(InvocationContext ctx, Fqn fqn)
+                throws Exception {
+            if (fqn != null && dataContainer.peek(fqn, false, false) != null && loader.exists(fqn)) {
+                NodeSPI n = dataContainer.peek(fqn, true, false);// don't load
+                // node not null and attributes have been loaded?
+                if (n != null && n.isDataLoaded()) {
+                    // has children?
+                    boolean result = childrenLoaded(n);
+                    if (!n.getChildrenDirect().isEmpty() && result) {
+                        // children have been loaded, remove the node
+                        addRemoveMod(ctx, cacheLoaderModifications, fqn, n.getDataDirect());
+                        txActs++;
+                    }
+                    // doesn't have children, check the cache loader
+                    else if (loaderNoChildren(fqn)) {
+                        addRemoveMod(ctx, cacheLoaderModifications, fqn, n.getDataDirect());
+                        txActs++;
+                    }
+                }
             }
-         }
-      }
+        }
 
-      private boolean loaderNoChildren(Fqn fqn) throws Exception
-      {
-         return loader.getChildrenNames(fqn) != null;
-      }
+        private boolean loaderNoChildren(Fqn fqn) throws Exception {
+            return loader.getChildrenNames(fqn) != null;
+        }
 
-      private void addRemoveMod(InvocationContext ctx, List<Modification> l, Fqn fqn, Map data)
-      {
-         Modification mod = new Modification(Modification.ModificationType.REMOVE_NODE, fqn);
-         l.add(mod);
-         notifier.notifyNodeActivated(fqn, false, data, ctx);
-      }
+        private void addRemoveMod(InvocationContext ctx, List<Modification> l, Fqn fqn, Map data) {
+            Modification mod = new Modification(Modification.ModificationType.REMOVE_NODE, fqn);
+            l.add(mod);
+            notifier.notifyNodeActivated(fqn, false, data, ctx);
+        }
 
-      public List<Modification> getCacheLoaderModifications()
-      {
-         return cacheLoaderModifications;
-      }
+        public List<Modification> getCacheLoaderModifications() {
+            return cacheLoaderModifications;
+        }
 
-      public int getTxActs()
-      {
-         return txActs;
-      }
+        public int getTxActs() {
+            return txActs;
+        }
 
-   }
+    }
 
-   @ManagedOperation
-   public void resetStatistics()
-   {
-      super.resetStatistics();
-      activations = 0;
-   }
+    @ManagedOperation
+    public void resetStatistics() {
+        super.resetStatistics();
+        activations = 0;
+    }
 
-   @ManagedOperation
-   public Map<String, Object> dumpStatistics()
-   {
-      Map<String, Object> retval = super.dumpStatistics();
-      if (retval == null)
-      {
-         retval = new HashMap<String, Object>();
-      }
-      retval.put("Activations", activations);
-      return retval;
-   }
+    @ManagedOperation
+    public Map<String, Object> dumpStatistics() {
+        Map<String, Object> retval = super.dumpStatistics();
+        if (retval == null) {
+            retval = new HashMap<String, Object>();
+        }
+        retval.put("Activations", activations);
+        return retval;
+    }
 }
\ No newline at end of file




More information about the jbosscache-commits mailing list