2 * collectd - bindings/java/org/collectd/java/GenericJMXConfMBean.java
3 * Copyright (C) 2009,2010 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
27 package org.collectd.java;
29 import java.util.Iterator;
30 import java.util.List;
32 import java.util.ArrayList;
34 import javax.management.MBeanServerConnection;
35 import javax.management.ObjectName;
36 import javax.management.MalformedObjectNameException;
38 import org.collectd.api.Collectd;
39 import org.collectd.api.PluginData;
40 import org.collectd.api.OConfigValue;
41 import org.collectd.api.OConfigItem;
43 class GenericJMXConfMBean
45 private String _name; /* name by which this mapping is referenced */
46 private ObjectName _obj_name;
47 private String _instance_prefix;
48 private List<String> _instance_from;
49 private List<GenericJMXConfValue> _values;
51 private String getConfigString (OConfigItem ci) /* {{{ */
53 List<OConfigValue> values;
56 values = ci.getValues ();
57 if (values.size () != 1)
59 Collectd.logError ("GenericJMXConfMBean: The " + ci.getKey ()
60 + " configuration option needs exactly one string argument.");
65 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
67 Collectd.logError ("GenericJMXConfMBean: The " + ci.getKey ()
68 + " configuration option needs exactly one string argument.");
72 return (v.getString ());
73 } /* }}} String getConfigString */
76 * <MBean "alias name">
77 * ObjectName "object name"
78 * InstancePrefix "foobar"
85 public GenericJMXConfMBean (OConfigItem ci) /* {{{ */
86 throws IllegalArgumentException
88 List<OConfigItem> children;
89 Iterator<OConfigItem> iter;
91 this._name = getConfigString (ci);
92 if (this._name == null)
93 throw (new IllegalArgumentException ("No alias name was defined. "
94 + "MBean blocks need exactly one string argument."));
96 this._obj_name = null;
97 this._instance_prefix = null;
98 this._instance_from = new ArrayList<String> ();
99 this._values = new ArrayList<GenericJMXConfValue> ();
101 children = ci.getChildren ();
102 iter = children.iterator ();
103 while (iter.hasNext ())
105 OConfigItem child = iter.next ();
107 Collectd.logDebug ("GenericJMXConfMBean: child.getKey () = "
109 if (child.getKey ().equalsIgnoreCase ("ObjectName"))
111 String tmp = getConfigString (child);
117 this._obj_name = new ObjectName (tmp);
119 catch (MalformedObjectNameException e)
121 throw (new IllegalArgumentException ("Not a valid object name: "
125 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
127 String tmp = getConfigString (child);
129 this._instance_prefix = tmp;
131 else if (child.getKey ().equalsIgnoreCase ("InstanceFrom"))
133 String tmp = getConfigString (child);
135 this._instance_from.add (tmp);
137 else if (child.getKey ().equalsIgnoreCase ("Value"))
139 GenericJMXConfValue cv;
141 cv = new GenericJMXConfValue (child);
142 this._values.add (cv);
145 throw (new IllegalArgumentException ("Unknown option: "
149 if (this._obj_name == null)
150 throw (new IllegalArgumentException ("No object name was defined."));
152 if (this._values.size () == 0)
153 throw (new IllegalArgumentException ("No value block was defined."));
155 } /* }}} GenericJMXConfMBean (OConfigItem ci) */
157 public String getName () /* {{{ */
162 public int query (MBeanServerConnection conn, PluginData pd, /* {{{ */
163 String instance_prefix)
165 Set<ObjectName> names;
166 Iterator<ObjectName> iter;
170 names = conn.queryNames (this._obj_name, /* query = */ null);
174 Collectd.logError ("GenericJMXConfMBean: queryNames failed: " + e);
178 if (names.size () == 0)
180 Collectd.logWarning ("GenericJMXConfMBean: No MBean matched "
181 + "the ObjectName " + this._obj_name);
184 iter = names.iterator ();
185 while (iter.hasNext ())
189 List<String> instanceList;
190 StringBuffer instance;
192 objName = iter.next ();
193 pd_tmp = new PluginData (pd);
194 instanceList = new ArrayList<String> ();
195 instance = new StringBuffer ();
197 Collectd.logDebug ("GenericJMXConfMBean: objName = "
198 + objName.toString ());
200 for (int i = 0; i < this._instance_from.size (); i++)
203 String propertyValue;
205 propertyName = this._instance_from.get (i);
206 propertyValue = objName.getKeyProperty (propertyName);
207 if (propertyValue == null)
209 Collectd.logError ("GenericJMXConfMBean: "
210 + "No such property in object name: " + propertyName);
214 instanceList.add (propertyValue);
218 if (instance_prefix != null)
219 instance.append (instance_prefix);
221 if (this._instance_prefix != null)
222 instance.append (this._instance_prefix);
224 for (int i = 0; i < instanceList.size (); i++)
227 instance.append ("-");
228 instance.append (instanceList.get (i));
231 pd_tmp.setPluginInstance (instance.toString ());
233 Collectd.logDebug ("GenericJMXConfMBean: instance = " + instance.toString ());
235 for (int i = 0; i < this._values.size (); i++)
236 this._values.get (i).query (conn, objName, pd_tmp);
240 } /* }}} void query */
243 /* vim: set sw=2 sts=2 et fdm=marker : */