java plugin: Add support for `flush' callbacks.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 22 Feb 2009 21:15:47 +0000 (22:15 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 22 Feb 2009 21:15:47 +0000 (22:15 +0100)
bindings/java/org/collectd/api/Collectd.java
bindings/java/org/collectd/api/CollectdFlushInterface.java [new file with mode: 0644]
src/collectd-java.pod
src/java.c

index 7062959..3950b22 100644 (file)
@@ -59,6 +59,12 @@ public class Collectd
       CollectdWriteInterface object);
 
   /**
+   * Java representation of collectd/src/plugin.h:plugin_register_flush
+   */
+  native public static int registerFlush (String name,
+      CollectdFlushInterface object);
+
+  /**
    * Java representation of collectd/src/plugin.h:plugin_register_shutdown
    */
   native public static int registerShutdown (String name,
diff --git a/bindings/java/org/collectd/api/CollectdFlushInterface.java b/bindings/java/org/collectd/api/CollectdFlushInterface.java
new file mode 100644 (file)
index 0000000..03fa532
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * collectd/java - org/collectd/api/CollectdFlushInterface.java
+ * Copyright (C) 2009  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
+ * Free Software Foundation; only version 2 of the License is applicable.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ * Authors:
+ *   Florian octo Forster <octo at verplant.org>
+ */
+
+package org.collectd.api;
+
+public interface CollectdFlushInterface
+{
+       public int flush (int timeout, String identifier);
+}
index 9326cb0..c4f4ef7 100644 (file)
@@ -92,7 +92,7 @@ Each callback method is now explained in more detail:
 
 Interface: B<org.collectd.api.CollectdConfigInterface>
 
-Signature: I<int> B<config> (I<OConfigItem>)
+Signature: I<int> B<config> (I<OConfigItem> ci)
 
 This method is passed a B<OConfigItem> object, if both, method and
 configuration, are available. B<OConfigItem> is the root of a tree representing
@@ -137,7 +137,7 @@ Java "read"-methods are not suspended for increasing intervals like C
 
 Interface: B<org.collectd.api.CollectdWriteInterface>
 
-Signature: I<int> B<write> (I<ValueList>)
+Signature: I<int> B<write> (I<ValueList> vl)
 
 This method is called whenever a value is dispatched to the daemon. The
 corresponding C "write"-functions are passed a C<data_set_t>, so they can
@@ -148,6 +148,27 @@ method of the B<ValueList> object.
 To signal success, this method has to return zero. Anything else will be
 considered an error condition and cause an appropriate message to be logged.
 
+=head2 flush callback
+
+Interface: B<org.collectd.api.CollectdFlushInterface>
+
+Signature: I<int> B<flush> (I<int> timeout, I<String> identifier)
+
+This method is called when the daemon received a flush command. This can either
+be done using the C<USR1> signal (see L<collectd(1)>) or using the I<unixsock>
+plugin (see L<collectd-unixsock(1)>).
+
+If I<timeout> is greater than zero, only values older than this number of
+seconds should be flushed. To signal that all values should be flushed
+regardless of age, this argument is set to a negative number.
+
+The I<identifier> specifies which value should be flushed. If it is not
+possible to flush one specific value, flush all values. To signal that all
+values should be flushed, this argument is set to I<null>.
+
+To signal success, this method has to return zero. Anything else will be
+considered an error condition and cause an appropriate message to be logged.
+
 =head2 shutdown callback
 
 Interface: B<org.collectd.api.CollectdShutdownInterface>
@@ -232,6 +253,15 @@ Registers the B<write> function of I<object> with the daemon.
 
 Returns zero upon success and non-zero when an error occurred.
 
+=head2 registerFlush
+
+Signature: I<int> B<registerFlush> (I<String> name,
+I<CollectdFlushInterface> object)
+
+Registers the B<flush> function of I<object> with the daemon.
+
+Returns zero upon success and non-zero when an error occurred.
+
 =head2 registerShutdown
 
 Signature: I<int> B<registerShutdown> (I<String> name,
index 984d29a..39311c8 100644 (file)
@@ -64,7 +64,8 @@ typedef struct java_plugin_config_s java_plugin_config_t;
 #define CB_TYPE_INIT     2
 #define CB_TYPE_READ     3
 #define CB_TYPE_WRITE    4
-#define CB_TYPE_SHUTDOWN 5
+#define CB_TYPE_FLUSH    5
+#define CB_TYPE_SHUTDOWN 6
 struct cjni_callback_info_s /* {{{ */
 {
   char     *name;
@@ -113,6 +114,7 @@ static int cjni_callback_register (JNIEnv *jvm_env, jobject o_name,
 static int cjni_read (user_data_t *user_data);
 static int cjni_write (const data_set_t *ds, const value_list_t *vl,
     user_data_t *ud);
+static int cjni_flush (int timeout, const char *identifier, user_data_t *ud);
 
 /* 
  * C to Java conversion functions
@@ -1247,6 +1249,29 @@ static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
   return (0);
 } /* }}} jint cjni_api_register_write */
 
+static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
+    jobject this, jobject o_name, jobject o_flush)
+{
+  user_data_t ud;
+  cjni_callback_info_t *cbi;
+
+  cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
+  if (cbi == NULL)
+    return (-1);
+
+  DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
+
+  memset (&ud, 0, sizeof (ud));
+  ud.data = (void *) cbi;
+  ud.free_func = cjni_callback_info_destroy;
+
+  plugin_register_flush (cbi->name, cjni_flush, &ud);
+
+  (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
+
+  return (0);
+} /* }}} jint cjni_api_register_flush */
+
 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
     jobject this, jobject o_name, jobject o_shutdown)
 {
@@ -1304,6 +1329,10 @@ static JNINativeMethod jni_api_functions[] = /* {{{ */
     "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
     cjni_api_register_write },
 
+  { "registerFlush",
+    "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
+    cjni_api_register_flush },
+
   { "registerShutdown",
     "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
     cjni_api_register_shutdown },
@@ -1351,6 +1380,11 @@ static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{
       method_signature = "(Lorg/collectd/api/ValueList;)I";
       break;
 
+    case CB_TYPE_FLUSH:
+      method_name = "flush";
+      method_signature = "(ILjava/lang/String;)I";
+      break;
+
     case CB_TYPE_SHUTDOWN:
       method_name = "shutdown";
       method_signature = "()I";
@@ -1907,6 +1941,59 @@ static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
   return (status);
 } /* }}} int cjni_write */
 
+/* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
+static int cjni_flush (int timeout, const char *identifier, /* {{{ */
+    user_data_t *ud)
+{
+  JNIEnv *jvm_env;
+  cjni_callback_info_t *cbi;
+  jobject o_identifier;
+  int status;
+
+  if (jvm == NULL)
+  {
+    ERROR ("java plugin: cjni_flush: jvm == NULL");
+    return (-1);
+  }
+
+  if ((ud == NULL) || (ud->data == NULL))
+  {
+    ERROR ("java plugin: cjni_flush: Invalid user data.");
+    return (-1);
+  }
+
+  jvm_env = cjni_thread_attach ();
+  if (jvm_env == NULL)
+    return (-1);
+
+  cbi = (cjni_callback_info_t *) ud->data;
+
+  o_identifier = NULL;
+  if (identifier != NULL)
+  {
+    o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
+    if (o_identifier == NULL)
+    {
+      ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
+      return (-1);
+    }
+  }
+
+  status = (*jvm_env)->CallIntMethod (jvm_env,
+      cbi->object, cbi->method, (jint) timeout, o_identifier);
+
+  (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
+
+  status = cjni_thread_detach ();
+  if (status != 0)
+  {
+    ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
+    return (-1);
+  }
+
+  return (status);
+} /* }}} int cjni_flush */
+
 /* Iterate over `java_classes_list' and create one object of each class. This
  * will trigger the object's constructors, to the objects can register callback
  * methods. */