[exo-jcr-commits] exo-jcr SVN: r1319 - jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/jbosscache.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Jan 8 04:53:53 EST 2010


Author: nfilotto
Date: 2010-01-08 04:53:53 -0500 (Fri, 08 Jan 2010)
New Revision: 1319

Modified:
   jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/jbosscache/BufferedJbossCache.java
Log:
EXOJCR-371: Apply some remarks such as:
1. Instead of using "IllegalStateException", we use UnsupportedOperationException since it is more appropriate
2. ChangesContainer.compareTo should be implemented this way. We first compare the FQN (as you do) if they are equals, we return always -1
3. In commitTransaction, we make it safer with a try/finally block
4. We use an enumeration instead of an Interface for the ChangesType

Modified: jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/jbosscache/BufferedJbossCache.java
===================================================================
--- jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/jbosscache/BufferedJbossCache.java	2010-01-07 21:14:03 UTC (rev 1318)
+++ jcr/branches/1.12.0-JBCCACHE/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/jbosscache/BufferedJbossCache.java	2010-01-08 09:53:53 UTC (rev 1319)
@@ -1,7 +1,5 @@
 package org.exoplatform.services.jcr.impl.dataflow.persistent.jbosscache;
 
-import org.exoplatform.services.log.ExoLogger;
-import org.exoplatform.services.log.Log;
 import org.jboss.cache.Cache;
 import org.jboss.cache.CacheException;
 import org.jboss.cache.CacheSPI;
@@ -24,9 +22,10 @@
 
 import javax.transaction.TransactionManager;
 
+ at SuppressWarnings("unchecked")
 public class BufferedJbossCache implements Cache<Serializable, Object>
 {
-   private final Log log = ExoLogger.getLogger(BufferedJbossCache.class);
+//   private final Log log = ExoLogger.getLogger(BufferedJbossCache.class);
 
    /**
     * Parent cache.
@@ -54,20 +53,25 @@
     */
    public void commitTransaction()
    {
-
       List<ChangesContainer> changesContainer = changesList.get();
       if (changesContainer == null)
       {
-         throw new IllegalStateException("changesContainer == null");
+         throw new IllegalStateException("changesContainer should not be empty");
       }
-      //log.info("Before=" + changesContainer.toString());
-      Collections.sort(changesContainer);
-      //log.info("After=" + changesContainer.toString());
-      for (ChangesContainer cacheChange : changesContainer)
+      try
       {
-         cacheChange.apply();
+         //log.info("Before=" + changesContainer.toString());
+         Collections.sort(changesContainer);
+         //log.info("After=" + changesContainer.toString());
+         for (ChangesContainer cacheChange : changesContainer)
+         {
+            cacheChange.apply();
+         }
       }
-      changesList.set(null);
+      finally
+      {
+         changesList.set(null);
+      }
    }
 
    /**
@@ -83,7 +87,6 @@
     */
    public void addCacheListener(Object listener)
    {
-
       parentCache.addCacheListener(listener);
    }
 
@@ -93,7 +96,6 @@
    public void addInterceptor(CommandInterceptor i, Class<? extends CommandInterceptor> afterInterceptor)
    {
       parentCache.addInterceptor(i, afterInterceptor);
-
    }
 
    /* (non-Javadoc)
@@ -111,7 +113,6 @@
    public void clearData(Fqn fqn)
    {
       parentCache.clearData(fqn);
-
    }
 
    /* (non-Javadoc)
@@ -129,7 +130,6 @@
    public void create() throws CacheException
    {
       parentCache.create();
-
    }
 
    /* (non-Javadoc)
@@ -138,7 +138,6 @@
    public void destroy()
    {
       parentCache.destroy();
-
    }
 
    /* (non-Javadoc)
@@ -146,7 +145,6 @@
     */
    public void endBatch(boolean successful)
    {
-
       parentCache.endBatch(successful);
 
    }
@@ -157,7 +155,6 @@
    public void evict(Fqn fqn, boolean recursive)
    {
       parentCache.evict(fqn, recursive);
-
    }
 
    /* (non-Javadoc)
@@ -166,7 +163,6 @@
    public void evict(Fqn fqn)
    {
       parentCache.evict(fqn);
-
    }
 
    /* (non-Javadoc)
@@ -174,7 +170,6 @@
     */
    public Object get(Fqn fqn, Serializable key)
    {
-
       return parentCache.get(fqn, key);
    }
 
@@ -183,7 +178,6 @@
     */
    public Object get(String fqn, Serializable key)
    {
-
       return parentCache.get(fqn, key);
    }
 
@@ -192,7 +186,6 @@
     */
    public Set<Object> getCacheListeners()
    {
-
       return parentCache.getCacheListeners();
    }
 
@@ -201,7 +194,6 @@
     */
    public CacheStatus getCacheStatus()
    {
-
       return parentCache.getCacheStatus();
    }
 
@@ -210,7 +202,6 @@
     */
    public Set<Object> getChildrenNames(Fqn fqn)
    {
-
       return parentCache.getChildrenNames(fqn);
    }
 
@@ -219,7 +210,6 @@
     */
    public Set<String> getChildrenNames(String fqn)
    {
-
       return parentCache.getChildrenNames(fqn);
    }
 
@@ -228,7 +218,6 @@
     */
    public Configuration getConfiguration()
    {
-
       return parentCache.getConfiguration();
    }
 
@@ -237,7 +226,6 @@
     */
    public Map<Serializable, Object> getData(Fqn fqn)
    {
-
       return parentCache.getData(fqn);
    }
 
@@ -246,7 +234,6 @@
     */
    public InvocationContext getInvocationContext()
    {
-
       return parentCache.getInvocationContext();
    }
 
@@ -255,7 +242,6 @@
     */
    public Set<Serializable> getKeys(Fqn fqn)
    {
-
       return parentCache.getKeys(fqn);
    }
 
@@ -264,7 +250,6 @@
     */
    public Set<Serializable> getKeys(String fqn)
    {
-
       return parentCache.getKeys(fqn);
    }
 
@@ -273,7 +258,6 @@
     */
    public Address getLocalAddress()
    {
-
       return parentCache.getLocalAddress();
    }
 
@@ -282,7 +266,6 @@
     */
    public List<Address> getMembers()
    {
-
       return parentCache.getMembers();
    }
 
@@ -291,7 +274,6 @@
     */
    public Node<Serializable, Object> getNode(Fqn fqn)
    {
-
       return parentCache.getNode(fqn);
    }
 
@@ -300,7 +282,6 @@
     */
    public Node<Serializable, Object> getNode(String fqn)
    {
-
       return parentCache.getNode(fqn);
    }
 
@@ -309,7 +290,6 @@
     */
    public Region getRegion(Fqn fqn, boolean createIfAbsent)
    {
-
       return parentCache.getRegion(fqn, createIfAbsent);
    }
 
@@ -318,7 +298,6 @@
     */
    public Node<Serializable, Object> getRoot()
    {
-
       return parentCache.getRoot();
    }
 
@@ -327,7 +306,6 @@
     */
    public String getVersion()
    {
-
       return parentCache.getVersion();
    }
 
@@ -336,7 +314,6 @@
     */
    public boolean isLeaf(Fqn fqn)
    {
-
       return parentCache.isLeaf(fqn);
    }
 
@@ -345,7 +322,6 @@
     */
    public boolean isLeaf(String fqn)
    {
-
       return parentCache.isLeaf(fqn);
    }
 
@@ -355,7 +331,6 @@
    public void move(Fqn nodeToMove, Fqn newParent) throws NodeNotExistsException
    {
       parentCache.move(nodeToMove, newParent);
-
    }
 
    /* (non-Javadoc)
@@ -363,7 +338,6 @@
     */
    public void move(String nodeToMove, String newParent) throws NodeNotExistsException
    {
-
       parentCache.move(nodeToMove, newParent);
    }
 
@@ -376,7 +350,7 @@
       List<ChangesContainer> changesContainer = changesList.get();
       if (changesContainer == null)
       {
-         throw new IllegalStateException("changesContainer == null");
+         throw new IllegalStateException("changesContainer should not be empty");
       }
       changesContainer.add(new PutObjectContainer(fqn, data, parentCache));
    }
@@ -389,7 +363,7 @@
       List<ChangesContainer> changesContainer = changesList.get();
       if (changesContainer == null)
       {
-         throw new IllegalStateException("changesContainer == null");
+         throw new IllegalStateException("changesContainer should not be empty");
       }
       changesContainer.add(new PutKeyValueContainer(fqn, key, value, parentCache));
 
@@ -401,9 +375,8 @@
     */
    public void put(String fqn, Map<? extends Serializable, ? extends Object> data)
    {
-      throw new IllegalStateException(
-         "Unexpected method call use  put(Fqn fqn, Map<? extends Serializable, ? extends Object> data)");
-      //parentCache.put(fqn, data);
+      throw new UnsupportedOperationException(
+         "Unexpected method call use put(Fqn fqn, Map<? extends Serializable, ? extends Object> data)");
    }
 
    /* (non-Javadoc)
@@ -411,8 +384,7 @@
     */
    public Object put(String fqn, Serializable key, Object value)
    {
-
-      throw new IllegalStateException("Unexpected method call use  put(Fqn fqn, Serializable key, Object value)");
+      throw new UnsupportedOperationException("Unexpected method call use put(Fqn fqn, Serializable key, Object value)");
    }
 
    /* (non-Javadoc)
@@ -420,8 +392,7 @@
     */
    public void putForExternalRead(Fqn fqn, Serializable key, Object value)
    {
-
-      throw new IllegalStateException("Unexpected method call ");
+      throw new UnsupportedOperationException("Unexpected method call ");
    }
 
    /* (non-Javadoc)
@@ -432,7 +403,7 @@
       List<ChangesContainer> changesContainer = changesList.get();
       if (changesContainer == null)
       {
-         throw new IllegalStateException("changesContainer == null");
+         throw new IllegalStateException("changesContainer should not be empty");
       }
       changesContainer.add(new RemoveKeyContainer(fqn, key, parentCache));
       return parentCache.get(fqn, key);
@@ -443,8 +414,7 @@
     */
    public Object remove(String fqn, Serializable key)
    {
-
-      throw new IllegalStateException("Unexpected method call. Use  remove(Fqn fqn, Serializable key)");
+      throw new UnsupportedOperationException("Unexpected method call. Use remove(Fqn fqn, Serializable key)");
    }
 
    /* (non-Javadoc)
@@ -452,7 +422,6 @@
     */
    public void removeCacheListener(Object listener)
    {
-
       parentCache.removeCacheListener(listener);
    }
 
@@ -461,7 +430,6 @@
     */
    public void removeInterceptor(Class<? extends CommandInterceptor> interceptorType)
    {
-
       parentCache.removeInterceptor(interceptorType);
    }
 
@@ -470,7 +438,6 @@
     */
    public void removeInterceptor(int position)
    {
-
       parentCache.removeInterceptor(position);
    }
 
@@ -482,7 +449,7 @@
       List<ChangesContainer> changesContainer = changesList.get();
       if (changesContainer == null)
       {
-         throw new IllegalStateException("changesContainer == null");
+         throw new IllegalStateException("changesContainer should not be empty");
       }
       changesContainer.add(new RemoveNodeContainer(fqn, parentCache));
       return true;
@@ -493,8 +460,7 @@
     */
    public boolean removeNode(String fqn)
    {
-
-      throw new IllegalStateException("Unexpected method call. Use  remove removeNode(Fqn fqn)");
+      throw new UnsupportedOperationException("Unexpected method call. Use remove removeNode(Fqn fqn)");
    }
 
    /* (non-Javadoc)
@@ -502,8 +468,7 @@
     */
    public boolean removeRegion(Fqn fqn)
    {
-
-      throw new IllegalStateException("Unexpected method call ");
+      return parentCache.removeRegion(fqn);
    }
 
    /* (non-Javadoc)
@@ -512,7 +477,6 @@
    public void setInvocationContext(InvocationContext ctx)
    {
       parentCache.setInvocationContext(ctx);
-
    }
 
    /* (non-Javadoc)
@@ -520,7 +484,6 @@
     */
    public void start() throws CacheException
    {
-
       parentCache.start();
    }
 
@@ -530,7 +493,6 @@
    public void startBatch()
    {
       parentCache.startBatch();
-
    }
 
    /* (non-Javadoc)
@@ -539,7 +501,6 @@
    public void stop()
    {
       parentCache.stop();
-
    }
 
    public TransactionManager getTransactionManager()
@@ -547,12 +508,9 @@
       return ((CacheSPI<Serializable, Object>)parentCache).getTransactionManager();
    }
 
-   interface ChangesType
+   private static enum ChangesType
    {
-      int REMOVED = 0;
-
-      int ADDED = 1;
-
+      REMOVED, ADDED;
    }
 
    /**
@@ -560,15 +518,15 @@
     * @author sj
     *
     */
-   private abstract class ChangesContainer implements Comparable<ChangesContainer>
+   private static abstract class ChangesContainer implements Comparable<ChangesContainer>
    {
       protected final Fqn fqn;
 
-      protected final int changesType;
+      protected final ChangesType changesType;
 
       protected final Cache<Serializable, Object> cache;
 
-      public ChangesContainer(Fqn fqn, int changesType, Cache<Serializable, Object> cache)
+      public ChangesContainer(Fqn fqn, ChangesType changesType, Cache<Serializable, Object> cache)
       {
          super();
          this.fqn = fqn;
@@ -587,7 +545,7 @@
       /**
        * @return the changesType
        */
-      public int getChangesType()
+      public ChangesType getChangesType()
       {
          return changesType;
       }
@@ -598,19 +556,13 @@
       @Override
       public String toString()
       {
-
          return fqn + " type=" + changesType;
       }
 
       public int compareTo(ChangesContainer o)
       {
-//         if (changesType != o.getChangesType())
-//         {
-//            //removed first
-//            return changesType == ChangesType.REMOVED ? -1 : 1;
-//         }
-
-         return fqn.compareTo(o.getFqn());
+         int result = fqn.compareTo(o.getFqn());
+         return result == 0 ? -1 : result;
       }
 
       public abstract void apply();
@@ -621,7 +573,7 @@
     * @author sj
     *
     */
-   private class PutObjectContainer extends ChangesContainer
+   private static class PutObjectContainer extends ChangesContainer
    {
       private final Map<? extends Serializable, ? extends Object> data;
 
@@ -645,7 +597,7 @@
     * @author sj
     *
     */
-   private class PutKeyValueContainer extends ChangesContainer
+   private static class PutKeyValueContainer extends ChangesContainer
    {
       private final Serializable key;
 
@@ -654,7 +606,6 @@
       public PutKeyValueContainer(Fqn fqn, Serializable key, Object value, Cache<Serializable, Object> cache)
       {
          super(fqn, ChangesType.ADDED, cache);
-         // TODO Auto-generated constructor stub
          this.key = key;
          this.value = value;
       }
@@ -671,7 +622,7 @@
     * @author sj
     *
     */
-   private class RemoveKeyContainer extends ChangesContainer
+   private static class RemoveKeyContainer extends ChangesContainer
    {
       private final Serializable key;
 
@@ -695,22 +646,18 @@
     * @author sj
     *
     */
-   private class RemoveNodeContainer extends ChangesContainer
+   private static class RemoveNodeContainer extends ChangesContainer
    {
 
       public RemoveNodeContainer(Fqn fqn, Cache<Serializable, Object> cache)
       {
          super(fqn, ChangesType.REMOVED, cache);
-
       }
 
       @Override
       public void apply()
       {
          cache.removeNode(fqn);
-
       }
-
    }
-
 }



More information about the exo-jcr-commits mailing list