From jbosscache-commits at lists.jboss.org Wed Oct 31 12:54:34 2007 Content-Type: multipart/mixed; boundary="===============8907427446835397787==" MIME-Version: 1.0 From: jbosscache-commits at lists.jboss.org To: jbosscache-commits at lists.jboss.org Subject: [jbosscache-commits] JBoss Cache SVN: r4715 - in core/trunk/src: main/java/org/jboss/cache/factories and 1 other directories. Date: Wed, 31 Oct 2007 12:50:32 -0400 Message-ID: --===============8907427446835397787== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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/XmlConfigurationParse= r.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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- core/trunk/src/main/java/org/jboss/cache/CacheFactory.java 2007-10-31 1= 3:59:47 UTC (rev 4714) +++ core/trunk/src/main/java/org/jboss/cache/CacheFactory.java 2007-10-31 1= 6: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 s= et. *

@@ -31,7 +33,6 @@ * @author Manik Surtani (manik(a)jbo= ss.org) * @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 createCache(Configuration configuration, boolean start) thr= ows ConfigurationException; + + /** + * Creates a {@link Cache} instance based on an {@link java.io.InputStr= eam} 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 createCache(InputStream is) throws ConfigurationException; + + /** + * Creates a {@link Cache} instance based on an {@link java.io.InputStr= eam} 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 createCache(InputStream is, boolean start) throws Configura= tionException; } Modified: core/trunk/src/main/java/org/jboss/cache/DefaultCacheFactory.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 --- 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.CacheF= actory} interface. * Use {@link #getInstance()} to obtain an instance. @@ -93,4 +95,18 @@ throw new RuntimeException(e); } } + + public Cache createCache(InputStream is) throws ConfigurationExce= ption + { + XmlConfigurationParser parser =3D new XmlConfigurationParser(); + Configuration c =3D parser.parseStream(is); + return createCache(c); + } + + public Cache createCache(InputStream is, boolean start) throws Co= nfigurationException + { + XmlConfigurationParser parser =3D new XmlConfigurationParser(); + Configuration c =3D parser.parseStream(is); + return createCache(c, start); + } = } Modified: core/trunk/src/main/java/org/jboss/cache/factories/XmlConfigurati= onParser.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 --- core/trunk/src/main/java/org/jboss/cache/factories/XmlConfigurationPars= er.java 2007-10-31 13:59:47 UTC (rev 4714) +++ core/trunk/src/main/java/org/jboss/cache/factories/XmlConfigurationPars= er.java 2007-10-31 16:50:32 UTC (rev 4715) @@ -50,6 +50,7 @@ * * @author Manik Surtani (manik(a)jbo= ss.org) * @author Galder Zamarren= o + * @since 2.00. */ public class XmlConfigurationParser { @@ -59,7 +60,12 @@ public static final String NAME =3D "name"; = /** - * Parses an XML file and returns a new configuration. + * Parses an XML file and returns a new configuration. This method att= empts to look for the file name passed in on + * the classpath. If not found, it will search for the file on the fil= e 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 configura= tion in the file */ public Configuration parseFile(String filename) { @@ -83,8 +89,11 @@ = /** * Parses an input stream containing XML text and returns a new configu= ration. + * @param stream input stream to parse. SHould not be null. + * @since 2.1.0 + * @return a configured Configuration object representing the configura= tion in the stream */ - protected Configuration parseStream(InputStream stream) + public Configuration parseStream(InputStream stream) { // loop through all elements in XML. Element root =3D XmlHelper.getDocumentRoot(stream); Modified: core/trunk/src/test/java/org/jboss/cache/CacheFactoryTest.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 --- 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 Manik Surtani (manik(a)jbo= ss.org) */ +(a)Test (groups =3D {"functional"}) public class CacheFactoryTest { Configuration expected; @@ -42,7 +42,6 @@ } } = - @Test(groups =3D {"functional"}) public void testFromConfigFileStarted() { cache =3D (CacheImpl) DefaultCacheFactory.getInstance().createCache(= configFile); @@ -53,7 +52,6 @@ doSimpleConfTests(cache.getConfiguration()); } = - @Test(groups =3D {"functional"}) public void testFromConfigFileUnstarted() { cache =3D (CacheImpl) DefaultCacheFactory.getInstance().createCache(= configFile, false); @@ -65,7 +63,6 @@ doSimpleConfTests(cache.getConfiguration()); } = - @Test(groups =3D {"functional"}) public void testFromConfigObjStarted() { cache =3D (CacheImpl) DefaultCacheFactory.getInstance().createCache(= expected); @@ -75,7 +72,6 @@ doSimpleConfTests(cache.getConfiguration()); } = - @Test(groups =3D {"functional"}) public void testFromConfigObjUnstarted() { cache =3D (CacheImpl) DefaultCacheFactory.getInstance().createCache(= expected, false); @@ -95,7 +91,6 @@ // assertEquals("UDP(ip_mcast=3Dtrue;ip_ttl=3D64;loopback=3Dfalse;mc= ast_addr=3D228.1.2.3;mcast_port=3D48866;mcast_recv_buf_size=3D80000;mcast_s= end_buf_size=3D150000;ucast_recv_buf_size=3D80000;ucast_send_buf_size=3D150= 000):PING(down_thread=3Dfalse;num_initial_members=3D3;timeout=3D2000;up_thr= ead=3Dfalse):MERGE2(max_interval=3D20000;min_interval=3D10000):FD_SOCK:VERI= FY_SUSPECT(down_thread=3Dfalse;timeout=3D1500;up_thread=3Dfalse):pbcast.NAK= ACK(down_thread=3Dfalse;gc_lag=3D50;max_xmit_size=3D8192;retransmit_timeout= =3D600,1200,2400,4800;up_thread=3Dfalse):UNICAST(down_thread=3Dfalse;min_th= reshold=3D10;timeout=3D600,1200,2400;window_size=3D100):pbcast.STABLE(desir= ed_avg_gossip=3D20000;down_thread=3Dfalse;up_thread=3Dfalse):FRAG(down_thre= ad=3Dfalse;frag_size=3D8192;up_thread=3Dfalse):pbcast.GMS(join_retry_timeou= t=3D2000;join_timeout=3D5000;print_local_addr=3Dtrue;shun=3Dtrue):pbcast.ST= ATE_TRANSFER(down_thread=3Dtrue;up_thread=3Dtrue)", tc.getClusterConfig()); } = - @Test(groups =3D {"functional"}) public void testLifecycle() throws Exception { cache =3D (CacheImpl) DefaultCacheFactory.getInstance().createCache(= expected, false); @@ -105,4 +100,22 @@ cache.stop(); assertFalse(cache.isStarted()); } + + public void testCreationFromStreamStarted() throws Exception + { + InputStream is =3D getClass().getClassLoader().getResourceAsStream(c= onfigFile); + CacheFactory cf =3D DefaultCacheFactory.getInstance(); + cache =3D (CacheImpl) cf.createCache(is); + assertTrue("Should have started", cache.isStarted()); + doSimpleConfTests(cache.getConfiguration()); + } + = + public void testCreationFromStream() throws Exception + { + InputStream is =3D getClass().getClassLoader().getResourceAsStream(c= onfigFile); + CacheFactory cf =3D DefaultCacheFactory.getInstance(); + cache =3D (CacheImpl) cf.createCache(is, false); + assertFalse("Should not have started", cache.isStarted()); + doSimpleConfTests(cache.getConfiguration()); + } } --===============8907427446835397787==--