Merge remote-tracking branch 'origin/collectd-4.10' into collectd-5.3
[collectd.git] / bindings / java / org / collectd / java / GenericJMXConfMBean.java
index 1b4d9cc..b1fbfb3 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * collectd/java - org/collectd/java/GenericJMXConfMBean.java
- * Copyright (C) 2009  Florian octo Forster
+ * Copyright (C) 2009,2010  Florian octo Forster
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -39,7 +39,8 @@ class GenericJMXConfMBean
 {
   private String _name; /* name by which this mapping is referenced */
   private ObjectName _obj_name;
-  private String _instance;
+  private String _instance_prefix;
+  private List<String> _instance_from;
   private List<GenericJMXConfValue> _values;
 
   private String getConfigString (OConfigItem ci) /* {{{ */
@@ -68,8 +69,9 @@ class GenericJMXConfMBean
 
 /*
  * <MBean "alias name">
- *   Instance "foobar"
  *   ObjectName "object name"
+ *   InstancePrefix "foobar"
+ *   InstanceFrom "name"
  *   <Value />
  *   <Value />
  *   :
@@ -87,6 +89,8 @@ class GenericJMXConfMBean
             + "MBean blocks need exactly one string argument."));
 
     this._obj_name = null;
+    this._instance_prefix = null;
+    this._instance_from = new ArrayList<String> ();
     this._values = new ArrayList<GenericJMXConfValue> ();
 
     children = ci.getChildren ();
@@ -97,13 +101,7 @@ class GenericJMXConfMBean
 
       Collectd.logDebug ("GenericJMXConfMBean: child.getKey () = "
           + child.getKey ());
-      if (child.getKey ().equalsIgnoreCase ("Instance"))
-      {
-        String tmp = getConfigString (child);
-        if (tmp != null)
-          this._instance = tmp;
-      }
-      else if (child.getKey ().equalsIgnoreCase ("ObjectName"))
+      if (child.getKey ().equalsIgnoreCase ("ObjectName"))
       {
         String tmp = getConfigString (child);
         if (tmp == null)
@@ -119,6 +117,18 @@ class GenericJMXConfMBean
                 + tmp, e));
         }
       }
+      else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
+      {
+        String tmp = getConfigString (child);
+        if (tmp != null)
+          this._instance_prefix = tmp;
+      }
+      else if (child.getKey ().equalsIgnoreCase ("InstanceFrom"))
+      {
+        String tmp = getConfigString (child);
+        if (tmp != null)
+          this._instance_from.add (tmp);
+      }
       else if (child.getKey ().equalsIgnoreCase ("Value"))
       {
         GenericJMXConfValue cv;
@@ -144,7 +154,8 @@ class GenericJMXConfMBean
     return (this._name);
   } /* }}} */
 
-  public void query (MBeanServerConnection conn, PluginData pd) /* {{{ */
+  public int query (MBeanServerConnection conn, PluginData pd, /* {{{ */
+      String instance_prefix)
   {
     Set<ObjectName> names;
     Iterator<ObjectName> iter;
@@ -156,7 +167,7 @@ class GenericJMXConfMBean
     catch (Exception e)
     {
       Collectd.logError ("GenericJMXConfMBean: queryNames failed: " + e);
-      return;
+      return (-1);
     }
 
     if (names.size () == 0)
@@ -168,20 +179,59 @@ class GenericJMXConfMBean
     iter = names.iterator ();
     while (iter.hasNext ())
     {
-      ObjectName objName;
-      PluginData pd_tmp;
+      ObjectName   objName;
+      PluginData   pd_tmp;
+      List<String> instanceList;
+      StringBuffer instance;
 
-      objName = iter.next ();
-      pd_tmp = new PluginData (pd);
+      objName      = iter.next ();
+      pd_tmp       = new PluginData (pd);
+      instanceList = new ArrayList<String> ();
+      instance     = new StringBuffer ();
 
       Collectd.logDebug ("GenericJMXConfMBean: objName = "
           + objName.toString ());
 
-      pd_tmp.setPluginInstance ((this._instance != null) ? this._instance : "");
+      for (int i = 0; i < this._instance_from.size (); i++)
+      {
+        String propertyName;
+        String propertyValue;
+
+        propertyName = this._instance_from.get (i);
+        propertyValue = objName.getKeyProperty (propertyName);
+        if (propertyValue == null)
+        {
+          Collectd.logError ("GenericJMXConfMBean: "
+              + "No such property in object name: " + propertyName);
+        }
+        else
+        {
+          instanceList.add (propertyValue);
+        }
+      }
+
+      if (instance_prefix != null)
+        instance.append (instance_prefix);
+
+      if (this._instance_prefix != null)
+        instance.append (this._instance_prefix);
+
+      for (int i = 0; i < instanceList.size (); i++)
+      {
+        if (i > 0)
+          instance.append ("-");
+        instance.append (instanceList.get (i));
+      }
+
+      pd_tmp.setPluginInstance (instance.toString ());
+
+      Collectd.logDebug ("GenericJMXConfMBean: instance = " + instance.toString ());
 
       for (int i = 0; i < this._values.size (); i++)
         this._values.get (i).query (conn, objName, pd_tmp);
     }
+
+    return (0);
   } /* }}} void query */
 }