2 * collectd - bindings/java/org/collectd/java/GenericJMX.java
3 * Copyright (C) 2009 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.List;
30 import java.util.ArrayList;
32 import java.util.TreeMap;
34 import org.collectd.api.Collectd;
35 import org.collectd.api.CollectdConfigInterface;
36 import org.collectd.api.CollectdInitInterface;
37 import org.collectd.api.CollectdReadInterface;
38 import org.collectd.api.CollectdShutdownInterface;
39 import org.collectd.api.OConfigValue;
40 import org.collectd.api.OConfigItem;
42 public class GenericJMX implements CollectdConfigInterface,
43 CollectdReadInterface,
44 CollectdShutdownInterface
46 static private Map<String,GenericJMXConfMBean> _mbeans
47 = new TreeMap<String,GenericJMXConfMBean> ();
49 private List<GenericJMXConfConnection> _connections = null;
53 Collectd.registerConfig ("GenericJMX", this);
54 Collectd.registerRead ("GenericJMX", this);
55 Collectd.registerShutdown ("GenericJMX", this);
57 this._connections = new ArrayList<GenericJMXConfConnection> ();
60 public int config (OConfigItem ci) /* {{{ */
62 List<OConfigItem> children;
65 Collectd.logDebug ("GenericJMX plugin: config: ci = " + ci + ";");
67 children = ci.getChildren ();
68 for (i = 0; i < children.size (); i++)
73 child = children.get (i);
74 key = child.getKey ();
75 if (key.equalsIgnoreCase ("MBean"))
79 GenericJMXConfMBean mbean = new GenericJMXConfMBean (child);
82 catch (IllegalArgumentException e)
84 Collectd.logError ("GenericJMX plugin: "
85 + "Evaluating `MBean' block failed: " + e);
88 else if (key.equalsIgnoreCase ("Connection"))
92 GenericJMXConfConnection conn = new GenericJMXConfConnection (child);
93 this._connections.add (conn);
95 catch (IllegalArgumentException e)
97 Collectd.logError ("GenericJMX plugin: "
98 + "Evaluating `Connection' block failed: " + e);
103 Collectd.logError ("GenericJMX plugin: Unknown config option: " + key);
105 } /* for (i = 0; i < children.size (); i++) */
108 } /* }}} int config */
110 public int read () /* {{{ */
112 for (int i = 0; i < this._connections.size (); i++)
116 this._connections.get (i).query ();
120 Collectd.logError ("GenericJMX: Caught unexpected exception: " + e);
121 e.printStackTrace ();
128 public int shutdown () /* {{{ */
130 System.out.print ("org.collectd.java.GenericJMX.Shutdown ();\n");
131 this._connections = null;
133 } /* }}} int shutdown */
138 static public GenericJMXConfMBean getMBean (String alias)
140 return (_mbeans.get (alias));
143 static private void putMBean (GenericJMXConfMBean mbean)
145 Collectd.logDebug ("GenericJMX.putMBean: Adding " + mbean.getName ());
146 _mbeans.put (mbean.getName (), mbean);
148 } /* class GenericJMX */
150 /* vim: set sw=2 sts=2 et fdm=marker : */