[gatein-commits] gatein SVN: r571 - in portal/trunk/component/portal/src: main/java/org/exoplatform/portal/pom/config/cache and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Wed Nov 11 12:48:34 EST 2009


Author: julien_viet
Date: 2009-11-11 12:48:34 -0500 (Wed, 11 Nov 2009)
New Revision: 571

Added:
   portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/config/TaskExecutionDecorator.java
Modified:
   portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/config/POMDataStorage.java
   portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/config/cache/DataCache.java
   portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/data/OwnerKey.java
   portal/trunk/component/portal/src/test/java/org/exoplatform/portal/config/TestUserPortalConfigService.java
Log:
- fix issues with data cache
- add unit tests to ensure cache works


Modified: portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/config/POMDataStorage.java
===================================================================
--- portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/config/POMDataStorage.java	2009-11-11 12:40:30 UTC (rev 570)
+++ portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/config/POMDataStorage.java	2009-11-11 17:48:34 UTC (rev 571)
@@ -77,7 +77,7 @@
    private final Log log = ExoLogger.getLogger(getClass());
    
    /** . */
-   private final TaskExecutor executor;
+   private final TaskExecutionDecorator executor;
 
    public POMDataStorage(CacheService cacheService, POMSessionManager pomMgr, ConfigurationManager confManager)
    {
@@ -90,7 +90,12 @@
    {
       return pomMgr;
    }
-   
+
+   public <E extends TaskExecutionDecorator> E getDecorator(Class<E> decoratorClass)
+   {
+      return executor.getDecorator(decoratorClass);
+   }
+
    /**
     * <p>Execute the task with a session. The method attempts first to get a current session and if no such session
     * is found then a session will be created for the scope of the method.</p>
@@ -115,7 +120,7 @@
       }
       else
       {
-         session.execute(task);
+         executor.execute(session, task);
       }
 
       //

Added: portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/config/TaskExecutionDecorator.java
===================================================================
--- portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/config/TaskExecutionDecorator.java	                        (rev 0)
+++ portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/config/TaskExecutionDecorator.java	2009-11-11 17:48:34 UTC (rev 571)
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2003-2007 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.portal.pom.config;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class TaskExecutionDecorator implements TaskExecutor
+{
+
+   /** . */
+   private final TaskExecutor next;
+
+   public TaskExecutionDecorator(TaskExecutor next)
+   {
+      this.next = next;
+   }
+
+   public void execute(POMSession session, POMTask task) throws Exception
+   {
+      next.execute(session, task);
+   }
+
+   public <E extends TaskExecutor> E getDecorator(Class<E> decoratorClass)
+   {
+      if (decoratorClass.isInstance(this))
+      {
+         return decoratorClass.cast(this);
+      }
+      else
+      {
+         if (next != null && next instanceof TaskExecutionDecorator)
+         {
+            return ((TaskExecutionDecorator)next).getDecorator(decoratorClass);
+         }
+         else
+         {
+            return null;
+         }
+      }
+   }
+}

Modified: portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/config/cache/DataCache.java
===================================================================
--- portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/config/cache/DataCache.java	2009-11-11 12:40:30 UTC (rev 570)
+++ portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/config/cache/DataCache.java	2009-11-11 17:48:34 UTC (rev 571)
@@ -21,27 +21,31 @@
 import org.exoplatform.portal.pom.config.POMSession;
 import org.exoplatform.portal.pom.config.POMTask;
 import org.exoplatform.portal.pom.config.TaskExecutor;
+import org.exoplatform.portal.pom.config.TaskExecutionDecorator;
 import org.exoplatform.services.cache.CacheService;
 import org.exoplatform.services.cache.ExoCache;
 
 import java.io.Serializable;
+import java.util.concurrent.atomic.AtomicLong;
 
 /**
  * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
  * @version $Revision$
  */
-public class DataCache implements TaskExecutor
+public class DataCache extends TaskExecutionDecorator
 {
 
    /** . */
-   private TaskExecutor next;
+   private final ExoCache<Serializable, Object> cache;
 
    /** . */
-   private ExoCache<Serializable, Object> cache;
+   private final AtomicLong readCount = new AtomicLong();
 
    public DataCache(CacheService cacheService, TaskExecutor next)
    {
-      this.next = next;
+      super(next);
+
+      //
       this.cache = cacheService.getCacheInstance(DataCache.class.getSimpleName());
    }
 
@@ -70,31 +74,40 @@
                   throw new UnsupportedOperationException();
             }
          }
+         else
+         {
+            super.execute(session, task);
+         }
       }
       else
       {
-         next.execute(session, task);
+         super.execute(session, task);
       }
    }
 
+   public void clear()
+   {
+      cache.clearCache();
+   }
+
    private <K extends Serializable, V> void remove(POMSession session, CacheableDataTask<K, V> task) throws Exception
    {
       K key = task.getKey();
       cache.remove(key);
-      next.execute(session, task);
+      super.execute(session, task);
    }
 
    private <K extends Serializable, V> void write(POMSession session, CacheableDataTask<K, V> task) throws Exception
    {
       K key = task.getKey();
       cache.remove(key);
-      next.execute(session, task);
+      super.execute(session, task);
    }
 
    private <K extends Serializable, V> void create(POMSession session, CacheableDataTask<K, V> task) throws Exception
    {
       // Nothing to do for now
-      next.execute(session, task);
+      super.execute(session, task);
    }
 
    private <K extends Serializable, V> void read(POMSession session, CacheableDataTask<K, V> task) throws Exception
@@ -118,8 +131,10 @@
       }
       else
       {
+         readCount.incrementAndGet();
+
          //
-         next.execute(session, task);
+         super.execute(session, task);
 
          //
          v = task.getValue();
@@ -129,4 +144,9 @@
          }
       }
    }
+
+   public long getReadCount()
+   {
+      return readCount.longValue();
+   }
 }

Modified: portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/data/OwnerKey.java
===================================================================
--- portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/data/OwnerKey.java	2009-11-11 12:40:30 UTC (rev 570)
+++ portal/trunk/component/portal/src/main/java/org/exoplatform/portal/pom/data/OwnerKey.java	2009-11-11 17:48:34 UTC (rev 571)
@@ -24,7 +24,7 @@
  * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
  * @version $Revision$
  */
-public class OwnerKey implements Serializable
+public abstract class OwnerKey implements Serializable
 {
 
    /** . */
@@ -66,8 +66,12 @@
    @Override
    public boolean equals(Object obj)
    {
-      if (obj instanceof OwnerKey)
+      if (obj == this)
       {
+         return true;
+      }
+      if (getClass().isInstance(obj))
+      {
          OwnerKey that = (OwnerKey)obj;
          return type.equals(that.type) && id.equals(that.id);
       }

Modified: portal/trunk/component/portal/src/test/java/org/exoplatform/portal/config/TestUserPortalConfigService.java
===================================================================
--- portal/trunk/component/portal/src/test/java/org/exoplatform/portal/config/TestUserPortalConfigService.java	2009-11-11 12:40:30 UTC (rev 570)
+++ portal/trunk/component/portal/src/test/java/org/exoplatform/portal/config/TestUserPortalConfigService.java	2009-11-11 17:48:34 UTC (rev 571)
@@ -31,8 +31,10 @@
 import org.exoplatform.portal.config.model.PageBody;
 import org.exoplatform.portal.config.model.PageNavigation;
 import org.exoplatform.portal.config.model.PortalConfig;
+import org.exoplatform.portal.pom.config.POMDataStorage;
 import org.exoplatform.portal.pom.config.POMSession;
 import org.exoplatform.portal.pom.config.POMSessionManager;
+import org.exoplatform.portal.pom.config.cache.DataCache;
 import org.exoplatform.portal.pom.spi.portlet.Portlet;
 import org.exoplatform.portal.pom.spi.portlet.PortletBuilder;
 import org.exoplatform.services.listener.Event;
@@ -91,6 +93,9 @@
    /** . */
    private boolean registered;
 
+   /** . */
+   private POMDataStorage mopStorage;
+
    public TestUserPortalConfigService(String name)
    {
       super(name);
@@ -114,8 +119,7 @@
       };
 
       PortalContainer container = PortalContainer.getInstance();
-      userPortalConfigSer_ =
-         (UserPortalConfigService)container.getComponentInstanceOfType(UserPortalConfigService.class);
+      userPortalConfigSer_ = (UserPortalConfigService)container.getComponentInstanceOfType(UserPortalConfigService.class);
       orgService_ = (OrganizationService)container.getComponentInstanceOfType(OrganizationService.class);
       idmService = (JBossIDMService)container.getComponentInstanceOfType(JBossIDMService.class);
       mgr = (POMSessionManager)container.getComponentInstanceOfType(POMSessionManager.class);
@@ -123,6 +127,7 @@
       listenerService = (ListenerService)container.getComponentInstanceOfType(ListenerService.class);
       events = new LinkedList<Event>();
       storage_ = (DataStorage)container.getComponentInstanceOfType(DataStorage.class);
+      mopStorage = (POMDataStorage)container.getComponentInstanceOfType(POMDataStorage.class);
 
       // Register only once for all unit tests
       if (!registered)
@@ -706,6 +711,60 @@
       }.execute(null);
    }
 
+   public void testCacheUserPortalConfig()
+   {
+      new UnitTest()
+      {
+         public void execute() throws Exception
+         {
+            DataCache cache = mopStorage.getDecorator(DataCache.class);
+            long readCount0 = cache.getReadCount();
+            userPortalConfigSer_.getUserPortalConfig("classic", null);
+            long readCount1 = cache.getReadCount();
+            assertTrue(readCount1 > readCount0);
+            userPortalConfigSer_.getUserPortalConfig("classic", null);
+            long readCount2 = cache.getReadCount();
+            assertEquals(readCount1, readCount2);
+         }
+      }.execute(null);
+   }
+
+   public void testCachePage()
+   {
+      new UnitTest()
+      {
+         public void execute() throws Exception
+         {
+            DataCache cache = mopStorage.getDecorator(DataCache.class);
+            long readCount0 = cache.getReadCount();
+            userPortalConfigSer_.getPage("portal::test::test1");
+            long readCount1 = cache.getReadCount();
+            assertTrue(readCount1 > readCount0);
+            userPortalConfigSer_.getPage("portal::test::test1");
+            long readCount2 = cache.getReadCount();
+            assertEquals(readCount1, readCount2);
+         }
+      }.execute(null);
+   }
+
+   public void testCachePageNavigation()
+   {
+      new UnitTest()
+      {
+         public void execute() throws Exception
+         {
+            DataCache cache = mopStorage.getDecorator(DataCache.class);
+            long readCount0 = cache.getReadCount();
+            userPortalConfigSer_.getPageNavigation("portal", "test");
+            long readCount1 = cache.getReadCount();
+            assertTrue(readCount1 > readCount0);
+            userPortalConfigSer_.getPageNavigation("portal", "test");
+            long readCount2 = cache.getReadCount();
+            assertEquals(readCount1, readCount2);
+         }
+      }.execute(null);
+   }
+
    private abstract class UnitTest
    {
 
@@ -744,6 +803,10 @@
                failure = e;
             }
 
+            // Clear cache
+            DataCache cache = mopStorage.getDecorator(DataCache.class);
+            cache.clear();
+
             //
             mopSession = mgr.openSession();
             if (failure == null)



More information about the gatein-commits mailing list