+ 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);
+ }
+
+ return (0);
+} /* }}} int cjni_init_native */
+
+/* Create the JVM. This is called when the first thread tries to access the JVM
+ * via cjni_thread_attach. */
+static int cjni_create_jvm (void) /* {{{ */
+{
+ JNIEnv *jvm_env;
+ JavaVMInitArgs vm_args;
+ JavaVMOption vm_options[jvm_argc];
+
+ int status;
+ size_t i;
+
+ if (jvm != NULL)
+ return (0);
+
+ status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
+ if (status != 0)
+ {
+ ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
+ "with status %i.", status);
+ return (-1);
+ }
+
+ jvm_env = NULL;
+
+ memset (&vm_args, 0, sizeof (vm_args));
+ vm_args.version = JNI_VERSION_1_2;
+ vm_args.options = vm_options;
+ vm_args.nOptions = (jint) jvm_argc;
+
+ for (i = 0; i < jvm_argc; i++)
+ {
+ DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
+ i, jvm_argv[i]);
+ vm_args.options[i].optionString = jvm_argv[i];
+ }
+
+ status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
+ if (status != 0)
+ {
+ ERROR ("java plugin: cjni_create_jvm: "
+ "JNI_CreateJavaVM failed with status %i.",
+ status);
+ return (-1);
+ }
+ assert (jvm != NULL);
+ assert (jvm_env != NULL);
+
+ /* Call RegisterNatives */
+ status = cjni_init_native (jvm_env);
+ if (status != 0)
+ {
+ ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
+ return (-1);
+ }
+
+ DEBUG ("java plugin: The JVM has been created.");
+ return (0);
+} /* }}} int cjni_create_jvm */
+
+/* Increase the reference counter to the JVM for this thread. If it was zero,
+ * attach the JVM first. */
+static JNIEnv *cjni_thread_attach (void) /* {{{ */
+{
+ cjni_jvm_env_t *cjni_env;
+ JNIEnv *jvm_env;
+
+ /* If we're the first thread to access the JVM, we'll have to create it
+ * first.. */
+ if (jvm == NULL)
+ {
+ int status;
+
+ status = cjni_create_jvm ();
+ if (status != 0)
+ {
+ ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
+ return (NULL);
+ }
+ }
+ assert (jvm != NULL);
+
+ cjni_env = pthread_getspecific (jvm_env_key);
+ if (cjni_env == NULL)
+ {
+ /* This pointer is free'd in `cjni_jvm_env_destroy'. */
+ cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
+ if (cjni_env == NULL)
+ {
+ ERROR ("java plugin: cjni_thread_attach: malloc failed.");
+ return (NULL);
+ }
+ memset (cjni_env, 0, sizeof (*cjni_env));
+ cjni_env->reference_counter = 0;
+ cjni_env->jvm_env = NULL;
+
+ pthread_setspecific (jvm_env_key, cjni_env);
+ }
+
+ if (cjni_env->reference_counter > 0)
+ {
+ cjni_env->reference_counter++;
+ jvm_env = cjni_env->jvm_env;
+ }
+ else
+ {
+ int status;
+ JavaVMAttachArgs args;
+
+ assert (cjni_env->jvm_env == NULL);
+
+ memset (&args, 0, sizeof (args));
+ args.version = JNI_VERSION_1_2;
+
+ status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
+ if (status != 0)
+ {
+ ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
+ "with status %i.", status);
+ return (NULL);
+ }
+
+ cjni_env->reference_counter = 1;
+ cjni_env->jvm_env = jvm_env;
+ }
+
+ DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
+ cjni_env->reference_counter);
+ assert (jvm_env != NULL);
+ return (jvm_env);
+} /* }}} JNIEnv *cjni_thread_attach */
+
+/* Decrease the reference counter of this thread. If it reaches zero, detach
+ * from the JVM. */
+static int cjni_thread_detach (void) /* {{{ */
+{
+ cjni_jvm_env_t *cjni_env;
+ int status;
+
+ cjni_env = pthread_getspecific (jvm_env_key);
+ if (cjni_env == NULL)
+ {
+ ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
+ return (-1);
+ }
+
+ assert (cjni_env->reference_counter > 0);
+ assert (cjni_env->jvm_env != NULL);
+
+ cjni_env->reference_counter--;
+ DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
+ cjni_env->reference_counter);
+
+ if (cjni_env->reference_counter > 0)
+ return (0);
+
+ status = (*jvm)->DetachCurrentThread (jvm);
+ if (status != 0)
+ {
+ ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
+ "with status %i.", status);
+ }
+
+ cjni_env->reference_counter = 0;
+ cjni_env->jvm_env = NULL;
+
+ return (0);
+} /* }}} JNIEnv *cjni_thread_attach */
+
+static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
+{
+ char **tmp;
+
+ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ {
+ WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
+ return (-1);
+ }
+
+ if (jvm != NULL)
+ {
+ ERROR ("java plugin: All `JVMArg' options MUST appear before all "
+ "`LoadPlugin' options! The JVM is already started and I have to "
+ "ignore this argument: %s",
+ ci->values[0].value.string);
+ return (-1);
+ }
+
+ tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
+ if (tmp == NULL)
+ {
+ ERROR ("java plugin: realloc failed.");
+ return (-1);
+ }
+ jvm_argv = tmp;
+
+ jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
+ if (jvm_argv[jvm_argc] == NULL)
+ {
+ ERROR ("java plugin: strdup failed.");
+ return (-1);
+ }
+ jvm_argc++;
+
+ return (0);
+} /* }}} int cjni_config_add_jvm_arg */
+
+static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
+{
+ JNIEnv *jvm_env;
+ java_plugin_class_t *class;
+ jmethodID constructor_id;
+ jobject tmp_object;
+
+ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ {
+ WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
+ return (-1);
+ }
+
+ jvm_env = cjni_thread_attach ();
+ if (jvm_env == NULL)
+ return (-1);
+
+ class = (java_plugin_class_t *) realloc (java_classes_list,
+ (java_classes_list_len + 1) * sizeof (*java_classes_list));
+ if (class == NULL)
+ {
+ ERROR ("java plugin: realloc failed.");
+ cjni_thread_detach ();
+ return (-1);
+ }
+ java_classes_list = class;
+ class = java_classes_list + java_classes_list_len;
+
+ memset (class, 0, sizeof (*class));
+ class->name = strdup (ci->values[0].value.string);
+ if (class->name == NULL)
+ {
+ ERROR ("java plugin: strdup failed.");
+ cjni_thread_detach ();
+ return (-1);
+ }
+ class->class = NULL;
+ class->object = NULL;
+
+ DEBUG ("java plugin: Loading class %s", class->name);
+
+ class->class = (*jvm_env)->FindClass (jvm_env, class->name);
+ if (class->class == NULL)
+ {
+ ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
+ class->name);
+ cjni_thread_detach ();
+ free (class->name);
+ return (-1);
+ }
+
+ constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
+ "<init>", "()V");
+ if (constructor_id == NULL)
+ {
+ ERROR ("java plugin: cjni_config_load_plugin: "
+ "Could not find the constructor for `%s'.",
+ class->name);
+ cjni_thread_detach ();
+ free (class->name);
+ return (-1);
+ }
+
+ tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
+ constructor_id);
+ if (tmp_object != NULL)
+ class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
+ else
+ class->object = NULL;
+ if (class->object == NULL)
+ {
+ ERROR ("java plugin: cjni_config_load_plugin: "
+ "Could create a new `%s' object.",
+ class->name);
+ cjni_thread_detach ();
+ free (class->name);
+ return (-1);
+ }
+
+ cjni_thread_detach ();
+
+ java_classes_list_len++;
+
+ return (0);
+} /* }}} int cjni_config_load_plugin */
+
+static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
+{
+ JNIEnv *jvm_env;
+ cjni_callback_info_t *cbi;
+ jobject o_ocitem;
+ const char *name;
+ int status;
+ size_t i;
+
+ jclass class;
+ jmethodID method;
+
+ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ {
+ WARNING ("java plugin: `Plugin' blocks "
+ "need exactly one string argument.");
+ return (-1);
+ }
+
+ name = ci->values[0].value.string;
+
+ cbi = NULL;
+ for (i = 0; i < java_callbacks_num; i++)
+ {
+ if (java_callbacks[i].type != CB_TYPE_CONFIG)
+ continue;
+
+ if (strcmp (name, java_callbacks[i].name) != 0)
+ continue;
+
+ cbi = java_callbacks + i;
+ break;
+ }
+
+ if (cbi == NULL)
+ {
+ NOTICE ("java plugin: Configuration block for `%s' found, but no such "
+ "configuration callback has been registered. Please make sure, the "
+ "`LoadPlugin' lines precede the `Plugin' blocks.",
+ name);
+ return (0);
+ }
+
+ DEBUG ("java plugin: Configuring %s", name);
+
+ jvm_env = cjni_thread_attach ();
+ if (jvm_env == NULL)
+ return (-1);
+
+ o_ocitem = ctoj_oconfig_item (jvm_env, ci);
+ if (o_ocitem == NULL)
+ {
+ ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
+ cjni_thread_detach ();
+ return (-1);
+ }
+
+ class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
+ method = (*jvm_env)->GetMethodID (jvm_env, class,
+ "config", "(Lorg/collectd/api/OConfigItem;)I");
+
+ status = (*jvm_env)->CallIntMethod (jvm_env,
+ cbi->object, method, o_ocitem);
+
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
+ cjni_thread_detach ();
+ return (0);
+} /* }}} int cjni_config_plugin_block */
+
+static int cjni_config (oconfig_item_t *ci) /* {{{ */
+{
+ int success;
+ int errors;
+ int status;
+ int i;
+
+ success = 0;
+ errors = 0;
+
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child = ci->children + i;