X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fjava.c;h=01dc1b8ab006c48da164b6c2bc6ecde3b7b71668;hb=edd9af8a874ebc9f2a7f02846807229a648917db;hp=ee14373ed9f75485a91b9dc3b93283f8e4eafcd9;hpb=c1219a1c9db2e8400e2ee94b87f86ccd441485d5;p=collectd.git diff --git a/src/java.c b/src/java.c index ee14373e..01dc1b8a 100644 --- a/src/java.c +++ b/src/java.c @@ -17,7 +17,7 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian octo Forster + * Florian octo Forster * Justo Alonso Achaques **/ @@ -26,7 +26,6 @@ #include "common.h" #include "filter_chain.h" -#include #include #if !defined(JNI_VERSION_1_2) @@ -120,7 +119,7 @@ static int cjni_match_target_destroy (void **user_data); static int cjni_match_target_invoke (const data_set_t *ds, value_list_t *vl, notification_meta_t **meta, void **user_data); -/* +/* * C to Java conversion functions */ static int ctoj_string (JNIEnv *jvm_env, /* {{{ */ @@ -159,6 +158,23 @@ static int ctoj_string (JNIEnv *jvm_env, /* {{{ */ return (0); } /* }}} int ctoj_string */ +static jstring ctoj_output_string (JNIEnv *jvm_env, /* {{{ */ + const char *string) +{ + jstring o_string; + + /* Create a java.lang.String */ + o_string = (*jvm_env)->NewStringUTF (jvm_env, + (string != NULL) ? string : ""); + if (o_string == NULL) + { + ERROR ("java plugin: ctoj_output_string: NewStringUTF failed."); + return NULL; + } + + return (o_string); +} /* }}} int ctoj_output_string */ + static int ctoj_int (JNIEnv *jvm_env, /* {{{ */ jint value, jclass class_ptr, jobject object_ptr, const char *method_name) @@ -601,7 +617,7 @@ static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */ jmethodID m_add; jobject o_type; jobject o_dataset; - int i; + size_t i; /* Look up the org/collectd/api/DataSet class */ c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet"); @@ -746,7 +762,7 @@ static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */ jmethodID m_valuelist_constructor; jobject o_valuelist; int status; - int i; + size_t i; /* First, create a new ValueList instance.. * Look up the class.. */ @@ -1319,7 +1335,7 @@ static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */ return (0); } /* }}} int jtoc_notification */ -/* +/* * Functions accessible from Java */ static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */ @@ -1421,7 +1437,7 @@ static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */ ud.free_func = cjni_callback_info_destroy; plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read, - /* interval = */ NULL, &ud); + /* interval = */ 0, &ud); (*jvm_env)->DeleteLocalRef (jvm_env, o_read); @@ -1629,6 +1645,11 @@ static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */ (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str); } /* }}} void cjni_api_log */ +static jstring JNICALL cjni_api_get_hostname (JNIEnv *jvm_env, jobject this) +{ + return ctoj_output_string(jvm_env, hostname_g); +} + /* List of ``native'' functions, i. e. C-functions that can be called from * Java. */ static JNINativeMethod jni_api_functions[] = /* {{{ */ @@ -1688,6 +1709,11 @@ static JNINativeMethod jni_api_functions[] = /* {{{ */ { "log", "(ILjava/lang/String;)V", cjni_api_log }, + + { "getHostname", + "()Ljava/lang/String;", + cjni_api_get_hostname }, + }; static size_t jni_api_functions_num = sizeof (jni_api_functions) / sizeof (jni_api_functions[0]); @@ -1774,14 +1800,13 @@ static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ return (NULL); } - cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi)); + cbi = calloc (1, sizeof (*cbi)); if (cbi == NULL) { - ERROR ("java plugin: cjni_callback_info_create: malloc failed."); + ERROR ("java plugin: cjni_callback_info_create: calloc failed."); (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name); return (NULL); } - memset (cbi, 0, sizeof (*cbi)); cbi->type = type; cbi->name = strdup (c_name); @@ -1879,7 +1904,7 @@ static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */ pthread_mutex_lock (&java_callbacks_lock); - tmp = (cjni_callback_info_t *) realloc (java_callbacks, + tmp = realloc (java_callbacks, (java_callbacks_num + 1) * sizeof (*java_callbacks)); if (tmp == NULL) { @@ -2040,13 +2065,12 @@ static JNIEnv *cjni_thread_attach (void) /* {{{ */ if (cjni_env == NULL) { /* This pointer is free'd in `cjni_jvm_env_destroy'. */ - cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env)); + cjni_env = calloc (1, sizeof (*cjni_env)); if (cjni_env == NULL) { - ERROR ("java plugin: cjni_thread_attach: malloc failed."); + ERROR ("java plugin: cjni_thread_attach: calloc failed."); return (NULL); } - memset (cjni_env, 0, sizeof (*cjni_env)); cjni_env->reference_counter = 0; cjni_env->jvm_env = NULL; @@ -2142,7 +2166,7 @@ static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */ return (-1); } - tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1)); + tmp = realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1)); if (tmp == NULL) { ERROR ("java plugin: realloc failed."); @@ -2178,7 +2202,7 @@ static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */ if (jvm_env == NULL) return (-1); - class = (java_plugin_class_t *) realloc (java_classes_list, + class = realloc (java_classes_list, (java_classes_list_len + 1) * sizeof (*java_classes_list)); if (class == NULL) { @@ -2442,7 +2466,7 @@ static void cjni_callback_info_destroy (void *arg) /* {{{ */ cbi = (cjni_callback_info_t *) arg; - /* This condition can occurr when shutting down. */ + /* This condition can occur when shutting down. */ if (jvm == NULL) { sfree (cbi); @@ -2774,13 +2798,13 @@ static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */ /* Allocate a new callback info structure. This is going to be our user_data * pointer. */ - cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret)); + cbi_ret = calloc (1, sizeof (*cbi_ret)); if (cbi_ret == NULL) { - ERROR ("java plugin: cjni_match_target_create: malloc failed."); + ERROR ("java plugin: cjni_match_target_create: calloc failed."); BAIL_OUT (-1); } - memset (cbi_ret, 0, sizeof (*cbi_ret)); + cbi_ret->object = NULL; cbi_ret->type = type;