You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by vi...@apache.org on 2008/03/20 15:56:57 UTC

svn commit: r639303 - in /geronimo/server/trunk/plugins/monitoring/agent-jar: ./ src/main/java/org/apache/geronimo/monitoring/snapshot/ src/xsd/

Author: viet
Date: Thu Mar 20 07:56:56 2008
New Revision: 639303

URL: http://svn.apache.org/viewvc?rev=639303&view=rev
Log:
Fix for Geronimo-3925. Uses JAXB to manipulate XML.

Added:
    geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java   (with props)
    geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java   (with props)
    geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/
    geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd   (with props)
Modified:
    geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml
    geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java

Modified: geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml?rev=639303&r1=639302&r2=639303&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml (original)
+++ geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml Thu Mar 20 07:56:56 2008
@@ -53,6 +53,18 @@
             <artifactId>geronimo-j2ee-management_1.1_spec</artifactId>
             <scope>provided</scope>
         </dependency>
+
+        <dependency>
+            <groupId>javax.xml.bind</groupId>
+            <artifactId>jaxb-api</artifactId>
+            <version>2.0</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>javax.xml.bind</groupId>
+                    <artifactId>jsr173_api</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
     </dependencies>
 
 </project>

Added: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java?rev=639303&view=auto
==============================================================================
--- geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java (added)
+++ geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java Thu Mar 20 07:56:56 2008
@@ -0,0 +1,57 @@
+//
+// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0-b26-ea3 
+// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
+// Any modifications to this file will be lost upon recompilation of the source schema. 
+// Generated on: 2008.03.18 at 12:52:02 AM GMT-05:00 
+//
+
+
+package org.apache.geronimo.monitoring.snapshot;
+
+import javax.xml.bind.annotation.XmlRegistry;
+import org.apache.geronimo.monitoring.snapshot.SnapshotConfig;
+import org.apache.geronimo.monitoring.snapshot.SnapshotConfig.Mbeans;
+
+
+/**
+ * This object contains factory methods for each 
+ * Java content interface and Java element interface 
+ * generated in the org.apache.geronimo package. 
+ * <p>An ObjectFactory allows you to programatically 
+ * construct new instances of the Java representation 
+ * for XML content. The Java representation of XML 
+ * content can consist of schema derived interfaces 
+ * and classes representing the binding of schema 
+ * type definitions, element declarations and model 
+ * groups.  Factory methods for each of these are 
+ * provided in this class.
+ * 
+ */
+@XmlRegistry
+public class ObjectFactory {
+
+
+    /**
+     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.apache.geronimo
+     * 
+     */
+    public ObjectFactory() {
+    }
+
+    /**
+     * Create an instance of {@link Mbeans }
+     * 
+     */
+    public Mbeans createSnapshotConfigMbeans() {
+        return new Mbeans();
+    }
+
+    /**
+     * Create an instance of {@link SnapshotConfig }
+     * 
+     */
+    public SnapshotConfig createSnapshotConfig() {
+        return new SnapshotConfig();
+    }
+
+}

Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java?rev=639303&view=auto
==============================================================================
--- geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java (added)
+++ geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java Thu Mar 20 07:56:56 2008
@@ -0,0 +1,196 @@
+//
+// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0-b26-ea3 
+// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
+// Any modifications to this file will be lost upon recompilation of the source schema. 
+// Generated on: 2008.03.18 at 12:52:02 AM GMT-05:00 
+//
+
+
+package org.apache.geronimo.monitoring.snapshot;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlType;
+
+
+/**
+ * <p>Java class for snapshot-config element declaration.
+ * 
+ * <p>The following schema fragment specifies the expected content contained within this class.
+ * 
+ * <pre>
+ * &lt;element name="snapshot-config">
+ *   &lt;complexType>
+ *     &lt;complexContent>
+ *       &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *         &lt;sequence>
+ *           &lt;element name="duration" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           &lt;element name="retention" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           &lt;element name="mbeans">
+ *             &lt;complexType>
+ *               &lt;complexContent>
+ *                 &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *                   &lt;sequence>
+ *                     &lt;element name="mbean" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/>
+ *                   &lt;/sequence>
+ *                 &lt;/restriction>
+ *               &lt;/complexContent>
+ *             &lt;/complexType>
+ *           &lt;/element>
+ *         &lt;/sequence>
+ *       &lt;/restriction>
+ *     &lt;/complexContent>
+ *   &lt;/complexType>
+ * &lt;/element>
+ * </pre>
+ * 
+ * 
+ */
+@XmlAccessorType(XmlAccessType.FIELD)
+@XmlType(name = "", propOrder = {
+    "duration",
+    "retention",
+    "mbeans"
+})
+@XmlRootElement(name = "snapshot-config")
+public class SnapshotConfig {
+
+    protected String duration;
+    protected String retention;
+    protected Mbeans mbeans;
+
+    /**
+     * Gets the value of the duration property.
+     * 
+     * @return
+     *     possible object is
+     *     {@link String }
+     *     
+     */
+    public String getDuration() {
+        return duration;
+    }
+
+    /**
+     * Sets the value of the duration property.
+     * 
+     * @param value
+     *     allowed object is
+     *     {@link String }
+     *     
+     */
+    public void setDuration(String value) {
+        this.duration = value;
+    }
+
+    /**
+     * Gets the value of the retention property.
+     * 
+     * @return
+     *     possible object is
+     *     {@link String }
+     *     
+     */
+    public String getRetention() {
+        return retention;
+    }
+
+    /**
+     * Sets the value of the retention property.
+     * 
+     * @param value
+     *     allowed object is
+     *     {@link String }
+     *     
+     */
+    public void setRetention(String value) {
+        this.retention = value;
+    }
+
+    /**
+     * Gets the value of the mbeans property.
+     * 
+     * @return
+     *     possible object is
+     *     {@link Mbeans }
+     *     
+     */
+    public Mbeans getMbeans() {
+        return mbeans;
+    }
+
+    /**
+     * Sets the value of the mbeans property.
+     * 
+     * @param value
+     *     allowed object is
+     *     {@link Mbeans }
+     *     
+     */
+    public void setMbeans(Mbeans value) {
+        this.mbeans = value;
+    }
+
+
+    /**
+     * <p>Java class for anonymous complex type.
+     * 
+     * <p>The following schema fragment specifies the expected content contained within this class.
+     * 
+     * <pre>
+     * &lt;complexType>
+     *   &lt;complexContent>
+     *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+     *       &lt;sequence>
+     *         &lt;element name="mbean" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/>
+     *       &lt;/sequence>
+     *     &lt;/restriction>
+     *   &lt;/complexContent>
+     * &lt;/complexType>
+     * </pre>
+     * 
+     * 
+     */
+    @XmlAccessorType(XmlAccessType.FIELD)
+    @XmlType(name = "", propOrder = {
+        "mbean"
+    })
+    public static class Mbeans {
+
+        protected List<String> mbean;
+
+        /**
+         * Gets the value of the mbean property.
+         * 
+         * <p>
+         * This accessor method returns a reference to the live list,
+         * not a snapshot. Therefore any modification you make to the
+         * returned list will be present inside the JAXB object.
+         * This is why there is not a <CODE>set</CODE> method for the mbean property.
+         * 
+         * <p>
+         * For example, to add a new item, do as follows:
+         * <pre>
+         *    getMbean().add(newItem);
+         * </pre>
+         * 
+         * 
+         * <p>
+         * Objects of the following type(s) are allowed in the list
+         * {@link String }
+         * 
+         * 
+         */
+        public List<String> getMbean() {
+            if (mbean == null) {
+                mbean = new ArrayList<String>();
+            }
+            return this.mbean;
+        }
+
+    }
+
+}

Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java?rev=639303&r1=639302&r2=639303&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java (original)
+++ geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java Thu Mar 20 07:56:56 2008
@@ -20,18 +20,12 @@
 import java.io.FileOutputStream;
 import java.util.ArrayList;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.xmlbeans.XmlCursor;
-import org.apache.xmlbeans.XmlObject;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
 
 /**
  * In charge of dealing with the XML processing of the snapshot's data.
@@ -46,88 +40,91 @@
     private static final String DURATION = "duration";
     private static final String RETENTION = "retention";
     private static final String MBEAN = "mbean"; 
+
+    private static JAXBContext jc = null;
+
+    static {
+        try {
+            jc = JAXBContext.newInstance("org.apache.geronimo.monitoring.snapshot");
+        } catch(Exception e) {
+            log.error(e.getMessage());
+        }
+    }
+
+    /**
+     * @return SnapshotConfig object which represents the XML document
+     */
+    private static SnapshotConfig getSnapshotConfig() {
+        // ensure that there is a snapshot-config.xml at all times
+        try {
+            if(checkXMLExists()) {
+                // unmarshall the xml document into SnapshotConfig object
+                Unmarshaller m = jc.createUnmarshaller();
+                return (SnapshotConfig)m.unmarshal( new File(pathToXML) );
+            } else {
+                SnapshotConfig sc = new SnapshotConfig();
+                sc.setMbeans( new SnapshotConfig.Mbeans() );
+                saveDocument(sc);
+                return sc;
+            }
+        } catch(Exception e) {
+            log.error(e.getMessage());
+            return null;    // in the case of an error, return null
+        }
+    }
     
     /**
      * @return A list of all mbean names that have been previously saved.
      * These mbean names are those to keep track of for per snapshot.
      */
     public static ArrayList<String> getMBeanNames() {
-        ArrayList<String> mbeanList = new ArrayList<String>();
-        // get an instance of the document
-        Document doc = openDocument();
-        // get the root element node
-        Element rootElement = doc.getDocumentElement();
-        // get all children in the root node (i.e. all config properties)
-        NodeList configNodes = rootElement.getChildNodes();
-        // find the duration node and save it
-        for(int i = 0; i < configNodes.getLength(); i++) {
-            if(MBEAN.equals(configNodes.item(i).getNodeName())) {
-                mbeanList.add( configNodes.item(i).getTextContent() );
-            }
+        if(getSnapshotConfig().getMbeans() == null) {
+            return new ArrayList<String>();
+        } else {
+            return (ArrayList<String>)getSnapshotConfig().getMbeans().getMbean();
         }
-        return mbeanList;
     }
     
     /**
-     * Adds to the snapshot-config.xml another configuration element <mbean>
+     * Removes from the snapshot-config.xml a configuration element <mbean>
      * in order to persistently keep track of all user requested statistics.
-     * If there is a duplicate, nothing will be done.
+     * If there does not exist an instance of the mbeanNAme, nothing will be done.
      */
     public static boolean removeMBeanName(String mbeanName) {
-        ArrayList<String> mbeanList = getMBeanNames();
-        // operate on the snapshot-config.xml if there exists the mbean name
-        if(mbeanList.contains(mbeanName)) {
-            // get an instance of the document
-            Document doc = openDocument();
-            // get the root element node
-            Element rootElement = doc.getDocumentElement();
-            // find the Node that represents the mbeanName
-            NodeList list = rootElement.getChildNodes();
-            for(int i = 0; i < list.getLength(); i++) {
-                // check the Node's text context for a match with mbeanName
-                if(list.item(i).getTextContent().equals(mbeanName)) {
-                    // remove the node from rootElement
-                    Node toRemoveNode = list.item(i);
-                    rootElement.removeChild(toRemoveNode);
-                    break;
-                }
+        ArrayList<String> mbeanNames = getMBeanNames();
+        for(int i = 0 ; i < (int)mbeanNames.size(); i++) {
+            if(mbeanNames.get(i).equals(mbeanName)) {
+                // remove the mbean name by directly accessing it, because it is by reference
+                SnapshotConfig sc = getSnapshotConfig();
+                sc.getMbeans().getMbean().remove(i);
+                // save the current state of the SnapshotConfig object
+                saveDocument( sc );
+                return true;
             }
-            // save the document
-            saveDocument(doc, pathToXML);
-            return true;
-        } else {
-            return false;
         }
+        return false;
     }
 
     /**
-     * Removes from the snapshot-config.xml a configuration element <mbean>
+     * Adds to the snapshot-config.xml another configuration element <mbean>
      * in order to persistently keep track of all user requested statistics.
-     * If there does not exist an instance of the mbeanNAme, nothing will be done.
+     * If there is a duplicate, nothing will be done.
      */
     public static boolean addMBeanName(String mbeanName) {
-        ArrayList<String> mbeanList = getMBeanNames();
-        if(mbeanList.contains(mbeanName)) {
-            return false;
-        } else {
-            // get an instance of the document
-            Document doc = openDocument();
-            // get the root element node
-            Element rootElement = doc.getDocumentElement();
-            // create <mbean> element
-            Element mbeanElement = doc.createElement(MBEAN);
-            mbeanElement.setTextContent(mbeanName);
-            // add <mbean> element to the rootElement
-            rootElement.appendChild(mbeanElement);
-            try {
-                Thread.sleep(1000);
-            } catch(Exception e) {
-                
+        // check to see if the mbean name already exists
+        ArrayList<String> mbeanNames = getMBeanNames();
+        for(int i = 0 ; i < (int)mbeanNames.size(); i++) {
+            if(mbeanNames.get(i).equals(mbeanName)) {
+                return false;   // nothing needs to be done if it is already there
             }
-            // save the document
-            saveDocument(doc, pathToXML);
-            return true;
         }
+
+        // insert the mbean name into the SnapshotConfig object
+        SnapshotConfig sc = getSnapshotConfig();
+        sc.getMbeans().getMbean().add(mbeanName);
+        // write the object to XML
+        saveDocument(sc);
+        return true;
     }
     
     /**
@@ -135,7 +132,9 @@
      * @param duration
      */
     public static void saveDuration(long duration) {
-        saveAttribute(DURATION, duration);
+        SnapshotConfig sc = getSnapshotConfig();
+        sc.setDuration("" + duration);
+        saveDocument(sc);
     }
 
     /**
@@ -143,47 +142,11 @@
      * @param retention
      */
     public static void saveRetention(int retention) {
-        saveAttribute(RETENTION, retention);
+        SnapshotConfig sc = getSnapshotConfig();
+        sc.setRetention("" + retention);
+        saveDocument(sc);
     }
-    
-    /**
-     * Saves a generic attribute value into the node with text = attribute name.
-     * Creates one if there is not an instance of one.
-     * @param attrName
-     * @param attributeValue
-     */
-    private static void saveAttribute(String attrName, long attributeValue) {
-        Document doc = openDocument();
-        // get the root node        
-        Element rootElement = doc.getDocumentElement();
-        // get all children in the root node (i.e. all config properties)
-        NodeList configNodes = rootElement.getChildNodes();
-        // find the duration node and save it
-        boolean foundNode = false;
-        for(int i = 0; i < configNodes.getLength() && !foundNode; i++) {
-            Node configNode = configNodes.item(i);
-            if(attrName.equals(configNode.getNodeName())) {
-                // found a match
-                configNode.setTextContent(attributeValue + "");
-                foundNode = true;
-            }
-        }
-        // if there was not a duration node, make one
-        if(!foundNode) {
-            Element element = doc.createElement(attrName);
-            element.setTextContent(attributeValue + "");
-            rootElement.appendChild(element);
-        }
-        try {
-            Thread.sleep(1000);
-        } catch(Exception e) {
-            
-        }
-        log.info("***saving:  " + attrName + " = " + attributeValue);
-        // save the document to file
-        saveDocument(doc, pathToXML);
-    }
-    
+       
     /**
      * Returns the value of the configuration attribute, defined by the key
      * @param key
@@ -191,107 +154,39 @@
      * @throws Exception
      */
     public static String getAttributeValue(String key) throws Exception {
-        // ensure that there exists the 'monitor' directory
-        ensureMonitorDir();
-        // get an instance of the document
-        Document doc = openDocument();
-        // get the root element node
-        Element rootElement = doc.getDocumentElement();
-        // get all children in the root node (i.e. all config properties)
-        NodeList configNodes = rootElement.getChildNodes();
-        // find the duration node and save it
-        for(int i = 0; i < configNodes.getLength(); i++) {
-            if(key.equals(configNodes.item(i).getNodeName())) {
-                return configNodes.item(i).getTextContent();
-            }
+        if(key.equals( DURATION )) {
+            return getSnapshotConfig().getDuration();
+        } else if(key.equals( RETENTION )) {
+            return getSnapshotConfig().getRetention();
+        } else {
+            // Houston, we have a problem
+            throw new Exception("[WARNING] Attribute: " + key + " is not valid.");
         }
-        throw new Exception("[WARNING] " + key + " is not found in " + SNAPSHOT_CONFIG);
     }
     
     /**
      * Ensures that there is an existing XML file. Creates one if there
      * does not exist one already. 
      */
-    public static void checkXMLExists() {
+    public static boolean checkXMLExists() {
+        ensureMonitorDir();
         File docFile = new File(pathToXML);
-        // create an XML document if it does not exist
-        if(!docFile.exists()) {
-            Document doc = setUpDocument( createDocument() );
-            saveDocument(doc, pathToXML);
-        }
+        return docFile.exists();
     }
     
-    /**
-     * Prepares the root element for a document.
-     */
-    public static Document setUpDocument(Document document) {
-        // add <snapshot-config> tag as the root
-        Element rootElement = document.createElement("snapshot-config");
-        document.appendChild(rootElement);
-        return document;
-    }
-    
-    /**
-     * Creates an instance of a Document and returns it
-     */
-    public static Document createDocument() {
-        // get an instance of factory
-        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-        try {
-            // get an instance of builder
-            DocumentBuilder db = dbf.newDocumentBuilder();
-            // create an instance of DOM
-            return db.newDocument();
-        } catch(ParserConfigurationException pce) {
-            log.error("Error while trying to instantiate DocumentBuilder", pce);
-        }
-        return null;
-    }
     
     /**
-     * Write the document object to the file location specified by
-     * the path.
+     * Write the XML document.
      */
-    public static void saveDocument(Document document, String path) {
+    public static void saveDocument(SnapshotConfig sc) {
         try {
-            // before saving, make sure the directory is present
-            ensureMonitorDir();
-
-            //TODO GERONIMO-3719.  Hack to use xmlbeans to write out xml instead of sun specific classes.
-            XmlObject xmlObject = XmlObject.Factory.parse(document.getDocumentElement());
-            xmlObject.save(new File(path));
-
-            // formatting the doc
-            // generate a file output
+            Marshaller m = jc.createMarshaller();
+            m.marshal(sc, new FileOutputStream( pathToXML ));
         } catch(Exception e) {
-            log.error(e.getMessage(), e);
+            log.error(e.getMessage());
         }
     }
 
-    /**
-     * Parses the XML document specified by the private member 'pathToXML'
-     * and stores the information in the a Document object
-     */
-    public static Document openDocument() {
-        // ensure that the XML file is there
-        checkXMLExists();
-        // get the factory
-        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-        // continue to attempt to parse
-        while(true) {
-            try {
-                // Using factory get an instance of document builder
-                DocumentBuilder db = dbf.newDocumentBuilder();
-                // parse using builder to get DOM representation of the XML file
-                Document doc = db.parse(pathToXML);
-                return doc;
-            } catch(Exception e) {
-                // Either this file is being read/written to by snapshot thread
-                // or there is an UNKNOWN error
-                log.error(e.getMessage(), e);
-            }
-        }
-    }
     
     /**
      * Checks to see if the GERONIMO_HOME/var/monitoring/ directory was made.

Added: geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd?rev=639303&view=auto
==============================================================================
--- geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd (added)
+++ geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd Thu Mar 20 07:56:56 2008
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+     http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+-->
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+            targetNamespace="http://geronimo.apache.org"
+            xmlns:tns="http://geronimo.apache.org">
+
+    <xsd:element name="snapshot-config">
+        <xsd:complexType>
+            <xsd:sequence>
+                <xsd:element name="duration" type="xsd:string"
+                             minOccurs="1"  maxOccurs="1"/>
+                <xsd:element name="retention" type="xsd:string"
+                             minOccurs="1"  maxOccurs="1"/>
+                <xsd:element name="mbeans">
+                    <xsd:complexType>
+                        <xsd:sequence>
+                            <xsd:element name="mbean" type="xsd:string" 
+                                         minOccurs="0" maxOccurs="unbounded"/>
+                        </xsd:sequence>
+                    </xsd:complexType>
+                </xsd:element>
+            </xsd:sequence>
+        </xsd:complexType>
+
+    </xsd:element>
+
+</xsd:schema>

Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd
------------------------------------------------------------------------------
    svn:mime-type = text/xml



Re: svn commit: r639303 - in /geronimo/server/trunk/plugins/monitoring/agent-jar: ./ src/main/java/org/apache/geronimo/monitoring/snapshot/ src/xsd/

Posted by Joe Bohn <jo...@earthlink.net>.
Donald Woods wrote:
<snip>
>> +
>> +        <dependency>
>> +            <groupId>javax.xml.bind</groupId>
>> +            <artifactId>jaxb-api</artifactId>
>> +            <version>2.0</version>
> 
> Can you remove the explicit <version> and use the one set by 
> trunk/server/pom.xml?
<snip>

Good catch Donald ... thanks for reviewing the commits.

Joe

Re: svn commit: r639303 - in /geronimo/server/trunk/plugins/monitoring/agent-jar: ./ src/main/java/org/apache/geronimo/monitoring/snapshot/ src/xsd/

Posted by Viet Nguyen <vh...@gmail.com>.
Thanks Donald, I took out the <version> tag and inserted
<scope>provided</scope>.

--Viet

On Thu, Mar 20, 2008 at 8:35 PM, Donald Woods <dw...@apache.org> wrote:
>
>
>
>  viet@apache.org wrote:
>  > Author: viet
>  > Date: Thu Mar 20 07:56:56 2008
>  > New Revision: 639303
>  >
>  > URL: http://svn.apache.org/viewvc?rev=639303&view=rev
>  > Log:
>  > Fix for Geronimo-3925. Uses JAXB to manipulate XML.
>  >
>  > Added:
>  >     geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java   (with props)
>  >     geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java   (with props)
>  >     geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/
>  >     geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd   (with props)
>  > Modified:
>  >     geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml
>  >     geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java
>  >
>  > Modified: geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml
>  > URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml?rev=639303&r1=639302&r2=639303&view=diff
>  > ==============================================================================
>  > --- geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml (original)
>  > +++ geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml Thu Mar 20 07:56:56 2008
>  > @@ -53,6 +53,18 @@
>  >              <artifactId>geronimo-j2ee-management_1.1_spec</artifactId>
>  >              <scope>provided</scope>
>  >          </dependency>
>  > +
>  > +        <dependency>
>  > +            <groupId>javax.xml.bind</groupId>
>  > +            <artifactId>jaxb-api</artifactId>
>  > +            <version>2.0</version>
>
>  Can you remove the explicit <version> and use the one set by
>  trunk/server/pom.xml?
>
>
>
>
>  > +            <exclusions>
>  > +                <exclusion>
>  > +                    <groupId>javax.xml.bind</groupId>
>  > +                    <artifactId>jsr173_api</artifactId>
>  > +                </exclusion>
>  > +            </exclusions>
>  > +        </dependency>
>  >      </dependencies>
>  >
>  >  </project>
>  >
>  > Added: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java
>  > URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java?rev=639303&view=auto
>  > ==============================================================================
>  > --- geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java (added)
>  > +++ geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java Thu Mar 20 07:56:56 2008
>  > @@ -0,0 +1,57 @@
>  > +//
>  > +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0-b26-ea3
>  > +// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
>  > +// Any modifications to this file will be lost upon recompilation of the source schema.
>  > +// Generated on: 2008.03.18 at 12:52:02 AM GMT-05:00
>  > +//
>  > +
>  > +
>  > +package org.apache.geronimo.monitoring.snapshot;
>  > +
>  > +import javax.xml.bind.annotation.XmlRegistry;
>  > +import org.apache.geronimo.monitoring.snapshot.SnapshotConfig;
>  > +import org.apache.geronimo.monitoring.snapshot.SnapshotConfig.Mbeans;
>  > +
>  > +
>  > +/**
>  > + * This object contains factory methods for each
>  > + * Java content interface and Java element interface
>  > + * generated in the org.apache.geronimo package.
>  > + * <p>An ObjectFactory allows you to programatically
>  > + * construct new instances of the Java representation
>  > + * for XML content. The Java representation of XML
>  > + * content can consist of schema derived interfaces
>  > + * and classes representing the binding of schema
>  > + * type definitions, element declarations and model
>  > + * groups.  Factory methods for each of these are
>  > + * provided in this class.
>  > + *
>  > + */
>  > +@XmlRegistry
>  > +public class ObjectFactory {
>  > +
>  > +
>  > +    /**
>  > +     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.apache.geronimo
>  > +     *
>  > +     */
>  > +    public ObjectFactory() {
>  > +    }
>  > +
>  > +    /**
>  > +     * Create an instance of {@link Mbeans }
>  > +     *
>  > +     */
>  > +    public Mbeans createSnapshotConfigMbeans() {
>  > +        return new Mbeans();
>  > +    }
>  > +
>  > +    /**
>  > +     * Create an instance of {@link SnapshotConfig }
>  > +     *
>  > +     */
>  > +    public SnapshotConfig createSnapshotConfig() {
>  > +        return new SnapshotConfig();
>  > +    }
>  > +
>  > +}
>  >
>  > Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java
>  > ------------------------------------------------------------------------------
>  >     svn:eol-style = native
>  >
>  > Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java
>  > ------------------------------------------------------------------------------
>  >     svn:keywords = Date Revision
>  >
>  > Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java
>  > ------------------------------------------------------------------------------
>  >     svn:mime-type = text/plain
>  >
>  > Added: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java
>  > URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java?rev=639303&view=auto
>  > ==============================================================================
>  > --- geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java (added)
>  > +++ geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java Thu Mar 20 07:56:56 2008
>  > @@ -0,0 +1,196 @@
>  > +//
>  > +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0-b26-ea3
>  > +// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
>  > +// Any modifications to this file will be lost upon recompilation of the source schema.
>  > +// Generated on: 2008.03.18 at 12:52:02 AM GMT-05:00
>  > +//
>  > +
>  > +
>  > +package org.apache.geronimo.monitoring.snapshot;
>  > +
>  > +import java.util.ArrayList;
>  > +import java.util.List;
>  > +import javax.xml.bind.annotation.XmlAccessType;
>  > +import javax.xml.bind.annotation.XmlAccessorType;
>  > +import javax.xml.bind.annotation.XmlRootElement;
>  > +import javax.xml.bind.annotation.XmlType;
>  > +
>  > +
>  > +/**
>  > + * <p>Java class for snapshot-config element declaration.
>  > + *
>  > + * <p>The following schema fragment specifies the expected content contained within this class.
>  > + *
>  > + * <pre>
>  > + * &lt;element name="snapshot-config">
>  > + *   &lt;complexType>
>  > + *     &lt;complexContent>
>  > + *       &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
>  > + *         &lt;sequence>
>  > + *           &lt;element name="duration" type="{http://www.w3.org/2001/XMLSchema}string"/>
>  > + *           &lt;element name="retention" type="{http://www.w3.org/2001/XMLSchema}string"/>
>  > + *           &lt;element name="mbeans">
>  > + *             &lt;complexType>
>  > + *               &lt;complexContent>
>  > + *                 &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
>  > + *                   &lt;sequence>
>  > + *                     &lt;element name="mbean" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/>
>  > + *                   &lt;/sequence>
>  > + *                 &lt;/restriction>
>  > + *               &lt;/complexContent>
>  > + *             &lt;/complexType>
>  > + *           &lt;/element>
>  > + *         &lt;/sequence>
>  > + *       &lt;/restriction>
>  > + *     &lt;/complexContent>
>  > + *   &lt;/complexType>
>  > + * &lt;/element>
>  > + * </pre>
>  > + *
>  > + *
>  > + */
>  > +@XmlAccessorType(XmlAccessType.FIELD)
>  > +@XmlType(name = "", propOrder = {
>  > +    "duration",
>  > +    "retention",
>  > +    "mbeans"
>  > +})
>  > +@XmlRootElement(name = "snapshot-config")
>  > +public class SnapshotConfig {
>  > +
>  > +    protected String duration;
>  > +    protected String retention;
>  > +    protected Mbeans mbeans;
>  > +
>  > +    /**
>  > +     * Gets the value of the duration property.
>  > +     *
>  > +     * @return
>  > +     *     possible object is
>  > +     *     {@link String }
>  > +     *
>  > +     */
>  > +    public String getDuration() {
>  > +        return duration;
>  > +    }
>  > +
>  > +    /**
>  > +     * Sets the value of the duration property.
>  > +     *
>  > +     * @param value
>  > +     *     allowed object is
>  > +     *     {@link String }
>  > +     *
>  > +     */
>  > +    public void setDuration(String value) {
>  > +        this.duration = value;
>  > +    }
>  > +
>  > +    /**
>  > +     * Gets the value of the retention property.
>  > +     *
>  > +     * @return
>  > +     *     possible object is
>  > +     *     {@link String }
>  > +     *
>  > +     */
>  > +    public String getRetention() {
>  > +        return retention;
>  > +    }
>  > +
>  > +    /**
>  > +     * Sets the value of the retention property.
>  > +     *
>  > +     * @param value
>  > +     *     allowed object is
>  > +     *     {@link String }
>  > +     *
>  > +     */
>  > +    public void setRetention(String value) {
>  > +        this.retention = value;
>  > +    }
>  > +
>  > +    /**
>  > +     * Gets the value of the mbeans property.
>  > +     *
>  > +     * @return
>  > +     *     possible object is
>  > +     *     {@link Mbeans }
>  > +     *
>  > +     */
>  > +    public Mbeans getMbeans() {
>  > +        return mbeans;
>  > +    }
>  > +
>  > +    /**
>  > +     * Sets the value of the mbeans property.
>  > +     *
>  > +     * @param value
>  > +     *     allowed object is
>  > +     *     {@link Mbeans }
>  > +     *
>  > +     */
>  > +    public void setMbeans(Mbeans value) {
>  > +        this.mbeans = value;
>  > +    }
>  > +
>  > +
>  > +    /**
>  > +     * <p>Java class for anonymous complex type.
>  > +     *
>  > +     * <p>The following schema fragment specifies the expected content contained within this class.
>  > +     *
>  > +     * <pre>
>  > +     * &lt;complexType>
>  > +     *   &lt;complexContent>
>  > +     *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
>  > +     *       &lt;sequence>
>  > +     *         &lt;element name="mbean" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/>
>  > +     *       &lt;/sequence>
>  > +     *     &lt;/restriction>
>  > +     *   &lt;/complexContent>
>  > +     * &lt;/complexType>
>  > +     * </pre>
>  > +     *
>  > +     *
>  > +     */
>  > +    @XmlAccessorType(XmlAccessType.FIELD)
>  > +    @XmlType(name = "", propOrder = {
>  > +        "mbean"
>  > +    })
>  > +    public static class Mbeans {
>  > +
>  > +        protected List<String> mbean;
>  > +
>  > +        /**
>  > +         * Gets the value of the mbean property.
>  > +         *
>  > +         * <p>
>  > +         * This accessor method returns a reference to the live list,
>  > +         * not a snapshot. Therefore any modification you make to the
>  > +         * returned list will be present inside the JAXB object.
>  > +         * This is why there is not a <CODE>set</CODE> method for the mbean property.
>  > +         *
>  > +         * <p>
>  > +         * For example, to add a new item, do as follows:
>  > +         * <pre>
>  > +         *    getMbean().add(newItem);
>  > +         * </pre>
>  > +         *
>  > +         *
>  > +         * <p>
>  > +         * Objects of the following type(s) are allowed in the list
>  > +         * {@link String }
>  > +         *
>  > +         *
>  > +         */
>  > +        public List<String> getMbean() {
>  > +            if (mbean == null) {
>  > +                mbean = new ArrayList<String>();
>  > +            }
>  > +            return this.mbean;
>  > +        }
>  > +
>  > +    }
>  > +
>  > +}
>  >
>  > Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java
>  > ------------------------------------------------------------------------------
>  >     svn:eol-style = native
>  >
>  > Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java
>  > ------------------------------------------------------------------------------
>  >     svn:keywords = Date Revision
>  >
>  > Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java
>  > ------------------------------------------------------------------------------
>  >     svn:mime-type = text/plain
>  >
>  > Modified: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java
>  > URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java?rev=639303&r1=639302&r2=639303&view=diff
>  > ==============================================================================
>  > --- geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java (original)
>  > +++ geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java Thu Mar 20 07:56:56 2008
>  > @@ -20,18 +20,12 @@
>  >  import java.io.FileOutputStream;
>  >  import java.util.ArrayList;
>  >
>  > -import javax.xml.parsers.DocumentBuilder;
>  > -import javax.xml.parsers.DocumentBuilderFactory;
>  > -import javax.xml.parsers.ParserConfigurationException;
>  > +import javax.xml.bind.JAXBContext;
>  > +import javax.xml.bind.Marshaller;
>  > +import javax.xml.bind.Unmarshaller;
>  >
>  >  import org.apache.commons.logging.Log;
>  >  import org.apache.commons.logging.LogFactory;
>  > -import org.apache.xmlbeans.XmlCursor;
>  > -import org.apache.xmlbeans.XmlObject;
>  > -import org.w3c.dom.Document;
>  > -import org.w3c.dom.Element;
>  > -import org.w3c.dom.Node;
>  > -import org.w3c.dom.NodeList;
>  >
>  >  /**
>  >   * In charge of dealing with the XML processing of the snapshot's data.
>  > @@ -46,88 +40,91 @@
>  >      private static final String DURATION = "duration";
>  >      private static final String RETENTION = "retention";
>  >      private static final String MBEAN = "mbean";
>  > +
>  > +    private static JAXBContext jc = null;
>  > +
>  > +    static {
>  > +        try {
>  > +            jc = JAXBContext.newInstance("org.apache.geronimo.monitoring.snapshot");
>  > +        } catch(Exception e) {
>  > +            log.error(e.getMessage());
>  > +        }
>  > +    }
>  > +
>  > +    /**
>  > +     * @return SnapshotConfig object which represents the XML document
>  > +     */
>  > +    private static SnapshotConfig getSnapshotConfig() {
>  > +        // ensure that there is a snapshot-config.xml at all times
>  > +        try {
>  > +            if(checkXMLExists()) {
>  > +                // unmarshall the xml document into SnapshotConfig object
>  > +                Unmarshaller m = jc.createUnmarshaller();
>  > +                return (SnapshotConfig)m.unmarshal( new File(pathToXML) );
>  > +            } else {
>  > +                SnapshotConfig sc = new SnapshotConfig();
>  > +                sc.setMbeans( new SnapshotConfig.Mbeans() );
>  > +                saveDocument(sc);
>  > +                return sc;
>  > +            }
>  > +        } catch(Exception e) {
>  > +            log.error(e.getMessage());
>  > +            return null;    // in the case of an error, return null
>  > +        }
>  > +    }
>  >
>  >      /**
>  >       * @return A list of all mbean names that have been previously saved.
>  >       * These mbean names are those to keep track of for per snapshot.
>  >       */
>  >      public static ArrayList<String> getMBeanNames() {
>  > -        ArrayList<String> mbeanList = new ArrayList<String>();
>  > -        // get an instance of the document
>  > -        Document doc = openDocument();
>  > -        // get the root element node
>  > -        Element rootElement = doc.getDocumentElement();
>  > -        // get all children in the root node (i.e. all config properties)
>  > -        NodeList configNodes = rootElement.getChildNodes();
>  > -        // find the duration node and save it
>  > -        for(int i = 0; i < configNodes.getLength(); i++) {
>  > -            if(MBEAN.equals(configNodes.item(i).getNodeName())) {
>  > -                mbeanList.add( configNodes.item(i).getTextContent() );
>  > -            }
>  > +        if(getSnapshotConfig().getMbeans() == null) {
>  > +            return new ArrayList<String>();
>  > +        } else {
>  > +            return (ArrayList<String>)getSnapshotConfig().getMbeans().getMbean();
>  >          }
>  > -        return mbeanList;
>  >      }
>  >
>  >      /**
>  > -     * Adds to the snapshot-config.xml another configuration element <mbean>
>  > +     * Removes from the snapshot-config.xml a configuration element <mbean>
>  >       * in order to persistently keep track of all user requested statistics.
>  > -     * If there is a duplicate, nothing will be done.
>  > +     * If there does not exist an instance of the mbeanNAme, nothing will be done.
>  >       */
>  >      public static boolean removeMBeanName(String mbeanName) {
>  > -        ArrayList<String> mbeanList = getMBeanNames();
>  > -        // operate on the snapshot-config.xml if there exists the mbean name
>  > -        if(mbeanList.contains(mbeanName)) {
>  > -            // get an instance of the document
>  > -            Document doc = openDocument();
>  > -            // get the root element node
>  > -            Element rootElement = doc.getDocumentElement();
>  > -            // find the Node that represents the mbeanName
>  > -            NodeList list = rootElement.getChildNodes();
>  > -            for(int i = 0; i < list.getLength(); i++) {
>  > -                // check the Node's text context for a match with mbeanName
>  > -                if(list.item(i).getTextContent().equals(mbeanName)) {
>  > -                    // remove the node from rootElement
>  > -                    Node toRemoveNode = list.item(i);
>  > -                    rootElement.removeChild(toRemoveNode);
>  > -                    break;
>  > -                }
>  > +        ArrayList<String> mbeanNames = getMBeanNames();
>  > +        for(int i = 0 ; i < (int)mbeanNames.size(); i++) {
>  > +            if(mbeanNames.get(i).equals(mbeanName)) {
>  > +                // remove the mbean name by directly accessing it, because it is by reference
>  > +                SnapshotConfig sc = getSnapshotConfig();
>  > +                sc.getMbeans().getMbean().remove(i);
>  > +                // save the current state of the SnapshotConfig object
>  > +                saveDocument( sc );
>  > +                return true;
>  >              }
>  > -            // save the document
>  > -            saveDocument(doc, pathToXML);
>  > -            return true;
>  > -        } else {
>  > -            return false;
>  >          }
>  > +        return false;
>  >      }
>  >
>  >      /**
>  > -     * Removes from the snapshot-config.xml a configuration element <mbean>
>  > +     * Adds to the snapshot-config.xml another configuration element <mbean>
>  >       * in order to persistently keep track of all user requested statistics.
>  > -     * If there does not exist an instance of the mbeanNAme, nothing will be done.
>  > +     * If there is a duplicate, nothing will be done.
>  >       */
>  >      public static boolean addMBeanName(String mbeanName) {
>  > -        ArrayList<String> mbeanList = getMBeanNames();
>  > -        if(mbeanList.contains(mbeanName)) {
>  > -            return false;
>  > -        } else {
>  > -            // get an instance of the document
>  > -            Document doc = openDocument();
>  > -            // get the root element node
>  > -            Element rootElement = doc.getDocumentElement();
>  > -            // create <mbean> element
>  > -            Element mbeanElement = doc.createElement(MBEAN);
>  > -            mbeanElement.setTextContent(mbeanName);
>  > -            // add <mbean> element to the rootElement
>  > -            rootElement.appendChild(mbeanElement);
>  > -            try {
>  > -                Thread.sleep(1000);
>  > -            } catch(Exception e) {
>  > -
>  > +        // check to see if the mbean name already exists
>  > +        ArrayList<String> mbeanNames = getMBeanNames();
>  > +        for(int i = 0 ; i < (int)mbeanNames.size(); i++) {
>  > +            if(mbeanNames.get(i).equals(mbeanName)) {
>  > +                return false;   // nothing needs to be done if it is already there
>  >              }
>  > -            // save the document
>  > -            saveDocument(doc, pathToXML);
>  > -            return true;
>  >          }
>  > +
>  > +        // insert the mbean name into the SnapshotConfig object
>  > +        SnapshotConfig sc = getSnapshotConfig();
>  > +        sc.getMbeans().getMbean().add(mbeanName);
>  > +        // write the object to XML
>  > +        saveDocument(sc);
>  > +        return true;
>  >      }
>  >
>  >      /**
>  > @@ -135,7 +132,9 @@
>  >       * @param duration
>  >       */
>  >      public static void saveDuration(long duration) {
>  > -        saveAttribute(DURATION, duration);
>  > +        SnapshotConfig sc = getSnapshotConfig();
>  > +        sc.setDuration("" + duration);
>  > +        saveDocument(sc);
>  >      }
>  >
>  >      /**
>  > @@ -143,47 +142,11 @@
>  >       * @param retention
>  >       */
>  >      public static void saveRetention(int retention) {
>  > -        saveAttribute(RETENTION, retention);
>  > +        SnapshotConfig sc = getSnapshotConfig();
>  > +        sc.setRetention("" + retention);
>  > +        saveDocument(sc);
>  >      }
>  > -
>  > -    /**
>  > -     * Saves a generic attribute value into the node with text = attribute name.
>  > -     * Creates one if there is not an instance of one.
>  > -     * @param attrName
>  > -     * @param attributeValue
>  > -     */
>  > -    private static void saveAttribute(String attrName, long attributeValue) {
>  > -        Document doc = openDocument();
>  > -        // get the root node
>  > -        Element rootElement = doc.getDocumentElement();
>  > -        // get all children in the root node (i.e. all config properties)
>  > -        NodeList configNodes = rootElement.getChildNodes();
>  > -        // find the duration node and save it
>  > -        boolean foundNode = false;
>  > -        for(int i = 0; i < configNodes.getLength() && !foundNode; i++) {
>  > -            Node configNode = configNodes.item(i);
>  > -            if(attrName.equals(configNode.getNodeName())) {
>  > -                // found a match
>  > -                configNode.setTextContent(attributeValue + "");
>  > -                foundNode = true;
>  > -            }
>  > -        }
>  > -        // if there was not a duration node, make one
>  > -        if(!foundNode) {
>  > -            Element element = doc.createElement(attrName);
>  > -            element.setTextContent(attributeValue + "");
>  > -            rootElement.appendChild(element);
>  > -        }
>  > -        try {
>  > -            Thread.sleep(1000);
>  > -        } catch(Exception e) {
>  > -
>  > -        }
>  > -        log.info("***saving:  " + attrName + " = " + attributeValue);
>  > -        // save the document to file
>  > -        saveDocument(doc, pathToXML);
>  > -    }
>  > -
>  > +
>  >      /**
>  >       * Returns the value of the configuration attribute, defined by the key
>  >       * @param key
>  > @@ -191,107 +154,39 @@
>  >       * @throws Exception
>  >       */
>  >      public static String getAttributeValue(String key) throws Exception {
>  > -        // ensure that there exists the 'monitor' directory
>  > -        ensureMonitorDir();
>  > -        // get an instance of the document
>  > -        Document doc = openDocument();
>  > -        // get the root element node
>  > -        Element rootElement = doc.getDocumentElement();
>  > -        // get all children in the root node (i.e. all config properties)
>  > -        NodeList configNodes = rootElement.getChildNodes();
>  > -        // find the duration node and save it
>  > -        for(int i = 0; i < configNodes.getLength(); i++) {
>  > -            if(key.equals(configNodes.item(i).getNodeName())) {
>  > -                return configNodes.item(i).getTextContent();
>  > -            }
>  > +        if(key.equals( DURATION )) {
>  > +            return getSnapshotConfig().getDuration();
>  > +        } else if(key.equals( RETENTION )) {
>  > +            return getSnapshotConfig().getRetention();
>  > +        } else {
>  > +            // Houston, we have a problem
>  > +            throw new Exception("[WARNING] Attribute: " + key + " is not valid.");
>  >          }
>  > -        throw new Exception("[WARNING] " + key + " is not found in " + SNAPSHOT_CONFIG);
>  >      }
>  >
>  >      /**
>  >       * Ensures that there is an existing XML file. Creates one if there
>  >       * does not exist one already.
>  >       */
>  > -    public static void checkXMLExists() {
>  > +    public static boolean checkXMLExists() {
>  > +        ensureMonitorDir();
>  >          File docFile = new File(pathToXML);
>  > -        // create an XML document if it does not exist
>  > -        if(!docFile.exists()) {
>  > -            Document doc = setUpDocument( createDocument() );
>  > -            saveDocument(doc, pathToXML);
>  > -        }
>  > +        return docFile.exists();
>  >      }
>  >
>  > -    /**
>  > -     * Prepares the root element for a document.
>  > -     */
>  > -    public static Document setUpDocument(Document document) {
>  > -        // add <snapshot-config> tag as the root
>  > -        Element rootElement = document.createElement("snapshot-config");
>  > -        document.appendChild(rootElement);
>  > -        return document;
>  > -    }
>  > -
>  > -    /**
>  > -     * Creates an instance of a Document and returns it
>  > -     */
>  > -    public static Document createDocument() {
>  > -        // get an instance of factory
>  > -        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
>  > -        try {
>  > -            // get an instance of builder
>  > -            DocumentBuilder db = dbf.newDocumentBuilder();
>  > -            // create an instance of DOM
>  > -            return db.newDocument();
>  > -        } catch(ParserConfigurationException pce) {
>  > -            log.error("Error while trying to instantiate DocumentBuilder", pce);
>  > -        }
>  > -        return null;
>  > -    }
>  >
>  >      /**
>  > -     * Write the document object to the file location specified by
>  > -     * the path.
>  > +     * Write the XML document.
>  >       */
>  > -    public static void saveDocument(Document document, String path) {
>  > +    public static void saveDocument(SnapshotConfig sc) {
>  >          try {
>  > -            // before saving, make sure the directory is present
>  > -            ensureMonitorDir();
>  > -
>  > -            //TODO GERONIMO-3719.  Hack to use xmlbeans to write out xml instead of sun specific classes.
>  > -            XmlObject xmlObject = XmlObject.Factory.parse(document.getDocumentElement());
>  > -            xmlObject.save(new File(path));
>  > -
>  > -            // formatting the doc
>  > -            // generate a file output
>  > +            Marshaller m = jc.createMarshaller();
>  > +            m.marshal(sc, new FileOutputStream( pathToXML ));
>  >          } catch(Exception e) {
>  > -            log.error(e.getMessage(), e);
>  > +            log.error(e.getMessage());
>  >          }
>  >      }
>  >
>  > -    /**
>  > -     * Parses the XML document specified by the private member 'pathToXML'
>  > -     * and stores the information in the a Document object
>  > -     */
>  > -    public static Document openDocument() {
>  > -        // ensure that the XML file is there
>  > -        checkXMLExists();
>  > -        // get the factory
>  > -        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
>  > -        // continue to attempt to parse
>  > -        while(true) {
>  > -            try {
>  > -                // Using factory get an instance of document builder
>  > -                DocumentBuilder db = dbf.newDocumentBuilder();
>  > -                // parse using builder to get DOM representation of the XML file
>  > -                Document doc = db.parse(pathToXML);
>  > -                return doc;
>  > -            } catch(Exception e) {
>  > -                // Either this file is being read/written to by snapshot thread
>  > -                // or there is an UNKNOWN error
>  > -                log.error(e.getMessage(), e);
>  > -            }
>  > -        }
>  > -    }
>  >
>  >      /**
>  >       * Checks to see if the GERONIMO_HOME/var/monitoring/ directory was made.
>  >
>  > Added: geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd
>  > URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd?rev=639303&view=auto
>  > ==============================================================================
>  > --- geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd (added)
>  > +++ geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd Thu Mar 20 07:56:56 2008
>  > @@ -0,0 +1,44 @@
>  > +<?xml version="1.0" encoding="UTF-8"?>
>  > +<!--
>  > +    Licensed to the Apache Software Foundation (ASF) under one
>  > +    or more contributor license agreements.  See the NOTICE file
>  > +    distributed with this work for additional information
>  > +    regarding copyright ownership.  The ASF licenses this file
>  > +    to you under the Apache License, Version 2.0 (the
>  > +    "License"); you may not use this file except in compliance
>  > +    with the License.  You may obtain a copy of the License at
>  > +
>  > +     http://www.apache.org/licenses/LICENSE-2.0
>  > +
>  > +    Unless required by applicable law or agreed to in writing,
>  > +    software distributed under the License is distributed on an
>  > +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
>  > +    KIND, either express or implied.  See the License for the
>  > +    specific language governing permissions and limitations
>  > +    under the License.
>  > +-->
>  > +<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>  > +            targetNamespace="http://geronimo.apache.org"
>  > +            xmlns:tns="http://geronimo.apache.org">
>  > +
>  > +    <xsd:element name="snapshot-config">
>  > +        <xsd:complexType>
>  > +            <xsd:sequence>
>  > +                <xsd:element name="duration" type="xsd:string"
>  > +                             minOccurs="1"  maxOccurs="1"/>
>  > +                <xsd:element name="retention" type="xsd:string"
>  > +                             minOccurs="1"  maxOccurs="1"/>
>  > +                <xsd:element name="mbeans">
>  > +                    <xsd:complexType>
>  > +                        <xsd:sequence>
>  > +                            <xsd:element name="mbean" type="xsd:string"
>  > +                                         minOccurs="0" maxOccurs="unbounded"/>
>  > +                        </xsd:sequence>
>  > +                    </xsd:complexType>
>  > +                </xsd:element>
>  > +            </xsd:sequence>
>  > +        </xsd:complexType>
>  > +
>  > +    </xsd:element>
>  > +
>  > +</xsd:schema>
>  >
>  > Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd
>  > ------------------------------------------------------------------------------
>  >     svn:eol-style = native
>  >
>  > Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd
>  > ------------------------------------------------------------------------------
>  >     svn:keywords = Date Revision
>  >
>  > Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd
>  > ------------------------------------------------------------------------------
>  >     svn:mime-type = text/xml
>  >
>  >
>  >
>

Re: svn commit: r639303 - in /geronimo/server/trunk/plugins/monitoring/agent-jar: ./ src/main/java/org/apache/geronimo/monitoring/snapshot/ src/xsd/

Posted by Donald Woods <dw...@apache.org>.

viet@apache.org wrote:
> Author: viet
> Date: Thu Mar 20 07:56:56 2008
> New Revision: 639303
> 
> URL: http://svn.apache.org/viewvc?rev=639303&view=rev
> Log:
> Fix for Geronimo-3925. Uses JAXB to manipulate XML.
> 
> Added:
>     geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java   (with props)
>     geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java   (with props)
>     geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/
>     geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd   (with props)
> Modified:
>     geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml
>     geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java
> 
> Modified: geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml
> URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml?rev=639303&r1=639302&r2=639303&view=diff
> ==============================================================================
> --- geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml (original)
> +++ geronimo/server/trunk/plugins/monitoring/agent-jar/pom.xml Thu Mar 20 07:56:56 2008
> @@ -53,6 +53,18 @@
>              <artifactId>geronimo-j2ee-management_1.1_spec</artifactId>
>              <scope>provided</scope>
>          </dependency>
> +
> +        <dependency>
> +            <groupId>javax.xml.bind</groupId>
> +            <artifactId>jaxb-api</artifactId>
> +            <version>2.0</version>

Can you remove the explicit <version> and use the one set by 
trunk/server/pom.xml?


> +            <exclusions>
> +                <exclusion>
> +                    <groupId>javax.xml.bind</groupId>
> +                    <artifactId>jsr173_api</artifactId>
> +                </exclusion>
> +            </exclusions>
> +        </dependency>
>      </dependencies>
>  
>  </project>
> 
> Added: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java
> URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java?rev=639303&view=auto
> ==============================================================================
> --- geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java (added)
> +++ geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java Thu Mar 20 07:56:56 2008
> @@ -0,0 +1,57 @@
> +//
> +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0-b26-ea3 
> +// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
> +// Any modifications to this file will be lost upon recompilation of the source schema. 
> +// Generated on: 2008.03.18 at 12:52:02 AM GMT-05:00 
> +//
> +
> +
> +package org.apache.geronimo.monitoring.snapshot;
> +
> +import javax.xml.bind.annotation.XmlRegistry;
> +import org.apache.geronimo.monitoring.snapshot.SnapshotConfig;
> +import org.apache.geronimo.monitoring.snapshot.SnapshotConfig.Mbeans;
> +
> +
> +/**
> + * This object contains factory methods for each 
> + * Java content interface and Java element interface 
> + * generated in the org.apache.geronimo package. 
> + * <p>An ObjectFactory allows you to programatically 
> + * construct new instances of the Java representation 
> + * for XML content. The Java representation of XML 
> + * content can consist of schema derived interfaces 
> + * and classes representing the binding of schema 
> + * type definitions, element declarations and model 
> + * groups.  Factory methods for each of these are 
> + * provided in this class.
> + * 
> + */
> +@XmlRegistry
> +public class ObjectFactory {
> +
> +
> +    /**
> +     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.apache.geronimo
> +     * 
> +     */
> +    public ObjectFactory() {
> +    }
> +
> +    /**
> +     * Create an instance of {@link Mbeans }
> +     * 
> +     */
> +    public Mbeans createSnapshotConfigMbeans() {
> +        return new Mbeans();
> +    }
> +
> +    /**
> +     * Create an instance of {@link SnapshotConfig }
> +     * 
> +     */
> +    public SnapshotConfig createSnapshotConfig() {
> +        return new SnapshotConfig();
> +    }
> +
> +}
> 
> Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java
> ------------------------------------------------------------------------------
>     svn:eol-style = native
> 
> Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java
> ------------------------------------------------------------------------------
>     svn:keywords = Date Revision
> 
> Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/ObjectFactory.java
> ------------------------------------------------------------------------------
>     svn:mime-type = text/plain
> 
> Added: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java
> URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java?rev=639303&view=auto
> ==============================================================================
> --- geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java (added)
> +++ geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java Thu Mar 20 07:56:56 2008
> @@ -0,0 +1,196 @@
> +//
> +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0-b26-ea3 
> +// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
> +// Any modifications to this file will be lost upon recompilation of the source schema. 
> +// Generated on: 2008.03.18 at 12:52:02 AM GMT-05:00 
> +//
> +
> +
> +package org.apache.geronimo.monitoring.snapshot;
> +
> +import java.util.ArrayList;
> +import java.util.List;
> +import javax.xml.bind.annotation.XmlAccessType;
> +import javax.xml.bind.annotation.XmlAccessorType;
> +import javax.xml.bind.annotation.XmlRootElement;
> +import javax.xml.bind.annotation.XmlType;
> +
> +
> +/**
> + * <p>Java class for snapshot-config element declaration.
> + * 
> + * <p>The following schema fragment specifies the expected content contained within this class.
> + * 
> + * <pre>
> + * &lt;element name="snapshot-config">
> + *   &lt;complexType>
> + *     &lt;complexContent>
> + *       &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
> + *         &lt;sequence>
> + *           &lt;element name="duration" type="{http://www.w3.org/2001/XMLSchema}string"/>
> + *           &lt;element name="retention" type="{http://www.w3.org/2001/XMLSchema}string"/>
> + *           &lt;element name="mbeans">
> + *             &lt;complexType>
> + *               &lt;complexContent>
> + *                 &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
> + *                   &lt;sequence>
> + *                     &lt;element name="mbean" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/>
> + *                   &lt;/sequence>
> + *                 &lt;/restriction>
> + *               &lt;/complexContent>
> + *             &lt;/complexType>
> + *           &lt;/element>
> + *         &lt;/sequence>
> + *       &lt;/restriction>
> + *     &lt;/complexContent>
> + *   &lt;/complexType>
> + * &lt;/element>
> + * </pre>
> + * 
> + * 
> + */
> +@XmlAccessorType(XmlAccessType.FIELD)
> +@XmlType(name = "", propOrder = {
> +    "duration",
> +    "retention",
> +    "mbeans"
> +})
> +@XmlRootElement(name = "snapshot-config")
> +public class SnapshotConfig {
> +
> +    protected String duration;
> +    protected String retention;
> +    protected Mbeans mbeans;
> +
> +    /**
> +     * Gets the value of the duration property.
> +     * 
> +     * @return
> +     *     possible object is
> +     *     {@link String }
> +     *     
> +     */
> +    public String getDuration() {
> +        return duration;
> +    }
> +
> +    /**
> +     * Sets the value of the duration property.
> +     * 
> +     * @param value
> +     *     allowed object is
> +     *     {@link String }
> +     *     
> +     */
> +    public void setDuration(String value) {
> +        this.duration = value;
> +    }
> +
> +    /**
> +     * Gets the value of the retention property.
> +     * 
> +     * @return
> +     *     possible object is
> +     *     {@link String }
> +     *     
> +     */
> +    public String getRetention() {
> +        return retention;
> +    }
> +
> +    /**
> +     * Sets the value of the retention property.
> +     * 
> +     * @param value
> +     *     allowed object is
> +     *     {@link String }
> +     *     
> +     */
> +    public void setRetention(String value) {
> +        this.retention = value;
> +    }
> +
> +    /**
> +     * Gets the value of the mbeans property.
> +     * 
> +     * @return
> +     *     possible object is
> +     *     {@link Mbeans }
> +     *     
> +     */
> +    public Mbeans getMbeans() {
> +        return mbeans;
> +    }
> +
> +    /**
> +     * Sets the value of the mbeans property.
> +     * 
> +     * @param value
> +     *     allowed object is
> +     *     {@link Mbeans }
> +     *     
> +     */
> +    public void setMbeans(Mbeans value) {
> +        this.mbeans = value;
> +    }
> +
> +
> +    /**
> +     * <p>Java class for anonymous complex type.
> +     * 
> +     * <p>The following schema fragment specifies the expected content contained within this class.
> +     * 
> +     * <pre>
> +     * &lt;complexType>
> +     *   &lt;complexContent>
> +     *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
> +     *       &lt;sequence>
> +     *         &lt;element name="mbean" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/>
> +     *       &lt;/sequence>
> +     *     &lt;/restriction>
> +     *   &lt;/complexContent>
> +     * &lt;/complexType>
> +     * </pre>
> +     * 
> +     * 
> +     */
> +    @XmlAccessorType(XmlAccessType.FIELD)
> +    @XmlType(name = "", propOrder = {
> +        "mbean"
> +    })
> +    public static class Mbeans {
> +
> +        protected List<String> mbean;
> +
> +        /**
> +         * Gets the value of the mbean property.
> +         * 
> +         * <p>
> +         * This accessor method returns a reference to the live list,
> +         * not a snapshot. Therefore any modification you make to the
> +         * returned list will be present inside the JAXB object.
> +         * This is why there is not a <CODE>set</CODE> method for the mbean property.
> +         * 
> +         * <p>
> +         * For example, to add a new item, do as follows:
> +         * <pre>
> +         *    getMbean().add(newItem);
> +         * </pre>
> +         * 
> +         * 
> +         * <p>
> +         * Objects of the following type(s) are allowed in the list
> +         * {@link String }
> +         * 
> +         * 
> +         */
> +        public List<String> getMbean() {
> +            if (mbean == null) {
> +                mbean = new ArrayList<String>();
> +            }
> +            return this.mbean;
> +        }
> +
> +    }
> +
> +}
> 
> Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java
> ------------------------------------------------------------------------------
>     svn:eol-style = native
> 
> Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java
> ------------------------------------------------------------------------------
>     svn:keywords = Date Revision
> 
> Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfig.java
> ------------------------------------------------------------------------------
>     svn:mime-type = text/plain
> 
> Modified: geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java
> URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java?rev=639303&r1=639302&r2=639303&view=diff
> ==============================================================================
> --- geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java (original)
> +++ geronimo/server/trunk/plugins/monitoring/agent-jar/src/main/java/org/apache/geronimo/monitoring/snapshot/SnapshotConfigXMLBuilder.java Thu Mar 20 07:56:56 2008
> @@ -20,18 +20,12 @@
>  import java.io.FileOutputStream;
>  import java.util.ArrayList;
>  
> -import javax.xml.parsers.DocumentBuilder;
> -import javax.xml.parsers.DocumentBuilderFactory;
> -import javax.xml.parsers.ParserConfigurationException;
> +import javax.xml.bind.JAXBContext;
> +import javax.xml.bind.Marshaller;
> +import javax.xml.bind.Unmarshaller;
>  
>  import org.apache.commons.logging.Log;
>  import org.apache.commons.logging.LogFactory;
> -import org.apache.xmlbeans.XmlCursor;
> -import org.apache.xmlbeans.XmlObject;
> -import org.w3c.dom.Document;
> -import org.w3c.dom.Element;
> -import org.w3c.dom.Node;
> -import org.w3c.dom.NodeList;
>  
>  /**
>   * In charge of dealing with the XML processing of the snapshot's data.
> @@ -46,88 +40,91 @@
>      private static final String DURATION = "duration";
>      private static final String RETENTION = "retention";
>      private static final String MBEAN = "mbean"; 
> +
> +    private static JAXBContext jc = null;
> +
> +    static {
> +        try {
> +            jc = JAXBContext.newInstance("org.apache.geronimo.monitoring.snapshot");
> +        } catch(Exception e) {
> +            log.error(e.getMessage());
> +        }
> +    }
> +
> +    /**
> +     * @return SnapshotConfig object which represents the XML document
> +     */
> +    private static SnapshotConfig getSnapshotConfig() {
> +        // ensure that there is a snapshot-config.xml at all times
> +        try {
> +            if(checkXMLExists()) {
> +                // unmarshall the xml document into SnapshotConfig object
> +                Unmarshaller m = jc.createUnmarshaller();
> +                return (SnapshotConfig)m.unmarshal( new File(pathToXML) );
> +            } else {
> +                SnapshotConfig sc = new SnapshotConfig();
> +                sc.setMbeans( new SnapshotConfig.Mbeans() );
> +                saveDocument(sc);
> +                return sc;
> +            }
> +        } catch(Exception e) {
> +            log.error(e.getMessage());
> +            return null;    // in the case of an error, return null
> +        }
> +    }
>      
>      /**
>       * @return A list of all mbean names that have been previously saved.
>       * These mbean names are those to keep track of for per snapshot.
>       */
>      public static ArrayList<String> getMBeanNames() {
> -        ArrayList<String> mbeanList = new ArrayList<String>();
> -        // get an instance of the document
> -        Document doc = openDocument();
> -        // get the root element node
> -        Element rootElement = doc.getDocumentElement();
> -        // get all children in the root node (i.e. all config properties)
> -        NodeList configNodes = rootElement.getChildNodes();
> -        // find the duration node and save it
> -        for(int i = 0; i < configNodes.getLength(); i++) {
> -            if(MBEAN.equals(configNodes.item(i).getNodeName())) {
> -                mbeanList.add( configNodes.item(i).getTextContent() );
> -            }
> +        if(getSnapshotConfig().getMbeans() == null) {
> +            return new ArrayList<String>();
> +        } else {
> +            return (ArrayList<String>)getSnapshotConfig().getMbeans().getMbean();
>          }
> -        return mbeanList;
>      }
>      
>      /**
> -     * Adds to the snapshot-config.xml another configuration element <mbean>
> +     * Removes from the snapshot-config.xml a configuration element <mbean>
>       * in order to persistently keep track of all user requested statistics.
> -     * If there is a duplicate, nothing will be done.
> +     * If there does not exist an instance of the mbeanNAme, nothing will be done.
>       */
>      public static boolean removeMBeanName(String mbeanName) {
> -        ArrayList<String> mbeanList = getMBeanNames();
> -        // operate on the snapshot-config.xml if there exists the mbean name
> -        if(mbeanList.contains(mbeanName)) {
> -            // get an instance of the document
> -            Document doc = openDocument();
> -            // get the root element node
> -            Element rootElement = doc.getDocumentElement();
> -            // find the Node that represents the mbeanName
> -            NodeList list = rootElement.getChildNodes();
> -            for(int i = 0; i < list.getLength(); i++) {
> -                // check the Node's text context for a match with mbeanName
> -                if(list.item(i).getTextContent().equals(mbeanName)) {
> -                    // remove the node from rootElement
> -                    Node toRemoveNode = list.item(i);
> -                    rootElement.removeChild(toRemoveNode);
> -                    break;
> -                }
> +        ArrayList<String> mbeanNames = getMBeanNames();
> +        for(int i = 0 ; i < (int)mbeanNames.size(); i++) {
> +            if(mbeanNames.get(i).equals(mbeanName)) {
> +                // remove the mbean name by directly accessing it, because it is by reference
> +                SnapshotConfig sc = getSnapshotConfig();
> +                sc.getMbeans().getMbean().remove(i);
> +                // save the current state of the SnapshotConfig object
> +                saveDocument( sc );
> +                return true;
>              }
> -            // save the document
> -            saveDocument(doc, pathToXML);
> -            return true;
> -        } else {
> -            return false;
>          }
> +        return false;
>      }
>  
>      /**
> -     * Removes from the snapshot-config.xml a configuration element <mbean>
> +     * Adds to the snapshot-config.xml another configuration element <mbean>
>       * in order to persistently keep track of all user requested statistics.
> -     * If there does not exist an instance of the mbeanNAme, nothing will be done.
> +     * If there is a duplicate, nothing will be done.
>       */
>      public static boolean addMBeanName(String mbeanName) {
> -        ArrayList<String> mbeanList = getMBeanNames();
> -        if(mbeanList.contains(mbeanName)) {
> -            return false;
> -        } else {
> -            // get an instance of the document
> -            Document doc = openDocument();
> -            // get the root element node
> -            Element rootElement = doc.getDocumentElement();
> -            // create <mbean> element
> -            Element mbeanElement = doc.createElement(MBEAN);
> -            mbeanElement.setTextContent(mbeanName);
> -            // add <mbean> element to the rootElement
> -            rootElement.appendChild(mbeanElement);
> -            try {
> -                Thread.sleep(1000);
> -            } catch(Exception e) {
> -                
> +        // check to see if the mbean name already exists
> +        ArrayList<String> mbeanNames = getMBeanNames();
> +        for(int i = 0 ; i < (int)mbeanNames.size(); i++) {
> +            if(mbeanNames.get(i).equals(mbeanName)) {
> +                return false;   // nothing needs to be done if it is already there
>              }
> -            // save the document
> -            saveDocument(doc, pathToXML);
> -            return true;
>          }
> +
> +        // insert the mbean name into the SnapshotConfig object
> +        SnapshotConfig sc = getSnapshotConfig();
> +        sc.getMbeans().getMbean().add(mbeanName);
> +        // write the object to XML
> +        saveDocument(sc);
> +        return true;
>      }
>      
>      /**
> @@ -135,7 +132,9 @@
>       * @param duration
>       */
>      public static void saveDuration(long duration) {
> -        saveAttribute(DURATION, duration);
> +        SnapshotConfig sc = getSnapshotConfig();
> +        sc.setDuration("" + duration);
> +        saveDocument(sc);
>      }
>  
>      /**
> @@ -143,47 +142,11 @@
>       * @param retention
>       */
>      public static void saveRetention(int retention) {
> -        saveAttribute(RETENTION, retention);
> +        SnapshotConfig sc = getSnapshotConfig();
> +        sc.setRetention("" + retention);
> +        saveDocument(sc);
>      }
> -    
> -    /**
> -     * Saves a generic attribute value into the node with text = attribute name.
> -     * Creates one if there is not an instance of one.
> -     * @param attrName
> -     * @param attributeValue
> -     */
> -    private static void saveAttribute(String attrName, long attributeValue) {
> -        Document doc = openDocument();
> -        // get the root node        
> -        Element rootElement = doc.getDocumentElement();
> -        // get all children in the root node (i.e. all config properties)
> -        NodeList configNodes = rootElement.getChildNodes();
> -        // find the duration node and save it
> -        boolean foundNode = false;
> -        for(int i = 0; i < configNodes.getLength() && !foundNode; i++) {
> -            Node configNode = configNodes.item(i);
> -            if(attrName.equals(configNode.getNodeName())) {
> -                // found a match
> -                configNode.setTextContent(attributeValue + "");
> -                foundNode = true;
> -            }
> -        }
> -        // if there was not a duration node, make one
> -        if(!foundNode) {
> -            Element element = doc.createElement(attrName);
> -            element.setTextContent(attributeValue + "");
> -            rootElement.appendChild(element);
> -        }
> -        try {
> -            Thread.sleep(1000);
> -        } catch(Exception e) {
> -            
> -        }
> -        log.info("***saving:  " + attrName + " = " + attributeValue);
> -        // save the document to file
> -        saveDocument(doc, pathToXML);
> -    }
> -    
> +       
>      /**
>       * Returns the value of the configuration attribute, defined by the key
>       * @param key
> @@ -191,107 +154,39 @@
>       * @throws Exception
>       */
>      public static String getAttributeValue(String key) throws Exception {
> -        // ensure that there exists the 'monitor' directory
> -        ensureMonitorDir();
> -        // get an instance of the document
> -        Document doc = openDocument();
> -        // get the root element node
> -        Element rootElement = doc.getDocumentElement();
> -        // get all children in the root node (i.e. all config properties)
> -        NodeList configNodes = rootElement.getChildNodes();
> -        // find the duration node and save it
> -        for(int i = 0; i < configNodes.getLength(); i++) {
> -            if(key.equals(configNodes.item(i).getNodeName())) {
> -                return configNodes.item(i).getTextContent();
> -            }
> +        if(key.equals( DURATION )) {
> +            return getSnapshotConfig().getDuration();
> +        } else if(key.equals( RETENTION )) {
> +            return getSnapshotConfig().getRetention();
> +        } else {
> +            // Houston, we have a problem
> +            throw new Exception("[WARNING] Attribute: " + key + " is not valid.");
>          }
> -        throw new Exception("[WARNING] " + key + " is not found in " + SNAPSHOT_CONFIG);
>      }
>      
>      /**
>       * Ensures that there is an existing XML file. Creates one if there
>       * does not exist one already. 
>       */
> -    public static void checkXMLExists() {
> +    public static boolean checkXMLExists() {
> +        ensureMonitorDir();
>          File docFile = new File(pathToXML);
> -        // create an XML document if it does not exist
> -        if(!docFile.exists()) {
> -            Document doc = setUpDocument( createDocument() );
> -            saveDocument(doc, pathToXML);
> -        }
> +        return docFile.exists();
>      }
>      
> -    /**
> -     * Prepares the root element for a document.
> -     */
> -    public static Document setUpDocument(Document document) {
> -        // add <snapshot-config> tag as the root
> -        Element rootElement = document.createElement("snapshot-config");
> -        document.appendChild(rootElement);
> -        return document;
> -    }
> -    
> -    /**
> -     * Creates an instance of a Document and returns it
> -     */
> -    public static Document createDocument() {
> -        // get an instance of factory
> -        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
> -        try {
> -            // get an instance of builder
> -            DocumentBuilder db = dbf.newDocumentBuilder();
> -            // create an instance of DOM
> -            return db.newDocument();
> -        } catch(ParserConfigurationException pce) {
> -            log.error("Error while trying to instantiate DocumentBuilder", pce);
> -        }
> -        return null;
> -    }
>      
>      /**
> -     * Write the document object to the file location specified by
> -     * the path.
> +     * Write the XML document.
>       */
> -    public static void saveDocument(Document document, String path) {
> +    public static void saveDocument(SnapshotConfig sc) {
>          try {
> -            // before saving, make sure the directory is present
> -            ensureMonitorDir();
> -
> -            //TODO GERONIMO-3719.  Hack to use xmlbeans to write out xml instead of sun specific classes.
> -            XmlObject xmlObject = XmlObject.Factory.parse(document.getDocumentElement());
> -            xmlObject.save(new File(path));
> -
> -            // formatting the doc
> -            // generate a file output
> +            Marshaller m = jc.createMarshaller();
> +            m.marshal(sc, new FileOutputStream( pathToXML ));
>          } catch(Exception e) {
> -            log.error(e.getMessage(), e);
> +            log.error(e.getMessage());
>          }
>      }
>  
> -    /**
> -     * Parses the XML document specified by the private member 'pathToXML'
> -     * and stores the information in the a Document object
> -     */
> -    public static Document openDocument() {
> -        // ensure that the XML file is there
> -        checkXMLExists();
> -        // get the factory
> -        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
> -        // continue to attempt to parse
> -        while(true) {
> -            try {
> -                // Using factory get an instance of document builder
> -                DocumentBuilder db = dbf.newDocumentBuilder();
> -                // parse using builder to get DOM representation of the XML file
> -                Document doc = db.parse(pathToXML);
> -                return doc;
> -            } catch(Exception e) {
> -                // Either this file is being read/written to by snapshot thread
> -                // or there is an UNKNOWN error
> -                log.error(e.getMessage(), e);
> -            }
> -        }
> -    }
>      
>      /**
>       * Checks to see if the GERONIMO_HOME/var/monitoring/ directory was made.
> 
> Added: geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd
> URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd?rev=639303&view=auto
> ==============================================================================
> --- geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd (added)
> +++ geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd Thu Mar 20 07:56:56 2008
> @@ -0,0 +1,44 @@
> +<?xml version="1.0" encoding="UTF-8"?>
> +<!--
> +    Licensed to the Apache Software Foundation (ASF) under one
> +    or more contributor license agreements.  See the NOTICE file
> +    distributed with this work for additional information
> +    regarding copyright ownership.  The ASF licenses this file
> +    to you under the Apache License, Version 2.0 (the
> +    "License"); you may not use this file except in compliance
> +    with the License.  You may obtain a copy of the License at
> +    
> +     http://www.apache.org/licenses/LICENSE-2.0
> +    
> +    Unless required by applicable law or agreed to in writing,
> +    software distributed under the License is distributed on an
> +    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> +    KIND, either express or implied.  See the License for the
> +    specific language governing permissions and limitations
> +    under the License.
> +-->
> +<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> +            targetNamespace="http://geronimo.apache.org"
> +            xmlns:tns="http://geronimo.apache.org">
> +
> +    <xsd:element name="snapshot-config">
> +        <xsd:complexType>
> +            <xsd:sequence>
> +                <xsd:element name="duration" type="xsd:string"
> +                             minOccurs="1"  maxOccurs="1"/>
> +                <xsd:element name="retention" type="xsd:string"
> +                             minOccurs="1"  maxOccurs="1"/>
> +                <xsd:element name="mbeans">
> +                    <xsd:complexType>
> +                        <xsd:sequence>
> +                            <xsd:element name="mbean" type="xsd:string" 
> +                                         minOccurs="0" maxOccurs="unbounded"/>
> +                        </xsd:sequence>
> +                    </xsd:complexType>
> +                </xsd:element>
> +            </xsd:sequence>
> +        </xsd:complexType>
> +
> +    </xsd:element>
> +
> +</xsd:schema>
> 
> Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd
> ------------------------------------------------------------------------------
>     svn:eol-style = native
> 
> Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd
> ------------------------------------------------------------------------------
>     svn:keywords = Date Revision
> 
> Propchange: geronimo/server/trunk/plugins/monitoring/agent-jar/src/xsd/SnapshotConfig.xsd
> ------------------------------------------------------------------------------
>     svn:mime-type = text/xml
> 
> 
>