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"
31 #if !defined(JNI_VERSION_1_2)
32 # error "Need JNI 1.2 compatible interface!"
38 struct cjni_jvm_env_s /* {{{ */
41 int reference_counter;
43 typedef struct cjni_jvm_env_s cjni_jvm_env_t;
46 struct java_plugin_class_s /* {{{ */
52 typedef struct java_plugin_class_s java_plugin_class_t;
55 #define CB_TYPE_CONFIG 1
56 #define CB_TYPE_INIT 2
57 #define CB_TYPE_READ 3
58 #define CB_TYPE_WRITE 4
59 #define CB_TYPE_FLUSH 5
60 #define CB_TYPE_SHUTDOWN 6
62 #define CB_TYPE_NOTIFICATION 8
63 #define CB_TYPE_MATCH 9
64 #define CB_TYPE_TARGET 10
65 struct cjni_callback_info_s /* {{{ */
73 typedef struct cjni_callback_info_s cjni_callback_info_t;
79 static JavaVM *jvm = NULL;
80 static pthread_key_t jvm_env_key;
82 /* Configuration options for the JVM. */
83 static char **jvm_argv = NULL;
84 static size_t jvm_argc = 0;
86 /* List of class names to load */
87 static java_plugin_class_t *java_classes_list = NULL;
88 static size_t java_classes_list_len;
90 /* List of config, init, and shutdown callbacks. */
91 static cjni_callback_info_t *java_callbacks = NULL;
92 static size_t java_callbacks_num = 0;
93 static pthread_mutex_t java_callbacks_lock = PTHREAD_MUTEX_INITIALIZER;
95 static oconfig_item_t *config_block = NULL;
100 * Mostly functions that are needed by the Java interface (``native'')
103 static void cjni_callback_info_destroy (void *arg);
104 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env,
105 jobject o_name, jobject o_callback, int type);
106 static int cjni_callback_register (JNIEnv *jvm_env, jobject o_name,
107 jobject o_callback, int type);
108 static int cjni_read (user_data_t *user_data);
109 static int cjni_write (const data_set_t *ds, const value_list_t *vl,
111 static int cjni_flush (cdtime_t timeout, const char *identifier, user_data_t *ud);
112 static void cjni_log (int severity, const char *message, user_data_t *ud);
113 static int cjni_notification (const notification_t *n, user_data_t *ud);
115 /* Create, destroy, and match/invoke functions, used by both, matches AND
117 static int cjni_match_target_create (const oconfig_item_t *ci, void **user_data);
118 static int cjni_match_target_destroy (void **user_data);
119 static int cjni_match_target_invoke (const data_set_t *ds, value_list_t *vl,
120 notification_meta_t **meta, void **user_data);
123 * C to Java conversion functions
125 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
127 jclass class_ptr, jobject object_ptr, const char *method_name)
132 /* Create a java.lang.String */
133 o_string = (*jvm_env)->NewStringUTF (jvm_env,
134 (string != NULL) ? string : "");
135 if (o_string == NULL)
137 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
141 /* Search for the `void setFoo (String s)' method. */
142 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
143 method_name, "(Ljava/lang/String;)V");
146 ERROR ("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
148 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
152 /* Call the method. */
153 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, o_string);
155 /* Decrease reference counter on the java.lang.String object. */
156 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
159 } /* }}} int ctoj_string */
161 static jstring ctoj_output_string (JNIEnv *jvm_env, /* {{{ */
166 /* Create a java.lang.String */
167 o_string = (*jvm_env)->NewStringUTF (jvm_env,
168 (string != NULL) ? string : "");
169 if (o_string == NULL)
171 ERROR ("java plugin: ctoj_output_string: NewStringUTF failed.");
176 } /* }}} int ctoj_output_string */
178 static int ctoj_int (JNIEnv *jvm_env, /* {{{ */
180 jclass class_ptr, jobject object_ptr, const char *method_name)
184 /* Search for the `void setFoo (int i)' method. */
185 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
186 method_name, "(I)V");
189 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
194 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
197 } /* }}} int ctoj_int */
199 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
201 jclass class_ptr, jobject object_ptr, const char *method_name)
205 /* Search for the `void setFoo (long l)' method. */
206 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
207 method_name, "(J)V");
210 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
215 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
218 } /* }}} int ctoj_long */
220 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
222 jclass class_ptr, jobject object_ptr, const char *method_name)
226 /* Search for the `void setFoo (double d)' method. */
227 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
228 method_name, "(D)V");
231 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
236 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
239 } /* }}} int ctoj_double */
241 /* Convert a jlong to a java.lang.Number */
242 static jobject ctoj_jlong_to_number (JNIEnv *jvm_env, jlong value) /* {{{ */
245 jmethodID m_long_constructor;
247 /* Look up the java.lang.Long class */
248 c_long = (*jvm_env)->FindClass (jvm_env, "java/lang/Long");
251 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
252 "java.lang.Long class failed.");
256 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
257 c_long, "<init>", "(J)V");
258 if (m_long_constructor == NULL)
260 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
261 "`Long (long)' constructor failed.");
265 return ((*jvm_env)->NewObject (jvm_env,
266 c_long, m_long_constructor, value));
267 } /* }}} jobject ctoj_jlong_to_number */
269 /* Convert a jdouble to a java.lang.Number */
270 static jobject ctoj_jdouble_to_number (JNIEnv *jvm_env, jdouble value) /* {{{ */
273 jmethodID m_double_constructor;
275 /* Look up the java.lang.Long class */
276 c_double = (*jvm_env)->FindClass (jvm_env, "java/lang/Double");
277 if (c_double == NULL)
279 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
280 "java.lang.Double class failed.");
284 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
285 c_double, "<init>", "(D)V");
286 if (m_double_constructor == NULL)
288 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
289 "`Double (double)' constructor failed.");
293 return ((*jvm_env)->NewObject (jvm_env,
294 c_double, m_double_constructor, value));
295 } /* }}} jobject ctoj_jdouble_to_number */
297 /* Convert a value_t to a java.lang.Number */
298 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
299 value_t value, int ds_type)
301 if (ds_type == DS_TYPE_COUNTER)
302 return (ctoj_jlong_to_number (jvm_env, (jlong) value.counter));
303 else if (ds_type == DS_TYPE_GAUGE)
304 return (ctoj_jdouble_to_number (jvm_env, (jdouble) value.gauge));
305 if (ds_type == DS_TYPE_DERIVE)
306 return (ctoj_jlong_to_number (jvm_env, (jlong) value.derive));
307 if (ds_type == DS_TYPE_ABSOLUTE)
308 return (ctoj_jlong_to_number (jvm_env, (jlong) value.absolute));
311 } /* }}} jobject ctoj_value_to_number */
313 /* Convert a data_source_t to a org/collectd/api/DataSource */
314 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
315 const data_source_t *dsrc)
318 jmethodID m_datasource_constructor;
319 jobject o_datasource;
322 /* Look up the DataSource class */
323 c_datasource = (*jvm_env)->FindClass (jvm_env,
324 "org/collectd/api/DataSource");
325 if (c_datasource == NULL)
327 ERROR ("java plugin: ctoj_data_source: "
328 "FindClass (org/collectd/api/DataSource) failed.");
332 /* Lookup the `ValueList ()' constructor. */
333 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
335 if (m_datasource_constructor == NULL)
337 ERROR ("java plugin: ctoj_data_source: Cannot find the "
338 "`DataSource ()' constructor.");
342 /* Create a new instance. */
343 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
344 m_datasource_constructor);
345 if (o_datasource == NULL)
347 ERROR ("java plugin: ctoj_data_source: "
348 "Creating a new DataSource instance failed.");
352 /* Set name via `void setName (String name)' */
353 status = ctoj_string (jvm_env, dsrc->name,
354 c_datasource, o_datasource, "setName");
357 ERROR ("java plugin: ctoj_data_source: "
358 "ctoj_string (setName) failed.");
359 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
363 /* Set type via `void setType (int type)' */
364 status = ctoj_int (jvm_env, dsrc->type,
365 c_datasource, o_datasource, "setType");
368 ERROR ("java plugin: ctoj_data_source: "
369 "ctoj_int (setType) failed.");
370 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
374 /* Set min via `void setMin (double min)' */
375 status = ctoj_double (jvm_env, dsrc->min,
376 c_datasource, o_datasource, "setMin");
379 ERROR ("java plugin: ctoj_data_source: "
380 "ctoj_double (setMin) failed.");
381 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
385 /* Set max via `void setMax (double max)' */
386 status = ctoj_double (jvm_env, dsrc->max,
387 c_datasource, o_datasource, "setMax");
390 ERROR ("java plugin: ctoj_data_source: "
391 "ctoj_double (setMax) failed.");
392 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
396 return (o_datasource);
397 } /* }}} jobject ctoj_data_source */
399 /* Convert a oconfig_value_t to a org/collectd/api/OConfigValue */
400 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
401 oconfig_value_t ocvalue)
404 jmethodID m_ocvalue_constructor;
408 m_ocvalue_constructor = NULL;
411 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
412 "org/collectd/api/OConfigValue");
413 if (c_ocvalue == NULL)
415 ERROR ("java plugin: ctoj_oconfig_value: "
416 "FindClass (org/collectd/api/OConfigValue) failed.");
420 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
422 jboolean tmp_boolean;
424 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
426 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
428 if (m_ocvalue_constructor == NULL)
430 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
431 "`OConfigValue (boolean)' constructor.");
435 return ((*jvm_env)->NewObject (jvm_env,
436 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
437 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
438 else if (ocvalue.type == OCONFIG_TYPE_STRING)
440 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
441 "<init>", "(Ljava/lang/String;)V");
442 if (m_ocvalue_constructor == NULL)
444 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
445 "`OConfigValue (String)' constructor.");
449 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
450 if (o_argument == NULL)
452 ERROR ("java plugin: ctoj_oconfig_value: "
453 "Creating a String object failed.");
457 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
459 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
460 "<init>", "(Ljava/lang/Number;)V");
461 if (m_ocvalue_constructor == NULL)
463 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
464 "`OConfigValue (Number)' constructor.");
468 o_argument = ctoj_jdouble_to_number (jvm_env,
469 (jdouble) ocvalue.value.number);
470 if (o_argument == NULL)
472 ERROR ("java plugin: ctoj_oconfig_value: "
473 "Creating a Number object failed.");
482 assert (m_ocvalue_constructor != NULL);
483 assert (o_argument != NULL);
485 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
486 c_ocvalue, m_ocvalue_constructor, o_argument);
487 if (o_ocvalue == NULL)
489 ERROR ("java plugin: ctoj_oconfig_value: "
490 "Creating an OConfigValue object failed.");
491 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
495 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
497 } /* }}} jobject ctoj_oconfig_value */
499 /* Convert a oconfig_item_t to a org/collectd/api/OConfigItem */
500 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
501 const oconfig_item_t *ci)
504 jmethodID m_ocitem_constructor;
505 jmethodID m_addvalue;
506 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 (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 (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;
622 /* Look up the org/collectd/api/DataSet class */
623 c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet");
624 if (c_dataset == NULL)
626 ERROR ("java plugin: ctoj_data_set: Looking up the "
627 "org/collectd/api/DataSet class failed.");
631 /* Search for the `DataSet (String type)' constructor. */
632 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
633 c_dataset, "<init>", "(Ljava/lang/String;)V");
634 if (m_constructor == NULL)
636 ERROR ("java plugin: ctoj_data_set: Looking up the "
637 "`DataSet (String)' constructor failed.");
641 /* Search for the `void addDataSource (DataSource)' method. */
642 m_add = (*jvm_env)->GetMethodID (jvm_env,
643 c_dataset, "addDataSource", "(Lorg/collectd/api/DataSource;)V");
646 ERROR ("java plugin: ctoj_data_set: Looking up the "
647 "`addDataSource (DataSource)' method failed.");
651 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
654 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
658 o_dataset = (*jvm_env)->NewObject (jvm_env,
659 c_dataset, m_constructor, o_type);
660 if (o_dataset == NULL)
662 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
663 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
667 /* Decrease reference counter on the java.lang.String object. */
668 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
670 for (i = 0; i < ds->ds_num; i++)
672 jobject o_datasource;
674 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
675 if (o_datasource == NULL)
677 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
678 ds->type, ds->ds[i].name);
679 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
683 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
685 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
686 } /* for (i = 0; i < ds->ds_num; i++) */
689 } /* }}} jobject ctoj_data_set */
691 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
692 value_t value, int ds_type,
693 jclass class_ptr, jobject object_ptr)
695 jmethodID m_addvalue;
698 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
699 "addValue", "(Ljava/lang/Number;)V");
700 if (m_addvalue == NULL)
702 ERROR ("java plugin: ctoj_value_list_add_value: "
703 "Cannot find method `void addValue (Number)'.");
707 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
708 if (o_number == NULL)
710 ERROR ("java plugin: ctoj_value_list_add_value: "
711 "ctoj_value_to_number failed.");
715 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
717 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
720 } /* }}} int ctoj_value_list_add_value */
722 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
723 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
725 jmethodID m_setdataset;
728 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
729 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
730 "setDataSet", "(Lorg/collectd/api/DataSet;)V");
731 if (m_setdataset == NULL)
733 ERROR ("java plugin: ctoj_value_list_add_data_set: "
734 "Cannot find the `void setDataSet (DataSet)' method.");
738 /* Create a DataSet object. */
739 o_dataset = ctoj_data_set (jvm_env, ds);
740 if (o_dataset == NULL)
742 ERROR ("java plugin: ctoj_value_list_add_data_set: "
743 "ctoj_data_set (%s) failed.", ds->type);
747 /* Actually call the method. */
748 (*jvm_env)->CallVoidMethod (jvm_env,
749 o_valuelist, m_setdataset, o_dataset);
751 /* Decrease reference counter on the List<DataSource> object. */
752 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
755 } /* }}} int ctoj_value_list_add_data_set */
757 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
758 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
759 const data_set_t *ds, const value_list_t *vl)
762 jmethodID m_valuelist_constructor;
767 /* First, create a new ValueList instance..
768 * Look up the class.. */
769 c_valuelist = (*jvm_env)->FindClass (jvm_env,
770 "org/collectd/api/ValueList");
771 if (c_valuelist == NULL)
773 ERROR ("java plugin: ctoj_value_list: "
774 "FindClass (org/collectd/api/ValueList) failed.");
778 /* Lookup the `ValueList ()' constructor. */
779 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
781 if (m_valuelist_constructor == NULL)
783 ERROR ("java plugin: ctoj_value_list: Cannot find the "
784 "`ValueList ()' constructor.");
788 /* Create a new instance. */
789 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
790 m_valuelist_constructor);
791 if (o_valuelist == NULL)
793 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
798 status = ctoj_value_list_add_data_set (jvm_env,
799 c_valuelist, o_valuelist, ds);
802 ERROR ("java plugin: ctoj_value_list: "
803 "ctoj_value_list_add_data_set failed.");
804 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
808 /* Set the strings.. */
809 #define SET_STRING(str,method_name) do { \
810 status = ctoj_string (jvm_env, str, \
811 c_valuelist, o_valuelist, method_name); \
813 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
815 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
819 SET_STRING (vl->host, "setHost");
820 SET_STRING (vl->plugin, "setPlugin");
821 SET_STRING (vl->plugin_instance, "setPluginInstance");
822 SET_STRING (vl->type, "setType");
823 SET_STRING (vl->type_instance, "setTypeInstance");
827 /* Set the `time' member. Java stores time in milliseconds. */
828 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (vl->time),
829 c_valuelist, o_valuelist, "setTime");
832 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
833 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
837 /* Set the `interval' member.. */
838 status = ctoj_long (jvm_env,
839 (jlong) CDTIME_T_TO_MS (vl->interval),
840 c_valuelist, o_valuelist, "setInterval");
843 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
844 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
848 for (i = 0; i < vl->values_len; i++)
850 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
851 c_valuelist, o_valuelist);
854 ERROR ("java plugin: ctoj_value_list: "
855 "ctoj_value_list_add_value failed.");
856 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
861 return (o_valuelist);
862 } /* }}} jobject ctoj_value_list */
864 /* Convert a notification_t to a org/collectd/api/Notification */
865 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
866 const notification_t *n)
868 jclass c_notification;
869 jmethodID m_constructor;
870 jobject o_notification;
873 /* First, create a new Notification instance..
874 * Look up the class.. */
875 c_notification = (*jvm_env)->FindClass (jvm_env,
876 "org/collectd/api/Notification");
877 if (c_notification == NULL)
879 ERROR ("java plugin: ctoj_notification: "
880 "FindClass (org/collectd/api/Notification) failed.");
884 /* Lookup the `Notification ()' constructor. */
885 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
887 if (m_constructor == NULL)
889 ERROR ("java plugin: ctoj_notification: Cannot find the "
890 "`Notification ()' constructor.");
894 /* Create a new instance. */
895 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
897 if (o_notification == NULL)
899 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
904 /* Set the strings.. */
905 #define SET_STRING(str,method_name) do { \
906 status = ctoj_string (jvm_env, str, \
907 c_notification, o_notification, method_name); \
909 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
911 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
915 SET_STRING (n->host, "setHost");
916 SET_STRING (n->plugin, "setPlugin");
917 SET_STRING (n->plugin_instance, "setPluginInstance");
918 SET_STRING (n->type, "setType");
919 SET_STRING (n->type_instance, "setTypeInstance");
920 SET_STRING (n->message, "setMessage");
924 /* Set the `time' member. Java stores time in milliseconds. */
925 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (n->time),
926 c_notification, o_notification, "setTime");
929 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
930 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
934 /* Set the `severity' member.. */
935 status = ctoj_int (jvm_env, (jint) n->severity,
936 c_notification, o_notification, "setSeverity");
939 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
940 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
944 return (o_notification);
945 } /* }}} jobject ctoj_notification */
948 * Java to C conversion functions
950 /* Call a `String <method> ()' method. */
951 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
952 char *buffer, size_t buffer_size, int empty_okay,
953 jclass class_ptr, jobject object_ptr, const char *method_name)
959 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
960 method_name, "()Ljava/lang/String;");
961 if (method_id == NULL)
963 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
968 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
969 if ((string_obj == NULL) && (empty_okay == 0))
971 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
975 else if ((string_obj == NULL) && (empty_okay != 0))
977 memset (buffer, 0, buffer_size);
981 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
984 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
985 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
989 sstrncpy (buffer, c_str, buffer_size);
991 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
992 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
995 } /* }}} int jtoc_string */
997 /* Call an `int <method> ()' method. */
998 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
1000 jclass class_ptr, jobject object_ptr, const char *method_name)
1002 jmethodID method_id;
1004 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1005 method_name, "()I");
1006 if (method_id == NULL)
1008 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
1013 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
1016 } /* }}} int jtoc_int */
1018 /* Call a `long <method> ()' method. */
1019 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
1021 jclass class_ptr, jobject object_ptr, const char *method_name)
1023 jmethodID method_id;
1025 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1026 method_name, "()J");
1027 if (method_id == NULL)
1029 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1034 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1037 } /* }}} int jtoc_long */
1039 /* Call a `double <method> ()' method. */
1040 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1042 jclass class_ptr, jobject object_ptr, const char *method_name)
1044 jmethodID method_id;
1046 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1047 method_name, "()D");
1048 if (method_id == NULL)
1050 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1055 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1058 } /* }}} int jtoc_double */
1060 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1061 value_t *ret_value, int ds_type, jobject object_ptr)
1066 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1068 if (ds_type == DS_TYPE_GAUGE)
1072 status = jtoc_double (jvm_env, &tmp_double,
1073 class_ptr, object_ptr, "doubleValue");
1076 ERROR ("java plugin: jtoc_value: "
1077 "jtoc_double failed.");
1080 (*ret_value).gauge = (gauge_t) tmp_double;
1086 status = jtoc_long (jvm_env, &tmp_long,
1087 class_ptr, object_ptr, "longValue");
1090 ERROR ("java plugin: jtoc_value: "
1091 "jtoc_long failed.");
1095 if (ds_type == DS_TYPE_DERIVE)
1096 (*ret_value).derive = (derive_t) tmp_long;
1097 else if (ds_type == DS_TYPE_ABSOLUTE)
1098 (*ret_value).absolute = (absolute_t) tmp_long;
1100 (*ret_value).counter = (counter_t) tmp_long;
1104 } /* }}} int jtoc_value */
1106 /* Read a List<Number>, convert it to `value_t' and add it to the given
1107 * `value_list_t'. */
1108 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1109 const data_set_t *ds, value_list_t *vl,
1110 jclass class_ptr, jobject object_ptr)
1112 jmethodID m_getvalues;
1113 jmethodID m_toarray;
1115 jobjectArray o_number_array;
1121 values_num = ds->ds_num;
1124 o_number_array = NULL;
1127 #define BAIL_OUT(status) \
1129 if (o_number_array != NULL) \
1130 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1131 if (o_list != NULL) \
1132 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1135 /* Call: List<Number> ValueList.getValues () */
1136 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1137 "getValues", "()Ljava/util/List;");
1138 if (m_getvalues == NULL)
1140 ERROR ("java plugin: jtoc_values_array: "
1141 "Cannot find method `List getValues ()'.");
1145 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1148 ERROR ("java plugin: jtoc_values_array: "
1149 "CallObjectMethod (getValues) failed.");
1153 /* Call: Number[] List.toArray () */
1154 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1155 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1156 "toArray", "()[Ljava/lang/Object;");
1157 if (m_toarray == NULL)
1159 ERROR ("java plugin: jtoc_values_array: "
1160 "Cannot find method `Object[] toArray ()'.");
1164 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1165 if (o_number_array == NULL)
1167 ERROR ("java plugin: jtoc_values_array: "
1168 "CallObjectMethod (toArray) failed.");
1172 values = (value_t *) calloc (values_num, sizeof (value_t));
1175 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1179 for (i = 0; i < values_num; i++)
1184 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1185 o_number_array, (jsize) i);
1186 if (o_number == NULL)
1188 ERROR ("java plugin: jtoc_values_array: "
1189 "GetObjectArrayElement (%i) failed.", i);
1193 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1196 ERROR ("java plugin: jtoc_values_array: "
1197 "jtoc_value (%i) failed.", i);
1200 } /* for (i = 0; i < values_num; i++) */
1202 vl->values = values;
1203 vl->values_len = values_num;
1206 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1207 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1209 } /* }}} int jtoc_values_array */
1211 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1212 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1218 const data_set_t *ds;
1220 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1221 if (class_ptr == NULL)
1223 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1227 /* eo == empty okay */
1228 #define SET_STRING(buffer,method, eo) do { \
1229 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1230 class_ptr, object_ptr, method); \
1231 if (status != 0) { \
1232 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1237 SET_STRING(vl->type, "getType", /* empty = */ 0);
1239 ds = plugin_get_ds (vl->type);
1242 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1243 "Please consult the types.db(5) manpage for mor information.",
1248 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1249 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1250 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1251 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1255 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1258 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1261 /* Java measures time in milliseconds. */
1262 vl->time = MS_TO_CDTIME_T (tmp_long);
1264 status = jtoc_long (jvm_env, &tmp_long,
1265 class_ptr, object_ptr, "getInterval");
1268 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1271 vl->interval = MS_TO_CDTIME_T (tmp_long);
1273 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1276 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1281 } /* }}} int jtoc_value_list */
1283 /* Convert a org/collectd/api/Notification to a notification_t. */
1284 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1292 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1293 if (class_ptr == NULL)
1295 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1299 /* eo == empty okay */
1300 #define SET_STRING(buffer,method, eo) do { \
1301 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1302 class_ptr, object_ptr, method); \
1303 if (status != 0) { \
1304 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1309 SET_STRING (n->host, "getHost", /* empty = */ 1);
1310 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1311 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1312 SET_STRING (n->type, "getType", /* empty = */ 1);
1313 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1314 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1318 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1321 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1324 /* Java measures time in milliseconds. */
1325 n->time = MS_TO_CDTIME_T(tmp_long);
1327 status = jtoc_int (jvm_env, &tmp_int,
1328 class_ptr, object_ptr, "getSeverity");
1331 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1334 n->severity = (int) tmp_int;
1337 } /* }}} int jtoc_notification */
1339 * Functions accessible from Java
1341 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1342 jobject this, jobject java_vl)
1344 value_list_t vl = VALUE_LIST_INIT;
1347 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1349 status = jtoc_value_list (jvm_env, &vl, java_vl);
1352 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1356 status = plugin_dispatch_values (&vl);
1361 } /* }}} jint cjni_api_dispatch_values */
1363 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1364 jobject this, jobject o_notification)
1369 memset (&n, 0, sizeof (n));
1372 status = jtoc_notification (jvm_env, &n, o_notification);
1375 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1379 status = plugin_dispatch_notification (&n);
1382 } /* }}} jint cjni_api_dispatch_notification */
1384 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1385 jobject this, jobject o_string_type)
1387 const char *ds_name;
1388 const data_set_t *ds;
1391 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1392 if (ds_name == NULL)
1394 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1398 ds = plugin_get_ds (ds_name);
1399 DEBUG ("java plugin: cjni_api_get_ds: "
1400 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1402 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1407 o_dataset = ctoj_data_set (jvm_env, ds);
1409 } /* }}} jint cjni_api_get_ds */
1411 static jint JNICALL cjni_api_register_config (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_CONFIG));
1415 } /* }}} jint cjni_api_register_config */
1417 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1418 jobject this, jobject o_name, jobject o_config)
1420 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1421 } /* }}} jint cjni_api_register_init */
1423 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1424 jobject this, jobject o_name, jobject o_read)
1427 cjni_callback_info_t *cbi;
1429 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1433 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1435 memset (&ud, 0, sizeof (ud));
1436 ud.data = (void *) cbi;
1437 ud.free_func = cjni_callback_info_destroy;
1439 plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read,
1440 /* interval = */ 0, &ud);
1442 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1445 } /* }}} jint cjni_api_register_read */
1447 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1448 jobject this, jobject o_name, jobject o_write)
1451 cjni_callback_info_t *cbi;
1453 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1457 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1459 memset (&ud, 0, sizeof (ud));
1460 ud.data = (void *) cbi;
1461 ud.free_func = cjni_callback_info_destroy;
1463 plugin_register_write (cbi->name, cjni_write, &ud);
1465 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1468 } /* }}} jint cjni_api_register_write */
1470 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1471 jobject this, jobject o_name, jobject o_flush)
1474 cjni_callback_info_t *cbi;
1476 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1480 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1482 memset (&ud, 0, sizeof (ud));
1483 ud.data = (void *) cbi;
1484 ud.free_func = cjni_callback_info_destroy;
1486 plugin_register_flush (cbi->name, cjni_flush, &ud);
1488 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1491 } /* }}} jint cjni_api_register_flush */
1493 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1494 jobject this, jobject o_name, jobject o_shutdown)
1496 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1498 } /* }}} jint cjni_api_register_shutdown */
1500 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1501 jobject this, jobject o_name, jobject o_log)
1504 cjni_callback_info_t *cbi;
1506 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1510 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1512 memset (&ud, 0, sizeof (ud));
1513 ud.data = (void *) cbi;
1514 ud.free_func = cjni_callback_info_destroy;
1516 plugin_register_log (cbi->name, cjni_log, &ud);
1518 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1521 } /* }}} jint cjni_api_register_log */
1523 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1524 jobject this, jobject o_name, jobject o_notification)
1527 cjni_callback_info_t *cbi;
1529 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1530 CB_TYPE_NOTIFICATION);
1534 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1536 memset (&ud, 0, sizeof (ud));
1537 ud.data = (void *) cbi;
1538 ud.free_func = cjni_callback_info_destroy;
1540 plugin_register_notification (cbi->name, cjni_notification, &ud);
1542 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1545 } /* }}} jint cjni_api_register_notification */
1547 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1548 jobject this, jobject o_name, jobject o_match, int type)
1553 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1556 ERROR ("java plugin: cjni_api_register_match_target: "
1557 "GetStringUTFChars failed.");
1561 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1564 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1568 if (type == CB_TYPE_MATCH)
1570 match_proc_t m_proc;
1572 memset (&m_proc, 0, sizeof (m_proc));
1573 m_proc.create = cjni_match_target_create;
1574 m_proc.destroy = cjni_match_target_destroy;
1575 m_proc.match = (void *) cjni_match_target_invoke;
1577 status = fc_register_match (c_name, m_proc);
1579 else if (type == CB_TYPE_TARGET)
1581 target_proc_t t_proc;
1583 memset (&t_proc, 0, sizeof (t_proc));
1584 t_proc.create = cjni_match_target_create;
1585 t_proc.destroy = cjni_match_target_destroy;
1586 t_proc.invoke = cjni_match_target_invoke;
1588 status = fc_register_target (c_name, t_proc);
1592 ERROR ("java plugin: cjni_api_register_match_target: "
1593 "Don't know whether to create a match or a target.");
1594 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1600 ERROR ("java plugin: cjni_api_register_match_target: "
1602 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1603 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1607 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1610 } /* }}} jint cjni_api_register_match_target */
1612 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1613 jobject this, jobject o_name, jobject o_match)
1615 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1617 } /* }}} jint cjni_api_register_match */
1619 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1620 jobject this, jobject o_name, jobject o_target)
1622 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1624 } /* }}} jint cjni_api_register_target */
1626 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1627 jobject this, jint severity, jobject o_message)
1631 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1634 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1638 if (severity < LOG_ERR)
1640 if (severity > LOG_DEBUG)
1641 severity = LOG_DEBUG;
1643 plugin_log (severity, "%s", c_str);
1645 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1646 } /* }}} void cjni_api_log */
1648 static jstring JNICALL cjni_api_get_hostname (JNIEnv *jvm_env, jobject this)
1650 return ctoj_output_string(jvm_env, hostname_g);
1653 /* List of ``native'' functions, i. e. C-functions that can be called from
1655 static JNINativeMethod jni_api_functions[] = /* {{{ */
1658 "(Lorg/collectd/api/ValueList;)I",
1659 cjni_api_dispatch_values },
1661 { "dispatchNotification",
1662 "(Lorg/collectd/api/Notification;)I",
1663 cjni_api_dispatch_notification },
1666 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1670 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1671 cjni_api_register_config },
1674 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1675 cjni_api_register_init },
1678 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1679 cjni_api_register_read },
1682 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1683 cjni_api_register_write },
1686 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1687 cjni_api_register_flush },
1689 { "registerShutdown",
1690 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1691 cjni_api_register_shutdown },
1694 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1695 cjni_api_register_log },
1697 { "registerNotification",
1698 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1699 cjni_api_register_notification },
1702 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1703 cjni_api_register_match },
1706 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1707 cjni_api_register_target },
1710 "(ILjava/lang/String;)V",
1714 "()Ljava/lang/String;",
1715 cjni_api_get_hostname },
1718 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1719 / sizeof (jni_api_functions[0]);
1725 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1726 * all registration functions. */
1727 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1728 jobject o_name, jobject o_callback, int type)
1731 cjni_callback_info_t *cbi;
1732 const char *method_name;
1733 const char *method_signature;
1737 case CB_TYPE_CONFIG:
1738 method_name = "config";
1739 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1743 method_name = "init";
1744 method_signature = "()I";
1748 method_name = "read";
1749 method_signature = "()I";
1753 method_name = "write";
1754 method_signature = "(Lorg/collectd/api/ValueList;)I";
1758 method_name = "flush";
1759 method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I";
1762 case CB_TYPE_SHUTDOWN:
1763 method_name = "shutdown";
1764 method_signature = "()I";
1768 method_name = "log";
1769 method_signature = "(ILjava/lang/String;)V";
1772 case CB_TYPE_NOTIFICATION:
1773 method_name = "notification";
1774 method_signature = "(Lorg/collectd/api/Notification;)I";
1778 method_name = "createMatch";
1779 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1780 "Lorg/collectd/api/CollectdMatchInterface;";
1783 case CB_TYPE_TARGET:
1784 method_name = "createTarget";
1785 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1786 "Lorg/collectd/api/CollectdTargetInterface;";
1790 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1795 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1798 ERROR ("java plugin: cjni_callback_info_create: "
1799 "GetStringUTFChars failed.");
1803 cbi = calloc (1, sizeof (*cbi));
1806 ERROR ("java plugin: cjni_callback_info_create: calloc failed.");
1807 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1812 cbi->name = strdup (c_name);
1813 if (cbi->name == NULL)
1815 pthread_mutex_unlock (&java_callbacks_lock);
1816 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1817 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1822 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1824 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1825 if (cbi->object == NULL)
1827 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1833 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1834 if (cbi->class == NULL)
1836 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1837 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1843 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1844 method_name, method_signature);
1845 if (cbi->method == NULL)
1847 ERROR ("java plugin: cjni_callback_info_create: "
1848 "Cannot find the `%s' method with signature `%s'.",
1849 method_name, method_signature);
1850 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1857 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1859 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1860 * to the global `java_callbacks' variable. This is used for `config', `init',
1861 * and `shutdown' callbacks. */
1862 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1863 jobject o_name, jobject o_callback, int type)
1865 cjni_callback_info_t *cbi;
1866 cjni_callback_info_t *tmp;
1868 const char *type_str;
1871 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1878 case CB_TYPE_CONFIG:
1879 type_str = "config";
1886 case CB_TYPE_SHUTDOWN:
1887 type_str = "shutdown";
1894 case CB_TYPE_TARGET:
1895 type_str = "target";
1899 type_str = "<unknown>";
1901 DEBUG ("java plugin: Registering new %s callback: %s",
1902 type_str, cbi->name);
1905 pthread_mutex_lock (&java_callbacks_lock);
1907 tmp = realloc (java_callbacks,
1908 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1911 pthread_mutex_unlock (&java_callbacks_lock);
1912 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1914 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1919 java_callbacks = tmp;
1920 java_callbacks[java_callbacks_num] = *cbi;
1921 java_callbacks_num++;
1923 pthread_mutex_unlock (&java_callbacks_lock);
1927 } /* }}} int cjni_callback_register */
1929 /* Callback for `pthread_key_create'. It frees the data contained in
1930 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1931 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1933 cjni_jvm_env_t *cjni_env;
1938 cjni_env = (cjni_jvm_env_t *) args;
1940 if (cjni_env->reference_counter > 0)
1942 ERROR ("java plugin: cjni_jvm_env_destroy: "
1943 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1946 if (cjni_env->jvm_env != NULL)
1948 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1949 (void *) cjni_env->jvm_env);
1952 /* The pointer is allocated in `cjni_thread_attach' */
1954 } /* }}} void cjni_jvm_env_destroy */
1956 /* Register ``native'' functions with the JVM. Native functions are C-functions
1957 * that can be called by Java code. */
1958 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1960 jclass api_class_ptr;
1963 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1964 if (api_class_ptr == NULL)
1966 ERROR ("cjni_init_native: Cannot find the API class \"org.collectd.api"
1967 ".Collectd\". Please set the correct class path "
1968 "using 'JVMArg \"-Djava.class.path=...\"'.");
1972 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1973 jni_api_functions, (jint) jni_api_functions_num);
1976 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1981 } /* }}} int cjni_init_native */
1983 /* Create the JVM. This is called when the first thread tries to access the JVM
1984 * via cjni_thread_attach. */
1985 static int cjni_create_jvm (void) /* {{{ */
1988 JavaVMInitArgs vm_args;
1989 JavaVMOption vm_options[jvm_argc];
1997 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
2000 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
2001 "with status %i.", status);
2007 memset (&vm_args, 0, sizeof (vm_args));
2008 vm_args.version = JNI_VERSION_1_2;
2009 vm_args.options = vm_options;
2010 vm_args.nOptions = (jint) jvm_argc;
2012 for (i = 0; i < jvm_argc; i++)
2014 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
2016 vm_args.options[i].optionString = jvm_argv[i];
2019 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
2022 ERROR ("java plugin: cjni_create_jvm: "
2023 "JNI_CreateJavaVM failed with status %i.",
2027 assert (jvm != NULL);
2028 assert (jvm_env != NULL);
2030 /* Call RegisterNatives */
2031 status = cjni_init_native (jvm_env);
2034 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
2038 DEBUG ("java plugin: The JVM has been created.");
2040 } /* }}} int cjni_create_jvm */
2042 /* Increase the reference counter to the JVM for this thread. If it was zero,
2043 * attach the JVM first. */
2044 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2046 cjni_jvm_env_t *cjni_env;
2049 /* If we're the first thread to access the JVM, we'll have to create it
2055 status = cjni_create_jvm ();
2058 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2062 assert (jvm != NULL);
2064 cjni_env = pthread_getspecific (jvm_env_key);
2065 if (cjni_env == NULL)
2067 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2068 cjni_env = calloc (1, sizeof (*cjni_env));
2069 if (cjni_env == NULL)
2071 ERROR ("java plugin: cjni_thread_attach: calloc failed.");
2074 cjni_env->reference_counter = 0;
2075 cjni_env->jvm_env = NULL;
2077 pthread_setspecific (jvm_env_key, cjni_env);
2080 if (cjni_env->reference_counter > 0)
2082 cjni_env->reference_counter++;
2083 jvm_env = cjni_env->jvm_env;
2088 JavaVMAttachArgs args;
2090 assert (cjni_env->jvm_env == NULL);
2092 memset (&args, 0, sizeof (args));
2093 args.version = JNI_VERSION_1_2;
2095 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2098 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2099 "with status %i.", status);
2103 cjni_env->reference_counter = 1;
2104 cjni_env->jvm_env = jvm_env;
2107 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2108 cjni_env->reference_counter);
2109 assert (jvm_env != NULL);
2111 } /* }}} JNIEnv *cjni_thread_attach */
2113 /* Decrease the reference counter of this thread. If it reaches zero, detach
2115 static int cjni_thread_detach (void) /* {{{ */
2117 cjni_jvm_env_t *cjni_env;
2120 cjni_env = pthread_getspecific (jvm_env_key);
2121 if (cjni_env == NULL)
2123 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2127 assert (cjni_env->reference_counter > 0);
2128 assert (cjni_env->jvm_env != NULL);
2130 cjni_env->reference_counter--;
2131 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2132 cjni_env->reference_counter);
2134 if (cjni_env->reference_counter > 0)
2137 status = (*jvm)->DetachCurrentThread (jvm);
2140 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2141 "with status %i.", status);
2144 cjni_env->reference_counter = 0;
2145 cjni_env->jvm_env = NULL;
2148 } /* }}} int cjni_thread_detach */
2150 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2154 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2156 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2162 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2163 "`LoadPlugin' options! The JVM is already started and I have to "
2164 "ignore this argument: %s",
2165 ci->values[0].value.string);
2169 tmp = realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2172 ERROR ("java plugin: realloc failed.");
2177 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2178 if (jvm_argv[jvm_argc] == NULL)
2180 ERROR ("java plugin: strdup failed.");
2186 } /* }}} int cjni_config_add_jvm_arg */
2188 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2191 java_plugin_class_t *class;
2192 jmethodID constructor_id;
2195 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2197 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2201 jvm_env = cjni_thread_attach ();
2202 if (jvm_env == NULL)
2205 class = realloc (java_classes_list,
2206 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2209 ERROR ("java plugin: realloc failed.");
2210 cjni_thread_detach ();
2213 java_classes_list = class;
2214 class = java_classes_list + java_classes_list_len;
2216 memset (class, 0, sizeof (*class));
2217 class->name = strdup (ci->values[0].value.string);
2218 if (class->name == NULL)
2220 ERROR ("java plugin: strdup failed.");
2221 cjni_thread_detach ();
2224 class->class = NULL;
2225 class->object = NULL;
2227 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2228 thorough the Java community, but (Sun's) `FindClass' and friends need
2231 for (i = 0; class->name[i] != 0; i++)
2232 if (class->name[i] == '.')
2233 class->name[i] = '/';
2236 DEBUG ("java plugin: Loading class %s", class->name);
2238 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2239 if (class->class == NULL)
2241 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2243 cjni_thread_detach ();
2248 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2250 if (constructor_id == NULL)
2252 ERROR ("java plugin: cjni_config_load_plugin: "
2253 "Could not find the constructor for `%s'.",
2255 cjni_thread_detach ();
2260 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2262 if (tmp_object != NULL)
2263 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2265 class->object = NULL;
2266 if (class->object == NULL)
2268 ERROR ("java plugin: cjni_config_load_plugin: "
2269 "Could create a new `%s' object.",
2271 cjni_thread_detach ();
2276 cjni_thread_detach ();
2278 java_classes_list_len++;
2281 } /* }}} int cjni_config_load_plugin */
2283 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2286 cjni_callback_info_t *cbi;
2294 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2296 WARNING ("java plugin: `Plugin' blocks "
2297 "need exactly one string argument.");
2301 name = ci->values[0].value.string;
2304 for (i = 0; i < java_callbacks_num; i++)
2306 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2309 if (strcmp (name, java_callbacks[i].name) != 0)
2312 cbi = java_callbacks + i;
2318 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2319 "configuration callback has been registered. Please make sure, the "
2320 "`LoadPlugin' lines precede the `Plugin' blocks.",
2325 DEBUG ("java plugin: Configuring %s", name);
2327 jvm_env = cjni_thread_attach ();
2328 if (jvm_env == NULL)
2331 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2332 if (o_ocitem == NULL)
2334 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2335 cjni_thread_detach ();
2339 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2340 method = (*jvm_env)->GetMethodID (jvm_env, class,
2341 "config", "(Lorg/collectd/api/OConfigItem;)I");
2343 (*jvm_env)->CallIntMethod (jvm_env,
2344 cbi->object, method, o_ocitem);
2346 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2347 cjni_thread_detach ();
2349 } /* }}} int cjni_config_plugin_block */
2351 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2361 for (i = 0; i < ci->children_num; i++)
2363 oconfig_item_t *child = ci->children + i;
2365 if (strcasecmp ("JVMArg", child->key) == 0)
2367 status = cjni_config_add_jvm_arg (child);
2373 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2375 status = cjni_config_load_plugin (child);
2381 else if (strcasecmp ("Plugin", child->key) == 0)
2383 status = cjni_config_plugin_block (child);
2391 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2396 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2397 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2399 if ((success == 0) && (errors > 0))
2401 ERROR ("java plugin: All statements failed.");
2406 } /* }}} int cjni_config_perform */
2408 /* Copy the children of `ci' to the global `config_block' variable. */
2409 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2411 oconfig_item_t *ci_copy;
2412 oconfig_item_t *tmp;
2414 assert (ci != NULL);
2415 if (ci->children_num == 0)
2416 return (0); /* nothing to do */
2418 ci_copy = oconfig_clone (ci);
2419 if (ci_copy == NULL)
2421 ERROR ("java plugin: oconfig_clone failed.");
2425 if (config_block == NULL)
2427 config_block = ci_copy;
2431 tmp = realloc (config_block->children,
2432 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2435 ERROR ("java plugin: realloc failed.");
2436 oconfig_free (ci_copy);
2439 config_block->children = tmp;
2441 /* Copy the pointers */
2442 memcpy (config_block->children + config_block->children_num,
2444 ci_copy->children_num * sizeof (*ci_copy->children));
2445 config_block->children_num += ci_copy->children_num;
2447 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2448 memset (ci_copy->children, 0,
2449 ci_copy->children_num * sizeof (*ci_copy->children));
2450 ci_copy->children_num = 0;
2452 oconfig_free (ci_copy);
2455 } /* }}} int cjni_config_callback */
2457 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2458 * and `cjni_write'. In particular, delete the global reference to the Java
2460 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2463 cjni_callback_info_t *cbi;
2465 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2467 cbi = (cjni_callback_info_t *) arg;
2469 /* This condition can occur when shutting down. */
2479 jvm_env = cjni_thread_attach ();
2480 if (jvm_env == NULL)
2482 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2486 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2493 cjni_thread_detach ();
2494 } /* }}} void cjni_callback_info_destroy */
2496 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2497 static int cjni_read (user_data_t *ud) /* {{{ */
2500 cjni_callback_info_t *cbi;
2505 ERROR ("java plugin: cjni_read: jvm == NULL");
2509 if ((ud == NULL) || (ud->data == NULL))
2511 ERROR ("java plugin: cjni_read: Invalid user data.");
2515 jvm_env = cjni_thread_attach ();
2516 if (jvm_env == NULL)
2519 cbi = (cjni_callback_info_t *) ud->data;
2521 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2524 cjni_thread_detach ();
2525 return (ret_status);
2526 } /* }}} int cjni_read */
2528 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2529 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2533 cjni_callback_info_t *cbi;
2539 ERROR ("java plugin: cjni_write: jvm == NULL");
2543 if ((ud == NULL) || (ud->data == NULL))
2545 ERROR ("java plugin: cjni_write: Invalid user data.");
2549 jvm_env = cjni_thread_attach ();
2550 if (jvm_env == NULL)
2553 cbi = (cjni_callback_info_t *) ud->data;
2555 vl_java = ctoj_value_list (jvm_env, ds, vl);
2556 if (vl_java == NULL)
2558 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2559 cjni_thread_detach ();
2563 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2564 cbi->object, cbi->method, vl_java);
2566 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2568 cjni_thread_detach ();
2569 return (ret_status);
2570 } /* }}} int cjni_write */
2572 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2573 static int cjni_flush (cdtime_t timeout, const char *identifier, /* {{{ */
2577 cjni_callback_info_t *cbi;
2579 jobject o_identifier;
2584 ERROR ("java plugin: cjni_flush: jvm == NULL");
2588 if ((ud == NULL) || (ud->data == NULL))
2590 ERROR ("java plugin: cjni_flush: Invalid user data.");
2594 jvm_env = cjni_thread_attach ();
2595 if (jvm_env == NULL)
2598 cbi = (cjni_callback_info_t *) ud->data;
2600 o_timeout = ctoj_jdouble_to_number (jvm_env,
2601 (jdouble) CDTIME_T_TO_DOUBLE (timeout));
2602 if (o_timeout == NULL)
2604 ERROR ("java plugin: cjni_flush: Converting double "
2605 "to Number object failed.");
2606 cjni_thread_detach ();
2610 o_identifier = NULL;
2611 if (identifier != NULL)
2613 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2614 if (o_identifier == NULL)
2616 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2617 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2618 cjni_thread_detach ();
2623 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2624 cbi->object, cbi->method, o_timeout, o_identifier);
2626 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2627 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2629 cjni_thread_detach ();
2630 return (ret_status);
2631 } /* }}} int cjni_flush */
2633 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2634 static void cjni_log (int severity, const char *message, /* {{{ */
2638 cjni_callback_info_t *cbi;
2644 if ((ud == NULL) || (ud->data == NULL))
2647 jvm_env = cjni_thread_attach ();
2648 if (jvm_env == NULL)
2651 cbi = (cjni_callback_info_t *) ud->data;
2653 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2654 if (o_message == NULL)
2656 cjni_thread_detach ();
2660 (*jvm_env)->CallVoidMethod (jvm_env,
2661 cbi->object, cbi->method, (jint) severity, o_message);
2663 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2665 cjni_thread_detach ();
2666 } /* }}} void cjni_log */
2668 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2670 static int cjni_notification (const notification_t *n, /* {{{ */
2674 cjni_callback_info_t *cbi;
2675 jobject o_notification;
2680 ERROR ("java plugin: cjni_read: jvm == NULL");
2684 if ((ud == NULL) || (ud->data == NULL))
2686 ERROR ("java plugin: cjni_read: Invalid user data.");
2690 jvm_env = cjni_thread_attach ();
2691 if (jvm_env == NULL)
2694 cbi = (cjni_callback_info_t *) ud->data;
2696 o_notification = ctoj_notification (jvm_env, n);
2697 if (o_notification == NULL)
2699 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2700 cjni_thread_detach ();
2704 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2705 cbi->object, cbi->method, o_notification);
2707 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2709 cjni_thread_detach ();
2710 return (ret_status);
2711 } /* }}} int cjni_notification */
2713 /* Callbacks for matches implemented in Java */
2714 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2718 cjni_callback_info_t *cbi_ret;
2719 cjni_callback_info_t *cbi_factory;
2730 #define BAIL_OUT(status) \
2731 if (cbi_ret != NULL) { \
2732 free (cbi_ret->name); \
2733 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2734 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2738 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2739 cjni_thread_detach (); \
2744 ERROR ("java plugin: cjni_read: jvm == NULL");
2748 jvm_env = cjni_thread_attach ();
2749 if (jvm_env == NULL)
2752 /* Find out whether to create a match or a target. */
2753 if (strcasecmp ("Match", ci->key) == 0)
2754 type = CB_TYPE_MATCH;
2755 else if (strcasecmp ("Target", ci->key) == 0)
2756 type = CB_TYPE_TARGET;
2759 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2760 "to create a match or a target.");
2764 /* This is the name of the match we should create. */
2765 name = ci->values[0].value.string;
2767 /* Lets see if we have a matching factory here.. */
2769 for (i = 0; i < java_callbacks_num; i++)
2771 if (java_callbacks[i].type != type)
2774 if (strcmp (name, java_callbacks[i].name) != 0)
2777 cbi_factory = java_callbacks + i;
2781 /* Nope, no factory for that name.. */
2782 if (cbi_factory == NULL)
2784 ERROR ("java plugin: cjni_match_target_create: "
2785 "No such match factory registered: %s",
2790 /* We convert `ci' to its Java equivalent.. */
2791 o_ci = ctoj_oconfig_item (jvm_env, ci);
2794 ERROR ("java plugin: cjni_match_target_create: "
2795 "ctoj_oconfig_item failed.");
2799 /* Allocate a new callback info structure. This is going to be our user_data
2801 cbi_ret = calloc (1, sizeof (*cbi_ret));
2802 if (cbi_ret == NULL)
2804 ERROR ("java plugin: cjni_match_target_create: calloc failed.");
2808 cbi_ret->object = NULL;
2809 cbi_ret->type = type;
2811 /* Lets fill the callback info structure.. First, the name: */
2812 cbi_ret->name = strdup (name);
2813 if (cbi_ret->name == NULL)
2815 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2819 /* Then call the factory method so it creates a new object for us. */
2820 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2821 cbi_factory->object, cbi_factory->method, o_ci);
2824 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2828 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2831 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2835 /* This is the class of the match. It is possibly different from the class of
2836 * the match-factory! */
2837 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2838 if (cbi_ret->class == NULL)
2840 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2844 /* Lookup the `int match (DataSet, ValueList)' method. */
2845 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2846 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2847 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2848 if (cbi_ret->method == NULL)
2850 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2854 /* Return the newly created match via the user_data pointer. */
2855 *user_data = (void *) cbi_ret;
2857 cjni_thread_detach ();
2859 DEBUG ("java plugin: cjni_match_target_create: "
2860 "Successfully created a `%s' %s.",
2861 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2866 } /* }}} int cjni_match_target_create */
2868 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2870 cjni_callback_info_destroy (*user_data);
2874 } /* }}} int cjni_match_target_destroy */
2876 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2877 value_list_t *vl, notification_meta_t **meta, void **user_data)
2880 cjni_callback_info_t *cbi;
2888 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2892 jvm_env = cjni_thread_attach ();
2893 if (jvm_env == NULL)
2896 cbi = (cjni_callback_info_t *) *user_data;
2898 o_vl = ctoj_value_list (jvm_env, ds, vl);
2901 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2902 cjni_thread_detach ();
2906 o_ds = ctoj_data_set (jvm_env, ds);
2909 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2910 cjni_thread_detach ();
2914 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2917 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2919 /* If we're executing a target, copy the `ValueList' back to our
2920 * `value_list_t'. */
2921 if (cbi->type == CB_TYPE_TARGET)
2923 value_list_t new_vl;
2925 memset (&new_vl, 0, sizeof (new_vl));
2926 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2929 ERROR ("java plugin: cjni_match_target_invoke: "
2930 "jtoc_value_list failed.");
2932 else /* if (status == 0) */
2934 /* plugin_dispatch_values assures that this is dynamically allocated
2938 /* This will replace the vl->values pointer to a new, dynamically
2939 * allocated piece of memory. */
2940 memcpy (vl, &new_vl, sizeof (*vl));
2942 } /* if (cbi->type == CB_TYPE_TARGET) */
2944 cjni_thread_detach ();
2945 return (ret_status);
2946 } /* }}} int cjni_match_target_invoke */
2948 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2949 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2954 for (i = 0; i < java_callbacks_num; i++)
2956 if (java_callbacks[i].type != CB_TYPE_INIT)
2959 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2961 status = (*jvm_env)->CallIntMethod (jvm_env,
2962 java_callbacks[i].object, java_callbacks[i].method);
2965 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2966 "Removing read function.",
2967 java_callbacks[i].name, status);
2968 plugin_unregister_read (java_callbacks[i].name);
2973 } /* }}} int cjni_init_plugins */
2975 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2976 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2981 for (i = 0; i < java_callbacks_num; i++)
2983 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2986 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2988 status = (*jvm_env)->CallIntMethod (jvm_env,
2989 java_callbacks[i].object, java_callbacks[i].method);
2992 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2993 java_callbacks[i].name, status);
2998 } /* }}} int cjni_shutdown_plugins */
3001 static int cjni_shutdown (void) /* {{{ */
3004 JavaVMAttachArgs args;
3012 memset (&args, 0, sizeof (args));
3013 args.version = JNI_VERSION_1_2;
3015 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
3018 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
3023 /* Execute all the shutdown functions registered by plugins. */
3024 cjni_shutdown_plugins (jvm_env);
3026 /* Release all the global references to callback functions */
3027 for (i = 0; i < java_callbacks_num; i++)
3029 if (java_callbacks[i].object != NULL)
3031 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3032 java_callbacks[i].object = NULL;
3034 sfree (java_callbacks[i].name);
3036 java_callbacks_num = 0;
3037 sfree (java_callbacks);
3039 /* Release all the global references to directly loaded classes. */
3040 for (i = 0; i < java_classes_list_len; i++)
3042 if (java_classes_list[i].object != NULL)
3044 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3045 java_classes_list[i].object = NULL;
3047 sfree (java_classes_list[i].name);
3049 java_classes_list_len = 0;
3050 sfree (java_classes_list);
3052 /* Destroy the JVM */
3053 DEBUG ("java plugin: Destroying the JVM.");
3054 (*jvm)->DestroyJavaVM (jvm);
3058 pthread_key_delete (jvm_env_key);
3060 /* Free the JVM argument list */
3061 for (i = 0; i < jvm_argc; i++)
3062 sfree (jvm_argv[i]);
3067 } /* }}} int cjni_shutdown */
3069 /* Initialization: Create a JVM, load all configured classes and call their
3070 * `config' and `init' callback methods. */
3071 static int cjni_init (void) /* {{{ */
3075 if ((config_block == NULL) && (jvm == NULL))
3077 ERROR ("java plugin: cjni_init: No configuration block for "
3078 "the java plugin was found.");
3082 if (config_block != NULL)
3084 cjni_config_perform (config_block);
3085 oconfig_free (config_block);
3090 ERROR ("java plugin: cjni_init: jvm == NULL");
3094 jvm_env = cjni_thread_attach ();
3095 if (jvm_env == NULL)
3098 cjni_init_plugins (jvm_env);
3100 cjni_thread_detach ();
3102 } /* }}} int cjni_init */
3104 void module_register (void)
3106 plugin_register_complex_config ("java", cjni_config_callback);
3107 plugin_register_init ("java", cjni_init);
3108 plugin_register_shutdown ("java", cjni_shutdown);
3109 } /* void module_register (void) */
3111 /* vim: set sw=2 sts=2 et fdm=marker : */