+ class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
+ if (class_ptr == NULL)
+ {
+ ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
+ return (-1);
+ }
+
+ /* eo == empty okay */
+#define SET_STRING(buffer,method, eo) do { \
+ status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
+ class_ptr, object_ptr, method); \
+ if (status != 0) { \
+ ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
+ method); \
+ return (-1); \
+ } } while (0)
+
+ SET_STRING (n->host, "getHost", /* empty = */ 1);
+ SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
+ SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
+ SET_STRING (n->type, "getType", /* empty = */ 1);
+ SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
+ SET_STRING (n->message, "getMessage", /* empty = */ 0);
+
+#undef SET_STRING
+
+ status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
+ if (status != 0)
+ {
+ ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
+ return (-1);
+ }
+ /* Java measures time in milliseconds. */
+ n->time = (time_t) (tmp_long / ((jlong) 1000));
+
+ status = jtoc_int (jvm_env, &tmp_int,
+ class_ptr, object_ptr, "getSeverity");
+ if (status != 0)
+ {
+ ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
+ return (-1);
+ }
+ n->severity = (int) tmp_int;
+
+ return (0);
+} /* }}} int jtoc_notification */
+/*
+ * Functions accessible from Java
+ */
+static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jobject java_vl)
+{
+ value_list_t vl = VALUE_LIST_INIT;
+ int status;
+
+ DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
+
+ status = jtoc_value_list (jvm_env, &vl, java_vl);
+ if (status != 0)
+ {
+ ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
+ return (-1);
+ }
+
+ status = plugin_dispatch_values (&vl);
+
+ sfree (vl.values);
+
+ return (status);
+} /* }}} jint cjni_api_dispatch_values */
+
+static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jobject o_notification)
+{
+ notification_t n;
+ int status;
+
+ memset (&n, 0, sizeof (n));
+ n.meta = NULL;
+
+ status = jtoc_notification (jvm_env, &n, o_notification);
+ if (status != 0)
+ {
+ ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
+ return (-1);
+ }
+
+ status = plugin_dispatch_notification (&n);
+
+ return (status);
+} /* }}} jint cjni_api_dispatch_notification */
+
+static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jobject o_string_type)
+{
+ const char *ds_name;
+ const data_set_t *ds;
+ jobject o_dataset;
+
+ ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
+ if (ds_name == NULL)
+ {
+ ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
+ return (NULL);
+ }
+
+ ds = plugin_get_ds (ds_name);
+ DEBUG ("java plugin: cjni_api_get_ds: "
+ "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
+
+ (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
+
+ if (ds == NULL)
+ return (NULL);
+
+ o_dataset = ctoj_data_set (jvm_env, ds);
+ return (o_dataset);
+} /* }}} jint cjni_api_get_ds */
+
+static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jobject o_name, jobject o_config)
+{
+ return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
+} /* }}} jint cjni_api_register_config */
+
+static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jobject o_name, jobject o_config)
+{
+ return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
+} /* }}} jint cjni_api_register_init */
+
+static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jobject o_name, jobject o_read)
+{
+ user_data_t ud;
+ cjni_callback_info_t *cbi;
+
+ cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
+ if (cbi == NULL)
+ return (-1);
+
+ DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
+
+ memset (&ud, 0, sizeof (ud));
+ ud.data = (void *) cbi;
+ ud.free_func = cjni_callback_info_destroy;
+
+ plugin_register_complex_read (cbi->name, cjni_read,
+ /* interval = */ NULL, &ud);
+
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
+
+ return (0);
+} /* }}} jint cjni_api_register_read */
+
+static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jobject o_name, jobject o_write)
+{
+ user_data_t ud;
+ cjni_callback_info_t *cbi;
+
+ cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
+ if (cbi == NULL)
+ return (-1);
+
+ DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
+
+ memset (&ud, 0, sizeof (ud));
+ ud.data = (void *) cbi;
+ ud.free_func = cjni_callback_info_destroy;
+
+ plugin_register_write (cbi->name, cjni_write, &ud);
+
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
+
+ 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)
+{
+ return (cjni_callback_register (jvm_env, o_name, o_shutdown,
+ CB_TYPE_SHUTDOWN));
+} /* }}} jint cjni_api_register_shutdown */
+
+static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jobject o_name, jobject o_log)
+{
+ user_data_t ud;
+ cjni_callback_info_t *cbi;
+
+ cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
+ if (cbi == NULL)
+ return (-1);
+
+ DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
+
+ memset (&ud, 0, sizeof (ud));
+ ud.data = (void *) cbi;
+ ud.free_func = cjni_callback_info_destroy;
+
+ plugin_register_log (cbi->name, cjni_log, &ud);
+
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
+
+ return (0);
+} /* }}} jint cjni_api_register_log */
+
+static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jobject o_name, jobject o_notification)
+{
+ user_data_t ud;
+ cjni_callback_info_t *cbi;
+
+ cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
+ CB_TYPE_NOTIFICATION);
+ if (cbi == NULL)
+ return (-1);
+
+ DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
+
+ memset (&ud, 0, sizeof (ud));
+ ud.data = (void *) cbi;
+ ud.free_func = cjni_callback_info_destroy;
+
+ plugin_register_notification (cbi->name, cjni_notification, &ud);
+
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
+
+ return (0);
+} /* }}} jint cjni_api_register_notification */
+
+static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jobject o_name, jobject o_match, int type)
+{
+ int status;
+ const char *c_name;
+
+ c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
+ if (c_name == NULL)
+ {
+ ERROR ("java plugin: cjni_api_register_match_target: "
+ "GetStringUTFChars failed.");
+ return (-1);
+ }
+
+ status = cjni_callback_register (jvm_env, o_name, o_match, type);
+ if (status != 0)
+ {
+ (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
+ return (-1);
+ }
+
+ if (type == CB_TYPE_MATCH)
+ {
+ match_proc_t m_proc;
+
+ memset (&m_proc, 0, sizeof (m_proc));
+ m_proc.create = cjni_match_target_create;
+ m_proc.destroy = cjni_match_target_destroy;
+ m_proc.match = (void *) cjni_match_target_invoke;
+
+ status = fc_register_match (c_name, m_proc);
+ }
+ else if (type == CB_TYPE_TARGET)
+ {
+ target_proc_t t_proc;
+
+ memset (&t_proc, 0, sizeof (t_proc));
+ t_proc.create = cjni_match_target_create;
+ t_proc.destroy = cjni_match_target_destroy;
+ t_proc.invoke = cjni_match_target_invoke;
+
+ status = fc_register_target (c_name, t_proc);
+ }
+ else
+ {
+ ERROR ("java plugin: cjni_api_register_match_target: "
+ "Don't know whether to create a match or a target.");
+ (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
+ return (-1);
+ }
+
+ if (status != 0)
+ {
+ ERROR ("java plugin: cjni_api_register_match_target: "
+ "%s failed.",
+ (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
+ (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
+ return (-1);
+ }
+
+ (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
+
+ return (0);
+} /* }}} jint cjni_api_register_match_target */
+
+static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jobject o_name, jobject o_match)
+{
+ return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
+ CB_TYPE_MATCH));
+} /* }}} jint cjni_api_register_match */
+
+static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jobject o_name, jobject o_target)
+{
+ return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
+ CB_TYPE_TARGET));
+} /* }}} jint cjni_api_register_target */
+
+static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
+ jobject this, jint severity, jobject o_message)
+{
+ const char *c_str;
+
+ c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
+ if (c_str == NULL)
+ {
+ ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
+ return;
+ }
+
+ if (severity < LOG_ERR)
+ severity = LOG_ERR;
+ if (severity > LOG_DEBUG)
+ severity = LOG_DEBUG;
+
+ plugin_log (severity, "%s", c_str);
+
+ (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
+} /* }}} void cjni_api_log */
+
+/* List of ``native'' functions, i. e. C-functions that can be called from
+ * Java. */
+static JNINativeMethod jni_api_functions[] = /* {{{ */
+{
+ { "dispatchValues",
+ "(Lorg/collectd/api/ValueList;)I",
+ cjni_api_dispatch_values },
+
+ { "dispatchNotification",
+ "(Lorg/collectd/api/Notification;)I",
+ cjni_api_dispatch_notification },
+
+ { "getDS",
+ "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
+ cjni_api_get_ds },
+
+ { "registerConfig",
+ "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
+ cjni_api_register_config },
+
+ { "registerInit",
+ "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
+ cjni_api_register_init },
+
+ { "registerRead",
+ "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
+ cjni_api_register_read },
+
+ { "registerWrite",
+ "(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 },
+
+ { "registerLog",
+ "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
+ cjni_api_register_log },
+
+ { "registerNotification",
+ "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
+ cjni_api_register_notification },
+
+ { "registerMatch",
+ "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
+ cjni_api_register_match },
+
+ { "registerTarget",
+ "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
+ cjni_api_register_target },
+
+ { "log",
+ "(ILjava/lang/String;)V",
+ cjni_api_log },
+};
+static size_t jni_api_functions_num = sizeof (jni_api_functions)
+ / sizeof (jni_api_functions[0]);
+/* }}} */
+
+/*
+ * Functions
+ */
+/* Allocate a `cjni_callback_info_t' given the type and objects necessary for
+ * all registration functions. */
+static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
+ jobject o_name, jobject o_callback, int type)
+{
+ const char *c_name;
+ cjni_callback_info_t *cbi;
+ const char *method_name;
+ const char *method_signature;
+
+ switch (type)
+ {
+ case CB_TYPE_CONFIG:
+ method_name = "config";
+ method_signature = "(Lorg/collectd/api/OConfigItem;)I";
+ break;
+
+ case CB_TYPE_INIT:
+ method_name = "init";
+ method_signature = "()I";
+ break;
+
+ case CB_TYPE_READ:
+ method_name = "read";
+ method_signature = "()I";
+ break;
+
+ case CB_TYPE_WRITE:
+ method_name = "write";
+ 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";
+ break;
+
+ case CB_TYPE_LOG:
+ method_name = "log";
+ method_signature = "(ILjava/lang/String;)V";
+ break;
+
+ case CB_TYPE_NOTIFICATION:
+ method_name = "notification";
+ method_signature = "(Lorg/collectd/api/Notification;)I";
+ break;
+
+ case CB_TYPE_MATCH:
+ method_name = "createMatch";
+ method_signature = "(Lorg/collectd/api/OConfigItem;)"
+ "Lorg/collectd/api/CollectdMatchInterface;";
+ break;
+
+ case CB_TYPE_TARGET:
+ method_name = "createTarget";
+ method_signature = "(Lorg/collectd/api/OConfigItem;)"
+ "Lorg/collectd/api/CollectdTargetInterface;";
+ break;
+
+ default:
+ ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
+ type);
+ return (NULL);
+ }
+
+ c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
+ if (c_name == NULL)
+ {
+ ERROR ("java plugin: cjni_callback_info_create: "
+ "GetStringUTFChars failed.");
+ return (NULL);
+ }
+
+ cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
+ if (cbi == NULL)
+ {
+ ERROR ("java plugin: cjni_callback_info_create: malloc 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);
+ if (cbi->name == NULL)
+ {
+ pthread_mutex_unlock (&java_callbacks_lock);
+ ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
+ (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
+ return (NULL);
+ }
+
+ (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
+
+ cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
+ if (cbi->object == NULL)
+ {
+ ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
+ free (cbi);
+ return (NULL);
+ }
+
+ cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
+ if (cbi->class == NULL)
+ {
+ ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
+ free (cbi);
+ return (NULL);
+ }
+
+ cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
+ method_name, method_signature);
+ if (cbi->method == NULL)
+ {
+ ERROR ("java plugin: cjni_callback_info_create: "
+ "Cannot find the `%s' method with signature `%s'.",
+ method_name, method_signature);
+ free (cbi);
+ return (NULL);
+ }
+
+ return (cbi);
+} /* }}} cjni_callback_info_t cjni_callback_info_create */
+
+/* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
+ * to the global `java_callbacks' variable. This is used for `config', `init',
+ * and `shutdown' callbacks. */
+static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
+ jobject o_name, jobject o_callback, int type)
+{
+ cjni_callback_info_t *cbi;
+ cjni_callback_info_t *tmp;
+#if COLLECT_DEBUG
+ const char *type_str;
+#endif
+
+ cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
+ if (cbi == NULL)
+ return (-1);
+
+#if COLLECT_DEBUG
+ switch (type)
+ {
+ case CB_TYPE_CONFIG:
+ type_str = "config";
+ break;
+
+ case CB_TYPE_INIT:
+ type_str = "init";
+ break;
+
+ case CB_TYPE_SHUTDOWN:
+ type_str = "shutdown";
+ break;
+
+ case CB_TYPE_MATCH:
+ type_str = "match";
+ break;
+
+ case CB_TYPE_TARGET:
+ type_str = "target";
+ break;
+
+ default:
+ type_str = "<unknown>";
+ }
+ DEBUG ("java plugin: Registering new %s callback: %s",
+ type_str, cbi->name);
+#endif
+
+ pthread_mutex_lock (&java_callbacks_lock);
+
+ tmp = (cjni_callback_info_t *) realloc (java_callbacks,
+ (java_callbacks_num + 1) * sizeof (*java_callbacks));
+ if (tmp == NULL)
+ {
+ pthread_mutex_unlock (&java_callbacks_lock);
+ ERROR ("java plugin: cjni_callback_register: realloc failed.");
+
+ (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
+ free (cbi);
+
+ return (-1);
+ }
+ java_callbacks = tmp;
+ java_callbacks[java_callbacks_num] = *cbi;
+ java_callbacks_num++;
+
+ pthread_mutex_unlock (&java_callbacks_lock);
+
+ free (cbi);
+ return (0);
+} /* }}} int cjni_callback_register */
+
+/* Callback for `pthread_key_create'. It frees the data contained in
+ * `jvm_env_key' and prints a warning if the reference counter is not zero. */
+static void cjni_jvm_env_destroy (void *args) /* {{{ */
+{
+ cjni_jvm_env_t *cjni_env;
+
+ if (args == NULL)
+ return;
+
+ cjni_env = (cjni_jvm_env_t *) args;
+
+ if (cjni_env->reference_counter > 0)
+ {
+ ERROR ("java plugin: cjni_jvm_env_destroy: "
+ "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
+ }
+
+ if (cjni_env->jvm_env != NULL)
+ {
+ ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
+ (void *) cjni_env->jvm_env);
+ }
+
+ /* The pointer is allocated in `cjni_thread_attach' */
+ free (cjni_env);
+} /* }}} void cjni_jvm_env_destroy */
+
+/* Register ``native'' functions with the JVM. Native functions are C-functions
+ * that can be called by Java code. */
+static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
+{
+ jclass api_class_ptr;
+ int status;
+
+ api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
+ if (api_class_ptr == NULL)
+ {
+ ERROR ("cjni_init_native: Cannot find API class `org/collectd/api/Collectd'.");
+ return (-1);
+ }
+
+ status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
+ jni_api_functions, (jint) jni_api_functions_num);
+ if (status != 0)
+ {
+ ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
+ return (-1);
+ }