+ ERROR ("java plugin: ctoj_data_set: Looking up the "
+ "org.collectd.api.DataSet class failed.");
+ return (NULL);
+ }
+
+ /* Search for the `DataSet (String type)' constructor. */
+ m_constructor = (*jvm_env)->GetMethodID (jvm_env,
+ c_dataset, "<init>", "(Ljava.lang.String;)V");
+ if (m_constructor == NULL)
+ {
+ ERROR ("java plugin: ctoj_data_set: Looking up the "
+ "`DataSet (String)' constructor failed.");
+ return (NULL);
+ }
+
+ /* Search for the `void addDataSource (DataSource)' method. */
+ m_add = (*jvm_env)->GetMethodID (jvm_env,
+ c_dataset, "addDataSource", "(Lorg.collectd.api.DataSource;)V");
+ if (m_add == NULL)
+ {
+ ERROR ("java plugin: ctoj_data_set: Looking up the "
+ "`addDataSource (DataSource)' method failed.");
+ return (NULL);
+ }
+
+ o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
+ if (o_type == NULL)
+ {
+ ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
+ return (NULL);
+ }
+
+ o_dataset = (*jvm_env)->NewObject (jvm_env,
+ c_dataset, m_constructor, o_type);
+ if (o_dataset == NULL)
+ {
+ ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
+ return (NULL);
+ }
+
+ /* Decrease reference counter on the java.lang.String object. */
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
+
+ for (i = 0; i < ds->ds_num; i++)
+ {
+ jobject o_datasource;
+
+ o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
+ if (o_datasource == NULL)
+ {
+ ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
+ ds->type, ds->ds[i].name);
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
+ return (NULL);
+ }
+
+ (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
+
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
+ } /* for (i = 0; i < ds->ds_num; i++) */
+
+ return (o_dataset);
+} /* }}} jobject ctoj_data_set */
+
+static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
+ value_t value, int ds_type,
+ jclass class_ptr, jobject object_ptr)
+{
+ jmethodID m_addvalue;
+ jobject o_number;
+
+ m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
+ "addValue", "(Ljava/lang/Number;)V");
+ if (m_addvalue == NULL)
+ {
+ ERROR ("java plugin: ctoj_value_list_add_value: "
+ "Cannot find method `void addValue (Number)'.");
+ return (-1);
+ }
+
+ o_number = ctoj_value_to_number (jvm_env, value, ds_type);
+ if (o_number == NULL)
+ {
+ ERROR ("java plugin: ctoj_value_list_add_value: "
+ "ctoj_value_to_number failed.");
+ return (-1);
+ }
+
+ (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
+
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
+
+ return (0);
+} /* }}} int ctoj_value_list_add_value */
+
+static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
+ jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
+{
+ jmethodID m_setdataset;
+ jobject o_dataset;
+
+ /* Look for the `void setDataSource (List<DataSource> ds)' method. */
+ m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
+ "setDataSet", "(Lorg.collectd.api.DataSet;)V");
+ if (m_setdataset == NULL)
+ {
+ ERROR ("java plugin: ctoj_value_list_add_data_set: "
+ "Cannot find the `void setDataSet (DataSet)' method.");
+ return (-1);
+ }
+
+ /* Create a DataSet object. */
+ o_dataset = ctoj_data_set (jvm_env, ds);
+ if (o_dataset == NULL)
+ {
+ ERROR ("java plugin: ctoj_value_list_add_data_set: "
+ "ctoj_data_set (%s) failed.", ds->type);
+ return (-1);
+ }
+
+ /* Actually call the method. */
+ (*jvm_env)->CallVoidMethod (jvm_env,
+ o_valuelist, m_setdataset, o_dataset);
+
+ /* Decrease reference counter on the List<DataSource> object. */
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
+
+ return (0);
+} /* }}} int ctoj_value_list_add_data_set */
+
+/* Convert a value_list_t (and data_set_t) to a org.collectd.api.ValueList */
+static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
+ const data_set_t *ds, const value_list_t *vl)
+{
+ jclass c_valuelist;
+ jmethodID m_valuelist_constructor;
+ jobject o_valuelist;
+ int status;
+ int i;
+
+ /* First, create a new ValueList instance..
+ * Look up the class.. */
+ c_valuelist = (*jvm_env)->FindClass (jvm_env,
+ "org.collectd.api.ValueList");
+ if (c_valuelist == NULL)
+ {
+ ERROR ("java plugin: ctoj_value_list: "
+ "FindClass (org.collectd.api.ValueList) failed.");
+ return (NULL);
+ }
+
+ /* Lookup the `ValueList ()' constructor. */
+ m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
+ "<init>", "()V");
+ if (m_valuelist_constructor == NULL)
+ {
+ ERROR ("java plugin: ctoj_value_list: Cannot find the "
+ "`ValueList ()' constructor.");
+ return (NULL);
+ }
+
+ /* Create a new instance. */
+ o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
+ m_valuelist_constructor);
+ if (o_valuelist == NULL)
+ {
+ ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
+ "failed.");
+ return (NULL);
+ }
+
+ status = ctoj_value_list_add_data_set (jvm_env,
+ c_valuelist, o_valuelist, ds);
+ if (status != 0)
+ {
+ ERROR ("java plugin: ctoj_value_list: "
+ "ctoj_value_list_add_data_set failed.");
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
+ return (NULL);
+ }
+
+ /* Set the strings.. */
+#define SET_STRING(str,method_name) do { \
+ status = ctoj_string (jvm_env, str, \
+ c_valuelist, o_valuelist, method_name); \
+ if (status != 0) { \
+ ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
+ method_name); \
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
+ return (NULL); \
+ } } while (0)
+
+ SET_STRING (vl->host, "setHost");
+ SET_STRING (vl->plugin, "setPlugin");
+ SET_STRING (vl->plugin_instance, "setPluginInstance");
+ SET_STRING (vl->type, "setType");
+ SET_STRING (vl->type_instance, "setTypeInstance");
+
+#undef SET_STRING
+
+ /* Set the `time' member. Java stores time in milliseconds. */
+ status = ctoj_long (jvm_env, ((jlong) vl->time) * ((jlong) 1000),
+ c_valuelist, o_valuelist, "setTime");
+ if (status != 0)
+ {
+ ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
+ return (NULL);
+ }
+
+ /* Set the `interval' member.. */
+ status = ctoj_long (jvm_env, (jlong) vl->interval,
+ c_valuelist, o_valuelist, "setInterval");
+ if (status != 0)
+ {
+ ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
+ return (NULL);
+ }
+
+ for (i = 0; i < vl->values_len; i++)
+ {
+ status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
+ c_valuelist, o_valuelist);
+ if (status != 0)
+ {
+ ERROR ("java plugin: ctoj_value_list: "
+ "ctoj_value_list_add_value failed.");
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
+ return (NULL);
+ }
+ }
+
+ return (o_valuelist);
+} /* }}} jobject ctoj_value_list */
+
+/* Convert a notification_t to a org.collectd.api.Notification */
+static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
+ const notification_t *n)
+{
+ jclass c_notification;
+ jmethodID m_constructor;
+ jobject o_notification;
+ int status;
+
+ /* First, create a new Notification instance..
+ * Look up the class.. */
+ c_notification = (*jvm_env)->FindClass (jvm_env,
+ "org.collectd.api.Notification");
+ if (c_notification == NULL)
+ {
+ ERROR ("java plugin: ctoj_notification: "
+ "FindClass (org.collectd.api.Notification) failed.");
+ return (NULL);
+ }
+
+ /* Lookup the `Notification ()' constructor. */
+ m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
+ "<init>", "()V");
+ if (m_constructor == NULL)
+ {
+ ERROR ("java plugin: ctoj_notification: Cannot find the "
+ "`Notification ()' constructor.");
+ return (NULL);
+ }
+
+ /* Create a new instance. */
+ o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
+ m_constructor);
+ if (o_notification == NULL)
+ {
+ ERROR ("java plugin: ctoj_notification: Creating a new Notification "
+ "instance failed.");
+ return (NULL);
+ }
+
+ /* Set the strings.. */
+#define SET_STRING(str,method_name) do { \
+ status = ctoj_string (jvm_env, str, \
+ c_notification, o_notification, method_name); \
+ if (status != 0) { \
+ ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
+ method_name); \
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
+ return (NULL); \
+ } } while (0)
+
+ SET_STRING (n->host, "setHost");
+ SET_STRING (n->plugin, "setPlugin");
+ SET_STRING (n->plugin_instance, "setPluginInstance");
+ SET_STRING (n->type, "setType");
+ SET_STRING (n->type_instance, "setTypeInstance");
+ SET_STRING (n->message, "setMessage");
+
+#undef SET_STRING
+
+ /* Set the `time' member. Java stores time in milliseconds. */
+ status = ctoj_long (jvm_env, ((jlong) n->time) * ((jlong) 1000),
+ c_notification, o_notification, "setTime");
+ if (status != 0)
+ {
+ ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
+ return (NULL);
+ }
+
+ /* Set the `interval' member.. */
+ status = ctoj_int (jvm_env, (jint) n->severity,
+ c_notification, o_notification, "setSeverity");
+ if (status != 0)
+ {
+ ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
+ return (NULL);
+ }
+
+ return (o_notification);
+} /* }}} jobject ctoj_notification */
+
+/*
+ * Java to C conversion functions
+ */
+/* Call a `String <method> ()' method. */
+static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
+ char *buffer, size_t buffer_size, int empty_okay,
+ jclass class_ptr, jobject object_ptr, const char *method_name)
+{
+ jmethodID method_id;
+ jobject string_obj;
+ const char *c_str;
+
+ method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
+ method_name, "()Ljava/lang/String;");
+ if (method_id == NULL)
+ {
+ ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
+ method_name);
+ return (-1);
+ }
+
+ string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
+ if ((string_obj == NULL) && (empty_okay == 0))
+ {
+ ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
+ method_name);
+ return (-1);
+ }
+ else if ((string_obj == NULL) && (empty_okay != 0))
+ {
+ memset (buffer, 0, buffer_size);
+ return (0);
+ }
+
+ c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
+ if (c_str == NULL)
+ {
+ ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
+ (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
+ return (-1);
+ }
+
+ sstrncpy (buffer, c_str, buffer_size);
+
+ (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
+ (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
+
+ return (0);
+} /* }}} int jtoc_string */
+
+/* Call an `int <method> ()' method. */
+static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
+ jint *ret_value,
+ jclass class_ptr, jobject object_ptr, const char *method_name)
+{
+ jmethodID method_id;
+
+ method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
+ method_name, "()I");
+ if (method_id == NULL)
+ {
+ ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
+ method_name);
+ return (-1);
+ }
+
+ *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
+
+ return (0);
+} /* }}} int jtoc_int */
+
+/* Call a `long <method> ()' method. */
+static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
+ jlong *ret_value,
+ jclass class_ptr, jobject object_ptr, const char *method_name)
+{
+ jmethodID method_id;
+
+ method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
+ method_name, "()J");
+ if (method_id == NULL)
+ {
+ ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
+ method_name);
+ return (-1);
+ }
+
+ *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
+
+ return (0);
+} /* }}} int jtoc_long */
+
+/* Call a `double <method> ()' method. */
+static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
+ jdouble *ret_value,
+ jclass class_ptr, jobject object_ptr, const char *method_name)
+{
+ jmethodID method_id;
+
+ method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
+ method_name, "()D");
+ if (method_id == NULL)
+ {
+ ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
+ method_name);
+ return (-1);
+ }
+
+ *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
+
+ return (0);
+} /* }}} int jtoc_double */
+
+static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
+ value_t *ret_value, int ds_type, jobject object_ptr)
+{
+ jclass class_ptr;
+ int status;
+
+ class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
+
+ if (ds_type == DS_TYPE_COUNTER)
+ {
+ jlong tmp_long;
+
+ status = jtoc_long (jvm_env, &tmp_long,
+ class_ptr, object_ptr, "longValue");
+ if (status != 0)
+ {
+ ERROR ("java plugin: jtoc_value: "
+ "jtoc_long failed.");
+ return (-1);
+ }
+ (*ret_value).counter = (counter_t) tmp_long;
+ }
+ else
+ {
+ jdouble tmp_double;
+
+ status = jtoc_double (jvm_env, &tmp_double,
+ class_ptr, object_ptr, "doubleValue");
+ if (status != 0)
+ {
+ ERROR ("java plugin: jtoc_value: "
+ "jtoc_double failed.");
+ return (-1);
+ }
+ (*ret_value).gauge = (gauge_t) tmp_double;
+ }
+
+ return (0);
+} /* }}} int jtoc_value */
+
+/* Read a List<Number>, convert it to `value_t' and add it to the given
+ * `value_list_t'. */
+static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
+ const data_set_t *ds, value_list_t *vl,
+ jclass class_ptr, jobject object_ptr)
+{
+ jmethodID m_getvalues;
+ jmethodID m_toarray;
+ jobject o_list;
+ jobjectArray o_number_array;
+
+ value_t *values;
+ int values_num;
+ int i;
+
+ values_num = ds->ds_num;
+
+ values = NULL;
+ o_number_array = NULL;
+ o_list = NULL;
+
+#define BAIL_OUT(status) \
+ free (values); \
+ if (o_number_array != NULL) \
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
+ if (o_list != NULL) \
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
+ return (status);
+
+ /* Call: List<Number> ValueList.getValues () */
+ m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
+ "getValues", "()Ljava/util/List;");
+ if (m_getvalues == NULL)
+ {
+ ERROR ("java plugin: jtoc_values_array: "
+ "Cannot find method `List getValues ()'.");
+ BAIL_OUT (-1);
+ }
+
+ o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
+ if (o_list == NULL)
+ {
+ ERROR ("java plugin: jtoc_values_array: "
+ "CallObjectMethod (getValues) failed.");
+ BAIL_OUT (-1);
+ }
+
+ /* Call: Number[] List.toArray () */
+ m_toarray = (*jvm_env)->GetMethodID (jvm_env,
+ (*jvm_env)->GetObjectClass (jvm_env, o_list),
+ "toArray", "()[Ljava/lang/Object;");
+ if (m_toarray == NULL)
+ {
+ ERROR ("java plugin: jtoc_values_array: "
+ "Cannot find method `Object[] toArray ()'.");
+ BAIL_OUT (-1);
+ }
+
+ o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
+ if (o_number_array == NULL)
+ {
+ ERROR ("java plugin: jtoc_values_array: "
+ "CallObjectMethod (toArray) failed.");
+ BAIL_OUT (-1);
+ }
+
+ values = (value_t *) calloc (values_num, sizeof (value_t));
+ if (values == NULL)
+ {
+ ERROR ("java plugin: jtoc_values_array: calloc failed.");
+ BAIL_OUT (-1);
+ }
+
+ for (i = 0; i < values_num; i++)
+ {
+ jobject o_number;
+ int status;
+
+ o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
+ o_number_array, (jsize) i);
+ if (o_number == NULL)
+ {
+ ERROR ("java plugin: jtoc_values_array: "
+ "GetObjectArrayElement (%i) failed.", i);
+ BAIL_OUT (-1);
+ }
+
+ status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
+ if (status != 0)
+ {
+ ERROR ("java plugin: jtoc_values_array: "
+ "jtoc_value (%i) failed.", i);
+ BAIL_OUT (-1);
+ }
+ } /* for (i = 0; i < values_num; i++) */
+
+ vl->values = values;
+ vl->values_len = values_num;
+
+#undef BAIL_OUT
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
+ (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
+ return (0);
+} /* }}} int jtoc_values_array */
+
+/* Convert a org.collectd.api.ValueList to a value_list_t. */
+static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
+ jobject object_ptr)
+{
+ jclass class_ptr;
+ int status;
+ jlong tmp_long;
+ const data_set_t *ds;
+
+ class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
+ if (class_ptr == NULL)
+ {
+ ERROR ("java plugin: jtoc_value_list: 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_value_list: jtoc_string (%s) failed.", \
+ method); \
+ return (-1); \
+ } } while (0)
+
+ SET_STRING(vl->type, "getType", /* empty = */ 0);
+
+ ds = plugin_get_ds (vl->type);
+ if (ds == NULL)
+ {
+ ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
+ "Please consult the types.db(5) manpage for mor information.",
+ vl->type);
+ return (-1);
+ }
+
+ SET_STRING(vl->host, "getHost", /* empty = */ 0);
+ SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
+ SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
+ SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
+
+#undef SET_STRING
+
+ status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
+ if (status != 0)
+ {
+ ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
+ return (-1);
+ }
+ /* Java measures time in milliseconds. */
+ vl->time = (time_t) (tmp_long / ((jlong) 1000));
+
+ status = jtoc_long (jvm_env, &tmp_long,
+ class_ptr, object_ptr, "getInterval");
+ if (status != 0)
+ {
+ ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
+ return (-1);
+ }
+ vl->interval = (int) tmp_long;
+
+ status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
+ if (status != 0)
+ {
+ ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
+ return (-1);
+ }
+
+ return (0);
+} /* }}} int jtoc_value_list */
+
+/* Convert a org.collectd.api.Notification to a notification_t. */
+static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
+ jobject object_ptr)
+{
+ jclass class_ptr;
+ int status;
+ jlong tmp_long;
+ jint tmp_int;
+
+ 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, &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->class = (*jvm_env)->GetObjectClass (jvm_env, o_callback);
+ if (cbi->class == NULL)
+ {
+ ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
+ free (cbi);
+ return (NULL);
+ }
+
+ cbi->object = o_callback;
+
+ 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);
+ }
+
+ (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
+
+ 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];
+ }
+ /*
+ vm_args.options[0].optionString = "-verbose:jni";
+ vm_args.options[1].optionString = "-Djava.class.path=/home/octo/collectd/bindings/java";
+ */
+
+ 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;
+
+ 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);
+ }
+
+ class->object = (*jvm_env)->NewObject (jvm_env, class->class,
+ constructor_id);
+ 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);
+ }
+
+ (*jvm_env)->NewGlobalRef (jvm_env, class->object);
+ 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;
+
+ 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);
+ }
+
+ status = (*jvm_env)->CallIntMethod (jvm_env,
+ cbi->object, cbi->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;
+
+ if (strcasecmp ("JVMArg", child->key) == 0)
+ {
+ status = cjni_config_add_jvm_arg (child);
+ if (status == 0)
+ success++;
+ else
+ errors++;
+ }
+ else if (strcasecmp ("LoadPlugin", child->key) == 0)
+ {
+ status = cjni_config_load_plugin (child);
+ if (status == 0)
+ success++;
+ else
+ errors++;
+ }
+ else if (strcasecmp ("Plugin", child->key) == 0)
+ {
+ status = cjni_config_plugin_block (child);
+ if (status == 0)
+ success++;
+ else
+ errors++;
+ }
+ else
+ {
+ WARNING ("java plugin: Option `%s' not allowed here.", child->key);
+ errors++;
+ }
+ }
+
+ DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
+ DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
+
+ if ((success == 0) && (errors > 0))
+ {
+ ERROR ("java plugin: All statements failed.");
+ return (-1);
+ }
+
+ return (0);
+} /* }}} int cjni_config */
+
+/* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
+ * and `cjni_write'. In particular, delete the global reference to the Java
+ * object. */
+static void cjni_callback_info_destroy (void *arg) /* {{{ */
+{
+ JNIEnv *jvm_env;
+ cjni_callback_info_t *cbi;
+
+ DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
+
+ cbi = (cjni_callback_info_t *) arg;
+
+ /* This condition can occurr when shutting down. */
+ if (jvm == NULL)
+ {
+ sfree (cbi);
+ return;
+ }
+
+ if (arg == NULL)
+ return;
+
+ jvm_env = cjni_thread_attach ();
+ if (jvm_env == NULL)
+ {
+ ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
+ return;
+ }
+
+ (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
+
+ cbi->method = NULL;
+ cbi->object = NULL;
+ cbi->class = NULL;
+ free (cbi);
+
+ cjni_thread_detach ();
+} /* }}} void cjni_callback_info_destroy */
+
+/* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
+static int cjni_read (user_data_t *ud) /* {{{ */
+{
+ JNIEnv *jvm_env;
+ cjni_callback_info_t *cbi;
+ int status;
+ int ret_status;
+
+ if (jvm == NULL)
+ {
+ ERROR ("java plugin: cjni_read: jvm == NULL");