[jboss-cvs] JBossCache/src/org/jboss/cache/factories ...
Brian Stansberry
brian.stansberry at jboss.com
Fri Oct 27 15:26:07 EDT 2006
User: bstansberry
Date: 06/10/27 15:26:07
Modified: src/org/jboss/cache/factories XmlConfigurationParser.java
Log:
[JBCACHE-823] Refactor eviction configuration away from EvictionConfiguration interface
Revision Changes Path
1.4 +173 -87 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.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- XmlConfigurationParser.java 25 Oct 2006 04:50:19 -0000 1.3
+++ XmlConfigurationParser.java 27 Oct 2006 19:26:07 -0000 1.4
@@ -6,6 +6,21 @@
*/
package org.jboss.cache.factories;
+import java.beans.PropertyEditor;
+import java.beans.PropertyEditorManager;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Map.Entry;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.jboss.cache.buddyreplication.NextMemberBuddyLocator;
import org.jboss.cache.config.BuddyReplicationConfig;
import org.jboss.cache.config.CacheLoaderConfig;
@@ -13,29 +28,16 @@
import org.jboss.cache.config.ConfigurationException;
import org.jboss.cache.config.EvictionConfig;
import org.jboss.cache.config.EvictionRegionConfig;
-import org.jboss.cache.config.BuddyReplicationConfig.BuddyLocatorConfig;
import org.jboss.cache.config.MissingPolicyException;
+import org.jboss.cache.config.BuddyReplicationConfig.BuddyLocatorConfig;
import org.jboss.cache.eviction.EvictionConfiguration;
import org.jboss.cache.eviction.EvictionPolicy;
+import org.jboss.cache.eviction.EvictionPolicyConfig;
import org.jboss.cache.util.Util;
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.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.Properties;
-import java.util.Set;
-import java.lang.reflect.Method;
-import java.beans.PropertyEditor;
-import java.beans.PropertyEditorManager;
+import org.w3c.dom.NodeList;
/**
* Reads in XMLconfiguration files and spits out a {@link org.jboss.cache.config.Configuration} object. When deployed as a
@@ -47,6 +49,10 @@
public class XmlConfigurationParser
{
private static Log log = LogFactory.getLog(XmlConfigurationParser.class);
+
+ public static final String ATTR = "attribute";
+ public static final String NAME = "name";
+
public Configuration parseFile(String filename)
{
return parseStream(getAsInputStreamFromClassLoader(filename));
@@ -57,51 +63,20 @@
// loop through all elements in XML.
if (stream == null) throw new ConfigurationException("Input stream for configuration xml is null!");
- String defaultEvictionClass = 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);
- if (log.isDebugEnabled()) log.debug("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);
+ ParsedAttributes attributes = extractAttributes(mbeanElement);
// Special handling for the old separate property for
// eviction policy -- just cache it and use with the eviction XML
- if ("EvictionPolicyClass".equals(name))
- {
- defaultEvictionClass = valueStr;
- continue;
- }
-
- 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);
- }
+ String defaultEvictionClass = (String) attributes.stringAttribs.remove("EvictionPolicyClass");
Configuration c = new Configuration();
- setValues(c, stringAttribs.keySet(), stringAttribs);
- setXmlValues(c, xmlAttribs.keySet(), xmlAttribs, defaultEvictionClass);
+ setValues(c, attributes.stringAttribs, false);
+ // Special handling for XML elements -- we hard code the parsing
+ setXmlValues(c, attributes.xmlAttribs, defaultEvictionClass);
+
return c;
}
@@ -132,20 +107,30 @@
}
- private void setValues(Configuration conf, Set<String> keys, Map attribs)
+ private static void setValues(Object target, Map attribs, boolean isXmlAttribs)
{
- Class objectClass = conf.getClass();
+ Class objectClass = target.getClass();
// go thru simple string setters first.
- for (String propName : keys)
+ for (Iterator iter = attribs.entrySet().iterator(); iter.hasNext();)
{
- String setter = "set" + propName;
+ Map.Entry entry = (Map.Entry) iter.next();
+ String propName = (String) entry.getKey();
+ String setter = getSetterName(propName);
Method method = null;
try
{
+ if (isXmlAttribs)
+ {
+ method = objectClass.getMethod(setter, new Class[]{Element.class});
+ method.invoke(target, new Object[]{ entry.getValue() });
+ }
+ else
+ {
method = objectClass.getMethod(setter, new Class[]{String.class});
- method.invoke(conf, new Object[]{attribs.get(propName)});
+ method.invoke(target, new Object[]{ entry.getValue() });
+ }
continue;
}
@@ -159,7 +144,7 @@
throw new ConfigurationException("Unable to invoke setter " + setter + " on " + objectClass, e);
}
- // if we get here, we could not find a String setter.
+ // if we get here, we could not find a String or Element setter.
for (Method m : objectClass.getMethods())
{
if (setter.equals(m.getName()))
@@ -178,7 +163,7 @@
try
{
- m.invoke(conf, new Object[]{parameter});
+ m.invoke(target, new Object[]{parameter});
}
catch (Exception e)
{
@@ -189,32 +174,34 @@
}
}
- private void setXmlValues(Configuration conf, Set<String> keys, Map<String, Element> attribs, String defaultEvictionClass)
+ private void setXmlValues(Configuration conf, Map<String, Element> attribs, String defaultEvictionClass)
{
- for (String propname : keys)
+ for (Iterator it = attribs.entrySet().iterator(); it.hasNext(); )
{
+ Map.Entry entry = (Entry) it.next();
+ String propname = (String) entry.getKey();
if ("BuddyReplicationConfiguration".equals(propname)
|| "BuddyReplicationConfig".equals(propname))
{
- BuddyReplicationConfig brc = parseBuddyReplicationConfig(attribs.get(propname));
+ BuddyReplicationConfig brc = parseBuddyReplicationConfig((Element) entry.getValue());
conf.setBuddyReplicationConfig(brc);
}
else if ("CacheLoaderConfiguration".equals(propname)
|| "CacheLoaderConfig".equals(propname))
{
- CacheLoaderConfig clc = parseCacheLoaderConfig(attribs.get(propname));
+ CacheLoaderConfig clc = parseCacheLoaderConfig((Element) entry.getValue());
conf.setCacheLoaderConfig(clc);
}
else if ("EvictionPolicyConfiguration".equals(propname)
|| "EvictionPolicyConfig".equals(propname))
{
- EvictionConfig ec = parseEvictionConfig(attribs.get(propname),
+ EvictionConfig ec = parseEvictionConfig((Element) entry.getValue(),
defaultEvictionClass);
conf.setEvictionConfig(ec);
}
else if ("ClusterConfig".equals(propname))
{
- conf.setClusterConfig(attribs.get(propname));
+ conf.setClusterConfig((Element) entry.getValue());
}
else
{
@@ -318,8 +305,18 @@
if (element != null)
{
+ // If they set the default eviction policy in the element, use that
+ // in preference to the external attribute
String temp = XmlHelper.getTagContents(element,
- EvictionConfiguration.WAKEUP_INTERVAL_SECONDS, EvictionConfiguration.ATTR, EvictionConfiguration.NAME);
+ EvictionConfig.EVICTION_POLICY_CLASS, ATTR, NAME);
+ if (temp != null && temp.length() > 0)
+ {
+ defaultEvictionClass = temp;
+ ec.setDefaultEvictionPolicyClass(defaultEvictionClass);
+ }
+
+ temp = XmlHelper.getTagContents(element,
+ EvictionConfig.WAKEUP_INTERVAL_SECONDS, ATTR, NAME);
int wakeupIntervalSeconds = 0;
if (temp != null)
@@ -328,24 +325,27 @@
}
if (wakeupIntervalSeconds <= 0)
- wakeupIntervalSeconds = EvictionConfiguration.WAKEUP_DEFAULT;
+ wakeupIntervalSeconds = EvictionConfig.WAKEUP_DEFAULT;
ec.setWakeupIntervalSeconds(wakeupIntervalSeconds);
int eventQueueSize = 0;
temp = XmlHelper.getTagContents(element,
- EvictionConfiguration.EVENT_QUEUE_SIZE, EvictionConfiguration.ATTR, EvictionConfiguration.NAME);
+ EvictionConfig.EVENT_QUEUE_SIZE, ATTR, NAME);
if (temp != null)
{
eventQueueSize = Integer.parseInt(temp);
}
- if (eventQueueSize <= 0) eventQueueSize = EvictionConfiguration.EVENT_QUEUE_SIZE_DEFAULT;
+ if (eventQueueSize <= 0)
+ {
+ eventQueueSize = EvictionConfig.EVENT_QUEUE_SIZE_DEFAULT;
+ }
- ec.setEventQueueSize(eventQueueSize);
+ ec.setDefaultEventQueueSize(eventQueueSize);
- NodeList list = element.getElementsByTagName(EvictionConfiguration.REGION);
+ NodeList list = element.getElementsByTagName(EvictionRegionConfig.REGION);
if (list != null && list.getLength() > 0)
{
List regionConfigs = new ArrayList(list.getLength());
@@ -356,7 +356,7 @@
continue;
try
{
- regionConfigs.add(parseEvictionRegionConfig((Element) node, defaultEvictionClass));
+ regionConfigs.add(parseEvictionRegionConfig((Element) node, defaultEvictionClass, eventQueueSize));
}
catch (MissingPolicyException ignored)
{
@@ -375,13 +375,25 @@
}
- public static EvictionRegionConfig parseEvictionRegionConfig(Element element, String defaultEvictionClass)
+ public static EvictionRegionConfig parseEvictionRegionConfig(Element element,
+ String defaultEvictionClass,
+ int defaultQueueCapacity)
{
EvictionRegionConfig erc = new EvictionRegionConfig();
- erc.setRegionName(element.getAttribute(EvictionConfiguration.NAME));
+ erc.setRegionName(element.getAttribute(EvictionRegionConfig.NAME));
- String evictionClass = element.getAttribute(EvictionConfiguration.REGION_POLICY_CLASS);
+ String temp = element.getAttribute(EvictionRegionConfig.EVENT_QUEUE_SIZE);
+ if (temp != null && temp.length() > 0)
+ {
+ erc.setEventQueueSize(Integer.parseInt(temp));
+ }
+ else
+ {
+ erc.setEventQueueSize(defaultQueueCapacity);
+ }
+
+ String evictionClass = element.getAttribute(EvictionRegionConfig.REGION_POLICY_CLASS);
if (evictionClass == null || evictionClass.length() == 0)
{
evictionClass = defaultEvictionClass;
@@ -407,12 +419,10 @@
throw new RuntimeException("Eviction class is not properly loaded in classloader", e);
}
- erc.setEvictionPolicyClass(evictionClass);
-
- EvictionConfiguration evc = null;
+ EvictionPolicyConfig epc = null;
try
{
- evc = (EvictionConfiguration) policy.getEvictionConfigurationClass().newInstance();
+ epc = (EvictionPolicyConfig) policy.getEvictionConfigurationClass().newInstance();
}
catch (RuntimeException e)
{
@@ -424,9 +434,85 @@
policy.getEvictionConfigurationClass(), e);
}
- evc.parseXMLConfig(element);
- erc.setEvictionConfiguration(evc);
+ if (epc instanceof EvictionConfiguration)
+ {
+ // Configure the old fashioned way
+ ((EvictionConfiguration)epc).parseXMLConfig(element);
+ }
+ else
+ {
+ parseEvictionPolicyConfig(element, epc);
+ }
+
+ erc.setEvictionPolicyConfig(epc);
return erc;
}
+
+ public static void parseEvictionPolicyConfig(Element element, EvictionPolicyConfig target)
+ {
+ ParsedAttributes attributes = extractAttributes(element);
+ setValues(target, attributes.stringAttribs, false);
+ setValues(target, attributes.xmlAttribs, true);
+ target.validate();
+ }
+
+ private static ParsedAttributes extractAttributes(Element source)
+ {
+ Map<String, String> stringAttribs = new HashMap<String, String>();
+ Map<String, Element> xmlAttribs = new HashMap<String, Element>();
+ NodeList list=source.getElementsByTagName(XmlHelper.ATTR);
+ if (log.isDebugEnabled()) log.debug("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);
+ }
+
+ return new ParsedAttributes(stringAttribs, xmlAttribs);
+ }
+
+ private static String getSetterName(String propName)
+ {
+ StringBuffer sb = new StringBuffer("set");
+ if (propName != null && propName.length() > 0)
+ {
+ sb.append(propName.substring(0, 1).toUpperCase());
+ if (propName.length() > 1)
+ sb.append(propName.substring(1));
+ }
+ return sb.toString();
+ }
+
+ static class ParsedAttributes
+ {
+ Map<String, String> stringAttribs;
+ Map<String, Element> xmlAttribs;
+
+ ParsedAttributes(Map strings, Map elements)
+ {
+ this.stringAttribs = strings;
+ this.xmlAttribs = elements;
+ }
+ }
}
More information about the jboss-cvs-commits
mailing list