From: Florian Forster Date: Sun, 22 Feb 2009 21:15:47 +0000 (+0100) Subject: java plugin: Add support for `flush' callbacks. X-Git-Tag: collectd-4.7.0~127^2~18 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=ddf1f52c7fb0eab19a0f35e0747dfa21edcae109;p=collectd.git java plugin: Add support for `flush' callbacks. --- diff --git a/bindings/java/org/collectd/api/Collectd.java b/bindings/java/org/collectd/api/Collectd.java index 70629595..3950b220 100644 --- a/bindings/java/org/collectd/api/Collectd.java +++ b/bindings/java/org/collectd/api/Collectd.java @@ -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 index 00000000..03fa532a --- /dev/null +++ b/bindings/java/org/collectd/api/CollectdFlushInterface.java @@ -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 + */ + +package org.collectd.api; + +public interface CollectdFlushInterface +{ + public int flush (int timeout, String identifier); +} diff --git a/src/collectd-java.pod b/src/collectd-java.pod index 9326cb00..c4f4ef7f 100644 --- a/src/collectd-java.pod +++ b/src/collectd-java.pod @@ -92,7 +92,7 @@ Each callback method is now explained in more detail: Interface: B -Signature: I B (I) +Signature: I B (I ci) This method is passed a B object, if both, method and configuration, are available. B is the root of a tree representing @@ -137,7 +137,7 @@ Java "read"-methods are not suspended for increasing intervals like C Interface: B -Signature: I B (I) +Signature: I B (I vl) This method is called whenever a value is dispatched to the daemon. The corresponding C "write"-functions are passed a C, so they can @@ -148,6 +148,27 @@ method of the B 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 + +Signature: I B (I timeout, I identifier) + +This method is called when the daemon received a flush command. This can either +be done using the C signal (see L) or using the I +plugin (see L). + +If I 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 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. + +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 @@ -232,6 +253,15 @@ Registers the B function of I with the daemon. Returns zero upon success and non-zero when an error occurred. +=head2 registerFlush + +Signature: I B (I name, +I object) + +Registers the B function of I with the daemon. + +Returns zero upon success and non-zero when an error occurred. + =head2 registerShutdown Signature: I B (I name, diff --git a/src/java.c b/src/java.c index 984d29a4..39311c8b 100644 --- a/src/java.c +++ b/src/java.c @@ -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. */