1 /**
2 * collectd - bindings/java/org/collectd/java/GenericJMX.java
3 * Copyright (C) 2009 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 */
27 package org.collectd.java;
29 import java.util.List;
30 import java.util.ArrayList;
31 import java.util.Map;
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
45 {
46 static private Map<String,GenericJMXConfMBean> _mbeans
47 = new TreeMap<String,GenericJMXConfMBean> ();
49 private List<GenericJMXConfConnection> _connections = null;
51 public GenericJMX ()
52 {
53 Collectd.registerConfig ("GenericJMX", this);
54 Collectd.registerRead ("GenericJMX", this);
55 Collectd.registerShutdown ("GenericJMX", this);
57 this._connections = new ArrayList<GenericJMXConfConnection> ();
58 }
60 public int config (OConfigItem ci) /* {{{ */
61 {
62 List<OConfigItem> children;
63 int i;
65 Collectd.logDebug ("GenericJMX plugin: config: ci = " + ci + ";");
67 children = ci.getChildren ();
68 for (i = 0; i < children.size (); i++)
69 {
70 OConfigItem child;
71 String key;
73 child = children.get (i);
74 key = child.getKey ();
75 if (key.equalsIgnoreCase ("MBean"))
76 {
77 try
78 {
79 GenericJMXConfMBean mbean = new GenericJMXConfMBean (child);
80 putMBean (mbean);
81 }
82 catch (IllegalArgumentException e)
83 {
84 Collectd.logError ("GenericJMX plugin: "
85 + "Evaluating `MBean' block failed: " + e);
86 }
87 }
88 else if (key.equalsIgnoreCase ("Connection"))
89 {
90 try
91 {
92 GenericJMXConfConnection conn = new GenericJMXConfConnection (child);
93 this._connections.add (conn);
94 }
95 catch (IllegalArgumentException e)
96 {
97 Collectd.logError ("GenericJMX plugin: "
98 + "Evaluating `Connection' block failed: " + e);
99 }
100 }
101 else
102 {
103 Collectd.logError ("GenericJMX plugin: Unknown config option: " + key);
104 }
105 } /* for (i = 0; i < children.size (); i++) */
107 return (0);
108 } /* }}} int config */
110 public int read () /* {{{ */
111 {
112 for (int i = 0; i < this._connections.size (); i++)
113 {
114 try
115 {
116 this._connections.get (i).query ();
117 }
118 catch (Exception e)
119 {
120 Collectd.logError ("GenericJMX: Caught unexpected exception: " + e);
121 e.printStackTrace ();
122 }
123 }
125 return (0);
126 } /* }}} int read */
128 public int shutdown () /* {{{ */
129 {
130 System.out.print ("org.collectd.java.GenericJMX.Shutdown ();\n");
131 this._connections = null;
132 return (0);
133 } /* }}} int shutdown */
135 /*
136 * static functions
137 */
138 static public GenericJMXConfMBean getMBean (String alias)
139 {
140 return (_mbeans.get (alias));
141 }
143 static private void putMBean (GenericJMXConfMBean mbean)
144 {
145 Collectd.logDebug ("GenericJMX.putMBean: Adding " + mbean.getName ());
146 _mbeans.put (mbean.getName (), mbean);
147 }
148 } /* class GenericJMX */
150 /* vim: set sw=2 sts=2 et fdm=marker : */