|
From: <sa...@us...> - 2004-02-19 10:58:28
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28339/org/jrobin/core Modified Files: ArcDef.java RrdToolkit.java XmlReader.java Log Message: Added method(s) to RRDToolkit class to create RrdDef object from various XML sources Index: ArcDef.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/ArcDef.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ArcDef.java 8 Dec 2003 13:47:40 -0000 1.4 --- ArcDef.java 19 Feb 2004 10:48:12 -0000 1.5 *************** *** 110,114 **** throw new RrdException("Invalid consolidation function specified: " + consolFun); } ! if(xff < 0.0 || xff >= 1.0) { throw new RrdException("Invalid xff, must be >= 0 and < 1: " + xff); } --- 110,114 ---- throw new RrdException("Invalid consolidation function specified: " + consolFun); } ! if(Double.isNaN(xff) || xff < 0.0 || xff >= 1.0) { throw new RrdException("Invalid xff, must be >= 0 and < 1: " + xff); } Index: RrdToolkit.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdToolkit.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** RrdToolkit.java 8 Dec 2003 13:47:40 -0000 1.6 --- RrdToolkit.java 19 Feb 2004 10:48:12 -0000 1.7 *************** *** 25,30 **** package org.jrobin.core; ! import java.io.IOException; ! import java.io.File; /** * <p>Class used to perform various complex operations on RRD files. Use an instance of the --- 25,35 ---- package org.jrobin.core; ! import org.xml.sax.InputSource; ! import org.xml.sax.SAXException; ! import org.w3c.dom.Element; ! import org.w3c.dom.Node; ! import javax.xml.parsers.FactoryConfigurationError; ! import javax.xml.parsers.ParserConfigurationException; ! import java.io.*; /** * <p>Class used to perform various complex operations on RRD files. Use an instance of the *************** *** 417,420 **** --- 422,600 ---- } + /** + * Creates RrdDef object from its XML string equivalent. The format of the input string is + * the same as the format described in + * {@link #createRrdDefFromXmlFile createRrdDefFromXmlFile} method. + * @param xmlString XML formatted string containing complete RRD definition + * @return RrdDef object which can be used to create new RrdDb object + * @throws RrdException thrown in case of bad XML format or bad RRD definition parameters + * @throws IOException thrown in case of I/O error + */ + public RrdDef createRrdDefFromXmlString(String xmlString) throws RrdException, IOException { + return createRrdDefFromXmlSource(new InputSource(new StringReader(xmlString))); + } + + /** + * Creates RrdDef object from the file containing its XML equivalent. Here is an example + * of a properly formatted XML file:</p> + * <pre><code> + * <rrd_def> + * <path>test.rrd</path> + * <!-- not mandatory --> + * <start>1000123123</start> + * <!-- not mandatory --> + * <step>150</step> + * <!-- at least one datasource must be supplied --> + * <datasource> + * <name>input</name> + * <type>COUNTER</type> + * <heartbeat>300</heartbeat> + * <min>0</min> + * <max>U</max> + * </datasource> + * <datasource> + * <name>temperature</name> + * <type>GAUGE</type> + * <heartbeat>400</heartbeat> + * <min>U</min> + * <max>1000</max> + * </datasource> + * <!-- at least one archive must be supplied --> + * <archive> + * <cf>AVERAGE</cf> + * <xff>0.5</xff> + * <steps>1</steps> + * <rows>700</rows> + * </archive> + * <archive> + * <cf>MAX</cf> + * <xff>0.6</xff> + * <steps>6</steps> + * <rows>7000</rows> + * </archive> + * </rrd_def> + * </code></pre> + * + * @param filepath path containing XML formatted RRD definition (like the one given above) + * @return RrdDef object which can be used to create new RrdDb object + * @throws RrdException thrown in case of bad XML format or bad RRD definition parameters + * @throws IOException thrown in case of I/O error + */ + public RrdDef createRrdDefFromXmlFile(String filepath) throws RrdException, IOException { + FileReader fileReader = null; + try { + fileReader = new FileReader(filepath); + return createRrdDefFromXmlSource(new InputSource(fileReader)); + } + finally { + if(fileReader != null) { + fileReader.close(); + } + } + } + + /** + * Creates RrdDef object from any parsable XML source. The format of the underlying input + * source data must conform to the format described for the + * {@link #createRrdDefFromXmlFile createRrdDefFromXmlFile} method. + * @param inputSource parsable XML source containing complete RRD definition + * @return RrdDef object which can be used to create new RrdDb object + * @throws RrdException thrown in case of bad XML format or bad RRD definition parameters + * @throws IOException thrown in case of I/O error + */ + public RrdDef createRrdDefFromXmlSource(InputSource inputSource) throws RrdException, IOException { + try { + Element root = XmlReader.getRootElement(inputSource); + // must start with <rrd_def> + if(!root.getTagName().equals("rrd_def")) { + throw new RrdException("XML definition must start with <rrd_def>"); + } + // PATH must be supplied or exception is thrown + String path = XmlReader.getChildValue(root, "path"); + RrdDef rrdDef = new RrdDef(path); + try { + // START is not mandatory + long start = XmlReader.getChildValueAsLong(root, "start"); + rrdDef.setStartTime(start); + } + catch(RrdException e) { } + try { + // STEP is not mandatory + long step = XmlReader.getChildValueAsLong(root, "step"); + rrdDef.setStep(step); + } + catch(RrdException e) { + // NOP + } + // datsources + Node[] dsNodes = XmlReader.getChildNodes(root, "datasource"); + for(int i = 0; i < dsNodes.length; i++) { + String name = XmlReader.getChildValue(dsNodes[i], "name"); + String type = XmlReader.getChildValue(dsNodes[i], "type"); + long heartbeat = XmlReader.getChildValueAsLong(dsNodes[i], "heartbeat"); + double min = XmlReader.getChildValueAsDouble(dsNodes[i], "min"); + double max = XmlReader.getChildValueAsDouble(dsNodes[i], "max"); + rrdDef.addDatasource(name, type, heartbeat, min, max); + } + // archives + Node[] arcNodes = XmlReader.getChildNodes(root, "archive"); + for(int i = 0; i < arcNodes.length; i++) { + String consolFun = XmlReader.getChildValue(arcNodes[i], "cf"); + double xff = XmlReader.getChildValueAsDouble(arcNodes[i], "xff"); + int steps = XmlReader.getChildValueAsInt(arcNodes[i], "steps"); + int rows = XmlReader.getChildValueAsInt(arcNodes[i], "rows"); + rrdDef.addArchive(consolFun, xff, steps, rows); + } + return rrdDef; + } catch (FactoryConfigurationError e) { + throw new RrdException("XML error: " + e); + } catch (ParserConfigurationException e) { + throw new RrdException("XML error: " + e); + } catch (SAXException e) { + throw new RrdException("XML error: " + e); + } catch(NumberFormatException e) { + throw new RrdException("XML error: " + e); + } + } + + /* + public static void main(String[] args) throws RrdException, IOException { + String s = + "<rrd_def> " + + " <path>test.rrd</path> " + + " <start>1000000000</start> " + + " <step>151</step> " + + " <datasource> " + + " <name>input</name> " + + " <type>COUNTER</type> " + + " <heartbeat>300</heartbeat> " + + " <min>10</min> " + + " <max>U</max> " + + " </datasource> " + + " <datasource> " + + " <name>temperature</name> " + + " <type>GAUGE</type> " + + " <heartbeat>400</heartbeat> " + + " <min>U</min> " + + " <max>1000</max> " + + " </datasource> " + + " <archive> " + + " <cf>AVERAGE</cf> " + + " <xff>0.5</xff> " + + " <steps>1</steps> " + + " <rows>700</rows> " + + " </archive> " + + " <archive> " + + " <cf>MAX</cf> " + + " <xff>0.6</xff> " + + " <steps>6</steps> " + + " <rows>7000</rows> " + + " </archive> " + + "</rrd_def> "; + RrdDef def = RrdToolkit.getInstance().createRrdDefFromXmlString(s); + RrdDb db = new RrdDb(def); + db.close(); + } + */ } Index: XmlReader.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/XmlReader.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** XmlReader.java 10 Nov 2003 08:52:27 -0000 1.2 --- XmlReader.java 19 Feb 2004 10:48:12 -0000 1.3 *************** *** 31,38 **** import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import javax.xml.parsers.*; import java.io.IOException; ! import java.io.File; import java.util.ArrayList; --- 31,39 ---- import org.w3c.dom.NodeList; import org.xml.sax.SAXException; + import org.xml.sax.InputSource; import javax.xml.parsers.*; import java.io.IOException; ! import java.io.FileReader; import java.util.ArrayList; *************** *** 43,53 **** XmlReader(String xmlFilePath) throws IOException, RrdException { try { ! DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); ! factory.setValidating(false); ! factory.setNamespaceAware(false); ! DocumentBuilder builder = factory.newDocumentBuilder(); ! Document doc = builder.parse(new File(xmlFilePath)); ! root = doc.getDocumentElement(); dsNodes = getChildNodes(root, "ds"); arcNodes = getChildNodes(root, "rra"); --- 44,52 ---- XmlReader(String xmlFilePath) throws IOException, RrdException { + FileReader fileReader = null; try { ! fileReader = new FileReader(xmlFilePath); ! InputSource source = new InputSource(fileReader); ! root = getRootElement(source); dsNodes = getChildNodes(root, "ds"); arcNodes = getChildNodes(root, "rra"); *************** *** 59,62 **** --- 58,66 ---- throw new RrdException("XML error: " + e); } + finally { + if(fileReader != null) { + fileReader.close(); + } + } } *************** *** 202,204 **** --- 206,218 ---- return Util.parseDouble(valueStr); } + + static Element getRootElement(InputSource inputSource) + throws ParserConfigurationException, IOException, SAXException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setValidating(false); + factory.setNamespaceAware(false); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(inputSource); + return doc.getDocumentElement(); + } } \ No newline at end of file |