Fix building of java binding
[collectd.git] / bindings / java / org / collectd / java / GenericJMXConfConnection.java
1 /*
2  * collectd/java - org/collectd/java/GenericJMXConfConnection.java
3  * Copyright (C) 2009-2012  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 collectd.org>
20  */
21
22 package org.collectd.java;
23
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Iterator;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.net.InetAddress;
30 import java.net.UnknownHostException;
31
32 import javax.management.MBeanServerConnection;
33 import javax.management.ObjectName;
34 import javax.management.MalformedObjectNameException;
35
36 import javax.management.remote.JMXServiceURL;
37 import javax.management.remote.JMXConnector;
38 import javax.management.remote.JMXConnectorFactory;
39
40 import org.collectd.api.Collectd;
41 import org.collectd.api.PluginData;
42 import org.collectd.api.OConfigValue;
43 import org.collectd.api.OConfigItem;
44
45 class GenericJMXConfConnection
46 {
47   private String _username = null;
48   private String _password = null;
49   private String _host = null;
50   private String _instance_prefix = null;
51   private String _service_url = null;
52   private MBeanServerConnection _jmx_connection = null;
53   private List<GenericJMXConfMBean> _mbeans = null;
54
55   /*
56    * private methods
57    */
58   private String getConfigString (OConfigItem ci) /* {{{ */
59   {
60     List<OConfigValue> values;
61     OConfigValue v;
62
63     values = ci.getValues ();
64     if (values.size () != 1)
65     {
66       Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
67           + " configuration option needs exactly one string argument.");
68       return (null);
69     }
70
71     v = values.get (0);
72     if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
73     {
74       Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
75           + " configuration option needs exactly one string argument.");
76       return (null);
77     }
78
79     return (v.getString ());
80   } /* }}} String getConfigString */
81
82   private String getHost () /* {{{ */
83   {
84     if (this._host != null)
85     {
86       return (this._host);
87     }
88
89     try
90     {
91       InetAddress localHost = InetAddress.getLocalHost();
92       return (localHost.getHostName ());
93     }
94     catch (UnknownHostException e)
95     {
96       return ("localhost");
97     }
98   } /* }}} String getHost */
99
100 private void connect () /* {{{ */
101 {
102   JMXServiceURL service_url;
103   JMXConnector connector;
104   Map environment;
105
106   if (_jmx_connection != null)
107     return;
108
109   environment = null;
110   if (this._password != null)
111   {
112     String[] credentials;
113
114     if (this._username == null)
115       this._username = new String ("monitorRole");
116
117     credentials = new String[] { this._username, this._password };
118
119     environment = new HashMap ();
120     environment.put (JMXConnector.CREDENTIALS, credentials);
121   }
122
123   try
124   {
125     service_url = new JMXServiceURL (this._service_url);
126     connector = JMXConnectorFactory.connect (service_url, environment);
127     _jmx_connection = connector.getMBeanServerConnection ();
128   }
129   catch (Exception e)
130   {
131     Collectd.logError ("GenericJMXConfConnection: "
132         + "Creating MBean server connection failed: " + e);
133     return;
134   }
135 } /* }}} void connect */
136
137 /*
138  * public methods
139  *
140  * <Connection>
141  *   Host "tomcat0.mycompany"
142  *   ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
143  *   Collect "java.lang:type=GarbageCollector,name=Copy"
144  *   Collect "java.lang:type=Memory"
145  * </Connection>
146  *
147  */
148   public GenericJMXConfConnection (OConfigItem ci) /* {{{ */
149     throws IllegalArgumentException
150   {
151     List<OConfigItem> children;
152     Iterator<OConfigItem> iter;
153
154     this._mbeans = new ArrayList<GenericJMXConfMBean> ();
155
156     children = ci.getChildren ();
157     iter = children.iterator ();
158     while (iter.hasNext ())
159     {
160       OConfigItem child = iter.next ();
161
162       if (child.getKey ().equalsIgnoreCase ("Host"))
163       {
164         String tmp = getConfigString (child);
165         if (tmp != null)
166           this._host = tmp;
167       }
168       else if (child.getKey ().equalsIgnoreCase ("User"))
169       {
170         String tmp = getConfigString (child);
171         if (tmp != null)
172           this._username = tmp;
173       }
174       else if (child.getKey ().equalsIgnoreCase ("Password"))
175       {
176         String tmp = getConfigString (child);
177         if (tmp != null)
178           this._password = tmp;
179       }
180       else if (child.getKey ().equalsIgnoreCase ("ServiceURL"))
181       {
182         String tmp = getConfigString (child);
183         if (tmp != null)
184           this._service_url = tmp;
185       }
186       else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
187       {
188         String tmp = getConfigString (child);
189         if (tmp != null)
190           this._instance_prefix = tmp;
191       }
192       else if (child.getKey ().equalsIgnoreCase ("Collect"))
193       {
194         String tmp = getConfigString (child);
195         if (tmp != null)
196         {
197           GenericJMXConfMBean mbean;
198
199           mbean = GenericJMX.getMBean (tmp);
200           if (mbean == null)
201             throw (new IllegalArgumentException ("No such MBean defined: "
202                   + tmp + ". Please make sure all `MBean' blocks appear "
203                   + "before (above) all `Connection' blocks."));
204           Collectd.logDebug ("GenericJMXConfConnection: " + this._host + ": Add " + tmp);
205           this._mbeans.add (mbean);
206         }
207       }
208       else
209         throw (new IllegalArgumentException ("Unknown option: "
210               + child.getKey ()));
211     }
212
213     if (this._service_url == null)
214       throw (new IllegalArgumentException ("No service URL was defined."));
215     if (this._mbeans.size () == 0)
216       throw (new IllegalArgumentException ("No valid collect statement "
217             + "present."));
218   } /* }}} GenericJMXConfConnection (OConfigItem ci) */
219
220   public void query () /* {{{ */
221   {
222     PluginData pd;
223
224     connect ();
225
226     if (this._jmx_connection == null)
227       return;
228
229     Collectd.logDebug ("GenericJMXConfConnection.query: "
230         + "Reading " + this._mbeans.size () + " mbeans from "
231         + ((this._host != null) ? this._host : "(null)"));
232
233     pd = new PluginData ();
234     pd.setHost (this.getHost ());
235     pd.setPlugin ("GenericJMX");
236
237     for (int i = 0; i < this._mbeans.size (); i++)
238     {
239       int status;
240
241       status = this._mbeans.get (i).query (this._jmx_connection, pd,
242           this._instance_prefix);
243       if (status != 0)
244       {
245         this._jmx_connection = null;
246         return;
247       }
248     } /* for */
249   } /* }}} void query */
250
251   public String toString ()
252   {
253     return (new String ("host = " + this._host + "; "
254           + "url = " + this._service_url));
255   }
256 }
257
258 /* vim: set sw=2 sts=2 et fdm=marker : */