[jboss-cvs] JBossCache/src/org/jboss/cache/factories ...
Manik Surtani
msurtani at jboss.com
Thu Jul 20 00:49:52 EDT 2006
User: msurtani
Date: 06/07/20 00:49:52
Modified: src/org/jboss/cache/factories CacheFactoryTest.java
DefaultCacheFactory.java
Added: src/org/jboss/cache/factories XmlConfigurationParser.java
Log:
Fixed some broken UTs to use the new xml parsers, cache factories, etc.
Revision Changes Path
1.2 +5 -3 JBossCache/src/org/jboss/cache/factories/CacheFactoryTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CacheFactoryTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/factories/CacheFactoryTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- CacheFactoryTest.java 19 Jul 2006 21:34:43 -0000 1.1
+++ CacheFactoryTest.java 20 Jul 2006 04:49:52 -0000 1.2
@@ -8,6 +8,7 @@
import junit.framework.TestCase;
import org.jboss.cache.TreeCacheProxyImpl;
+import org.jboss.cache.lock.IsolationLevel;
import org.jboss.cache.config.Configuration;
/**
@@ -24,7 +25,8 @@
protected void setUp()
{
factory = new DefaultCacheFactory();
- expected = factory.readConfigurationStream( factory.getAsInputStreamFromClassLoader( configFile ) );
+ XmlConfigurationParser parser = new XmlConfigurationParser();
+ expected = parser.parseFile( configFile );
}
protected void tearDown()
@@ -74,9 +76,9 @@
private void doSimpleConfTests(Configuration tc)
{
- assertEquals("REPL_SYNC", tc.getCacheMode());
+ assertEquals(Configuration.CacheMode.REPL_SYNC, tc.getCacheModeInt());
assertEquals(10000, tc.getLockAcquisitionTimeout());
- assertEquals("REPEATABLE_READ", tc.getIsolationLevel());
+ assertEquals(IsolationLevel.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());
1.6 +2 -152 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.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- DefaultCacheFactory.java 19 Jul 2006 21:34:43 -0000 1.5
+++ DefaultCacheFactory.java 20 Jul 2006 04:49:52 -0000 1.6
@@ -6,24 +6,10 @@
*/
package org.jboss.cache.factories;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Cache;
import org.jboss.cache.TreeCache;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.ConfigurationException;
-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;
/**
* Default implementation of the Cache Factory
@@ -32,8 +18,6 @@
*/
public class DefaultCacheFactory implements CacheFactory
{
- private static Log log = LogFactory.getLog(DefaultCacheFactory.class);
-
public Cache createCache(String configFileName) throws ConfigurationException
{
return createCache(configFileName, true);
@@ -41,7 +25,8 @@
public Cache createCache(String configFileName, boolean start) throws ConfigurationException
{
- Configuration c = readConfigurationStream(getAsInputStreamFromClassLoader(configFileName));
+ XmlConfigurationParser parser = new XmlConfigurationParser();
+ Configuration c = parser.parseFile(configFileName);
return createCache(c, start);
}
@@ -65,139 +50,4 @@
throw new ConfigurationException(e.getMessage());
}
}
-
- InputStream getAsInputStreamFromClassLoader(String filename)
- {
- return Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
- }
-
- Configuration readConfigurationStream(InputStream stream)
- {
- // 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;
- }
-
-
- private void setValues(Configuration conf, Set<String> keys, Map attribs, boolean isXmlAttribs)
- {
- Class objectClass = conf.getClass();
-
- // go thru simple string setters first.
- for (String propName : keys)
- {
- String setter = "set" + propName;
- Method method = null;
-
- try
- {
- if (isXmlAttribs)
- {
- method = objectClass.getMethod(setter, new Class[]{Element.class});
- method.invoke(conf, new Object[]{attribs.get(propName)});
- }
- else
- {
- method = objectClass.getMethod(setter, new Class[]{String.class});
- method.invoke(conf, new Object[]{attribs.get(propName)});
- }
-
- continue;
- }
- catch (NoSuchMethodException me)
- {
- log.debug("Unable to find String setter for " + setter);
- }
- catch (Exception e)
- {
- throw new ConfigurationException("Unable to invoke setter " + setter + " on " + objectClass, e);
- }
-
- // if we get here, we could not find a String or XML setter.
- for (Method m : objectClass.getMethods())
- {
- if (setter.equals(m.getName()))
- {
- Class paramTypes[] = m.getParameterTypes();
- if (paramTypes.length != 1) throw new ConfigurationException("Setter " + setter + " does not contain the expected number of params. Has " + paramTypes.length + " instead of just 1.");
-
- Class parameterType = paramTypes[0];
- PropertyEditor editor= PropertyEditorManager.findEditor(parameterType);
- if(editor == null) throw new ConfigurationException("Couldn't find a property editor for parameter type " + parameterType);
-
- 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(conf, new Object[]{parameter});
- }
- catch (Exception e)
- {
- throw new ConfigurationException("Unable to invoke setter " + setter + " on " + objectClass, e);
- }
- }
- }
- }
- }
}
1.1 date: 2006/07/20 04:49:52; author: msurtani; state: Exp;JBossCache/src/org/jboss/cache/factories/XmlConfigurationParser.java
Index: XmlConfigurationParser.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.factories;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.ConfigurationException;
import org.jboss.cache.xml.XmlHelper;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.InputStream;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.lang.reflect.Method;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
/**
* Reads in XMLconfiguration files and spits out a {@link org.jboss.cache.config.Configuration} object. When deployed as a
* JBoss MBean, this role is performed by the JBoss Microcontainer. This class is only used internally in unit tests
* or within {@link CacheFactory} implementations for standalone JBoss Cache usage.
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
*/
public class XmlConfigurationParser
{
private static Log log = LogFactory.getLog(XmlConfigurationParser.class);
public Configuration parseFile(String filename)
{
return parseStream(getAsInputStreamFromClassLoader(filename));
}
public Configuration parseStream(InputStream stream)
{
// 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 InputStream getAsInputStreamFromClassLoader(String filename)
{
return Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
}
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;
}
private void setValues(Configuration conf, Set<String> keys, Map attribs, boolean isXmlAttribs)
{
Class objectClass = conf.getClass();
// go thru simple string setters first.
for (String propName : keys)
{
String setter = "set" + propName;
Method method = null;
try
{
if (isXmlAttribs)
{
method = objectClass.getMethod(setter, new Class[]{Element.class});
method.invoke(conf, new Object[]{attribs.get(propName)});
}
else
{
method = objectClass.getMethod(setter, new Class[]{String.class});
method.invoke(conf, new Object[]{attribs.get(propName)});
}
continue;
}
catch (NoSuchMethodException me)
{
log.debug("Unable to find String setter for " + setter);
}
catch (Exception e)
{
throw new ConfigurationException("Unable to invoke setter " + setter + " on " + objectClass, e);
}
// if we get here, we could not find a String or XML setter.
for (Method m : objectClass.getMethods())
{
if (setter.equals(m.getName()))
{
Class paramTypes[] = m.getParameterTypes();
if (paramTypes.length != 1) throw new ConfigurationException("Setter " + setter + " does not contain the expected number of params. Has " + paramTypes.length + " instead of just 1.");
Class parameterType = paramTypes[0];
PropertyEditor editor= PropertyEditorManager.findEditor(parameterType);
if(editor == null) throw new ConfigurationException("Couldn't find a property editor for parameter type " + parameterType);
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(conf, new Object[]{parameter});
}
catch (Exception e)
{
throw new ConfigurationException("Unable to invoke setter " + setter + " on " + objectClass, e);
}
}
}
}
}
}
More information about the jboss-cvs-commits
mailing list