Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 7769 invoked from network); 9 Apr 2003 07:25:29 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 9 Apr 2003 07:25:29 -0000 Received: (qmail 14671 invoked by uid 97); 9 Apr 2003 07:27:32 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 14664 invoked from network); 9 Apr 2003 07:27:32 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 9 Apr 2003 07:27:32 -0000 Received: (qmail 7517 invoked by uid 500); 9 Apr 2003 07:25:22 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 7490 invoked by uid 500); 9 Apr 2003 07:25:20 -0000 Received: (qmail 7477 invoked from network); 9 Apr 2003 07:25:19 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 9 Apr 2003 07:25:19 -0000 Received: (qmail 78484 invoked by uid 1052); 9 Apr 2003 07:25:18 -0000 Date: 9 Apr 2003 07:25:18 -0000 Message-ID: <20030409072518.78482.qmail@icarus.apache.org> From: costin@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/modeler/src/java/org/apache/commons/modeler/util DomUtil.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N 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 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