[jboss-cvs] JBossCache/src/org/jboss/cache/factories ...
Vladmir Blagojevic
vladimir.blagojevic at jboss.com
Wed Jan 3 15:41:36 EST 2007
User: vblagojevic
Date: 07/01/03 15:41:36
Modified: src/org/jboss/cache/factories XmlConfigurationParser.java
Added: src/org/jboss/cache/factories UnitTestCacheFactory.java
Log:
created UnitTestCacheFactory
Revision Changes Path
1.9 +5 -5 JBossCache/src/org/jboss/cache/factories/XmlConfigurationParser.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: XmlConfigurationParser.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/factories/XmlConfigurationParser.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- XmlConfigurationParser.java 15 Nov 2006 15:50:59 -0000 1.8
+++ XmlConfigurationParser.java 3 Jan 2007 20:41:36 -0000 1.9
@@ -88,12 +88,12 @@
return c;
}
- private InputStream getAsInputStreamFromClassLoader(String filename)
+ protected InputStream getAsInputStreamFromClassLoader(String filename)
{
return Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
}
- private Element getMBeanElement(Element root)
+ protected Element getMBeanElement(Element root)
{
// This is following JBoss convention.
NodeList list=root.getElementsByTagName(XmlHelper.ROOT);
@@ -115,7 +115,7 @@
}
- private static void setValues(Object target, Map attribs, boolean isXmlAttribs)
+ protected static void setValues(Object target, Map attribs, boolean isXmlAttribs)
{
Class objectClass = target.getClass();
@@ -182,7 +182,7 @@
}
}
- private void setXmlValues(Configuration conf, Map<String, Element> attribs, String defaultEvictionClass)
+ protected void setXmlValues(Configuration conf, Map<String, Element> attribs, String defaultEvictionClass)
{
for (Iterator it = attribs.entrySet().iterator(); it.hasNext(); )
{
@@ -511,7 +511,7 @@
return buffer.toString();
}
- private static ParsedAttributes extractAttributes(Element source)
+ protected static ParsedAttributes extractAttributes(Element source)
{
Map<String, String> stringAttribs = new HashMap<String, String>();
Map<String, Element> xmlAttribs = new HashMap<String, Element>();
1.1 date: 2007/01/03 20:41:36; author: vblagojevic; state: Exp;JBossCache/src/org/jboss/cache/factories/UnitTestCacheFactory.java
Index: UnitTestCacheFactory.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.factories;
import java.io.InputStream;
import org.jboss.cache.Cache;
import org.jboss.cache.CacheImpl;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.ConfigurationException;
import org.jboss.cache.config.Configuration.CacheMode;
import org.jboss.cache.xml.XmlHelper;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* Cache factory used by unit tests.
*
*
*/
public class UnitTestCacheFactory
{
public static String JGROUPS_CHANNEL = "udp"; //use udp by default
public static String JGROUPS_STACK_TYPE = "jgroups.stack";
public static String DEFAULT_CONFIGURATION_FILE = "META-INF/unit-test-cache-service.xml";
static
{
JGROUPS_CHANNEL = System.getProperty(JGROUPS_STACK_TYPE, JGROUPS_CHANNEL);
}
public static Cache createCache() throws ConfigurationException
{
return createCache(new Configuration());
}
public static Cache createCache(CacheMode mode, boolean start) throws ConfigurationException
{
return createCache(createConfiguration(mode), start);
}
public static Configuration createConfiguration(CacheMode mode) throws ConfigurationException
{
UnitTestXmlConfigurationParser parser = new UnitTestXmlConfigurationParser();
Configuration c = parser.parseFile(DEFAULT_CONFIGURATION_FILE,mode);
return c;
}
public static Cache createCache(Configuration configuration) throws ConfigurationException
{
return createCache(configuration, true);
}
public static Cache createCache(Configuration configuration, boolean start) throws ConfigurationException
{
try
{
CacheImpl cache = new CacheImpl();
cache.setConfiguration(configuration);
if (start) cache.start();
return cache;
}
catch (Exception e)
{
if (e instanceof ConfigurationException) throw (ConfigurationException) e;
throw new RuntimeException(e);
}
}
private static class UnitTestXmlConfigurationParser extends XmlConfigurationParser
{
public Configuration parseFile(String filename, CacheMode mode)
{
return parseStream(getAsInputStreamFromClassLoader(DEFAULT_CONFIGURATION_FILE),mode);
}
public Configuration parseStream(InputStream stream,CacheMode mode)
{
// loop through all elements in XML.
if (stream == null) throw new ConfigurationException("Input stream for configuration xml is null!");
Element root= XmlHelper.getDocumentRoot(stream);
Element mbeanElement= getMBeanElement(root);
ParsedAttributes attributes = extractAttributes(mbeanElement);
// Special handling for the old separate property for
// eviction policy -- just cache it and use with the eviction XML
String defaultEvictionClass = (String) attributes.stringAttribs.remove("EvictionPolicyClass");
// Deal with rename of the old property that controlled MBean registration
String keepStats = (String) attributes.stringAttribs.remove("UseMbean");
if (keepStats != null && attributes.stringAttribs.get("ExposeManagementStatistics") == null)
{
attributes.stringAttribs.put("ExposeManagementStatistics", keepStats);
}
Configuration c = new Configuration();
setValues(c, attributes.stringAttribs, false);
// Special handling for XML elements -- we hard code the parsing
setXmlValues(c, attributes.xmlAttribs, defaultEvictionClass);
Element list = (Element) root.getElementsByTagName("protocol_stacks").item(0);
NodeList stacks = list.getElementsByTagName("stack");
for (int i = 0; i < stacks.getLength(); i++)
{
Element stack = (Element) stacks.item(i);
String stackName = stack.getAttribute("name");
if (stackName.startsWith(JGROUPS_CHANNEL))
{
Element jgroupsStack = (Element) stack.getElementsByTagName("config").item(0);
if(mode == CacheMode.REPL_ASYNC && !stackName.contains("-"))
{
c.setClusterConfig(jgroupsStack);
c.setCacheMode(CacheMode.REPL_ASYNC);
break;
}
else if (mode == CacheMode.REPL_SYNC && stackName.contains("-"))
{
c.setClusterConfig(jgroupsStack);
c.setCacheMode(CacheMode.REPL_SYNC);
break;
}
}
}
return c;
}
}
}
More information about the jboss-cvs-commits
mailing list