2 * collectd/java - org/collectd/java/GenericJMXConfMBean.java
3 * Copyright (C) 2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
22 package org.collectd.java;
24 import java.util.List;
25 import java.util.Iterator;
26 import java.util.ArrayList;
28 import javax.management.MBeanServerConnection;
29 import javax.management.ObjectName;
30 import javax.management.MalformedObjectNameException;
33 import org.collectd.api.Collectd;
34 import org.collectd.api.PluginData;
35 import org.collectd.api.OConfigValue;
36 import org.collectd.api.OConfigItem;
38 class GenericJMXConfMBean
40 private String _name; /* name by which this mapping is referenced */
41 private ObjectName _obj_name;
42 private String _instance;
43 private List<GenericJMXConfValue> _values;
45 private String getConfigString (OConfigItem ci) /* {{{ */
47 List<OConfigValue> values;
50 values = ci.getValues ();
51 if (values.size () != 1)
53 Collectd.logError ("GenericJMXConfMBean: The " + ci.getKey ()
54 + " configuration option needs exactly one string argument.");
59 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
61 Collectd.logError ("GenericJMXConfMBean: The " + ci.getKey ()
62 + " configuration option needs exactly one string argument.");
66 return (v.getString ());
67 } /* }}} String getConfigString */
70 * <MBean "alias name">
72 * ObjectName "object name"
78 public GenericJMXConfMBean (OConfigItem ci) /* {{{ */
79 throws IllegalArgumentException
81 List<OConfigItem> children;
82 Iterator<OConfigItem> iter;
84 this._name = getConfigString (ci);
85 if (this._name == null)
86 throw (new IllegalArgumentException ("No alias name was defined. "
87 + "MBean blocks need exactly one string argument."));
89 this._obj_name = null;
90 this._values = new ArrayList<GenericJMXConfValue> ();
92 children = ci.getChildren ();
93 iter = children.iterator ();
94 while (iter.hasNext ())
96 OConfigItem child = iter.next ();
98 Collectd.logDebug ("GenericJMXConfMBean: child.getKey () = "
100 if (child.getKey ().equalsIgnoreCase ("Instance"))
102 String tmp = getConfigString (child);
104 this._instance = tmp;
106 else if (child.getKey ().equalsIgnoreCase ("ObjectName"))
108 String tmp = getConfigString (child);
114 this._obj_name = new ObjectName (tmp);
116 catch (MalformedObjectNameException e)
118 throw (new IllegalArgumentException ("Not a valid object name: "
122 else if (child.getKey ().equalsIgnoreCase ("Value"))
124 GenericJMXConfValue cv;
126 cv = new GenericJMXConfValue (child);
127 this._values.add (cv);
130 throw (new IllegalArgumentException ("Unknown option: "
134 if (this._obj_name == null)
135 throw (new IllegalArgumentException ("No object name was defined."));
137 if (this._values.size () == 0)
138 throw (new IllegalArgumentException ("No value block was defined."));
140 } /* }}} GenericJMXConfMBean (OConfigItem ci) */
142 public String getName ()
147 public void query (MBeanServerConnection conn, PluginData pd) /* {{{ */
149 pd.setPluginInstance ((this._instance != null) ? this._instance : "");
151 for (int i = 0; i < this._values.size (); i++)
152 this._values.get (i).query (conn, this._obj_name, pd);
153 } /* }}} void query */
156 /* vim: set sw=2 sts=2 et fdm=marker : */