2 * collectd - src/java.c
3 * Copyright (C) 2009 Florian octo Forster
4 * Copyright (C) 2008 Justo Alonso Achaques
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at collectd.org>
21 * Justo Alonso Achaques <justo.alonso at gmail.com>
27 #include "filter_chain.h"
32 #if !defined(JNI_VERSION_1_2)
33 # error "Need JNI 1.2 compatible interface!"
39 struct cjni_jvm_env_s /* {{{ */
42 int reference_counter;
44 typedef struct cjni_jvm_env_s cjni_jvm_env_t;
47 struct java_plugin_class_s /* {{{ */
53 typedef struct java_plugin_class_s java_plugin_class_t;
56 #define CB_TYPE_CONFIG 1
57 #define CB_TYPE_INIT 2
58 #define CB_TYPE_READ 3
59 #define CB_TYPE_WRITE 4
60 #define CB_TYPE_FLUSH 5
61 #define CB_TYPE_SHUTDOWN 6
63 #define CB_TYPE_NOTIFICATION 8
64 #define CB_TYPE_MATCH 9
65 #define CB_TYPE_TARGET 10
66 struct cjni_callback_info_s /* {{{ */
74 typedef struct cjni_callback_info_s cjni_callback_info_t;
80 static JavaVM *jvm = NULL;
81 static pthread_key_t jvm_env_key;
83 /* Configuration options for the JVM. */
84 static char **jvm_argv = NULL;
85 static size_t jvm_argc = 0;
87 /* List of class names to load */
88 static java_plugin_class_t *java_classes_list = NULL;
89 static size_t java_classes_list_len;
91 /* List of config, init, and shutdown callbacks. */
92 static cjni_callback_info_t *java_callbacks = NULL;
93 static size_t java_callbacks_num = 0;
94 static pthread_mutex_t java_callbacks_lock = PTHREAD_MUTEX_INITIALIZER;
96 static oconfig_item_t *config_block = NULL;
101 * Mostly functions that are needed by the Java interface (``native'')
104 static void cjni_callback_info_destroy (void *arg);
105 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env,
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;
512 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/OConfigItem");
513 if (c_ocitem == NULL)
515 ERROR ("java plugin: ctoj_oconfig_item: "
516 "FindClass (org/collectd/api/OConfigItem) failed.");
520 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
522 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
523 "<init>", "(Ljava/lang/String;)V");
524 if (m_ocitem_constructor == NULL)
526 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
527 "`OConfigItem (String)' constructor.");
531 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
532 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
533 if (m_addvalue == NULL)
535 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
536 "`addValue (OConfigValue)' method.");
540 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
541 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
542 if (m_addchild == NULL)
544 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
545 "`addChild (OConfigItem)' method.");
550 /* Create a String object with the key.
551 * Needed for calling the constructor. */
552 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
555 ERROR ("java plugin: ctoj_oconfig_item: "
556 "Creating String object failed.");
560 /* Create an OConfigItem object */
561 o_ocitem = (*jvm_env)->NewObject (jvm_env,
562 c_ocitem, m_ocitem_constructor, o_key);
563 if (o_ocitem == NULL)
565 ERROR ("java plugin: ctoj_oconfig_item: "
566 "Creating an OConfigItem object failed.");
567 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
571 /* We don't need the String object any longer.. */
572 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
574 /* Call OConfigItem.addValue for each value */
575 for (i = 0; i < ci->values_num; i++) /* {{{ */
579 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
582 ERROR ("java plugin: ctoj_oconfig_item: "
583 "Creating an OConfigValue object failed.");
584 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
588 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
589 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
590 } /* }}} for (i = 0; i < ci->values_num; i++) */
592 /* Call OConfigItem.addChild for each child */
593 for (i = 0; i < ci->children_num; i++) /* {{{ */
597 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
600 ERROR ("java plugin: ctoj_oconfig_item: "
601 "Creating an OConfigItem object failed.");
602 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
606 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addchild, o_child);
607 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
608 } /* }}} for (i = 0; i < ci->children_num; i++) */
611 } /* }}} jobject ctoj_oconfig_item */
613 /* Convert a data_set_t to a org/collectd/api/DataSet */
614 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
617 jmethodID m_constructor;
623 /* Look up the org/collectd/api/DataSet class */
624 c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet");
625 if (c_dataset == NULL)
627 ERROR ("java plugin: ctoj_data_set: Looking up the "
628 "org/collectd/api/DataSet class failed.");
632 /* Search for the `DataSet (String type)' constructor. */
633 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
634 c_dataset, "<init>", "(Ljava/lang/String;)V");
635 if (m_constructor == NULL)
637 ERROR ("java plugin: ctoj_data_set: Looking up the "
638 "`DataSet (String)' constructor failed.");
642 /* Search for the `void addDataSource (DataSource)' method. */
643 m_add = (*jvm_env)->GetMethodID (jvm_env,
644 c_dataset, "addDataSource", "(Lorg/collectd/api/DataSource;)V");
647 ERROR ("java plugin: ctoj_data_set: Looking up the "
648 "`addDataSource (DataSource)' method failed.");
652 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
655 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
659 o_dataset = (*jvm_env)->NewObject (jvm_env,
660 c_dataset, m_constructor, o_type);
661 if (o_dataset == NULL)
663 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
664 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
668 /* Decrease reference counter on the java.lang.String object. */
669 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
671 for (i = 0; i < ds->ds_num; i++)
673 jobject o_datasource;
675 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
676 if (o_datasource == NULL)
678 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
679 ds->type, ds->ds[i].name);
680 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
684 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
686 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
687 } /* for (i = 0; i < ds->ds_num; i++) */
690 } /* }}} jobject ctoj_data_set */
692 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
693 value_t value, int ds_type,
694 jclass class_ptr, jobject object_ptr)
696 jmethodID m_addvalue;
699 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
700 "addValue", "(Ljava/lang/Number;)V");
701 if (m_addvalue == NULL)
703 ERROR ("java plugin: ctoj_value_list_add_value: "
704 "Cannot find method `void addValue (Number)'.");
708 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
709 if (o_number == NULL)
711 ERROR ("java plugin: ctoj_value_list_add_value: "
712 "ctoj_value_to_number failed.");
716 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
718 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
721 } /* }}} int ctoj_value_list_add_value */
723 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
724 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
726 jmethodID m_setdataset;
729 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
730 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
731 "setDataSet", "(Lorg/collectd/api/DataSet;)V");
732 if (m_setdataset == NULL)
734 ERROR ("java plugin: ctoj_value_list_add_data_set: "
735 "Cannot find the `void setDataSet (DataSet)' method.");
739 /* Create a DataSet object. */
740 o_dataset = ctoj_data_set (jvm_env, ds);
741 if (o_dataset == NULL)
743 ERROR ("java plugin: ctoj_value_list_add_data_set: "
744 "ctoj_data_set (%s) failed.", ds->type);
748 /* Actually call the method. */
749 (*jvm_env)->CallVoidMethod (jvm_env,
750 o_valuelist, m_setdataset, o_dataset);
752 /* Decrease reference counter on the List<DataSource> object. */
753 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
756 } /* }}} int ctoj_value_list_add_data_set */
758 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
759 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
760 const data_set_t *ds, const value_list_t *vl)
763 jmethodID m_valuelist_constructor;
768 /* First, create a new ValueList instance..
769 * Look up the class.. */
770 c_valuelist = (*jvm_env)->FindClass (jvm_env,
771 "org/collectd/api/ValueList");
772 if (c_valuelist == NULL)
774 ERROR ("java plugin: ctoj_value_list: "
775 "FindClass (org/collectd/api/ValueList) failed.");
779 /* Lookup the `ValueList ()' constructor. */
780 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
782 if (m_valuelist_constructor == NULL)
784 ERROR ("java plugin: ctoj_value_list: Cannot find the "
785 "`ValueList ()' constructor.");
789 /* Create a new instance. */
790 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
791 m_valuelist_constructor);
792 if (o_valuelist == NULL)
794 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
799 status = ctoj_value_list_add_data_set (jvm_env,
800 c_valuelist, o_valuelist, ds);
803 ERROR ("java plugin: ctoj_value_list: "
804 "ctoj_value_list_add_data_set failed.");
805 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
809 /* Set the strings.. */
810 #define SET_STRING(str,method_name) do { \
811 status = ctoj_string (jvm_env, str, \
812 c_valuelist, o_valuelist, method_name); \
814 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
816 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
820 SET_STRING (vl->host, "setHost");
821 SET_STRING (vl->plugin, "setPlugin");
822 SET_STRING (vl->plugin_instance, "setPluginInstance");
823 SET_STRING (vl->type, "setType");
824 SET_STRING (vl->type_instance, "setTypeInstance");
828 /* Set the `time' member. Java stores time in milliseconds. */
829 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (vl->time),
830 c_valuelist, o_valuelist, "setTime");
833 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
834 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
838 /* Set the `interval' member.. */
839 status = ctoj_long (jvm_env,
840 (jlong) CDTIME_T_TO_MS (vl->interval),
841 c_valuelist, o_valuelist, "setInterval");
844 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
845 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
849 for (i = 0; i < vl->values_len; i++)
851 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
852 c_valuelist, o_valuelist);
855 ERROR ("java plugin: ctoj_value_list: "
856 "ctoj_value_list_add_value failed.");
857 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
862 return (o_valuelist);
863 } /* }}} jobject ctoj_value_list */
865 /* Convert a notification_t to a org/collectd/api/Notification */
866 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
867 const notification_t *n)
869 jclass c_notification;
870 jmethodID m_constructor;
871 jobject o_notification;
874 /* First, create a new Notification instance..
875 * Look up the class.. */
876 c_notification = (*jvm_env)->FindClass (jvm_env,
877 "org/collectd/api/Notification");
878 if (c_notification == NULL)
880 ERROR ("java plugin: ctoj_notification: "
881 "FindClass (org/collectd/api/Notification) failed.");
885 /* Lookup the `Notification ()' constructor. */
886 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
888 if (m_constructor == NULL)
890 ERROR ("java plugin: ctoj_notification: Cannot find the "
891 "`Notification ()' constructor.");
895 /* Create a new instance. */
896 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
898 if (o_notification == NULL)
900 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
905 /* Set the strings.. */
906 #define SET_STRING(str,method_name) do { \
907 status = ctoj_string (jvm_env, str, \
908 c_notification, o_notification, method_name); \
910 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
912 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
916 SET_STRING (n->host, "setHost");
917 SET_STRING (n->plugin, "setPlugin");
918 SET_STRING (n->plugin_instance, "setPluginInstance");
919 SET_STRING (n->type, "setType");
920 SET_STRING (n->type_instance, "setTypeInstance");
921 SET_STRING (n->message, "setMessage");
925 /* Set the `time' member. Java stores time in milliseconds. */
926 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (n->time),
927 c_notification, o_notification, "setTime");
930 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
931 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
935 /* Set the `severity' member.. */
936 status = ctoj_int (jvm_env, (jint) n->severity,
937 c_notification, o_notification, "setSeverity");
940 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
941 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
945 return (o_notification);
946 } /* }}} jobject ctoj_notification */
949 * Java to C conversion functions
951 /* Call a `String <method> ()' method. */
952 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
953 char *buffer, size_t buffer_size, int empty_okay,
954 jclass class_ptr, jobject object_ptr, const char *method_name)
960 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
961 method_name, "()Ljava/lang/String;");
962 if (method_id == NULL)
964 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
969 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
970 if ((string_obj == NULL) && (empty_okay == 0))
972 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
976 else if ((string_obj == NULL) && (empty_okay != 0))
978 memset (buffer, 0, buffer_size);
982 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
985 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
986 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
990 sstrncpy (buffer, c_str, buffer_size);
992 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
993 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
996 } /* }}} int jtoc_string */
998 /* Call an `int <method> ()' method. */
999 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
1001 jclass class_ptr, jobject object_ptr, const char *method_name)
1003 jmethodID method_id;
1005 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1006 method_name, "()I");
1007 if (method_id == NULL)
1009 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
1014 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
1017 } /* }}} int jtoc_int */
1019 /* Call a `long <method> ()' method. */
1020 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
1022 jclass class_ptr, jobject object_ptr, const char *method_name)
1024 jmethodID method_id;
1026 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1027 method_name, "()J");
1028 if (method_id == NULL)
1030 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1035 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1038 } /* }}} int jtoc_long */
1040 /* Call a `double <method> ()' method. */
1041 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1043 jclass class_ptr, jobject object_ptr, const char *method_name)
1045 jmethodID method_id;
1047 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1048 method_name, "()D");
1049 if (method_id == NULL)
1051 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1056 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1059 } /* }}} int jtoc_double */
1061 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1062 value_t *ret_value, int ds_type, jobject object_ptr)
1067 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1069 if (ds_type == DS_TYPE_GAUGE)
1073 status = jtoc_double (jvm_env, &tmp_double,
1074 class_ptr, object_ptr, "doubleValue");
1077 ERROR ("java plugin: jtoc_value: "
1078 "jtoc_double failed.");
1081 (*ret_value).gauge = (gauge_t) tmp_double;
1087 status = jtoc_long (jvm_env, &tmp_long,
1088 class_ptr, object_ptr, "longValue");
1091 ERROR ("java plugin: jtoc_value: "
1092 "jtoc_long failed.");
1096 if (ds_type == DS_TYPE_DERIVE)
1097 (*ret_value).derive = (derive_t) tmp_long;
1098 else if (ds_type == DS_TYPE_ABSOLUTE)
1099 (*ret_value).absolute = (absolute_t) tmp_long;
1101 (*ret_value).counter = (counter_t) tmp_long;
1105 } /* }}} int jtoc_value */
1107 /* Read a List<Number>, convert it to `value_t' and add it to the given
1108 * `value_list_t'. */
1109 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1110 const data_set_t *ds, value_list_t *vl,
1111 jclass class_ptr, jobject object_ptr)
1113 jmethodID m_getvalues;
1114 jmethodID m_toarray;
1116 jobjectArray o_number_array;
1122 values_num = ds->ds_num;
1125 o_number_array = NULL;
1128 #define BAIL_OUT(status) \
1130 if (o_number_array != NULL) \
1131 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1132 if (o_list != NULL) \
1133 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1136 /* Call: List<Number> ValueList.getValues () */
1137 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1138 "getValues", "()Ljava/util/List;");
1139 if (m_getvalues == NULL)
1141 ERROR ("java plugin: jtoc_values_array: "
1142 "Cannot find method `List getValues ()'.");
1146 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1149 ERROR ("java plugin: jtoc_values_array: "
1150 "CallObjectMethod (getValues) failed.");
1154 /* Call: Number[] List.toArray () */
1155 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1156 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1157 "toArray", "()[Ljava/lang/Object;");
1158 if (m_toarray == NULL)
1160 ERROR ("java plugin: jtoc_values_array: "
1161 "Cannot find method `Object[] toArray ()'.");
1165 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1166 if (o_number_array == NULL)
1168 ERROR ("java plugin: jtoc_values_array: "
1169 "CallObjectMethod (toArray) failed.");
1173 values = (value_t *) calloc (values_num, sizeof (value_t));
1176 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1180 for (i = 0; i < values_num; i++)
1185 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1186 o_number_array, (jsize) i);
1187 if (o_number == NULL)
1189 ERROR ("java plugin: jtoc_values_array: "
1190 "GetObjectArrayElement (%i) failed.", i);
1194 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1197 ERROR ("java plugin: jtoc_values_array: "
1198 "jtoc_value (%i) failed.", i);
1201 } /* for (i = 0; i < values_num; i++) */
1203 vl->values = values;
1204 vl->values_len = values_num;
1207 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1208 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1210 } /* }}} int jtoc_values_array */
1212 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1213 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1219 const data_set_t *ds;
1221 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1222 if (class_ptr == NULL)
1224 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1228 /* eo == empty okay */
1229 #define SET_STRING(buffer,method, eo) do { \
1230 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1231 class_ptr, object_ptr, method); \
1232 if (status != 0) { \
1233 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1238 SET_STRING(vl->type, "getType", /* empty = */ 0);
1240 ds = plugin_get_ds (vl->type);
1243 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1244 "Please consult the types.db(5) manpage for mor information.",
1249 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1250 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1251 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1252 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1256 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1259 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1262 /* Java measures time in milliseconds. */
1263 vl->time = MS_TO_CDTIME_T (tmp_long);
1265 status = jtoc_long (jvm_env, &tmp_long,
1266 class_ptr, object_ptr, "getInterval");
1269 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1272 vl->interval = MS_TO_CDTIME_T (tmp_long);
1274 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1277 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1282 } /* }}} int jtoc_value_list */
1284 /* Convert a org/collectd/api/Notification to a notification_t. */
1285 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1293 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1294 if (class_ptr == NULL)
1296 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1300 /* eo == empty okay */
1301 #define SET_STRING(buffer,method, eo) do { \
1302 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1303 class_ptr, object_ptr, method); \
1304 if (status != 0) { \
1305 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1310 SET_STRING (n->host, "getHost", /* empty = */ 1);
1311 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1312 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1313 SET_STRING (n->type, "getType", /* empty = */ 1);
1314 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1315 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1319 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1322 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1325 /* Java measures time in milliseconds. */
1326 n->time = MS_TO_CDTIME_T(tmp_long);
1328 status = jtoc_int (jvm_env, &tmp_int,
1329 class_ptr, object_ptr, "getSeverity");
1332 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1335 n->severity = (int) tmp_int;
1338 } /* }}} int jtoc_notification */
1340 * Functions accessible from Java
1342 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1343 jobject this, jobject java_vl)
1345 value_list_t vl = VALUE_LIST_INIT;
1348 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1350 status = jtoc_value_list (jvm_env, &vl, java_vl);
1353 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1357 status = plugin_dispatch_values (&vl);
1362 } /* }}} jint cjni_api_dispatch_values */
1364 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1365 jobject this, jobject o_notification)
1370 memset (&n, 0, sizeof (n));
1373 status = jtoc_notification (jvm_env, &n, o_notification);
1376 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1380 status = plugin_dispatch_notification (&n);
1383 } /* }}} jint cjni_api_dispatch_notification */
1385 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1386 jobject this, jobject o_string_type)
1388 const char *ds_name;
1389 const data_set_t *ds;
1392 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1393 if (ds_name == NULL)
1395 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1399 ds = plugin_get_ds (ds_name);
1400 DEBUG ("java plugin: cjni_api_get_ds: "
1401 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1403 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1408 o_dataset = ctoj_data_set (jvm_env, ds);
1410 } /* }}} jint cjni_api_get_ds */
1412 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1413 jobject this, jobject o_name, jobject o_config)
1415 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1416 } /* }}} jint cjni_api_register_config */
1418 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1419 jobject this, jobject o_name, jobject o_config)
1421 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1422 } /* }}} jint cjni_api_register_init */
1424 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1425 jobject this, jobject o_name, jobject o_read)
1428 cjni_callback_info_t *cbi;
1430 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1434 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1436 memset (&ud, 0, sizeof (ud));
1437 ud.data = (void *) cbi;
1438 ud.free_func = cjni_callback_info_destroy;
1440 plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read,
1441 /* interval = */ 0, &ud);
1443 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1446 } /* }}} jint cjni_api_register_read */
1448 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1449 jobject this, jobject o_name, jobject o_write)
1452 cjni_callback_info_t *cbi;
1454 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1458 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1460 memset (&ud, 0, sizeof (ud));
1461 ud.data = (void *) cbi;
1462 ud.free_func = cjni_callback_info_destroy;
1464 plugin_register_write (cbi->name, cjni_write, &ud);
1466 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1469 } /* }}} jint cjni_api_register_write */
1471 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1472 jobject this, jobject o_name, jobject o_flush)
1475 cjni_callback_info_t *cbi;
1477 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1481 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1483 memset (&ud, 0, sizeof (ud));
1484 ud.data = (void *) cbi;
1485 ud.free_func = cjni_callback_info_destroy;
1487 plugin_register_flush (cbi->name, cjni_flush, &ud);
1489 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1492 } /* }}} jint cjni_api_register_flush */
1494 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1495 jobject this, jobject o_name, jobject o_shutdown)
1497 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1499 } /* }}} jint cjni_api_register_shutdown */
1501 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1502 jobject this, jobject o_name, jobject o_log)
1505 cjni_callback_info_t *cbi;
1507 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1511 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1513 memset (&ud, 0, sizeof (ud));
1514 ud.data = (void *) cbi;
1515 ud.free_func = cjni_callback_info_destroy;
1517 plugin_register_log (cbi->name, cjni_log, &ud);
1519 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1522 } /* }}} jint cjni_api_register_log */
1524 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1525 jobject this, jobject o_name, jobject o_notification)
1528 cjni_callback_info_t *cbi;
1530 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1531 CB_TYPE_NOTIFICATION);
1535 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1537 memset (&ud, 0, sizeof (ud));
1538 ud.data = (void *) cbi;
1539 ud.free_func = cjni_callback_info_destroy;
1541 plugin_register_notification (cbi->name, cjni_notification, &ud);
1543 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1546 } /* }}} jint cjni_api_register_notification */
1548 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1549 jobject this, jobject o_name, jobject o_match, int type)
1554 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1557 ERROR ("java plugin: cjni_api_register_match_target: "
1558 "GetStringUTFChars failed.");
1562 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1565 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1569 if (type == CB_TYPE_MATCH)
1571 match_proc_t m_proc;
1573 memset (&m_proc, 0, sizeof (m_proc));
1574 m_proc.create = cjni_match_target_create;
1575 m_proc.destroy = cjni_match_target_destroy;
1576 m_proc.match = (void *) cjni_match_target_invoke;
1578 status = fc_register_match (c_name, m_proc);
1580 else if (type == CB_TYPE_TARGET)
1582 target_proc_t t_proc;
1584 memset (&t_proc, 0, sizeof (t_proc));
1585 t_proc.create = cjni_match_target_create;
1586 t_proc.destroy = cjni_match_target_destroy;
1587 t_proc.invoke = cjni_match_target_invoke;
1589 status = fc_register_target (c_name, t_proc);
1593 ERROR ("java plugin: cjni_api_register_match_target: "
1594 "Don't know whether to create a match or a target.");
1595 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1601 ERROR ("java plugin: cjni_api_register_match_target: "
1603 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1604 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1608 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1611 } /* }}} jint cjni_api_register_match_target */
1613 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1614 jobject this, jobject o_name, jobject o_match)
1616 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1618 } /* }}} jint cjni_api_register_match */
1620 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1621 jobject this, jobject o_name, jobject o_target)
1623 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1625 } /* }}} jint cjni_api_register_target */
1627 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1628 jobject this, jint severity, jobject o_message)
1632 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1635 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1639 if (severity < LOG_ERR)
1641 if (severity > LOG_DEBUG)
1642 severity = LOG_DEBUG;
1644 plugin_log (severity, "%s", c_str);
1646 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1647 } /* }}} void cjni_api_log */
1649 static jstring JNICALL cjni_api_get_hostname (JNIEnv *jvm_env, jobject this)
1651 return ctoj_output_string(jvm_env, hostname_g);
1654 /* List of ``native'' functions, i. e. C-functions that can be called from
1656 static JNINativeMethod jni_api_functions[] = /* {{{ */
1659 "(Lorg/collectd/api/ValueList;)I",
1660 cjni_api_dispatch_values },
1662 { "dispatchNotification",
1663 "(Lorg/collectd/api/Notification;)I",
1664 cjni_api_dispatch_notification },
1667 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1671 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1672 cjni_api_register_config },
1675 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1676 cjni_api_register_init },
1679 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1680 cjni_api_register_read },
1683 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1684 cjni_api_register_write },
1687 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1688 cjni_api_register_flush },
1690 { "registerShutdown",
1691 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1692 cjni_api_register_shutdown },
1695 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1696 cjni_api_register_log },
1698 { "registerNotification",
1699 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1700 cjni_api_register_notification },
1703 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1704 cjni_api_register_match },
1707 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1708 cjni_api_register_target },
1711 "(ILjava/lang/String;)V",
1715 "()Ljava/lang/String;",
1716 cjni_api_get_hostname },
1719 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1720 / sizeof (jni_api_functions[0]);
1726 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1727 * all registration functions. */
1728 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1729 jobject o_name, jobject o_callback, int type)
1732 cjni_callback_info_t *cbi;
1733 const char *method_name;
1734 const char *method_signature;
1738 case CB_TYPE_CONFIG:
1739 method_name = "config";
1740 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1744 method_name = "init";
1745 method_signature = "()I";
1749 method_name = "read";
1750 method_signature = "()I";
1754 method_name = "write";
1755 method_signature = "(Lorg/collectd/api/ValueList;)I";
1759 method_name = "flush";
1760 method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I";
1763 case CB_TYPE_SHUTDOWN:
1764 method_name = "shutdown";
1765 method_signature = "()I";
1769 method_name = "log";
1770 method_signature = "(ILjava/lang/String;)V";
1773 case CB_TYPE_NOTIFICATION:
1774 method_name = "notification";
1775 method_signature = "(Lorg/collectd/api/Notification;)I";
1779 method_name = "createMatch";
1780 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1781 "Lorg/collectd/api/CollectdMatchInterface;";
1784 case CB_TYPE_TARGET:
1785 method_name = "createTarget";
1786 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1787 "Lorg/collectd/api/CollectdTargetInterface;";
1791 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1796 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1799 ERROR ("java plugin: cjni_callback_info_create: "
1800 "GetStringUTFChars failed.");
1804 cbi = calloc (1, sizeof (*cbi));
1807 ERROR ("java plugin: cjni_callback_info_create: calloc failed.");
1808 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1813 cbi->name = strdup (c_name);
1814 if (cbi->name == NULL)
1816 pthread_mutex_unlock (&java_callbacks_lock);
1817 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1818 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1823 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1825 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1826 if (cbi->object == NULL)
1828 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1834 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1835 if (cbi->class == NULL)
1837 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1838 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1844 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1845 method_name, method_signature);
1846 if (cbi->method == NULL)
1848 ERROR ("java plugin: cjni_callback_info_create: "
1849 "Cannot find the `%s' method with signature `%s'.",
1850 method_name, method_signature);
1851 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1858 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1860 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1861 * to the global `java_callbacks' variable. This is used for `config', `init',
1862 * and `shutdown' callbacks. */
1863 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1864 jobject o_name, jobject o_callback, int type)
1866 cjni_callback_info_t *cbi;
1867 cjni_callback_info_t *tmp;
1869 const char *type_str;
1872 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1879 case CB_TYPE_CONFIG:
1880 type_str = "config";
1887 case CB_TYPE_SHUTDOWN:
1888 type_str = "shutdown";
1895 case CB_TYPE_TARGET:
1896 type_str = "target";
1900 type_str = "<unknown>";
1902 DEBUG ("java plugin: Registering new %s callback: %s",
1903 type_str, cbi->name);
1906 pthread_mutex_lock (&java_callbacks_lock);
1908 tmp = realloc (java_callbacks,
1909 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1912 pthread_mutex_unlock (&java_callbacks_lock);
1913 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1915 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1920 java_callbacks = tmp;
1921 java_callbacks[java_callbacks_num] = *cbi;
1922 java_callbacks_num++;
1924 pthread_mutex_unlock (&java_callbacks_lock);
1928 } /* }}} int cjni_callback_register */
1930 /* Callback for `pthread_key_create'. It frees the data contained in
1931 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1932 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1934 cjni_jvm_env_t *cjni_env;
1939 cjni_env = (cjni_jvm_env_t *) args;
1941 if (cjni_env->reference_counter > 0)
1943 ERROR ("java plugin: cjni_jvm_env_destroy: "
1944 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1947 if (cjni_env->jvm_env != NULL)
1949 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1950 (void *) cjni_env->jvm_env);
1953 /* The pointer is allocated in `cjni_thread_attach' */
1955 } /* }}} void cjni_jvm_env_destroy */
1957 /* Register ``native'' functions with the JVM. Native functions are C-functions
1958 * that can be called by Java code. */
1959 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1961 jclass api_class_ptr;
1964 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1965 if (api_class_ptr == NULL)
1967 ERROR ("cjni_init_native: Cannot find the API class \"org.collectd.api"
1968 ".Collectd\". Please set the correct class path "
1969 "using 'JVMArg \"-Djava.class.path=...\"'.");
1973 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1974 jni_api_functions, (jint) jni_api_functions_num);
1977 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1982 } /* }}} int cjni_init_native */
1984 /* Create the JVM. This is called when the first thread tries to access the JVM
1985 * via cjni_thread_attach. */
1986 static int cjni_create_jvm (void) /* {{{ */
1989 JavaVMInitArgs vm_args;
1990 JavaVMOption vm_options[jvm_argc];
1998 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
2001 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
2002 "with status %i.", status);
2008 memset (&vm_args, 0, sizeof (vm_args));
2009 vm_args.version = JNI_VERSION_1_2;
2010 vm_args.options = vm_options;
2011 vm_args.nOptions = (jint) jvm_argc;
2013 for (i = 0; i < jvm_argc; i++)
2015 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
2017 vm_args.options[i].optionString = jvm_argv[i];
2020 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
2023 ERROR ("java plugin: cjni_create_jvm: "
2024 "JNI_CreateJavaVM failed with status %i.",
2028 assert (jvm != NULL);
2029 assert (jvm_env != NULL);
2031 /* Call RegisterNatives */
2032 status = cjni_init_native (jvm_env);
2035 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
2039 DEBUG ("java plugin: The JVM has been created.");
2041 } /* }}} int cjni_create_jvm */
2043 /* Increase the reference counter to the JVM for this thread. If it was zero,
2044 * attach the JVM first. */
2045 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2047 cjni_jvm_env_t *cjni_env;
2050 /* If we're the first thread to access the JVM, we'll have to create it
2056 status = cjni_create_jvm ();
2059 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2063 assert (jvm != NULL);
2065 cjni_env = pthread_getspecific (jvm_env_key);
2066 if (cjni_env == NULL)
2068 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2069 cjni_env = calloc (1, sizeof (*cjni_env));
2070 if (cjni_env == NULL)
2072 ERROR ("java plugin: cjni_thread_attach: calloc failed.");
2075 cjni_env->reference_counter = 0;
2076 cjni_env->jvm_env = NULL;
2078 pthread_setspecific (jvm_env_key, cjni_env);
2081 if (cjni_env->reference_counter > 0)
2083 cjni_env->reference_counter++;
2084 jvm_env = cjni_env->jvm_env;
2089 JavaVMAttachArgs args;
2091 assert (cjni_env->jvm_env == NULL);
2093 memset (&args, 0, sizeof (args));
2094 args.version = JNI_VERSION_1_2;
2096 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2099 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2100 "with status %i.", status);
2104 cjni_env->reference_counter = 1;
2105 cjni_env->jvm_env = jvm_env;
2108 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2109 cjni_env->reference_counter);
2110 assert (jvm_env != NULL);
2112 } /* }}} JNIEnv *cjni_thread_attach */
2114 /* Decrease the reference counter of this thread. If it reaches zero, detach
2116 static int cjni_thread_detach (void) /* {{{ */
2118 cjni_jvm_env_t *cjni_env;
2121 cjni_env = pthread_getspecific (jvm_env_key);
2122 if (cjni_env == NULL)
2124 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2128 assert (cjni_env->reference_counter > 0);
2129 assert (cjni_env->jvm_env != NULL);
2131 cjni_env->reference_counter--;
2132 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2133 cjni_env->reference_counter);
2135 if (cjni_env->reference_counter > 0)
2138 status = (*jvm)->DetachCurrentThread (jvm);
2141 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2142 "with status %i.", status);
2145 cjni_env->reference_counter = 0;
2146 cjni_env->jvm_env = NULL;
2149 } /* }}} int cjni_thread_detach */
2151 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2155 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2157 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2163 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2164 "`LoadPlugin' options! The JVM is already started and I have to "
2165 "ignore this argument: %s",
2166 ci->values[0].value.string);
2170 tmp = realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2173 ERROR ("java plugin: realloc failed.");
2178 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2179 if (jvm_argv[jvm_argc] == NULL)
2181 ERROR ("java plugin: strdup failed.");
2187 } /* }}} int cjni_config_add_jvm_arg */
2189 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2192 java_plugin_class_t *class;
2193 jmethodID constructor_id;
2196 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2198 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2202 jvm_env = cjni_thread_attach ();
2203 if (jvm_env == NULL)
2206 class = realloc (java_classes_list,
2207 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2210 ERROR ("java plugin: realloc failed.");
2211 cjni_thread_detach ();
2214 java_classes_list = class;
2215 class = java_classes_list + java_classes_list_len;
2217 memset (class, 0, sizeof (*class));
2218 class->name = strdup (ci->values[0].value.string);
2219 if (class->name == NULL)
2221 ERROR ("java plugin: strdup failed.");
2222 cjni_thread_detach ();
2225 class->class = NULL;
2226 class->object = NULL;
2228 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2229 thorough the Java community, but (Sun's) `FindClass' and friends need
2232 for (i = 0; class->name[i] != 0; i++)
2233 if (class->name[i] == '.')
2234 class->name[i] = '/';
2237 DEBUG ("java plugin: Loading class %s", class->name);
2239 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2240 if (class->class == NULL)
2242 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2244 cjni_thread_detach ();
2249 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2251 if (constructor_id == NULL)
2253 ERROR ("java plugin: cjni_config_load_plugin: "
2254 "Could not find the constructor for `%s'.",
2256 cjni_thread_detach ();
2261 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2263 if (tmp_object != NULL)
2264 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2266 class->object = NULL;
2267 if (class->object == NULL)
2269 ERROR ("java plugin: cjni_config_load_plugin: "
2270 "Could create a new `%s' object.",
2272 cjni_thread_detach ();
2277 cjni_thread_detach ();
2279 java_classes_list_len++;
2282 } /* }}} int cjni_config_load_plugin */
2284 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2287 cjni_callback_info_t *cbi;
2295 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2297 WARNING ("java plugin: `Plugin' blocks "
2298 "need exactly one string argument.");
2302 name = ci->values[0].value.string;
2305 for (i = 0; i < java_callbacks_num; i++)
2307 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2310 if (strcmp (name, java_callbacks[i].name) != 0)
2313 cbi = java_callbacks + i;
2319 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2320 "configuration callback has been registered. Please make sure, the "
2321 "`LoadPlugin' lines precede the `Plugin' blocks.",
2326 DEBUG ("java plugin: Configuring %s", name);
2328 jvm_env = cjni_thread_attach ();
2329 if (jvm_env == NULL)
2332 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2333 if (o_ocitem == NULL)
2335 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2336 cjni_thread_detach ();
2340 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2341 method = (*jvm_env)->GetMethodID (jvm_env, class,
2342 "config", "(Lorg/collectd/api/OConfigItem;)I");
2344 (*jvm_env)->CallIntMethod (jvm_env,
2345 cbi->object, method, o_ocitem);
2347 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2348 cjni_thread_detach ();
2350 } /* }}} int cjni_config_plugin_block */
2352 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2362 for (i = 0; i < ci->children_num; i++)
2364 oconfig_item_t *child = ci->children + i;
2366 if (strcasecmp ("JVMArg", child->key) == 0)
2368 status = cjni_config_add_jvm_arg (child);
2374 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2376 status = cjni_config_load_plugin (child);
2382 else if (strcasecmp ("Plugin", child->key) == 0)
2384 status = cjni_config_plugin_block (child);
2392 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2397 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2398 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2400 if ((success == 0) && (errors > 0))
2402 ERROR ("java plugin: All statements failed.");
2407 } /* }}} int cjni_config_perform */
2409 /* Copy the children of `ci' to the global `config_block' variable. */
2410 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2412 oconfig_item_t *ci_copy;
2413 oconfig_item_t *tmp;
2415 assert (ci != NULL);
2416 if (ci->children_num == 0)
2417 return (0); /* nothing to do */
2419 ci_copy = oconfig_clone (ci);
2420 if (ci_copy == NULL)
2422 ERROR ("java plugin: oconfig_clone failed.");
2426 if (config_block == NULL)
2428 config_block = ci_copy;
2432 tmp = realloc (config_block->children,
2433 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2436 ERROR ("java plugin: realloc failed.");
2437 oconfig_free (ci_copy);
2440 config_block->children = tmp;
2442 /* Copy the pointers */
2443 memcpy (config_block->children + config_block->children_num,
2445 ci_copy->children_num * sizeof (*ci_copy->children));
2446 config_block->children_num += ci_copy->children_num;
2448 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2449 memset (ci_copy->children, 0,
2450 ci_copy->children_num * sizeof (*ci_copy->children));
2451 ci_copy->children_num = 0;
2453 oconfig_free (ci_copy);
2456 } /* }}} int cjni_config_callback */
2458 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2459 * and `cjni_write'. In particular, delete the global reference to the Java
2461 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2464 cjni_callback_info_t *cbi;
2466 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2468 cbi = (cjni_callback_info_t *) arg;
2470 /* This condition can occur when shutting down. */
2480 jvm_env = cjni_thread_attach ();
2481 if (jvm_env == NULL)
2483 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2487 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2494 cjni_thread_detach ();
2495 } /* }}} void cjni_callback_info_destroy */
2497 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2498 static int cjni_read (user_data_t *ud) /* {{{ */
2501 cjni_callback_info_t *cbi;
2506 ERROR ("java plugin: cjni_read: jvm == NULL");
2510 if ((ud == NULL) || (ud->data == NULL))
2512 ERROR ("java plugin: cjni_read: Invalid user data.");
2516 jvm_env = cjni_thread_attach ();
2517 if (jvm_env == NULL)
2520 cbi = (cjni_callback_info_t *) ud->data;
2522 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2525 cjni_thread_detach ();
2526 return (ret_status);
2527 } /* }}} int cjni_read */
2529 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2530 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2534 cjni_callback_info_t *cbi;
2540 ERROR ("java plugin: cjni_write: jvm == NULL");
2544 if ((ud == NULL) || (ud->data == NULL))
2546 ERROR ("java plugin: cjni_write: Invalid user data.");
2550 jvm_env = cjni_thread_attach ();
2551 if (jvm_env == NULL)
2554 cbi = (cjni_callback_info_t *) ud->data;
2556 vl_java = ctoj_value_list (jvm_env, ds, vl);
2557 if (vl_java == NULL)
2559 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2560 cjni_thread_detach ();
2564 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2565 cbi->object, cbi->method, vl_java);
2567 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2569 cjni_thread_detach ();
2570 return (ret_status);
2571 } /* }}} int cjni_write */
2573 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2574 static int cjni_flush (cdtime_t timeout, const char *identifier, /* {{{ */
2578 cjni_callback_info_t *cbi;
2580 jobject o_identifier;
2585 ERROR ("java plugin: cjni_flush: jvm == NULL");
2589 if ((ud == NULL) || (ud->data == NULL))
2591 ERROR ("java plugin: cjni_flush: Invalid user data.");
2595 jvm_env = cjni_thread_attach ();
2596 if (jvm_env == NULL)
2599 cbi = (cjni_callback_info_t *) ud->data;
2601 o_timeout = ctoj_jdouble_to_number (jvm_env,
2602 (jdouble) CDTIME_T_TO_DOUBLE (timeout));
2603 if (o_timeout == NULL)
2605 ERROR ("java plugin: cjni_flush: Converting double "
2606 "to Number object failed.");
2607 cjni_thread_detach ();
2611 o_identifier = NULL;
2612 if (identifier != NULL)
2614 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2615 if (o_identifier == NULL)
2617 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2618 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2619 cjni_thread_detach ();
2624 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2625 cbi->object, cbi->method, o_timeout, o_identifier);
2627 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2628 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2630 cjni_thread_detach ();
2631 return (ret_status);
2632 } /* }}} int cjni_flush */
2634 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2635 static void cjni_log (int severity, const char *message, /* {{{ */
2639 cjni_callback_info_t *cbi;
2645 if ((ud == NULL) || (ud->data == NULL))
2648 jvm_env = cjni_thread_attach ();
2649 if (jvm_env == NULL)
2652 cbi = (cjni_callback_info_t *) ud->data;
2654 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2655 if (o_message == NULL)
2657 cjni_thread_detach ();
2661 (*jvm_env)->CallVoidMethod (jvm_env,
2662 cbi->object, cbi->method, (jint) severity, o_message);
2664 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2666 cjni_thread_detach ();
2667 } /* }}} void cjni_log */
2669 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2671 static int cjni_notification (const notification_t *n, /* {{{ */
2675 cjni_callback_info_t *cbi;
2676 jobject o_notification;
2681 ERROR ("java plugin: cjni_read: jvm == NULL");
2685 if ((ud == NULL) || (ud->data == NULL))
2687 ERROR ("java plugin: cjni_read: Invalid user data.");
2691 jvm_env = cjni_thread_attach ();
2692 if (jvm_env == NULL)
2695 cbi = (cjni_callback_info_t *) ud->data;
2697 o_notification = ctoj_notification (jvm_env, n);
2698 if (o_notification == NULL)
2700 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2701 cjni_thread_detach ();
2705 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2706 cbi->object, cbi->method, o_notification);
2708 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2710 cjni_thread_detach ();
2711 return (ret_status);
2712 } /* }}} int cjni_notification */
2714 /* Callbacks for matches implemented in Java */
2715 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2719 cjni_callback_info_t *cbi_ret;
2720 cjni_callback_info_t *cbi_factory;
2731 #define BAIL_OUT(status) \
2732 if (cbi_ret != NULL) { \
2733 free (cbi_ret->name); \
2734 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2735 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2739 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2740 cjni_thread_detach (); \
2745 ERROR ("java plugin: cjni_read: jvm == NULL");
2749 jvm_env = cjni_thread_attach ();
2750 if (jvm_env == NULL)
2753 /* Find out whether to create a match or a target. */
2754 if (strcasecmp ("Match", ci->key) == 0)
2755 type = CB_TYPE_MATCH;
2756 else if (strcasecmp ("Target", ci->key) == 0)
2757 type = CB_TYPE_TARGET;
2760 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2761 "to create a match or a target.");
2765 /* This is the name of the match we should create. */
2766 name = ci->values[0].value.string;
2768 /* Lets see if we have a matching factory here.. */
2770 for (i = 0; i < java_callbacks_num; i++)
2772 if (java_callbacks[i].type != type)
2775 if (strcmp (name, java_callbacks[i].name) != 0)
2778 cbi_factory = java_callbacks + i;
2782 /* Nope, no factory for that name.. */
2783 if (cbi_factory == NULL)
2785 ERROR ("java plugin: cjni_match_target_create: "
2786 "No such match factory registered: %s",
2791 /* We convert `ci' to its Java equivalent.. */
2792 o_ci = ctoj_oconfig_item (jvm_env, ci);
2795 ERROR ("java plugin: cjni_match_target_create: "
2796 "ctoj_oconfig_item failed.");
2800 /* Allocate a new callback info structure. This is going to be our user_data
2802 cbi_ret = calloc (1, sizeof (*cbi_ret));
2803 if (cbi_ret == NULL)
2805 ERROR ("java plugin: cjni_match_target_create: calloc failed.");
2809 cbi_ret->object = NULL;
2810 cbi_ret->type = type;
2812 /* Lets fill the callback info structure.. First, the name: */
2813 cbi_ret->name = strdup (name);
2814 if (cbi_ret->name == NULL)
2816 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2820 /* Then call the factory method so it creates a new object for us. */
2821 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2822 cbi_factory->object, cbi_factory->method, o_ci);
2825 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2829 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2832 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2836 /* This is the class of the match. It is possibly different from the class of
2837 * the match-factory! */
2838 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2839 if (cbi_ret->class == NULL)
2841 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2845 /* Lookup the `int match (DataSet, ValueList)' method. */
2846 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2847 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2848 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2849 if (cbi_ret->method == NULL)
2851 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2855 /* Return the newly created match via the user_data pointer. */
2856 *user_data = (void *) cbi_ret;
2858 cjni_thread_detach ();
2860 DEBUG ("java plugin: cjni_match_target_create: "
2861 "Successfully created a `%s' %s.",
2862 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2867 } /* }}} int cjni_match_target_create */
2869 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2871 cjni_callback_info_destroy (*user_data);
2875 } /* }}} int cjni_match_target_destroy */
2877 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2878 value_list_t *vl, notification_meta_t **meta, void **user_data)
2881 cjni_callback_info_t *cbi;
2889 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2893 jvm_env = cjni_thread_attach ();
2894 if (jvm_env == NULL)
2897 cbi = (cjni_callback_info_t *) *user_data;
2899 o_vl = ctoj_value_list (jvm_env, ds, vl);
2902 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2903 cjni_thread_detach ();
2907 o_ds = ctoj_data_set (jvm_env, ds);
2910 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2911 cjni_thread_detach ();
2915 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2918 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2920 /* If we're executing a target, copy the `ValueList' back to our
2921 * `value_list_t'. */
2922 if (cbi->type == CB_TYPE_TARGET)
2924 value_list_t new_vl;
2926 memset (&new_vl, 0, sizeof (new_vl));
2927 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2930 ERROR ("java plugin: cjni_match_target_invoke: "
2931 "jtoc_value_list failed.");
2933 else /* if (status == 0) */
2935 /* plugin_dispatch_values assures that this is dynamically allocated
2939 /* This will replace the vl->values pointer to a new, dynamically
2940 * allocated piece of memory. */
2941 memcpy (vl, &new_vl, sizeof (*vl));
2943 } /* if (cbi->type == CB_TYPE_TARGET) */
2945 cjni_thread_detach ();
2946 return (ret_status);
2947 } /* }}} int cjni_match_target_invoke */
2949 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2950 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2955 for (i = 0; i < java_callbacks_num; i++)
2957 if (java_callbacks[i].type != CB_TYPE_INIT)
2960 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2962 status = (*jvm_env)->CallIntMethod (jvm_env,
2963 java_callbacks[i].object, java_callbacks[i].method);
2966 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2967 "Removing read function.",
2968 java_callbacks[i].name, status);
2969 plugin_unregister_read (java_callbacks[i].name);
2974 } /* }}} int cjni_init_plugins */
2976 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2977 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2982 for (i = 0; i < java_callbacks_num; i++)
2984 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2987 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2989 status = (*jvm_env)->CallIntMethod (jvm_env,
2990 java_callbacks[i].object, java_callbacks[i].method);
2993 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2994 java_callbacks[i].name, status);
2999 } /* }}} int cjni_shutdown_plugins */
3002 static int cjni_shutdown (void) /* {{{ */
3005 JavaVMAttachArgs args;
3013 memset (&args, 0, sizeof (args));
3014 args.version = JNI_VERSION_1_2;
3016 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
3019 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
3024 /* Execute all the shutdown functions registered by plugins. */
3025 cjni_shutdown_plugins (jvm_env);
3027 /* Release all the global references to callback functions */
3028 for (i = 0; i < java_callbacks_num; i++)
3030 if (java_callbacks[i].object != NULL)
3032 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3033 java_callbacks[i].object = NULL;
3035 sfree (java_callbacks[i].name);
3037 java_callbacks_num = 0;
3038 sfree (java_callbacks);
3040 /* Release all the global references to directly loaded classes. */
3041 for (i = 0; i < java_classes_list_len; i++)
3043 if (java_classes_list[i].object != NULL)
3045 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3046 java_classes_list[i].object = NULL;
3048 sfree (java_classes_list[i].name);
3050 java_classes_list_len = 0;
3051 sfree (java_classes_list);
3053 /* Destroy the JVM */
3054 DEBUG ("java plugin: Destroying the JVM.");
3055 (*jvm)->DestroyJavaVM (jvm);
3059 pthread_key_delete (jvm_env_key);
3061 /* Free the JVM argument list */
3062 for (i = 0; i < jvm_argc; i++)
3063 sfree (jvm_argv[i]);
3068 } /* }}} int cjni_shutdown */
3070 /* Initialization: Create a JVM, load all configured classes and call their
3071 * `config' and `init' callback methods. */
3072 static int cjni_init (void) /* {{{ */
3076 if ((config_block == NULL) && (jvm == NULL))
3078 ERROR ("java plugin: cjni_init: No configuration block for "
3079 "the java plugin was found.");
3083 if (config_block != NULL)
3085 cjni_config_perform (config_block);
3086 oconfig_free (config_block);
3091 ERROR ("java plugin: cjni_init: jvm == NULL");
3095 jvm_env = cjni_thread_attach ();
3096 if (jvm_env == NULL)
3099 cjni_init_plugins (jvm_env);
3101 cjni_thread_detach ();
3103 } /* }}} int cjni_init */
3105 void module_register (void)
3107 plugin_register_complex_config ("java", cjni_config_callback);
3108 plugin_register_init ("java", cjni_init);
3109 plugin_register_shutdown ("java", cjni_shutdown);
3110 } /* void module_register (void) */
3112 /* vim: set sw=2 sts=2 et fdm=marker : */