Code

java bindings: GenericJMX: Implement user/password authentication.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Thu, 20 Aug 2009 15:21:33 +0000 (17:21 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Thu, 20 Aug 2009 15:21:33 +0000 (17:21 +0200)
bindings/java/org/collectd/java/GenericJMXConfConnection.java
contrib/GenericJMX.conf

index 0108b53fdb1d5f3b56e7d477c54c5c2ef35a73ad..7214fd761c54f1f5ddd184bd5da311e0d82e5226 100644 (file)
 package org.collectd.java;
 
 import java.util.List;
+import java.util.Map;
 import java.util.Iterator;
 import java.util.ArrayList;
+import java.util.HashMap;
 
 import javax.management.MBeanServerConnection;
 import javax.management.ObjectName;
@@ -40,6 +42,8 @@ import org.collectd.api.OConfigItem;
 
 class GenericJMXConfConnection
 {
+  private String _username = null;
+  private String _password = null;
   private String _host = null;
   private String _service_url = null;
   private MBeanServerConnection _jmx_connection = null;
@@ -76,14 +80,29 @@ private void connect () /* {{{ */
 {
   JMXServiceURL service_url;
   JMXConnector connector;
+  Map environment;
 
   if (_jmx_connection != null)
     return;
 
+  environment = null;
+  if (this._password != null)
+  {
+    String[] credentials;
+
+    if (this._username == null)
+      this._username = new String ("monitorRole");
+
+    credentials = new String[] { this._username, this._password };
+
+    environment = new HashMap ();
+    environment.put (JMXConnector.CREDENTIALS, credentials);
+  }
+
   try
   {
     service_url = new JMXServiceURL (this._service_url);
-    connector = JMXConnectorFactory.connect (service_url);
+    connector = JMXConnectorFactory.connect (service_url, environment);
     _jmx_connection = connector.getMBeanServerConnection ();
   }
   catch (Exception e)
@@ -125,6 +144,18 @@ private void connect () /* {{{ */
         if (tmp != null)
           this._host = tmp;
       }
+      else if (child.getKey ().equalsIgnoreCase ("User"))
+      {
+        String tmp = getConfigString (child);
+        if (tmp != null)
+          this._username = tmp;
+      }
+      else if (child.getKey ().equalsIgnoreCase ("Password"))
+      {
+        String tmp = getConfigString (child);
+        if (tmp != null)
+          this._password = tmp;
+      }
       else if (child.getKey ().equalsIgnoreCase ("ServiceURL"))
       {
         String tmp = getConfigString (child);
index 394dd3065a25c487ac9504ab406f473442a70798..f4dd8f0ead954d5281be573d641f6ca20a1cb322 100644 (file)
     # Connection blocks #
     #####################
     <Connection>
-      Host "localhost"
       ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
+      User "monitorRole"
+      Password "queeZie1"
+      Host "localhost"
       Collect "classes"
       Collect "compilation"
       Collect "garbage_collector"