Author: ndkhoiits
Date: 2010-11-29 03:36:33 -0500 (Mon, 29 Nov 2010)
New Revision: 5338
Added:
exo/portal/branches/3.1.x/component/portal/src/main/java/org/exoplatform/portal/pom/config/cache/PortalNamesCache.java
Modified:
exo/portal/branches/3.1.x/component/portal/src/main/java/org/exoplatform/portal/config/model/PortalConfig.java
exo/portal/branches/3.1.x/component/portal/src/main/java/org/exoplatform/portal/pom/config/POMSessionManager.java
exo/portal/branches/3.1.x/component/portal/src/test/java/org/exoplatform/portal/config/TestDataStorage.java
Log:
EXOGTN-182 DataStorage getAllPortalNames() cache
Modified:
exo/portal/branches/3.1.x/component/portal/src/main/java/org/exoplatform/portal/config/model/PortalConfig.java
===================================================================
---
exo/portal/branches/3.1.x/component/portal/src/main/java/org/exoplatform/portal/config/model/PortalConfig.java 2010-11-29
07:52:03 UTC (rev 5337)
+++
exo/portal/branches/3.1.x/component/portal/src/main/java/org/exoplatform/portal/config/model/PortalConfig.java 2010-11-29
08:36:33 UTC (rev 5338)
@@ -67,6 +67,16 @@
this(PORTAL_TYPE);
}
+ public PortalConfig(String type)
+ {
+ this(type, null);
+ }
+
+ public PortalConfig(String type, String ownerId)
+ {
+ this(type, ownerId, null);
+ }
+
public PortalConfig(String type, String ownerId, String storageId)
{
super(storageId);
@@ -74,16 +84,9 @@
//
this.type = type;
this.name = ownerId;
+ this.portalLayout = new Container();
}
- public PortalConfig(String type)
- {
- this.type = type;
-
- //
- setPortalLayout(new Container());
- }
-
public PortalConfig(PortalData data)
{
super(data.getStorageId());
@@ -99,14 +102,6 @@
this.portalLayout = new Container(data.getPortalLayout());
}
- PortalConfig(String storageId, String type)
- {
- super(storageId);
-
- //
- this.type = type;
- }
-
public String getType()
{
return type;
Modified:
exo/portal/branches/3.1.x/component/portal/src/main/java/org/exoplatform/portal/pom/config/POMSessionManager.java
===================================================================
---
exo/portal/branches/3.1.x/component/portal/src/main/java/org/exoplatform/portal/pom/config/POMSessionManager.java 2010-11-29
07:52:03 UTC (rev 5337)
+++
exo/portal/branches/3.1.x/component/portal/src/main/java/org/exoplatform/portal/pom/config/POMSessionManager.java 2010-11-29
08:36:33 UTC (rev 5338)
@@ -23,6 +23,7 @@
import org.exoplatform.commons.chromattic.ChromatticManager;
import org.exoplatform.commons.chromattic.SessionContext;
import org.exoplatform.portal.pom.config.cache.DataCache;
+import org.exoplatform.portal.pom.config.cache.PortalNamesCache;
import org.exoplatform.services.cache.CacheService;
import org.exoplatform.services.cache.ExoCache;
import org.exoplatform.services.jcr.RepositoryService;
@@ -69,7 +70,7 @@
this.manager = manager;
this.cache = cacheService.getCacheInstance("MOPSessionManager");
this.pomService = null;
- this.executor = new DataCache(new ExecutorDispatcher());
+ this.executor = new PortalNamesCache(new DataCache(new ExecutorDispatcher()));
}
public void cachePut(Serializable key, Object value)
Added:
exo/portal/branches/3.1.x/component/portal/src/main/java/org/exoplatform/portal/pom/config/cache/PortalNamesCache.java
===================================================================
---
exo/portal/branches/3.1.x/component/portal/src/main/java/org/exoplatform/portal/pom/config/cache/PortalNamesCache.java
(rev 0)
+++
exo/portal/branches/3.1.x/component/portal/src/main/java/org/exoplatform/portal/pom/config/cache/PortalNamesCache.java 2010-11-29
08:36:33 UTC (rev 5338)
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.portal.pom.config.cache;
+
+import org.exoplatform.commons.utils.LazyPageList;
+import org.exoplatform.commons.utils.ListAccessImpl;
+import org.exoplatform.portal.pom.config.POMSession;
+import org.exoplatform.portal.pom.config.POMTask;
+import org.exoplatform.portal.pom.config.TaskExecutionDecorator;
+import org.exoplatform.portal.pom.config.TaskExecutor;
+import org.exoplatform.portal.pom.config.tasks.PortalConfigTask;
+import org.exoplatform.portal.pom.config.tasks.SearchTask;
+import org.exoplatform.portal.pom.data.PortalKey;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class PortalNamesCache extends TaskExecutionDecorator
+{
+
+ public PortalNamesCache(TaskExecutor next)
+ {
+ super(next);
+ }
+
+ @Override
+ public <V> V execute(POMSession session, POMTask<V> task) throws
Exception
+ {
+ if (!session.isModified())
+ {
+ if (task instanceof SearchTask.FindSiteKey)
+ {
+ List<PortalKey> data =
(List<PortalKey>)session.getFromCache(SearchTask.FindSiteKey.class);
+ if (data == null)
+ {
+ V result = super.execute(session, task);
+ LazyPageList<PortalKey> list =
(LazyPageList<PortalKey>)result;
+ session.putInCache(SearchTask.FindSiteKey.class,
Collections.unmodifiableList(new ArrayList<PortalKey>(list.getAll())));
+ return result;
+ }
+ else
+ {
+ return (V)new LazyPageList<PortalKey>(new
ListAccessImpl<PortalKey>(PortalKey.class, data), 10);
+ }
+ }
+ else if (task instanceof PortalConfigTask.Save || task instanceof
PortalConfigTask.Remove)
+ {
+ V result = super.execute(session, task);
+ session.scheduleForEviction(SearchTask.FindSiteKey.class);
+ return result;
+ }
+ }
+
+ //
+ return super.execute(session, task);
+ }
+}
Modified:
exo/portal/branches/3.1.x/component/portal/src/test/java/org/exoplatform/portal/config/TestDataStorage.java
===================================================================
---
exo/portal/branches/3.1.x/component/portal/src/test/java/org/exoplatform/portal/config/TestDataStorage.java 2010-11-29
07:52:03 UTC (rev 5337)
+++
exo/portal/branches/3.1.x/component/portal/src/test/java/org/exoplatform/portal/config/TestDataStorage.java 2010-11-29
08:36:33 UTC (rev 5338)
@@ -21,6 +21,7 @@
import static org.exoplatform.portal.pom.config.Utils.split;
+import junit.framework.AssertionFailedError;
import org.exoplatform.container.PortalContainer;
import org.exoplatform.portal.application.PortletPreferences;
import org.exoplatform.portal.application.Preference;
@@ -36,6 +37,8 @@
import org.exoplatform.services.listener.ListenerService;
import java.util.*;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicReference;
/**
* Created by The eXo Platform SARL Author : Tung Pham thanhtungty(a)gmail.com Nov
@@ -869,6 +872,121 @@
assertEquals("foo", storage_.getId(gadgetApp.getState()));
}
+ public void testGetAllPortalNames() throws Exception
+ {
+ final List<String> names = storage_.getAllPortalNames();
+
+ // Create new portal
+ storage_.create(new PortalConfig("portal",
"testGetAllPortalNames"));
+
+ // Test during tx we see the good names
+ List<String> transientNames = storage_.getAllPortalNames();
+ assertTrue(transientNames.containsAll(names));
+ transientNames.removeAll(names);
+ assertEquals(Collections.singletonList("testGetAllPortalNames"),
transientNames);
+
+ // Test we have not seen anything yet outside of tx
+ final CountDownLatch addSync = new CountDownLatch(1);
+ final AtomicReference<Throwable> error = new
AtomicReference<Throwable>();
+ new Thread()
+ {
+ @Override
+ public void run()
+ {
+ begin();
+ try
+ {
+ List<String> isolatedNames = storage_.getAllPortalNames();
+ assertEquals(new HashSet<String>(names), new
HashSet<String>(isolatedNames));
+ }
+ catch (Throwable t)
+ {
+ error.set(t);
+ }
+ finally
+ {
+ addSync.countDown();
+ end();
+ }
+ }
+ }.start();
+
+ //
+ addSync.await();
+ if (error.get() != null)
+ {
+ AssertionFailedError afe = new AssertionFailedError();
+ afe.initCause(error.get());
+ throw afe;
+ }
+
+ // Now commit tx
+ session.close(true);
+ end(true);
+
+ // We test we observe the change
+ begin();
+ session = mgr.openSession();
+ List<String> afterNames = storage_.getAllPortalNames();
+ assertTrue(afterNames.containsAll(names));
+ afterNames.removeAll(names);
+ assertEquals(Collections.singletonList("testGetAllPortalNames"),
afterNames);
+
+ // Then we remove the newly created portal
+ storage_.remove(new PortalConfig("portal",
"testGetAllPortalNames"));
+
+ // Test we are syeing the transient change
+ transientNames.clear();
+ transientNames = storage_.getAllPortalNames();
+ assertEquals(names, transientNames);
+
+ // Test we have not seen anything yet outside of tx
+ error.set(null);
+ final CountDownLatch removeSync = new CountDownLatch(1);
+ new Thread()
+ {
+ public void run()
+ {
+ begin();
+ try
+ {
+ List<String> isolatedNames = storage_.getAllPortalNames();
+ assertTrue(isolatedNames.containsAll(names));
+ isolatedNames.removeAll(names);
+ assertEquals(Collections.singletonList("testGetAllPortalNames"),
isolatedNames);
+ }
+ catch (Throwable t)
+ {
+ error.set(t);
+ }
+ finally
+ {
+ removeSync.countDown();
+ end();
+ }
+ }
+ }.start();
+
+ //
+ removeSync.await();
+ if (error.get() != null)
+ {
+ AssertionFailedError afe = new AssertionFailedError();
+ afe.initCause(error.get());
+ throw afe;
+ }
+
+ //
+ session.close(true);
+ end(true);
+
+ // Now test it is still removed
+ begin();
+ session = mgr.openSession();
+ afterNames = storage_.getAllPortalNames();
+ assertEquals(new HashSet<String>(names), new
HashSet<String>(afterNames));
+ }
+
private Application<Portlet> create(String instanceId)
{
int i0 = instanceId.indexOf("#");