commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cos...@apache.org
Subject cvs commit: jakarta-commons/modeler/src/java/org/apache/commons/modeler/util DomUtil.java
Date Wed, 09 Apr 2003 07:25:18 GMT
costin      2003/04/09 00:25:18

  Modified:    modeler/src/java/org/apache/commons/modeler/modules
                        MbeansSource.java
               modeler/src/java/org/apache/commons/modeler/util
                        DomUtil.java
  Log:
  Finally, the first version of modeler that supports persistence.
  
  Comments and structure of the original file are preserved.
  
  This is obviously very experimental, but both remote jmx and persistence
  should be working at least for the simple cases.
  
  Going to bed now...
  
  Revision  Changes    Path
  1.10      +51 -4     jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansSource.java
  
  Index: MbeansSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansSource.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- MbeansSource.java	7 Apr 2003 06:53:11 -0000	1.9
  +++ MbeansSource.java	9 Apr 2003 07:25:18 -0000	1.10
  @@ -2,6 +2,7 @@
   
   import org.w3c.dom.Document;
   import org.w3c.dom.Node;
  +import org.w3c.dom.NamedNodeMap;
   import org.apache.commons.modeler.util.DomUtil;
   import org.apache.commons.modeler.*;
   import org.apache.commons.logging.Log;
  @@ -9,7 +10,10 @@
   
   import javax.management.*;
   import javax.management.loading.MLet;
  +import javax.xml.transform.TransformerException;
   import java.io.InputStream;
  +import java.io.FileOutputStream;
  +import java.io.FileNotFoundException;
   import java.net.URL;
   import java.util.ArrayList;
   import java.util.List;
  @@ -33,11 +37,17 @@
       String location;
       String type;
       Object source;
  +
  +    // true if we are during the original loading
  +    boolean loading=true;
       List mbeans=new ArrayList();
       static boolean loaderLoaded=false;
       private Document document;
       private HashMap object2Node = new HashMap();
   
  +    long lastUpdate;
  +    long updateInterval=10000; // 10s
  +
       public void setRegistry(Registry reg) {
           this.registry=reg;
       }
  @@ -204,22 +214,59 @@
   
               long t2=System.currentTimeMillis();
               log.info( "Reading mbeans  " + (t2-t1));
  +            loading=false;
           } catch( Exception ex ) {
               log.error( "Error reading mbeans ", ex);
           }
       }
       
       public void updateField( ObjectName oname, String name, 
  -                             Object value ) {
  -        // nothing by default 
  +                             Object value )
  +    {
  +        if( loading ) return;
  +        // nothing by default
           //log.info( "XXX UpdateField " + oname + " " + name + " " + value);
           Node n=(Node)object2Node.get( oname );
           if( n == null ) {
               log.info( "Node not found " + oname );
               return;
           }
  -        n.getAttributes();
  -        
  +        // n is an <mbean> node
  +        Node firstAttN=DomUtil.getChild(n, "attribute");
  +        boolean found=false;
  +        for (Node descN = firstAttN; descN != null;
  +             descN = DomUtil.getNext( descN ))
  +        {
  +            String thisAttName=DomUtil.getAttribute(descN, "name");
  +            if( name.equals( thisAttName )) {
  +                // XXX value = null, remove existing, conversions
  +                found=true;
  +                DomUtil.setAttribute(descN, "value", value.toString());
  +                break;
  +            }
  +        }
  +        if( ! found ) {
  +            Node attN=n.getOwnerDocument().createElement("attribute");
  +            DomUtil.setAttribute(attN, "name", name);
  +            DomUtil.setAttribute(attN, "value", value.toString());
  +            n.appendChild(attN);
  +        }
  +
  +
  +        // XXX no often than, etc.
  +        long time=System.currentTimeMillis();
  +        if( location!=null &&
  +                time - lastUpdate > updateInterval ) {
  +            lastUpdate=time;
  +            try {
  +                FileOutputStream fos=new FileOutputStream(location);
  +                DomUtil.writeXml(document, fos);
  +            } catch (TransformerException e) {
  +                log.error( "Error writing");
  +            } catch (FileNotFoundException e) {
  +                log.error( "Error writing" ,e );
  +            }
  +        }
       }
       
       public void store() {
  
  
  
  1.4       +25 -0     jakarta-commons/modeler/src/java/org/apache/commons/modeler/util/DomUtil.java
  
  Index: DomUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/util/DomUtil.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DomUtil.java	7 Jan 2003 21:07:40 -0000	1.3
  +++ DomUtil.java	9 Apr 2003 07:25:18 -0000	1.4
  @@ -61,6 +61,12 @@
   import java.io.*;
   
   import javax.xml.parsers.*;
  +import javax.xml.transform.TransformerFactory;
  +import javax.xml.transform.Transformer;
  +import javax.xml.transform.TransformerConfigurationException;
  +import javax.xml.transform.TransformerException;
  +import javax.xml.transform.dom.DOMSource;
  +import javax.xml.transform.stream.StreamResult;
   
   import org.w3c.dom.*;
   import org.xml.sax.*;
  @@ -121,6 +127,17 @@
           return attN.getNodeValue();
       }
   
  +    public static void setAttribute(Node node, String attName, String val) {
  +        Element element=(Element)node;
  +        if( element.getAttribute(attName) != null ) {
  +            element.removeAttribute(attName);
  +        }
  +//        Node attNode=element.getOwnerDocument().createAttribute(attName);
  +//        attNode.setNodeValue(val);
  +        element.setAttribute(attName, val);
  +//        element.appendChild(attNode);
  +    }
  +
       /** Get the first child's content ( ie it's included TEXT node ).
        */
       public static String getChildContent( Node parent, String name ) {
  @@ -216,4 +233,12 @@
           return doc;
       }
   
  +    public static void writeXml( Node n, OutputStream os )
  +            throws TransformerException
  +    {
  +        TransformerFactory tf=TransformerFactory.newInstance();
  +        //identity
  +        Transformer t=tf.newTransformer();
  +        t.transform(new DOMSource( n ), new StreamResult( os ));
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message