[jboss-cvs] JBossCache/src/org/jboss/cache/factories ...

Brian Stansberry brian.stansberry at jboss.com
Wed Oct 25 00:50:19 EDT 2006


  User: bstansberry
  Date: 06/10/25 00:50:19

  Modified:    src/org/jboss/cache/factories  XmlConfigurationParser.java
  Log:
  Handle parsing of config XML in XmlConfigurationParser 
  
  Revision  Changes    Path
  1.3       +270 -14   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.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- XmlConfigurationParser.java	21 Aug 2006 13:13:16 -0000	1.2
  +++ XmlConfigurationParser.java	25 Oct 2006 04:50:19 -0000	1.3
  @@ -6,8 +6,18 @@
    */
   package org.jboss.cache.factories;
   
  +import org.jboss.cache.buddyreplication.NextMemberBuddyLocator;
  +import org.jboss.cache.config.BuddyReplicationConfig;
  +import org.jboss.cache.config.CacheLoaderConfig;
   import org.jboss.cache.config.Configuration;
   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.eviction.EvictionConfiguration;
  +import org.jboss.cache.eviction.EvictionPolicy;
  +import org.jboss.cache.util.Util;
   import org.jboss.cache.xml.XmlHelper;
   import org.w3c.dom.Element;
   import org.w3c.dom.NodeList;
  @@ -15,9 +25,13 @@
   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;
  @@ -43,6 +57,7 @@
           // 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>();
   
  @@ -63,6 +78,14 @@
              String name = element.getAttribute(XmlHelper.NAME);
              String valueStr = XmlHelper.getElementContent(element, true);
   
  +           // 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)
              {
  @@ -77,8 +100,8 @@
           }
   
           Configuration c = new Configuration();
  -        setValues(c, stringAttribs.keySet(), stringAttribs, false);
  -        setValues(c, xmlAttribs.keySet(), xmlAttribs, true);
  +        setValues(c, stringAttribs.keySet(), stringAttribs);
  +        setXmlValues(c, xmlAttribs.keySet(), xmlAttribs, defaultEvictionClass);
           return c;
       }
   
  @@ -109,7 +132,7 @@
      }
   
   
  -    private void setValues(Configuration conf, Set<String> keys, Map attribs, boolean isXmlAttribs)
  +    private void setValues(Configuration conf, Set<String> keys, Map attribs)
       {
           Class objectClass = conf.getClass();
   
  @@ -121,16 +144,8 @@
   
               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;
               }
  @@ -144,7 +159,7 @@
                   throw new ConfigurationException("Unable to invoke setter " + setter + " on " + objectClass, e);
               }
   
  -            // if we get here, we could not find a String or XML setter.
  +            // if we get here, we could not find a String setter.
               for (Method m : objectClass.getMethods())
               {
                   if (setter.equals(m.getName()))
  @@ -173,4 +188,245 @@
               }
           }
       }
  +    
  +    private void setXmlValues(Configuration conf, Set<String> keys, Map<String, Element> attribs, String defaultEvictionClass)
  +    {
  +       for (String propname : keys)
  +       {
  +          if ("BuddyReplicationConfiguration".equals(propname)
  +                || "BuddyReplicationConfig".equals(propname))
  +          {
  +             BuddyReplicationConfig brc = parseBuddyReplicationConfig(attribs.get(propname));
  +             conf.setBuddyReplicationConfig(brc);
  +          }
  +          else if ("CacheLoaderConfiguration".equals(propname)
  +                   || "CacheLoaderConfig".equals(propname))
  +          {
  +             CacheLoaderConfig clc = parseCacheLoaderConfig(attribs.get(propname));
  +             conf.setCacheLoaderConfig(clc);
  +          }
  +          else if ("EvictionPolicyConfiguration".equals(propname)
  +                || "EvictionPolicyConfig".equals(propname))
  +          {
  +             EvictionConfig ec = parseEvictionConfig(attribs.get(propname), 
  +                                                     defaultEvictionClass);
  +             conf.setEvictionConfig(ec);
  +          }
  +          else if ("ClusterConfig".equals(propname))
  +          {
  +             conf.setClusterConfig(attribs.get(propname));
  +          }
  +          else
  +          {
  +             throw new ConfigurationException("Unknown configuration element " + propname);
  +          }
  +       }
  +    }
  +    
  +    public static BuddyReplicationConfig parseBuddyReplicationConfig(Element element)
  +    {
  +       BuddyReplicationConfig brc = new BuddyReplicationConfig();
  +       brc.setEnabled(XmlHelper.readBooleanContents(element, "buddyReplicationEnabled"));
  +       brc.setDataGravitationRemoveOnFind(XmlHelper.readBooleanContents(element, "dataGravitationRemoveOnFind", true));
  +       brc.setDataGravitationSearchBackupTrees(XmlHelper.readBooleanContents(element, "dataGravitationSearchBackupTrees", true));
  +       brc.setAutoDataGravitation(brc.isEnabled() && XmlHelper.readBooleanContents(element, "autoDataGravitation", false));
  +
  +       String strBuddyCommunicationTimeout = XmlHelper.readStringContents(element, "buddyCommunicationTimeout");
  +       try
  +       {
  +          brc.setBuddyCommunicationTimeout(Integer.parseInt(strBuddyCommunicationTimeout));
  +       }
  +       catch (Exception e)
  +       {
  +       }
  +       finally
  +       {
  +          if (log.isDebugEnabled())
  +             log.debug("Using buddy communication timeout of " + brc.getBuddyCommunicationTimeout() + " millis");
  +       }
  +       String buddyPoolName = XmlHelper.readStringContents(element, "buddyPoolName");
  +       if ("".equals(buddyPoolName))
  +          buddyPoolName = null;
  +
  +       brc.setBuddyPoolName(buddyPoolName);
  +       
  +       // now read the buddy locator details
  +
  +       String buddyLocatorClass = XmlHelper.readStringContents(element, "buddyLocatorClass");
  +       if (buddyLocatorClass == null || buddyLocatorClass.length() == 0)
  +          buddyLocatorClass = NextMemberBuddyLocator.class.getName();
  +       Properties props = null;
  +       try
  +       {
  +          props = XmlHelper.readPropertiesContents(element, "buddyLocatorProperties");
  +       }
  +       catch (IOException e)
  +       {
  +          log.warn("Caught exception reading buddyLocatorProperties", e);
  +          log.error("Unable to read buddyLocatorProperties specified!  Using defaults for [" + buddyLocatorClass + "]");
  +       }
  +       BuddyLocatorConfig blc = new BuddyLocatorConfig();
  +       blc.setBuddyLocatorClass(buddyLocatorClass);
  +       blc.setBuddyLocatorProperties(props);
  +       brc.setBuddyLocatorConfig(blc);
  +       
  +       return brc;
  +    }
  +    
  +    public static CacheLoaderConfig parseCacheLoaderConfig(Element element)
  +    {
  +       CacheLoaderConfig clc = new CacheLoaderConfig();
  +       clc.setPassivation(XmlHelper.readBooleanContents(element, "passivation"));
  +       clc.setPreload(XmlHelper.readStringContents(element, "preload"));
  +       clc.setShared(XmlHelper.readBooleanContents(element, "shared"));
  +       
  +       NodeList cacheLoaderNodes = element.getElementsByTagName("cacheloader");
  +       for (int i = 0; i < cacheLoaderNodes.getLength(); i++)
  +       {
  +          Node node = cacheLoaderNodes.item(i);
  +          if (node.getNodeType() == Node.ELEMENT_NODE)
  +          {
  +             Element indivElement = (Element) node;
  +             CacheLoaderConfig.IndividualCacheLoaderConfig iclc = new CacheLoaderConfig.IndividualCacheLoaderConfig();
  +             iclc.setAsync(XmlHelper.readBooleanContents(indivElement, "async", false));
  +             iclc.setIgnoreModifications(XmlHelper.readBooleanContents(indivElement, "ignoreModifications", false));
  +             iclc.setFetchPersistentState(XmlHelper.readBooleanContents(indivElement, "fetchPersistentState", false));
  +             iclc.setPurgeOnStartup(XmlHelper.readBooleanContents(indivElement, "purgeOnStartup", false));
  +             iclc.setClassName(XmlHelper.readStringContents(indivElement, "class"));
  +             try
  +             {
  +                iclc.setProperties(XmlHelper.readPropertiesContents(indivElement, "properties"));
  +             }
  +             catch (IOException e)
  +             {
  +                throw new ConfigurationException("Problem loader cache loader properties", e);
  +             }
  +             iclc.setSingletonStore(XmlHelper.readBooleanContents(indivElement, "singletonStore", false));
  +             iclc.setPushStateWhenCoordinator(XmlHelper.readBooleanAttribute(indivElement, "singletonStore", "pushStateWhenCoordinator", false));
  +             clc.addIndividualCacheLoaderConfig(iclc);
  +          }
  +       }
  +       
  +       return clc;
  +    }
  +    
  +    public static EvictionConfig parseEvictionConfig(Element element, String defaultEvictionClass)
  +    {
  +       EvictionConfig ec = new EvictionConfig();
  +       
  +       ec.setDefaultEvictionPolicyClass(defaultEvictionClass);
  +       
  +       if (element != null)
  +       {
  +          String temp = XmlHelper.getTagContents(element,
  +                  EvictionConfiguration.WAKEUP_INTERVAL_SECONDS, EvictionConfiguration.ATTR, EvictionConfiguration.NAME);
  +    
  +          int wakeupIntervalSeconds = 0;
  +          if (temp != null)
  +          {             
  +             wakeupIntervalSeconds = Integer.parseInt(temp);
  +          }
  +    
  +          if (wakeupIntervalSeconds <= 0)
  +             wakeupIntervalSeconds = EvictionConfiguration.WAKEUP_DEFAULT;
  +    
  +          ec.setWakeupIntervalSeconds(wakeupIntervalSeconds);
  +
  +          int eventQueueSize = 0;
  +          temp = XmlHelper.getTagContents(element,
  +                  EvictionConfiguration.EVENT_QUEUE_SIZE, EvictionConfiguration.ATTR, EvictionConfiguration.NAME);
  +
  +          if (temp != null)
  +          {
  +             eventQueueSize = Integer.parseInt(temp);
  +          }
  +
  +          if (eventQueueSize <= 0) eventQueueSize = EvictionConfiguration.EVENT_QUEUE_SIZE_DEFAULT;
  +
  +          ec.setEventQueueSize(eventQueueSize);
  +          
  +          NodeList list = element.getElementsByTagName(EvictionConfiguration.REGION);
  +          if (list != null && list.getLength() > 0)
  +          {
  +             List regionConfigs = new ArrayList(list.getLength());
  +             for (int i = 0; i < list.getLength(); i++)
  +             {
  +                org.w3c.dom.Node node = list.item(i);
  +                if (node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE)
  +                   continue;
  +                try
  +                {
  +                   regionConfigs.add(parseEvictionRegionConfig((Element) node, defaultEvictionClass));
  +                }
  +                catch (MissingPolicyException ignored)
  +                {
  +                   // Just log a warning and continue
  +                   // TODO (BES Oct-2006) I did it this way as that is how it worked
  +                   // before, but why not just throw the exception?
  +                   LogFactory.getLog(EvictionConfig.class).warn(ignored.getLocalizedMessage());
  +                }
  +             }
  +             
  +             ec.setEvictionRegionConfigs(regionConfigs);
  +          }
  +       }
  +       
  +       return ec;
  +       
  +    }
  +    
  +    public static EvictionRegionConfig parseEvictionRegionConfig(Element element, String defaultEvictionClass)
  +    {
  +       EvictionRegionConfig erc = new EvictionRegionConfig();
  +       
  +       erc.setRegionName(element.getAttribute(EvictionConfiguration.NAME));
  +       
  +       String evictionClass = element.getAttribute(EvictionConfiguration.REGION_POLICY_CLASS);
  +       if (evictionClass == null || evictionClass.length() == 0)
  +       {
  +          evictionClass = defaultEvictionClass;
  +          // if it's still null... what do we configure?
  +          if (evictionClass == null || evictionClass.length() == 0)
  +          {
  +             throw new MissingPolicyException(
  +                     "There is no Eviction Policy Class specified on the region or for the entire cache!");
  +          }
  +       }
  +
  +       EvictionPolicy policy = null;
  +       try
  +       {
  +          policy = (EvictionPolicy) Util.loadClass(evictionClass).newInstance();
  +       }
  +       catch (RuntimeException e)
  +       {
  +          throw e;
  +       }
  +       catch (Exception e)
  +       {
  +          throw new RuntimeException("Eviction class is not properly loaded in classloader", e);
  +       }
  +       
  +       erc.setEvictionPolicyClass(evictionClass);
  +       
  +       EvictionConfiguration evc = null;
  +       try
  +       {
  +          evc = (EvictionConfiguration) policy.getEvictionConfigurationClass().newInstance();
  +       }
  +       catch (RuntimeException e)
  +       {
  +          throw e;
  +       }
  +       catch (Exception e)
  +       {
  +          throw new RuntimeException("Failed to instantiate EvictionConfiguration of class " + 
  +                                     policy.getEvictionConfigurationClass(), e);
  +       }
  +       
  +       evc.parseXMLConfig(element);
  +       erc.setEvictionConfiguration(evc);
  +       
  +       return erc;
  +    }
   }
  
  
  



More information about the jboss-cvs-commits mailing list