[jbosscache-commits] JBoss Cache SVN: r7282 - in core/branches/flat/src: test/java/org/jboss/starobrno and 3 other directories.
jbosscache-commits at lists.jboss.org
jbosscache-commits at lists.jboss.org
Thu Dec 11 11:33:03 EST 2008
Author: manik.surtani at jboss.com
Date: 2008-12-11 11:33:03 -0500 (Thu, 11 Dec 2008)
New Revision: 7282
Added:
core/branches/flat/src/main/java/org/jboss/starobrno/manager/CacheNameExistsException.java
core/branches/flat/src/main/java/org/jboss/starobrno/manager/NamedCacheNotFoundException.java
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/manager/CacheManager.java
core/branches/flat/src/test/java/org/jboss/starobrno/BasicTest.java
core/branches/flat/src/test/java/org/jboss/starobrno/UnitTestCacheManager.java
core/branches/flat/src/test/java/org/jboss/starobrno/api/tree/TreeCacheAPITest.java
core/branches/flat/src/test/java/org/jboss/starobrno/replication/AsyncReplTest.java
core/branches/flat/src/test/java/org/jboss/starobrno/tx/LocalModeTxTest.java
Log:
Cache manager improvements
Modified: core/branches/flat/src/main/java/org/jboss/starobrno/manager/CacheManager.java
===================================================================
--- core/branches/flat/src/main/java/org/jboss/starobrno/manager/CacheManager.java 2008-12-11 14:44:12 UTC (rev 7281)
+++ core/branches/flat/src/main/java/org/jboss/starobrno/manager/CacheManager.java 2008-12-11 16:33:03 UTC (rev 7282)
@@ -21,83 +21,265 @@
*/
package org.jboss.starobrno.manager;
-import org.jboss.cache.DefaultCacheFactory;
import org.jboss.starobrno.Cache;
import org.jboss.starobrno.config.Configuration;
+import org.jboss.starobrno.config.ConfigurationException;
+import org.jboss.starobrno.config.parsing.XmlConfigurationParser;
import org.jboss.starobrno.lifecycle.Lifecycle;
-import org.jboss.starobrno.tree.TreeCache;
-import org.jboss.starobrno.tree.TreeCacheImpl;
+import java.io.InputStream;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
- * // TODO: MANIK: Document this
+ * A <tt>CacheManager</tt> is the primary mechanism for retrieving a {@link Cache} instance, and is often used as a
+ * starting point to using the {@link Cache}.
+ * <p/>
+ * <tt>CacheManager</tt>s are heavyweight objects, and we foresee no more than one <tt>CacheManager</tt> being used per
+ * JVM (unless specific configuration requirements require more than one; but either way, this would be a minimal and
+ * finite number of instances).
+ * <p/>
+ * Constructing a <tt>CacheManager</tt> is done via one of its constructors, which optionally take in a
+ * {@link org.jboss.starobrno.config.Configuration} or a path or URL to a configuration XML file.
+ * <p/>
+ * Lifecycle - <tt>CacheManager</tt>s have a lifecycle (it implements {@link Lifecycle}) and the default constructors also
+ * call {@link #start()}. Overloaded versions of the constructors are available, that do not start the <tt>CacheManager</tt>,
+ * although it must be kept in mind that <tt>CacheManager</tt>s need to be started before they can be used to create <tt>Cache</tt>
+ * instances.
+ * <p/>
+ * Once constructed, <tt>CacheManager</tt>s should be made available to any component that requires a <tt>Cache</tt>,
+ * via JNDI or via some other mechanism such as an IoC container.
+ * <p/>
+ * You obtain <tt>Cache</tt> instances from the <tt>CacheManager</tt> by using one of the overloaded <tt>getCache()</tt>,
+ * methods. Note that with <tt>getCache()</tt>, there is no guarantee that the instance you get is brand-new and empty,
+ * since caches are named and shared. Because of this, the <tt>CacheManager</tt> also acts as a repository of <tt>Cache</tt>s,
+ * and is an effective mechanism of looking up or creating <tt>Cache</tt>s on demand.
+ * <p/>
+ * When the system shuts down, it should call {@link #stop()} on the <tt>CacheManager</tt>. This will ensure all caches
+ * within its scope are properly stopped as well.
+ * <p/>
+ * Sample usage:
+ * <code>
+ * CacheManager manager = CacheManager.getInstance("my-config-file.xml");
+ * Cache entityCache = manager.getCache("myEntityCache");
+ * entityCache.put("aPerson", new Person());
+ * <p/>
+ * Configuration myNewConfiguration = new Configuration();
+ * myNewConfiguration.setCacheMode(Configuration.CacheMode.LOCAL);
+ * manager.defineCache("myLocalCache", myNewConfiguration);
+ * Cache localCache = manager.getCache("myLocalCache");
+ * </code>
*
* @author Manik Surtani (<a href="mailto:manik at jboss.org">manik at jboss.org</a>)
- * @since 3.0
*/
public class CacheManager implements Lifecycle
{
- protected Configuration c;
+ public static final String DEFAULT_CACHE_NAME = "org.jboss.starobrno.manager.CacheManager.DEFAULT_CACHE_NAME";
+ protected Configuration configuration;
private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();
- private final ConcurrentMap<String, TreeCache> treeCaches = new ConcurrentHashMap<String, TreeCache>();
+ private final ConcurrentMap<String, Configuration> configurationOverrides = new ConcurrentHashMap<String, Configuration>();
- public CacheManager(Configuration c)
+ /**
+ * Constructs and starts a default instance of the CacheManager, using configuration defaults.
+ */
+ public CacheManager()
{
- //if a config is shared between multiple managers, then each registers it's
- // own chnnel in runtime
- this.c = c.clone();
+ this(false);
}
- public void start()
+ /**
+ * Constructs a default instance of the CacheManager, using configuration defaults.
+ *
+ * @param start if true, the cache manager is started
+ */
+ public CacheManager(boolean start)
{
- // this will bootstrap the cache manager
+ configuration = new Configuration();
+ if (start) start();
}
- public void stop()
+ /**
+ * Constructs and starts a new instance of the CacheManager, using the configuration file passed in.
+ *
+ * @param configuration configuration file to use as a template for all caches created
+ */
+ public CacheManager(Configuration configuration)
{
- // this will stop the cache manager.
+ this(configuration, true);
}
- public Cache createCache(String cacheName)
+ /**
+ * Constructs a new instance of the CacheManager, using the configuration file passed in.
+ *
+ * @param configuration configuration file to use as a template for all caches created
+ * @param start if true, the cache manager is started
+ */
+ public CacheManager(Configuration configuration, boolean start)
{
- if (!caches.containsKey(cacheName))
- caches.putIfAbsent(cacheName, createNewCache());
+ this.configuration = configuration.clone();
+ if (start) start();
+ }
- Cache c = caches.get(cacheName);
- c.start();
- return c;
+ /**
+ * Constructs and starts a new instance of the CacheManager, using the configuration file name passed in. This constructor
+ * first searches for the named file on the classpath, and failing that, treats the file name as an absolute
+ * path.
+ *
+ * @param configurationFile name of configuration file to use as a template for all caches created
+ * @throws org.jboss.starobrno.config.ConfigurationException
+ * if there is a problem with the configuration file.
+ */
+ public CacheManager(String configurationFile) throws ConfigurationException
+ {
+ this(configurationFile, true);
}
- public TreeCache createTreeCache(String cacheName)
+ /**
+ * Constructs a new instance of the CacheManager, using the configuration file name passed in. This constructor
+ * first searches for the named file on the classpath, and failing that, treats the file name as an absolute
+ * path.
+ *
+ * @param configurationFile name of configuration file to use as a template for all caches created
+ * @param start if true, the cache manager is started
+ * @throws org.jboss.starobrno.config.ConfigurationException
+ * if there is a problem with the configuration file.
+ */
+ public CacheManager(String configurationFile, boolean start) throws ConfigurationException
{
- if (!treeCaches.containsKey(cacheName))
- treeCaches.putIfAbsent(cacheName, createTreeCache());
- TreeCache tc = treeCaches.get(cacheName);
- return tc;
+ try
+ {
+ // todo - need to update parser as per https://docspace.corp.redhat.com/clearspace/docs/DOC-16643
+ // todo - need to store named cache overrides
+ XmlConfigurationParser parser = new XmlConfigurationParser();
+ configuration = parser.parseFile(configurationFile);
+ }
+ catch (ConfigurationException ce)
+ {
+ throw ce;
+ }
+ catch (RuntimeException re)
+ {
+ throw new ConfigurationException(re);
+ }
+ if (start) start();
}
- public void destroyCache(String cacheName)
+ /**
+ * Constructs and starts a new instance of the CacheManager, using the input stream passed in to read configuration file contents.
+ *
+ * @param configurationStream stream containing configuration file contents, to use as a template for all caches created
+ * @throws org.jboss.starobrno.config.ConfigurationException
+ * if there is a problem with the configuration stream.
+ */
+ public CacheManager(InputStream configurationStream) throws ConfigurationException
{
- Cache c = caches.remove(cacheName);
- if (c != null) c.stop();
+ this(configurationStream, true);
}
- protected TreeCache createTreeCache()
+ /**
+ * Constructs a new instance of the CacheManager, using the input stream passed in to read configuration file contents.
+ *
+ * @param configurationStream stream containing configuration file contents, to use as a template for all caches created
+ * @param start if true, the cache manager is started
+ * @throws org.jboss.starobrno.config.ConfigurationException
+ * if there is a problem with the configuration stream.
+ */
+ public CacheManager(InputStream configurationStream, boolean start) throws ConfigurationException
{
- Configuration cfg = null;
- cfg = c.clone();
+ try
+ {
+ // todo - need to update parser as per https://docspace.corp.redhat.com/clearspace/docs/DOC-16643
+ // todo - need to store named cache overrides
+ XmlConfigurationParser parser = new XmlConfigurationParser();
+ configuration = parser.parseStream(configurationStream);
+ }
+ catch (ConfigurationException ce)
+ {
+ throw ce;
+ }
+ catch (RuntimeException re)
+ {
+ throw new ConfigurationException(re);
+ }
+ if (start) start();
+ }
- cfg.setInvocationBatchingEnabled(true); // needed for the TreeCache
- DefaultCacheFactory dcf = new DefaultCacheFactory();
- return new TreeCacheImpl(dcf.createCache(cfg));
+ /**
+ * Defines a named cache. Named caches can be defined by using this method, in which case the configuration
+ * passed in is used to override the default configuration used when this cache manager instance was created.
+ * <p/>
+ * The other way to define named caches is declaratively, in the XML file passed in to the cache manager.
+ * <p/>
+ * A combination of approaches may also be used, provided the names do not conflict.
+ *
+ * @param cacheName name of cache to define
+ * @param configurationOverride configuration overrides to use
+ * @throws CacheNameExistsException if the name is already in use.
+ */
+ public void defineCache(String cacheName, Configuration configurationOverride) throws CacheNameExistsException
+ {
+ if (cacheName == null || configurationOverrides == null)
+ throw new NullPointerException("Null arguments not allowed");
+ if (cacheName.equals(DEFAULT_CACHE_NAME))
+ throw new IllegalArgumentException("Cache name cannot be used as it is a reserved, internal name");
+ if (configurationOverrides.putIfAbsent(cacheName, configurationOverride) != null)
+ throw new CacheNameExistsException("Cache name [" + cacheName + "] already in use!");
}
- protected Cache createNewCache()
+
+ /**
+ * Retrieves the default cache associated with this cache manager. Note that the default cache does not need to be
+ * explicitly created with {@link #createCache(String)} since it is automatically created lazily when first used.
+ * <p/>
+ * As such, this method is always guaranteed to return the default cache.
+ *
+ * @return the default cache.
+ */
+ public Cache getCache()
{
- // for now latch on to the existing cache creation mechanisms
- DefaultCacheFactory dcf = new DefaultCacheFactory();
- return dcf.createCache(c);
+ return getOrCreateCache(DEFAULT_CACHE_NAME);
}
+
+ /**
+ * Retrieves a named cache from the system. The named cache must have been created previously using the {@link #createCache(String)}
+ * or {@link #getOrCreateCache(String)} methods, or have been declared in the configuration file used by this <tt>CacheManager</tt>
+ * instance, or this method will throw a {@link org.jboss.starobrno.manager.NamedCacheNotFoundException}.
+ *
+ * @param cacheName name of cache to retrieve
+ * @return a cache instance identified by cacheName
+ * @throws NamedCacheNotFoundException if the named cache does not exist, or is not defined declaratively.
+ */
+ public Cache getCache(String cacheName) throws NamedCacheNotFoundException
+ {
+ if (cacheName == null) throw new NullPointerException("Null arguments not allowed");
+ return getOrCreateCache(cacheName);
+ }
+
+ private Cache getOrCreateCache(String cacheName)
+ {
+ // todo
+ return null;
+ }
+
+
+ private Cache createCache(String cacheName)
+ {
+ // todo
+// if (!caches.containsKey(cacheName))
+// caches.putIfAbsent(cacheName, createNewCache());
+
+ Cache c = caches.get(cacheName);
+ c.start();
+ return c;
+ }
+
+ public void start()
+ {
+ // todo
+ }
+
+ public void stop()
+ {
+ // todo
+ }
}
Added: core/branches/flat/src/main/java/org/jboss/starobrno/manager/CacheNameExistsException.java
===================================================================
--- core/branches/flat/src/main/java/org/jboss/starobrno/manager/CacheNameExistsException.java (rev 0)
+++ core/branches/flat/src/main/java/org/jboss/starobrno/manager/CacheNameExistsException.java 2008-12-11 16:33:03 UTC (rev 7282)
@@ -0,0 +1,26 @@
+package org.jboss.starobrno.manager;
+
+/**
+ * Thrown if a cache creation is attempted but the name already exists
+ */
+public class CacheNameExistsException extends Exception
+{
+ public CacheNameExistsException()
+ {
+ }
+
+ public CacheNameExistsException(String message)
+ {
+ super(message);
+ }
+
+ public CacheNameExistsException(String message, Throwable cause)
+ {
+ super(message, cause);
+ }
+
+ public CacheNameExistsException(Throwable cause)
+ {
+ super(cause);
+ }
+}
Added: core/branches/flat/src/main/java/org/jboss/starobrno/manager/NamedCacheNotFoundException.java
===================================================================
--- core/branches/flat/src/main/java/org/jboss/starobrno/manager/NamedCacheNotFoundException.java (rev 0)
+++ core/branches/flat/src/main/java/org/jboss/starobrno/manager/NamedCacheNotFoundException.java 2008-12-11 16:33:03 UTC (rev 7282)
@@ -0,0 +1,26 @@
+package org.jboss.starobrno.manager;
+
+/**
+ * Thrown when a named cache cannot be found.
+ */
+public class NamedCacheNotFoundException extends Exception
+{
+ public NamedCacheNotFoundException()
+ {
+ }
+
+ public NamedCacheNotFoundException(String message)
+ {
+ super(message);
+ }
+
+ public NamedCacheNotFoundException(String message, Throwable cause)
+ {
+ super(message, cause);
+ }
+
+ public NamedCacheNotFoundException(Throwable cause)
+ {
+ super(cause);
+ }
+}
Modified: core/branches/flat/src/test/java/org/jboss/starobrno/BasicTest.java
===================================================================
--- core/branches/flat/src/test/java/org/jboss/starobrno/BasicTest.java 2008-12-11 14:44:12 UTC (rev 7281)
+++ core/branches/flat/src/test/java/org/jboss/starobrno/BasicTest.java 2008-12-11 16:33:03 UTC (rev 7282)
@@ -21,17 +21,18 @@
*/
package org.jboss.starobrno;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.jboss.starobrno.config.Configuration;
import org.jboss.starobrno.manager.CacheManager;
+import org.jboss.starobrno.manager.NamedCacheNotFoundException;
import org.jboss.starobrno.util.TestingUtil;
import org.testng.annotations.Test;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
@Test(groups = "functional")
public class BasicTest
{
- public void basicTest()
+ public void basicTest() throws Exception
{
// create a cache manager
Configuration c = new Configuration(); // LOCAL mode
@@ -40,7 +41,7 @@
try
{
cm.start();
- Cache cache = cm.createCache("test");
+ Cache cache = cm.getCache("test");
String key = "key", value = "value";
assert cache.isEmpty();
@@ -60,13 +61,13 @@
}
finally
{
- cm.destroyCache("test");
cm.stop();
}
}
public static final Log log = LogFactory.getLog(BasicTest.class);
- public void testBasicReplication()
+
+ public void testBasicReplication() throws NamedCacheNotFoundException
{
Configuration configuration = new Configuration();
configuration.setCacheMode(Configuration.CacheMode.REPL_SYNC);
@@ -79,13 +80,13 @@
firstManager.start();
secondManager.start();
- CacheSPI firstCache = (CacheSPI) firstManager.createCache("test");
- CacheSPI secondCache = (CacheSPI) secondManager.createCache("test");
+ CacheSPI firstCache = (CacheSPI) firstManager.getCache("test");
+ CacheSPI secondCache = (CacheSPI) secondManager.getCache("test");
TestingUtil.blockUntilViewReceived(secondCache, 2, 3000);
- firstCache.put("key","value");
+ firstCache.put("key", "value");
assert secondCache.get("key").equals("value");
assert firstCache.get("key").equals("value");
@@ -93,10 +94,11 @@
assert firstCache.get("key").equals("value2");
firstCache.remove("key");
assert secondCache.get("key") == null;
- } finally
+ }
+ finally
{
- firstManager.destroyCache("test");
- secondManager.destroyCache("test");
+ firstManager.stop();
+ secondManager.stop();
}
}
Modified: core/branches/flat/src/test/java/org/jboss/starobrno/UnitTestCacheManager.java
===================================================================
--- core/branches/flat/src/test/java/org/jboss/starobrno/UnitTestCacheManager.java 2008-12-11 14:44:12 UTC (rev 7281)
+++ core/branches/flat/src/test/java/org/jboss/starobrno/UnitTestCacheManager.java 2008-12-11 16:33:03 UTC (rev 7282)
@@ -6,22 +6,27 @@
package org.jboss.starobrno;
import org.jboss.starobrno.config.Configuration;
-import org.jboss.starobrno.manager.CacheManager;
/**
- *
* @author dpospisi
*/
-public class UnitTestCacheManager extends CacheManager
+public class UnitTestCacheManager
{
- public UnitTestCacheManager(Configuration c) {
- super(c);
+ Configuration c;
+
+ public UnitTestCacheManager(Configuration c)
+ {
+ this.c = c;
}
-
- @Override
- protected Cache createNewCache()
+
+ protected Cache createNewCache()
{
UnitTestCacheFactory dcf = new UnitTestCacheFactory();
- return dcf.createCache(c);
+ return dcf.createCache(c);
}
+
+ public Cache createCache(String name)
+ {
+ return createNewCache();
+ }
}
Modified: core/branches/flat/src/test/java/org/jboss/starobrno/api/tree/TreeCacheAPITest.java
===================================================================
--- core/branches/flat/src/test/java/org/jboss/starobrno/api/tree/TreeCacheAPITest.java 2008-12-11 14:44:12 UTC (rev 7281)
+++ core/branches/flat/src/test/java/org/jboss/starobrno/api/tree/TreeCacheAPITest.java 2008-12-11 16:33:03 UTC (rev 7282)
@@ -1,5 +1,6 @@
package org.jboss.starobrno.api.tree;
+import org.jboss.starobrno.Cache;
import org.jboss.starobrno.CacheSPI;
import org.jboss.starobrno.config.Configuration;
import org.jboss.starobrno.manager.CacheManager;
@@ -7,6 +8,7 @@
import org.jboss.starobrno.tree.Fqn;
import org.jboss.starobrno.tree.Node;
import org.jboss.starobrno.tree.TreeCache;
+import org.jboss.starobrno.tree.TreeCacheImpl;
import org.jboss.starobrno.util.TestingUtil;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.AfterMethod;
@@ -37,7 +39,9 @@
c.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
CacheManager cm = new CacheManager(c);
- cache = cm.createTreeCache(getClass().getSimpleName());
+ Cache flatcache = cm.getCache(getClass().getSimpleName());
+ cache = new TreeCacheImpl(flatcache);
+
tm = ((CacheSPI) cache.getCache()).getTransactionManager();
}
Modified: core/branches/flat/src/test/java/org/jboss/starobrno/replication/AsyncReplTest.java
===================================================================
--- core/branches/flat/src/test/java/org/jboss/starobrno/replication/AsyncReplTest.java 2008-12-11 14:44:12 UTC (rev 7281)
+++ core/branches/flat/src/test/java/org/jboss/starobrno/replication/AsyncReplTest.java 2008-12-11 16:33:03 UTC (rev 7282)
@@ -12,7 +12,6 @@
import org.jboss.starobrno.CacheSPI;
import org.jboss.starobrno.UnitTestCacheManager;
import org.jboss.starobrno.config.Configuration;
-import org.jboss.starobrno.manager.CacheManager;
import org.jboss.starobrno.transaction.DummyTransactionManagerLookup;
import org.jboss.starobrno.util.TestingUtil;
import org.jboss.starobrno.util.internals.ReplicationListener;
@@ -35,7 +34,7 @@
{
private Configuration configuration;
private CacheSPI<Object, Object> cache1, cache2;
- private CacheManager cacheManager1, cacheManager2;
+ private UnitTestCacheManager cacheManager1, cacheManager2;
private ReplicationListener replListener1, replListener2;
}
Modified: core/branches/flat/src/test/java/org/jboss/starobrno/tx/LocalModeTxTest.java
===================================================================
--- core/branches/flat/src/test/java/org/jboss/starobrno/tx/LocalModeTxTest.java 2008-12-11 14:44:12 UTC (rev 7281)
+++ core/branches/flat/src/test/java/org/jboss/starobrno/tx/LocalModeTxTest.java 2008-12-11 16:33:03 UTC (rev 7282)
@@ -24,7 +24,6 @@
import org.jboss.starobrno.CacheSPI;
import org.jboss.starobrno.UnitTestCacheManager;
import org.jboss.starobrno.config.Configuration;
-import org.jboss.starobrno.manager.CacheManager;
import org.jboss.starobrno.transaction.DummyTransactionManagerLookup;
import org.jboss.starobrno.util.TestingUtil;
import org.testng.annotations.Test;
@@ -39,7 +38,7 @@
{
Configuration cfg = new Configuration();
cfg.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
- CacheManager cm = new UnitTestCacheManager(cfg);
+ UnitTestCacheManager cm = new UnitTestCacheManager(cfg);
return (CacheSPI<String, String>) cm.createCache("test");
}
More information about the jbosscache-commits
mailing list