2 * collectd - src/java.c
3 * Copyright (C) 2009 Florian octo Forster
4 * Copyright (C) 2008 Justo Alonso Achaques
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at collectd.org>
21 * Justo Alonso Achaques <justo.alonso at gmail.com>
27 #include "filter_chain.h"
32 #if !defined(JNI_VERSION_1_2)
33 #error "Need JNI 1.2 compatible interface!"
39 struct cjni_jvm_env_s /* {{{ */
42 int reference_counter;
44 typedef struct cjni_jvm_env_s cjni_jvm_env_t;
47 struct java_plugin_class_s /* {{{ */
53 typedef struct java_plugin_class_s java_plugin_class_t;
56 #define CB_TYPE_CONFIG 1
57 #define CB_TYPE_INIT 2
58 #define CB_TYPE_READ 3
59 #define CB_TYPE_WRITE 4
60 #define CB_TYPE_FLUSH 5
61 #define CB_TYPE_SHUTDOWN 6
63 #define CB_TYPE_NOTIFICATION 8
64 #define CB_TYPE_MATCH 9
65 #define CB_TYPE_TARGET 10
66 struct cjni_callback_info_s /* {{{ */
74 typedef struct cjni_callback_info_s cjni_callback_info_t;
81 static pthread_key_t jvm_env_key;
83 /* Configuration options for the JVM. */
84 static char **jvm_argv;
85 static size_t jvm_argc;
87 /* List of class names to load */
88 static java_plugin_class_t *java_classes_list;
89 static size_t java_classes_list_len;
91 /* List of config, init, and shutdown callbacks. */
92 static cjni_callback_info_t *java_callbacks;
93 static size_t java_callbacks_num;
94 static pthread_mutex_t java_callbacks_lock = PTHREAD_MUTEX_INITIALIZER;
96 static oconfig_item_t *config_block;
101 * Mostly functions that are needed by the Java interface (``native'')
104 static void cjni_callback_info_destroy(void *arg);
105 static cjni_callback_info_t *cjni_callback_info_create(JNIEnv *jvm_env,
109 static int cjni_callback_register(JNIEnv *jvm_env, jobject o_name,
110 jobject o_callback, int type);
111 static int cjni_read(user_data_t *user_data);
112 static int cjni_write(const data_set_t *ds, const value_list_t *vl,
114 static int cjni_flush(cdtime_t timeout, const char *identifier,
116 static void cjni_log(int severity, const char *message, user_data_t *ud);
117 static int cjni_notification(const notification_t *n, user_data_t *ud);
119 /* Create, destroy, and match/invoke functions, used by both, matches AND
121 static int cjni_match_target_create(const oconfig_item_t *ci, void **user_data);
122 static int cjni_match_target_destroy(void **user_data);
123 static int cjni_match_target_invoke(const data_set_t *ds, value_list_t *vl,
124 notification_meta_t **meta,
128 * C to Java conversion functions
130 static int ctoj_string(JNIEnv *jvm_env, /* {{{ */
131 const char *string, jclass class_ptr, jobject object_ptr,
132 const char *method_name) {
136 /* Create a java.lang.String */
137 o_string = (*jvm_env)->NewStringUTF(jvm_env, (string != NULL) ? string : "");
138 if (o_string == NULL) {
139 ERROR("java plugin: ctoj_string: NewStringUTF failed.");
143 /* Search for the `void setFoo (String s)' method. */
144 m_set = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name,
145 "(Ljava/lang/String;)V");
147 ERROR("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
149 (*jvm_env)->DeleteLocalRef(jvm_env, o_string);
153 /* Call the method. */
154 (*jvm_env)->CallVoidMethod(jvm_env, object_ptr, m_set, o_string);
156 /* Decrease reference counter on the java.lang.String object. */
157 (*jvm_env)->DeleteLocalRef(jvm_env, o_string);
160 } /* }}} int ctoj_string */
162 static jstring ctoj_output_string(JNIEnv *jvm_env, /* {{{ */
163 const char *string) {
166 /* Create a java.lang.String */
167 o_string = (*jvm_env)->NewStringUTF(jvm_env, (string != NULL) ? string : "");
168 if (o_string == NULL) {
169 ERROR("java plugin: ctoj_output_string: NewStringUTF failed.");
174 } /* }}} int ctoj_output_string */
176 static int ctoj_int(JNIEnv *jvm_env, /* {{{ */
177 jint value, jclass class_ptr, jobject object_ptr,
178 const char *method_name) {
181 /* Search for the `void setFoo (int i)' method. */
182 m_set = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "(I)V");
184 ERROR("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
189 (*jvm_env)->CallVoidMethod(jvm_env, object_ptr, m_set, value);
192 } /* }}} int ctoj_int */
194 static int ctoj_long(JNIEnv *jvm_env, /* {{{ */
195 jlong value, jclass class_ptr, jobject object_ptr,
196 const char *method_name) {
199 /* Search for the `void setFoo (long l)' method. */
200 m_set = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "(J)V");
202 ERROR("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
207 (*jvm_env)->CallVoidMethod(jvm_env, object_ptr, m_set, value);
210 } /* }}} int ctoj_long */
212 static int ctoj_double(JNIEnv *jvm_env, /* {{{ */
213 jdouble value, jclass class_ptr, jobject object_ptr,
214 const char *method_name) {
217 /* Search for the `void setFoo (double d)' method. */
218 m_set = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "(D)V");
220 ERROR("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
225 (*jvm_env)->CallVoidMethod(jvm_env, object_ptr, m_set, value);
228 } /* }}} int ctoj_double */
230 /* Convert a jlong to a java.lang.Number */
231 static jobject ctoj_jlong_to_number(JNIEnv *jvm_env, jlong value) /* {{{ */
234 jmethodID m_long_constructor;
236 /* Look up the java.lang.Long class */
237 c_long = (*jvm_env)->FindClass(jvm_env, "java/lang/Long");
238 if (c_long == NULL) {
239 ERROR("java plugin: ctoj_jlong_to_number: Looking up the "
240 "java.lang.Long class failed.");
245 (*jvm_env)->GetMethodID(jvm_env, c_long, "<init>", "(J)V");
246 if (m_long_constructor == NULL) {
247 ERROR("java plugin: ctoj_jlong_to_number: Looking up the "
248 "`Long (long)' constructor failed.");
252 return (*jvm_env)->NewObject(jvm_env, c_long, m_long_constructor, value);
253 } /* }}} jobject ctoj_jlong_to_number */
255 /* Convert a jdouble to a java.lang.Number */
256 static jobject ctoj_jdouble_to_number(JNIEnv *jvm_env, jdouble value) /* {{{ */
259 jmethodID m_double_constructor;
261 /* Look up the java.lang.Long class */
262 c_double = (*jvm_env)->FindClass(jvm_env, "java/lang/Double");
263 if (c_double == NULL) {
264 ERROR("java plugin: ctoj_jdouble_to_number: Looking up the "
265 "java.lang.Double class failed.");
269 m_double_constructor =
270 (*jvm_env)->GetMethodID(jvm_env, c_double, "<init>", "(D)V");
271 if (m_double_constructor == NULL) {
272 ERROR("java plugin: ctoj_jdouble_to_number: Looking up the "
273 "`Double (double)' constructor failed.");
277 return (*jvm_env)->NewObject(jvm_env, c_double, m_double_constructor, value);
278 } /* }}} jobject ctoj_jdouble_to_number */
280 /* Convert a value_t to a java.lang.Number */
281 static jobject ctoj_value_to_number(JNIEnv *jvm_env, /* {{{ */
282 value_t value, int ds_type) {
283 if (ds_type == DS_TYPE_COUNTER)
284 return ctoj_jlong_to_number(jvm_env, (jlong)value.counter);
285 else if (ds_type == DS_TYPE_GAUGE)
286 return ctoj_jdouble_to_number(jvm_env, (jdouble)value.gauge);
287 if (ds_type == DS_TYPE_DERIVE)
288 return ctoj_jlong_to_number(jvm_env, (jlong)value.derive);
289 if (ds_type == DS_TYPE_ABSOLUTE)
290 return ctoj_jlong_to_number(jvm_env, (jlong)value.absolute);
293 } /* }}} jobject ctoj_value_to_number */
295 /* Convert a data_source_t to a org/collectd/api/DataSource */
296 static jobject ctoj_data_source(JNIEnv *jvm_env, /* {{{ */
297 const data_source_t *dsrc) {
299 jmethodID m_datasource_constructor;
300 jobject o_datasource;
303 /* Look up the DataSource class */
304 c_datasource = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/DataSource");
305 if (c_datasource == NULL) {
306 ERROR("java plugin: ctoj_data_source: "
307 "FindClass (org/collectd/api/DataSource) failed.");
311 /* Lookup the `ValueList ()' constructor. */
312 m_datasource_constructor =
313 (*jvm_env)->GetMethodID(jvm_env, c_datasource, "<init>", "()V");
314 if (m_datasource_constructor == NULL) {
315 ERROR("java plugin: ctoj_data_source: Cannot find the "
316 "`DataSource ()' constructor.");
320 /* Create a new instance. */
322 (*jvm_env)->NewObject(jvm_env, c_datasource, m_datasource_constructor);
323 if (o_datasource == NULL) {
324 ERROR("java plugin: ctoj_data_source: "
325 "Creating a new DataSource instance failed.");
329 /* Set name via `void setName (String name)' */
331 ctoj_string(jvm_env, dsrc->name, c_datasource, o_datasource, "setName");
333 ERROR("java plugin: ctoj_data_source: "
334 "ctoj_string (setName) failed.");
335 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
339 /* Set type via `void setType (int type)' */
340 status = ctoj_int(jvm_env, dsrc->type, c_datasource, o_datasource, "setType");
342 ERROR("java plugin: ctoj_data_source: "
343 "ctoj_int (setType) failed.");
344 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
348 /* Set min via `void setMin (double min)' */
350 ctoj_double(jvm_env, dsrc->min, c_datasource, o_datasource, "setMin");
352 ERROR("java plugin: ctoj_data_source: "
353 "ctoj_double (setMin) failed.");
354 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
358 /* Set max via `void setMax (double max)' */
360 ctoj_double(jvm_env, dsrc->max, c_datasource, o_datasource, "setMax");
362 ERROR("java plugin: ctoj_data_source: "
363 "ctoj_double (setMax) failed.");
364 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
369 } /* }}} jobject ctoj_data_source */
371 /* Convert a oconfig_value_t to a org/collectd/api/OConfigValue */
372 static jobject ctoj_oconfig_value(JNIEnv *jvm_env, /* {{{ */
373 oconfig_value_t ocvalue) {
375 jmethodID m_ocvalue_constructor;
379 m_ocvalue_constructor = NULL;
382 c_ocvalue = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/OConfigValue");
383 if (c_ocvalue == NULL) {
384 ERROR("java plugin: ctoj_oconfig_value: "
385 "FindClass (org/collectd/api/OConfigValue) failed.");
389 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) {
390 jboolean tmp_boolean;
392 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
394 m_ocvalue_constructor =
395 (*jvm_env)->GetMethodID(jvm_env, c_ocvalue, "<init>", "(Z)V");
396 if (m_ocvalue_constructor == NULL) {
397 ERROR("java plugin: ctoj_oconfig_value: Cannot find the "
398 "`OConfigValue (boolean)' constructor.");
402 return (*jvm_env)->NewObject(jvm_env, c_ocvalue, m_ocvalue_constructor,
404 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
405 else if (ocvalue.type == OCONFIG_TYPE_STRING) {
406 m_ocvalue_constructor = (*jvm_env)->GetMethodID(
407 jvm_env, c_ocvalue, "<init>", "(Ljava/lang/String;)V");
408 if (m_ocvalue_constructor == NULL) {
409 ERROR("java plugin: ctoj_oconfig_value: Cannot find the "
410 "`OConfigValue (String)' constructor.");
414 o_argument = (*jvm_env)->NewStringUTF(jvm_env, ocvalue.value.string);
415 if (o_argument == NULL) {
416 ERROR("java plugin: ctoj_oconfig_value: "
417 "Creating a String object failed.");
420 } else if (ocvalue.type == OCONFIG_TYPE_NUMBER) {
421 m_ocvalue_constructor = (*jvm_env)->GetMethodID(
422 jvm_env, c_ocvalue, "<init>", "(Ljava/lang/Number;)V");
423 if (m_ocvalue_constructor == NULL) {
424 ERROR("java plugin: ctoj_oconfig_value: Cannot find the "
425 "`OConfigValue (Number)' constructor.");
429 o_argument = ctoj_jdouble_to_number(jvm_env, (jdouble)ocvalue.value.number);
430 if (o_argument == NULL) {
431 ERROR("java plugin: ctoj_oconfig_value: "
432 "Creating a Number object failed.");
439 assert(m_ocvalue_constructor != NULL);
440 assert(o_argument != NULL);
442 o_ocvalue = (*jvm_env)->NewObject(jvm_env, c_ocvalue, m_ocvalue_constructor,
444 if (o_ocvalue == NULL) {
445 ERROR("java plugin: ctoj_oconfig_value: "
446 "Creating an OConfigValue object failed.");
447 (*jvm_env)->DeleteLocalRef(jvm_env, o_argument);
451 (*jvm_env)->DeleteLocalRef(jvm_env, o_argument);
453 } /* }}} jobject ctoj_oconfig_value */
455 /* Convert a oconfig_item_t to a org/collectd/api/OConfigItem */
456 static jobject ctoj_oconfig_item(JNIEnv *jvm_env, /* {{{ */
457 const oconfig_item_t *ci) {
459 jmethodID m_ocitem_constructor;
460 jmethodID m_addvalue;
461 jmethodID m_addchild;
465 c_ocitem = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/OConfigItem");
466 if (c_ocitem == NULL) {
467 ERROR("java plugin: ctoj_oconfig_item: "
468 "FindClass (org/collectd/api/OConfigItem) failed.");
472 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
474 m_ocitem_constructor = (*jvm_env)->GetMethodID(jvm_env, c_ocitem, "<init>",
475 "(Ljava/lang/String;)V");
476 if (m_ocitem_constructor == NULL) {
477 ERROR("java plugin: ctoj_oconfig_item: Cannot find the "
478 "`OConfigItem (String)' constructor.");
482 m_addvalue = (*jvm_env)->GetMethodID(jvm_env, c_ocitem, "addValue",
483 "(Lorg/collectd/api/OConfigValue;)V");
484 if (m_addvalue == NULL) {
485 ERROR("java plugin: ctoj_oconfig_item: Cannot find the "
486 "`addValue (OConfigValue)' method.");
490 m_addchild = (*jvm_env)->GetMethodID(jvm_env, c_ocitem, "addChild",
491 "(Lorg/collectd/api/OConfigItem;)V");
492 if (m_addchild == NULL) {
493 ERROR("java plugin: ctoj_oconfig_item: Cannot find the "
494 "`addChild (OConfigItem)' method.");
499 /* Create a String object with the key.
500 * Needed for calling the constructor. */
501 o_key = (*jvm_env)->NewStringUTF(jvm_env, ci->key);
503 ERROR("java plugin: ctoj_oconfig_item: "
504 "Creating String object failed.");
508 /* Create an OConfigItem object */
510 (*jvm_env)->NewObject(jvm_env, c_ocitem, m_ocitem_constructor, o_key);
511 if (o_ocitem == NULL) {
512 ERROR("java plugin: ctoj_oconfig_item: "
513 "Creating an OConfigItem object failed.");
514 (*jvm_env)->DeleteLocalRef(jvm_env, o_key);
518 /* We don't need the String object any longer.. */
519 (*jvm_env)->DeleteLocalRef(jvm_env, o_key);
521 /* Call OConfigItem.addValue for each value */
522 for (int i = 0; i < ci->values_num; i++) /* {{{ */
526 o_value = ctoj_oconfig_value(jvm_env, ci->values[i]);
527 if (o_value == NULL) {
528 ERROR("java plugin: ctoj_oconfig_item: "
529 "Creating an OConfigValue object failed.");
530 (*jvm_env)->DeleteLocalRef(jvm_env, o_ocitem);
534 (*jvm_env)->CallVoidMethod(jvm_env, o_ocitem, m_addvalue, o_value);
535 (*jvm_env)->DeleteLocalRef(jvm_env, o_value);
536 } /* }}} for (i = 0; i < ci->values_num; i++) */
538 /* Call OConfigItem.addChild for each child */
539 for (int i = 0; i < ci->children_num; i++) /* {{{ */
543 o_child = ctoj_oconfig_item(jvm_env, ci->children + i);
544 if (o_child == NULL) {
545 ERROR("java plugin: ctoj_oconfig_item: "
546 "Creating an OConfigItem object failed.");
547 (*jvm_env)->DeleteLocalRef(jvm_env, o_ocitem);
551 (*jvm_env)->CallVoidMethod(jvm_env, o_ocitem, m_addchild, o_child);
552 (*jvm_env)->DeleteLocalRef(jvm_env, o_child);
553 } /* }}} for (i = 0; i < ci->children_num; i++) */
556 } /* }}} jobject ctoj_oconfig_item */
558 /* Convert a data_set_t to a org/collectd/api/DataSet */
559 static jobject ctoj_data_set(JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
562 jmethodID m_constructor;
567 /* Look up the org/collectd/api/DataSet class */
568 c_dataset = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/DataSet");
569 if (c_dataset == NULL) {
570 ERROR("java plugin: ctoj_data_set: Looking up the "
571 "org/collectd/api/DataSet class failed.");
575 /* Search for the `DataSet (String type)' constructor. */
576 m_constructor = (*jvm_env)->GetMethodID(jvm_env, c_dataset, "<init>",
577 "(Ljava/lang/String;)V");
578 if (m_constructor == NULL) {
579 ERROR("java plugin: ctoj_data_set: Looking up the "
580 "`DataSet (String)' constructor failed.");
584 /* Search for the `void addDataSource (DataSource)' method. */
585 m_add = (*jvm_env)->GetMethodID(jvm_env, c_dataset, "addDataSource",
586 "(Lorg/collectd/api/DataSource;)V");
588 ERROR("java plugin: ctoj_data_set: Looking up the "
589 "`addDataSource (DataSource)' method failed.");
593 o_type = (*jvm_env)->NewStringUTF(jvm_env, ds->type);
594 if (o_type == NULL) {
595 ERROR("java plugin: ctoj_data_set: Creating a String object failed.");
599 o_dataset = (*jvm_env)->NewObject(jvm_env, c_dataset, m_constructor, o_type);
600 if (o_dataset == NULL) {
601 ERROR("java plugin: ctoj_data_set: Creating a DataSet object failed.");
602 (*jvm_env)->DeleteLocalRef(jvm_env, o_type);
606 /* Decrease reference counter on the java.lang.String object. */
607 (*jvm_env)->DeleteLocalRef(jvm_env, o_type);
609 for (size_t i = 0; i < ds->ds_num; i++) {
610 jobject o_datasource;
612 o_datasource = ctoj_data_source(jvm_env, ds->ds + i);
613 if (o_datasource == NULL) {
614 ERROR("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
615 ds->type, ds->ds[i].name);
616 (*jvm_env)->DeleteLocalRef(jvm_env, o_dataset);
620 (*jvm_env)->CallVoidMethod(jvm_env, o_dataset, m_add, o_datasource);
622 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
623 } /* for (i = 0; i < ds->ds_num; i++) */
626 } /* }}} jobject ctoj_data_set */
628 static int ctoj_value_list_add_value(JNIEnv *jvm_env, /* {{{ */
629 value_t value, int ds_type,
630 jclass class_ptr, jobject object_ptr) {
631 jmethodID m_addvalue;
634 m_addvalue = (*jvm_env)->GetMethodID(jvm_env, class_ptr, "addValue",
635 "(Ljava/lang/Number;)V");
636 if (m_addvalue == NULL) {
637 ERROR("java plugin: ctoj_value_list_add_value: "
638 "Cannot find method `void addValue (Number)'.");
642 o_number = ctoj_value_to_number(jvm_env, value, ds_type);
643 if (o_number == NULL) {
644 ERROR("java plugin: ctoj_value_list_add_value: "
645 "ctoj_value_to_number failed.");
649 (*jvm_env)->CallVoidMethod(jvm_env, object_ptr, m_addvalue, o_number);
651 (*jvm_env)->DeleteLocalRef(jvm_env, o_number);
654 } /* }}} int ctoj_value_list_add_value */
656 static int ctoj_value_list_add_data_set(JNIEnv *jvm_env, /* {{{ */
657 jclass c_valuelist, jobject o_valuelist,
658 const data_set_t *ds) {
659 jmethodID m_setdataset;
662 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
663 m_setdataset = (*jvm_env)->GetMethodID(jvm_env, c_valuelist, "setDataSet",
664 "(Lorg/collectd/api/DataSet;)V");
665 if (m_setdataset == NULL) {
666 ERROR("java plugin: ctoj_value_list_add_data_set: "
667 "Cannot find the `void setDataSet (DataSet)' method.");
671 /* Create a DataSet object. */
672 o_dataset = ctoj_data_set(jvm_env, ds);
673 if (o_dataset == NULL) {
674 ERROR("java plugin: ctoj_value_list_add_data_set: "
675 "ctoj_data_set (%s) failed.",
680 /* Actually call the method. */
681 (*jvm_env)->CallVoidMethod(jvm_env, o_valuelist, m_setdataset, o_dataset);
683 /* Decrease reference counter on the List<DataSource> object. */
684 (*jvm_env)->DeleteLocalRef(jvm_env, o_dataset);
687 } /* }}} int ctoj_value_list_add_data_set */
689 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
690 static jobject ctoj_value_list(JNIEnv *jvm_env, /* {{{ */
691 const data_set_t *ds, const value_list_t *vl) {
693 jmethodID m_valuelist_constructor;
697 /* First, create a new ValueList instance..
698 * Look up the class.. */
699 c_valuelist = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/ValueList");
700 if (c_valuelist == NULL) {
701 ERROR("java plugin: ctoj_value_list: "
702 "FindClass (org/collectd/api/ValueList) failed.");
706 /* Lookup the `ValueList ()' constructor. */
707 m_valuelist_constructor =
708 (*jvm_env)->GetMethodID(jvm_env, c_valuelist, "<init>", "()V");
709 if (m_valuelist_constructor == NULL) {
710 ERROR("java plugin: ctoj_value_list: Cannot find the "
711 "`ValueList ()' constructor.");
715 /* Create a new instance. */
717 (*jvm_env)->NewObject(jvm_env, c_valuelist, m_valuelist_constructor);
718 if (o_valuelist == NULL) {
719 ERROR("java plugin: ctoj_value_list: Creating a new ValueList instance "
724 status = ctoj_value_list_add_data_set(jvm_env, c_valuelist, o_valuelist, ds);
726 ERROR("java plugin: ctoj_value_list: "
727 "ctoj_value_list_add_data_set failed.");
728 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist);
732 /* Set the strings.. */
733 #define SET_STRING(str, method_name) \
735 status = ctoj_string(jvm_env, str, c_valuelist, o_valuelist, method_name); \
737 ERROR("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
739 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist); \
744 SET_STRING(vl->host, "setHost");
745 SET_STRING(vl->plugin, "setPlugin");
746 SET_STRING(vl->plugin_instance, "setPluginInstance");
747 SET_STRING(vl->type, "setType");
748 SET_STRING(vl->type_instance, "setTypeInstance");
752 /* Set the `time' member. Java stores time in milliseconds. */
753 status = ctoj_long(jvm_env, (jlong)CDTIME_T_TO_MS(vl->time), c_valuelist,
754 o_valuelist, "setTime");
756 ERROR("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
757 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist);
761 /* Set the `interval' member.. */
762 status = ctoj_long(jvm_env, (jlong)CDTIME_T_TO_MS(vl->interval), c_valuelist,
763 o_valuelist, "setInterval");
765 ERROR("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
766 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist);
770 for (size_t i = 0; i < vl->values_len; i++) {
771 status = ctoj_value_list_add_value(jvm_env, vl->values[i], ds->ds[i].type,
772 c_valuelist, o_valuelist);
774 ERROR("java plugin: ctoj_value_list: "
775 "ctoj_value_list_add_value failed.");
776 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist);
782 } /* }}} jobject ctoj_value_list */
784 /* Convert a notification_t to a org/collectd/api/Notification */
785 static jobject ctoj_notification(JNIEnv *jvm_env, /* {{{ */
786 const notification_t *n) {
787 jclass c_notification;
788 jmethodID m_constructor;
789 jobject o_notification;
792 /* First, create a new Notification instance..
793 * Look up the class.. */
795 (*jvm_env)->FindClass(jvm_env, "org/collectd/api/Notification");
796 if (c_notification == NULL) {
797 ERROR("java plugin: ctoj_notification: "
798 "FindClass (org/collectd/api/Notification) failed.");
802 /* Lookup the `Notification ()' constructor. */
804 (*jvm_env)->GetMethodID(jvm_env, c_notification, "<init>", "()V");
805 if (m_constructor == NULL) {
806 ERROR("java plugin: ctoj_notification: Cannot find the "
807 "`Notification ()' constructor.");
811 /* Create a new instance. */
813 (*jvm_env)->NewObject(jvm_env, c_notification, m_constructor);
814 if (o_notification == NULL) {
815 ERROR("java plugin: ctoj_notification: Creating a new Notification "
820 /* Set the strings.. */
821 #define SET_STRING(str, method_name) \
823 status = ctoj_string(jvm_env, str, c_notification, o_notification, \
826 ERROR("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
828 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification); \
833 SET_STRING(n->host, "setHost");
834 SET_STRING(n->plugin, "setPlugin");
835 SET_STRING(n->plugin_instance, "setPluginInstance");
836 SET_STRING(n->type, "setType");
837 SET_STRING(n->type_instance, "setTypeInstance");
838 SET_STRING(n->message, "setMessage");
842 /* Set the `time' member. Java stores time in milliseconds. */
843 status = ctoj_long(jvm_env, (jlong)CDTIME_T_TO_MS(n->time), c_notification,
844 o_notification, "setTime");
846 ERROR("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
847 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification);
851 /* Set the `severity' member.. */
852 status = ctoj_int(jvm_env, (jint)n->severity, c_notification, o_notification,
855 ERROR("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
856 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification);
860 return o_notification;
861 } /* }}} jobject ctoj_notification */
864 * Java to C conversion functions
866 /* Call a `String <method> ()' method. */
867 static int jtoc_string(JNIEnv *jvm_env, /* {{{ */
868 char *buffer, size_t buffer_size, int empty_okay,
869 jclass class_ptr, jobject object_ptr,
870 const char *method_name) {
875 method_id = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name,
876 "()Ljava/lang/String;");
877 if (method_id == NULL) {
878 ERROR("java plugin: jtoc_string: Cannot find method `String %s ()'.",
883 string_obj = (*jvm_env)->CallObjectMethod(jvm_env, object_ptr, method_id);
884 if ((string_obj == NULL) && (empty_okay == 0)) {
885 ERROR("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
888 } else if ((string_obj == NULL) && (empty_okay != 0)) {
889 memset(buffer, 0, buffer_size);
893 c_str = (*jvm_env)->GetStringUTFChars(jvm_env, string_obj, 0);
895 ERROR("java plugin: jtoc_string: GetStringUTFChars failed.");
896 (*jvm_env)->DeleteLocalRef(jvm_env, string_obj);
900 sstrncpy(buffer, c_str, buffer_size);
902 (*jvm_env)->ReleaseStringUTFChars(jvm_env, string_obj, c_str);
903 (*jvm_env)->DeleteLocalRef(jvm_env, string_obj);
906 } /* }}} int jtoc_string */
908 /* Call an `int <method> ()' method. */
909 static int jtoc_int(JNIEnv *jvm_env, /* {{{ */
910 jint *ret_value, jclass class_ptr, jobject object_ptr,
911 const char *method_name) {
914 method_id = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "()I");
915 if (method_id == NULL) {
916 ERROR("java plugin: jtoc_int: Cannot find method `int %s ()'.",
921 *ret_value = (*jvm_env)->CallIntMethod(jvm_env, object_ptr, method_id);
924 } /* }}} int jtoc_int */
926 /* Call a `long <method> ()' method. */
927 static int jtoc_long(JNIEnv *jvm_env, /* {{{ */
928 jlong *ret_value, jclass class_ptr, jobject object_ptr,
929 const char *method_name) {
932 method_id = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "()J");
933 if (method_id == NULL) {
934 ERROR("java plugin: jtoc_long: Cannot find method `long %s ()'.",
939 *ret_value = (*jvm_env)->CallLongMethod(jvm_env, object_ptr, method_id);
942 } /* }}} int jtoc_long */
944 /* Call a `double <method> ()' method. */
945 static int jtoc_double(JNIEnv *jvm_env, /* {{{ */
946 jdouble *ret_value, jclass class_ptr, jobject object_ptr,
947 const char *method_name) {
950 method_id = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "()D");
951 if (method_id == NULL) {
952 ERROR("java plugin: jtoc_double: Cannot find method `double %s ()'.",
957 *ret_value = (*jvm_env)->CallDoubleMethod(jvm_env, object_ptr, method_id);
960 } /* }}} int jtoc_double */
962 static int jtoc_value(JNIEnv *jvm_env, /* {{{ */
963 value_t *ret_value, int ds_type, jobject object_ptr) {
967 class_ptr = (*jvm_env)->GetObjectClass(jvm_env, object_ptr);
969 if (ds_type == DS_TYPE_GAUGE) {
973 jtoc_double(jvm_env, &tmp_double, class_ptr, object_ptr, "doubleValue");
975 ERROR("java plugin: jtoc_value: "
976 "jtoc_double failed.");
979 (*ret_value).gauge = (gauge_t)tmp_double;
983 status = jtoc_long(jvm_env, &tmp_long, class_ptr, object_ptr, "longValue");
985 ERROR("java plugin: jtoc_value: "
986 "jtoc_long failed.");
990 if (ds_type == DS_TYPE_DERIVE)
991 (*ret_value).derive = (derive_t)tmp_long;
992 else if (ds_type == DS_TYPE_ABSOLUTE)
993 (*ret_value).absolute = (absolute_t)tmp_long;
995 (*ret_value).counter = (counter_t)tmp_long;
999 } /* }}} int jtoc_value */
1001 /* Read a List<Number>, convert it to `value_t' and add it to the given
1002 * `value_list_t'. */
1003 static int jtoc_values_array(JNIEnv *jvm_env, /* {{{ */
1004 const data_set_t *ds, value_list_t *vl,
1005 jclass class_ptr, jobject object_ptr) {
1006 jmethodID m_getvalues;
1007 jmethodID m_toarray;
1009 jobjectArray o_number_array;
1013 size_t values_num = ds->ds_num;
1016 o_number_array = NULL;
1019 #define BAIL_OUT(status) \
1021 if (o_number_array != NULL) \
1022 (*jvm_env)->DeleteLocalRef(jvm_env, o_number_array); \
1023 if (o_list != NULL) \
1024 (*jvm_env)->DeleteLocalRef(jvm_env, o_list); \
1027 /* Call: List<Number> ValueList.getValues () */
1028 m_getvalues = (*jvm_env)->GetMethodID(jvm_env, class_ptr, "getValues",
1029 "()Ljava/util/List;");
1030 if (m_getvalues == NULL) {
1031 ERROR("java plugin: jtoc_values_array: "
1032 "Cannot find method `List getValues ()'.");
1036 o_list = (*jvm_env)->CallObjectMethod(jvm_env, object_ptr, m_getvalues);
1037 if (o_list == NULL) {
1038 ERROR("java plugin: jtoc_values_array: "
1039 "CallObjectMethod (getValues) failed.");
1043 /* Call: Number[] List.toArray () */
1044 m_toarray = (*jvm_env)->GetMethodID(
1045 jvm_env, (*jvm_env)->GetObjectClass(jvm_env, o_list), "toArray",
1046 "()[Ljava/lang/Object;");
1047 if (m_toarray == NULL) {
1048 ERROR("java plugin: jtoc_values_array: "
1049 "Cannot find method `Object[] toArray ()'.");
1053 o_number_array = (*jvm_env)->CallObjectMethod(jvm_env, o_list, m_toarray);
1054 if (o_number_array == NULL) {
1055 ERROR("java plugin: jtoc_values_array: "
1056 "CallObjectMethod (toArray) failed.");
1060 values = (value_t *)calloc(values_num, sizeof(value_t));
1061 if (values == NULL) {
1062 ERROR("java plugin: jtoc_values_array: calloc failed.");
1066 for (size_t i = 0; i < values_num; i++) {
1071 (*jvm_env)->GetObjectArrayElement(jvm_env, o_number_array, (jsize)i);
1072 if (o_number == NULL) {
1073 ERROR("java plugin: jtoc_values_array: "
1074 "GetObjectArrayElement (%zu) failed.",
1079 status = jtoc_value(jvm_env, values + i, ds->ds[i].type, o_number);
1081 ERROR("java plugin: jtoc_values_array: "
1082 "jtoc_value (%zu) failed.",
1086 } /* for (i = 0; i < values_num; i++) */
1088 vl->values = values;
1089 vl->values_len = values_num;
1092 (*jvm_env)->DeleteLocalRef(jvm_env, o_number_array);
1093 (*jvm_env)->DeleteLocalRef(jvm_env, o_list);
1095 } /* }}} int jtoc_values_array */
1097 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1098 static int jtoc_value_list(JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1099 jobject object_ptr) {
1103 const data_set_t *ds;
1105 class_ptr = (*jvm_env)->GetObjectClass(jvm_env, object_ptr);
1106 if (class_ptr == NULL) {
1107 ERROR("java plugin: jtoc_value_list: GetObjectClass failed.");
1111 /* eo == empty okay */
1112 #define SET_STRING(buffer, method, eo) \
1114 status = jtoc_string(jvm_env, buffer, sizeof(buffer), eo, class_ptr, \
1115 object_ptr, method); \
1116 if (status != 0) { \
1117 ERROR("java plugin: jtoc_value_list: jtoc_string (%s) failed.", method); \
1122 SET_STRING(vl->type, "getType", /* empty = */ 0);
1124 ds = plugin_get_ds(vl->type);
1126 ERROR("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1127 "Please consult the types.db(5) manpage for mor information.",
1132 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1133 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1134 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1135 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1139 status = jtoc_long(jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1141 ERROR("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1144 /* Java measures time in milliseconds. */
1145 vl->time = MS_TO_CDTIME_T(tmp_long);
1147 status = jtoc_long(jvm_env, &tmp_long, class_ptr, object_ptr, "getInterval");
1149 ERROR("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1152 vl->interval = MS_TO_CDTIME_T(tmp_long);
1154 status = jtoc_values_array(jvm_env, ds, vl, class_ptr, object_ptr);
1156 ERROR("java plugin: jtoc_value_list: jtoc_values_array failed.");
1161 } /* }}} int jtoc_value_list */
1163 /* Convert a org/collectd/api/Notification to a notification_t. */
1164 static int jtoc_notification(JNIEnv *jvm_env, notification_t *n, /* {{{ */
1165 jobject object_ptr) {
1171 class_ptr = (*jvm_env)->GetObjectClass(jvm_env, object_ptr);
1172 if (class_ptr == NULL) {
1173 ERROR("java plugin: jtoc_notification: GetObjectClass failed.");
1177 /* eo == empty okay */
1178 #define SET_STRING(buffer, method, eo) \
1180 status = jtoc_string(jvm_env, buffer, sizeof(buffer), eo, class_ptr, \
1181 object_ptr, method); \
1182 if (status != 0) { \
1183 ERROR("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1189 SET_STRING(n->host, "getHost", /* empty = */ 1);
1190 SET_STRING(n->plugin, "getPlugin", /* empty = */ 1);
1191 SET_STRING(n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1192 SET_STRING(n->type, "getType", /* empty = */ 1);
1193 SET_STRING(n->type_instance, "getTypeInstance", /* empty = */ 1);
1194 SET_STRING(n->message, "getMessage", /* empty = */ 0);
1198 status = jtoc_long(jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1200 ERROR("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1203 /* Java measures time in milliseconds. */
1204 n->time = MS_TO_CDTIME_T(tmp_long);
1206 status = jtoc_int(jvm_env, &tmp_int, class_ptr, object_ptr, "getSeverity");
1208 ERROR("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1211 n->severity = (int)tmp_int;
1214 } /* }}} int jtoc_notification */
1216 * Functions accessible from Java
1218 static jint JNICALL cjni_api_dispatch_values(JNIEnv *jvm_env, /* {{{ */
1219 jobject this, jobject java_vl) {
1220 value_list_t vl = VALUE_LIST_INIT;
1223 DEBUG("cjni_api_dispatch_values: java_vl = %p;", (void *)java_vl);
1225 status = jtoc_value_list(jvm_env, &vl, java_vl);
1227 ERROR("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1231 status = plugin_dispatch_values(&vl);
1236 } /* }}} jint cjni_api_dispatch_values */
1238 static jint JNICALL cjni_api_dispatch_notification(JNIEnv *jvm_env, /* {{{ */
1240 jobject o_notification) {
1241 notification_t n = {0};
1244 status = jtoc_notification(jvm_env, &n, o_notification);
1246 ERROR("java plugin: cjni_api_dispatch_notification: jtoc_notification "
1251 status = plugin_dispatch_notification(&n);
1254 } /* }}} jint cjni_api_dispatch_notification */
1256 static jobject JNICALL cjni_api_get_ds(JNIEnv *jvm_env, /* {{{ */
1257 jobject this, jobject o_string_type) {
1258 const char *ds_name;
1259 const data_set_t *ds;
1262 ds_name = (*jvm_env)->GetStringUTFChars(jvm_env, o_string_type, 0);
1263 if (ds_name == NULL) {
1264 ERROR("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1268 ds = plugin_get_ds(ds_name);
1269 DEBUG("java plugin: cjni_api_get_ds: "
1270 "plugin_get_ds (%s) = %p;",
1271 ds_name, (void *)ds);
1273 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_string_type, ds_name);
1278 o_dataset = ctoj_data_set(jvm_env, ds);
1280 } /* }}} jint cjni_api_get_ds */
1282 static jint JNICALL cjni_api_register_config(JNIEnv *jvm_env, /* {{{ */
1283 jobject this, jobject o_name,
1285 return cjni_callback_register(jvm_env, o_name, o_config, CB_TYPE_CONFIG);
1286 } /* }}} jint cjni_api_register_config */
1288 static jint JNICALL cjni_api_register_init(JNIEnv *jvm_env, /* {{{ */
1289 jobject this, jobject o_name,
1291 return cjni_callback_register(jvm_env, o_name, o_config, CB_TYPE_INIT);
1292 } /* }}} jint cjni_api_register_init */
1294 static jint JNICALL cjni_api_register_read(JNIEnv *jvm_env, /* {{{ */
1295 jobject this, jobject o_name,
1297 cjni_callback_info_t *cbi;
1299 cbi = cjni_callback_info_create(jvm_env, o_name, o_read, CB_TYPE_READ);
1303 DEBUG("java plugin: Registering new read callback: %s", cbi->name);
1305 plugin_register_complex_read(
1306 /* group = */ NULL, cbi->name, cjni_read,
1309 .data = cbi, .free_func = cjni_callback_info_destroy,
1312 (*jvm_env)->DeleteLocalRef(jvm_env, o_read);
1315 } /* }}} jint cjni_api_register_read */
1317 static jint JNICALL cjni_api_register_write(JNIEnv *jvm_env, /* {{{ */
1318 jobject this, jobject o_name,
1320 cjni_callback_info_t *cbi;
1322 cbi = cjni_callback_info_create(jvm_env, o_name, o_write, CB_TYPE_WRITE);
1326 DEBUG("java plugin: Registering new write callback: %s", cbi->name);
1328 plugin_register_write(
1329 cbi->name, cjni_write,
1331 .data = cbi, .free_func = cjni_callback_info_destroy,
1334 (*jvm_env)->DeleteLocalRef(jvm_env, o_write);
1337 } /* }}} jint cjni_api_register_write */
1339 static jint JNICALL cjni_api_register_flush(JNIEnv *jvm_env, /* {{{ */
1340 jobject this, jobject o_name,
1342 cjni_callback_info_t *cbi;
1344 cbi = cjni_callback_info_create(jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1348 DEBUG("java plugin: Registering new flush callback: %s", cbi->name);
1350 plugin_register_flush(
1351 cbi->name, cjni_flush,
1353 .data = cbi, .free_func = cjni_callback_info_destroy,
1356 (*jvm_env)->DeleteLocalRef(jvm_env, o_flush);
1359 } /* }}} jint cjni_api_register_flush */
1361 static jint JNICALL cjni_api_register_shutdown(JNIEnv *jvm_env, /* {{{ */
1362 jobject this, jobject o_name,
1363 jobject o_shutdown) {
1364 return cjni_callback_register(jvm_env, o_name, o_shutdown, CB_TYPE_SHUTDOWN);
1365 } /* }}} jint cjni_api_register_shutdown */
1367 static jint JNICALL cjni_api_register_log(JNIEnv *jvm_env, /* {{{ */
1368 jobject this, jobject o_name,
1370 cjni_callback_info_t *cbi;
1372 cbi = cjni_callback_info_create(jvm_env, o_name, o_log, CB_TYPE_LOG);
1376 DEBUG("java plugin: Registering new log callback: %s", cbi->name);
1378 plugin_register_log(cbi->name, cjni_log,
1380 .data = cbi, .free_func = cjni_callback_info_destroy,
1383 (*jvm_env)->DeleteLocalRef(jvm_env, o_log);
1386 } /* }}} jint cjni_api_register_log */
1388 static jint JNICALL cjni_api_register_notification(JNIEnv *jvm_env, /* {{{ */
1389 jobject this, jobject o_name,
1390 jobject o_notification) {
1391 cjni_callback_info_t *cbi;
1393 cbi = cjni_callback_info_create(jvm_env, o_name, o_notification,
1394 CB_TYPE_NOTIFICATION);
1398 DEBUG("java plugin: Registering new notification callback: %s", cbi->name);
1400 plugin_register_notification(
1401 cbi->name, cjni_notification,
1403 .data = cbi, .free_func = cjni_callback_info_destroy,
1406 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification);
1409 } /* }}} jint cjni_api_register_notification */
1411 static jint JNICALL cjni_api_register_match_target(JNIEnv *jvm_env, /* {{{ */
1412 jobject this, jobject o_name,
1413 jobject o_match, int type) {
1417 c_name = (*jvm_env)->GetStringUTFChars(jvm_env, o_name, 0);
1418 if (c_name == NULL) {
1419 ERROR("java plugin: cjni_api_register_match_target: "
1420 "GetStringUTFChars failed.");
1424 status = cjni_callback_register(jvm_env, o_name, o_match, type);
1426 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1430 if (type == CB_TYPE_MATCH) {
1431 match_proc_t m_proc = {0};
1433 m_proc.create = cjni_match_target_create;
1434 m_proc.destroy = cjni_match_target_destroy;
1435 m_proc.match = (void *)cjni_match_target_invoke;
1437 status = fc_register_match(c_name, m_proc);
1438 } else if (type == CB_TYPE_TARGET) {
1439 target_proc_t t_proc = {0};
1441 t_proc.create = cjni_match_target_create;
1442 t_proc.destroy = cjni_match_target_destroy;
1443 t_proc.invoke = cjni_match_target_invoke;
1445 status = fc_register_target(c_name, t_proc);
1447 ERROR("java plugin: cjni_api_register_match_target: "
1448 "Don't know whether to create a match or a target.");
1449 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1454 ERROR("java plugin: cjni_api_register_match_target: "
1456 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1457 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1461 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1464 } /* }}} jint cjni_api_register_match_target */
1466 static jint JNICALL cjni_api_register_match(JNIEnv *jvm_env, /* {{{ */
1467 jobject this, jobject o_name,
1469 return cjni_api_register_match_target(jvm_env, this, o_name, o_match,
1471 } /* }}} jint cjni_api_register_match */
1473 static jint JNICALL cjni_api_register_target(JNIEnv *jvm_env, /* {{{ */
1474 jobject this, jobject o_name,
1476 return cjni_api_register_match_target(jvm_env, this, o_name, o_target,
1478 } /* }}} jint cjni_api_register_target */
1480 static void JNICALL cjni_api_log(JNIEnv *jvm_env, /* {{{ */
1481 jobject this, jint severity,
1482 jobject o_message) {
1485 c_str = (*jvm_env)->GetStringUTFChars(jvm_env, o_message, 0);
1486 if (c_str == NULL) {
1487 ERROR("java plugin: cjni_api_log: GetStringUTFChars failed.");
1491 if (severity < LOG_ERR)
1493 if (severity > LOG_DEBUG)
1494 severity = LOG_DEBUG;
1496 plugin_log(severity, "%s", c_str);
1498 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_message, c_str);
1499 } /* }}} void cjni_api_log */
1501 static jstring JNICALL cjni_api_get_hostname(JNIEnv *jvm_env, jobject this) {
1502 return ctoj_output_string(jvm_env, hostname_g);
1505 /* List of ``native'' functions, i. e. C-functions that can be called from
1507 static JNINativeMethod jni_api_functions[] = /* {{{ */
1509 {"dispatchValues", "(Lorg/collectd/api/ValueList;)I",
1510 cjni_api_dispatch_values},
1512 {"dispatchNotification", "(Lorg/collectd/api/Notification;)I",
1513 cjni_api_dispatch_notification},
1515 {"getDS", "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1519 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1520 cjni_api_register_config},
1523 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1524 cjni_api_register_init},
1527 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1528 cjni_api_register_read},
1531 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1532 cjni_api_register_write},
1535 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1536 cjni_api_register_flush},
1538 {"registerShutdown",
1539 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1540 cjni_api_register_shutdown},
1543 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1544 cjni_api_register_log},
1546 {"registerNotification", "(Ljava/lang/String;Lorg/collectd/api/"
1547 "CollectdNotificationInterface;)I",
1548 cjni_api_register_notification},
1550 {"registerMatch", "(Ljava/lang/String;Lorg/collectd/api/"
1551 "CollectdMatchFactoryInterface;)I",
1552 cjni_api_register_match},
1554 {"registerTarget", "(Ljava/lang/String;Lorg/collectd/api/"
1555 "CollectdTargetFactoryInterface;)I",
1556 cjni_api_register_target},
1558 {"log", "(ILjava/lang/String;)V", cjni_api_log},
1560 {"getHostname", "()Ljava/lang/String;", cjni_api_get_hostname},
1563 static size_t jni_api_functions_num =
1564 sizeof(jni_api_functions) / sizeof(jni_api_functions[0]);
1570 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1571 * all registration functions. */
1572 static cjni_callback_info_t *
1573 cjni_callback_info_create(JNIEnv *jvm_env, /* {{{ */
1574 jobject o_name, jobject o_callback, int type) {
1576 cjni_callback_info_t *cbi;
1577 const char *method_name;
1578 const char *method_signature;
1581 case CB_TYPE_CONFIG:
1582 method_name = "config";
1583 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1587 method_name = "init";
1588 method_signature = "()I";
1592 method_name = "read";
1593 method_signature = "()I";
1597 method_name = "write";
1598 method_signature = "(Lorg/collectd/api/ValueList;)I";
1602 method_name = "flush";
1603 method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I";
1606 case CB_TYPE_SHUTDOWN:
1607 method_name = "shutdown";
1608 method_signature = "()I";
1612 method_name = "log";
1613 method_signature = "(ILjava/lang/String;)V";
1616 case CB_TYPE_NOTIFICATION:
1617 method_name = "notification";
1618 method_signature = "(Lorg/collectd/api/Notification;)I";
1622 method_name = "createMatch";
1623 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1624 "Lorg/collectd/api/CollectdMatchInterface;";
1627 case CB_TYPE_TARGET:
1628 method_name = "createTarget";
1629 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1630 "Lorg/collectd/api/CollectdTargetInterface;";
1634 ERROR("java plugin: cjni_callback_info_create: Unknown type: %#x", type);
1638 c_name = (*jvm_env)->GetStringUTFChars(jvm_env, o_name, 0);
1639 if (c_name == NULL) {
1640 ERROR("java plugin: cjni_callback_info_create: "
1641 "GetStringUTFChars failed.");
1645 cbi = calloc(1, sizeof(*cbi));
1647 ERROR("java plugin: cjni_callback_info_create: calloc failed.");
1648 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1653 cbi->name = strdup(c_name);
1654 if (cbi->name == NULL) {
1655 pthread_mutex_unlock(&java_callbacks_lock);
1656 ERROR("java plugin: cjni_callback_info_create: strdup failed.");
1657 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1662 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1664 cbi->object = (*jvm_env)->NewGlobalRef(jvm_env, o_callback);
1665 if (cbi->object == NULL) {
1666 ERROR("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1672 cbi->class = (*jvm_env)->GetObjectClass(jvm_env, cbi->object);
1673 if (cbi->class == NULL) {
1674 ERROR("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1675 (*jvm_env)->DeleteGlobalRef(jvm_env, cbi->object);
1681 cbi->method = (*jvm_env)->GetMethodID(jvm_env, cbi->class, method_name,
1683 if (cbi->method == NULL) {
1684 ERROR("java plugin: cjni_callback_info_create: "
1685 "Cannot find the `%s' method with signature `%s'.",
1686 method_name, method_signature);
1687 (*jvm_env)->DeleteGlobalRef(jvm_env, cbi->object);
1694 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1696 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1697 * to the global `java_callbacks' variable. This is used for `config', `init',
1698 * and `shutdown' callbacks. */
1699 static int cjni_callback_register(JNIEnv *jvm_env, /* {{{ */
1700 jobject o_name, jobject o_callback,
1702 cjni_callback_info_t *cbi;
1703 cjni_callback_info_t *tmp;
1705 const char *type_str;
1708 cbi = cjni_callback_info_create(jvm_env, o_name, o_callback, type);
1714 case CB_TYPE_CONFIG:
1715 type_str = "config";
1722 case CB_TYPE_SHUTDOWN:
1723 type_str = "shutdown";
1730 case CB_TYPE_TARGET:
1731 type_str = "target";
1735 type_str = "<unknown>";
1737 DEBUG("java plugin: Registering new %s callback: %s", type_str, cbi->name);
1740 pthread_mutex_lock(&java_callbacks_lock);
1742 tmp = realloc(java_callbacks,
1743 (java_callbacks_num + 1) * sizeof(*java_callbacks));
1745 pthread_mutex_unlock(&java_callbacks_lock);
1746 ERROR("java plugin: cjni_callback_register: realloc failed.");
1748 (*jvm_env)->DeleteGlobalRef(jvm_env, cbi->object);
1753 java_callbacks = tmp;
1754 java_callbacks[java_callbacks_num] = *cbi;
1755 java_callbacks_num++;
1757 pthread_mutex_unlock(&java_callbacks_lock);
1761 } /* }}} int cjni_callback_register */
1763 /* Callback for `pthread_key_create'. It frees the data contained in
1764 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1765 static void cjni_jvm_env_destroy(void *args) /* {{{ */
1767 cjni_jvm_env_t *cjni_env;
1772 cjni_env = (cjni_jvm_env_t *)args;
1774 if (cjni_env->reference_counter > 0) {
1775 ERROR("java plugin: cjni_jvm_env_destroy: "
1776 "cjni_env->reference_counter = %i;",
1777 cjni_env->reference_counter);
1780 if (cjni_env->jvm_env != NULL) {
1781 ERROR("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1782 (void *)cjni_env->jvm_env);
1785 /* The pointer is allocated in `cjni_thread_attach' */
1787 } /* }}} void cjni_jvm_env_destroy */
1789 /* Register ``native'' functions with the JVM. Native functions are C-functions
1790 * that can be called by Java code. */
1791 static int cjni_init_native(JNIEnv *jvm_env) /* {{{ */
1793 jclass api_class_ptr;
1796 api_class_ptr = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/Collectd");
1797 if (api_class_ptr == NULL) {
1798 ERROR("cjni_init_native: Cannot find the API class \"org.collectd.api"
1799 ".Collectd\". Please set the correct class path "
1800 "using 'JVMArg \"-Djava.class.path=...\"'.");
1804 status = (*jvm_env)->RegisterNatives(
1805 jvm_env, api_class_ptr, jni_api_functions, (jint)jni_api_functions_num);
1807 ERROR("cjni_init_native: RegisterNatives failed with status %i.", status);
1812 } /* }}} int cjni_init_native */
1814 /* Create the JVM. This is called when the first thread tries to access the JVM
1815 * via cjni_thread_attach. */
1816 static int cjni_create_jvm(void) /* {{{ */
1819 JavaVMInitArgs vm_args = {0};
1820 JavaVMOption vm_options[jvm_argc];
1827 status = pthread_key_create(&jvm_env_key, cjni_jvm_env_destroy);
1829 ERROR("java plugin: cjni_create_jvm: pthread_key_create failed "
1837 vm_args.version = JNI_VERSION_1_2;
1838 vm_args.options = vm_options;
1839 vm_args.nOptions = (jint)jvm_argc;
1841 for (size_t i = 0; i < jvm_argc; i++) {
1842 DEBUG("java plugin: cjni_create_jvm: jvm_argv[%" PRIsz "] = %s", i,
1844 vm_args.options[i].optionString = jvm_argv[i];
1847 status = JNI_CreateJavaVM(&jvm, (void *)&jvm_env, (void *)&vm_args);
1849 ERROR("java plugin: cjni_create_jvm: "
1850 "JNI_CreateJavaVM failed with status %i.",
1854 assert(jvm != NULL);
1855 assert(jvm_env != NULL);
1857 /* Call RegisterNatives */
1858 status = cjni_init_native(jvm_env);
1860 ERROR("java plugin: cjni_create_jvm: cjni_init_native failed.");
1864 DEBUG("java plugin: The JVM has been created.");
1866 } /* }}} int cjni_create_jvm */
1868 /* Increase the reference counter to the JVM for this thread. If it was zero,
1869 * attach the JVM first. */
1870 static JNIEnv *cjni_thread_attach(void) /* {{{ */
1872 cjni_jvm_env_t *cjni_env;
1875 /* If we're the first thread to access the JVM, we'll have to create it
1880 status = cjni_create_jvm();
1882 ERROR("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
1886 assert(jvm != NULL);
1888 cjni_env = pthread_getspecific(jvm_env_key);
1889 if (cjni_env == NULL) {
1890 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
1891 cjni_env = calloc(1, sizeof(*cjni_env));
1892 if (cjni_env == NULL) {
1893 ERROR("java plugin: cjni_thread_attach: calloc failed.");
1896 cjni_env->reference_counter = 0;
1897 cjni_env->jvm_env = NULL;
1899 pthread_setspecific(jvm_env_key, cjni_env);
1902 if (cjni_env->reference_counter > 0) {
1903 cjni_env->reference_counter++;
1904 jvm_env = cjni_env->jvm_env;
1907 JavaVMAttachArgs args = {0};
1909 assert(cjni_env->jvm_env == NULL);
1911 args.version = JNI_VERSION_1_2;
1913 status = (*jvm)->AttachCurrentThread(jvm, (void *)&jvm_env, (void *)&args);
1915 ERROR("java plugin: cjni_thread_attach: AttachCurrentThread failed "
1921 cjni_env->reference_counter = 1;
1922 cjni_env->jvm_env = jvm_env;
1925 DEBUG("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
1926 cjni_env->reference_counter);
1927 assert(jvm_env != NULL);
1929 } /* }}} JNIEnv *cjni_thread_attach */
1931 /* Decrease the reference counter of this thread. If it reaches zero, detach
1933 static int cjni_thread_detach(void) /* {{{ */
1935 cjni_jvm_env_t *cjni_env;
1938 cjni_env = pthread_getspecific(jvm_env_key);
1939 if (cjni_env == NULL) {
1940 ERROR("java plugin: cjni_thread_detach: pthread_getspecific failed.");
1944 assert(cjni_env->reference_counter > 0);
1945 assert(cjni_env->jvm_env != NULL);
1947 cjni_env->reference_counter--;
1948 DEBUG("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
1949 cjni_env->reference_counter);
1951 if (cjni_env->reference_counter > 0)
1954 status = (*jvm)->DetachCurrentThread(jvm);
1956 ERROR("java plugin: cjni_thread_detach: DetachCurrentThread failed "
1961 cjni_env->reference_counter = 0;
1962 cjni_env->jvm_env = NULL;
1965 } /* }}} int cjni_thread_detach */
1967 static int cjni_config_add_jvm_arg(oconfig_item_t *ci) /* {{{ */
1971 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1972 WARNING("java plugin: `JVMArg' needs exactly one string argument.");
1977 ERROR("java plugin: All `JVMArg' options MUST appear before all "
1978 "`LoadPlugin' options! The JVM is already started and I have to "
1979 "ignore this argument: %s",
1980 ci->values[0].value.string);
1984 tmp = realloc(jvm_argv, sizeof(char *) * (jvm_argc + 1));
1986 ERROR("java plugin: realloc failed.");
1991 jvm_argv[jvm_argc] = strdup(ci->values[0].value.string);
1992 if (jvm_argv[jvm_argc] == NULL) {
1993 ERROR("java plugin: strdup failed.");
1999 } /* }}} int cjni_config_add_jvm_arg */
2001 static int cjni_config_load_plugin(oconfig_item_t *ci) /* {{{ */
2004 java_plugin_class_t *class;
2005 jmethodID constructor_id;
2008 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2009 WARNING("java plugin: `LoadPlugin' needs exactly one string argument.");
2013 jvm_env = cjni_thread_attach();
2014 if (jvm_env == NULL)
2017 class = realloc(java_classes_list,
2018 (java_classes_list_len + 1) * sizeof(*java_classes_list));
2019 if (class == NULL) {
2020 ERROR("java plugin: realloc failed.");
2021 cjni_thread_detach();
2024 java_classes_list = class;
2025 class = java_classes_list + java_classes_list_len;
2027 memset(class, 0, sizeof(*class));
2028 class->name = strdup(ci->values[0].value.string);
2029 if (class->name == NULL) {
2030 ERROR("java plugin: strdup failed.");
2031 cjni_thread_detach();
2034 class->class = NULL;
2035 class->object = NULL;
2037 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2038 thorough the Java community, but (Sun's) `FindClass' and friends need
2040 for (size_t i = 0; class->name[i] != 0; i++)
2041 if (class->name[i] == '.')
2042 class->name[i] = '/';
2045 DEBUG("java plugin: Loading class %s", class->name);
2047 class->class = (*jvm_env)->FindClass(jvm_env, class->name);
2048 if (class->class == NULL) {
2049 ERROR("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2051 cjni_thread_detach();
2057 (*jvm_env)->GetMethodID(jvm_env, class->class, "<init>", "()V");
2058 if (constructor_id == NULL) {
2059 ERROR("java plugin: cjni_config_load_plugin: "
2060 "Could not find the constructor for `%s'.",
2062 cjni_thread_detach();
2067 tmp_object = (*jvm_env)->NewObject(jvm_env, class->class, constructor_id);
2068 if (tmp_object != NULL)
2069 class->object = (*jvm_env)->NewGlobalRef(jvm_env, tmp_object);
2071 class->object = NULL;
2072 if (class->object == NULL) {
2073 ERROR("java plugin: cjni_config_load_plugin: "
2074 "Could create a new `%s' object.",
2076 cjni_thread_detach();
2081 cjni_thread_detach();
2083 java_classes_list_len++;
2086 } /* }}} int cjni_config_load_plugin */
2088 static int cjni_config_plugin_block(oconfig_item_t *ci) /* {{{ */
2091 cjni_callback_info_t *cbi;
2098 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2099 WARNING("java plugin: `Plugin' blocks "
2100 "need exactly one string argument.");
2104 name = ci->values[0].value.string;
2107 for (size_t i = 0; i < java_callbacks_num; i++) {
2108 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2111 if (strcmp(name, java_callbacks[i].name) != 0)
2114 cbi = java_callbacks + i;
2119 NOTICE("java plugin: Configuration block for `%s' found, but no such "
2120 "configuration callback has been registered. Please make sure, the "
2121 "`LoadPlugin' lines precede the `Plugin' blocks.",
2126 DEBUG("java plugin: Configuring %s", name);
2128 jvm_env = cjni_thread_attach();
2129 if (jvm_env == NULL)
2132 o_ocitem = ctoj_oconfig_item(jvm_env, ci);
2133 if (o_ocitem == NULL) {
2134 ERROR("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2135 cjni_thread_detach();
2139 class = (*jvm_env)->GetObjectClass(jvm_env, cbi->object);
2140 method = (*jvm_env)->GetMethodID(jvm_env, class, "config",
2141 "(Lorg/collectd/api/OConfigItem;)I");
2143 (*jvm_env)->CallIntMethod(jvm_env, cbi->object, method, o_ocitem);
2145 (*jvm_env)->DeleteLocalRef(jvm_env, o_ocitem);
2146 cjni_thread_detach();
2148 } /* }}} int cjni_config_plugin_block */
2150 static int cjni_config_perform(oconfig_item_t *ci) /* {{{ */
2159 for (int i = 0; i < ci->children_num; i++) {
2160 oconfig_item_t *child = ci->children + i;
2162 if (strcasecmp("JVMArg", child->key) == 0) {
2163 status = cjni_config_add_jvm_arg(child);
2168 } else if (strcasecmp("LoadPlugin", child->key) == 0) {
2169 status = cjni_config_load_plugin(child);
2174 } else if (strcasecmp("Plugin", child->key) == 0) {
2175 status = cjni_config_plugin_block(child);
2181 WARNING("java plugin: Option `%s' not allowed here.", child->key);
2186 DEBUG("java plugin: jvm_argc = %" PRIsz ";", jvm_argc);
2187 DEBUG("java plugin: java_classes_list_len = %" PRIsz ";",
2188 java_classes_list_len);
2190 if ((success == 0) && (errors > 0)) {
2191 ERROR("java plugin: All statements failed.");
2196 } /* }}} int cjni_config_perform */
2198 /* Copy the children of `ci' to the global `config_block' variable. */
2199 static int cjni_config_callback(oconfig_item_t *ci) /* {{{ */
2201 oconfig_item_t *ci_copy;
2202 oconfig_item_t *tmp;
2205 if (ci->children_num == 0)
2206 return 0; /* nothing to do */
2208 ci_copy = oconfig_clone(ci);
2209 if (ci_copy == NULL) {
2210 ERROR("java plugin: oconfig_clone failed.");
2214 if (config_block == NULL) {
2215 config_block = ci_copy;
2219 tmp = realloc(config_block->children,
2220 (config_block->children_num + ci_copy->children_num) *
2223 ERROR("java plugin: realloc failed.");
2224 oconfig_free(ci_copy);
2227 config_block->children = tmp;
2229 /* Copy the pointers */
2230 memcpy(config_block->children + config_block->children_num, ci_copy->children,
2231 ci_copy->children_num * sizeof(*ci_copy->children));
2232 config_block->children_num += ci_copy->children_num;
2234 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2235 memset(ci_copy->children, 0,
2236 ci_copy->children_num * sizeof(*ci_copy->children));
2237 ci_copy->children_num = 0;
2239 oconfig_free(ci_copy);
2242 } /* }}} int cjni_config_callback */
2244 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2245 * and `cjni_write'. In particular, delete the global reference to the Java
2247 static void cjni_callback_info_destroy(void *arg) /* {{{ */
2250 cjni_callback_info_t *cbi;
2252 DEBUG("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2254 cbi = (cjni_callback_info_t *)arg;
2256 /* This condition can occur when shutting down. */
2265 jvm_env = cjni_thread_attach();
2266 if (jvm_env == NULL) {
2268 "java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2272 (*jvm_env)->DeleteGlobalRef(jvm_env, cbi->object);
2279 cjni_thread_detach();
2280 } /* }}} void cjni_callback_info_destroy */
2282 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2283 static int cjni_read(user_data_t *ud) /* {{{ */
2286 cjni_callback_info_t *cbi;
2290 ERROR("java plugin: cjni_read: jvm == NULL");
2294 if ((ud == NULL) || (ud->data == NULL)) {
2295 ERROR("java plugin: cjni_read: Invalid user data.");
2299 jvm_env = cjni_thread_attach();
2300 if (jvm_env == NULL)
2303 cbi = (cjni_callback_info_t *)ud->data;
2305 ret_status = (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method);
2307 cjni_thread_detach();
2309 } /* }}} int cjni_read */
2311 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2312 static int cjni_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
2315 cjni_callback_info_t *cbi;
2320 ERROR("java plugin: cjni_write: jvm == NULL");
2324 if ((ud == NULL) || (ud->data == NULL)) {
2325 ERROR("java plugin: cjni_write: Invalid user data.");
2329 jvm_env = cjni_thread_attach();
2330 if (jvm_env == NULL)
2333 cbi = (cjni_callback_info_t *)ud->data;
2335 vl_java = ctoj_value_list(jvm_env, ds, vl);
2336 if (vl_java == NULL) {
2337 ERROR("java plugin: cjni_write: ctoj_value_list failed.");
2338 cjni_thread_detach();
2343 (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method, vl_java);
2345 (*jvm_env)->DeleteLocalRef(jvm_env, vl_java);
2347 cjni_thread_detach();
2349 } /* }}} int cjni_write */
2351 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2352 static int cjni_flush(cdtime_t timeout, const char *identifier, /* {{{ */
2355 cjni_callback_info_t *cbi;
2357 jobject o_identifier;
2361 ERROR("java plugin: cjni_flush: jvm == NULL");
2365 if ((ud == NULL) || (ud->data == NULL)) {
2366 ERROR("java plugin: cjni_flush: Invalid user data.");
2370 jvm_env = cjni_thread_attach();
2371 if (jvm_env == NULL)
2374 cbi = (cjni_callback_info_t *)ud->data;
2377 ctoj_jdouble_to_number(jvm_env, (jdouble)CDTIME_T_TO_DOUBLE(timeout));
2378 if (o_timeout == NULL) {
2379 ERROR("java plugin: cjni_flush: Converting double "
2380 "to Number object failed.");
2381 cjni_thread_detach();
2385 o_identifier = NULL;
2386 if (identifier != NULL) {
2387 o_identifier = (*jvm_env)->NewStringUTF(jvm_env, identifier);
2388 if (o_identifier == NULL) {
2389 (*jvm_env)->DeleteLocalRef(jvm_env, o_timeout);
2390 ERROR("java plugin: cjni_flush: NewStringUTF failed.");
2391 cjni_thread_detach();
2396 ret_status = (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method,
2397 o_timeout, o_identifier);
2399 (*jvm_env)->DeleteLocalRef(jvm_env, o_identifier);
2400 (*jvm_env)->DeleteLocalRef(jvm_env, o_timeout);
2402 cjni_thread_detach();
2404 } /* }}} int cjni_flush */
2406 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2407 static void cjni_log(int severity, const char *message, /* {{{ */
2410 cjni_callback_info_t *cbi;
2416 if ((ud == NULL) || (ud->data == NULL))
2419 jvm_env = cjni_thread_attach();
2420 if (jvm_env == NULL)
2423 cbi = (cjni_callback_info_t *)ud->data;
2425 o_message = (*jvm_env)->NewStringUTF(jvm_env, message);
2426 if (o_message == NULL) {
2427 cjni_thread_detach();
2431 (*jvm_env)->CallVoidMethod(jvm_env, cbi->object, cbi->method, (jint)severity,
2434 (*jvm_env)->DeleteLocalRef(jvm_env, o_message);
2436 cjni_thread_detach();
2437 } /* }}} void cjni_log */
2439 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2441 static int cjni_notification(const notification_t *n, /* {{{ */
2444 cjni_callback_info_t *cbi;
2445 jobject o_notification;
2449 ERROR("java plugin: cjni_read: jvm == NULL");
2453 if ((ud == NULL) || (ud->data == NULL)) {
2454 ERROR("java plugin: cjni_read: Invalid user data.");
2458 jvm_env = cjni_thread_attach();
2459 if (jvm_env == NULL)
2462 cbi = (cjni_callback_info_t *)ud->data;
2464 o_notification = ctoj_notification(jvm_env, n);
2465 if (o_notification == NULL) {
2466 ERROR("java plugin: cjni_notification: ctoj_notification failed.");
2467 cjni_thread_detach();
2471 ret_status = (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method,
2474 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification);
2476 cjni_thread_detach();
2478 } /* }}} int cjni_notification */
2480 /* Callbacks for matches implemented in Java */
2481 static int cjni_match_target_create(const oconfig_item_t *ci, /* {{{ */
2484 cjni_callback_info_t *cbi_ret;
2485 cjni_callback_info_t *cbi_factory;
2495 #define BAIL_OUT(status) \
2496 if (cbi_ret != NULL) { \
2497 free(cbi_ret->name); \
2498 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2499 (*jvm_env)->DeleteLocalRef(jvm_env, cbi_ret->object); \
2503 (*jvm_env)->DeleteLocalRef(jvm_env, o_ci); \
2504 cjni_thread_detach(); \
2508 ERROR("java plugin: cjni_read: jvm == NULL");
2512 jvm_env = cjni_thread_attach();
2513 if (jvm_env == NULL)
2516 /* Find out whether to create a match or a target. */
2517 if (strcasecmp("Match", ci->key) == 0)
2518 type = CB_TYPE_MATCH;
2519 else if (strcasecmp("Target", ci->key) == 0)
2520 type = CB_TYPE_TARGET;
2522 ERROR("java plugin: cjni_match_target_create: Can't figure out whether "
2523 "to create a match or a target.");
2527 /* This is the name of the match we should create. */
2528 name = ci->values[0].value.string;
2530 /* Lets see if we have a matching factory here.. */
2532 for (size_t i = 0; i < java_callbacks_num; i++) {
2533 if (java_callbacks[i].type != type)
2536 if (strcmp(name, java_callbacks[i].name) != 0)
2539 cbi_factory = java_callbacks + i;
2543 /* Nope, no factory for that name.. */
2544 if (cbi_factory == NULL) {
2545 ERROR("java plugin: cjni_match_target_create: "
2546 "No such match factory registered: %s",
2551 /* We convert `ci' to its Java equivalent.. */
2552 o_ci = ctoj_oconfig_item(jvm_env, ci);
2554 ERROR("java plugin: cjni_match_target_create: "
2555 "ctoj_oconfig_item failed.");
2559 /* Allocate a new callback info structure. This is going to be our user_data
2561 cbi_ret = calloc(1, sizeof(*cbi_ret));
2562 if (cbi_ret == NULL) {
2563 ERROR("java plugin: cjni_match_target_create: calloc failed.");
2567 cbi_ret->object = NULL;
2568 cbi_ret->type = type;
2570 /* Lets fill the callback info structure.. First, the name: */
2571 cbi_ret->name = strdup(name);
2572 if (cbi_ret->name == NULL) {
2573 ERROR("java plugin: cjni_match_target_create: strdup failed.");
2577 /* Then call the factory method so it creates a new object for us. */
2578 o_tmp = (*jvm_env)->CallObjectMethod(jvm_env, cbi_factory->object,
2579 cbi_factory->method, o_ci);
2580 if (o_tmp == NULL) {
2581 ERROR("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2585 cbi_ret->object = (*jvm_env)->NewGlobalRef(jvm_env, o_tmp);
2586 if (o_tmp == NULL) {
2587 ERROR("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2591 /* This is the class of the match. It is possibly different from the class of
2592 * the match-factory! */
2593 cbi_ret->class = (*jvm_env)->GetObjectClass(jvm_env, cbi_ret->object);
2594 if (cbi_ret->class == NULL) {
2595 ERROR("java plugin: cjni_match_target_create: GetObjectClass failed.");
2599 /* Lookup the `int match (DataSet, ValueList)' method. */
2600 cbi_ret->method = (*jvm_env)->GetMethodID(
2601 jvm_env, cbi_ret->class,
2602 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2603 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2604 if (cbi_ret->method == NULL) {
2605 ERROR("java plugin: cjni_match_target_create: GetMethodID failed.");
2609 /* Return the newly created match via the user_data pointer. */
2610 *user_data = (void *)cbi_ret;
2612 cjni_thread_detach();
2614 DEBUG("java plugin: cjni_match_target_create: "
2615 "Successfully created a `%s' %s.",
2616 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2621 } /* }}} int cjni_match_target_create */
2623 static int cjni_match_target_destroy(void **user_data) /* {{{ */
2625 cjni_callback_info_destroy(*user_data);
2629 } /* }}} int cjni_match_target_destroy */
2631 static int cjni_match_target_invoke(const data_set_t *ds, /* {{{ */
2633 notification_meta_t **meta,
2636 cjni_callback_info_t *cbi;
2643 ERROR("java plugin: cjni_match_target_invoke: jvm == NULL");
2647 jvm_env = cjni_thread_attach();
2648 if (jvm_env == NULL)
2651 cbi = (cjni_callback_info_t *)*user_data;
2653 o_vl = ctoj_value_list(jvm_env, ds, vl);
2655 ERROR("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2656 cjni_thread_detach();
2660 o_ds = ctoj_data_set(jvm_env, ds);
2662 ERROR("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2663 cjni_thread_detach();
2668 (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method, o_ds, o_vl);
2670 DEBUG("java plugin: cjni_match_target_invoke: Method returned %i.",
2673 /* If we're executing a target, copy the `ValueList' back to our
2674 * `value_list_t'. */
2675 if (cbi->type == CB_TYPE_TARGET) {
2676 value_list_t new_vl = {0};
2678 status = jtoc_value_list(jvm_env, &new_vl, o_vl);
2680 ERROR("java plugin: cjni_match_target_invoke: "
2681 "jtoc_value_list failed.");
2682 } else /* if (status == 0) */
2684 /* plugin_dispatch_values assures that this is dynamically allocated
2688 /* This will replace the vl->values pointer to a new, dynamically
2689 * allocated piece of memory. */
2690 memcpy(vl, &new_vl, sizeof(*vl));
2692 } /* if (cbi->type == CB_TYPE_TARGET) */
2694 cjni_thread_detach();
2696 } /* }}} int cjni_match_target_invoke */
2698 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2699 static int cjni_init_plugins(JNIEnv *jvm_env) /* {{{ */
2703 for (size_t i = 0; i < java_callbacks_num; i++) {
2704 if (java_callbacks[i].type != CB_TYPE_INIT)
2707 DEBUG("java plugin: Initializing %s", java_callbacks[i].name);
2709 status = (*jvm_env)->CallIntMethod(jvm_env, java_callbacks[i].object,
2710 java_callbacks[i].method);
2712 ERROR("java plugin: Initializing `%s' failed with status %i. "
2713 "Removing read function.",
2714 java_callbacks[i].name, status);
2715 plugin_unregister_read(java_callbacks[i].name);
2720 } /* }}} int cjni_init_plugins */
2722 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2723 static int cjni_shutdown_plugins(JNIEnv *jvm_env) /* {{{ */
2727 for (size_t i = 0; i < java_callbacks_num; i++) {
2728 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2731 DEBUG("java plugin: Shutting down %s", java_callbacks[i].name);
2733 status = (*jvm_env)->CallIntMethod(jvm_env, java_callbacks[i].object,
2734 java_callbacks[i].method);
2736 ERROR("java plugin: Shutting down `%s' failed with status %i. ",
2737 java_callbacks[i].name, status);
2742 } /* }}} int cjni_shutdown_plugins */
2744 static int cjni_shutdown(void) /* {{{ */
2747 JavaVMAttachArgs args = {0};
2754 args.version = JNI_VERSION_1_2;
2756 status = (*jvm)->AttachCurrentThread(jvm, (void *)&jvm_env, &args);
2758 ERROR("java plugin: cjni_shutdown: AttachCurrentThread failed with status "
2764 /* Execute all the shutdown functions registered by plugins. */
2765 cjni_shutdown_plugins(jvm_env);
2767 /* Release all the global references to callback functions */
2768 for (size_t i = 0; i < java_callbacks_num; i++) {
2769 if (java_callbacks[i].object != NULL) {
2770 (*jvm_env)->DeleteGlobalRef(jvm_env, java_callbacks[i].object);
2771 java_callbacks[i].object = NULL;
2773 sfree(java_callbacks[i].name);
2775 java_callbacks_num = 0;
2776 sfree(java_callbacks);
2778 /* Release all the global references to directly loaded classes. */
2779 for (size_t i = 0; i < java_classes_list_len; i++) {
2780 if (java_classes_list[i].object != NULL) {
2781 (*jvm_env)->DeleteGlobalRef(jvm_env, java_classes_list[i].object);
2782 java_classes_list[i].object = NULL;
2784 sfree(java_classes_list[i].name);
2786 java_classes_list_len = 0;
2787 sfree(java_classes_list);
2789 /* Destroy the JVM */
2790 DEBUG("java plugin: Destroying the JVM.");
2791 (*jvm)->DestroyJavaVM(jvm);
2795 pthread_key_delete(jvm_env_key);
2797 /* Free the JVM argument list */
2798 for (size_t i = 0; i < jvm_argc; i++)
2804 } /* }}} int cjni_shutdown */
2806 /* Initialization: Create a JVM, load all configured classes and call their
2807 * `config' and `init' callback methods. */
2808 static int cjni_init(void) /* {{{ */
2812 if ((config_block == NULL) && (jvm == NULL)) {
2813 ERROR("java plugin: cjni_init: No configuration block for "
2814 "the java plugin was found.");
2818 if (config_block != NULL) {
2819 cjni_config_perform(config_block);
2820 oconfig_free(config_block);
2824 ERROR("java plugin: cjni_init: jvm == NULL");
2828 jvm_env = cjni_thread_attach();
2829 if (jvm_env == NULL)
2832 cjni_init_plugins(jvm_env);
2834 cjni_thread_detach();
2836 } /* }}} int cjni_init */
2838 void module_register(void) {
2839 plugin_register_complex_config("java", cjni_config_callback);
2840 plugin_register_init("java", cjni_init);
2841 plugin_register_shutdown("java", cjni_shutdown);
2842 } /* void module_register (void) */