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 user_data_t ud = { 0 };
1421 cjni_callback_info_t *cbi;
1423 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1427 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1429 ud.data = (void *) cbi;
1430 ud.free_func = cjni_callback_info_destroy;
1432 plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read,
1433 /* interval = */ 0, &ud);
1435 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1438 } /* }}} jint cjni_api_register_read */
1440 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1441 jobject this, jobject o_name, jobject o_write)
1443 user_data_t ud = { 0 };
1444 cjni_callback_info_t *cbi;
1446 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1450 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1452 ud.data = (void *) cbi;
1453 ud.free_func = cjni_callback_info_destroy;
1455 plugin_register_write (cbi->name, cjni_write, &ud);
1457 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1460 } /* }}} jint cjni_api_register_write */
1462 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1463 jobject this, jobject o_name, jobject o_flush)
1465 user_data_t ud = { 0 };
1466 cjni_callback_info_t *cbi;
1468 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1472 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1474 ud.data = (void *) cbi;
1475 ud.free_func = cjni_callback_info_destroy;
1477 plugin_register_flush (cbi->name, cjni_flush, &ud);
1479 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1482 } /* }}} jint cjni_api_register_flush */
1484 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1485 jobject this, jobject o_name, jobject o_shutdown)
1487 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1489 } /* }}} jint cjni_api_register_shutdown */
1491 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1492 jobject this, jobject o_name, jobject o_log)
1494 user_data_t ud = { 0 };
1495 cjni_callback_info_t *cbi;
1497 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1501 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1503 ud.data = (void *) cbi;
1504 ud.free_func = cjni_callback_info_destroy;
1506 plugin_register_log (cbi->name, cjni_log, &ud);
1508 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1511 } /* }}} jint cjni_api_register_log */
1513 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1514 jobject this, jobject o_name, jobject o_notification)
1516 user_data_t ud = { 0 };
1517 cjni_callback_info_t *cbi;
1519 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1520 CB_TYPE_NOTIFICATION);
1524 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1526 ud.data = (void *) cbi;
1527 ud.free_func = cjni_callback_info_destroy;
1529 plugin_register_notification (cbi->name, cjni_notification, &ud);
1531 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1534 } /* }}} jint cjni_api_register_notification */
1536 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1537 jobject this, jobject o_name, jobject o_match, int type)
1542 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1545 ERROR ("java plugin: cjni_api_register_match_target: "
1546 "GetStringUTFChars failed.");
1550 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1553 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1557 if (type == CB_TYPE_MATCH)
1559 match_proc_t m_proc = { 0 };
1561 m_proc.create = cjni_match_target_create;
1562 m_proc.destroy = cjni_match_target_destroy;
1563 m_proc.match = (void *) cjni_match_target_invoke;
1565 status = fc_register_match (c_name, m_proc);
1567 else if (type == CB_TYPE_TARGET)
1569 target_proc_t t_proc = { 0 };
1571 t_proc.create = cjni_match_target_create;
1572 t_proc.destroy = cjni_match_target_destroy;
1573 t_proc.invoke = cjni_match_target_invoke;
1575 status = fc_register_target (c_name, t_proc);
1579 ERROR ("java plugin: cjni_api_register_match_target: "
1580 "Don't know whether to create a match or a target.");
1581 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1587 ERROR ("java plugin: cjni_api_register_match_target: "
1589 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1590 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1594 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1597 } /* }}} jint cjni_api_register_match_target */
1599 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1600 jobject this, jobject o_name, jobject o_match)
1602 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1604 } /* }}} jint cjni_api_register_match */
1606 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1607 jobject this, jobject o_name, jobject o_target)
1609 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1611 } /* }}} jint cjni_api_register_target */
1613 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1614 jobject this, jint severity, jobject o_message)
1618 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1621 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1625 if (severity < LOG_ERR)
1627 if (severity > LOG_DEBUG)
1628 severity = LOG_DEBUG;
1630 plugin_log (severity, "%s", c_str);
1632 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1633 } /* }}} void cjni_api_log */
1635 static jstring JNICALL cjni_api_get_hostname (JNIEnv *jvm_env, jobject this)
1637 return ctoj_output_string(jvm_env, hostname_g);
1640 /* List of ``native'' functions, i. e. C-functions that can be called from
1642 static JNINativeMethod jni_api_functions[] = /* {{{ */
1645 "(Lorg/collectd/api/ValueList;)I",
1646 cjni_api_dispatch_values },
1648 { "dispatchNotification",
1649 "(Lorg/collectd/api/Notification;)I",
1650 cjni_api_dispatch_notification },
1653 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1657 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1658 cjni_api_register_config },
1661 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1662 cjni_api_register_init },
1665 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1666 cjni_api_register_read },
1669 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1670 cjni_api_register_write },
1673 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1674 cjni_api_register_flush },
1676 { "registerShutdown",
1677 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1678 cjni_api_register_shutdown },
1681 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1682 cjni_api_register_log },
1684 { "registerNotification",
1685 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1686 cjni_api_register_notification },
1689 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1690 cjni_api_register_match },
1693 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1694 cjni_api_register_target },
1697 "(ILjava/lang/String;)V",
1701 "()Ljava/lang/String;",
1702 cjni_api_get_hostname },
1705 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1706 / sizeof (jni_api_functions[0]);
1712 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1713 * all registration functions. */
1714 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1715 jobject o_name, jobject o_callback, int type)
1718 cjni_callback_info_t *cbi;
1719 const char *method_name;
1720 const char *method_signature;
1724 case CB_TYPE_CONFIG:
1725 method_name = "config";
1726 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1730 method_name = "init";
1731 method_signature = "()I";
1735 method_name = "read";
1736 method_signature = "()I";
1740 method_name = "write";
1741 method_signature = "(Lorg/collectd/api/ValueList;)I";
1745 method_name = "flush";
1746 method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I";
1749 case CB_TYPE_SHUTDOWN:
1750 method_name = "shutdown";
1751 method_signature = "()I";
1755 method_name = "log";
1756 method_signature = "(ILjava/lang/String;)V";
1759 case CB_TYPE_NOTIFICATION:
1760 method_name = "notification";
1761 method_signature = "(Lorg/collectd/api/Notification;)I";
1765 method_name = "createMatch";
1766 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1767 "Lorg/collectd/api/CollectdMatchInterface;";
1770 case CB_TYPE_TARGET:
1771 method_name = "createTarget";
1772 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1773 "Lorg/collectd/api/CollectdTargetInterface;";
1777 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1782 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1785 ERROR ("java plugin: cjni_callback_info_create: "
1786 "GetStringUTFChars failed.");
1790 cbi = calloc (1, sizeof (*cbi));
1793 ERROR ("java plugin: cjni_callback_info_create: calloc failed.");
1794 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1799 cbi->name = strdup (c_name);
1800 if (cbi->name == NULL)
1802 pthread_mutex_unlock (&java_callbacks_lock);
1803 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1804 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1809 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1811 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1812 if (cbi->object == NULL)
1814 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1820 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1821 if (cbi->class == NULL)
1823 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1824 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1830 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1831 method_name, method_signature);
1832 if (cbi->method == NULL)
1834 ERROR ("java plugin: cjni_callback_info_create: "
1835 "Cannot find the `%s' method with signature `%s'.",
1836 method_name, method_signature);
1837 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1844 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1846 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1847 * to the global `java_callbacks' variable. This is used for `config', `init',
1848 * and `shutdown' callbacks. */
1849 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1850 jobject o_name, jobject o_callback, int type)
1852 cjni_callback_info_t *cbi;
1853 cjni_callback_info_t *tmp;
1855 const char *type_str;
1858 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1865 case CB_TYPE_CONFIG:
1866 type_str = "config";
1873 case CB_TYPE_SHUTDOWN:
1874 type_str = "shutdown";
1881 case CB_TYPE_TARGET:
1882 type_str = "target";
1886 type_str = "<unknown>";
1888 DEBUG ("java plugin: Registering new %s callback: %s",
1889 type_str, cbi->name);
1892 pthread_mutex_lock (&java_callbacks_lock);
1894 tmp = realloc (java_callbacks,
1895 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1898 pthread_mutex_unlock (&java_callbacks_lock);
1899 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1901 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1906 java_callbacks = tmp;
1907 java_callbacks[java_callbacks_num] = *cbi;
1908 java_callbacks_num++;
1910 pthread_mutex_unlock (&java_callbacks_lock);
1914 } /* }}} int cjni_callback_register */
1916 /* Callback for `pthread_key_create'. It frees the data contained in
1917 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1918 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1920 cjni_jvm_env_t *cjni_env;
1925 cjni_env = (cjni_jvm_env_t *) args;
1927 if (cjni_env->reference_counter > 0)
1929 ERROR ("java plugin: cjni_jvm_env_destroy: "
1930 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1933 if (cjni_env->jvm_env != NULL)
1935 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1936 (void *) cjni_env->jvm_env);
1939 /* The pointer is allocated in `cjni_thread_attach' */
1941 } /* }}} void cjni_jvm_env_destroy */
1943 /* Register ``native'' functions with the JVM. Native functions are C-functions
1944 * that can be called by Java code. */
1945 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1947 jclass api_class_ptr;
1950 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1951 if (api_class_ptr == NULL)
1953 ERROR ("cjni_init_native: Cannot find the API class \"org.collectd.api"
1954 ".Collectd\". Please set the correct class path "
1955 "using 'JVMArg \"-Djava.class.path=...\"'.");
1959 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1960 jni_api_functions, (jint) jni_api_functions_num);
1963 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1968 } /* }}} int cjni_init_native */
1970 /* Create the JVM. This is called when the first thread tries to access the JVM
1971 * via cjni_thread_attach. */
1972 static int cjni_create_jvm (void) /* {{{ */
1975 JavaVMInitArgs vm_args = { 0 };
1976 JavaVMOption vm_options[jvm_argc];
1983 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1986 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1987 "with status %i.", status);
1993 vm_args.version = JNI_VERSION_1_2;
1994 vm_args.options = vm_options;
1995 vm_args.nOptions = (jint) jvm_argc;
1997 for (size_t i = 0; i < jvm_argc; i++)
1999 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
2001 vm_args.options[i].optionString = jvm_argv[i];
2004 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
2007 ERROR ("java plugin: cjni_create_jvm: "
2008 "JNI_CreateJavaVM failed with status %i.",
2012 assert (jvm != NULL);
2013 assert (jvm_env != NULL);
2015 /* Call RegisterNatives */
2016 status = cjni_init_native (jvm_env);
2019 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
2023 DEBUG ("java plugin: The JVM has been created.");
2025 } /* }}} int cjni_create_jvm */
2027 /* Increase the reference counter to the JVM for this thread. If it was zero,
2028 * attach the JVM first. */
2029 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2031 cjni_jvm_env_t *cjni_env;
2034 /* If we're the first thread to access the JVM, we'll have to create it
2040 status = cjni_create_jvm ();
2043 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2047 assert (jvm != NULL);
2049 cjni_env = pthread_getspecific (jvm_env_key);
2050 if (cjni_env == NULL)
2052 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2053 cjni_env = calloc (1, sizeof (*cjni_env));
2054 if (cjni_env == NULL)
2056 ERROR ("java plugin: cjni_thread_attach: calloc failed.");
2059 cjni_env->reference_counter = 0;
2060 cjni_env->jvm_env = NULL;
2062 pthread_setspecific (jvm_env_key, cjni_env);
2065 if (cjni_env->reference_counter > 0)
2067 cjni_env->reference_counter++;
2068 jvm_env = cjni_env->jvm_env;
2073 JavaVMAttachArgs args = { 0 };
2075 assert (cjni_env->jvm_env == NULL);
2077 args.version = JNI_VERSION_1_2;
2079 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2082 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2083 "with status %i.", status);
2087 cjni_env->reference_counter = 1;
2088 cjni_env->jvm_env = jvm_env;
2091 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2092 cjni_env->reference_counter);
2093 assert (jvm_env != NULL);
2095 } /* }}} JNIEnv *cjni_thread_attach */
2097 /* Decrease the reference counter of this thread. If it reaches zero, detach
2099 static int cjni_thread_detach (void) /* {{{ */
2101 cjni_jvm_env_t *cjni_env;
2104 cjni_env = pthread_getspecific (jvm_env_key);
2105 if (cjni_env == NULL)
2107 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2111 assert (cjni_env->reference_counter > 0);
2112 assert (cjni_env->jvm_env != NULL);
2114 cjni_env->reference_counter--;
2115 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2116 cjni_env->reference_counter);
2118 if (cjni_env->reference_counter > 0)
2121 status = (*jvm)->DetachCurrentThread (jvm);
2124 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2125 "with status %i.", status);
2128 cjni_env->reference_counter = 0;
2129 cjni_env->jvm_env = NULL;
2132 } /* }}} int cjni_thread_detach */
2134 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2138 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2140 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2146 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2147 "`LoadPlugin' options! The JVM is already started and I have to "
2148 "ignore this argument: %s",
2149 ci->values[0].value.string);
2153 tmp = realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2156 ERROR ("java plugin: realloc failed.");
2161 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2162 if (jvm_argv[jvm_argc] == NULL)
2164 ERROR ("java plugin: strdup failed.");
2170 } /* }}} int cjni_config_add_jvm_arg */
2172 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2175 java_plugin_class_t *class;
2176 jmethodID constructor_id;
2179 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2181 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2185 jvm_env = cjni_thread_attach ();
2186 if (jvm_env == NULL)
2189 class = realloc (java_classes_list,
2190 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2193 ERROR ("java plugin: realloc failed.");
2194 cjni_thread_detach ();
2197 java_classes_list = class;
2198 class = java_classes_list + java_classes_list_len;
2200 memset (class, 0, sizeof (*class));
2201 class->name = strdup (ci->values[0].value.string);
2202 if (class->name == NULL)
2204 ERROR ("java plugin: strdup failed.");
2205 cjni_thread_detach ();
2208 class->class = NULL;
2209 class->object = NULL;
2211 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2212 thorough the Java community, but (Sun's) `FindClass' and friends need
2214 for (size_t i = 0; class->name[i] != 0; i++)
2215 if (class->name[i] == '.')
2216 class->name[i] = '/';
2219 DEBUG ("java plugin: Loading class %s", class->name);
2221 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2222 if (class->class == NULL)
2224 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2226 cjni_thread_detach ();
2231 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2233 if (constructor_id == NULL)
2235 ERROR ("java plugin: cjni_config_load_plugin: "
2236 "Could not find the constructor for `%s'.",
2238 cjni_thread_detach ();
2243 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2245 if (tmp_object != NULL)
2246 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2248 class->object = NULL;
2249 if (class->object == NULL)
2251 ERROR ("java plugin: cjni_config_load_plugin: "
2252 "Could create a new `%s' object.",
2254 cjni_thread_detach ();
2259 cjni_thread_detach ();
2261 java_classes_list_len++;
2264 } /* }}} int cjni_config_load_plugin */
2266 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2269 cjni_callback_info_t *cbi;
2276 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2278 WARNING ("java plugin: `Plugin' blocks "
2279 "need exactly one string argument.");
2283 name = ci->values[0].value.string;
2286 for (size_t i = 0; i < java_callbacks_num; i++)
2288 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2291 if (strcmp (name, java_callbacks[i].name) != 0)
2294 cbi = java_callbacks + i;
2300 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2301 "configuration callback has been registered. Please make sure, the "
2302 "`LoadPlugin' lines precede the `Plugin' blocks.",
2307 DEBUG ("java plugin: Configuring %s", name);
2309 jvm_env = cjni_thread_attach ();
2310 if (jvm_env == NULL)
2313 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2314 if (o_ocitem == NULL)
2316 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2317 cjni_thread_detach ();
2321 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2322 method = (*jvm_env)->GetMethodID (jvm_env, class,
2323 "config", "(Lorg/collectd/api/OConfigItem;)I");
2325 (*jvm_env)->CallIntMethod (jvm_env,
2326 cbi->object, method, o_ocitem);
2328 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2329 cjni_thread_detach ();
2331 } /* }}} int cjni_config_plugin_block */
2333 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2342 for (int i = 0; i < ci->children_num; i++)
2344 oconfig_item_t *child = ci->children + i;
2346 if (strcasecmp ("JVMArg", child->key) == 0)
2348 status = cjni_config_add_jvm_arg (child);
2354 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2356 status = cjni_config_load_plugin (child);
2362 else if (strcasecmp ("Plugin", child->key) == 0)
2364 status = cjni_config_plugin_block (child);
2372 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2377 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2378 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2380 if ((success == 0) && (errors > 0))
2382 ERROR ("java plugin: All statements failed.");
2387 } /* }}} int cjni_config_perform */
2389 /* Copy the children of `ci' to the global `config_block' variable. */
2390 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2392 oconfig_item_t *ci_copy;
2393 oconfig_item_t *tmp;
2395 assert (ci != NULL);
2396 if (ci->children_num == 0)
2397 return (0); /* nothing to do */
2399 ci_copy = oconfig_clone (ci);
2400 if (ci_copy == NULL)
2402 ERROR ("java plugin: oconfig_clone failed.");
2406 if (config_block == NULL)
2408 config_block = ci_copy;
2412 tmp = realloc (config_block->children,
2413 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2416 ERROR ("java plugin: realloc failed.");
2417 oconfig_free (ci_copy);
2420 config_block->children = tmp;
2422 /* Copy the pointers */
2423 memcpy (config_block->children + config_block->children_num,
2425 ci_copy->children_num * sizeof (*ci_copy->children));
2426 config_block->children_num += ci_copy->children_num;
2428 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2429 memset (ci_copy->children, 0,
2430 ci_copy->children_num * sizeof (*ci_copy->children));
2431 ci_copy->children_num = 0;
2433 oconfig_free (ci_copy);
2436 } /* }}} int cjni_config_callback */
2438 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2439 * and `cjni_write'. In particular, delete the global reference to the Java
2441 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2444 cjni_callback_info_t *cbi;
2446 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2448 cbi = (cjni_callback_info_t *) arg;
2450 /* This condition can occur when shutting down. */
2460 jvm_env = cjni_thread_attach ();
2461 if (jvm_env == NULL)
2463 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2467 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2474 cjni_thread_detach ();
2475 } /* }}} void cjni_callback_info_destroy */
2477 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2478 static int cjni_read (user_data_t *ud) /* {{{ */
2481 cjni_callback_info_t *cbi;
2486 ERROR ("java plugin: cjni_read: jvm == NULL");
2490 if ((ud == NULL) || (ud->data == NULL))
2492 ERROR ("java plugin: cjni_read: Invalid user data.");
2496 jvm_env = cjni_thread_attach ();
2497 if (jvm_env == NULL)
2500 cbi = (cjni_callback_info_t *) ud->data;
2502 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2505 cjni_thread_detach ();
2506 return (ret_status);
2507 } /* }}} int cjni_read */
2509 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2510 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2514 cjni_callback_info_t *cbi;
2520 ERROR ("java plugin: cjni_write: jvm == NULL");
2524 if ((ud == NULL) || (ud->data == NULL))
2526 ERROR ("java plugin: cjni_write: Invalid user data.");
2530 jvm_env = cjni_thread_attach ();
2531 if (jvm_env == NULL)
2534 cbi = (cjni_callback_info_t *) ud->data;
2536 vl_java = ctoj_value_list (jvm_env, ds, vl);
2537 if (vl_java == NULL)
2539 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2540 cjni_thread_detach ();
2544 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2545 cbi->object, cbi->method, vl_java);
2547 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2549 cjni_thread_detach ();
2550 return (ret_status);
2551 } /* }}} int cjni_write */
2553 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2554 static int cjni_flush (cdtime_t timeout, const char *identifier, /* {{{ */
2558 cjni_callback_info_t *cbi;
2560 jobject o_identifier;
2565 ERROR ("java plugin: cjni_flush: jvm == NULL");
2569 if ((ud == NULL) || (ud->data == NULL))
2571 ERROR ("java plugin: cjni_flush: Invalid user data.");
2575 jvm_env = cjni_thread_attach ();
2576 if (jvm_env == NULL)
2579 cbi = (cjni_callback_info_t *) ud->data;
2581 o_timeout = ctoj_jdouble_to_number (jvm_env,
2582 (jdouble) CDTIME_T_TO_DOUBLE (timeout));
2583 if (o_timeout == NULL)
2585 ERROR ("java plugin: cjni_flush: Converting double "
2586 "to Number object failed.");
2587 cjni_thread_detach ();
2591 o_identifier = NULL;
2592 if (identifier != NULL)
2594 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2595 if (o_identifier == NULL)
2597 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2598 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2599 cjni_thread_detach ();
2604 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2605 cbi->object, cbi->method, o_timeout, o_identifier);
2607 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2608 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2610 cjni_thread_detach ();
2611 return (ret_status);
2612 } /* }}} int cjni_flush */
2614 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2615 static void cjni_log (int severity, const char *message, /* {{{ */
2619 cjni_callback_info_t *cbi;
2625 if ((ud == NULL) || (ud->data == NULL))
2628 jvm_env = cjni_thread_attach ();
2629 if (jvm_env == NULL)
2632 cbi = (cjni_callback_info_t *) ud->data;
2634 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2635 if (o_message == NULL)
2637 cjni_thread_detach ();
2641 (*jvm_env)->CallVoidMethod (jvm_env,
2642 cbi->object, cbi->method, (jint) severity, o_message);
2644 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2646 cjni_thread_detach ();
2647 } /* }}} void cjni_log */
2649 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2651 static int cjni_notification (const notification_t *n, /* {{{ */
2655 cjni_callback_info_t *cbi;
2656 jobject o_notification;
2661 ERROR ("java plugin: cjni_read: jvm == NULL");
2665 if ((ud == NULL) || (ud->data == NULL))
2667 ERROR ("java plugin: cjni_read: Invalid user data.");
2671 jvm_env = cjni_thread_attach ();
2672 if (jvm_env == NULL)
2675 cbi = (cjni_callback_info_t *) ud->data;
2677 o_notification = ctoj_notification (jvm_env, n);
2678 if (o_notification == NULL)
2680 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2681 cjni_thread_detach ();
2685 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2686 cbi->object, cbi->method, o_notification);
2688 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2690 cjni_thread_detach ();
2691 return (ret_status);
2692 } /* }}} int cjni_notification */
2694 /* Callbacks for matches implemented in Java */
2695 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2699 cjni_callback_info_t *cbi_ret;
2700 cjni_callback_info_t *cbi_factory;
2710 #define BAIL_OUT(status) \
2711 if (cbi_ret != NULL) { \
2712 free (cbi_ret->name); \
2713 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2714 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2718 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2719 cjni_thread_detach (); \
2724 ERROR ("java plugin: cjni_read: jvm == NULL");
2728 jvm_env = cjni_thread_attach ();
2729 if (jvm_env == NULL)
2732 /* Find out whether to create a match or a target. */
2733 if (strcasecmp ("Match", ci->key) == 0)
2734 type = CB_TYPE_MATCH;
2735 else if (strcasecmp ("Target", ci->key) == 0)
2736 type = CB_TYPE_TARGET;
2739 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2740 "to create a match or a target.");
2744 /* This is the name of the match we should create. */
2745 name = ci->values[0].value.string;
2747 /* Lets see if we have a matching factory here.. */
2749 for (size_t i = 0; i < java_callbacks_num; i++)
2751 if (java_callbacks[i].type != type)
2754 if (strcmp (name, java_callbacks[i].name) != 0)
2757 cbi_factory = java_callbacks + i;
2761 /* Nope, no factory for that name.. */
2762 if (cbi_factory == NULL)
2764 ERROR ("java plugin: cjni_match_target_create: "
2765 "No such match factory registered: %s",
2770 /* We convert `ci' to its Java equivalent.. */
2771 o_ci = ctoj_oconfig_item (jvm_env, ci);
2774 ERROR ("java plugin: cjni_match_target_create: "
2775 "ctoj_oconfig_item failed.");
2779 /* Allocate a new callback info structure. This is going to be our user_data
2781 cbi_ret = calloc (1, sizeof (*cbi_ret));
2782 if (cbi_ret == NULL)
2784 ERROR ("java plugin: cjni_match_target_create: calloc failed.");
2788 cbi_ret->object = NULL;
2789 cbi_ret->type = type;
2791 /* Lets fill the callback info structure.. First, the name: */
2792 cbi_ret->name = strdup (name);
2793 if (cbi_ret->name == NULL)
2795 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2799 /* Then call the factory method so it creates a new object for us. */
2800 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2801 cbi_factory->object, cbi_factory->method, o_ci);
2804 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2808 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2811 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2815 /* This is the class of the match. It is possibly different from the class of
2816 * the match-factory! */
2817 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2818 if (cbi_ret->class == NULL)
2820 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2824 /* Lookup the `int match (DataSet, ValueList)' method. */
2825 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2826 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2827 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2828 if (cbi_ret->method == NULL)
2830 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2834 /* Return the newly created match via the user_data pointer. */
2835 *user_data = (void *) cbi_ret;
2837 cjni_thread_detach ();
2839 DEBUG ("java plugin: cjni_match_target_create: "
2840 "Successfully created a `%s' %s.",
2841 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2846 } /* }}} int cjni_match_target_create */
2848 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2850 cjni_callback_info_destroy (*user_data);
2854 } /* }}} int cjni_match_target_destroy */
2856 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2857 value_list_t *vl, notification_meta_t **meta, void **user_data)
2860 cjni_callback_info_t *cbi;
2868 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2872 jvm_env = cjni_thread_attach ();
2873 if (jvm_env == NULL)
2876 cbi = (cjni_callback_info_t *) *user_data;
2878 o_vl = ctoj_value_list (jvm_env, ds, vl);
2881 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2882 cjni_thread_detach ();
2886 o_ds = ctoj_data_set (jvm_env, ds);
2889 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2890 cjni_thread_detach ();
2894 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2897 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2899 /* If we're executing a target, copy the `ValueList' back to our
2900 * `value_list_t'. */
2901 if (cbi->type == CB_TYPE_TARGET)
2903 value_list_t new_vl = { 0 };
2905 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2908 ERROR ("java plugin: cjni_match_target_invoke: "
2909 "jtoc_value_list failed.");
2911 else /* if (status == 0) */
2913 /* plugin_dispatch_values assures that this is dynamically allocated
2917 /* This will replace the vl->values pointer to a new, dynamically
2918 * allocated piece of memory. */
2919 memcpy (vl, &new_vl, sizeof (*vl));
2921 } /* if (cbi->type == CB_TYPE_TARGET) */
2923 cjni_thread_detach ();
2924 return (ret_status);
2925 } /* }}} int cjni_match_target_invoke */
2927 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2928 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2932 for (size_t i = 0; i < java_callbacks_num; i++)
2934 if (java_callbacks[i].type != CB_TYPE_INIT)
2937 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2939 status = (*jvm_env)->CallIntMethod (jvm_env,
2940 java_callbacks[i].object, java_callbacks[i].method);
2943 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2944 "Removing read function.",
2945 java_callbacks[i].name, status);
2946 plugin_unregister_read (java_callbacks[i].name);
2951 } /* }}} int cjni_init_plugins */
2953 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2954 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2958 for (size_t i = 0; i < java_callbacks_num; i++)
2960 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2963 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2965 status = (*jvm_env)->CallIntMethod (jvm_env,
2966 java_callbacks[i].object, java_callbacks[i].method);
2969 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2970 java_callbacks[i].name, status);
2975 } /* }}} int cjni_shutdown_plugins */
2978 static int cjni_shutdown (void) /* {{{ */
2981 JavaVMAttachArgs args = { 0 };
2988 args.version = JNI_VERSION_1_2;
2990 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
2993 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2998 /* Execute all the shutdown functions registered by plugins. */
2999 cjni_shutdown_plugins (jvm_env);
3001 /* Release all the global references to callback functions */
3002 for (size_t i = 0; i < java_callbacks_num; i++)
3004 if (java_callbacks[i].object != NULL)
3006 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3007 java_callbacks[i].object = NULL;
3009 sfree (java_callbacks[i].name);
3011 java_callbacks_num = 0;
3012 sfree (java_callbacks);
3014 /* Release all the global references to directly loaded classes. */
3015 for (size_t i = 0; i < java_classes_list_len; i++)
3017 if (java_classes_list[i].object != NULL)
3019 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3020 java_classes_list[i].object = NULL;
3022 sfree (java_classes_list[i].name);
3024 java_classes_list_len = 0;
3025 sfree (java_classes_list);
3027 /* Destroy the JVM */
3028 DEBUG ("java plugin: Destroying the JVM.");
3029 (*jvm)->DestroyJavaVM (jvm);
3033 pthread_key_delete (jvm_env_key);
3035 /* Free the JVM argument list */
3036 for (size_t i = 0; i < jvm_argc; i++)
3037 sfree (jvm_argv[i]);
3042 } /* }}} int cjni_shutdown */
3044 /* Initialization: Create a JVM, load all configured classes and call their
3045 * `config' and `init' callback methods. */
3046 static int cjni_init (void) /* {{{ */
3050 if ((config_block == NULL) && (jvm == NULL))
3052 ERROR ("java plugin: cjni_init: No configuration block for "
3053 "the java plugin was found.");
3057 if (config_block != NULL)
3059 cjni_config_perform (config_block);
3060 oconfig_free (config_block);
3065 ERROR ("java plugin: cjni_init: jvm == NULL");
3069 jvm_env = cjni_thread_attach ();
3070 if (jvm_env == NULL)
3073 cjni_init_plugins (jvm_env);
3075 cjni_thread_detach ();
3077 } /* }}} int cjni_init */
3079 void module_register (void)
3081 plugin_register_complex_config ("java", cjni_config_callback);
3082 plugin_register_init ("java", cjni_init);
3083 plugin_register_shutdown ("java", cjni_shutdown);
3084 } /* void module_register (void) */
3086 /* vim: set sw=2 sts=2 et fdm=marker : */