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>
28 #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,
106 jobject o_name, jobject o_callback, int type);
107 static int cjni_callback_register (JNIEnv *jvm_env, jobject o_name,
108 jobject o_callback, int type);
109 static int cjni_read (user_data_t *user_data);
110 static int cjni_write (const data_set_t *ds, const value_list_t *vl,
112 static int cjni_flush (cdtime_t timeout, const char *identifier, user_data_t *ud);
113 static void cjni_log (int severity, const char *message, user_data_t *ud);
114 static int cjni_notification (const notification_t *n, user_data_t *ud);
116 /* Create, destroy, and match/invoke functions, used by both, matches AND
118 static int cjni_match_target_create (const oconfig_item_t *ci, void **user_data);
119 static int cjni_match_target_destroy (void **user_data);
120 static int cjni_match_target_invoke (const data_set_t *ds, value_list_t *vl,
121 notification_meta_t **meta, void **user_data);
124 * C to Java conversion functions
126 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
128 jclass class_ptr, jobject object_ptr, const char *method_name)
133 /* Create a java.lang.String */
134 o_string = (*jvm_env)->NewStringUTF (jvm_env,
135 (string != NULL) ? string : "");
136 if (o_string == NULL)
138 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
142 /* Search for the `void setFoo (String s)' method. */
143 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
144 method_name, "(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, /* {{{ */
167 /* Create a java.lang.String */
168 o_string = (*jvm_env)->NewStringUTF (jvm_env,
169 (string != NULL) ? string : "");
170 if (o_string == NULL)
172 ERROR ("java plugin: ctoj_output_string: NewStringUTF failed.");
177 } /* }}} int ctoj_output_string */
179 static int ctoj_int (JNIEnv *jvm_env, /* {{{ */
181 jclass class_ptr, jobject object_ptr, const char *method_name)
185 /* Search for the `void setFoo (int i)' method. */
186 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
187 method_name, "(I)V");
190 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
195 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
198 } /* }}} int ctoj_int */
200 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
202 jclass class_ptr, jobject object_ptr, const char *method_name)
206 /* Search for the `void setFoo (long l)' method. */
207 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
208 method_name, "(J)V");
211 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
216 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
219 } /* }}} int ctoj_long */
221 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
223 jclass class_ptr, jobject object_ptr, const char *method_name)
227 /* Search for the `void setFoo (double d)' method. */
228 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
229 method_name, "(D)V");
232 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
237 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
240 } /* }}} int ctoj_double */
242 /* Convert a jlong to a java.lang.Number */
243 static jobject ctoj_jlong_to_number (JNIEnv *jvm_env, jlong value) /* {{{ */
246 jmethodID m_long_constructor;
248 /* Look up the java.lang.Long class */
249 c_long = (*jvm_env)->FindClass (jvm_env, "java/lang/Long");
252 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
253 "java.lang.Long class failed.");
257 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
258 c_long, "<init>", "(J)V");
259 if (m_long_constructor == NULL)
261 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
262 "`Long (long)' constructor failed.");
266 return ((*jvm_env)->NewObject (jvm_env,
267 c_long, m_long_constructor, value));
268 } /* }}} jobject ctoj_jlong_to_number */
270 /* Convert a jdouble to a java.lang.Number */
271 static jobject ctoj_jdouble_to_number (JNIEnv *jvm_env, jdouble value) /* {{{ */
274 jmethodID m_double_constructor;
276 /* Look up the java.lang.Long class */
277 c_double = (*jvm_env)->FindClass (jvm_env, "java/lang/Double");
278 if (c_double == NULL)
280 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
281 "java.lang.Double class failed.");
285 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
286 c_double, "<init>", "(D)V");
287 if (m_double_constructor == NULL)
289 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
290 "`Double (double)' constructor failed.");
294 return ((*jvm_env)->NewObject (jvm_env,
295 c_double, m_double_constructor, value));
296 } /* }}} jobject ctoj_jdouble_to_number */
298 /* Convert a value_t to a java.lang.Number */
299 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
300 value_t value, int ds_type)
302 if (ds_type == DS_TYPE_COUNTER)
303 return (ctoj_jlong_to_number (jvm_env, (jlong) value.counter));
304 else if (ds_type == DS_TYPE_GAUGE)
305 return (ctoj_jdouble_to_number (jvm_env, (jdouble) value.gauge));
306 if (ds_type == DS_TYPE_DERIVE)
307 return (ctoj_jlong_to_number (jvm_env, (jlong) value.derive));
308 if (ds_type == DS_TYPE_ABSOLUTE)
309 return (ctoj_jlong_to_number (jvm_env, (jlong) value.absolute));
312 } /* }}} jobject ctoj_value_to_number */
314 /* Convert a data_source_t to a org/collectd/api/DataSource */
315 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
316 const data_source_t *dsrc)
319 jmethodID m_datasource_constructor;
320 jobject o_datasource;
323 /* Look up the DataSource class */
324 c_datasource = (*jvm_env)->FindClass (jvm_env,
325 "org/collectd/api/DataSource");
326 if (c_datasource == NULL)
328 ERROR ("java plugin: ctoj_data_source: "
329 "FindClass (org/collectd/api/DataSource) failed.");
333 /* Lookup the `ValueList ()' constructor. */
334 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
336 if (m_datasource_constructor == NULL)
338 ERROR ("java plugin: ctoj_data_source: Cannot find the "
339 "`DataSource ()' constructor.");
343 /* Create a new instance. */
344 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
345 m_datasource_constructor);
346 if (o_datasource == NULL)
348 ERROR ("java plugin: ctoj_data_source: "
349 "Creating a new DataSource instance failed.");
353 /* Set name via `void setName (String name)' */
354 status = ctoj_string (jvm_env, dsrc->name,
355 c_datasource, o_datasource, "setName");
358 ERROR ("java plugin: ctoj_data_source: "
359 "ctoj_string (setName) failed.");
360 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
364 /* Set type via `void setType (int type)' */
365 status = ctoj_int (jvm_env, dsrc->type,
366 c_datasource, o_datasource, "setType");
369 ERROR ("java plugin: ctoj_data_source: "
370 "ctoj_int (setType) failed.");
371 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
375 /* Set min via `void setMin (double min)' */
376 status = ctoj_double (jvm_env, dsrc->min,
377 c_datasource, o_datasource, "setMin");
380 ERROR ("java plugin: ctoj_data_source: "
381 "ctoj_double (setMin) failed.");
382 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
386 /* Set max via `void setMax (double max)' */
387 status = ctoj_double (jvm_env, dsrc->max,
388 c_datasource, o_datasource, "setMax");
391 ERROR ("java plugin: ctoj_data_source: "
392 "ctoj_double (setMax) failed.");
393 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
397 return (o_datasource);
398 } /* }}} jobject ctoj_data_source */
400 /* Convert a oconfig_value_t to a org/collectd/api/OConfigValue */
401 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
402 oconfig_value_t ocvalue)
405 jmethodID m_ocvalue_constructor;
409 m_ocvalue_constructor = NULL;
412 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
413 "org/collectd/api/OConfigValue");
414 if (c_ocvalue == NULL)
416 ERROR ("java plugin: ctoj_oconfig_value: "
417 "FindClass (org/collectd/api/OConfigValue) failed.");
421 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
423 jboolean tmp_boolean;
425 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
427 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
429 if (m_ocvalue_constructor == NULL)
431 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
432 "`OConfigValue (boolean)' constructor.");
436 return ((*jvm_env)->NewObject (jvm_env,
437 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
438 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
439 else if (ocvalue.type == OCONFIG_TYPE_STRING)
441 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
442 "<init>", "(Ljava/lang/String;)V");
443 if (m_ocvalue_constructor == NULL)
445 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
446 "`OConfigValue (String)' constructor.");
450 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
451 if (o_argument == NULL)
453 ERROR ("java plugin: ctoj_oconfig_value: "
454 "Creating a String object failed.");
458 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
460 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
461 "<init>", "(Ljava/lang/Number;)V");
462 if (m_ocvalue_constructor == NULL)
464 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
465 "`OConfigValue (Number)' constructor.");
469 o_argument = ctoj_jdouble_to_number (jvm_env,
470 (jdouble) ocvalue.value.number);
471 if (o_argument == NULL)
473 ERROR ("java plugin: ctoj_oconfig_value: "
474 "Creating a Number object failed.");
483 assert (m_ocvalue_constructor != NULL);
484 assert (o_argument != NULL);
486 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
487 c_ocvalue, m_ocvalue_constructor, o_argument);
488 if (o_ocvalue == NULL)
490 ERROR ("java plugin: ctoj_oconfig_value: "
491 "Creating an OConfigValue object failed.");
492 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
496 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
498 } /* }}} jobject ctoj_oconfig_value */
500 /* Convert a oconfig_item_t to a org/collectd/api/OConfigItem */
501 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
502 const oconfig_item_t *ci)
505 jmethodID m_ocitem_constructor;
506 jmethodID m_addvalue;
507 jmethodID m_addchild;
511 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/OConfigItem");
512 if (c_ocitem == NULL)
514 ERROR ("java plugin: ctoj_oconfig_item: "
515 "FindClass (org/collectd/api/OConfigItem) failed.");
519 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
521 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
522 "<init>", "(Ljava/lang/String;)V");
523 if (m_ocitem_constructor == NULL)
525 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
526 "`OConfigItem (String)' constructor.");
530 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
531 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
532 if (m_addvalue == NULL)
534 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
535 "`addValue (OConfigValue)' method.");
539 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
540 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
541 if (m_addchild == NULL)
543 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
544 "`addChild (OConfigItem)' method.");
549 /* Create a String object with the key.
550 * Needed for calling the constructor. */
551 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
554 ERROR ("java plugin: ctoj_oconfig_item: "
555 "Creating String object failed.");
559 /* Create an OConfigItem object */
560 o_ocitem = (*jvm_env)->NewObject (jvm_env,
561 c_ocitem, m_ocitem_constructor, o_key);
562 if (o_ocitem == NULL)
564 ERROR ("java plugin: ctoj_oconfig_item: "
565 "Creating an OConfigItem object failed.");
566 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
570 /* We don't need the String object any longer.. */
571 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
573 /* Call OConfigItem.addValue for each value */
574 for (int i = 0; i < ci->values_num; i++) /* {{{ */
578 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
581 ERROR ("java plugin: ctoj_oconfig_item: "
582 "Creating an OConfigValue object failed.");
583 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
587 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
588 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
589 } /* }}} for (i = 0; i < ci->values_num; i++) */
591 /* Call OConfigItem.addChild for each child */
592 for (int i = 0; i < ci->children_num; i++) /* {{{ */
596 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
599 ERROR ("java plugin: ctoj_oconfig_item: "
600 "Creating an OConfigItem object failed.");
601 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
605 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addchild, o_child);
606 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
607 } /* }}} for (i = 0; i < ci->children_num; i++) */
610 } /* }}} jobject ctoj_oconfig_item */
612 /* Convert a data_set_t to a org/collectd/api/DataSet */
613 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
616 jmethodID m_constructor;
621 /* Look up the org/collectd/api/DataSet class */
622 c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet");
623 if (c_dataset == NULL)
625 ERROR ("java plugin: ctoj_data_set: Looking up the "
626 "org/collectd/api/DataSet class failed.");
630 /* Search for the `DataSet (String type)' constructor. */
631 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
632 c_dataset, "<init>", "(Ljava/lang/String;)V");
633 if (m_constructor == NULL)
635 ERROR ("java plugin: ctoj_data_set: Looking up the "
636 "`DataSet (String)' constructor failed.");
640 /* Search for the `void addDataSource (DataSource)' method. */
641 m_add = (*jvm_env)->GetMethodID (jvm_env,
642 c_dataset, "addDataSource", "(Lorg/collectd/api/DataSource;)V");
645 ERROR ("java plugin: ctoj_data_set: Looking up the "
646 "`addDataSource (DataSource)' method failed.");
650 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
653 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
657 o_dataset = (*jvm_env)->NewObject (jvm_env,
658 c_dataset, m_constructor, o_type);
659 if (o_dataset == NULL)
661 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
662 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
666 /* Decrease reference counter on the java.lang.String object. */
667 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
669 for (size_t i = 0; i < ds->ds_num; i++)
671 jobject o_datasource;
673 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
674 if (o_datasource == NULL)
676 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
677 ds->type, ds->ds[i].name);
678 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
682 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
684 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
685 } /* for (i = 0; i < ds->ds_num; i++) */
688 } /* }}} jobject ctoj_data_set */
690 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
691 value_t value, int ds_type,
692 jclass class_ptr, jobject object_ptr)
694 jmethodID m_addvalue;
697 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
698 "addValue", "(Ljava/lang/Number;)V");
699 if (m_addvalue == NULL)
701 ERROR ("java plugin: ctoj_value_list_add_value: "
702 "Cannot find method `void addValue (Number)'.");
706 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
707 if (o_number == NULL)
709 ERROR ("java plugin: ctoj_value_list_add_value: "
710 "ctoj_value_to_number failed.");
714 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
716 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
719 } /* }}} int ctoj_value_list_add_value */
721 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
722 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
724 jmethodID m_setdataset;
727 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
728 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
729 "setDataSet", "(Lorg/collectd/api/DataSet;)V");
730 if (m_setdataset == NULL)
732 ERROR ("java plugin: ctoj_value_list_add_data_set: "
733 "Cannot find the `void setDataSet (DataSet)' method.");
737 /* Create a DataSet object. */
738 o_dataset = ctoj_data_set (jvm_env, ds);
739 if (o_dataset == NULL)
741 ERROR ("java plugin: ctoj_value_list_add_data_set: "
742 "ctoj_data_set (%s) failed.", ds->type);
746 /* Actually call the method. */
747 (*jvm_env)->CallVoidMethod (jvm_env,
748 o_valuelist, m_setdataset, o_dataset);
750 /* Decrease reference counter on the List<DataSource> object. */
751 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
754 } /* }}} int ctoj_value_list_add_data_set */
756 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
757 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
758 const data_set_t *ds, const value_list_t *vl)
761 jmethodID m_valuelist_constructor;
765 /* First, create a new ValueList instance..
766 * Look up the class.. */
767 c_valuelist = (*jvm_env)->FindClass (jvm_env,
768 "org/collectd/api/ValueList");
769 if (c_valuelist == NULL)
771 ERROR ("java plugin: ctoj_value_list: "
772 "FindClass (org/collectd/api/ValueList) failed.");
776 /* Lookup the `ValueList ()' constructor. */
777 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
779 if (m_valuelist_constructor == NULL)
781 ERROR ("java plugin: ctoj_value_list: Cannot find the "
782 "`ValueList ()' constructor.");
786 /* Create a new instance. */
787 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
788 m_valuelist_constructor);
789 if (o_valuelist == NULL)
791 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
796 status = ctoj_value_list_add_data_set (jvm_env,
797 c_valuelist, o_valuelist, ds);
800 ERROR ("java plugin: ctoj_value_list: "
801 "ctoj_value_list_add_data_set failed.");
802 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
806 /* Set the strings.. */
807 #define SET_STRING(str,method_name) do { \
808 status = ctoj_string (jvm_env, str, \
809 c_valuelist, o_valuelist, method_name); \
811 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
813 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
817 SET_STRING (vl->host, "setHost");
818 SET_STRING (vl->plugin, "setPlugin");
819 SET_STRING (vl->plugin_instance, "setPluginInstance");
820 SET_STRING (vl->type, "setType");
821 SET_STRING (vl->type_instance, "setTypeInstance");
825 /* Set the `time' member. Java stores time in milliseconds. */
826 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (vl->time),
827 c_valuelist, o_valuelist, "setTime");
830 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
831 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
835 /* Set the `interval' member.. */
836 status = ctoj_long (jvm_env,
837 (jlong) CDTIME_T_TO_MS (vl->interval),
838 c_valuelist, o_valuelist, "setInterval");
841 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
842 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
846 for (size_t i = 0; i < vl->values_len; i++)
848 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
849 c_valuelist, o_valuelist);
852 ERROR ("java plugin: ctoj_value_list: "
853 "ctoj_value_list_add_value failed.");
854 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
859 return (o_valuelist);
860 } /* }}} jobject ctoj_value_list */
862 /* Convert a notification_t to a org/collectd/api/Notification */
863 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
864 const notification_t *n)
866 jclass c_notification;
867 jmethodID m_constructor;
868 jobject o_notification;
871 /* First, create a new Notification instance..
872 * Look up the class.. */
873 c_notification = (*jvm_env)->FindClass (jvm_env,
874 "org/collectd/api/Notification");
875 if (c_notification == NULL)
877 ERROR ("java plugin: ctoj_notification: "
878 "FindClass (org/collectd/api/Notification) failed.");
882 /* Lookup the `Notification ()' constructor. */
883 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
885 if (m_constructor == NULL)
887 ERROR ("java plugin: ctoj_notification: Cannot find the "
888 "`Notification ()' constructor.");
892 /* Create a new instance. */
893 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
895 if (o_notification == NULL)
897 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
902 /* Set the strings.. */
903 #define SET_STRING(str,method_name) do { \
904 status = ctoj_string (jvm_env, str, \
905 c_notification, o_notification, method_name); \
907 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
909 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
913 SET_STRING (n->host, "setHost");
914 SET_STRING (n->plugin, "setPlugin");
915 SET_STRING (n->plugin_instance, "setPluginInstance");
916 SET_STRING (n->type, "setType");
917 SET_STRING (n->type_instance, "setTypeInstance");
918 SET_STRING (n->message, "setMessage");
922 /* Set the `time' member. Java stores time in milliseconds. */
923 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (n->time),
924 c_notification, o_notification, "setTime");
927 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
928 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
932 /* Set the `severity' member.. */
933 status = ctoj_int (jvm_env, (jint) n->severity,
934 c_notification, o_notification, "setSeverity");
937 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
938 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
942 return (o_notification);
943 } /* }}} jobject ctoj_notification */
946 * Java to C conversion functions
948 /* Call a `String <method> ()' method. */
949 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
950 char *buffer, size_t buffer_size, int empty_okay,
951 jclass class_ptr, jobject object_ptr, const char *method_name)
957 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
958 method_name, "()Ljava/lang/String;");
959 if (method_id == NULL)
961 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
966 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
967 if ((string_obj == NULL) && (empty_okay == 0))
969 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
973 else if ((string_obj == NULL) && (empty_okay != 0))
975 memset (buffer, 0, buffer_size);
979 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
982 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
983 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
987 sstrncpy (buffer, c_str, buffer_size);
989 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
990 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
993 } /* }}} int jtoc_string */
995 /* Call an `int <method> ()' method. */
996 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
998 jclass class_ptr, jobject object_ptr, const char *method_name)
1000 jmethodID method_id;
1002 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1003 method_name, "()I");
1004 if (method_id == NULL)
1006 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
1011 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
1014 } /* }}} int jtoc_int */
1016 /* Call a `long <method> ()' method. */
1017 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
1019 jclass class_ptr, jobject object_ptr, const char *method_name)
1021 jmethodID method_id;
1023 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1024 method_name, "()J");
1025 if (method_id == NULL)
1027 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1032 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1035 } /* }}} int jtoc_long */
1037 /* Call a `double <method> ()' method. */
1038 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1040 jclass class_ptr, jobject object_ptr, const char *method_name)
1042 jmethodID method_id;
1044 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1045 method_name, "()D");
1046 if (method_id == NULL)
1048 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1053 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1056 } /* }}} int jtoc_double */
1058 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1059 value_t *ret_value, int ds_type, jobject object_ptr)
1064 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1066 if (ds_type == DS_TYPE_GAUGE)
1070 status = jtoc_double (jvm_env, &tmp_double,
1071 class_ptr, object_ptr, "doubleValue");
1074 ERROR ("java plugin: jtoc_value: "
1075 "jtoc_double failed.");
1078 (*ret_value).gauge = (gauge_t) tmp_double;
1084 status = jtoc_long (jvm_env, &tmp_long,
1085 class_ptr, object_ptr, "longValue");
1088 ERROR ("java plugin: jtoc_value: "
1089 "jtoc_long failed.");
1093 if (ds_type == DS_TYPE_DERIVE)
1094 (*ret_value).derive = (derive_t) tmp_long;
1095 else if (ds_type == DS_TYPE_ABSOLUTE)
1096 (*ret_value).absolute = (absolute_t) tmp_long;
1098 (*ret_value).counter = (counter_t) tmp_long;
1102 } /* }}} int jtoc_value */
1104 /* Read a List<Number>, convert it to `value_t' and add it to the given
1105 * `value_list_t'. */
1106 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1107 const data_set_t *ds, value_list_t *vl,
1108 jclass class_ptr, jobject object_ptr)
1110 jmethodID m_getvalues;
1111 jmethodID m_toarray;
1113 jobjectArray o_number_array;
1118 values_num = ds->ds_num;
1121 o_number_array = NULL;
1124 #define BAIL_OUT(status) \
1126 if (o_number_array != NULL) \
1127 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1128 if (o_list != NULL) \
1129 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1132 /* Call: List<Number> ValueList.getValues () */
1133 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1134 "getValues", "()Ljava/util/List;");
1135 if (m_getvalues == NULL)
1137 ERROR ("java plugin: jtoc_values_array: "
1138 "Cannot find method `List getValues ()'.");
1142 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1145 ERROR ("java plugin: jtoc_values_array: "
1146 "CallObjectMethod (getValues) failed.");
1150 /* Call: Number[] List.toArray () */
1151 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1152 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1153 "toArray", "()[Ljava/lang/Object;");
1154 if (m_toarray == NULL)
1156 ERROR ("java plugin: jtoc_values_array: "
1157 "Cannot find method `Object[] toArray ()'.");
1161 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1162 if (o_number_array == NULL)
1164 ERROR ("java plugin: jtoc_values_array: "
1165 "CallObjectMethod (toArray) failed.");
1169 values = (value_t *) calloc (values_num, sizeof (value_t));
1172 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1176 for (int i = 0; i < values_num; i++)
1181 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1182 o_number_array, (jsize) i);
1183 if (o_number == NULL)
1185 ERROR ("java plugin: jtoc_values_array: "
1186 "GetObjectArrayElement (%i) failed.", i);
1190 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1193 ERROR ("java plugin: jtoc_values_array: "
1194 "jtoc_value (%i) failed.", i);
1197 } /* for (i = 0; i < values_num; i++) */
1199 vl->values = values;
1200 vl->values_len = values_num;
1203 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1204 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1206 } /* }}} int jtoc_values_array */
1208 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1209 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1215 const data_set_t *ds;
1217 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1218 if (class_ptr == NULL)
1220 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1224 /* eo == empty okay */
1225 #define SET_STRING(buffer,method, eo) do { \
1226 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1227 class_ptr, object_ptr, method); \
1228 if (status != 0) { \
1229 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1234 SET_STRING(vl->type, "getType", /* empty = */ 0);
1236 ds = plugin_get_ds (vl->type);
1239 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1240 "Please consult the types.db(5) manpage for mor information.",
1245 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1246 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1247 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1248 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1252 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1255 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1258 /* Java measures time in milliseconds. */
1259 vl->time = MS_TO_CDTIME_T (tmp_long);
1261 status = jtoc_long (jvm_env, &tmp_long,
1262 class_ptr, object_ptr, "getInterval");
1265 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1268 vl->interval = MS_TO_CDTIME_T (tmp_long);
1270 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1273 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1278 } /* }}} int jtoc_value_list */
1280 /* Convert a org/collectd/api/Notification to a notification_t. */
1281 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1289 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1290 if (class_ptr == NULL)
1292 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1296 /* eo == empty okay */
1297 #define SET_STRING(buffer,method, eo) do { \
1298 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1299 class_ptr, object_ptr, method); \
1300 if (status != 0) { \
1301 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1306 SET_STRING (n->host, "getHost", /* empty = */ 1);
1307 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1308 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1309 SET_STRING (n->type, "getType", /* empty = */ 1);
1310 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1311 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1315 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1318 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1321 /* Java measures time in milliseconds. */
1322 n->time = MS_TO_CDTIME_T(tmp_long);
1324 status = jtoc_int (jvm_env, &tmp_int,
1325 class_ptr, object_ptr, "getSeverity");
1328 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1331 n->severity = (int) tmp_int;
1334 } /* }}} int jtoc_notification */
1336 * Functions accessible from Java
1338 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1339 jobject this, jobject java_vl)
1341 value_list_t vl = VALUE_LIST_INIT;
1344 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1346 status = jtoc_value_list (jvm_env, &vl, java_vl);
1349 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1353 status = plugin_dispatch_values (&vl);
1358 } /* }}} jint cjni_api_dispatch_values */
1360 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1361 jobject this, jobject o_notification)
1363 notification_t n = { 0 };
1366 status = jtoc_notification (jvm_env, &n, o_notification);
1369 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1373 status = plugin_dispatch_notification (&n);
1376 } /* }}} jint cjni_api_dispatch_notification */
1378 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1379 jobject this, jobject o_string_type)
1381 const char *ds_name;
1382 const data_set_t *ds;
1385 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1386 if (ds_name == NULL)
1388 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1392 ds = plugin_get_ds (ds_name);
1393 DEBUG ("java plugin: cjni_api_get_ds: "
1394 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1396 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1401 o_dataset = ctoj_data_set (jvm_env, ds);
1403 } /* }}} jint cjni_api_get_ds */
1405 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1406 jobject this, jobject o_name, jobject o_config)
1408 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1409 } /* }}} jint cjni_api_register_config */
1411 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1412 jobject this, jobject o_name, jobject o_config)
1414 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1415 } /* }}} jint cjni_api_register_init */
1417 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1418 jobject this, jobject o_name, jobject o_read)
1420 cjni_callback_info_t *cbi;
1422 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1426 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1428 plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read,
1429 /* interval = */ 0, &(user_data_t) {
1431 .free_func = cjni_callback_info_destroy,
1434 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1437 } /* }}} jint cjni_api_register_read */
1439 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1440 jobject this, jobject o_name, jobject o_write)
1442 cjni_callback_info_t *cbi;
1444 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1448 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1450 plugin_register_write (cbi->name, cjni_write, &(user_data_t) {
1452 .free_func = cjni_callback_info_destroy,
1455 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1458 } /* }}} jint cjni_api_register_write */
1460 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1461 jobject this, jobject o_name, jobject o_flush)
1463 cjni_callback_info_t *cbi;
1465 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1469 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1471 plugin_register_flush (cbi->name, cjni_flush, &(user_data_t) {
1473 .free_func = cjni_callback_info_destroy,
1476 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1479 } /* }}} jint cjni_api_register_flush */
1481 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1482 jobject this, jobject o_name, jobject o_shutdown)
1484 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1486 } /* }}} jint cjni_api_register_shutdown */
1488 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1489 jobject this, jobject o_name, jobject o_log)
1491 cjni_callback_info_t *cbi;
1493 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1497 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1499 plugin_register_log (cbi->name, cjni_log, &(user_data_t) {
1501 .free_func = cjni_callback_info_destroy,
1504 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1507 } /* }}} jint cjni_api_register_log */
1509 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1510 jobject this, jobject o_name, jobject o_notification)
1512 cjni_callback_info_t *cbi;
1514 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1515 CB_TYPE_NOTIFICATION);
1519 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1521 plugin_register_notification (cbi->name, cjni_notification, &(user_data_t) {
1523 .free_func = cjni_callback_info_destroy,
1526 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1529 } /* }}} jint cjni_api_register_notification */
1531 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1532 jobject this, jobject o_name, jobject o_match, int type)
1537 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1540 ERROR ("java plugin: cjni_api_register_match_target: "
1541 "GetStringUTFChars failed.");
1545 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1548 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1552 if (type == CB_TYPE_MATCH)
1554 match_proc_t m_proc = { 0 };
1556 m_proc.create = cjni_match_target_create;
1557 m_proc.destroy = cjni_match_target_destroy;
1558 m_proc.match = (void *) cjni_match_target_invoke;
1560 status = fc_register_match (c_name, m_proc);
1562 else if (type == CB_TYPE_TARGET)
1564 target_proc_t t_proc = { 0 };
1566 t_proc.create = cjni_match_target_create;
1567 t_proc.destroy = cjni_match_target_destroy;
1568 t_proc.invoke = cjni_match_target_invoke;
1570 status = fc_register_target (c_name, t_proc);
1574 ERROR ("java plugin: cjni_api_register_match_target: "
1575 "Don't know whether to create a match or a target.");
1576 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1582 ERROR ("java plugin: cjni_api_register_match_target: "
1584 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1585 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1589 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1592 } /* }}} jint cjni_api_register_match_target */
1594 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1595 jobject this, jobject o_name, jobject o_match)
1597 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1599 } /* }}} jint cjni_api_register_match */
1601 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1602 jobject this, jobject o_name, jobject o_target)
1604 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1606 } /* }}} jint cjni_api_register_target */
1608 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1609 jobject this, jint severity, jobject o_message)
1613 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1616 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1620 if (severity < LOG_ERR)
1622 if (severity > LOG_DEBUG)
1623 severity = LOG_DEBUG;
1625 plugin_log (severity, "%s", c_str);
1627 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1628 } /* }}} void cjni_api_log */
1630 static jstring JNICALL cjni_api_get_hostname (JNIEnv *jvm_env, jobject this)
1632 return ctoj_output_string(jvm_env, hostname_g);
1635 /* List of ``native'' functions, i. e. C-functions that can be called from
1637 static JNINativeMethod jni_api_functions[] = /* {{{ */
1640 "(Lorg/collectd/api/ValueList;)I",
1641 cjni_api_dispatch_values },
1643 { "dispatchNotification",
1644 "(Lorg/collectd/api/Notification;)I",
1645 cjni_api_dispatch_notification },
1648 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1652 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1653 cjni_api_register_config },
1656 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1657 cjni_api_register_init },
1660 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1661 cjni_api_register_read },
1664 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1665 cjni_api_register_write },
1668 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1669 cjni_api_register_flush },
1671 { "registerShutdown",
1672 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1673 cjni_api_register_shutdown },
1676 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1677 cjni_api_register_log },
1679 { "registerNotification",
1680 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1681 cjni_api_register_notification },
1684 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1685 cjni_api_register_match },
1688 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1689 cjni_api_register_target },
1692 "(ILjava/lang/String;)V",
1696 "()Ljava/lang/String;",
1697 cjni_api_get_hostname },
1700 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1701 / sizeof (jni_api_functions[0]);
1707 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1708 * all registration functions. */
1709 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1710 jobject o_name, jobject o_callback, int type)
1713 cjni_callback_info_t *cbi;
1714 const char *method_name;
1715 const char *method_signature;
1719 case CB_TYPE_CONFIG:
1720 method_name = "config";
1721 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1725 method_name = "init";
1726 method_signature = "()I";
1730 method_name = "read";
1731 method_signature = "()I";
1735 method_name = "write";
1736 method_signature = "(Lorg/collectd/api/ValueList;)I";
1740 method_name = "flush";
1741 method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I";
1744 case CB_TYPE_SHUTDOWN:
1745 method_name = "shutdown";
1746 method_signature = "()I";
1750 method_name = "log";
1751 method_signature = "(ILjava/lang/String;)V";
1754 case CB_TYPE_NOTIFICATION:
1755 method_name = "notification";
1756 method_signature = "(Lorg/collectd/api/Notification;)I";
1760 method_name = "createMatch";
1761 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1762 "Lorg/collectd/api/CollectdMatchInterface;";
1765 case CB_TYPE_TARGET:
1766 method_name = "createTarget";
1767 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1768 "Lorg/collectd/api/CollectdTargetInterface;";
1772 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1777 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1780 ERROR ("java plugin: cjni_callback_info_create: "
1781 "GetStringUTFChars failed.");
1785 cbi = calloc (1, sizeof (*cbi));
1788 ERROR ("java plugin: cjni_callback_info_create: calloc failed.");
1789 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1794 cbi->name = strdup (c_name);
1795 if (cbi->name == NULL)
1797 pthread_mutex_unlock (&java_callbacks_lock);
1798 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1799 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1804 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1806 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1807 if (cbi->object == NULL)
1809 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1815 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1816 if (cbi->class == NULL)
1818 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1819 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1825 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1826 method_name, method_signature);
1827 if (cbi->method == NULL)
1829 ERROR ("java plugin: cjni_callback_info_create: "
1830 "Cannot find the `%s' method with signature `%s'.",
1831 method_name, method_signature);
1832 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1839 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1841 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1842 * to the global `java_callbacks' variable. This is used for `config', `init',
1843 * and `shutdown' callbacks. */
1844 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1845 jobject o_name, jobject o_callback, int type)
1847 cjni_callback_info_t *cbi;
1848 cjni_callback_info_t *tmp;
1850 const char *type_str;
1853 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1860 case CB_TYPE_CONFIG:
1861 type_str = "config";
1868 case CB_TYPE_SHUTDOWN:
1869 type_str = "shutdown";
1876 case CB_TYPE_TARGET:
1877 type_str = "target";
1881 type_str = "<unknown>";
1883 DEBUG ("java plugin: Registering new %s callback: %s",
1884 type_str, cbi->name);
1887 pthread_mutex_lock (&java_callbacks_lock);
1889 tmp = realloc (java_callbacks,
1890 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1893 pthread_mutex_unlock (&java_callbacks_lock);
1894 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1896 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1901 java_callbacks = tmp;
1902 java_callbacks[java_callbacks_num] = *cbi;
1903 java_callbacks_num++;
1905 pthread_mutex_unlock (&java_callbacks_lock);
1909 } /* }}} int cjni_callback_register */
1911 /* Callback for `pthread_key_create'. It frees the data contained in
1912 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1913 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1915 cjni_jvm_env_t *cjni_env;
1920 cjni_env = (cjni_jvm_env_t *) args;
1922 if (cjni_env->reference_counter > 0)
1924 ERROR ("java plugin: cjni_jvm_env_destroy: "
1925 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1928 if (cjni_env->jvm_env != NULL)
1930 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1931 (void *) cjni_env->jvm_env);
1934 /* The pointer is allocated in `cjni_thread_attach' */
1936 } /* }}} void cjni_jvm_env_destroy */
1938 /* Register ``native'' functions with the JVM. Native functions are C-functions
1939 * that can be called by Java code. */
1940 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1942 jclass api_class_ptr;
1945 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1946 if (api_class_ptr == NULL)
1948 ERROR ("cjni_init_native: Cannot find the API class \"org.collectd.api"
1949 ".Collectd\". Please set the correct class path "
1950 "using 'JVMArg \"-Djava.class.path=...\"'.");
1954 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1955 jni_api_functions, (jint) jni_api_functions_num);
1958 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1963 } /* }}} int cjni_init_native */
1965 /* Create the JVM. This is called when the first thread tries to access the JVM
1966 * via cjni_thread_attach. */
1967 static int cjni_create_jvm (void) /* {{{ */
1970 JavaVMInitArgs vm_args = { 0 };
1971 JavaVMOption vm_options[jvm_argc];
1978 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1981 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1982 "with status %i.", status);
1988 vm_args.version = JNI_VERSION_1_2;
1989 vm_args.options = vm_options;
1990 vm_args.nOptions = (jint) jvm_argc;
1992 for (size_t i = 0; i < jvm_argc; i++)
1994 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
1996 vm_args.options[i].optionString = jvm_argv[i];
1999 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
2002 ERROR ("java plugin: cjni_create_jvm: "
2003 "JNI_CreateJavaVM failed with status %i.",
2007 assert (jvm != NULL);
2008 assert (jvm_env != NULL);
2010 /* Call RegisterNatives */
2011 status = cjni_init_native (jvm_env);
2014 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
2018 DEBUG ("java plugin: The JVM has been created.");
2020 } /* }}} int cjni_create_jvm */
2022 /* Increase the reference counter to the JVM for this thread. If it was zero,
2023 * attach the JVM first. */
2024 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2026 cjni_jvm_env_t *cjni_env;
2029 /* If we're the first thread to access the JVM, we'll have to create it
2035 status = cjni_create_jvm ();
2038 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2042 assert (jvm != NULL);
2044 cjni_env = pthread_getspecific (jvm_env_key);
2045 if (cjni_env == NULL)
2047 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2048 cjni_env = calloc (1, sizeof (*cjni_env));
2049 if (cjni_env == NULL)
2051 ERROR ("java plugin: cjni_thread_attach: calloc failed.");
2054 cjni_env->reference_counter = 0;
2055 cjni_env->jvm_env = NULL;
2057 pthread_setspecific (jvm_env_key, cjni_env);
2060 if (cjni_env->reference_counter > 0)
2062 cjni_env->reference_counter++;
2063 jvm_env = cjni_env->jvm_env;
2068 JavaVMAttachArgs args = { 0 };
2070 assert (cjni_env->jvm_env == NULL);
2072 args.version = JNI_VERSION_1_2;
2074 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2077 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2078 "with status %i.", status);
2082 cjni_env->reference_counter = 1;
2083 cjni_env->jvm_env = jvm_env;
2086 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2087 cjni_env->reference_counter);
2088 assert (jvm_env != NULL);
2090 } /* }}} JNIEnv *cjni_thread_attach */
2092 /* Decrease the reference counter of this thread. If it reaches zero, detach
2094 static int cjni_thread_detach (void) /* {{{ */
2096 cjni_jvm_env_t *cjni_env;
2099 cjni_env = pthread_getspecific (jvm_env_key);
2100 if (cjni_env == NULL)
2102 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2106 assert (cjni_env->reference_counter > 0);
2107 assert (cjni_env->jvm_env != NULL);
2109 cjni_env->reference_counter--;
2110 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2111 cjni_env->reference_counter);
2113 if (cjni_env->reference_counter > 0)
2116 status = (*jvm)->DetachCurrentThread (jvm);
2119 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2120 "with status %i.", status);
2123 cjni_env->reference_counter = 0;
2124 cjni_env->jvm_env = NULL;
2127 } /* }}} int cjni_thread_detach */
2129 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2133 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2135 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2141 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2142 "`LoadPlugin' options! The JVM is already started and I have to "
2143 "ignore this argument: %s",
2144 ci->values[0].value.string);
2148 tmp = realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2151 ERROR ("java plugin: realloc failed.");
2156 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2157 if (jvm_argv[jvm_argc] == NULL)
2159 ERROR ("java plugin: strdup failed.");
2165 } /* }}} int cjni_config_add_jvm_arg */
2167 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2170 java_plugin_class_t *class;
2171 jmethodID constructor_id;
2174 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2176 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2180 jvm_env = cjni_thread_attach ();
2181 if (jvm_env == NULL)
2184 class = realloc (java_classes_list,
2185 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2188 ERROR ("java plugin: realloc failed.");
2189 cjni_thread_detach ();
2192 java_classes_list = class;
2193 class = java_classes_list + java_classes_list_len;
2195 memset (class, 0, sizeof (*class));
2196 class->name = strdup (ci->values[0].value.string);
2197 if (class->name == NULL)
2199 ERROR ("java plugin: strdup failed.");
2200 cjni_thread_detach ();
2203 class->class = NULL;
2204 class->object = NULL;
2206 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2207 thorough the Java community, but (Sun's) `FindClass' and friends need
2209 for (size_t i = 0; class->name[i] != 0; i++)
2210 if (class->name[i] == '.')
2211 class->name[i] = '/';
2214 DEBUG ("java plugin: Loading class %s", class->name);
2216 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2217 if (class->class == NULL)
2219 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2221 cjni_thread_detach ();
2226 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2228 if (constructor_id == NULL)
2230 ERROR ("java plugin: cjni_config_load_plugin: "
2231 "Could not find the constructor for `%s'.",
2233 cjni_thread_detach ();
2238 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2240 if (tmp_object != NULL)
2241 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2243 class->object = NULL;
2244 if (class->object == NULL)
2246 ERROR ("java plugin: cjni_config_load_plugin: "
2247 "Could create a new `%s' object.",
2249 cjni_thread_detach ();
2254 cjni_thread_detach ();
2256 java_classes_list_len++;
2259 } /* }}} int cjni_config_load_plugin */
2261 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2264 cjni_callback_info_t *cbi;
2271 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2273 WARNING ("java plugin: `Plugin' blocks "
2274 "need exactly one string argument.");
2278 name = ci->values[0].value.string;
2281 for (size_t i = 0; i < java_callbacks_num; i++)
2283 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2286 if (strcmp (name, java_callbacks[i].name) != 0)
2289 cbi = java_callbacks + i;
2295 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2296 "configuration callback has been registered. Please make sure, the "
2297 "`LoadPlugin' lines precede the `Plugin' blocks.",
2302 DEBUG ("java plugin: Configuring %s", name);
2304 jvm_env = cjni_thread_attach ();
2305 if (jvm_env == NULL)
2308 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2309 if (o_ocitem == NULL)
2311 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2312 cjni_thread_detach ();
2316 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2317 method = (*jvm_env)->GetMethodID (jvm_env, class,
2318 "config", "(Lorg/collectd/api/OConfigItem;)I");
2320 (*jvm_env)->CallIntMethod (jvm_env,
2321 cbi->object, method, o_ocitem);
2323 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2324 cjni_thread_detach ();
2326 } /* }}} int cjni_config_plugin_block */
2328 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2337 for (int i = 0; i < ci->children_num; i++)
2339 oconfig_item_t *child = ci->children + i;
2341 if (strcasecmp ("JVMArg", child->key) == 0)
2343 status = cjni_config_add_jvm_arg (child);
2349 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2351 status = cjni_config_load_plugin (child);
2357 else if (strcasecmp ("Plugin", child->key) == 0)
2359 status = cjni_config_plugin_block (child);
2367 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2372 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2373 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2375 if ((success == 0) && (errors > 0))
2377 ERROR ("java plugin: All statements failed.");
2382 } /* }}} int cjni_config_perform */
2384 /* Copy the children of `ci' to the global `config_block' variable. */
2385 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2387 oconfig_item_t *ci_copy;
2388 oconfig_item_t *tmp;
2390 assert (ci != NULL);
2391 if (ci->children_num == 0)
2392 return (0); /* nothing to do */
2394 ci_copy = oconfig_clone (ci);
2395 if (ci_copy == NULL)
2397 ERROR ("java plugin: oconfig_clone failed.");
2401 if (config_block == NULL)
2403 config_block = ci_copy;
2407 tmp = realloc (config_block->children,
2408 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2411 ERROR ("java plugin: realloc failed.");
2412 oconfig_free (ci_copy);
2415 config_block->children = tmp;
2417 /* Copy the pointers */
2418 memcpy (config_block->children + config_block->children_num,
2420 ci_copy->children_num * sizeof (*ci_copy->children));
2421 config_block->children_num += ci_copy->children_num;
2423 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2424 memset (ci_copy->children, 0,
2425 ci_copy->children_num * sizeof (*ci_copy->children));
2426 ci_copy->children_num = 0;
2428 oconfig_free (ci_copy);
2431 } /* }}} int cjni_config_callback */
2433 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2434 * and `cjni_write'. In particular, delete the global reference to the Java
2436 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2439 cjni_callback_info_t *cbi;
2441 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2443 cbi = (cjni_callback_info_t *) arg;
2445 /* This condition can occur when shutting down. */
2455 jvm_env = cjni_thread_attach ();
2456 if (jvm_env == NULL)
2458 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2462 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2469 cjni_thread_detach ();
2470 } /* }}} void cjni_callback_info_destroy */
2472 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2473 static int cjni_read (user_data_t *ud) /* {{{ */
2476 cjni_callback_info_t *cbi;
2481 ERROR ("java plugin: cjni_read: jvm == NULL");
2485 if ((ud == NULL) || (ud->data == NULL))
2487 ERROR ("java plugin: cjni_read: Invalid user data.");
2491 jvm_env = cjni_thread_attach ();
2492 if (jvm_env == NULL)
2495 cbi = (cjni_callback_info_t *) ud->data;
2497 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2500 cjni_thread_detach ();
2501 return (ret_status);
2502 } /* }}} int cjni_read */
2504 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2505 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2509 cjni_callback_info_t *cbi;
2515 ERROR ("java plugin: cjni_write: jvm == NULL");
2519 if ((ud == NULL) || (ud->data == NULL))
2521 ERROR ("java plugin: cjni_write: Invalid user data.");
2525 jvm_env = cjni_thread_attach ();
2526 if (jvm_env == NULL)
2529 cbi = (cjni_callback_info_t *) ud->data;
2531 vl_java = ctoj_value_list (jvm_env, ds, vl);
2532 if (vl_java == NULL)
2534 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2535 cjni_thread_detach ();
2539 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2540 cbi->object, cbi->method, vl_java);
2542 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2544 cjni_thread_detach ();
2545 return (ret_status);
2546 } /* }}} int cjni_write */
2548 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2549 static int cjni_flush (cdtime_t timeout, const char *identifier, /* {{{ */
2553 cjni_callback_info_t *cbi;
2555 jobject o_identifier;
2560 ERROR ("java plugin: cjni_flush: jvm == NULL");
2564 if ((ud == NULL) || (ud->data == NULL))
2566 ERROR ("java plugin: cjni_flush: Invalid user data.");
2570 jvm_env = cjni_thread_attach ();
2571 if (jvm_env == NULL)
2574 cbi = (cjni_callback_info_t *) ud->data;
2576 o_timeout = ctoj_jdouble_to_number (jvm_env,
2577 (jdouble) CDTIME_T_TO_DOUBLE (timeout));
2578 if (o_timeout == NULL)
2580 ERROR ("java plugin: cjni_flush: Converting double "
2581 "to Number object failed.");
2582 cjni_thread_detach ();
2586 o_identifier = NULL;
2587 if (identifier != NULL)
2589 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2590 if (o_identifier == NULL)
2592 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2593 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2594 cjni_thread_detach ();
2599 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2600 cbi->object, cbi->method, o_timeout, o_identifier);
2602 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2603 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2605 cjni_thread_detach ();
2606 return (ret_status);
2607 } /* }}} int cjni_flush */
2609 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2610 static void cjni_log (int severity, const char *message, /* {{{ */
2614 cjni_callback_info_t *cbi;
2620 if ((ud == NULL) || (ud->data == NULL))
2623 jvm_env = cjni_thread_attach ();
2624 if (jvm_env == NULL)
2627 cbi = (cjni_callback_info_t *) ud->data;
2629 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2630 if (o_message == NULL)
2632 cjni_thread_detach ();
2636 (*jvm_env)->CallVoidMethod (jvm_env,
2637 cbi->object, cbi->method, (jint) severity, o_message);
2639 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2641 cjni_thread_detach ();
2642 } /* }}} void cjni_log */
2644 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2646 static int cjni_notification (const notification_t *n, /* {{{ */
2650 cjni_callback_info_t *cbi;
2651 jobject o_notification;
2656 ERROR ("java plugin: cjni_read: jvm == NULL");
2660 if ((ud == NULL) || (ud->data == NULL))
2662 ERROR ("java plugin: cjni_read: Invalid user data.");
2666 jvm_env = cjni_thread_attach ();
2667 if (jvm_env == NULL)
2670 cbi = (cjni_callback_info_t *) ud->data;
2672 o_notification = ctoj_notification (jvm_env, n);
2673 if (o_notification == NULL)
2675 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2676 cjni_thread_detach ();
2680 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2681 cbi->object, cbi->method, o_notification);
2683 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2685 cjni_thread_detach ();
2686 return (ret_status);
2687 } /* }}} int cjni_notification */
2689 /* Callbacks for matches implemented in Java */
2690 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2694 cjni_callback_info_t *cbi_ret;
2695 cjni_callback_info_t *cbi_factory;
2705 #define BAIL_OUT(status) \
2706 if (cbi_ret != NULL) { \
2707 free (cbi_ret->name); \
2708 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2709 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2713 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2714 cjni_thread_detach (); \
2719 ERROR ("java plugin: cjni_read: jvm == NULL");
2723 jvm_env = cjni_thread_attach ();
2724 if (jvm_env == NULL)
2727 /* Find out whether to create a match or a target. */
2728 if (strcasecmp ("Match", ci->key) == 0)
2729 type = CB_TYPE_MATCH;
2730 else if (strcasecmp ("Target", ci->key) == 0)
2731 type = CB_TYPE_TARGET;
2734 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2735 "to create a match or a target.");
2739 /* This is the name of the match we should create. */
2740 name = ci->values[0].value.string;
2742 /* Lets see if we have a matching factory here.. */
2744 for (size_t i = 0; i < java_callbacks_num; i++)
2746 if (java_callbacks[i].type != type)
2749 if (strcmp (name, java_callbacks[i].name) != 0)
2752 cbi_factory = java_callbacks + i;
2756 /* Nope, no factory for that name.. */
2757 if (cbi_factory == NULL)
2759 ERROR ("java plugin: cjni_match_target_create: "
2760 "No such match factory registered: %s",
2765 /* We convert `ci' to its Java equivalent.. */
2766 o_ci = ctoj_oconfig_item (jvm_env, ci);
2769 ERROR ("java plugin: cjni_match_target_create: "
2770 "ctoj_oconfig_item failed.");
2774 /* Allocate a new callback info structure. This is going to be our user_data
2776 cbi_ret = calloc (1, sizeof (*cbi_ret));
2777 if (cbi_ret == NULL)
2779 ERROR ("java plugin: cjni_match_target_create: calloc failed.");
2783 cbi_ret->object = NULL;
2784 cbi_ret->type = type;
2786 /* Lets fill the callback info structure.. First, the name: */
2787 cbi_ret->name = strdup (name);
2788 if (cbi_ret->name == NULL)
2790 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2794 /* Then call the factory method so it creates a new object for us. */
2795 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2796 cbi_factory->object, cbi_factory->method, o_ci);
2799 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2803 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2806 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2810 /* This is the class of the match. It is possibly different from the class of
2811 * the match-factory! */
2812 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2813 if (cbi_ret->class == NULL)
2815 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2819 /* Lookup the `int match (DataSet, ValueList)' method. */
2820 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2821 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2822 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2823 if (cbi_ret->method == NULL)
2825 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2829 /* Return the newly created match via the user_data pointer. */
2830 *user_data = (void *) cbi_ret;
2832 cjni_thread_detach ();
2834 DEBUG ("java plugin: cjni_match_target_create: "
2835 "Successfully created a `%s' %s.",
2836 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2841 } /* }}} int cjni_match_target_create */
2843 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2845 cjni_callback_info_destroy (*user_data);
2849 } /* }}} int cjni_match_target_destroy */
2851 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2852 value_list_t *vl, notification_meta_t **meta, void **user_data)
2855 cjni_callback_info_t *cbi;
2863 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2867 jvm_env = cjni_thread_attach ();
2868 if (jvm_env == NULL)
2871 cbi = (cjni_callback_info_t *) *user_data;
2873 o_vl = ctoj_value_list (jvm_env, ds, vl);
2876 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2877 cjni_thread_detach ();
2881 o_ds = ctoj_data_set (jvm_env, ds);
2884 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2885 cjni_thread_detach ();
2889 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2892 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2894 /* If we're executing a target, copy the `ValueList' back to our
2895 * `value_list_t'. */
2896 if (cbi->type == CB_TYPE_TARGET)
2898 value_list_t new_vl = { 0 };
2900 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2903 ERROR ("java plugin: cjni_match_target_invoke: "
2904 "jtoc_value_list failed.");
2906 else /* if (status == 0) */
2908 /* plugin_dispatch_values assures that this is dynamically allocated
2912 /* This will replace the vl->values pointer to a new, dynamically
2913 * allocated piece of memory. */
2914 memcpy (vl, &new_vl, sizeof (*vl));
2916 } /* if (cbi->type == CB_TYPE_TARGET) */
2918 cjni_thread_detach ();
2919 return (ret_status);
2920 } /* }}} int cjni_match_target_invoke */
2922 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2923 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2927 for (size_t i = 0; i < java_callbacks_num; i++)
2929 if (java_callbacks[i].type != CB_TYPE_INIT)
2932 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2934 status = (*jvm_env)->CallIntMethod (jvm_env,
2935 java_callbacks[i].object, java_callbacks[i].method);
2938 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2939 "Removing read function.",
2940 java_callbacks[i].name, status);
2941 plugin_unregister_read (java_callbacks[i].name);
2946 } /* }}} int cjni_init_plugins */
2948 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2949 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2953 for (size_t i = 0; i < java_callbacks_num; i++)
2955 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2958 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2960 status = (*jvm_env)->CallIntMethod (jvm_env,
2961 java_callbacks[i].object, java_callbacks[i].method);
2964 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2965 java_callbacks[i].name, status);
2970 } /* }}} int cjni_shutdown_plugins */
2973 static int cjni_shutdown (void) /* {{{ */
2976 JavaVMAttachArgs args = { 0 };
2983 args.version = JNI_VERSION_1_2;
2985 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
2988 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2993 /* Execute all the shutdown functions registered by plugins. */
2994 cjni_shutdown_plugins (jvm_env);
2996 /* Release all the global references to callback functions */
2997 for (size_t i = 0; i < java_callbacks_num; i++)
2999 if (java_callbacks[i].object != NULL)
3001 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3002 java_callbacks[i].object = NULL;
3004 sfree (java_callbacks[i].name);
3006 java_callbacks_num = 0;
3007 sfree (java_callbacks);
3009 /* Release all the global references to directly loaded classes. */
3010 for (size_t i = 0; i < java_classes_list_len; i++)
3012 if (java_classes_list[i].object != NULL)
3014 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3015 java_classes_list[i].object = NULL;
3017 sfree (java_classes_list[i].name);
3019 java_classes_list_len = 0;
3020 sfree (java_classes_list);
3022 /* Destroy the JVM */
3023 DEBUG ("java plugin: Destroying the JVM.");
3024 (*jvm)->DestroyJavaVM (jvm);
3028 pthread_key_delete (jvm_env_key);
3030 /* Free the JVM argument list */
3031 for (size_t i = 0; i < jvm_argc; i++)
3032 sfree (jvm_argv[i]);
3037 } /* }}} int cjni_shutdown */
3039 /* Initialization: Create a JVM, load all configured classes and call their
3040 * `config' and `init' callback methods. */
3041 static int cjni_init (void) /* {{{ */
3045 if ((config_block == NULL) && (jvm == NULL))
3047 ERROR ("java plugin: cjni_init: No configuration block for "
3048 "the java plugin was found.");
3052 if (config_block != NULL)
3054 cjni_config_perform (config_block);
3055 oconfig_free (config_block);
3060 ERROR ("java plugin: cjni_init: jvm == NULL");
3064 jvm_env = cjni_thread_attach ();
3065 if (jvm_env == NULL)
3068 cjni_init_plugins (jvm_env);
3070 cjni_thread_detach ();
3072 } /* }}} int cjni_init */
3074 void module_register (void)
3076 plugin_register_complex_config ("java", cjni_config_callback);
3077 plugin_register_init ("java", cjni_init);
3078 plugin_register_shutdown ("java", cjni_shutdown);
3079 } /* void module_register (void) */
3081 /* vim: set sw=2 sts=2 et fdm=marker : */