319615c96ff43752a44d6ac985bed8b903832756
[collectd.git] / bindings / java / org / collectd / java / GenericJMX.java
1 /*
2  * collectd/java - org/collectd/java/GenericJMX.java
3  * Copyright (C) 2009  Florian octo Forster
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  */
21
22 package org.collectd.java;
23
24 import java.util.List;
25 import java.util.ArrayList;
26 import java.util.Map;
27 import java.util.TreeMap;
28
29 import org.collectd.api.Collectd;
30 import org.collectd.api.CollectdConfigInterface;
31 import org.collectd.api.CollectdInitInterface;
32 import org.collectd.api.CollectdReadInterface;
33 import org.collectd.api.CollectdShutdownInterface;
34 import org.collectd.api.OConfigValue;
35 import org.collectd.api.OConfigItem;
36
37 public class GenericJMX implements CollectdConfigInterface,
38        CollectdReadInterface,
39        CollectdShutdownInterface
40 {
41   static private Map<String,GenericJMXConfMBean> _mbeans
42     = new TreeMap<String,GenericJMXConfMBean> ();
43
44   private List<GenericJMXConfConnection> _connections = null;
45
46   public GenericJMX ()
47   {
48     Collectd.registerConfig   ("GenericJMX", this);
49     Collectd.registerRead     ("GenericJMX", this);
50     Collectd.registerShutdown ("GenericJMX", this);
51
52     this._connections = new ArrayList<GenericJMXConfConnection> ();
53   }
54
55   public int config (OConfigItem ci) /* {{{ */
56   {
57     List<OConfigItem> children;
58     int i;
59
60     Collectd.logDebug ("GenericJMX plugin: config: ci = " + ci + ";");
61
62     children = ci.getChildren ();
63     for (i = 0; i < children.size (); i++)
64     {
65       OConfigItem child;
66       String key;
67
68       child = children.get (i);
69       key = child.getKey ();
70       if (key.equalsIgnoreCase ("MBean"))
71       {
72         try
73         {
74           GenericJMXConfMBean mbean = new GenericJMXConfMBean (child);
75           putMBean (mbean);
76         }
77         catch (IllegalArgumentException e)
78         {
79           Collectd.logError ("GenericJMX plugin: "
80               + "Evaluating `MBean' block failed: " + e);
81         }
82       }
83       else if (key.equalsIgnoreCase ("Connection"))
84       {
85         try
86         {
87           GenericJMXConfConnection conn = new GenericJMXConfConnection (child);
88           this._connections.add (conn);
89         }
90         catch (IllegalArgumentException e)
91         {
92           Collectd.logError ("GenericJMX plugin: "
93               + "Evaluating `Connection' block failed: " + e);
94         }
95       }
96       else
97       {
98         Collectd.logError ("GenericJMX plugin: Unknown config option: " + key);
99       }
100     } /* for (i = 0; i < children.size (); i++) */
101
102     return (0);
103   } /* }}} int config */
104
105   public int read () /* {{{ */
106   {
107     for (int i = 0; i < this._connections.size (); i++)
108     {
109       try
110       {
111         this._connections.get (i).query ();
112       }
113       catch (Exception e)
114       {
115         Collectd.logError ("GenericJMX: Caught unexpected exception: " + e);
116         e.printStackTrace ();
117       }
118     }
119
120     return (0);
121   } /* }}} int read */
122
123   public int shutdown () /* {{{ */
124   {
125     System.out.print ("org.collectd.java.GenericJMX.Shutdown ();\n");
126     this._connections = null;
127     return (0);
128   } /* }}} int shutdown */
129
130   /*
131    * static functions
132    */
133   static public GenericJMXConfMBean getMBean (String alias)
134   {
135     return (_mbeans.get (alias));
136   }
137
138   static private void putMBean (GenericJMXConfMBean mbean)
139   {
140     Collectd.logDebug ("GenericJMX.putMBean: Adding " + mbean.getName ());
141     _mbeans.put (mbean.getName (), mbean);
142   }
143 } /* class GenericJMX */
144
145 /* vim: set sw=2 sts=2 et fdm=marker : */