org.collectd.mx.MBeanConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jcollectd Show documentation
Show all versions of jcollectd Show documentation
The jcollectd package implements the collectd protocol in Java, making it possible for Java applications to push data into collectd over the wire.
The newest version!
/*
* jcollectd
* Copyright (C) 2009 Hyperic, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License is applicable.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.collectd.mx;
import java.io.File;
import java.io.InputStream;
import java.util.logging.Logger;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.collectd.protocol.TypesDB;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/**
* Convert jcollectd.xml filters to MBeanCollect objects.
*/
public class MBeanConfig {
private static final Logger _log =
Logger.getLogger(MBeanConfig.class.getName());
private XPath _xpath = XPathFactory.newInstance().newXPath();
private String getAttribute(Node node, String name) {
NamedNodeMap attrs = node.getAttributes();
if (attrs == null) {
return null;
}
Node item = attrs.getNamedItem(name);
if (item == null) {
return null;
}
return item.getNodeValue();
}
private NodeList eval(String name, Node node)
throws XPathExpressionException {
return (NodeList)_xpath.evaluate(name, node, XPathConstants.NODESET);
}
private NodeList eval(String name, InputSource is)
throws XPathExpressionException {
return (NodeList)_xpath.evaluate(name, is, XPathConstants.NODESET);
}
public MBeanCollector add(String source) throws Exception {
String name = source + "-jcollectd.xml";
if (new File(source).exists()) {
return add(new InputSource(source));
}
else if (new File(name).exists()) {
return add(new InputSource(name));
}
else {
String[] rs = { name, "etc/" + name, "META-INF/" + name };
for (int i=0; i