eea2d8ab81901b2e375859d9bb55f2f2fd557663
[collectd.git] / bindings / java / org / collectd / java / GenericJMXConfMBean.java
1 /*
2  * collectd/java - org/collectd/java/GenericJMXConfMBean.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.Iterator;
26 import java.util.ArrayList;
27
28 import javax.management.MBeanServerConnection;
29 import javax.management.ObjectName;
30 import javax.management.MalformedObjectNameException;
31
32
33 import org.collectd.api.Collectd;
34 import org.collectd.api.PluginData;
35 import org.collectd.api.OConfigValue;
36 import org.collectd.api.OConfigItem;
37
38 class GenericJMXConfMBean
39 {
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;
44
45   private String getConfigString (OConfigItem ci) /* {{{ */
46   {
47     List<OConfigValue> values;
48     OConfigValue v;
49
50     values = ci.getValues ();
51     if (values.size () != 1)
52     {
53       Collectd.logError ("GenericJMXConfMBean: The " + ci.getKey ()
54           + " configuration option needs exactly one string argument.");
55       return (null);
56     }
57
58     v = values.get (0);
59     if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
60     {
61       Collectd.logError ("GenericJMXConfMBean: The " + ci.getKey ()
62           + " configuration option needs exactly one string argument.");
63       return (null);
64     }
65
66     return (v.getString ());
67   } /* }}} String getConfigString */
68
69 /*
70  * <MBean "alias name">
71  *   Instance "foobar"
72  *   ObjectName "object name"
73  *   <Value />
74  *   <Value />
75  *   :
76  * </MBean>
77  */
78   public GenericJMXConfMBean (OConfigItem ci) /* {{{ */
79     throws IllegalArgumentException
80   {
81     List<OConfigItem> children;
82     Iterator<OConfigItem> iter;
83
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."));
88
89     this._obj_name = null;
90     this._values = new ArrayList<GenericJMXConfValue> ();
91
92     children = ci.getChildren ();
93     iter = children.iterator ();
94     while (iter.hasNext ())
95     {
96       OConfigItem child = iter.next ();
97
98       Collectd.logDebug ("GenericJMXConfMBean: child.getKey () = "
99           + child.getKey ());
100       if (child.getKey ().equalsIgnoreCase ("Instance"))
101       {
102         String tmp = getConfigString (child);
103         if (tmp != null)
104           this._instance = tmp;
105       }
106       else if (child.getKey ().equalsIgnoreCase ("ObjectName"))
107       {
108         String tmp = getConfigString (child);
109         if (tmp == null)
110           continue;
111
112         try
113         {
114           this._obj_name = new ObjectName (tmp);
115         }
116         catch (MalformedObjectNameException e)
117         {
118           throw (new IllegalArgumentException ("Not a valid object name: "
119                 + tmp, e));
120         }
121       }
122       else if (child.getKey ().equalsIgnoreCase ("Value"))
123       {
124         GenericJMXConfValue cv;
125
126         cv = new GenericJMXConfValue (child);
127         this._values.add (cv);
128       }
129       else
130         throw (new IllegalArgumentException ("Unknown option: "
131               + child.getKey ()));
132     }
133
134     if (this._obj_name == null)
135       throw (new IllegalArgumentException ("No object name was defined."));
136
137     if (this._values.size () == 0)
138       throw (new IllegalArgumentException ("No value block was defined."));
139
140   } /* }}} GenericJMXConfMBean (OConfigItem ci) */
141
142   public String getName ()
143   {
144     return (this._name);
145   }
146
147   public void query (MBeanServerConnection conn, PluginData pd) /* {{{ */
148   {
149     pd.setPluginInstance ((this._instance != null) ? this._instance : "");
150
151     for (int i = 0; i < this._values.size (); i++)
152       this._values.get (i).query (conn, this._obj_name, pd);
153   } /* }}} void query */
154 }
155
156 /* vim: set sw=2 sts=2 et fdm=marker : */