From do-not-reply at jboss.org Tue Apr 6 09:32:15 2010
Content-Type: multipart/mixed; boundary="===============5563369333057244138=="
MIME-Version: 1.0
From: do-not-reply at jboss.org
To: gatein-commits at lists.jboss.org
Subject: [gatein-commits] gatein SVN: r2492 - in
portal/branches/EPP_5_0_Branch/component:
common/src/main/java/org/exoplatform/commons/cache/future and 5 other
directories.
Date: Tue, 06 Apr 2010 09:32:15 -0400
Message-ID: <201004061332.o36DWF2O029781@svn01.web.mwc.hst.phx2.redhat.com>
--===============5563369333057244138==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Author: mpodolin
Date: 2010-04-06 09:32:13 -0400 (Tue, 06 Apr 2010)
New Revision: 2492
Added:
portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopla=
tform/commons/cache/future/
portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopla=
tform/commons/cache/future/Entry.java
portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopla=
tform/commons/cache/future/FutureCache.java
portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopla=
tform/commons/cache/future/FutureExoCache.java
portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopla=
tform/commons/cache/future/Loader.java
portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopla=
tform/commons/cache/
portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopla=
tform/commons/cache/future/
portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopla=
tform/commons/cache/future/ConcurrentGetWhenPutTestCase.java
portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopla=
tform/commons/cache/future/FutureMap.java
portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopla=
tform/commons/cache/future/GetTestCase.java
portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopla=
tform/commons/cache/future/StringLoader.java
portal/branches/EPP_5_0_Branch/component/scripting/src/main/java/org/exo=
platform/resolver/ResourceKey.java
Modified:
portal/branches/EPP_5_0_Branch/component/scripting/src/main/java/org/exo=
platform/groovyscript/text/TemplateService.java
portal/branches/EPP_5_0_Branch/component/scripting/src/main/java/org/exo=
platform/resolver/ResourceResolver.java
Log:
JBEPP-265: GTNPORTAL-945 ported to the branch
Added: portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/ex=
oplatform/commons/cache/future/Entry.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopl=
atform/commons/cache/future/Entry.java (rev 0)
+++ portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopl=
atform/commons/cache/future/Entry.java 2010-04-06 13:32:13 UTC (rev 2492)
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2009 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.commons.cache.future;
+
+import java.io.Serializable;
+
+/**
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public class Entry implements Serializable
+{
+
+ public static Entry create(V v)
+ {
+ return new Entry(v);
+ }
+
+ /** . */
+ private final V value;
+
+ private Entry(V value)
+ {
+ if (value =3D=3D null)
+ {
+ throw new NullPointerException();
+ }
+ this.value =3D value;
+ }
+
+ public V getValue()
+ {
+ return value;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "Entry[" + value + "]";
+ }
+}
Added: portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/ex=
oplatform/commons/cache/future/FutureCache.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopl=
atform/commons/cache/future/FutureCache.java (rev 0)
+++ portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopl=
atform/commons/cache/future/FutureCache.java 2010-04-06 13:32:13 UTC (rev 2=
492)
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2009 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.commons.cache.future;
+
+import org.gatein.common.logging.Logger;
+import org.gatein.common.logging.LoggerFactory;
+
+import java.util.concurrent.*;
+
+/**
+ * A future cache that prevents the loading of the same resource twice. Th=
is should be used when the resource
+ * to load is very expensive or cannot be concurrently retrieved (like a c=
lassloading). =
+ *
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public abstract class FutureCache
+{
+
+ /** . */
+ private final Loader loader;
+
+ /** . */
+ private final ConcurrentMap>> local;
+
+ /** . */
+ private final Logger log =3D LoggerFactory.getLogger(FutureCache.class);
+
+ public FutureCache(Loader loader)
+ {
+ this.loader =3D loader;
+ this.local =3D new ConcurrentHashMap>>();
+ }
+
+ protected abstract Entry get(K key);
+
+ protected abstract void put(K key, Entry entry);
+
+ public V get(final C context, final K key)
+ {
+ // First we try a simple cache get
+ Entry entry =3D get(key);
+
+ // If it does not succeed then we go through a process that will avo=
id to load
+ // the same resource concurrently
+ if (entry =3D=3D null)
+ {
+ // Create our future
+ FutureTask> future =3D new FutureTask>(new Call=
able>()
+ {
+ public Entry call() throws Exception
+ {
+ // Retrieve the value from the loader
+ V value =3D loader.retrieve(context, key);
+
+ //
+ if (value !=3D null)
+ {
+ // Create the entry
+ Entry entry =3D Entry.create(value);
+
+ // Cache it, it is made available to other threads (unle=
ss someone removes it)
+ put(key, entry);
+
+ // Return entry
+ return entry;
+ }
+ else
+ {
+ return null;
+ }
+ }
+ });
+
+ // Was our means that we inserted in the local
+ boolean inserted =3D true;
+
+ //
+ try
+ {
+ FutureTask> phantom =3D local.putIfAbsent(key, future=
);
+
+ // Use the entry that could have been inserted by another thre=
ad
+ if (phantom !=3D null)
+ {
+ future =3D phantom;
+ inserted =3D false;
+ }
+ else
+ {
+ future.run();
+ }
+
+ // Returns the entry
+ entry =3D future.get();
+ }
+ catch (ExecutionException e)
+ {
+ log.error("Computing of resource " + key + " threw an exceptio=
n", e.getCause());
+ }
+ catch (Exception e)
+ {
+ log.error("Retrieval of resource " + key + " threw an exceptio=
n", e);
+ }
+ finally
+ {
+ // Clean up the per key map but only if our insertion succeede=
d and with our future
+ if (inserted)
+ {
+ local.remove(key, future);
+ }
+ }
+ }
+
+ //
+ return entry !=3D null ? entry.getValue() : null;
+ }
+}
Added: portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/ex=
oplatform/commons/cache/future/FutureExoCache.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopl=
atform/commons/cache/future/FutureExoCache.java (re=
v 0)
+++ portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopl=
atform/commons/cache/future/FutureExoCache.java 2010-04-06 13:32:13 UTC (re=
v 2492)
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2009 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.commons.cache.future;
+
+import org.exoplatform.services.cache.ExoCache;
+
+import java.io.Serializable;
+
+/**
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public class FutureExoCache extends FutureCa=
che
+{
+
+ /** . */
+ private final ExoCache> cache;
+
+ public FutureExoCache(Loader loader, ExoCache> cac=
he)
+ {
+ super(loader);
+
+ //
+ this.cache =3D cache;
+ }
+
+ @Override
+ protected Entry get(K key)
+ {
+ return cache.get(key);
+ }
+
+ @Override
+ protected void put(K key, Entry entry)
+ {
+ cache.put(key, entry);
+ }
+}
Added: portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/ex=
oplatform/commons/cache/future/Loader.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopl=
atform/commons/cache/future/Loader.java (rev 0)
+++ portal/branches/EPP_5_0_Branch/component/common/src/main/java/org/exopl=
atform/commons/cache/future/Loader.java 2010-04-06 13:32:13 UTC (rev 2492)
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2009 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.commons.cache.future;
+
+import java.io.Serializable;
+
+/**
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public interface Loader
+{
+
+ V retrieve(C context, K key) throws Exception;
+
+}
Added: portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/ex=
oplatform/commons/cache/future/ConcurrentGetWhenPutTestCase.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopl=
atform/commons/cache/future/ConcurrentGetWhenPutTestCase.java =
(rev 0)
+++ portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopl=
atform/commons/cache/future/ConcurrentGetWhenPutTestCase.java 2010-04-06 13=
:32:13 UTC (rev 2492)
@@ -0,0 +1,177 @@
+/*
+ * Copyright (C) 2009 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.commons.cache.future;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+import org.exoplatform.commons.cache.future.Entry;
+import org.exoplatform.commons.cache.future.FutureCache;
+import org.gatein.common.util.Tools;
+
+import java.util.*;
+import java.util.concurrent.Callable;
+
+/**
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public class ConcurrentGetWhenPutTestCase extends TestCase
+{
+
+ /** . */
+ private AssertionFailedError failure;
+
+ /** . */
+ private LinkedList events =3D new LinkedList();
+
+ FutureCache> futureCache =3D new Futur=
eCache>(new StringLoader()) {
+
+
+ @Override
+ protected Entry get(String key)
+ {
+ if (key =3D=3D key1)
+ {
+ if (Thread.currentThread() !=3D thread1)
+ {
+ failure =3D new AssertionFailedError();
+ }
+ events.addLast("get/key1");
+ }
+ else if (key =3D=3D key2)
+ {
+ if (Thread.currentThread() !=3D thread2)
+ {
+ failure =3D new AssertionFailedError();
+ }
+ events.addLast("get/key2");
+ }
+ else
+ {
+ failure =3D new AssertionFailedError();
+ }
+ return null;
+ }
+
+ @Override
+ protected void put(String key, Entry entry)
+ {
+ if (key =3D=3D key1)
+ {
+ if (Thread.currentThread() =3D=3D thread1)
+ {
+ events.addLast("begin_put/key1/" + entry);
+
+ //
+ thread2.start();
+
+ //
+ while (thread2.getState() !=3D Thread.State.WAITING)
+ {
+ // Wait until thread 2 is blocked
+ }
+
+ //
+ events.addLast("end_put/key1");
+ }
+ else
+ {
+ failure =3D new AssertionFailedError();
+ }
+ }
+ else
+ {
+ failure =3D new AssertionFailedError();
+ }
+ }
+ };
+
+ /** . */
+ private final String key1 =3D new String("foo");
+
+ /** . */
+ private final String key2 =3D new String("foo");
+
+ Thread thread1 =3D new Thread()
+ {
+ @Override
+ public void run()
+ {
+ String v =3D futureCache.get(new Callable()
+ {
+ public String call() throws Exception
+ {
+ events.addLast("call/key1");
+ return "foo_value_1";
+ }
+ }, key1);
+ events.addLast("retrieved/key1/" + v);
+ }
+ };
+
+ Thread thread2 =3D new Thread()
+ {
+ @Override
+ public void run()
+ {
+ String v =3D futureCache.get(new Callable()
+ {
+ public String call() throws Exception
+ {
+ failure =3D new AssertionFailedError();
+ return "foo_value_2";
+ }
+ }, key2);
+ events.addLast("retrieved/key2/" + v);
+ }
+ };
+
+ public void testMain() throws Exception
+ {
+ thread1.start();
+
+ //
+ thread1.join();
+ thread2.join();
+
+ //
+ if (failure !=3D null)
+ {
+ throw failure;
+ }
+
+ //
+ List expectedEvents =3D Arrays.asList(
+ "get/key1",
+ "call/key1",
+ "begin_put/key1/Entry[foo_value_1]",
+ "get/key2",
+ "end_put/key1"
+ );
+
+ //
+ assertEquals(expectedEvents, events.subList(0, expectedEvents.size()=
));
+
+ //
+ Set expectedEndEvents =3D Tools.toSet("retrieved/key1/foo_va=
lue_1", "retrieved/key2/foo_value_1");
+ assertEquals(expectedEndEvents, new HashSet(events.subList(e=
xpectedEvents.size(), events.size())));
+ }
+
+}
\ No newline at end of file
Added: portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/ex=
oplatform/commons/cache/future/FutureMap.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopl=
atform/commons/cache/future/FutureMap.java (rev 0)
+++ portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopl=
atform/commons/cache/future/FutureMap.java 2010-04-06 13:32:13 UTC (rev 249=
2)
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2009 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.commons.cache.future;
+
+import org.exoplatform.commons.cache.future.Entry;
+import org.exoplatform.commons.cache.future.FutureCache;
+import org.exoplatform.commons.cache.future.Loader;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public class FutureMap extends FutureCache
+{
+
+ /** . */
+ final Map> data;
+
+ public FutureMap(Loader loader)
+ {
+ super(loader);
+
+ //
+ this.data =3D Collections.synchronizedMap(new HashMap>());
+ }
+
+ @Override
+ protected Entry get(String key)
+ {
+ return data.get(key);
+ }
+
+ @Override
+ protected void put(String key, Entry entry)
+ {
+ data.put(key, entry);
+ }
+}
Added: portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/ex=
oplatform/commons/cache/future/GetTestCase.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopl=
atform/commons/cache/future/GetTestCase.java (rev 0)
+++ portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopl=
atform/commons/cache/future/GetTestCase.java 2010-04-06 13:32:13 UTC (rev 2=
492)
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2009 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.commons.cache.future;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import java.util.concurrent.Callable;
+
+/**
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public class GetTestCase extends TestCase
+{
+ public void testGet()
+ {
+ FutureMap> futureCache =3D new FutureMap>(new StringLoader());
+ Assert.assertEquals("foo_value", futureCache.get(new Callable()
+ {
+ public String call() throws Exception
+ {
+ return "foo_value";
+ }
+ }, "foo"));
+ Assert.assertEquals("foo_value", futureCache.data.get("foo").getValu=
e());
+ }
+
+ public void testNullValue()
+ {
+ FutureMap> futureCache =3D new FutureMap>(new StringLoader());
+ Assert.assertEquals(null, futureCache.get(new Callable()
+ {
+ public String call() throws Exception
+ {
+ return null;
+ }
+ }, "foo"));
+ Assert.assertFalse(futureCache.data.containsKey("foo"));
+ }
+
+ public void testThrowException()
+ {
+ FutureMap> futureCache =3D new FutureMap>(new StringLoader());
+ Assert.assertEquals(null, futureCache.get(new Callable()
+ {
+ public String call() throws Exception
+ {
+ throw new Exception("DON'T FREAK OUT");
+ }
+ }, "foo"));
+ Assert.assertFalse(futureCache.data.containsKey("foo"));
+ }
+}
Added: portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/ex=
oplatform/commons/cache/future/StringLoader.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopl=
atform/commons/cache/future/StringLoader.java (rev =
0)
+++ portal/branches/EPP_5_0_Branch/component/common/src/test/java/org/exopl=
atform/commons/cache/future/StringLoader.java 2010-04-06 13:32:13 UTC (rev =
2492)
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2009 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.commons.cache.future;
+
+import org.exoplatform.commons.cache.future.Loader;
+
+import java.util.concurrent.Callable;
+
+/**
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public class StringLoader implements Loader>
+{
+ public String retrieve(Callable context, String key) throws Exc=
eption
+ {
+ return context.call();
+ }
+}
Modified: portal/branches/EPP_5_0_Branch/component/scripting/src/main/java/=
org/exoplatform/groovyscript/text/TemplateService.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/EPP_5_0_Branch/component/scripting/src/main/java/org/ex=
oplatform/groovyscript/text/TemplateService.java 2010-04-06 13:11:04 UTC (r=
ev 2491)
+++ portal/branches/EPP_5_0_Branch/component/scripting/src/main/java/org/ex=
oplatform/groovyscript/text/TemplateService.java 2010-04-06 13:32:13 UTC (r=
ev 2492)
@@ -22,8 +22,11 @@
import groovy.lang.Writable;
import groovy.text.Template;
=
+import org.exoplatform.commons.cache.future.Entry;
+import org.exoplatform.commons.cache.future.FutureCache;
+import org.exoplatform.commons.cache.future.FutureExoCache;
+import org.exoplatform.commons.cache.future.Loader;
import org.exoplatform.commons.utils.IOUtil;
-import org.exoplatform.container.xml.InitParams;
import org.exoplatform.groovyscript.GroovyTemplate;
import org.exoplatform.groovyscript.GroovyTemplateEngine;
import org.exoplatform.management.annotations.Impact;
@@ -34,9 +37,11 @@
import org.exoplatform.management.jmx.annotations.NameTemplate;
import org.exoplatform.management.jmx.annotations.Property;
import org.exoplatform.management.rest.annotations.RESTEndpoint;
+import org.exoplatform.resolver.ResourceKey;
import org.exoplatform.resolver.ResourceResolver;
import org.exoplatform.services.cache.CacheService;
import org.exoplatform.services.cache.ExoCache;
+import org.gatein.common.io.IOTools;
=
import java.io.InputStream;
import java.util.ArrayList;
@@ -54,17 +59,52 @@
=
private GroovyTemplateEngine engine_;
=
- private ExoCache templatesCache_;
+ private ExoCache> templatesCache_;
=
private TemplateStatisticService statisticService;
=
private boolean cacheTemplate_ =3D true;
=
+ private final Loader loa=
der =3D new Loader()
+ {
+ public GroovyTemplate retrieve(ResourceResolver context, ResourceKey=
key) throws Exception
+ {
+ byte[] bytes;
+ InputStream is =3D context.getInputStream(key.getURL());
+ try
+ {
+ bytes =3D IOUtil.getStreamContentAsBytes(is);
+ is.close();
+ }
+ finally
+ {
+ IOTools.safeClose(is);
+ }
+
+ // The template class name
+ int pos =3D key.getURL().lastIndexOf('/');
+ if (pos =3D=3D -1)
+ {
+ pos =3D 0;
+ }
+ String name =3D key.getURL().substring(pos);
+
+ // Julien: it's a bit dangerious here, with respect to the file e=
ncoding...
+ String text =3D new String(bytes);
+
+ // Finally do the expensive template creation
+ return engine_.createTemplate(key.getURL(), name, text);
+ }
+ };
+
+ private FutureCache futu=
reCache;
+
public TemplateService(TemplateStatisticService statisticService, Cache=
Service cservice) throws Exception
{
this.engine_ =3D new GroovyTemplateEngine();
this.statisticService =3D statisticService;
this.templatesCache_ =3D cservice.getCacheInstance(TemplateService.c=
lass.getSimpleName());
+ this.futureCache =3D new FutureExoCache(loader, templatesCache_);
}
=
public void merge(String name, BindingContext context) throws Exception
@@ -108,37 +148,18 @@
=
final public GroovyTemplate getTemplate(String url, ResourceResolver re=
solver, boolean cacheable) throws Exception
{
- GroovyTemplate template =3D null;
+ GroovyTemplate template;
+ ResourceKey resourceId =3D resolver.createResourceKey(url);
if (cacheable)
{
- String resourceId =3D resolver.createResourceId(url);
- template =3D getTemplatesCache().get(resourceId);
+ template =3D futureCache.get(resolver, resourceId);
}
- if (template !=3D null)
- return template;
- InputStream is;
- byte[] bytes =3D null;
- is =3D resolver.getInputStream(url);
- bytes =3D IOUtil.getStreamContentAsBytes(is);
- is.close();
-
- // The template class name
- int pos =3D url.lastIndexOf('/');
- if (pos =3D=3D -1)
+ else
{
- pos =3D 0;
+ template =3D loader.retrieve(resolver, resourceId);
}
- String name =3D url.substring(pos);
=
- String text =3D new String(bytes);
- template =3D engine_.createTemplate(url, name, text);
-
- if (cacheable)
- {
- String resourceId =3D resolver.createResourceId(url);
- getTemplatesCache().put(resourceId, template);
- }
-
+ //
return template;
}
=
@@ -148,7 +169,7 @@
getTemplatesCache().remove(resourceId);
}
=
- public ExoCache getTemplatesCache()
+ public ExoCache> getTemplatesCache()
{
return templatesCache_;
}
@@ -198,9 +219,13 @@
try
{
ArrayList list =3D new ArrayList();
- for (GroovyTemplate template : templatesCache_.getCachedObjects())
+ for (Entry entry : templatesCache_.getCachedObjec=
ts())
{
- list.add(template.getId());
+ GroovyTemplate template =3D entry.getValue();
+ if (template !=3D null)
+ {
+ list.add(template.getId());
+ }
}
return list.toArray(new String[list.size()]);
}
Added: portal/branches/EPP_5_0_Branch/component/scripting/src/main/java/org=
/exoplatform/resolver/ResourceKey.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/EPP_5_0_Branch/component/scripting/src/main/java/org/ex=
oplatform/resolver/ResourceKey.java (rev 0)
+++ portal/branches/EPP_5_0_Branch/component/scripting/src/main/java/org/ex=
oplatform/resolver/ResourceKey.java 2010-04-06 13:32:13 UTC (rev 2492)
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2009 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.resolver;
+
+import java.io.Serializable;
+
+/**
+ * @author Julien Viet
+ * @version $Revision$
+ */
+public class ResourceKey implements Serializable
+{
+
+ /** . */
+ private final int resolverId;
+
+ /** . */
+ private final String url;
+
+ public ResourceKey(int resolverId, String url)
+ {
+ if (url =3D=3D null)
+ {
+ throw new NullPointerException("no null URL accepted");
+ }
+ this.resolverId =3D resolverId;
+ this.url =3D url;
+ }
+
+ public String getURL()
+ {
+ return url;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return resolverId ^ url.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if (o =3D=3D this)
+ {
+ return true;
+ }
+ if (o instanceof ResourceKey)
+ {
+ ResourceKey that =3D (ResourceKey)o;
+ return resolverId =3D=3D that.resolverId && url.equals(that.url);
+ }
+ return false;
+ }
+}
Modified: portal/branches/EPP_5_0_Branch/component/scripting/src/main/java/=
org/exoplatform/resolver/ResourceResolver.java
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- portal/branches/EPP_5_0_Branch/component/scripting/src/main/java/org/ex=
oplatform/resolver/ResourceResolver.java 2010-04-06 13:11:04 UTC (rev 2491)
+++ portal/branches/EPP_5_0_Branch/component/scripting/src/main/java/org/ex=
oplatform/resolver/ResourceResolver.java 2010-04-06 13:32:13 UTC (rev 2492)
@@ -54,6 +54,11 @@
throw new RuntimeException("unsupported method");
}
=
+ public ResourceKey createResourceKey(String url)
+ {
+ return new ResourceKey(hashCode(), url);
+ }
+
public String createResourceId(String url)
{
return hashCode() + ":" + url;
--===============5563369333057244138==--