Author: nfilotto
Date: 2010-09-08 12:34:20 -0400 (Wed, 08 Sep 2010)
New Revision: 3094
Added:
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/AbstractExoCacheCreator.java
Modified:
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/AbstractExoCache.java
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/ExoCacheFactoryImpl.java
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/ea/EAExoCacheCreator.java
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/fifo/FIFOExoCacheCreator.java
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/lfu/LFUExoCacheCreator.java
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/lru/LRUExoCacheCreator.java
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/mru/MRUExoCacheCreator.java
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/test/java/org/exoplatform/services/cache/impl/jboss/TestAbstractExoCache.java
Log:
EXOJCR-943: Implementation based on the JBC Configuration, if 2 caches have the same
config (appart the Eviction Policy), they will be converted into Regions of the same
cache
Modified:
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/AbstractExoCache.java
===================================================================
---
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/AbstractExoCache.java 2010-09-08
11:25:25 UTC (rev 3093)
+++
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/AbstractExoCache.java 2010-09-08
16:34:20 UTC (rev 3094)
@@ -78,17 +78,24 @@
private final CopyOnWriteArrayList<ListenerContext<K, V>> listeners;
protected final CacheSPI<K, V> cache;
+
+ protected final Fqn<String> rootFqn;
- public AbstractExoCache(ExoCacheConfig config, Cache<K, V> cache)
+ public AbstractExoCache(ExoCacheConfig config, Cache<K, V> cache,
Fqn<String> rootFqn)
{
this.cache = (CacheSPI<K, V>)cache;
+ this.rootFqn = rootFqn;
this.listeners = new CopyOnWriteArrayList<ListenerContext<K, V>>();
setDistributed(config.isDistributed());
setLabel(config.getLabel());
setName(config.getName());
setLogEnabled(config.isLogEnabled());
setReplicated(config.isRepicated());
- cache.getConfiguration().setInvocationBatchingEnabled(true);
+ if (cache.getCacheStatus().createAllowed())
+ {
+ // Avoid set several times the same parameter if the cache is shared
+ cache.getConfiguration().setInvocationBatchingEnabled(true);
+ }
cache.addCacheListener(new CacheEventListener());
}
@@ -110,7 +117,7 @@
public void clearCache()
{
cache.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
- cache.removeNode(Fqn.ROOT);
+ cache.removeNode(rootFqn);
onClearCache();
}
@@ -158,7 +165,8 @@
*/
public int getCacheSize()
{
- return cache.getNumberOfNodes();
+ final NodeSPI<K, V> node = cache.peek(rootFqn, false);
+ return node == null ? 0 : node.getChildrenNamesDirect().size();
}
/**
@@ -167,7 +175,7 @@
public List<V> getCachedObjects()
{
final LinkedList<V> list = new LinkedList<V>();
- for (Node<K, V> node : cache.getRoot().getChildren())
+ for (Node<K, V> node : cache.getNode(rootFqn).getChildren())
{
if (node == null)
{
@@ -331,7 +339,7 @@
{
throw new IllegalArgumentException("No null selector");
}
- for (Node<K, V> node : cache.getRoot().getChildren())
+ for (Node<K, V> node : cache.getNode(rootFqn).getChildren())
{
if (node == null)
{
@@ -415,7 +423,7 @@
@SuppressWarnings("unchecked")
private K getKey(Fqn fqn)
{
- return (K)fqn.get(0);
+ return (K)fqn.get(rootFqn.size());
}
/**
@@ -423,7 +431,7 @@
*/
protected Fqn<Serializable> getFqn(Serializable name)
{
- return Fqn.fromElements(name);
+ return Fqn.fromRelativeElements(rootFqn, name);
}
void onExpire(K key, V obj)
@@ -521,13 +529,14 @@
}
@org.jboss.cache.notifications.annotation.CacheListener
+ @SuppressWarnings("unchecked")
public class CacheEventListener
{
@NodeEvicted
public void nodeEvicted(NodeEvictedEvent ne)
{
- if (ne.isPre())
+ if (ne.isPre() && ne.getFqn().isChildOf(rootFqn))
{
final NodeSPI<K, V> node = cache.peek(ne.getFqn(), true);
final K key = getKey(ne.getFqn());
@@ -535,11 +544,10 @@
}
}
- @SuppressWarnings("unchecked")
@NodeRemoved
public void nodeRemoved(NodeRemovedEvent ne)
{
- if (ne.isPre() && !ne.isOriginLocal())
+ if (ne.isPre() && !ne.isOriginLocal() &&
ne.getFqn().isChildOf(rootFqn))
{
final K key = getKey(ne.getFqn());
final Map<K, V> data = ne.getData();
@@ -547,11 +555,10 @@
}
}
- @SuppressWarnings("unchecked")
@NodeModified
public void nodeModified(NodeModifiedEvent ne)
{
- if (!ne.isOriginLocal() && !ne.isPre())
+ if (!ne.isOriginLocal() && !ne.isPre() &&
ne.getFqn().isChildOf(rootFqn))
{
final K key = getKey(ne.getFqn());
final Map<K, V> data = ne.getData();
Added:
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/AbstractExoCacheCreator.java
===================================================================
---
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/AbstractExoCacheCreator.java
(rev 0)
+++
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/AbstractExoCacheCreator.java 2010-09-08
16:34:20 UTC (rev 3094)
@@ -0,0 +1,41 @@
+package org.exoplatform.services.cache.impl.jboss;
+
+import org.exoplatform.services.cache.ExoCacheConfig;
+import org.jboss.cache.Cache;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.Region;
+import org.jboss.cache.config.EvictionAlgorithmConfig;
+import org.jboss.cache.config.EvictionRegionConfig;
+
+import java.io.Serializable;
+
+/**
+ * This class is used to propose a set of common methods generally needed by {@link
ExoCacheCreator}
+ * implementations
+ *
+ * @author <a href="mailto:nicolas.filotto@exoplatform.com">Nicolas
Filotto</a>
+ * @version $Id$
+ *
+ */
+public abstract class AbstractExoCacheCreator implements ExoCacheCreator
+{
+
+ /**
+ * Create a new region to the given cache.
+ * @param config The ExoCacheConfig from which we get the name of the region.
+ * @param cache the cache instance to which we want to add the new region
+ * @param eac The Eviction Algorithm to use for the new region to create.
+ * @return The root Fqn of the new created region
+ */
+ protected Fqn<String> addEvictionRegion(ExoCacheConfig config,
Cache<Serializable, Object> cache,
+ EvictionAlgorithmConfig eac)
+ {
+ Fqn<String> fqn = Fqn.fromElements(config.getName());
+ // Create the region
+ Region region = cache.getRegion(fqn, true);
+ // Set the eviction region config
+ region.setEvictionRegionConfig(new EvictionRegionConfig(fqn, eac));
+ return fqn;
+ }
+
+}
Modified:
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/ExoCacheFactoryImpl.java
===================================================================
---
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/ExoCacheFactoryImpl.java 2010-09-08
11:25:25 UTC (rev 3093)
+++
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/ExoCacheFactoryImpl.java 2010-09-08
16:34:20 UTC (rev 3094)
@@ -37,6 +37,7 @@
import org.jboss.cache.config.Configuration.CacheMode;
import java.io.Serializable;
+import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@@ -88,6 +89,12 @@
* The mapping between the cache names and the configuration paths
*/
private final Map<String, String> mappingCacheNameConfig = new
HashMap<String, String>();
+
+ /**
+ * A Map that contains all the registered JBC instances.
+ */
+ private final Map<ConfigurationKey, Cache<Serializable, Object>> caches =
+ new HashMap<ConfigurationKey, Cache<Serializable, Object>>();
/**
* The default creator
@@ -96,14 +103,19 @@
public ExoCacheFactoryImpl(InitParams params, ConfigurationManager configManager)
{
+ this(getValueParam(params, CACHE_CONFIG_TEMPLATE_KEY), configManager);
+ }
+
+ ExoCacheFactoryImpl(String cacheConfigTemplate, ConfigurationManager configManager)
+ {
this.configManager = configManager;
- this.cacheConfigTemplate = getValueParam(params, CACHE_CONFIG_TEMPLATE_KEY);
+ this.cacheConfigTemplate = cacheConfigTemplate;
if (cacheConfigTemplate == null)
{
throw new RuntimeException("The parameter '" +
CACHE_CONFIG_TEMPLATE_KEY + "' must be set");
}
}
-
+
/**
* To create a new cache instance according to the given configuration, we follow the
steps below:
*
@@ -116,7 +128,7 @@
{
final String region = config.getName();
final String customConfig = mappingCacheNameConfig.get(region);
- final Cache<Serializable, Object> cache;
+ Cache<Serializable, Object> cache;
final CacheFactory<Serializable, Object> factory = new
DefaultCacheFactory<Serializable, Object>();
final ExoCache<Serializable, Object> eXoCache;
try
@@ -143,6 +155,8 @@
cleanConfigurationTemplate(cache, region);
}
final ExoCacheCreator creator = getExoCacheCreator(config);
+ // Ensure that new created cache doesn't exist
+ cache = getUniqueInstance(cache, region);
// Create the cache
eXoCache = creator.create(config, cache);
// Create the cache
@@ -238,11 +252,102 @@
config.setEvictionConfig(evictionConfig);
}
evictionConfig.setEvictionRegionConfigs(new
LinkedList<EvictionRegionConfig>());
- // Rename the cluster name
- String clusterName = config.getClusterName();
- if (clusterName != null && (clusterName = clusterName.trim()).length() >
0)
+ }
+
+ /**
+ * Try to find if a Cache of the same type (i.e. their {@link Configuration} are
equals)
+ * has already been registered.
+ * If no cache has been registered, we register the given cache otherwise we
+ * use the previously registered cache.
+ * @param cache the cache to register
+ * @param region the region of the cache
+ * @return the given cache if has not been registered otherwise the cache of the same
+ * type that has already been registered
+ * @throws ExoCacheInitException
+ */
+ private synchronized Cache<Serializable, Object>
getUniqueInstance(Cache<Serializable, Object> cache, String region)
+ throws ExoCacheInitException
+ {
+ Configuration cfg = cache.getConfiguration();
+ ConfigurationKey key;
+ try
{
- config.setClusterName(clusterName + " " + region);
+ key = new ConfigurationKey(cfg);
}
+ catch (CloneNotSupportedException e)
+ {
+ throw new ExoCacheInitException("Cannot clone the configuration.",
e);
+ }
+ if (caches.containsKey(key))
+ {
+ cache = caches.get(key);
+ }
+ else
+ {
+ caches.put(key, cache);
+ if (LOG.isInfoEnabled())
+ LOG.info("A new eXo Cache based on JBoss Cache instance has been
registered for the region " + region);
+ }
+ return cache;
}
+
+ /**
+ * This class is used to make {@link Configuration} being usable as a Key in an
HashMap since
+ * some variables such as <code>jgroupsConfigFile</code> are not managed
as expected in the
+ * methods equals and hashCode. Moreover two cache with same config except the
EvictionConfig
+ * are considered as identical
+ */
+ private static class ConfigurationKey
+ {
+ private final URL jgroupsConfigFile;
+ private final Configuration conf;
+
+ public ConfigurationKey(Configuration initialConf) throws
CloneNotSupportedException
+ {
+ // Clone it first since it will be modified
+ this.conf = initialConf.clone();
+ this.jgroupsConfigFile = conf.getJGroupsConfigFile();
+ // remove the jgroupsConfigFile from the conf
+ conf.setJgroupsConfigFile(null);
+ // remove the EvictionConfig to ignore it
+ conf.setEvictionConfig(null);
+ }
+
+ @Override
+ public int hashCode()
+ {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((conf == null) ? 0 : conf.hashCode());
+ result = prime * result + ((jgroupsConfigFile == null) ? 0 :
jgroupsConfigFile.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ ConfigurationKey other = (ConfigurationKey)obj;
+ if (conf == null)
+ {
+ if (other.conf != null)
+ return false;
+ }
+ else if (!conf.equals(other.conf))
+ return false;
+ if (jgroupsConfigFile == null)
+ {
+ if (other.jgroupsConfigFile != null)
+ return false;
+ }
+ else if (!jgroupsConfigFile.equals(other.jgroupsConfigFile))
+ return false;
+ return true;
+ }
+ }
}
Modified:
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/ea/EAExoCacheCreator.java
===================================================================
---
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/ea/EAExoCacheCreator.java 2010-09-08
11:25:25 UTC (rev 3093)
+++
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/ea/EAExoCacheCreator.java 2010-09-08
16:34:20 UTC (rev 3094)
@@ -23,12 +23,9 @@
import org.exoplatform.services.cache.ExoCacheConfig;
import org.exoplatform.services.cache.ExoCacheInitException;
import org.exoplatform.services.cache.impl.jboss.AbstractExoCache;
-import org.exoplatform.services.cache.impl.jboss.ExoCacheCreator;
+import org.exoplatform.services.cache.impl.jboss.AbstractExoCacheCreator;
import org.jboss.cache.Cache;
import org.jboss.cache.Fqn;
-import org.jboss.cache.config.Configuration;
-import org.jboss.cache.config.EvictionConfig;
-import org.jboss.cache.config.EvictionRegionConfig;
import org.jboss.cache.eviction.ExpirationAlgorithm;
import org.jboss.cache.eviction.ExpirationAlgorithmConfig;
@@ -41,7 +38,7 @@
* nicolas.filotto(a)exoplatform.com
* 8 mars 2010
*/
-public class EAExoCacheCreator implements ExoCacheCreator
+public class EAExoCacheCreator extends AbstractExoCacheCreator
{
/**
@@ -95,18 +92,14 @@
private ExoCache<Serializable, Object> create(ExoCacheConfig config,
Cache<Serializable, Object> cache,
int maxNodes, long minTimeToLive, long expirationTimeout) throws
ExoCacheInitException
{
- final Configuration configuration = cache.getConfiguration();
final ExpirationAlgorithmConfig ea = new ExpirationAlgorithmConfig();
ea.setMaxNodes(maxNodes);
ea.setMinTimeToLive(minTimeToLive);
ea.setExpirationKeyName(ExpirationAlgorithmConfig.EXPIRATION_KEY);
- // Create an eviction region config
- final EvictionRegionConfig erc = new EvictionRegionConfig(Fqn.ROOT, ea);
- final EvictionConfig evictionConfig = configuration.getEvictionConfig();
- evictionConfig.setDefaultEvictionRegionConfig(erc);
+ Fqn<String> rooFqn = addEvictionRegion(config, cache, ea);
- return new EAExoCache(config, cache, ea, expirationTimeout);
+ return new EAExoCache(config, cache, rooFqn, ea, expirationTimeout);
}
/**
@@ -118,10 +111,10 @@
private final ExpirationAlgorithmConfig ea;
- public EAExoCache(ExoCacheConfig config, Cache<Serializable, Object> cache,
ExpirationAlgorithmConfig ea,
- long expirationTimeout)
+ public EAExoCache(ExoCacheConfig config, Cache<Serializable, Object> cache,
Fqn<String> rooFqn,
+ ExpirationAlgorithmConfig ea, long expirationTimeout)
{
- super(config, cache);
+ super(config, cache, rooFqn);
this.ea = ea;
this.expirationTimeout = expirationTimeout;
}
Modified:
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/fifo/FIFOExoCacheCreator.java
===================================================================
---
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/fifo/FIFOExoCacheCreator.java 2010-09-08
11:25:25 UTC (rev 3093)
+++
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/fifo/FIFOExoCacheCreator.java 2010-09-08
16:34:20 UTC (rev 3094)
@@ -24,12 +24,9 @@
import org.exoplatform.services.cache.ExoCacheConfig;
import org.exoplatform.services.cache.ExoCacheInitException;
import org.exoplatform.services.cache.impl.jboss.AbstractExoCache;
-import org.exoplatform.services.cache.impl.jboss.ExoCacheCreator;
+import org.exoplatform.services.cache.impl.jboss.AbstractExoCacheCreator;
import org.jboss.cache.Cache;
import org.jboss.cache.Fqn;
-import org.jboss.cache.config.Configuration;
-import org.jboss.cache.config.EvictionConfig;
-import org.jboss.cache.config.EvictionRegionConfig;
import org.jboss.cache.eviction.FIFOAlgorithmConfig;
import java.io.Serializable;
@@ -41,7 +38,7 @@
* exo(a)exoplatform.com
* 20 juil. 2009
*/
-public class FIFOExoCacheCreator implements ExoCacheCreator
+public class FIFOExoCacheCreator extends AbstractExoCacheCreator
{
/**
@@ -88,16 +85,10 @@
private ExoCache<Serializable, Object> create(ExoCacheConfig config,
Cache<Serializable, Object> cache, int maxNodes, long minTimeToLive)
throws ExoCacheInitException
{
- final Configuration configuration = cache.getConfiguration();
final FIFOAlgorithmConfig fifo = new FIFOAlgorithmConfig(maxNodes);
fifo.setMinTimeToLive(minTimeToLive);
- // Create an eviction region config
- final EvictionRegionConfig erc = new EvictionRegionConfig(Fqn.ROOT, fifo);
-
- final EvictionConfig evictionConfig = configuration.getEvictionConfig();
- evictionConfig.setDefaultEvictionRegionConfig(erc);
-
- return new AbstractExoCache<Serializable, Object>(config, cache)
+ Fqn<String> rooFqn = addEvictionRegion(config, cache, fifo);
+ return new AbstractExoCache<Serializable, Object>(config, cache, rooFqn)
{
public void setMaxSize(int max)
Modified:
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/lfu/LFUExoCacheCreator.java
===================================================================
---
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/lfu/LFUExoCacheCreator.java 2010-09-08
11:25:25 UTC (rev 3093)
+++
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/lfu/LFUExoCacheCreator.java 2010-09-08
16:34:20 UTC (rev 3094)
@@ -25,12 +25,9 @@
import org.exoplatform.services.cache.ExoCacheConfig;
import org.exoplatform.services.cache.ExoCacheInitException;
import org.exoplatform.services.cache.impl.jboss.AbstractExoCache;
-import org.exoplatform.services.cache.impl.jboss.ExoCacheCreator;
+import org.exoplatform.services.cache.impl.jboss.AbstractExoCacheCreator;
import org.jboss.cache.Cache;
import org.jboss.cache.Fqn;
-import org.jboss.cache.config.Configuration;
-import org.jboss.cache.config.EvictionConfig;
-import org.jboss.cache.config.EvictionRegionConfig;
import org.jboss.cache.eviction.LFUAlgorithmConfig;
import java.io.Serializable;
@@ -42,7 +39,7 @@
* exo(a)exoplatform.com
* 21 juil. 2009
*/
-public class LFUExoCacheCreator implements ExoCacheCreator
+public class LFUExoCacheCreator extends AbstractExoCacheCreator
{
/**
@@ -58,7 +55,8 @@
/**
* {@inheritDoc}
*/
- public ExoCache<Serializable, Object> create(ExoCacheConfig config,
Cache<Serializable, Object> cache) throws ExoCacheInitException
+ public ExoCache<Serializable, Object> create(ExoCacheConfig config,
Cache<Serializable, Object> cache)
+ throws ExoCacheInitException
{
if (config instanceof LFUExoCacheConfig)
{
@@ -75,18 +73,13 @@
/**
* Creates a new ExoCache instance with the relevant parameters
*/
- private ExoCache<Serializable, Object> create(ExoCacheConfig config,
Cache<Serializable, Object> cache, int maxNodes, int minNodes,
- long minTimeToLive) throws ExoCacheInitException
+ private ExoCache<Serializable, Object> create(ExoCacheConfig config,
Cache<Serializable, Object> cache,
+ int maxNodes, int minNodes, long minTimeToLive) throws ExoCacheInitException
{
- final Configuration configuration = cache.getConfiguration();
final LFUAlgorithmConfig lfu = new LFUAlgorithmConfig(maxNodes, minNodes);
lfu.setMinTimeToLive(minTimeToLive);
- // Create an eviction region config
- final EvictionRegionConfig erc = new EvictionRegionConfig(Fqn.ROOT, lfu);
-
- final EvictionConfig evictionConfig = configuration.getEvictionConfig();
- evictionConfig.setDefaultEvictionRegionConfig(erc);
- return new LFUExoCache(config, cache, lfu);
+ Fqn<String> rooFqn = addEvictionRegion(config, cache, lfu);
+ return new LFUExoCache(config, cache, rooFqn, lfu);
}
/**
@@ -113,9 +106,10 @@
private final LFUAlgorithmConfig lfu;
- public LFUExoCache(ExoCacheConfig config, Cache<Serializable, Object> cache,
LFUAlgorithmConfig lfu)
+ public LFUExoCache(ExoCacheConfig config, Cache<Serializable, Object> cache,
Fqn<String> rooFqn,
+ LFUAlgorithmConfig lfu)
{
- super(config, cache);
+ super(config, cache, rooFqn);
this.lfu = lfu;
}
Modified:
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/lru/LRUExoCacheCreator.java
===================================================================
---
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/lru/LRUExoCacheCreator.java 2010-09-08
11:25:25 UTC (rev 3093)
+++
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/lru/LRUExoCacheCreator.java 2010-09-08
16:34:20 UTC (rev 3094)
@@ -25,12 +25,9 @@
import org.exoplatform.services.cache.ExoCacheConfig;
import org.exoplatform.services.cache.ExoCacheInitException;
import org.exoplatform.services.cache.impl.jboss.AbstractExoCache;
-import org.exoplatform.services.cache.impl.jboss.ExoCacheCreator;
+import org.exoplatform.services.cache.impl.jboss.AbstractExoCacheCreator;
import org.jboss.cache.Cache;
import org.jboss.cache.Fqn;
-import org.jboss.cache.config.Configuration;
-import org.jboss.cache.config.EvictionConfig;
-import org.jboss.cache.config.EvictionRegionConfig;
import org.jboss.cache.eviction.LRUAlgorithmConfig;
import java.io.Serializable;
@@ -42,7 +39,7 @@
* exo(a)exoplatform.com
* 21 juil. 2009
*/
-public class LRUExoCacheCreator implements ExoCacheCreator
+public class LRUExoCacheCreator extends AbstractExoCacheCreator
{
/**
@@ -63,7 +60,8 @@
/**
* {@inheritDoc}
*/
- public ExoCache<Serializable, Object> create(ExoCacheConfig config,
Cache<Serializable, Object> cache) throws ExoCacheInitException
+ public ExoCache<Serializable, Object> create(ExoCacheConfig config,
Cache<Serializable, Object> cache)
+ throws ExoCacheInitException
{
if (config instanceof LRUExoCacheConfig)
{
@@ -82,18 +80,13 @@
/**
* Creates a new ExoCache instance with the relevant parameters
*/
- private ExoCache<Serializable, Object> create(ExoCacheConfig config,
Cache<Serializable, Object> cache, int maxNodes, long timeToLive,
- long maxAge, long minTimeToLive) throws ExoCacheInitException
+ private ExoCache<Serializable, Object> create(ExoCacheConfig config,
Cache<Serializable, Object> cache,
+ int maxNodes, long timeToLive, long maxAge, long minTimeToLive) throws
ExoCacheInitException
{
- final Configuration configuration = cache.getConfiguration();
final LRUAlgorithmConfig lru = new LRUAlgorithmConfig(timeToLive, maxAge,
maxNodes);
lru.setMinTimeToLive(minTimeToLive);
- // Create an eviction region config
- final EvictionRegionConfig erc = new EvictionRegionConfig(Fqn.ROOT, lru);
-
- final EvictionConfig evictionConfig = configuration.getEvictionConfig();
- evictionConfig.setDefaultEvictionRegionConfig(erc);
- return new LRUExoCache(config, cache, lru);
+ Fqn<String> rooFqn = addEvictionRegion(config, cache, lru);
+ return new LRUExoCache(config, cache, rooFqn, lru);
}
/**
@@ -120,9 +113,10 @@
private final LRUAlgorithmConfig lru;
- public LRUExoCache(ExoCacheConfig config, Cache<Serializable, Object> cache,
LRUAlgorithmConfig lru)
+ public LRUExoCache(ExoCacheConfig config, Cache<Serializable, Object> cache,
Fqn<String> rooFqn,
+ LRUAlgorithmConfig lru)
{
- super(config, cache);
+ super(config, cache, rooFqn);
this.lru = lru;
}
Modified:
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/mru/MRUExoCacheCreator.java
===================================================================
---
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/mru/MRUExoCacheCreator.java 2010-09-08
11:25:25 UTC (rev 3093)
+++
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/main/java/org/exoplatform/services/cache/impl/jboss/mru/MRUExoCacheCreator.java 2010-09-08
16:34:20 UTC (rev 3094)
@@ -24,12 +24,9 @@
import org.exoplatform.services.cache.ExoCacheConfig;
import org.exoplatform.services.cache.ExoCacheInitException;
import org.exoplatform.services.cache.impl.jboss.AbstractExoCache;
-import org.exoplatform.services.cache.impl.jboss.ExoCacheCreator;
+import org.exoplatform.services.cache.impl.jboss.AbstractExoCacheCreator;
import org.jboss.cache.Cache;
import org.jboss.cache.Fqn;
-import org.jboss.cache.config.Configuration;
-import org.jboss.cache.config.EvictionConfig;
-import org.jboss.cache.config.EvictionRegionConfig;
import org.jboss.cache.eviction.MRUAlgorithmConfig;
import java.io.Serializable;
@@ -41,7 +38,7 @@
* exo(a)exoplatform.com
* 21 juil. 2009
*/
-public class MRUExoCacheCreator implements ExoCacheCreator
+public class MRUExoCacheCreator extends AbstractExoCacheCreator
{
/**
@@ -72,16 +69,10 @@
private ExoCache<Serializable, Object> create(ExoCacheConfig config,
Cache<Serializable, Object> cache, int maxNodes, long minTimeToLive)
throws ExoCacheInitException
{
- final Configuration configuration = cache.getConfiguration();
final MRUAlgorithmConfig mru = new MRUAlgorithmConfig(maxNodes);
mru.setMinTimeToLive(minTimeToLive);
- // Create an eviction region config
- final EvictionRegionConfig erc = new EvictionRegionConfig(Fqn.ROOT, mru);
-
- final EvictionConfig evictionConfig = configuration.getEvictionConfig();
- evictionConfig.setDefaultEvictionRegionConfig(erc);
-
- return new AbstractExoCache<Serializable, Object>(config, cache)
+ Fqn<String> rooFqn = addEvictionRegion(config, cache, mru);
+ return new AbstractExoCache<Serializable, Object>(config, cache, rooFqn)
{
public void setMaxSize(int max)
Modified:
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/test/java/org/exoplatform/services/cache/impl/jboss/TestAbstractExoCache.java
===================================================================
---
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/test/java/org/exoplatform/services/cache/impl/jboss/TestAbstractExoCache.java 2010-09-08
11:25:25 UTC (rev 3093)
+++
kernel/trunk/exo.kernel.component.ext.cache.impl.jboss.v3/src/test/java/org/exoplatform/services/cache/impl/jboss/TestAbstractExoCache.java 2010-09-08
16:34:20 UTC (rev 3094)
@@ -19,6 +19,9 @@
package org.exoplatform.services.cache.impl.jboss;
import org.exoplatform.container.PortalContainer;
+import org.exoplatform.container.configuration.ConfigurationManager;
+import org.exoplatform.container.xml.InitParams;
+import org.exoplatform.container.xml.ObjectParameter;
import org.exoplatform.services.cache.CacheListener;
import org.exoplatform.services.cache.CacheListenerContext;
import org.exoplatform.services.cache.CacheService;
@@ -26,7 +29,9 @@
import org.exoplatform.services.cache.ExoCache;
import org.exoplatform.services.cache.ExoCacheConfig;
import org.exoplatform.services.cache.ExoCacheFactory;
+import org.exoplatform.services.cache.ExoCacheInitException;
import org.exoplatform.services.cache.ObjectCacheInfo;
+import org.exoplatform.services.cache.impl.jboss.lru.LRUExoCacheCreator;
import org.exoplatform.test.BasicTestCase;
import java.io.Serializable;
@@ -200,7 +205,8 @@
CachedObjectSelector<Serializable, Object> selector = new
CachedObjectSelector<Serializable, Object>()
{
- public void onSelect(ExoCache<? extends Serializable, ? extends Object>
cache, Serializable key, ObjectCacheInfo<? extends Object> ocinfo) throws Exception
+ public void onSelect(ExoCache<? extends Serializable, ? extends Object>
cache, Serializable key,
+ ObjectCacheInfo<? extends Object> ocinfo) throws Exception
{
assertTrue(key.equals(new MyKey("a")) || key.equals(new
MyKey("b")) || key.equals(new MyKey("c")));
assertTrue(ocinfo.get().equals(1) || ocinfo.get().equals(2) ||
ocinfo.get().equals(3));
@@ -229,10 +235,27 @@
assertEquals(2, cache.getCacheMiss() - misses);
}
+ private ExoCacheFactory getExoCacheFactoryInstance() throws ExoCacheInitException
+ {
+ PortalContainer pc = PortalContainer.getInstance();
+ ExoCacheFactoryImpl factory = new
ExoCacheFactoryImpl("jar:/conf/portal/cache-configuration-template.xml",
(ConfigurationManager)pc
+ .getComponentInstanceOfType(ConfigurationManager.class));
+ InitParams params = new InitParams();
+ ObjectParameter param = new ObjectParameter();
+ param.setName("LRU");
+ param.setObject(new LRUExoCacheCreator());
+ params.addParam(param);
+ ExoCacheCreatorPlugin plugin = new ExoCacheCreatorPlugin(params);
+ factory.addCreator(plugin);
+ return factory;
+ }
+
@SuppressWarnings("unchecked")
public void testDistributedCache() throws Exception
{
- System.out.println("WARNING: For Linux distributions the following JVM
parameter must be set to true, java.net.preferIPv4Stack = " +
System.getProperty("java.net.preferIPv4Stack"));
+ System.out
+ .println("WARNING: For Linux distributions the following JVM parameter must
be set to true, java.net.preferIPv4Stack = "
+ + System.getProperty("java.net.preferIPv4Stack"));
ExoCacheConfig config = new ExoCacheConfig();
config.setName("MyCacheDistributed");
config.setMaxSize(5);
@@ -245,13 +268,16 @@
config2.setLiveTime(1);
config2.setImplementation("LRU");
config2.setDistributed(true);
- AbstractExoCache<Serializable, Object> cache1 =
(AbstractExoCache<Serializable, Object>)factory.createCache(config);
+ AbstractExoCache<Serializable, Object> cache1 =
+ (AbstractExoCache<Serializable,
Object>)getExoCacheFactoryInstance().createCache(config);
MyCacheListener listener1 = new MyCacheListener();
cache1.addCacheListener(listener1);
- AbstractExoCache<Serializable, Object> cache2 =
(AbstractExoCache<Serializable, Object>)factory.createCache(config);
+ AbstractExoCache<Serializable, Object> cache2 =
+ (AbstractExoCache<Serializable,
Object>)getExoCacheFactoryInstance().createCache(config);
MyCacheListener listener2 = new MyCacheListener();
cache2.addCacheListener(listener2);
- AbstractExoCache<Serializable, Object> cache3 =
(AbstractExoCache<Serializable, Object>)factory.createCache(config2);
+ AbstractExoCache<Serializable, Object> cache3 =
+ (AbstractExoCache<Serializable,
Object>)getExoCacheFactoryInstance().createCache(config2);
MyCacheListener listener3 = new MyCacheListener();
cache3.addCacheListener(listener3);
try
@@ -314,7 +340,7 @@
cache1.put(new MyKey("c"), "c");
cache1.clearCache();
assertEquals(0, cache1.getCacheSize());
- assertEquals(null, cache1.get(new MyKey("b")));
+ assertEquals(null, cache1.get(new MyKey("b")));
assertEquals("c", cache2.get(new MyKey("b")));
assertEquals("c", cache2.get(new MyKey("c")));
assertEquals(2, cache2.getCacheSize());
@@ -684,6 +710,7 @@
public static class MyKey implements Serializable
{
private static final long serialVersionUID = 1L;
+
public String value;
public MyKey(String value)