From 468ae3bbaa875edc0f45af7603a2c66680d6b80c Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 31 Oct 2010 13:48:05 +0100 Subject: [PATCH] java plugin: Fix conversion from / to Java. --- .../collectd/api/CollectdFlushInterface.java | 2 +- src/java.c | 22 ++++++++++++++----- src/utils_time.h | 2 ++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/bindings/java/org/collectd/api/CollectdFlushInterface.java b/bindings/java/org/collectd/api/CollectdFlushInterface.java index 3e492ddf..410c61c6 100644 --- a/bindings/java/org/collectd/api/CollectdFlushInterface.java +++ b/bindings/java/org/collectd/api/CollectdFlushInterface.java @@ -29,5 +29,5 @@ package org.collectd.api; */ public interface CollectdFlushInterface { - public int flush (int timeout, String identifier); + public int flush (Number timeout, String identifier); } diff --git a/src/java.c b/src/java.c index 528ec9c4..4d490eb6 100644 --- a/src/java.c +++ b/src/java.c @@ -109,7 +109,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); +static int cjni_flush (cdtime_t timeout, const char *identifier, user_data_t *ud); static void cjni_log (int severity, const char *message, user_data_t *ud); static int cjni_notification (const notification_t *n, user_data_t *ud); @@ -809,7 +809,7 @@ static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */ #undef SET_STRING /* Set the `time' member. Java stores time in milliseconds. */ - status = ctoj_long (jvm_env, ((jlong) vl->time) * ((jlong) 1000), + status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (vl->time), c_valuelist, o_valuelist, "setTime"); if (status != 0) { @@ -1729,7 +1729,7 @@ static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ case CB_TYPE_FLUSH: method_name = "flush"; - method_signature = "(ILjava/lang/String;)I"; + method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I"; break; case CB_TYPE_SHUTDOWN: @@ -2551,11 +2551,12 @@ static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */ } /* }}} int cjni_write */ /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */ -static int cjni_flush (int timeout, const char *identifier, /* {{{ */ +static int cjni_flush (cdtime_t timeout, const char *identifier, /* {{{ */ user_data_t *ud) { JNIEnv *jvm_env; cjni_callback_info_t *cbi; + jobject o_timeout; jobject o_identifier; int status; int ret_status; @@ -2578,21 +2579,32 @@ static int cjni_flush (int timeout, const char *identifier, /* {{{ */ cbi = (cjni_callback_info_t *) ud->data; + o_timeout = ctoj_jdouble_to_number (jvm_env, + (jdouble) CDTIME_T_TO_DOUBLE (timeout)); + if (o_timeout == NULL) + { + ERROR ("java plugin: cjni_flush: Converting double " + "to Number object failed."); + return (-1); + } + o_identifier = NULL; if (identifier != NULL) { o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier); if (o_identifier == NULL) { + (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout); ERROR ("java plugin: cjni_flush: NewStringUTF failed."); return (-1); } } ret_status = (*jvm_env)->CallIntMethod (jvm_env, - cbi->object, cbi->method, (jint) timeout, o_identifier); + cbi->object, cbi->method, o_timeout, o_identifier); (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier); + (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout); status = cjni_thread_detach (); if (status != 0) diff --git a/src/utils_time.h b/src/utils_time.h index 0aa4fce6..3c31b353 100644 --- a/src/utils_time.h +++ b/src/utils_time.h @@ -41,6 +41,8 @@ typedef uint64_t cdtime_t; #define CDTIME_T_TO_DOUBLE(t) (((double) (t)) / 1073741824.0) #define DOUBLE_TO_CDTIME_T(d) ((cdtime_t) ((d) * 1073741824.0)) +#define CDTIME_T_TO_MS(t) ((long) (((double) (t)) / 1073741.824)) + #define US_TO_CDTIME_T(us) ((cdtime_t) (((double) (us)) * 1073.741824)) #define CDTIME_T_TO_US(t) ((suseconds_t) (((double) (t)) / 1073.741824)) #define NS_TO_CDTIME_T(ns) ((cdtime_t) (((double) (ns)) * 1.073741824)) -- 2.30.2