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;
80 static JavaVM *jvm = NULL;
81 static pthread_key_t jvm_env_key;
83 /* Configuration options for the JVM. */
84 static char **jvm_argv = NULL;
85 static size_t jvm_argc = 0;
87 /* List of class names to load */
88 static java_plugin_class_t *java_classes_list = NULL;
89 static size_t java_classes_list_len;
91 /* List of config, init, and shutdown callbacks. */
92 static cjni_callback_info_t *java_callbacks = NULL;
93 static size_t java_callbacks_num = 0;
94 static pthread_mutex_t java_callbacks_lock = PTHREAD_MUTEX_INITIALIZER;
96 static oconfig_item_t *config_block = NULL;
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.");
278 (*jvm_env)->NewObject(jvm_env, c_double, m_double_constructor, value));
279 } /* }}} jobject ctoj_jdouble_to_number */
281 /* Convert a value_t to a java.lang.Number */
282 static jobject ctoj_value_to_number(JNIEnv *jvm_env, /* {{{ */
283 value_t value, int ds_type) {
284 if (ds_type == DS_TYPE_COUNTER)
285 return (ctoj_jlong_to_number(jvm_env, (jlong)value.counter));
286 else if (ds_type == DS_TYPE_GAUGE)
287 return (ctoj_jdouble_to_number(jvm_env, (jdouble)value.gauge));
288 if (ds_type == DS_TYPE_DERIVE)
289 return (ctoj_jlong_to_number(jvm_env, (jlong)value.derive));
290 if (ds_type == DS_TYPE_ABSOLUTE)
291 return (ctoj_jlong_to_number(jvm_env, (jlong)value.absolute));
294 } /* }}} jobject ctoj_value_to_number */
296 /* Convert a data_source_t to a org/collectd/api/DataSource */
297 static jobject ctoj_data_source(JNIEnv *jvm_env, /* {{{ */
298 const data_source_t *dsrc) {
300 jmethodID m_datasource_constructor;
301 jobject o_datasource;
304 /* Look up the DataSource class */
305 c_datasource = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/DataSource");
306 if (c_datasource == NULL) {
307 ERROR("java plugin: ctoj_data_source: "
308 "FindClass (org/collectd/api/DataSource) failed.");
312 /* Lookup the `ValueList ()' constructor. */
313 m_datasource_constructor =
314 (*jvm_env)->GetMethodID(jvm_env, c_datasource, "<init>", "()V");
315 if (m_datasource_constructor == NULL) {
316 ERROR("java plugin: ctoj_data_source: Cannot find the "
317 "`DataSource ()' constructor.");
321 /* Create a new instance. */
323 (*jvm_env)->NewObject(jvm_env, c_datasource, m_datasource_constructor);
324 if (o_datasource == NULL) {
325 ERROR("java plugin: ctoj_data_source: "
326 "Creating a new DataSource instance failed.");
330 /* Set name via `void setName (String name)' */
332 ctoj_string(jvm_env, dsrc->name, c_datasource, o_datasource, "setName");
334 ERROR("java plugin: ctoj_data_source: "
335 "ctoj_string (setName) failed.");
336 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
340 /* Set type via `void setType (int type)' */
341 status = ctoj_int(jvm_env, dsrc->type, c_datasource, o_datasource, "setType");
343 ERROR("java plugin: ctoj_data_source: "
344 "ctoj_int (setType) failed.");
345 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
349 /* Set min via `void setMin (double min)' */
351 ctoj_double(jvm_env, dsrc->min, c_datasource, o_datasource, "setMin");
353 ERROR("java plugin: ctoj_data_source: "
354 "ctoj_double (setMin) failed.");
355 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
359 /* Set max via `void setMax (double max)' */
361 ctoj_double(jvm_env, dsrc->max, c_datasource, o_datasource, "setMax");
363 ERROR("java plugin: ctoj_data_source: "
364 "ctoj_double (setMax) failed.");
365 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
369 return (o_datasource);
370 } /* }}} jobject ctoj_data_source */
372 /* Convert a oconfig_value_t to a org/collectd/api/OConfigValue */
373 static jobject ctoj_oconfig_value(JNIEnv *jvm_env, /* {{{ */
374 oconfig_value_t ocvalue) {
376 jmethodID m_ocvalue_constructor;
380 m_ocvalue_constructor = NULL;
383 c_ocvalue = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/OConfigValue");
384 if (c_ocvalue == NULL) {
385 ERROR("java plugin: ctoj_oconfig_value: "
386 "FindClass (org/collectd/api/OConfigValue) failed.");
390 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) {
391 jboolean tmp_boolean;
393 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
395 m_ocvalue_constructor =
396 (*jvm_env)->GetMethodID(jvm_env, c_ocvalue, "<init>", "(Z)V");
397 if (m_ocvalue_constructor == NULL) {
398 ERROR("java plugin: ctoj_oconfig_value: Cannot find the "
399 "`OConfigValue (boolean)' constructor.");
403 return ((*jvm_env)->NewObject(jvm_env, c_ocvalue, m_ocvalue_constructor,
405 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
406 else if (ocvalue.type == OCONFIG_TYPE_STRING) {
407 m_ocvalue_constructor = (*jvm_env)->GetMethodID(
408 jvm_env, c_ocvalue, "<init>", "(Ljava/lang/String;)V");
409 if (m_ocvalue_constructor == NULL) {
410 ERROR("java plugin: ctoj_oconfig_value: Cannot find the "
411 "`OConfigValue (String)' constructor.");
415 o_argument = (*jvm_env)->NewStringUTF(jvm_env, ocvalue.value.string);
416 if (o_argument == NULL) {
417 ERROR("java plugin: ctoj_oconfig_value: "
418 "Creating a String object failed.");
421 } else if (ocvalue.type == OCONFIG_TYPE_NUMBER) {
422 m_ocvalue_constructor = (*jvm_env)->GetMethodID(
423 jvm_env, c_ocvalue, "<init>", "(Ljava/lang/Number;)V");
424 if (m_ocvalue_constructor == NULL) {
425 ERROR("java plugin: ctoj_oconfig_value: Cannot find the "
426 "`OConfigValue (Number)' constructor.");
430 o_argument = ctoj_jdouble_to_number(jvm_env, (jdouble)ocvalue.value.number);
431 if (o_argument == NULL) {
432 ERROR("java plugin: ctoj_oconfig_value: "
433 "Creating a Number object failed.");
440 assert(m_ocvalue_constructor != NULL);
441 assert(o_argument != NULL);
443 o_ocvalue = (*jvm_env)->NewObject(jvm_env, c_ocvalue, m_ocvalue_constructor,
445 if (o_ocvalue == NULL) {
446 ERROR("java plugin: ctoj_oconfig_value: "
447 "Creating an OConfigValue object failed.");
448 (*jvm_env)->DeleteLocalRef(jvm_env, o_argument);
452 (*jvm_env)->DeleteLocalRef(jvm_env, o_argument);
454 } /* }}} jobject ctoj_oconfig_value */
456 /* Convert a oconfig_item_t to a org/collectd/api/OConfigItem */
457 static jobject ctoj_oconfig_item(JNIEnv *jvm_env, /* {{{ */
458 const oconfig_item_t *ci) {
460 jmethodID m_ocitem_constructor;
461 jmethodID m_addvalue;
462 jmethodID m_addchild;
466 c_ocitem = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/OConfigItem");
467 if (c_ocitem == NULL) {
468 ERROR("java plugin: ctoj_oconfig_item: "
469 "FindClass (org/collectd/api/OConfigItem) failed.");
473 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
475 m_ocitem_constructor = (*jvm_env)->GetMethodID(jvm_env, c_ocitem, "<init>",
476 "(Ljava/lang/String;)V");
477 if (m_ocitem_constructor == NULL) {
478 ERROR("java plugin: ctoj_oconfig_item: Cannot find the "
479 "`OConfigItem (String)' constructor.");
483 m_addvalue = (*jvm_env)->GetMethodID(jvm_env, c_ocitem, "addValue",
484 "(Lorg/collectd/api/OConfigValue;)V");
485 if (m_addvalue == NULL) {
486 ERROR("java plugin: ctoj_oconfig_item: Cannot find the "
487 "`addValue (OConfigValue)' method.");
491 m_addchild = (*jvm_env)->GetMethodID(jvm_env, c_ocitem, "addChild",
492 "(Lorg/collectd/api/OConfigItem;)V");
493 if (m_addchild == NULL) {
494 ERROR("java plugin: ctoj_oconfig_item: Cannot find the "
495 "`addChild (OConfigItem)' method.");
500 /* Create a String object with the key.
501 * Needed for calling the constructor. */
502 o_key = (*jvm_env)->NewStringUTF(jvm_env, ci->key);
504 ERROR("java plugin: ctoj_oconfig_item: "
505 "Creating String object failed.");
509 /* Create an OConfigItem object */
511 (*jvm_env)->NewObject(jvm_env, c_ocitem, m_ocitem_constructor, o_key);
512 if (o_ocitem == NULL) {
513 ERROR("java plugin: ctoj_oconfig_item: "
514 "Creating an OConfigItem object failed.");
515 (*jvm_env)->DeleteLocalRef(jvm_env, o_key);
519 /* We don't need the String object any longer.. */
520 (*jvm_env)->DeleteLocalRef(jvm_env, o_key);
522 /* Call OConfigItem.addValue for each value */
523 for (int i = 0; i < ci->values_num; i++) /* {{{ */
527 o_value = ctoj_oconfig_value(jvm_env, ci->values[i]);
528 if (o_value == NULL) {
529 ERROR("java plugin: ctoj_oconfig_item: "
530 "Creating an OConfigValue object failed.");
531 (*jvm_env)->DeleteLocalRef(jvm_env, o_ocitem);
535 (*jvm_env)->CallVoidMethod(jvm_env, o_ocitem, m_addvalue, o_value);
536 (*jvm_env)->DeleteLocalRef(jvm_env, o_value);
537 } /* }}} for (i = 0; i < ci->values_num; i++) */
539 /* Call OConfigItem.addChild for each child */
540 for (int i = 0; i < ci->children_num; i++) /* {{{ */
544 o_child = ctoj_oconfig_item(jvm_env, ci->children + i);
545 if (o_child == NULL) {
546 ERROR("java plugin: ctoj_oconfig_item: "
547 "Creating an OConfigItem object failed.");
548 (*jvm_env)->DeleteLocalRef(jvm_env, o_ocitem);
552 (*jvm_env)->CallVoidMethod(jvm_env, o_ocitem, m_addchild, o_child);
553 (*jvm_env)->DeleteLocalRef(jvm_env, o_child);
554 } /* }}} for (i = 0; i < ci->children_num; i++) */
557 } /* }}} jobject ctoj_oconfig_item */
559 /* Convert a data_set_t to a org/collectd/api/DataSet */
560 static jobject ctoj_data_set(JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
563 jmethodID m_constructor;
568 /* Look up the org/collectd/api/DataSet class */
569 c_dataset = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/DataSet");
570 if (c_dataset == NULL) {
571 ERROR("java plugin: ctoj_data_set: Looking up the "
572 "org/collectd/api/DataSet class failed.");
576 /* Search for the `DataSet (String type)' constructor. */
577 m_constructor = (*jvm_env)->GetMethodID(jvm_env, c_dataset, "<init>",
578 "(Ljava/lang/String;)V");
579 if (m_constructor == NULL) {
580 ERROR("java plugin: ctoj_data_set: Looking up the "
581 "`DataSet (String)' constructor failed.");
585 /* Search for the `void addDataSource (DataSource)' method. */
586 m_add = (*jvm_env)->GetMethodID(jvm_env, c_dataset, "addDataSource",
587 "(Lorg/collectd/api/DataSource;)V");
589 ERROR("java plugin: ctoj_data_set: Looking up the "
590 "`addDataSource (DataSource)' method failed.");
594 o_type = (*jvm_env)->NewStringUTF(jvm_env, ds->type);
595 if (o_type == NULL) {
596 ERROR("java plugin: ctoj_data_set: Creating a String object failed.");
600 o_dataset = (*jvm_env)->NewObject(jvm_env, c_dataset, m_constructor, o_type);
601 if (o_dataset == NULL) {
602 ERROR("java plugin: ctoj_data_set: Creating a DataSet object failed.");
603 (*jvm_env)->DeleteLocalRef(jvm_env, o_type);
607 /* Decrease reference counter on the java.lang.String object. */
608 (*jvm_env)->DeleteLocalRef(jvm_env, o_type);
610 for (size_t i = 0; i < ds->ds_num; i++) {
611 jobject o_datasource;
613 o_datasource = ctoj_data_source(jvm_env, ds->ds + i);
614 if (o_datasource == NULL) {
615 ERROR("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
616 ds->type, ds->ds[i].name);
617 (*jvm_env)->DeleteLocalRef(jvm_env, o_dataset);
621 (*jvm_env)->CallVoidMethod(jvm_env, o_dataset, m_add, o_datasource);
623 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
624 } /* for (i = 0; i < ds->ds_num; i++) */
627 } /* }}} jobject ctoj_data_set */
629 static int ctoj_value_list_add_value(JNIEnv *jvm_env, /* {{{ */
630 value_t value, int ds_type,
631 jclass class_ptr, jobject object_ptr) {
632 jmethodID m_addvalue;
635 m_addvalue = (*jvm_env)->GetMethodID(jvm_env, class_ptr, "addValue",
636 "(Ljava/lang/Number;)V");
637 if (m_addvalue == NULL) {
638 ERROR("java plugin: ctoj_value_list_add_value: "
639 "Cannot find method `void addValue (Number)'.");
643 o_number = ctoj_value_to_number(jvm_env, value, ds_type);
644 if (o_number == NULL) {
645 ERROR("java plugin: ctoj_value_list_add_value: "
646 "ctoj_value_to_number failed.");
650 (*jvm_env)->CallVoidMethod(jvm_env, object_ptr, m_addvalue, o_number);
652 (*jvm_env)->DeleteLocalRef(jvm_env, o_number);
655 } /* }}} int ctoj_value_list_add_value */
657 static int ctoj_value_list_add_data_set(JNIEnv *jvm_env, /* {{{ */
658 jclass c_valuelist, jobject o_valuelist,
659 const data_set_t *ds) {
660 jmethodID m_setdataset;
663 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
664 m_setdataset = (*jvm_env)->GetMethodID(jvm_env, c_valuelist, "setDataSet",
665 "(Lorg/collectd/api/DataSet;)V");
666 if (m_setdataset == NULL) {
667 ERROR("java plugin: ctoj_value_list_add_data_set: "
668 "Cannot find the `void setDataSet (DataSet)' method.");
672 /* Create a DataSet object. */
673 o_dataset = ctoj_data_set(jvm_env, ds);
674 if (o_dataset == NULL) {
675 ERROR("java plugin: ctoj_value_list_add_data_set: "
676 "ctoj_data_set (%s) failed.",
681 /* Actually call the method. */
682 (*jvm_env)->CallVoidMethod(jvm_env, o_valuelist, m_setdataset, o_dataset);
684 /* Decrease reference counter on the List<DataSource> object. */
685 (*jvm_env)->DeleteLocalRef(jvm_env, o_dataset);
688 } /* }}} int ctoj_value_list_add_data_set */
690 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
691 static jobject ctoj_value_list(JNIEnv *jvm_env, /* {{{ */
692 const data_set_t *ds, const value_list_t *vl) {
694 jmethodID m_valuelist_constructor;
698 /* First, create a new ValueList instance..
699 * Look up the class.. */
700 c_valuelist = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/ValueList");
701 if (c_valuelist == NULL) {
702 ERROR("java plugin: ctoj_value_list: "
703 "FindClass (org/collectd/api/ValueList) failed.");
707 /* Lookup the `ValueList ()' constructor. */
708 m_valuelist_constructor =
709 (*jvm_env)->GetMethodID(jvm_env, c_valuelist, "<init>", "()V");
710 if (m_valuelist_constructor == NULL) {
711 ERROR("java plugin: ctoj_value_list: Cannot find the "
712 "`ValueList ()' constructor.");
716 /* Create a new instance. */
718 (*jvm_env)->NewObject(jvm_env, c_valuelist, m_valuelist_constructor);
719 if (o_valuelist == NULL) {
720 ERROR("java plugin: ctoj_value_list: Creating a new ValueList instance "
725 status = ctoj_value_list_add_data_set(jvm_env, c_valuelist, o_valuelist, ds);
727 ERROR("java plugin: ctoj_value_list: "
728 "ctoj_value_list_add_data_set failed.");
729 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist);
733 /* Set the strings.. */
734 #define SET_STRING(str, method_name) \
736 status = ctoj_string(jvm_env, str, c_valuelist, o_valuelist, method_name); \
738 ERROR("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
740 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist); \
745 SET_STRING(vl->host, "setHost");
746 SET_STRING(vl->plugin, "setPlugin");
747 SET_STRING(vl->plugin_instance, "setPluginInstance");
748 SET_STRING(vl->type, "setType");
749 SET_STRING(vl->type_instance, "setTypeInstance");
753 /* Set the `time' member. Java stores time in milliseconds. */
754 status = ctoj_long(jvm_env, (jlong)CDTIME_T_TO_MS(vl->time), c_valuelist,
755 o_valuelist, "setTime");
757 ERROR("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
758 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist);
762 /* Set the `interval' member.. */
763 status = ctoj_long(jvm_env, (jlong)CDTIME_T_TO_MS(vl->interval), c_valuelist,
764 o_valuelist, "setInterval");
766 ERROR("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
767 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist);
771 for (size_t i = 0; i < vl->values_len; i++) {
772 status = ctoj_value_list_add_value(jvm_env, vl->values[i], ds->ds[i].type,
773 c_valuelist, o_valuelist);
775 ERROR("java plugin: ctoj_value_list: "
776 "ctoj_value_list_add_value failed.");
777 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist);
782 return (o_valuelist);
783 } /* }}} jobject ctoj_value_list */
785 /* Convert a notification_t to a org/collectd/api/Notification */
786 static jobject ctoj_notification(JNIEnv *jvm_env, /* {{{ */
787 const notification_t *n) {
788 jclass c_notification;
789 jmethodID m_constructor;
790 jobject o_notification;
793 /* First, create a new Notification instance..
794 * Look up the class.. */
796 (*jvm_env)->FindClass(jvm_env, "org/collectd/api/Notification");
797 if (c_notification == NULL) {
798 ERROR("java plugin: ctoj_notification: "
799 "FindClass (org/collectd/api/Notification) failed.");
803 /* Lookup the `Notification ()' constructor. */
805 (*jvm_env)->GetMethodID(jvm_env, c_notification, "<init>", "()V");
806 if (m_constructor == NULL) {
807 ERROR("java plugin: ctoj_notification: Cannot find the "
808 "`Notification ()' constructor.");
812 /* Create a new instance. */
814 (*jvm_env)->NewObject(jvm_env, c_notification, m_constructor);
815 if (o_notification == NULL) {
816 ERROR("java plugin: ctoj_notification: Creating a new Notification "
821 /* Set the strings.. */
822 #define SET_STRING(str, method_name) \
824 status = ctoj_string(jvm_env, str, c_notification, o_notification, \
827 ERROR("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
829 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification); \
834 SET_STRING(n->host, "setHost");
835 SET_STRING(n->plugin, "setPlugin");
836 SET_STRING(n->plugin_instance, "setPluginInstance");
837 SET_STRING(n->type, "setType");
838 SET_STRING(n->type_instance, "setTypeInstance");
839 SET_STRING(n->message, "setMessage");
843 /* Set the `time' member. Java stores time in milliseconds. */
844 status = ctoj_long(jvm_env, (jlong)CDTIME_T_TO_MS(n->time), c_notification,
845 o_notification, "setTime");
847 ERROR("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
848 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification);
852 /* Set the `severity' member.. */
853 status = ctoj_int(jvm_env, (jint)n->severity, c_notification, o_notification,
856 ERROR("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
857 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification);
861 return (o_notification);
862 } /* }}} jobject ctoj_notification */
865 * Java to C conversion functions
867 /* Call a `String <method> ()' method. */
868 static int jtoc_string(JNIEnv *jvm_env, /* {{{ */
869 char *buffer, size_t buffer_size, int empty_okay,
870 jclass class_ptr, jobject object_ptr,
871 const char *method_name) {
876 method_id = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name,
877 "()Ljava/lang/String;");
878 if (method_id == NULL) {
879 ERROR("java plugin: jtoc_string: Cannot find method `String %s ()'.",
884 string_obj = (*jvm_env)->CallObjectMethod(jvm_env, object_ptr, method_id);
885 if ((string_obj == NULL) && (empty_okay == 0)) {
886 ERROR("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
889 } else if ((string_obj == NULL) && (empty_okay != 0)) {
890 memset(buffer, 0, buffer_size);
894 c_str = (*jvm_env)->GetStringUTFChars(jvm_env, string_obj, 0);
896 ERROR("java plugin: jtoc_string: GetStringUTFChars failed.");
897 (*jvm_env)->DeleteLocalRef(jvm_env, string_obj);
901 sstrncpy(buffer, c_str, buffer_size);
903 (*jvm_env)->ReleaseStringUTFChars(jvm_env, string_obj, c_str);
904 (*jvm_env)->DeleteLocalRef(jvm_env, string_obj);
907 } /* }}} int jtoc_string */
909 /* Call an `int <method> ()' method. */
910 static int jtoc_int(JNIEnv *jvm_env, /* {{{ */
911 jint *ret_value, jclass class_ptr, jobject object_ptr,
912 const char *method_name) {
915 method_id = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "()I");
916 if (method_id == NULL) {
917 ERROR("java plugin: jtoc_int: Cannot find method `int %s ()'.",
922 *ret_value = (*jvm_env)->CallIntMethod(jvm_env, object_ptr, method_id);
925 } /* }}} int jtoc_int */
927 /* Call a `long <method> ()' method. */
928 static int jtoc_long(JNIEnv *jvm_env, /* {{{ */
929 jlong *ret_value, jclass class_ptr, jobject object_ptr,
930 const char *method_name) {
933 method_id = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "()J");
934 if (method_id == NULL) {
935 ERROR("java plugin: jtoc_long: Cannot find method `long %s ()'.",
940 *ret_value = (*jvm_env)->CallLongMethod(jvm_env, object_ptr, method_id);
943 } /* }}} int jtoc_long */
945 /* Call a `double <method> ()' method. */
946 static int jtoc_double(JNIEnv *jvm_env, /* {{{ */
947 jdouble *ret_value, jclass class_ptr, jobject object_ptr,
948 const char *method_name) {
951 method_id = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "()D");
952 if (method_id == NULL) {
953 ERROR("java plugin: jtoc_double: Cannot find method `double %s ()'.",
958 *ret_value = (*jvm_env)->CallDoubleMethod(jvm_env, object_ptr, method_id);
961 } /* }}} int jtoc_double */
963 static int jtoc_value(JNIEnv *jvm_env, /* {{{ */
964 value_t *ret_value, int ds_type, jobject object_ptr) {
968 class_ptr = (*jvm_env)->GetObjectClass(jvm_env, object_ptr);
970 if (ds_type == DS_TYPE_GAUGE) {
974 jtoc_double(jvm_env, &tmp_double, class_ptr, object_ptr, "doubleValue");
976 ERROR("java plugin: jtoc_value: "
977 "jtoc_double failed.");
980 (*ret_value).gauge = (gauge_t)tmp_double;
984 status = jtoc_long(jvm_env, &tmp_long, class_ptr, object_ptr, "longValue");
986 ERROR("java plugin: jtoc_value: "
987 "jtoc_long failed.");
991 if (ds_type == DS_TYPE_DERIVE)
992 (*ret_value).derive = (derive_t)tmp_long;
993 else if (ds_type == DS_TYPE_ABSOLUTE)
994 (*ret_value).absolute = (absolute_t)tmp_long;
996 (*ret_value).counter = (counter_t)tmp_long;
1000 } /* }}} int jtoc_value */
1002 /* Read a List<Number>, convert it to `value_t' and add it to the given
1003 * `value_list_t'. */
1004 static int jtoc_values_array(JNIEnv *jvm_env, /* {{{ */
1005 const data_set_t *ds, value_list_t *vl,
1006 jclass class_ptr, jobject object_ptr) {
1007 jmethodID m_getvalues;
1008 jmethodID m_toarray;
1010 jobjectArray o_number_array;
1015 values_num = ds->ds_num;
1018 o_number_array = NULL;
1021 #define BAIL_OUT(status) \
1023 if (o_number_array != NULL) \
1024 (*jvm_env)->DeleteLocalRef(jvm_env, o_number_array); \
1025 if (o_list != NULL) \
1026 (*jvm_env)->DeleteLocalRef(jvm_env, o_list); \
1029 /* Call: List<Number> ValueList.getValues () */
1030 m_getvalues = (*jvm_env)->GetMethodID(jvm_env, class_ptr, "getValues",
1031 "()Ljava/util/List;");
1032 if (m_getvalues == NULL) {
1033 ERROR("java plugin: jtoc_values_array: "
1034 "Cannot find method `List getValues ()'.");
1038 o_list = (*jvm_env)->CallObjectMethod(jvm_env, object_ptr, m_getvalues);
1039 if (o_list == NULL) {
1040 ERROR("java plugin: jtoc_values_array: "
1041 "CallObjectMethod (getValues) failed.");
1045 /* Call: Number[] List.toArray () */
1046 m_toarray = (*jvm_env)->GetMethodID(
1047 jvm_env, (*jvm_env)->GetObjectClass(jvm_env, o_list), "toArray",
1048 "()[Ljava/lang/Object;");
1049 if (m_toarray == NULL) {
1050 ERROR("java plugin: jtoc_values_array: "
1051 "Cannot find method `Object[] toArray ()'.");
1055 o_number_array = (*jvm_env)->CallObjectMethod(jvm_env, o_list, m_toarray);
1056 if (o_number_array == NULL) {
1057 ERROR("java plugin: jtoc_values_array: "
1058 "CallObjectMethod (toArray) failed.");
1062 values = (value_t *)calloc(values_num, sizeof(value_t));
1063 if (values == NULL) {
1064 ERROR("java plugin: jtoc_values_array: calloc failed.");
1068 for (int i = 0; i < values_num; i++) {
1073 (*jvm_env)->GetObjectArrayElement(jvm_env, o_number_array, (jsize)i);
1074 if (o_number == NULL) {
1075 ERROR("java plugin: jtoc_values_array: "
1076 "GetObjectArrayElement (%i) failed.",
1081 status = jtoc_value(jvm_env, values + i, ds->ds[i].type, o_number);
1083 ERROR("java plugin: jtoc_values_array: "
1084 "jtoc_value (%i) failed.",
1088 } /* for (i = 0; i < values_num; i++) */
1090 vl->values = values;
1091 vl->values_len = values_num;
1094 (*jvm_env)->DeleteLocalRef(jvm_env, o_number_array);
1095 (*jvm_env)->DeleteLocalRef(jvm_env, o_list);
1097 } /* }}} int jtoc_values_array */
1099 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1100 static int jtoc_value_list(JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1101 jobject object_ptr) {
1105 const data_set_t *ds;
1107 class_ptr = (*jvm_env)->GetObjectClass(jvm_env, object_ptr);
1108 if (class_ptr == NULL) {
1109 ERROR("java plugin: jtoc_value_list: GetObjectClass failed.");
1113 /* eo == empty okay */
1114 #define SET_STRING(buffer, method, eo) \
1116 status = jtoc_string(jvm_env, buffer, sizeof(buffer), eo, class_ptr, \
1117 object_ptr, method); \
1118 if (status != 0) { \
1119 ERROR("java plugin: jtoc_value_list: jtoc_string (%s) failed.", method); \
1124 SET_STRING(vl->type, "getType", /* empty = */ 0);
1126 ds = plugin_get_ds(vl->type);
1128 ERROR("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1129 "Please consult the types.db(5) manpage for mor information.",
1134 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1135 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1136 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1137 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1141 status = jtoc_long(jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1143 ERROR("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1146 /* Java measures time in milliseconds. */
1147 vl->time = MS_TO_CDTIME_T(tmp_long);
1149 status = jtoc_long(jvm_env, &tmp_long, class_ptr, object_ptr, "getInterval");
1151 ERROR("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1154 vl->interval = MS_TO_CDTIME_T(tmp_long);
1156 status = jtoc_values_array(jvm_env, ds, vl, class_ptr, object_ptr);
1158 ERROR("java plugin: jtoc_value_list: jtoc_values_array failed.");
1163 } /* }}} int jtoc_value_list */
1165 /* Convert a org/collectd/api/Notification to a notification_t. */
1166 static int jtoc_notification(JNIEnv *jvm_env, notification_t *n, /* {{{ */
1167 jobject object_ptr) {
1173 class_ptr = (*jvm_env)->GetObjectClass(jvm_env, object_ptr);
1174 if (class_ptr == NULL) {
1175 ERROR("java plugin: jtoc_notification: GetObjectClass failed.");
1179 /* eo == empty okay */
1180 #define SET_STRING(buffer, method, eo) \
1182 status = jtoc_string(jvm_env, buffer, sizeof(buffer), eo, class_ptr, \
1183 object_ptr, method); \
1184 if (status != 0) { \
1185 ERROR("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1191 SET_STRING(n->host, "getHost", /* empty = */ 1);
1192 SET_STRING(n->plugin, "getPlugin", /* empty = */ 1);
1193 SET_STRING(n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1194 SET_STRING(n->type, "getType", /* empty = */ 1);
1195 SET_STRING(n->type_instance, "getTypeInstance", /* empty = */ 1);
1196 SET_STRING(n->message, "getMessage", /* empty = */ 0);
1200 status = jtoc_long(jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1202 ERROR("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1205 /* Java measures time in milliseconds. */
1206 n->time = MS_TO_CDTIME_T(tmp_long);
1208 status = jtoc_int(jvm_env, &tmp_int, class_ptr, object_ptr, "getSeverity");
1210 ERROR("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1213 n->severity = (int)tmp_int;
1216 } /* }}} int jtoc_notification */
1218 * Functions accessible from Java
1220 static jint JNICALL cjni_api_dispatch_values(JNIEnv *jvm_env, /* {{{ */
1221 jobject this, jobject java_vl) {
1222 value_list_t vl = VALUE_LIST_INIT;
1225 DEBUG("cjni_api_dispatch_values: java_vl = %p;", (void *)java_vl);
1227 status = jtoc_value_list(jvm_env, &vl, java_vl);
1229 ERROR("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1233 status = plugin_dispatch_values(&vl);
1238 } /* }}} jint cjni_api_dispatch_values */
1240 static jint JNICALL cjni_api_dispatch_notification(JNIEnv *jvm_env, /* {{{ */
1242 jobject o_notification) {
1243 notification_t n = {0};
1246 status = jtoc_notification(jvm_env, &n, o_notification);
1248 ERROR("java plugin: cjni_api_dispatch_notification: jtoc_notification "
1253 status = plugin_dispatch_notification(&n);
1256 } /* }}} jint cjni_api_dispatch_notification */
1258 static jobject JNICALL cjni_api_get_ds(JNIEnv *jvm_env, /* {{{ */
1259 jobject this, jobject o_string_type) {
1260 const char *ds_name;
1261 const data_set_t *ds;
1264 ds_name = (*jvm_env)->GetStringUTFChars(jvm_env, o_string_type, 0);
1265 if (ds_name == NULL) {
1266 ERROR("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1270 ds = plugin_get_ds(ds_name);
1271 DEBUG("java plugin: cjni_api_get_ds: "
1272 "plugin_get_ds (%s) = %p;",
1273 ds_name, (void *)ds);
1275 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_string_type, ds_name);
1280 o_dataset = ctoj_data_set(jvm_env, ds);
1282 } /* }}} jint cjni_api_get_ds */
1284 static jint JNICALL cjni_api_register_config(JNIEnv *jvm_env, /* {{{ */
1285 jobject this, jobject o_name,
1287 return (cjni_callback_register(jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1288 } /* }}} jint cjni_api_register_config */
1290 static jint JNICALL cjni_api_register_init(JNIEnv *jvm_env, /* {{{ */
1291 jobject this, jobject o_name,
1293 return (cjni_callback_register(jvm_env, o_name, o_config, CB_TYPE_INIT));
1294 } /* }}} jint cjni_api_register_init */
1296 static jint JNICALL cjni_api_register_read(JNIEnv *jvm_env, /* {{{ */
1297 jobject this, jobject o_name,
1299 cjni_callback_info_t *cbi;
1301 cbi = cjni_callback_info_create(jvm_env, o_name, o_read, CB_TYPE_READ);
1305 DEBUG("java plugin: Registering new read callback: %s", cbi->name);
1307 user_data_t ud = {.data = cbi, .free_func = cjni_callback_info_destroy};
1309 plugin_register_complex_read(/* group = */ NULL, cbi->name, cjni_read,
1310 /* interval = */ 0, &ud);
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 user_data_t ud = {.data = cbi, .free_func = cjni_callback_info_destroy};
1330 plugin_register_write(cbi->name, cjni_write, &ud);
1332 (*jvm_env)->DeleteLocalRef(jvm_env, o_write);
1335 } /* }}} jint cjni_api_register_write */
1337 static jint JNICALL cjni_api_register_flush(JNIEnv *jvm_env, /* {{{ */
1338 jobject this, jobject o_name,
1340 cjni_callback_info_t *cbi;
1342 cbi = cjni_callback_info_create(jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1346 DEBUG("java plugin: Registering new flush callback: %s", cbi->name);
1348 user_data_t ud = {.data = cbi, .free_func = cjni_callback_info_destroy};
1350 plugin_register_flush(cbi->name, cjni_flush, &ud);
1352 (*jvm_env)->DeleteLocalRef(jvm_env, o_flush);
1355 } /* }}} jint cjni_api_register_flush */
1357 static jint JNICALL cjni_api_register_shutdown(JNIEnv *jvm_env, /* {{{ */
1358 jobject this, jobject o_name,
1359 jobject o_shutdown) {
1361 cjni_callback_register(jvm_env, o_name, o_shutdown, CB_TYPE_SHUTDOWN));
1362 } /* }}} jint cjni_api_register_shutdown */
1364 static jint JNICALL cjni_api_register_log(JNIEnv *jvm_env, /* {{{ */
1365 jobject this, jobject o_name,
1367 cjni_callback_info_t *cbi;
1369 cbi = cjni_callback_info_create(jvm_env, o_name, o_log, CB_TYPE_LOG);
1373 DEBUG("java plugin: Registering new log callback: %s", cbi->name);
1375 user_data_t ud = {.data = cbi, .free_func = cjni_callback_info_destroy};
1377 plugin_register_log(cbi->name, cjni_log, &ud);
1379 (*jvm_env)->DeleteLocalRef(jvm_env, o_log);
1382 } /* }}} jint cjni_api_register_log */
1384 static jint JNICALL cjni_api_register_notification(JNIEnv *jvm_env, /* {{{ */
1385 jobject this, jobject o_name,
1386 jobject o_notification) {
1387 cjni_callback_info_t *cbi;
1389 cbi = cjni_callback_info_create(jvm_env, o_name, o_notification,
1390 CB_TYPE_NOTIFICATION);
1394 DEBUG("java plugin: Registering new notification callback: %s", cbi->name);
1396 user_data_t ud = {.data = cbi, .free_func = cjni_callback_info_destroy};
1398 plugin_register_notification(cbi->name, cjni_notification, &ud);
1400 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification);
1403 } /* }}} jint cjni_api_register_notification */
1405 static jint JNICALL cjni_api_register_match_target(JNIEnv *jvm_env, /* {{{ */
1406 jobject this, jobject o_name,
1407 jobject o_match, int type) {
1411 c_name = (*jvm_env)->GetStringUTFChars(jvm_env, o_name, 0);
1412 if (c_name == NULL) {
1413 ERROR("java plugin: cjni_api_register_match_target: "
1414 "GetStringUTFChars failed.");
1418 status = cjni_callback_register(jvm_env, o_name, o_match, type);
1420 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1424 if (type == CB_TYPE_MATCH) {
1425 match_proc_t m_proc = {0};
1427 m_proc.create = cjni_match_target_create;
1428 m_proc.destroy = cjni_match_target_destroy;
1429 m_proc.match = (void *)cjni_match_target_invoke;
1431 status = fc_register_match(c_name, m_proc);
1432 } else if (type == CB_TYPE_TARGET) {
1433 target_proc_t t_proc = {0};
1435 t_proc.create = cjni_match_target_create;
1436 t_proc.destroy = cjni_match_target_destroy;
1437 t_proc.invoke = cjni_match_target_invoke;
1439 status = fc_register_target(c_name, t_proc);
1441 ERROR("java plugin: cjni_api_register_match_target: "
1442 "Don't know whether to create a match or a target.");
1443 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1448 ERROR("java plugin: cjni_api_register_match_target: "
1450 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1451 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1455 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1458 } /* }}} jint cjni_api_register_match_target */
1460 static jint JNICALL cjni_api_register_match(JNIEnv *jvm_env, /* {{{ */
1461 jobject this, jobject o_name,
1463 return (cjni_api_register_match_target(jvm_env, this, o_name, o_match,
1465 } /* }}} jint cjni_api_register_match */
1467 static jint JNICALL cjni_api_register_target(JNIEnv *jvm_env, /* {{{ */
1468 jobject this, jobject o_name,
1470 return (cjni_api_register_match_target(jvm_env, this, o_name, o_target,
1472 } /* }}} jint cjni_api_register_target */
1474 static void JNICALL cjni_api_log(JNIEnv *jvm_env, /* {{{ */
1475 jobject this, jint severity,
1476 jobject o_message) {
1479 c_str = (*jvm_env)->GetStringUTFChars(jvm_env, o_message, 0);
1480 if (c_str == NULL) {
1481 ERROR("java plugin: cjni_api_log: GetStringUTFChars failed.");
1485 if (severity < LOG_ERR)
1487 if (severity > LOG_DEBUG)
1488 severity = LOG_DEBUG;
1490 plugin_log(severity, "%s", c_str);
1492 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_message, c_str);
1493 } /* }}} void cjni_api_log */
1495 static jstring JNICALL cjni_api_get_hostname(JNIEnv *jvm_env, jobject this) {
1496 return ctoj_output_string(jvm_env, hostname_g);
1499 /* List of ``native'' functions, i. e. C-functions that can be called from
1501 static JNINativeMethod jni_api_functions[] = /* {{{ */
1503 {"dispatchValues", "(Lorg/collectd/api/ValueList;)I",
1504 cjni_api_dispatch_values},
1506 {"dispatchNotification", "(Lorg/collectd/api/Notification;)I",
1507 cjni_api_dispatch_notification},
1509 {"getDS", "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1513 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1514 cjni_api_register_config},
1517 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1518 cjni_api_register_init},
1521 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1522 cjni_api_register_read},
1525 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1526 cjni_api_register_write},
1529 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1530 cjni_api_register_flush},
1532 {"registerShutdown",
1533 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1534 cjni_api_register_shutdown},
1537 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1538 cjni_api_register_log},
1540 {"registerNotification", "(Ljava/lang/String;Lorg/collectd/api/"
1541 "CollectdNotificationInterface;)I",
1542 cjni_api_register_notification},
1544 {"registerMatch", "(Ljava/lang/String;Lorg/collectd/api/"
1545 "CollectdMatchFactoryInterface;)I",
1546 cjni_api_register_match},
1548 {"registerTarget", "(Ljava/lang/String;Lorg/collectd/api/"
1549 "CollectdTargetFactoryInterface;)I",
1550 cjni_api_register_target},
1552 {"log", "(ILjava/lang/String;)V", cjni_api_log},
1554 {"getHostname", "()Ljava/lang/String;", cjni_api_get_hostname},
1557 static size_t jni_api_functions_num =
1558 sizeof(jni_api_functions) / sizeof(jni_api_functions[0]);
1564 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1565 * all registration functions. */
1566 static cjni_callback_info_t *
1567 cjni_callback_info_create(JNIEnv *jvm_env, /* {{{ */
1568 jobject o_name, jobject o_callback, int type) {
1570 cjni_callback_info_t *cbi;
1571 const char *method_name;
1572 const char *method_signature;
1575 case CB_TYPE_CONFIG:
1576 method_name = "config";
1577 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1581 method_name = "init";
1582 method_signature = "()I";
1586 method_name = "read";
1587 method_signature = "()I";
1591 method_name = "write";
1592 method_signature = "(Lorg/collectd/api/ValueList;)I";
1596 method_name = "flush";
1597 method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I";
1600 case CB_TYPE_SHUTDOWN:
1601 method_name = "shutdown";
1602 method_signature = "()I";
1606 method_name = "log";
1607 method_signature = "(ILjava/lang/String;)V";
1610 case CB_TYPE_NOTIFICATION:
1611 method_name = "notification";
1612 method_signature = "(Lorg/collectd/api/Notification;)I";
1616 method_name = "createMatch";
1617 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1618 "Lorg/collectd/api/CollectdMatchInterface;";
1621 case CB_TYPE_TARGET:
1622 method_name = "createTarget";
1623 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1624 "Lorg/collectd/api/CollectdTargetInterface;";
1628 ERROR("java plugin: cjni_callback_info_create: Unknown type: %#x", type);
1632 c_name = (*jvm_env)->GetStringUTFChars(jvm_env, o_name, 0);
1633 if (c_name == NULL) {
1634 ERROR("java plugin: cjni_callback_info_create: "
1635 "GetStringUTFChars failed.");
1639 cbi = calloc(1, sizeof(*cbi));
1641 ERROR("java plugin: cjni_callback_info_create: calloc failed.");
1642 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1647 cbi->name = strdup(c_name);
1648 if (cbi->name == NULL) {
1649 pthread_mutex_unlock(&java_callbacks_lock);
1650 ERROR("java plugin: cjni_callback_info_create: strdup failed.");
1651 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1656 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1658 cbi->object = (*jvm_env)->NewGlobalRef(jvm_env, o_callback);
1659 if (cbi->object == NULL) {
1660 ERROR("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1666 cbi->class = (*jvm_env)->GetObjectClass(jvm_env, cbi->object);
1667 if (cbi->class == NULL) {
1668 ERROR("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1669 (*jvm_env)->DeleteGlobalRef(jvm_env, cbi->object);
1675 cbi->method = (*jvm_env)->GetMethodID(jvm_env, cbi->class, method_name,
1677 if (cbi->method == NULL) {
1678 ERROR("java plugin: cjni_callback_info_create: "
1679 "Cannot find the `%s' method with signature `%s'.",
1680 method_name, method_signature);
1681 (*jvm_env)->DeleteGlobalRef(jvm_env, cbi->object);
1688 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1690 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1691 * to the global `java_callbacks' variable. This is used for `config', `init',
1692 * and `shutdown' callbacks. */
1693 static int cjni_callback_register(JNIEnv *jvm_env, /* {{{ */
1694 jobject o_name, jobject o_callback,
1696 cjni_callback_info_t *cbi;
1697 cjni_callback_info_t *tmp;
1699 const char *type_str;
1702 cbi = cjni_callback_info_create(jvm_env, o_name, o_callback, type);
1708 case CB_TYPE_CONFIG:
1709 type_str = "config";
1716 case CB_TYPE_SHUTDOWN:
1717 type_str = "shutdown";
1724 case CB_TYPE_TARGET:
1725 type_str = "target";
1729 type_str = "<unknown>";
1731 DEBUG("java plugin: Registering new %s callback: %s", type_str, cbi->name);
1734 pthread_mutex_lock(&java_callbacks_lock);
1736 tmp = realloc(java_callbacks,
1737 (java_callbacks_num + 1) * sizeof(*java_callbacks));
1739 pthread_mutex_unlock(&java_callbacks_lock);
1740 ERROR("java plugin: cjni_callback_register: realloc failed.");
1742 (*jvm_env)->DeleteGlobalRef(jvm_env, cbi->object);
1747 java_callbacks = tmp;
1748 java_callbacks[java_callbacks_num] = *cbi;
1749 java_callbacks_num++;
1751 pthread_mutex_unlock(&java_callbacks_lock);
1755 } /* }}} int cjni_callback_register */
1757 /* Callback for `pthread_key_create'. It frees the data contained in
1758 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1759 static void cjni_jvm_env_destroy(void *args) /* {{{ */
1761 cjni_jvm_env_t *cjni_env;
1766 cjni_env = (cjni_jvm_env_t *)args;
1768 if (cjni_env->reference_counter > 0) {
1769 ERROR("java plugin: cjni_jvm_env_destroy: "
1770 "cjni_env->reference_counter = %i;",
1771 cjni_env->reference_counter);
1774 if (cjni_env->jvm_env != NULL) {
1775 ERROR("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1776 (void *)cjni_env->jvm_env);
1779 /* The pointer is allocated in `cjni_thread_attach' */
1781 } /* }}} void cjni_jvm_env_destroy */
1783 /* Register ``native'' functions with the JVM. Native functions are C-functions
1784 * that can be called by Java code. */
1785 static int cjni_init_native(JNIEnv *jvm_env) /* {{{ */
1787 jclass api_class_ptr;
1790 api_class_ptr = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/Collectd");
1791 if (api_class_ptr == NULL) {
1792 ERROR("cjni_init_native: Cannot find the API class \"org.collectd.api"
1793 ".Collectd\". Please set the correct class path "
1794 "using 'JVMArg \"-Djava.class.path=...\"'.");
1798 status = (*jvm_env)->RegisterNatives(
1799 jvm_env, api_class_ptr, jni_api_functions, (jint)jni_api_functions_num);
1801 ERROR("cjni_init_native: RegisterNatives failed with status %i.", status);
1806 } /* }}} int cjni_init_native */
1808 /* Create the JVM. This is called when the first thread tries to access the JVM
1809 * via cjni_thread_attach. */
1810 static int cjni_create_jvm(void) /* {{{ */
1813 JavaVMInitArgs vm_args = {0};
1814 JavaVMOption vm_options[jvm_argc];
1821 status = pthread_key_create(&jvm_env_key, cjni_jvm_env_destroy);
1823 ERROR("java plugin: cjni_create_jvm: pthread_key_create failed "
1831 vm_args.version = JNI_VERSION_1_2;
1832 vm_args.options = vm_options;
1833 vm_args.nOptions = (jint)jvm_argc;
1835 for (size_t i = 0; i < jvm_argc; i++) {
1836 DEBUG("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s", i, jvm_argv[i]);
1837 vm_args.options[i].optionString = jvm_argv[i];
1840 status = JNI_CreateJavaVM(&jvm, (void *)&jvm_env, (void *)&vm_args);
1842 ERROR("java plugin: cjni_create_jvm: "
1843 "JNI_CreateJavaVM failed with status %i.",
1847 assert(jvm != NULL);
1848 assert(jvm_env != NULL);
1850 /* Call RegisterNatives */
1851 status = cjni_init_native(jvm_env);
1853 ERROR("java plugin: cjni_create_jvm: cjni_init_native failed.");
1857 DEBUG("java plugin: The JVM has been created.");
1859 } /* }}} int cjni_create_jvm */
1861 /* Increase the reference counter to the JVM for this thread. If it was zero,
1862 * attach the JVM first. */
1863 static JNIEnv *cjni_thread_attach(void) /* {{{ */
1865 cjni_jvm_env_t *cjni_env;
1868 /* If we're the first thread to access the JVM, we'll have to create it
1873 status = cjni_create_jvm();
1875 ERROR("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
1879 assert(jvm != NULL);
1881 cjni_env = pthread_getspecific(jvm_env_key);
1882 if (cjni_env == NULL) {
1883 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
1884 cjni_env = calloc(1, sizeof(*cjni_env));
1885 if (cjni_env == NULL) {
1886 ERROR("java plugin: cjni_thread_attach: calloc failed.");
1889 cjni_env->reference_counter = 0;
1890 cjni_env->jvm_env = NULL;
1892 pthread_setspecific(jvm_env_key, cjni_env);
1895 if (cjni_env->reference_counter > 0) {
1896 cjni_env->reference_counter++;
1897 jvm_env = cjni_env->jvm_env;
1900 JavaVMAttachArgs args = {0};
1902 assert(cjni_env->jvm_env == NULL);
1904 args.version = JNI_VERSION_1_2;
1906 status = (*jvm)->AttachCurrentThread(jvm, (void *)&jvm_env, (void *)&args);
1908 ERROR("java plugin: cjni_thread_attach: AttachCurrentThread failed "
1914 cjni_env->reference_counter = 1;
1915 cjni_env->jvm_env = jvm_env;
1918 DEBUG("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
1919 cjni_env->reference_counter);
1920 assert(jvm_env != NULL);
1922 } /* }}} JNIEnv *cjni_thread_attach */
1924 /* Decrease the reference counter of this thread. If it reaches zero, detach
1926 static int cjni_thread_detach(void) /* {{{ */
1928 cjni_jvm_env_t *cjni_env;
1931 cjni_env = pthread_getspecific(jvm_env_key);
1932 if (cjni_env == NULL) {
1933 ERROR("java plugin: cjni_thread_detach: pthread_getspecific failed.");
1937 assert(cjni_env->reference_counter > 0);
1938 assert(cjni_env->jvm_env != NULL);
1940 cjni_env->reference_counter--;
1941 DEBUG("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
1942 cjni_env->reference_counter);
1944 if (cjni_env->reference_counter > 0)
1947 status = (*jvm)->DetachCurrentThread(jvm);
1949 ERROR("java plugin: cjni_thread_detach: DetachCurrentThread failed "
1954 cjni_env->reference_counter = 0;
1955 cjni_env->jvm_env = NULL;
1958 } /* }}} int cjni_thread_detach */
1960 static int cjni_config_add_jvm_arg(oconfig_item_t *ci) /* {{{ */
1964 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1965 WARNING("java plugin: `JVMArg' needs exactly one string argument.");
1970 ERROR("java plugin: All `JVMArg' options MUST appear before all "
1971 "`LoadPlugin' options! The JVM is already started and I have to "
1972 "ignore this argument: %s",
1973 ci->values[0].value.string);
1977 tmp = realloc(jvm_argv, sizeof(char *) * (jvm_argc + 1));
1979 ERROR("java plugin: realloc failed.");
1984 jvm_argv[jvm_argc] = strdup(ci->values[0].value.string);
1985 if (jvm_argv[jvm_argc] == NULL) {
1986 ERROR("java plugin: strdup failed.");
1992 } /* }}} int cjni_config_add_jvm_arg */
1994 static int cjni_config_load_plugin(oconfig_item_t *ci) /* {{{ */
1997 java_plugin_class_t *class;
1998 jmethodID constructor_id;
2001 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2002 WARNING("java plugin: `LoadPlugin' needs exactly one string argument.");
2006 jvm_env = cjni_thread_attach();
2007 if (jvm_env == NULL)
2010 class = realloc(java_classes_list,
2011 (java_classes_list_len + 1) * sizeof(*java_classes_list));
2012 if (class == NULL) {
2013 ERROR("java plugin: realloc failed.");
2014 cjni_thread_detach();
2017 java_classes_list = class;
2018 class = java_classes_list + java_classes_list_len;
2020 memset(class, 0, sizeof(*class));
2021 class->name = strdup(ci->values[0].value.string);
2022 if (class->name == NULL) {
2023 ERROR("java plugin: strdup failed.");
2024 cjni_thread_detach();
2027 class->class = NULL;
2028 class->object = NULL;
2030 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2031 thorough the Java community, but (Sun's) `FindClass' and friends need
2033 for (size_t i = 0; class->name[i] != 0; i++)
2034 if (class->name[i] == '.')
2035 class->name[i] = '/';
2038 DEBUG("java plugin: Loading class %s", class->name);
2040 class->class = (*jvm_env)->FindClass(jvm_env, class->name);
2041 if (class->class == NULL) {
2042 ERROR("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2044 cjni_thread_detach();
2050 (*jvm_env)->GetMethodID(jvm_env, class->class, "<init>", "()V");
2051 if (constructor_id == NULL) {
2052 ERROR("java plugin: cjni_config_load_plugin: "
2053 "Could not find the constructor for `%s'.",
2055 cjni_thread_detach();
2060 tmp_object = (*jvm_env)->NewObject(jvm_env, class->class, constructor_id);
2061 if (tmp_object != NULL)
2062 class->object = (*jvm_env)->NewGlobalRef(jvm_env, tmp_object);
2064 class->object = NULL;
2065 if (class->object == NULL) {
2066 ERROR("java plugin: cjni_config_load_plugin: "
2067 "Could create a new `%s' object.",
2069 cjni_thread_detach();
2074 cjni_thread_detach();
2076 java_classes_list_len++;
2079 } /* }}} int cjni_config_load_plugin */
2081 static int cjni_config_plugin_block(oconfig_item_t *ci) /* {{{ */
2084 cjni_callback_info_t *cbi;
2091 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2092 WARNING("java plugin: `Plugin' blocks "
2093 "need exactly one string argument.");
2097 name = ci->values[0].value.string;
2100 for (size_t i = 0; i < java_callbacks_num; i++) {
2101 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2104 if (strcmp(name, java_callbacks[i].name) != 0)
2107 cbi = java_callbacks + i;
2112 NOTICE("java plugin: Configuration block for `%s' found, but no such "
2113 "configuration callback has been registered. Please make sure, the "
2114 "`LoadPlugin' lines precede the `Plugin' blocks.",
2119 DEBUG("java plugin: Configuring %s", name);
2121 jvm_env = cjni_thread_attach();
2122 if (jvm_env == NULL)
2125 o_ocitem = ctoj_oconfig_item(jvm_env, ci);
2126 if (o_ocitem == NULL) {
2127 ERROR("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2128 cjni_thread_detach();
2132 class = (*jvm_env)->GetObjectClass(jvm_env, cbi->object);
2133 method = (*jvm_env)->GetMethodID(jvm_env, class, "config",
2134 "(Lorg/collectd/api/OConfigItem;)I");
2136 (*jvm_env)->CallIntMethod(jvm_env, cbi->object, method, o_ocitem);
2138 (*jvm_env)->DeleteLocalRef(jvm_env, o_ocitem);
2139 cjni_thread_detach();
2141 } /* }}} int cjni_config_plugin_block */
2143 static int cjni_config_perform(oconfig_item_t *ci) /* {{{ */
2152 for (int i = 0; i < ci->children_num; i++) {
2153 oconfig_item_t *child = ci->children + i;
2155 if (strcasecmp("JVMArg", child->key) == 0) {
2156 status = cjni_config_add_jvm_arg(child);
2161 } else if (strcasecmp("LoadPlugin", child->key) == 0) {
2162 status = cjni_config_load_plugin(child);
2167 } else if (strcasecmp("Plugin", child->key) == 0) {
2168 status = cjni_config_plugin_block(child);
2174 WARNING("java plugin: Option `%s' not allowed here.", child->key);
2179 DEBUG("java plugin: jvm_argc = %zu;", jvm_argc);
2180 DEBUG("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2182 if ((success == 0) && (errors > 0)) {
2183 ERROR("java plugin: All statements failed.");
2188 } /* }}} int cjni_config_perform */
2190 /* Copy the children of `ci' to the global `config_block' variable. */
2191 static int cjni_config_callback(oconfig_item_t *ci) /* {{{ */
2193 oconfig_item_t *ci_copy;
2194 oconfig_item_t *tmp;
2197 if (ci->children_num == 0)
2198 return (0); /* nothing to do */
2200 ci_copy = oconfig_clone(ci);
2201 if (ci_copy == NULL) {
2202 ERROR("java plugin: oconfig_clone failed.");
2206 if (config_block == NULL) {
2207 config_block = ci_copy;
2211 tmp = realloc(config_block->children,
2212 (config_block->children_num + ci_copy->children_num) *
2215 ERROR("java plugin: realloc failed.");
2216 oconfig_free(ci_copy);
2219 config_block->children = tmp;
2221 /* Copy the pointers */
2222 memcpy(config_block->children + config_block->children_num, ci_copy->children,
2223 ci_copy->children_num * sizeof(*ci_copy->children));
2224 config_block->children_num += ci_copy->children_num;
2226 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2227 memset(ci_copy->children, 0,
2228 ci_copy->children_num * sizeof(*ci_copy->children));
2229 ci_copy->children_num = 0;
2231 oconfig_free(ci_copy);
2234 } /* }}} int cjni_config_callback */
2236 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2237 * and `cjni_write'. In particular, delete the global reference to the Java
2239 static void cjni_callback_info_destroy(void *arg) /* {{{ */
2242 cjni_callback_info_t *cbi;
2244 DEBUG("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2246 cbi = (cjni_callback_info_t *)arg;
2248 /* This condition can occur when shutting down. */
2257 jvm_env = cjni_thread_attach();
2258 if (jvm_env == NULL) {
2260 "java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2264 (*jvm_env)->DeleteGlobalRef(jvm_env, cbi->object);
2271 cjni_thread_detach();
2272 } /* }}} void cjni_callback_info_destroy */
2274 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2275 static int cjni_read(user_data_t *ud) /* {{{ */
2278 cjni_callback_info_t *cbi;
2282 ERROR("java plugin: cjni_read: jvm == NULL");
2286 if ((ud == NULL) || (ud->data == NULL)) {
2287 ERROR("java plugin: cjni_read: Invalid user data.");
2291 jvm_env = cjni_thread_attach();
2292 if (jvm_env == NULL)
2295 cbi = (cjni_callback_info_t *)ud->data;
2297 ret_status = (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method);
2299 cjni_thread_detach();
2300 return (ret_status);
2301 } /* }}} int cjni_read */
2303 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2304 static int cjni_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
2307 cjni_callback_info_t *cbi;
2312 ERROR("java plugin: cjni_write: jvm == NULL");
2316 if ((ud == NULL) || (ud->data == NULL)) {
2317 ERROR("java plugin: cjni_write: Invalid user data.");
2321 jvm_env = cjni_thread_attach();
2322 if (jvm_env == NULL)
2325 cbi = (cjni_callback_info_t *)ud->data;
2327 vl_java = ctoj_value_list(jvm_env, ds, vl);
2328 if (vl_java == NULL) {
2329 ERROR("java plugin: cjni_write: ctoj_value_list failed.");
2330 cjni_thread_detach();
2335 (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method, vl_java);
2337 (*jvm_env)->DeleteLocalRef(jvm_env, vl_java);
2339 cjni_thread_detach();
2340 return (ret_status);
2341 } /* }}} int cjni_write */
2343 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2344 static int cjni_flush(cdtime_t timeout, const char *identifier, /* {{{ */
2347 cjni_callback_info_t *cbi;
2349 jobject o_identifier;
2353 ERROR("java plugin: cjni_flush: jvm == NULL");
2357 if ((ud == NULL) || (ud->data == NULL)) {
2358 ERROR("java plugin: cjni_flush: Invalid user data.");
2362 jvm_env = cjni_thread_attach();
2363 if (jvm_env == NULL)
2366 cbi = (cjni_callback_info_t *)ud->data;
2369 ctoj_jdouble_to_number(jvm_env, (jdouble)CDTIME_T_TO_DOUBLE(timeout));
2370 if (o_timeout == NULL) {
2371 ERROR("java plugin: cjni_flush: Converting double "
2372 "to Number object failed.");
2373 cjni_thread_detach();
2377 o_identifier = NULL;
2378 if (identifier != NULL) {
2379 o_identifier = (*jvm_env)->NewStringUTF(jvm_env, identifier);
2380 if (o_identifier == NULL) {
2381 (*jvm_env)->DeleteLocalRef(jvm_env, o_timeout);
2382 ERROR("java plugin: cjni_flush: NewStringUTF failed.");
2383 cjni_thread_detach();
2388 ret_status = (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method,
2389 o_timeout, o_identifier);
2391 (*jvm_env)->DeleteLocalRef(jvm_env, o_identifier);
2392 (*jvm_env)->DeleteLocalRef(jvm_env, o_timeout);
2394 cjni_thread_detach();
2395 return (ret_status);
2396 } /* }}} int cjni_flush */
2398 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2399 static void cjni_log(int severity, const char *message, /* {{{ */
2402 cjni_callback_info_t *cbi;
2408 if ((ud == NULL) || (ud->data == NULL))
2411 jvm_env = cjni_thread_attach();
2412 if (jvm_env == NULL)
2415 cbi = (cjni_callback_info_t *)ud->data;
2417 o_message = (*jvm_env)->NewStringUTF(jvm_env, message);
2418 if (o_message == NULL) {
2419 cjni_thread_detach();
2423 (*jvm_env)->CallVoidMethod(jvm_env, cbi->object, cbi->method, (jint)severity,
2426 (*jvm_env)->DeleteLocalRef(jvm_env, o_message);
2428 cjni_thread_detach();
2429 } /* }}} void cjni_log */
2431 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2433 static int cjni_notification(const notification_t *n, /* {{{ */
2436 cjni_callback_info_t *cbi;
2437 jobject o_notification;
2441 ERROR("java plugin: cjni_read: jvm == NULL");
2445 if ((ud == NULL) || (ud->data == NULL)) {
2446 ERROR("java plugin: cjni_read: Invalid user data.");
2450 jvm_env = cjni_thread_attach();
2451 if (jvm_env == NULL)
2454 cbi = (cjni_callback_info_t *)ud->data;
2456 o_notification = ctoj_notification(jvm_env, n);
2457 if (o_notification == NULL) {
2458 ERROR("java plugin: cjni_notification: ctoj_notification failed.");
2459 cjni_thread_detach();
2463 ret_status = (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method,
2466 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification);
2468 cjni_thread_detach();
2469 return (ret_status);
2470 } /* }}} int cjni_notification */
2472 /* Callbacks for matches implemented in Java */
2473 static int cjni_match_target_create(const oconfig_item_t *ci, /* {{{ */
2476 cjni_callback_info_t *cbi_ret;
2477 cjni_callback_info_t *cbi_factory;
2487 #define BAIL_OUT(status) \
2488 if (cbi_ret != NULL) { \
2489 free(cbi_ret->name); \
2490 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2491 (*jvm_env)->DeleteLocalRef(jvm_env, cbi_ret->object); \
2495 (*jvm_env)->DeleteLocalRef(jvm_env, o_ci); \
2496 cjni_thread_detach(); \
2500 ERROR("java plugin: cjni_read: jvm == NULL");
2504 jvm_env = cjni_thread_attach();
2505 if (jvm_env == NULL)
2508 /* Find out whether to create a match or a target. */
2509 if (strcasecmp("Match", ci->key) == 0)
2510 type = CB_TYPE_MATCH;
2511 else if (strcasecmp("Target", ci->key) == 0)
2512 type = CB_TYPE_TARGET;
2514 ERROR("java plugin: cjni_match_target_create: Can't figure out whether "
2515 "to create a match or a target.");
2519 /* This is the name of the match we should create. */
2520 name = ci->values[0].value.string;
2522 /* Lets see if we have a matching factory here.. */
2524 for (size_t i = 0; i < java_callbacks_num; i++) {
2525 if (java_callbacks[i].type != type)
2528 if (strcmp(name, java_callbacks[i].name) != 0)
2531 cbi_factory = java_callbacks + i;
2535 /* Nope, no factory for that name.. */
2536 if (cbi_factory == NULL) {
2537 ERROR("java plugin: cjni_match_target_create: "
2538 "No such match factory registered: %s",
2543 /* We convert `ci' to its Java equivalent.. */
2544 o_ci = ctoj_oconfig_item(jvm_env, ci);
2546 ERROR("java plugin: cjni_match_target_create: "
2547 "ctoj_oconfig_item failed.");
2551 /* Allocate a new callback info structure. This is going to be our user_data
2553 cbi_ret = calloc(1, sizeof(*cbi_ret));
2554 if (cbi_ret == NULL) {
2555 ERROR("java plugin: cjni_match_target_create: calloc failed.");
2559 cbi_ret->object = NULL;
2560 cbi_ret->type = type;
2562 /* Lets fill the callback info structure.. First, the name: */
2563 cbi_ret->name = strdup(name);
2564 if (cbi_ret->name == NULL) {
2565 ERROR("java plugin: cjni_match_target_create: strdup failed.");
2569 /* Then call the factory method so it creates a new object for us. */
2570 o_tmp = (*jvm_env)->CallObjectMethod(jvm_env, cbi_factory->object,
2571 cbi_factory->method, o_ci);
2572 if (o_tmp == NULL) {
2573 ERROR("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2577 cbi_ret->object = (*jvm_env)->NewGlobalRef(jvm_env, o_tmp);
2578 if (o_tmp == NULL) {
2579 ERROR("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2583 /* This is the class of the match. It is possibly different from the class of
2584 * the match-factory! */
2585 cbi_ret->class = (*jvm_env)->GetObjectClass(jvm_env, cbi_ret->object);
2586 if (cbi_ret->class == NULL) {
2587 ERROR("java plugin: cjni_match_target_create: GetObjectClass failed.");
2591 /* Lookup the `int match (DataSet, ValueList)' method. */
2592 cbi_ret->method = (*jvm_env)->GetMethodID(
2593 jvm_env, cbi_ret->class,
2594 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2595 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2596 if (cbi_ret->method == NULL) {
2597 ERROR("java plugin: cjni_match_target_create: GetMethodID failed.");
2601 /* Return the newly created match via the user_data pointer. */
2602 *user_data = (void *)cbi_ret;
2604 cjni_thread_detach();
2606 DEBUG("java plugin: cjni_match_target_create: "
2607 "Successfully created a `%s' %s.",
2608 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2613 } /* }}} int cjni_match_target_create */
2615 static int cjni_match_target_destroy(void **user_data) /* {{{ */
2617 cjni_callback_info_destroy(*user_data);
2621 } /* }}} int cjni_match_target_destroy */
2623 static int cjni_match_target_invoke(const data_set_t *ds, /* {{{ */
2625 notification_meta_t **meta,
2628 cjni_callback_info_t *cbi;
2635 ERROR("java plugin: cjni_match_target_invoke: jvm == NULL");
2639 jvm_env = cjni_thread_attach();
2640 if (jvm_env == NULL)
2643 cbi = (cjni_callback_info_t *)*user_data;
2645 o_vl = ctoj_value_list(jvm_env, ds, vl);
2647 ERROR("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2648 cjni_thread_detach();
2652 o_ds = ctoj_data_set(jvm_env, ds);
2654 ERROR("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2655 cjni_thread_detach();
2660 (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method, o_ds, o_vl);
2662 DEBUG("java plugin: cjni_match_target_invoke: Method returned %i.",
2665 /* If we're executing a target, copy the `ValueList' back to our
2666 * `value_list_t'. */
2667 if (cbi->type == CB_TYPE_TARGET) {
2668 value_list_t new_vl = {0};
2670 status = jtoc_value_list(jvm_env, &new_vl, o_vl);
2672 ERROR("java plugin: cjni_match_target_invoke: "
2673 "jtoc_value_list failed.");
2674 } else /* if (status == 0) */
2676 /* plugin_dispatch_values assures that this is dynamically allocated
2680 /* This will replace the vl->values pointer to a new, dynamically
2681 * allocated piece of memory. */
2682 memcpy(vl, &new_vl, sizeof(*vl));
2684 } /* if (cbi->type == CB_TYPE_TARGET) */
2686 cjni_thread_detach();
2687 return (ret_status);
2688 } /* }}} int cjni_match_target_invoke */
2690 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2691 static int cjni_init_plugins(JNIEnv *jvm_env) /* {{{ */
2695 for (size_t i = 0; i < java_callbacks_num; i++) {
2696 if (java_callbacks[i].type != CB_TYPE_INIT)
2699 DEBUG("java plugin: Initializing %s", java_callbacks[i].name);
2701 status = (*jvm_env)->CallIntMethod(jvm_env, java_callbacks[i].object,
2702 java_callbacks[i].method);
2704 ERROR("java plugin: Initializing `%s' failed with status %i. "
2705 "Removing read function.",
2706 java_callbacks[i].name, status);
2707 plugin_unregister_read(java_callbacks[i].name);
2712 } /* }}} int cjni_init_plugins */
2714 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2715 static int cjni_shutdown_plugins(JNIEnv *jvm_env) /* {{{ */
2719 for (size_t i = 0; i < java_callbacks_num; i++) {
2720 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2723 DEBUG("java plugin: Shutting down %s", java_callbacks[i].name);
2725 status = (*jvm_env)->CallIntMethod(jvm_env, java_callbacks[i].object,
2726 java_callbacks[i].method);
2728 ERROR("java plugin: Shutting down `%s' failed with status %i. ",
2729 java_callbacks[i].name, status);
2734 } /* }}} int cjni_shutdown_plugins */
2736 static int cjni_shutdown(void) /* {{{ */
2739 JavaVMAttachArgs args = {0};
2746 args.version = JNI_VERSION_1_2;
2748 status = (*jvm)->AttachCurrentThread(jvm, (void *)&jvm_env, &args);
2750 ERROR("java plugin: cjni_shutdown: AttachCurrentThread failed with status "
2756 /* Execute all the shutdown functions registered by plugins. */
2757 cjni_shutdown_plugins(jvm_env);
2759 /* Release all the global references to callback functions */
2760 for (size_t i = 0; i < java_callbacks_num; i++) {
2761 if (java_callbacks[i].object != NULL) {
2762 (*jvm_env)->DeleteGlobalRef(jvm_env, java_callbacks[i].object);
2763 java_callbacks[i].object = NULL;
2765 sfree(java_callbacks[i].name);
2767 java_callbacks_num = 0;
2768 sfree(java_callbacks);
2770 /* Release all the global references to directly loaded classes. */
2771 for (size_t i = 0; i < java_classes_list_len; i++) {
2772 if (java_classes_list[i].object != NULL) {
2773 (*jvm_env)->DeleteGlobalRef(jvm_env, java_classes_list[i].object);
2774 java_classes_list[i].object = NULL;
2776 sfree(java_classes_list[i].name);
2778 java_classes_list_len = 0;
2779 sfree(java_classes_list);
2781 /* Destroy the JVM */
2782 DEBUG("java plugin: Destroying the JVM.");
2783 (*jvm)->DestroyJavaVM(jvm);
2787 pthread_key_delete(jvm_env_key);
2789 /* Free the JVM argument list */
2790 for (size_t i = 0; i < jvm_argc; i++)
2796 } /* }}} int cjni_shutdown */
2798 /* Initialization: Create a JVM, load all configured classes and call their
2799 * `config' and `init' callback methods. */
2800 static int cjni_init(void) /* {{{ */
2804 if ((config_block == NULL) && (jvm == NULL)) {
2805 ERROR("java plugin: cjni_init: No configuration block for "
2806 "the java plugin was found.");
2810 if (config_block != NULL) {
2811 cjni_config_perform(config_block);
2812 oconfig_free(config_block);
2816 ERROR("java plugin: cjni_init: jvm == NULL");
2820 jvm_env = cjni_thread_attach();
2821 if (jvm_env == NULL)
2824 cjni_init_plugins(jvm_env);
2826 cjni_thread_detach();
2828 } /* }}} int cjni_init */
2830 void module_register(void) {
2831 plugin_register_complex_config("java", cjni_config_callback);
2832 plugin_register_init("java", cjni_init);
2833 plugin_register_shutdown("java", cjni_shutdown);
2834 } /* void module_register (void) */
2836 /* vim: set sw=2 sts=2 et fdm=marker : */