Author: manik.surtani(a)jboss.com
Date: 2007-10-31 12:50:32 -0400 (Wed, 31 Oct 2007)
New Revision: 4715
Modified:
core/trunk/src/main/java/org/jboss/cache/CacheFactory.java
core/trunk/src/main/java/org/jboss/cache/DefaultCacheFactory.java
core/trunk/src/main/java/org/jboss/cache/factories/XmlConfigurationParser.java
core/trunk/src/test/java/org/jboss/cache/CacheFactoryTest.java
Log:
JBCACHE-1208 - allow cache factories to create caches from streams
Modified: core/trunk/src/main/java/org/jboss/cache/CacheFactory.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/CacheFactory.java 2007-10-31 13:59:47 UTC
(rev 4714)
+++ core/trunk/src/main/java/org/jboss/cache/CacheFactory.java 2007-10-31 16:50:32 UTC
(rev 4715)
@@ -10,6 +10,8 @@
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.ConfigurationException;
+import java.io.InputStream;
+
/**
* This factory constructs a cache from a given or default configuration set.
* <p/>
@@ -31,7 +33,6 @@
* @author <a href="mailto:manik@jboss.org">Manik Surtani
(manik(a)jboss.org)</a>
* @see org.jboss.cache.Cache
* @see org.jboss.cache.DefaultCacheFactory
- * @see org.jboss.cache.pojo.PojoCacheFactory
* @since 2.0.0
*/
@ThreadSafe
@@ -109,4 +110,30 @@
* if there are problems with the configuration
*/
Cache<K, V> createCache(Configuration configuration, boolean start) throws
ConfigurationException;
+
+ /**
+ * Creates a {@link Cache} instance based on an {@link java.io.InputStream} passed in,
which should be a stream to a valid
+ * XML configuration file.
+ *
+ * @param is the {@link java.io.InputStream}
+ * @return a running {@link Cache} instance
+ * @throws org.jboss.cache.config.ConfigurationException
+ * if there are problems with the configuration
+ * @since 2.1.0
+ */
+ Cache<K, V> createCache(InputStream is) throws ConfigurationException;
+
+ /**
+ * Creates a {@link Cache} instance based on an {@link java.io.InputStream} passed in,
which should be a stream to a valid
+ * XML configuration file.
+ *
+ * @param is the {@link java.io.InputStream}
+ *
+ * @param start if true, the cache is started before returning.
+ * @return a running {@link Cache} instance
+ * @throws org.jboss.cache.config.ConfigurationException
+ * if there are problems with the configuration
+ * @since 2.1.0
+ */
+ Cache<K, V> createCache(InputStream is, boolean start) throws
ConfigurationException;
}
Modified: core/trunk/src/main/java/org/jboss/cache/DefaultCacheFactory.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/DefaultCacheFactory.java 2007-10-31 13:59:47
UTC (rev 4714)
+++ core/trunk/src/main/java/org/jboss/cache/DefaultCacheFactory.java 2007-10-31 16:50:32
UTC (rev 4715)
@@ -10,6 +10,8 @@
import org.jboss.cache.config.ConfigurationException;
import org.jboss.cache.factories.XmlConfigurationParser;
+import java.io.InputStream;
+
/**
* Default (singleton) implementation of the {@link org.jboss.cache.CacheFactory}
interface.
* Use {@link #getInstance()} to obtain an instance.
@@ -93,4 +95,18 @@
throw new RuntimeException(e);
}
}
+
+ public Cache<K, V> createCache(InputStream is) throws ConfigurationException
+ {
+ XmlConfigurationParser parser = new XmlConfigurationParser();
+ Configuration c = parser.parseStream(is);
+ return createCache(c);
+ }
+
+ public Cache<K, V> createCache(InputStream is, boolean start) throws
ConfigurationException
+ {
+ XmlConfigurationParser parser = new XmlConfigurationParser();
+ Configuration c = parser.parseStream(is);
+ return createCache(c, start);
+ }
}
Modified: core/trunk/src/main/java/org/jboss/cache/factories/XmlConfigurationParser.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/factories/XmlConfigurationParser.java 2007-10-31
13:59:47 UTC (rev 4714)
+++
core/trunk/src/main/java/org/jboss/cache/factories/XmlConfigurationParser.java 2007-10-31
16:50:32 UTC (rev 4715)
@@ -50,6 +50,7 @@
*
* @author <a href="mailto:manik@jboss.org">Manik Surtani
(manik(a)jboss.org)</a>
* @author <a href="mailto:galder.zamarreno@jboss.com">Galder
Zamarreno</a>
+ * @since 2.00.
*/
public class XmlConfigurationParser
{
@@ -59,7 +60,12 @@
public static final String NAME = "name";
/**
- * Parses an XML file and returns a new configuration.
+ * Parses an XML file and returns a new configuration. This method attempts to look
for the file name passed in on
+ * the classpath. If not found, it will search for the file on the file system
instead, treating the name as an
+ * absolute path.
+ *
+ * @param filename the name of the XML file to parse.
+ * @return a configured Configuration object representing the configuration in the
file
*/
public Configuration parseFile(String filename)
{
@@ -83,8 +89,11 @@
/**
* Parses an input stream containing XML text and returns a new configuration.
+ * @param stream input stream to parse. SHould not be null.
+ * @since 2.1.0
+ * @return a configured Configuration object representing the configuration in the
stream
*/
- protected Configuration parseStream(InputStream stream)
+ public Configuration parseStream(InputStream stream)
{
// loop through all elements in XML.
Element root = XmlHelper.getDocumentRoot(stream);
Modified: core/trunk/src/test/java/org/jboss/cache/CacheFactoryTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/CacheFactoryTest.java 2007-10-31 13:59:47 UTC
(rev 4714)
+++ core/trunk/src/test/java/org/jboss/cache/CacheFactoryTest.java 2007-10-31 16:50:32 UTC
(rev 4715)
@@ -6,20 +6,20 @@
*/
package org.jboss.cache;
-import static org.testng.AssertJUnit.assertEquals;
-import static org.testng.AssertJUnit.assertFalse;
-import static org.testng.AssertJUnit.assertTrue;
-
import org.jboss.cache.config.Configuration;
import org.jboss.cache.factories.XmlConfigurationParser;
import org.jboss.cache.lock.IsolationLevel;
+import static org.testng.AssertJUnit.*;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import java.io.InputStream;
+
/**
* @author <a href="mailto:manik@jboss.org">Manik Surtani
(manik(a)jboss.org)</a>
*/
+@Test (groups = {"functional"})
public class CacheFactoryTest
{
Configuration expected;
@@ -42,7 +42,6 @@
}
}
- @Test(groups = {"functional"})
public void testFromConfigFileStarted()
{
cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(configFile);
@@ -53,7 +52,6 @@
doSimpleConfTests(cache.getConfiguration());
}
- @Test(groups = {"functional"})
public void testFromConfigFileUnstarted()
{
cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(configFile,
false);
@@ -65,7 +63,6 @@
doSimpleConfTests(cache.getConfiguration());
}
- @Test(groups = {"functional"})
public void testFromConfigObjStarted()
{
cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(expected);
@@ -75,7 +72,6 @@
doSimpleConfTests(cache.getConfiguration());
}
- @Test(groups = {"functional"})
public void testFromConfigObjUnstarted()
{
cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(expected,
false);
@@ -95,7 +91,6 @@
//
assertEquals("UDP(ip_mcast=true;ip_ttl=64;loopback=false;mcast_addr=228.1.2.3;mcast_port=48866;mcast_recv_buf_size=80000;mcast_send_buf_size=150000;ucast_recv_buf_size=80000;ucast_send_buf_size=150000):PING(down_thread=false;num_initial_members=3;timeout=2000;up_thread=false):MERGE2(max_interval=20000;min_interval=10000):FD_SOCK:VERIFY_SUSPECT(down_thread=false;timeout=1500;up_thread=false):pbcast.NAKACK(down_thread=false;gc_lag=50;max_xmit_size=8192;retransmit_timeout=600,1200,2400,4800;up_thread=false):UNICAST(down_thread=false;min_threshold=10;timeout=600,1200,2400;window_size=100):pbcast.STABLE(desired_avg_gossip=20000;down_thread=false;up_thread=false):FRAG(down_thread=false;frag_size=8192;up_thread=false):pbcast.GMS(join_retry_timeout=2000;join_timeout=5000;print_local_addr=true;shun=true):pbcast.STATE_TRANSFER(down_thread=true;up_thread=true)",
tc.getClusterConfig());
}
- @Test(groups = {"functional"})
public void testLifecycle() throws Exception
{
cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(expected,
false);
@@ -105,4 +100,22 @@
cache.stop();
assertFalse(cache.isStarted());
}
+
+ public void testCreationFromStreamStarted() throws Exception
+ {
+ InputStream is = getClass().getClassLoader().getResourceAsStream(configFile);
+ CacheFactory cf = DefaultCacheFactory.getInstance();
+ cache = (CacheImpl) cf.createCache(is);
+ assertTrue("Should have started", cache.isStarted());
+ doSimpleConfTests(cache.getConfiguration());
+ }
+
+ public void testCreationFromStream() throws Exception
+ {
+ InputStream is = getClass().getClassLoader().getResourceAsStream(configFile);
+ CacheFactory cf = DefaultCacheFactory.getInstance();
+ cache = (CacheImpl) cf.createCache(is, false);
+ assertFalse("Should not have started", cache.isStarted());
+ doSimpleConfTests(cache.getConfiguration());
+ }
}