From f554ef29b3a20ff058878a3940e97d1dc21f1df0 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Ritschard Date: Tue, 29 Jul 2014 14:37:05 +0200 Subject: [PATCH] Bring in hostname as defined in the main configuration by default supersedes collectd/collectd#530 --- bindings/java/org/collectd/api/Collectd.java | 7 +++++ .../collectd/java/GenericJMXConfConnection.java | 10 +------ src/java.c | 31 ++++++++++++++++++++-- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/bindings/java/org/collectd/api/Collectd.java b/bindings/java/org/collectd/api/Collectd.java index 84e65926..e962f6e0 100644 --- a/bindings/java/org/collectd/api/Collectd.java +++ b/bindings/java/org/collectd/api/Collectd.java @@ -252,6 +252,13 @@ public class Collectd native private static void log (int severity, String message); /** + * Yield contents of collectd/src/collectd.h:hostname_g + * + * @return The hostname as set in the collectd configuration. + */ + native public static java.lang.String getHostname (); + + /** * Prints an error message. */ public static void logError (String message) diff --git a/bindings/java/org/collectd/java/GenericJMXConfConnection.java b/bindings/java/org/collectd/java/GenericJMXConfConnection.java index 99b140a2..186d1446 100644 --- a/bindings/java/org/collectd/java/GenericJMXConfConnection.java +++ b/bindings/java/org/collectd/java/GenericJMXConfConnection.java @@ -86,15 +86,7 @@ class GenericJMXConfConnection return (this._host); } - try - { - InetAddress localHost = InetAddress.getLocalHost(); - return (localHost.getHostName ()); - } - catch (UnknownHostException e) - { - return ("localhost"); - } + return Collectd.getHostname(); } /* }}} String getHost */ private void connect () /* {{{ */ diff --git a/src/java.c b/src/java.c index e4b10beb..2b6a5427 100644 --- a/src/java.c +++ b/src/java.c @@ -120,7 +120,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 +159,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) @@ -1319,7 +1336,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, /* {{{ */ @@ -1629,6 +1646,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 +1710,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]); -- 2.11.0