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

Manik Surtani msurtani at jboss.com
Tue Jul 18 06:50:45 EDT 2006


  User: msurtani
  Date: 06/07/18 06:50:45

  Modified:    src/org/jboss/cache/config   Configuration.java
                        ConfigurationImpl.java
  Log:
  Checked in new Habanero interfaces
  Updated codebase to deal with new interfaces
  
  Revision  Changes    Path
  1.2       +1 -0      JBossCache/src/org/jboss/cache/config/Configuration.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Configuration.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/config/Configuration.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- Configuration.java	21 Jun 2006 11:17:25 -0000	1.1
  +++ Configuration.java	18 Jul 2006 10:50:45 -0000	1.2
  @@ -18,6 +18,7 @@
    * will cache the values they need since the convenience methods are less than efficient.  
    *
    * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
  + * @since 2.0.0
    */
   public interface Configuration
   {
  
  
  
  1.2       +69 -161   JBossCache/src/org/jboss/cache/config/ConfigurationImpl.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ConfigurationImpl.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/config/ConfigurationImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- ConfigurationImpl.java	21 Jun 2006 11:17:25 -0000	1.1
  +++ ConfigurationImpl.java	18 Jul 2006 10:50:45 -0000	1.2
  @@ -10,6 +10,8 @@
   import org.apache.commons.logging.LogFactory;
   import org.jboss.cache.xml.XmlHelper;
   import org.w3c.dom.Element;
  +import org.w3c.dom.Node;
  +import org.w3c.dom.NodeList;
   
   import java.io.InputStream;
   import java.util.HashMap;
  @@ -86,172 +88,78 @@
           if (!immutable) stringAttribs.put(attribute, value);
       }
   
  -    public void readConfiguration(InputStream stream) throws ConfigurationException
  +    public void setImmutable(boolean immutable)
       {
  -//        if (!immutable)
  -//        {
  -//            // loop through all elements in XML.
  -//            if (stream == null) throw new ConfigurationException("Input stream for configuration xml is null!");
  -//
  -//            Element root=loadDocument(stream);
  -//            Element mbeanElement=getMBeanElement(root);
  -//            NodeList list=mbeanElement.getElementsByTagName(ATTR);
  -//            log.info("attribute size: " + list.getLength());
  -//
  -//            Class objClass=objToConfigure_.getClass();
  -//            Method[] methods=objClass.getMethods();
  -//            Class[] string_sig=new Class[]{String.class};
  -//
  -//            // step x. 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;
  -//
  -//               Element element=(Element)node;
  -//               String name=element.getAttribute(NAME);
  -//               String valueStr=getElementContent(element, true);
  -//               Element valueObj=null;
  -//               if(valueStr.length() == 0) { // try out element object
  -//                  valueObj=getSubElementObject(element);
  -//               }
  -//               String methodName="set" + name;
  -//               Object value=null;
  -//               Method method=null;
  -//
  -//
  -//               // try whether we have a setter that takes a simple String first
  -//               try {
  -//                  method=objClass.getMethod(methodName, string_sig);
  -//               }
  -//               catch(Exception e) {
  -//                  ; /* we don't have a setter with a String arg, just continue */
  -//               }
  -//
  -//               if(method != null) {
  -//                  try {
  -//                     log.debug("setting attribute " + name + " to " + valueStr);
  -//                     method.invoke(objToConfigure_, new Object[]{valueStr});
  -//                     continue;
  -//                  }
  -//                  catch(Exception ex) {
  -//                     ex.printStackTrace();
  -//                     throw new ConfigurationException("configure(): can't invoke " + methodName + " to configure " +
  -//                             "TreeCache properties. Exception: " + ex);
  -//                  }
  -//               }
  -//
  -//
  -//               // step x. Do a reflection on the object class to see
  -//               // if there is a set"Attribute" method. If not, exception is thrown.
  -//               for(int i=0; i < methods.length; i++) {
  -//                  // Exclude the possibility of overloading methods now.
  -//
  -//                  if(methodName.equals(methods[i].getName())) {
  -//                     method=methods[i];
  -//                     Class[] clz=method.getParameterTypes(); // size should be 1
  -//                     if(clz.length != 1)
  -//                        throw new ConfigurationException("Parameter size of " + methodName +
  -//                                " is not 1 but " + clz.length);
  -//
  -//                     Class classParam=clz[0];
  -//                     // step x. Get the class type for the set"Attribute" argument and convert
  -//                     // the attribute from String to the named class type.
  -//                     PropertyEditor editor= PropertyEditorManager.findEditor(classParam);
  -//                     if(editor == null) {
  -//                        String str="Could not find PropertyEditor for type class " +
  -//                                classParam;
  -//                        throw new ConfigurationException(str);
  -//                     }
  -//
  -//                     if(valueObj != null) { // means there is a sub element
  -//                        editor.setValue(valueObj);
  -//                     }
  -//                     else {
  -//                        editor.setAsText(valueStr);
  -//                     }
  -//
  -//                     value=editor.getValue();
  -//                     log.debug("Invoking setter method: " + method +
  -//                             " with parameter \"" + value + "\" of type " + value.getClass());
  -//
  -//                     // step x. invoke set"Attribute" from method.invoke(object, params)
  -//                     try {
  -//                        method.invoke(objToConfigure_, new Object[]{value});
  -//                        break;
  -//                     }
  -//                     catch(Exception ex) {
  -//                        throw new ConfigurationException("can't invoke " + methodName + " to configure TreeCache", ex);
  -//                     }
  -//                  }
  -//               }
  -//            }
  -//
  -//            // add string properties to the stringAttribs map
  -//            // add xml snippet properties to the xmlAttribs map
  -//        }
  +        this.immutable = immutable;
       }
   
  -    public void setImmutable(boolean immutable)
  +    protected Element getMBeanElement(Element root) throws ConfigurationException
       {
  -        this.immutable = immutable;
  +      // 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;
  +   }
  +
  +    public void readConfiguration(InputStream stream) throws ConfigurationException
  +    {
  +        if (immutable) throw new ConfigurationException("Current configuration is immutable");
  +
   
  +        // loop through all elements in XML.
  +        if (stream == null) throw new ConfigurationException("Input stream for configuration xml is null!");
   
  -//    private Element getSubElementObject(Element element) {
  -//       NodeList nl=element.getChildNodes();
  -//       for(int i=0; i < nl.getLength(); i++) {
  -//          Node node=nl.item(i);
  -//          if(node.getNodeType() == Node.ELEMENT_NODE &&
  -//                  SUB_ATTR.equals( ((Element)node).getTagName() ) ) {
  -//             return (Element)node;
  -//          }
  -//       }
  -//
  -//       log.debug("getSubElementObject(): element object. Does not exist for " + SUB_ATTR);
  -//       return null;
  -//    }
  -//
  -//    private String getElementContent(Element element, boolean trim) {
  -//       NodeList nl=element.getChildNodes();
  -//       String attributeText="";
  -//       for(int i=0; i < nl.getLength(); i++) {
  -//          Node n=nl.item(i);
  -//          if(n instanceof Text) {
  -//             attributeText+=((Text)n).getData();
  -//          }
  -//       } // end of for ()
  -//       if(trim)
  -//          attributeText=attributeText.trim();
  -//       return attributeText;
  -//    }
  -//
  -//
  -//    protected Element loadDocument(InputStream is) throws ConfigurationException {
  -//       Document doc=null;
  -//       try {
  -//          InputSource xmlInp=new InputSource(is);
  -//
  -//          DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory.newInstance();
  -//          DocumentBuilder parser=docBuilderFactory.newDocumentBuilder();
  -//          doc=parser.parse(xmlInp);
  -//          Element root=doc.getDocumentElement();
  -//          root.normalize();
  -//          return root;
  -//       }
  -//       catch(SAXParseException err) {
  -//          log.error("Configurator SAXParse error: " + err.getMessage());
  -//          err.printStackTrace();
  -//       }
  -//       catch(SAXException e) {
  -//          log.error("Configurator SAX error: " + e);
  -//          e.printStackTrace();
  -//       }
  -//       catch(Exception pce) {
  -//          log.error("Configurator general error: " + pce);
  -//          pce.printStackTrace();
  -//       }
  -//       return null;
  -//    }
  +        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);
  +        }
  +    }
  +
  +
  +    public String toString()
  +    {
  +        return "ConfigurationImpl{" +
  +                "stringAttribs=" + stringAttribs +
  +                ", xmlAttribs=" + xmlAttribs +
  +                ", immutable=" + immutable +
  +                '}';
  +    }
   }
  
  
  



More information about the jboss-cvs-commits mailing list