[jboss-cvs] JBossCache/src/org/jboss/cache/factories ...
Manik Surtani
msurtani at jboss.com
Wed Jul 19 17:34:43 EDT 2006
User: msurtani
Date: 06/07/19 17:34:43
Modified: src/org/jboss/cache/factories DefaultCacheFactory.java
InterceptorChainFactory.java
Added: src/org/jboss/cache/factories CacheFactoryTest.java
Log:
JBCACHE-657
JBCACHE-594
Revision Changes Path
1.5 +77 -15 JBossCache/src/org/jboss/cache/factories/DefaultCacheFactory.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: DefaultCacheFactory.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/factories/DefaultCacheFactory.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- DefaultCacheFactory.java 19 Jul 2006 08:29:17 -0000 1.4
+++ DefaultCacheFactory.java 19 Jul 2006 21:34:43 -0000 1.5
@@ -12,13 +12,17 @@
import org.jboss.cache.TreeCache;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.ConfigurationException;
-import org.jboss.cache.config.ConfigurationImpl;
+import org.jboss.cache.xml.XmlHelper;
import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.io.InputStream;
import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Set;
/**
@@ -37,8 +41,7 @@
public Cache createCache(String configFileName, boolean start) throws ConfigurationException
{
- ConfigurationImpl c = new ConfigurationImpl();
- c.readConfiguration(getAsInputStreamFromClassLoader(configFileName));
+ Configuration c = readConfigurationStream(getAsInputStreamFromClassLoader(configFileName));
return createCache(c, start);
}
@@ -53,7 +56,6 @@
{
TreeCache cache = new TreeCache();
cache.setConfiguration(configuration);
- configureCache(cache, configuration);
if (start) cache.startService();
return cache.getSPI();
}
@@ -64,23 +66,83 @@
}
}
- protected InputStream getAsInputStreamFromClassLoader(String filename)
+ InputStream getAsInputStreamFromClassLoader(String filename)
{
return Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
}
- protected void configureCache(TreeCache cache, Configuration conf)
+ Configuration readConfigurationStream(InputStream stream)
{
- configureCache(cache, conf, conf.getSimpleAttributeNames(), false);
- configureCache(cache, conf, conf.getXmlAttributeNames(), true);
+ // loop through all elements in XML.
+ if (stream == null) throw new ConfigurationException("Input stream for configuration xml is null!");
+
+ Map<String, String> stringAttribs = new HashMap<String, String>();
+ Map<String, Element> xmlAttribs = new HashMap<String, Element>();
+
+ Element root= XmlHelper.getDocumentRoot(stream);
+ Element mbeanElement=getMBeanElement(root);
+ NodeList list=mbeanElement.getElementsByTagName(XmlHelper.ATTR);
+ log.info("Attribute size: " + list.getLength());
+
+
+ // loop through attributes
+ for(int loop=0; loop < list.getLength(); loop++)
+ {
+ Node node=list.item(loop);
+ if(node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) continue;
+
+ // for each element (attribute) ...
+ Element element=(Element)node;
+ String name = element.getAttribute(XmlHelper.NAME);
+ String valueStr = XmlHelper.getElementContent(element, true);
+
+ Element valueXml=null;
+ if(valueStr.length() == 0)
+ {
+ // This may be an XML element ...
+ valueXml = XmlHelper.getConfigSubElement(element);
+ }
+
+ // add these to the maps.
+
+ if (valueStr.length() > 0) stringAttribs.put(name, valueStr);
+ if (valueXml != null) xmlAttribs.put(name, valueXml);
+ }
+
+ Configuration c = new Configuration();
+ setValues(c, stringAttribs.keySet(), stringAttribs, false);
+ setValues(c, xmlAttribs.keySet(), xmlAttribs, true);
+ return c;
+ }
+
+ private Element getMBeanElement(Element root)
+ {
+ // This is following JBoss convention.
+ NodeList list=root.getElementsByTagName(XmlHelper.ROOT);
+ if(list == null) throw new ConfigurationException("Can't find " + XmlHelper.ROOT + " tag");
+
+ if(list.getLength() > 1) throw new ConfigurationException("Has multiple " + XmlHelper.ROOT + " tag");
+
+ Node node=list.item(0);
+ Element element=null;
+ if(node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE)
+ {
+ element=(Element)node;
+ }
+ else
+ {
+ throw new ConfigurationException("Can't find " + XmlHelper.ROOT + " element");
+ }
+ return element;
}
- protected void configureCache(TreeCache cache, Configuration conf, Set<String> attributes, boolean isXmlAttribs)
+
+ private void setValues(Configuration conf, Set<String> keys, Map attribs, boolean isXmlAttribs)
{
- Class objectClass = cache.getClass();
+ Class objectClass = conf.getClass();
// go thru simple string setters first.
- for (String propName : attributes)
+ for (String propName : keys)
{
String setter = "set" + propName;
Method method = null;
@@ -90,12 +152,12 @@
if (isXmlAttribs)
{
method = objectClass.getMethod(setter, new Class[]{Element.class});
- method.invoke(cache, new Object[]{conf.getAttributeAsXml(propName)});
+ method.invoke(conf, new Object[]{attribs.get(propName)});
}
else
{
method = objectClass.getMethod(setter, new Class[]{String.class});
- method.invoke(cache, new Object[]{conf.getAttribute(propName)});
+ method.invoke(conf, new Object[]{attribs.get(propName)});
}
continue;
@@ -121,14 +183,14 @@
PropertyEditor editor= PropertyEditorManager.findEditor(parameterType);
if(editor == null) throw new ConfigurationException("Couldn't find a property editor for parameter type " + parameterType);
- editor.setAsText(conf.getAttribute(propName));
+ editor.setAsText((String) attribs.get(propName));
Object parameter = editor.getValue();
if (log.isDebugEnabled()) log.debug("Invoking setter method: " + setter + " with parameter \"" + parameter + "\" of type " + parameter.getClass());
try
{
- m.invoke(cache, new Object[]{parameter});
+ m.invoke(conf, new Object[]{parameter});
}
catch (Exception e)
{
1.20 +16 -16 JBossCache/src/org/jboss/cache/factories/InterceptorChainFactory.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: InterceptorChainFactory.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/factories/InterceptorChainFactory.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -b -r1.19 -r1.20
--- InterceptorChainFactory.java 22 Jun 2006 10:43:54 -0000 1.19
+++ InterceptorChainFactory.java 19 Jul 2006 21:34:43 -0000 1.20
@@ -26,7 +26,7 @@
public Interceptor buildInterceptorChain(TreeCache cache) throws IllegalAccessException, ClassNotFoundException, InstantiationException
{
- if (cache.isNodeLockingOptimistic())
+ if (cache.getConfiguration().isNodeLockingOptimistic())
{
return createOptimisticInterceptorChain(cache);
}
@@ -41,7 +41,7 @@
{
Class clazz = loadClass(classname);
Interceptor i = (Interceptor) clazz.newInstance();
- i.setCache(cache);
+ i.setCache(cache.getSPI());
return i;
}
@@ -155,18 +155,18 @@
txInterceptor = createInterceptor("org.jboss.cache.interceptors.TxInterceptor", cache);
- switch (cache.getCacheModeInternal())
+ switch (cache.getConfiguration().getCacheModeInt())
{
- case TreeCache.REPL_SYNC:
- case TreeCache.REPL_ASYNC:
+ case REPL_SYNC:
+ case REPL_ASYNC:
repl_interceptor = createInterceptor("org.jboss.cache.interceptors.ReplicationInterceptor", cache);
// if (!cache.isNodeLockingOptimistic()) cache.setReplicationHandler((Replicatable) repl_interceptor);
break;
- case TreeCache.INVALIDATION_SYNC:
- case TreeCache.INVALIDATION_ASYNC:
+ case INVALIDATION_SYNC:
+ case INVALIDATION_ASYNC:
repl_interceptor = createInterceptor("org.jboss.cache.interceptors.InvalidationInterceptor", cache);
break;
- case TreeCache.LOCAL:
+ case LOCAL:
//Nothing...
}
@@ -187,7 +187,7 @@
}
// load the cache management interceptor first
- if (cache.getUseInterceptorMbeans())
+ if (cache.getConfiguration().isUseInterceptorMbeans())
{
if (first == null)
first = cacheMgmtInterceptor;
@@ -348,18 +348,18 @@
// if (cache.getBuddyManager() != null && cache.getBuddyManager().isEnableDataGravitation()) dataGravitatorInterceptor = createInterceptor("org.jboss.cache.interceptors.DataGravitatorInterceptor", cache);
if (cache.getBuddyManager() != null) dataGravitatorInterceptor = createInterceptor("org.jboss.cache.interceptors.DataGravitatorInterceptor", cache);
- switch (cache.getCacheModeInternal())
+ switch (cache.getConfiguration().getCacheModeInt())
{
- case TreeCache.REPL_SYNC:
- case TreeCache.REPL_ASYNC:
+ case REPL_SYNC:
+ case REPL_ASYNC:
replicationInterceptor = createInterceptor("org.jboss.cache.interceptors.OptimisticReplicationInterceptor", cache);
// if (!cache.isNodeLockingOptimistic()) cache.setReplicationHandler((Replicatable) replicationInterceptor);
break;
- case TreeCache.INVALIDATION_SYNC:
- case TreeCache.INVALIDATION_ASYNC:
+ case INVALIDATION_SYNC:
+ case INVALIDATION_ASYNC:
replicationInterceptor = createInterceptor("org.jboss.cache.interceptors.InvalidationInterceptor", cache);
break;
- case TreeCache.LOCAL:
+ case LOCAL:
//Nothing...
}
@@ -378,7 +378,7 @@
evictionInterceptor = createInterceptor(cache.getEvictionInterceptorClass(), cache);
}
- if (cache.getUseInterceptorMbeans())
+ if (cache.getConfiguration().isUseInterceptorMbeans())
{
cacheMgmtInterceptor = createInterceptor("org.jboss.cache.interceptors.CacheMgmtInterceptor", cache);
if (first == null)
1.1 date: 2006/07/19 21:34:43; author: msurtani; state: Exp;JBossCache/src/org/jboss/cache/factories/CacheFactoryTest.java
Index: CacheFactoryTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.factories;
import junit.framework.TestCase;
import org.jboss.cache.TreeCacheProxyImpl;
import org.jboss.cache.config.Configuration;
/**
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
*/
public class CacheFactoryTest extends TestCase
{
DefaultCacheFactory factory;
Configuration expected;
String configFile = "META-INF/replSync-service.xml";
private TreeCacheProxyImpl cache;
protected void setUp()
{
factory = new DefaultCacheFactory();
expected = factory.readConfigurationStream( factory.getAsInputStreamFromClassLoader( configFile ) );
}
protected void tearDown()
{
if (cache != null)
{
cache.stop();
}
}
public void testFromConfigFileStarted()
{
cache = (TreeCacheProxyImpl) factory.createCache(configFile);
assertEquals(expected, cache.getConfiguration());
assertTrue("Should have started", cache.isStarted());
doSimpleConfTests(cache.getConfiguration());
}
public void testFromConfigFileUnstarted()
{
cache = (TreeCacheProxyImpl) factory.createCache(configFile, false);
assertEquals(expected, cache.getConfiguration());
assertFalse("Should not have started", cache.isStarted());
doSimpleConfTests(cache.getConfiguration());
}
public void testFromConfigObjStarted()
{
cache = (TreeCacheProxyImpl) factory.createCache(expected);
assertTrue("Should have started", cache.isStarted());
doSimpleConfTests(cache.getConfiguration());
}
public void testFromConfigObjUnstarted()
{
cache = (TreeCacheProxyImpl) factory.createCache(expected, false);
assertFalse("Should not have started", cache.isStarted());
doSimpleConfTests(cache.getConfiguration());
}
private void doSimpleConfTests(Configuration tc)
{
assertEquals("REPL_SYNC", tc.getCacheMode());
assertEquals(10000, tc.getLockAcquisitionTimeout());
assertEquals("REPEATABLE_READ", tc.getIsolationLevel());
assertEquals(true, tc.isUseRegionBasedMarshalling());
// test some of the XML content.
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());
}
public void testLifecycle() throws Exception
{
cache = (TreeCacheProxyImpl) factory.createCache(expected, false);
assertFalse(cache.isStarted());
cache.start();
assertTrue(cache.isStarted());
cache.stop();
assertFalse(cache.isStarted());
}
}
More information about the jboss-cvs-commits
mailing list