java.lang.Object
org.rrd4j.core.XmlTemplate
org.rrd4j.core.RrdDefTemplate
Class used to create an arbitrary number of
RrdDef (RRD definition) objects from a single XML template. XML template can be supplied as an XML InputSource, XML file or XML formatted string.Here is an example of a properly formatted XML template with all available options in it (unwanted options can be removed):
<rrd_def>
<path>test.rrd</path>
<!-- not mandatory -->
<start>1000123456</start>
<!-- not mandatory -->
<step>300</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>600</rows>
</archive>
<archive>
<cf>MAX</cf>
<xff>0.6</xff>
<steps>6</steps>
<rows>7000</rows>
</archive>
</rrd_def>
Notes on the template syntax:
- There is a strong relation between the XML template syntax and the syntax of
RrdDefclass methods. If you are not sure what some XML tag means, check javadoc for the corresponding class. - the path to the rrd can be specified either as a file path using path element or as an uri using a url tag
- starting timestamp can be supplied either as a long integer (like: 1000243567) or as an ISO formatted string (like: 2004-02-21 12:25:45)
- whitespaces are not harmful
- floating point values: anything that cannot be parsed will be treated as Double.NaN (like: U, unknown, 12r.23)
- comments are allowed.
Any template value (text between <some_tag> and </some_tag>
) can be replaced with a variable of the following form: ${variable_name}. Use setVariable() methods from the base class to replace template variables with real values at runtime.
Typical usage scenario:
- Create your XML template and save it to a file (template.xml, for example)
- Replace hardcoded template values with variables if you want to change them during runtime. For example, RRD path should not be hardcoded in the template - you probably want to create many different RRD files from the same XML template. For example, your XML template could start with:
<rrd_def> <path>${path}</path> <step>300</step> ... - In your Java code, create RrdDefTemplate object using your XML template file:
RrdDefTemplate t = new RrdDefTemplate(new File(template.xml));
- Then, specify real values for template variables:
t.setVariable("path", "demo/test.rrd"); - Once all template variables are set, just use the template object to create RrdDef object. This object is actually used to create Rrd4j RRD files:
RrdDef def = t.getRrdDef(); RrdDb rrd = new RrdDb(def); rrd.close();
Field Summary
Fields inherited from class XmlTemplate
rootConstructor Summary
ConstructorsConstructorDescriptionRrdDefTemplate(File xmlFile) Creates RrdDefTemplate object from the file containing XML template.RrdDefTemplate(String xmlString) Creates RrdDefTemplate object from the string containing XML template.RrdDefTemplate(InputSource xmlInputSource) Creates RrdDefTemplate object from any parsable XML input source.Method Summary
All MethodsMethods inherited from class XmlTemplate
clearValues, getChildNodes, getChildNodes, getChildValue, getChildValue, getChildValueAsBoolean, getChildValueAsDouble, getChildValueAsInt, getChildValueAsLong, getFirstChildNode, getValue, getValue, getValueAsBoolean, getValueAsColor, getValueAsDouble, getValueAsInt, getValueAsLong, getVariables, hasChildNode, hasVariables, isEmptyNode, setVariable, setVariable, setVariable, setVariable, setVariable, setVariable, setVariable, setVariable, validateTagsOnlyOnceModifier and TypeMethodDescriptionvoidRemoves all placeholder-value mappings.protected static Node[]getChildNodes(Node parentNode) Find every child node of the given node.protected static Node[]getChildNodes(Node parentNode, String childName) Find the child nodes of the given name.protected StringgetChildValue(Node parentNode, String childName) Read a named child element's text with its placeholders resolved.protected StringgetChildValue(Node parentNode, String childName, boolean trim) Read a named child element's text with its placeholders resolved.protected booleangetChildValueAsBoolean(Node parentNode, String childName) Read a named child element's text as a boolean.protected doublegetChildValueAsDouble(Node parentNode, String childName) Read a named child element's text as a double.protected intgetChildValueAsInt(Node parentNode, String childName) Read a named child element's text as an int.protected longgetChildValueAsLong(Node parentNode, String childName) Read a named child element's text as a long.protected static NodegetFirstChildNode(Node parentNode, String childName) Find the first child node of the given name.protected StringRead a node's own text with its placeholders resolved.protected StringRead a node's own text with its placeholders resolved.protected booleangetValueAsBoolean(Node parentNode) Read a node's own text as a boolean.protected PaintgetValueAsColor(Node parentNode) Read a node's own text as a colour.protected doublegetValueAsDouble(Node parentNode) Read a node's own text as a double.protected intgetValueAsInt(Node parentNode) Read a node's own text as an int.protected longgetValueAsLong(Node parentNode) Read a node's own text as a long.String[]Returns the list of variables that should be set in this template.protected booleanhasChildNode(Node parentNode, String childName) Test whether a node has a child of the given name.booleanSearches the XML template to see if there are variables in there that will need to be set.protected booleanisEmptyNode(Node node) Test whether a node carries no renderable content.voidsetVariable(String name, boolean value) Sets value for a single XML template variable.voidsetVariable(String name, double value) Sets value for a single XML template variable.voidsetVariable(String name, int value) Sets value for a single XML template variable.voidsetVariable(String name, long value) Sets value for a single XML template variable.voidsetVariable(String name, Color value) Sets value for a single XML template variable.voidsetVariable(String name, String value) Sets value for a single XML template variable.voidsetVariable(String name, Calendar value) Sets value for a single XML template variable.voidsetVariable(String name, Date value) Sets value for a single XML template variable.protected voidvalidateTagsOnlyOnce(Node parentNode, String[] allowedChildNames) Reject any child element not named in the allowed list, once per node.
Constructor Details
RrdDefTemplate
Creates RrdDefTemplate object from any parsable XML input source. Read general information for this class to find an example of a properly formatted RrdDef XML source.- Parameters:
xmlInputSource- Xml input source- Throws:
IOException- Thrown in case of I/O errorIllegalArgumentException- Thrown in case of XML related error (parsing error, for example)
RrdDefTemplate
Creates RrdDefTemplate object from the string containing XML template. Read general information for this class to see an example of a properly formatted XML source.- Parameters:
xmlString- String containing XML template- Throws:
IOException- Thrown in case of I/O errorIllegalArgumentException- Thrown in case of XML related error (parsing error, for example)
RrdDefTemplate
Creates RrdDefTemplate object from the file containing XML template. Read general information for this class to see an example of a properly formatted XML source.- Parameters:
xmlFile- File object representing file with XML template- Throws:
IOException- Thrown in case of I/O errorIllegalArgumentException- Thrown in case of XML related error (parsing error, for example)
Method Details
getRrdDef
Returns RrdDef object constructed from the underlying XML template. Before this method is called, values for all non-optional placeholders must be supplied. To specify placeholder values at runtime, use some of the overloadedsetVariable()methods. Once this method returns, all placeholder values are preserved. To remove them all, call inheritedclearValues()method explicitly.- Returns:
- RrdDef object constructed from the underlying XML template, with all placeholders replaced with real values. This object can be passed to the constructor of the new RrdDb object.
- Throws:
IllegalArgumentException- Thrown (in most cases) if the value for some placeholder was not supplied throughsetVariable()method call