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 verplant.org>
21 * Justo Alonso Achaques <justo.alonso at gmail.com>
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 struct java_plugin_config_s /* {{{ */
60 typedef struct java_plugin_config_s java_plugin_config_t;
63 #define CB_TYPE_CONFIG 1
64 #define CB_TYPE_INIT 2
65 #define CB_TYPE_READ 3
66 #define CB_TYPE_WRITE 4
67 #define CB_TYPE_FLUSH 5
68 #define CB_TYPE_SHUTDOWN 6
70 #define CB_TYPE_NOTIFICATION 8
71 struct cjni_callback_info_s /* {{{ */
79 typedef struct cjni_callback_info_s cjni_callback_info_t;
85 static JavaVM *jvm = NULL;
86 static pthread_key_t jvm_env_key;
88 /* Configuration options for the JVM. */
89 static char **jvm_argv = NULL;
90 static size_t jvm_argc = 0;
92 /* List of class names to load */
93 static java_plugin_class_t *java_classes_list = NULL;
94 static size_t java_classes_list_len;
96 /* List of `config_item_t's for Java plugins */
97 static java_plugin_config_t *java_plugin_configs = NULL;
98 static size_t java_plugin_configs_num = 0;
100 /* List of config, init, and shutdown callbacks. */
101 static cjni_callback_info_t *java_callbacks = NULL;
102 static size_t java_callbacks_num = 0;
103 static pthread_mutex_t java_callbacks_lock = PTHREAD_MUTEX_INITIALIZER;
108 * Mostly functions that are needed by the Java interface (``native'')
111 static void cjni_callback_info_destroy (void *arg);
112 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env,
113 jobject o_name, jobject o_callback, int type);
114 static int cjni_callback_register (JNIEnv *jvm_env, jobject o_name,
115 jobject o_callback, int type);
116 static int cjni_read (user_data_t *user_data);
117 static int cjni_write (const data_set_t *ds, const value_list_t *vl,
119 static int cjni_flush (int timeout, const char *identifier, user_data_t *ud);
120 static void cjni_log (int severity, const char *message, user_data_t *ud);
121 static int cjni_notification (const notification_t *n, user_data_t *ud);
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 int ctoj_int (JNIEnv *jvm_env, /* {{{ */
164 jclass class_ptr, jobject object_ptr, const char *method_name)
168 /* Search for the `void setFoo (int i)' method. */
169 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
170 method_name, "(I)V");
173 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
178 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
181 } /* }}} int ctoj_int */
183 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
185 jclass class_ptr, jobject object_ptr, const char *method_name)
189 /* Search for the `void setFoo (long l)' method. */
190 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
191 method_name, "(J)V");
194 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
199 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
202 } /* }}} int ctoj_long */
204 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
206 jclass class_ptr, jobject object_ptr, const char *method_name)
210 /* Search for the `void setFoo (double d)' method. */
211 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
212 method_name, "(D)V");
215 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
220 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
223 } /* }}} int ctoj_double */
225 /* Convert a jlong to a java.lang.Number */
226 static jobject ctoj_jlong_to_number (JNIEnv *jvm_env, jlong value) /* {{{ */
229 jmethodID m_long_constructor;
231 /* Look up the java.lang.Long class */
232 c_long = (*jvm_env)->FindClass (jvm_env, "java.lang.Long");
235 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
236 "java.lang.Long class failed.");
240 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
241 c_long, "<init>", "(J)V");
242 if (m_long_constructor == NULL)
244 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
245 "`Long (long)' constructor failed.");
249 return ((*jvm_env)->NewObject (jvm_env,
250 c_long, m_long_constructor, value));
251 } /* }}} jobject ctoj_jlong_to_number */
253 /* Convert a jdouble to a java.lang.Number */
254 static jobject ctoj_jdouble_to_number (JNIEnv *jvm_env, jdouble value) /* {{{ */
257 jmethodID m_double_constructor;
259 /* Look up the java.lang.Long class */
260 c_double = (*jvm_env)->FindClass (jvm_env, "java.lang.Double");
261 if (c_double == NULL)
263 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
264 "java.lang.Double class failed.");
268 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
269 c_double, "<init>", "(D)V");
270 if (m_double_constructor == NULL)
272 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
273 "`Double (double)' constructor failed.");
277 return ((*jvm_env)->NewObject (jvm_env,
278 c_double, m_double_constructor, value));
279 } /* }}} jobject ctoj_jdouble_to_number */
281 /* Convert a value_t to a java.lang.Number */
282 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
283 value_t value, int ds_type)
285 if (ds_type == DS_TYPE_COUNTER)
286 return (ctoj_jlong_to_number (jvm_env, (jlong) value.counter));
287 else if (ds_type == DS_TYPE_GAUGE)
288 return (ctoj_jdouble_to_number (jvm_env, (jdouble) value.gauge));
291 } /* }}} jobject ctoj_value_to_number */
293 /* Convert a data_source_t to a org.collectd.api.DataSource */
294 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
295 const data_source_t *dsrc)
298 jmethodID m_datasource_constructor;
299 jobject o_datasource;
302 /* Look up the DataSource class */
303 c_datasource = (*jvm_env)->FindClass (jvm_env,
304 "org.collectd.api.DataSource");
305 if (c_datasource == NULL)
307 ERROR ("java plugin: ctoj_data_source: "
308 "FindClass (org.collectd.api.DataSource) failed.");
312 /* Lookup the `ValueList ()' constructor. */
313 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
315 if (m_datasource_constructor == NULL)
317 ERROR ("java plugin: ctoj_data_source: Cannot find the "
318 "`DataSource ()' constructor.");
322 /* Create a new instance. */
323 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
324 m_datasource_constructor);
325 if (o_datasource == NULL)
327 ERROR ("java plugin: ctoj_data_source: "
328 "Creating a new DataSource instance failed.");
332 /* Set name via `void setName (String name)' */
333 status = ctoj_string (jvm_env, dsrc->name,
334 c_datasource, o_datasource, "setName");
337 ERROR ("java plugin: ctoj_data_source: "
338 "ctoj_string (setName) failed.");
339 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
343 /* Set type via `void setType (int type)' */
344 status = ctoj_int (jvm_env, dsrc->type,
345 c_datasource, o_datasource, "setType");
348 ERROR ("java plugin: ctoj_data_source: "
349 "ctoj_int (setType) failed.");
350 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
354 /* Set min via `void setMin (double min)' */
355 status = ctoj_double (jvm_env, dsrc->min,
356 c_datasource, o_datasource, "setMin");
359 ERROR ("java plugin: ctoj_data_source: "
360 "ctoj_double (setMin) failed.");
361 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
365 /* Set max via `void setMax (double max)' */
366 status = ctoj_double (jvm_env, dsrc->max,
367 c_datasource, o_datasource, "setMax");
370 ERROR ("java plugin: ctoj_data_source: "
371 "ctoj_double (setMax) failed.");
372 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
376 return (o_datasource);
377 } /* }}} jobject ctoj_data_source */
379 /* Convert a oconfig_value_t to a org.collectd.api.OConfigValue */
380 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
381 oconfig_value_t ocvalue)
384 jmethodID m_ocvalue_constructor;
388 m_ocvalue_constructor = NULL;
391 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
392 "org.collectd.api.OConfigValue");
393 if (c_ocvalue == NULL)
395 ERROR ("java plugin: ctoj_oconfig_value: "
396 "FindClass (org.collectd.api.OConfigValue) failed.");
400 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
402 jboolean tmp_boolean;
404 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
406 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
408 if (m_ocvalue_constructor == NULL)
410 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
411 "`OConfigValue (boolean)' constructor.");
415 return ((*jvm_env)->NewObject (jvm_env,
416 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
417 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
418 else if (ocvalue.type == OCONFIG_TYPE_STRING)
420 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
421 "<init>", "(Ljava/lang/String;)V");
422 if (m_ocvalue_constructor == NULL)
424 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
425 "`OConfigValue (String)' constructor.");
429 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
430 if (o_argument == NULL)
432 ERROR ("java plugin: ctoj_oconfig_value: "
433 "Creating a String object failed.");
437 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
439 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
440 "<init>", "(Ljava/lang/Number;)V");
441 if (m_ocvalue_constructor == NULL)
443 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
444 "`OConfigValue (Number)' constructor.");
448 o_argument = ctoj_jdouble_to_number (jvm_env,
449 (jdouble) ocvalue.value.number);
450 if (o_argument == NULL)
452 ERROR ("java plugin: ctoj_oconfig_value: "
453 "Creating a Number object failed.");
462 assert (m_ocvalue_constructor != NULL);
463 assert (o_argument != NULL);
465 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
466 c_ocvalue, m_ocvalue_constructor, o_argument);
467 if (o_ocvalue == NULL)
469 ERROR ("java plugin: ctoj_oconfig_value: "
470 "Creating an OConfigValue object failed.");
471 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
475 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
477 } /* }}} jobject ctoj_oconfig_value */
479 /* Convert a oconfig_item_t to a org.collectd.api.OConfigItem */
480 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
481 const oconfig_item_t *ci)
484 jmethodID m_ocitem_constructor;
485 jmethodID m_addvalue;
486 jmethodID m_addchild;
491 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.OConfigItem");
492 if (c_ocitem == NULL)
494 ERROR ("java plugin: ctoj_oconfig_item: "
495 "FindClass (org.collectd.api.OConfigItem) failed.");
499 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
501 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
502 "<init>", "(Ljava/lang/String;)V");
503 if (m_ocitem_constructor == NULL)
505 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
506 "`OConfigItem (String)' constructor.");
510 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
511 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
512 if (m_addvalue == NULL)
514 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
515 "`addValue (OConfigValue)' method.");
519 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
520 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
521 if (m_addchild == NULL)
523 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
524 "`addChild (OConfigItem)' method.");
529 /* Create a String object with the key.
530 * Needed for calling the constructor. */
531 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
534 ERROR ("java plugin: ctoj_oconfig_item: "
535 "Creating String object failed.");
539 /* Create an OConfigItem object */
540 o_ocitem = (*jvm_env)->NewObject (jvm_env,
541 c_ocitem, m_ocitem_constructor, o_key);
542 if (o_ocitem == NULL)
544 ERROR ("java plugin: ctoj_oconfig_item: "
545 "Creating an OConfigItem object failed.");
546 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
550 /* We don't need the String object any longer.. */
551 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
553 /* Call OConfigItem.addValue for each value */
554 for (i = 0; i < ci->values_num; i++) /* {{{ */
558 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
561 ERROR ("java plugin: ctoj_oconfig_item: "
562 "Creating an OConfigValue object failed.");
563 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
567 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
568 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
569 } /* }}} for (i = 0; i < ci->values_num; i++) */
571 /* Call OConfigItem.addChild for each child */
572 for (i = 0; i < ci->children_num; i++) /* {{{ */
576 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
579 ERROR ("java plugin: ctoj_oconfig_item: "
580 "Creating an OConfigItem object failed.");
581 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
585 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_child);
586 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
587 } /* }}} for (i = 0; i < ci->children_num; i++) */
590 } /* }}} jobject ctoj_oconfig_item */
592 /* Convert a data_set_t to a org.collectd.api.DataSet */
593 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
596 jmethodID m_constructor;
602 /* Look up the org.collectd.api.DataSet class */
603 c_dataset = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.DataSet");
604 if (c_dataset == NULL)
606 ERROR ("java plugin: ctoj_data_set: Looking up the "
607 "org.collectd.api.DataSet class failed.");
611 /* Search for the `DataSet (String type)' constructor. */
612 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
613 c_dataset, "<init>", "(Ljava.lang.String;)V");
614 if (m_constructor == NULL)
616 ERROR ("java plugin: ctoj_data_set: Looking up the "
617 "`DataSet (String)' constructor failed.");
621 /* Search for the `void addDataSource (DataSource)' method. */
622 m_add = (*jvm_env)->GetMethodID (jvm_env,
623 c_dataset, "addDataSource", "(Lorg.collectd.api.DataSource;)V");
626 ERROR ("java plugin: ctoj_data_set: Looking up the "
627 "`addDataSource (DataSource)' method failed.");
631 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
634 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
638 o_dataset = (*jvm_env)->NewObject (jvm_env,
639 c_dataset, m_constructor, o_type);
640 if (o_dataset == NULL)
642 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
643 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
647 /* Decrease reference counter on the java.lang.String object. */
648 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
650 for (i = 0; i < ds->ds_num; i++)
652 jobject o_datasource;
654 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
655 if (o_datasource == NULL)
657 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
658 ds->type, ds->ds[i].name);
659 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
663 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
665 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
666 } /* for (i = 0; i < ds->ds_num; i++) */
669 } /* }}} jobject ctoj_data_set */
671 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
672 value_t value, int ds_type,
673 jclass class_ptr, jobject object_ptr)
675 jmethodID m_addvalue;
678 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
679 "addValue", "(Ljava/lang/Number;)V");
680 if (m_addvalue == NULL)
682 ERROR ("java plugin: ctoj_value_list_add_value: "
683 "Cannot find method `void addValue (Number)'.");
687 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
688 if (o_number == NULL)
690 ERROR ("java plugin: ctoj_value_list_add_value: "
691 "ctoj_value_to_number failed.");
695 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
697 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
700 } /* }}} int ctoj_value_list_add_value */
702 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
703 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
705 jmethodID m_setdataset;
708 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
709 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
710 "setDataSet", "(Lorg.collectd.api.DataSet;)V");
711 if (m_setdataset == NULL)
713 ERROR ("java plugin: ctoj_value_list_add_data_set: "
714 "Cannot find the `void setDataSet (DataSet)' method.");
718 /* Create a DataSet object. */
719 o_dataset = ctoj_data_set (jvm_env, ds);
720 if (o_dataset == NULL)
722 ERROR ("java plugin: ctoj_value_list_add_data_set: "
723 "ctoj_data_set (%s) failed.", ds->type);
727 /* Actually call the method. */
728 (*jvm_env)->CallVoidMethod (jvm_env,
729 o_valuelist, m_setdataset, o_dataset);
731 /* Decrease reference counter on the List<DataSource> object. */
732 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
735 } /* }}} int ctoj_value_list_add_data_set */
737 /* Convert a value_list_t (and data_set_t) to a org.collectd.api.ValueList */
738 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
739 const data_set_t *ds, const value_list_t *vl)
742 jmethodID m_valuelist_constructor;
747 /* First, create a new ValueList instance..
748 * Look up the class.. */
749 c_valuelist = (*jvm_env)->FindClass (jvm_env,
750 "org.collectd.api.ValueList");
751 if (c_valuelist == NULL)
753 ERROR ("java plugin: ctoj_value_list: "
754 "FindClass (org.collectd.api.ValueList) failed.");
758 /* Lookup the `ValueList ()' constructor. */
759 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
761 if (m_valuelist_constructor == NULL)
763 ERROR ("java plugin: ctoj_value_list: Cannot find the "
764 "`ValueList ()' constructor.");
768 /* Create a new instance. */
769 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
770 m_valuelist_constructor);
771 if (o_valuelist == NULL)
773 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
778 status = ctoj_value_list_add_data_set (jvm_env,
779 c_valuelist, o_valuelist, ds);
782 ERROR ("java plugin: ctoj_value_list: "
783 "ctoj_value_list_add_data_set failed.");
784 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
788 /* Set the strings.. */
789 #define SET_STRING(str,method_name) do { \
790 status = ctoj_string (jvm_env, str, \
791 c_valuelist, o_valuelist, method_name); \
793 ERROR ("java plugin: ctoj_value_list: jtoc_string (%s) failed.", \
795 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
799 SET_STRING (vl->host, "setHost");
800 SET_STRING (vl->plugin, "setPlugin");
801 SET_STRING (vl->plugin_instance, "setPluginInstance");
802 SET_STRING (vl->type, "setType");
803 SET_STRING (vl->type_instance, "setTypeInstance");
807 /* Set the `time' member. Java stores time in milliseconds. */
808 status = ctoj_long (jvm_env, ((jlong) vl->time) * ((jlong) 1000),
809 c_valuelist, o_valuelist, "setTime");
812 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
813 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
817 /* Set the `interval' member.. */
818 status = ctoj_long (jvm_env, (jlong) vl->interval,
819 c_valuelist, o_valuelist, "setInterval");
822 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
823 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
827 for (i = 0; i < vl->values_len; i++)
829 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
830 c_valuelist, o_valuelist);
833 ERROR ("java plugin: ctoj_value_list: "
834 "ctoj_value_list_add_value failed.");
835 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
840 return (o_valuelist);
841 } /* }}} jobject ctoj_value_list */
843 /* Convert a notification_t to a org.collectd.api.Notification */
844 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
845 const notification_t *n)
847 jclass c_notification;
848 jmethodID m_constructor;
849 jobject o_notification;
852 /* First, create a new Notification instance..
853 * Look up the class.. */
854 c_notification = (*jvm_env)->FindClass (jvm_env,
855 "org.collectd.api.Notification");
856 if (c_notification == NULL)
858 ERROR ("java plugin: ctoj_notification: "
859 "FindClass (org.collectd.api.Notification) failed.");
863 /* Lookup the `Notification ()' constructor. */
864 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
866 if (m_constructor == NULL)
868 ERROR ("java plugin: ctoj_notification: Cannot find the "
869 "`Notification ()' constructor.");
873 /* Create a new instance. */
874 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
876 if (o_notification == NULL)
878 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
883 /* Set the strings.. */
884 #define SET_STRING(str,method_name) do { \
885 status = ctoj_string (jvm_env, str, \
886 c_notification, o_notification, method_name); \
888 ERROR ("java plugin: ctoj_notification: jtoc_string (%s) failed.", \
890 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
894 SET_STRING (n->host, "setHost");
895 SET_STRING (n->plugin, "setPlugin");
896 SET_STRING (n->plugin_instance, "setPluginInstance");
897 SET_STRING (n->type, "setType");
898 SET_STRING (n->type_instance, "setTypeInstance");
899 SET_STRING (n->message, "setMessage");
903 /* Set the `time' member. Java stores time in milliseconds. */
904 status = ctoj_long (jvm_env, ((jlong) n->time) * ((jlong) 1000),
905 c_notification, o_notification, "setTime");
908 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
909 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
913 /* Set the `interval' member.. */
914 status = ctoj_int (jvm_env, (jint) n->severity,
915 c_notification, o_notification, "setSeverity");
918 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
919 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
923 return (o_notification);
924 } /* }}} jobject ctoj_notification */
927 * Java to C conversion functions
929 /* Call a `String <method> ()' method. */
930 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
931 char *buffer, size_t buffer_size,
932 jclass class_ptr, jobject object_ptr, const char *method_name)
938 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
939 method_name, "()Ljava/lang/String;");
940 if (method_id == NULL)
942 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
947 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
948 if (string_obj == NULL)
950 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
955 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
958 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
959 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
963 sstrncpy (buffer, c_str, buffer_size);
965 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
966 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
969 } /* }}} int jtoc_string */
971 /* Call a `long <method> ()' method. */
972 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
974 jclass class_ptr, jobject object_ptr, const char *method_name)
978 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
980 if (method_id == NULL)
982 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
987 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
990 } /* }}} int jtoc_long */
992 /* Call a `double <method> ()' method. */
993 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
995 jclass class_ptr, jobject object_ptr, const char *method_name)
999 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1000 method_name, "()D");
1001 if (method_id == NULL)
1003 ERROR ("java plugin: jtoc_string: Cannot find method `double %s ()'.",
1008 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1011 } /* }}} int jtoc_double */
1013 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1014 value_t *ret_value, int ds_type, jobject object_ptr)
1019 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1021 if (ds_type == DS_TYPE_COUNTER)
1025 status = jtoc_long (jvm_env, &tmp_long,
1026 class_ptr, object_ptr, "longValue");
1029 ERROR ("java plugin: jtoc_value: "
1030 "jtoc_long failed.");
1033 (*ret_value).counter = (counter_t) tmp_long;
1039 status = jtoc_double (jvm_env, &tmp_double,
1040 class_ptr, object_ptr, "doubleValue");
1043 ERROR ("java plugin: jtoc_value: "
1044 "jtoc_double failed.");
1047 (*ret_value).gauge = (gauge_t) tmp_double;
1051 } /* }}} int jtoc_value */
1053 /* Read a List<Number>, convert it to `value_t' and add it to the given
1054 * `value_list_t'. */
1055 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1056 const data_set_t *ds, value_list_t *vl,
1057 jclass class_ptr, jobject object_ptr)
1059 jmethodID m_getvalues;
1060 jmethodID m_toarray;
1062 jobjectArray o_number_array;
1068 values_num = ds->ds_num;
1071 o_number_array = NULL;
1074 #define BAIL_OUT(status) \
1076 if (o_number_array != NULL) \
1077 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1078 if (o_list != NULL) \
1079 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1082 /* Call: List<Number> ValueList.getValues () */
1083 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1084 "getValues", "()Ljava/util/List;");
1085 if (m_getvalues == NULL)
1087 ERROR ("java plugin: jtoc_values_array: "
1088 "Cannot find method `List getValues ()'.");
1092 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1095 ERROR ("java plugin: jtoc_values_array: "
1096 "CallObjectMethod (getValues) failed.");
1100 /* Call: Number[] List.toArray () */
1101 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1102 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1103 "toArray", "()[Ljava/lang/Object;");
1104 if (m_toarray == NULL)
1106 ERROR ("java plugin: jtoc_values_array: "
1107 "Cannot find method `Object[] toArray ()'.");
1111 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1112 if (o_number_array == NULL)
1114 ERROR ("java plugin: jtoc_values_array: "
1115 "CallObjectMethod (toArray) failed.");
1119 values = (value_t *) calloc (values_num, sizeof (value_t));
1122 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1126 for (i = 0; i < values_num; i++)
1131 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1132 o_number_array, (jsize) i);
1133 if (o_number == NULL)
1135 ERROR ("java plugin: jtoc_values_array: "
1136 "GetObjectArrayElement (%i) failed.", i);
1140 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1143 ERROR ("java plugin: jtoc_values_array: "
1144 "jtoc_value (%i) failed.", i);
1147 } /* for (i = 0; i < values_num; i++) */
1149 vl->values = values;
1150 vl->values_len = values_num;
1153 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1154 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1156 } /* }}} int jtoc_values_array */
1158 /* Convert a org.collectd.api.ValueList to a value_list_t. */
1159 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1165 const data_set_t *ds;
1167 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1168 if (class_ptr == NULL)
1170 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1174 #define SET_STRING(buffer,method) do { \
1175 status = jtoc_string (jvm_env, buffer, sizeof (buffer), \
1176 class_ptr, object_ptr, method); \
1177 if (status != 0) { \
1178 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1183 SET_STRING(vl->type, "getType");
1185 ds = plugin_get_ds (vl->type);
1188 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1189 "Please consult the types.db(5) manpage for mor information.",
1194 SET_STRING(vl->host, "getHost");
1195 SET_STRING(vl->plugin, "getPlugin");
1196 SET_STRING(vl->plugin_instance, "getPluginInstance");
1197 SET_STRING(vl->type_instance, "getTypeInstance");
1201 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1204 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1207 /* Java measures time in milliseconds. */
1208 vl->time = (time_t) (tmp_long / ((jlong) 1000));
1210 status = jtoc_long (jvm_env, &tmp_long,
1211 class_ptr, object_ptr, "getInterval");
1214 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1217 vl->interval = (int) tmp_long;
1219 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1222 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1227 } /* }}} int jtoc_value_list */
1230 * Functions accessible from Java
1232 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1233 jobject this, jobject java_vl)
1235 value_list_t vl = VALUE_LIST_INIT;
1238 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1240 status = jtoc_value_list (jvm_env, &vl, java_vl);
1243 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1247 status = plugin_dispatch_values (&vl);
1252 } /* }}} jint cjni_api_dispatch_values */
1254 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1255 jobject this, jobject o_string_type)
1257 const char *ds_name;
1258 const data_set_t *ds;
1261 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1262 if (ds_name == NULL)
1264 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1268 ds = plugin_get_ds (ds_name);
1269 DEBUG ("java plugin: cjni_api_get_ds: "
1270 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1272 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1277 o_dataset = ctoj_data_set (jvm_env, ds);
1279 } /* }}} jint cjni_api_get_ds */
1281 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1282 jobject this, jobject o_name, jobject o_config)
1284 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1285 } /* }}} jint cjni_api_register_config */
1287 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1288 jobject this, jobject o_name, jobject o_config)
1290 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1291 } /* }}} jint cjni_api_register_init */
1293 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1294 jobject this, jobject o_name, jobject o_read)
1297 cjni_callback_info_t *cbi;
1299 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1303 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1305 memset (&ud, 0, sizeof (ud));
1306 ud.data = (void *) cbi;
1307 ud.free_func = cjni_callback_info_destroy;
1309 plugin_register_complex_read (cbi->name, cjni_read, &ud);
1311 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1314 } /* }}} jint cjni_api_register_read */
1316 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1317 jobject this, jobject o_name, jobject o_write)
1320 cjni_callback_info_t *cbi;
1322 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1326 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1328 memset (&ud, 0, sizeof (ud));
1329 ud.data = (void *) cbi;
1330 ud.free_func = cjni_callback_info_destroy;
1332 plugin_register_write (cbi->name, cjni_write, &ud);
1334 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1337 } /* }}} jint cjni_api_register_write */
1339 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1340 jobject this, jobject o_name, jobject o_flush)
1343 cjni_callback_info_t *cbi;
1345 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1349 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1351 memset (&ud, 0, sizeof (ud));
1352 ud.data = (void *) cbi;
1353 ud.free_func = cjni_callback_info_destroy;
1355 plugin_register_flush (cbi->name, cjni_flush, &ud);
1357 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1360 } /* }}} jint cjni_api_register_flush */
1362 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1363 jobject this, jobject o_name, jobject o_shutdown)
1365 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1367 } /* }}} jint cjni_api_register_shutdown */
1369 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1370 jobject this, jobject o_name, jobject o_log)
1373 cjni_callback_info_t *cbi;
1375 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1379 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1381 memset (&ud, 0, sizeof (ud));
1382 ud.data = (void *) cbi;
1383 ud.free_func = cjni_callback_info_destroy;
1385 plugin_register_log (cbi->name, cjni_log, &ud);
1387 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1390 } /* }}} jint cjni_api_register_log */
1392 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1393 jobject this, jobject o_name, jobject o_notification)
1396 cjni_callback_info_t *cbi;
1398 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1399 CB_TYPE_NOTIFICATION);
1403 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1405 memset (&ud, 0, sizeof (ud));
1406 ud.data = (void *) cbi;
1407 ud.free_func = cjni_callback_info_destroy;
1409 plugin_register_notification (cbi->name, cjni_notification, &ud);
1411 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1414 } /* }}} jint cjni_api_register_notification */
1416 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1417 jobject this, jint severity, jobject o_message)
1421 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1424 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1428 if (severity < LOG_ERR)
1430 if (severity > LOG_DEBUG)
1431 severity = LOG_DEBUG;
1433 plugin_log (severity, "%s", c_str);
1435 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1436 } /* }}} void cjni_api_log */
1438 /* List of ``native'' functions, i. e. C-functions that can be called from
1440 static JNINativeMethod jni_api_functions[] = /* {{{ */
1443 "(Lorg/collectd/api/ValueList;)I",
1444 cjni_api_dispatch_values },
1447 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1451 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1452 cjni_api_register_config },
1455 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1456 cjni_api_register_init },
1459 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1460 cjni_api_register_read },
1463 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1464 cjni_api_register_write },
1467 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1468 cjni_api_register_flush },
1470 { "registerShutdown",
1471 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1472 cjni_api_register_shutdown },
1475 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1476 cjni_api_register_log },
1478 { "registerNotification",
1479 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1480 cjni_api_register_notification },
1483 "(ILjava/lang/String;)V",
1486 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1487 / sizeof (jni_api_functions[0]);
1493 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1494 * all registration functions. */
1495 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1496 jobject o_name, jobject o_callback, int type)
1499 cjni_callback_info_t *cbi;
1500 const char *method_name;
1501 const char *method_signature;
1505 case CB_TYPE_CONFIG:
1506 method_name = "config";
1507 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1511 method_name = "init";
1512 method_signature = "()I";
1516 method_name = "read";
1517 method_signature = "()I";
1521 method_name = "write";
1522 method_signature = "(Lorg/collectd/api/ValueList;)I";
1526 method_name = "flush";
1527 method_signature = "(ILjava/lang/String;)I";
1530 case CB_TYPE_SHUTDOWN:
1531 method_name = "shutdown";
1532 method_signature = "()I";
1536 method_name = "log";
1537 method_signature = "(ILjava/lang/String;)V";
1540 case CB_TYPE_NOTIFICATION:
1541 method_name = "notification";
1542 method_signature = "(LLorg/collectd/api/Notification;)I";
1546 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1551 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1554 ERROR ("java plugin: cjni_callback_info_create: "
1555 "GetStringUTFChars failed.");
1559 cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1562 ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1563 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1566 memset (cbi, 0, sizeof (*cbi));
1569 cbi->name = strdup (c_name);
1570 if (cbi->name == NULL)
1572 pthread_mutex_unlock (&java_callbacks_lock);
1573 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1574 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1578 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1580 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, o_callback);
1581 if (cbi->class == NULL)
1583 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1588 cbi->object = o_callback;
1590 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1591 method_name, method_signature);
1592 if (cbi->method == NULL)
1594 ERROR ("java plugin: cjni_callback_info_create: "
1595 "Cannot find the `%s' method with signature `%s'.",
1596 method_name, method_signature);
1601 (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1604 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1606 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1607 * to the global `java_callbacks' variable. This is used for `config', `init',
1608 * and `shutdown' callbacks. */
1609 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1610 jobject o_name, jobject o_callback, int type)
1612 cjni_callback_info_t *cbi;
1613 cjni_callback_info_t *tmp;
1615 const char *type_str;
1618 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1625 case CB_TYPE_CONFIG:
1626 type_str = "config";
1633 case CB_TYPE_SHUTDOWN:
1634 type_str = "shutdown";
1638 type_str = "<unknown>";
1640 DEBUG ("java plugin: Registering new %s callback: %s",
1641 type_str, cbi->name);
1644 pthread_mutex_lock (&java_callbacks_lock);
1646 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1647 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1650 pthread_mutex_unlock (&java_callbacks_lock);
1651 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1653 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1658 java_callbacks = tmp;
1659 java_callbacks[java_callbacks_num] = *cbi;
1660 java_callbacks_num++;
1662 pthread_mutex_unlock (&java_callbacks_lock);
1666 } /* }}} int cjni_callback_register */
1668 /* Increase the reference counter to the JVM for this thread. If it was zero,
1669 * attach the JVM first. */
1670 static JNIEnv *cjni_thread_attach (void) /* {{{ */
1672 cjni_jvm_env_t *cjni_env;
1675 cjni_env = pthread_getspecific (jvm_env_key);
1676 if (cjni_env == NULL)
1678 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
1679 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
1680 if (cjni_env == NULL)
1682 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
1685 memset (cjni_env, 0, sizeof (*cjni_env));
1686 cjni_env->reference_counter = 0;
1687 cjni_env->jvm_env = NULL;
1689 pthread_setspecific (jvm_env_key, cjni_env);
1692 if (cjni_env->reference_counter > 0)
1694 cjni_env->reference_counter++;
1695 jvm_env = cjni_env->jvm_env;
1700 JavaVMAttachArgs args;
1702 assert (cjni_env->jvm_env == NULL);
1704 memset (&args, 0, sizeof (args));
1705 args.version = JNI_VERSION_1_2;
1707 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
1710 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
1711 "with status %i.", status);
1715 cjni_env->reference_counter = 1;
1716 cjni_env->jvm_env = jvm_env;
1719 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
1720 cjni_env->reference_counter);
1721 assert (jvm_env != NULL);
1723 } /* }}} JNIEnv *cjni_thread_attach */
1725 /* Decrease the reference counter of this thread. If it reaches zero, detach
1727 static int cjni_thread_detach (void) /* {{{ */
1729 cjni_jvm_env_t *cjni_env;
1732 cjni_env = pthread_getspecific (jvm_env_key);
1733 if (cjni_env == NULL)
1735 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
1739 assert (cjni_env->reference_counter > 0);
1740 assert (cjni_env->jvm_env != NULL);
1742 cjni_env->reference_counter--;
1743 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
1744 cjni_env->reference_counter);
1746 if (cjni_env->reference_counter > 0)
1749 status = (*jvm)->DetachCurrentThread (jvm);
1752 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
1753 "with status %i.", status);
1756 cjni_env->reference_counter = 0;
1757 cjni_env->jvm_env = NULL;
1760 } /* }}} JNIEnv *cjni_thread_attach */
1762 /* Callback for `pthread_key_create'. It frees the data contained in
1763 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1764 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1766 cjni_jvm_env_t *cjni_env;
1771 cjni_env = (cjni_jvm_env_t *) args;
1773 if (cjni_env->reference_counter > 0)
1775 ERROR ("java plugin: cjni_jvm_env_destroy: "
1776 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1779 if (cjni_env->jvm_env != NULL)
1781 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1782 (void *) cjni_env->jvm_env);
1785 /* The pointer is allocated in `cjni_thread_attach' */
1787 } /* }}} void cjni_jvm_env_destroy */
1789 /* Boring configuration functions.. {{{ */
1790 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
1794 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1796 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
1800 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
1803 ERROR ("java plugin: realloc failed.");
1808 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
1809 if (jvm_argv[jvm_argc] == NULL)
1811 ERROR ("java plugin: strdup failed.");
1817 } /* }}} int cjni_config_add_jvm_arg */
1819 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
1821 java_plugin_class_t *tmp;
1823 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1825 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
1829 tmp = (java_plugin_class_t *) realloc (java_classes_list,
1830 (java_classes_list_len + 1) * sizeof (*java_classes_list));
1833 ERROR ("java plugin: realloc failed.");
1836 java_classes_list = tmp;
1837 tmp = java_classes_list + java_classes_list_len;
1839 memset (tmp, 0, sizeof (*tmp));
1840 tmp->name = strdup (ci->values[0].value.string);
1841 if (tmp->name == NULL)
1843 ERROR ("java plugin: strdup failed.");
1849 java_classes_list_len++;
1852 } /* }}} int cjni_config_load_plugin */
1854 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
1856 java_plugin_config_t *tmp;
1860 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1862 WARNING ("java plugin: `Plugin' blocks "
1863 "need exactly one string argument.");
1867 name = strdup (ci->values[0].value.string);
1870 ERROR ("java plugin: cjni_config_plugin_block: strdup faiiled.");
1874 for (i = 0; i < java_plugin_configs_num; i++)
1876 if (strcmp (java_plugin_configs[i].name, name) == 0)
1878 WARNING ("java plugin: There is more than one <Plugin \"%s\"> block. "
1879 "This is currently not supported - "
1880 "only the first block will be used!",
1887 tmp = (java_plugin_config_t *) realloc (java_plugin_configs,
1888 (java_plugin_configs_num + 1) * sizeof (*java_plugin_configs));
1891 ERROR ("java plugin: cjni_config_plugin_block: realloc failed.");
1895 java_plugin_configs = tmp;
1896 tmp = java_plugin_configs + java_plugin_configs_num;
1899 tmp->ci = oconfig_clone (ci);
1900 if (tmp->ci == NULL)
1902 ERROR ("java plugin: cjni_config_plugin_block: "
1903 "oconfig_clone failed for `%s'.",
1909 DEBUG ("java plugin: cjni_config_plugin_block: "
1910 "Successfully copied config for `%s'.",
1913 java_plugin_configs_num++;
1915 } /* }}} int cjni_config_plugin_block */
1917 static int cjni_config (oconfig_item_t *ci) /* {{{ */
1927 for (i = 0; i < ci->children_num; i++)
1929 oconfig_item_t *child = ci->children + i;
1931 if (strcasecmp ("JVMArg", child->key) == 0)
1933 status = cjni_config_add_jvm_arg (child);
1939 else if (strcasecmp ("LoadPlugin", child->key) == 0)
1941 status = cjni_config_load_plugin (child);
1947 else if (strcasecmp ("Plugin", child->key) == 0)
1949 status = cjni_config_plugin_block (child);
1957 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
1962 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
1963 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
1964 DEBUG ("java plugin: java_plugin_configs_num = %zu;",
1965 java_plugin_configs_num);
1967 if ((success == 0) && (errors > 0))
1969 ERROR ("java plugin: All statements failed.");
1974 } /* }}} int cjni_config */
1977 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
1978 * and `cjni_write'. In particular, delete the global reference to the Java
1980 static void cjni_callback_info_destroy (void *arg) /* {{{ */
1983 cjni_callback_info_t *cbi;
1985 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
1987 cbi = (cjni_callback_info_t *) arg;
1989 /* This condition can occurr when shutting down. */
1999 jvm_env = cjni_thread_attach ();
2000 if (jvm_env == NULL)
2002 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2006 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2013 cjni_thread_detach ();
2014 } /* }}} void cjni_callback_info_destroy */
2016 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2017 static int cjni_read (user_data_t *ud) /* {{{ */
2020 cjni_callback_info_t *cbi;
2026 ERROR ("java plugin: cjni_read: jvm == NULL");
2030 if ((ud == NULL) || (ud->data == NULL))
2032 ERROR ("java plugin: cjni_read: Invalid user data.");
2036 jvm_env = cjni_thread_attach ();
2037 if (jvm_env == NULL)
2040 cbi = (cjni_callback_info_t *) ud->data;
2042 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2045 status = cjni_thread_detach ();
2048 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2052 return (ret_status);
2053 } /* }}} int cjni_read */
2055 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2056 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2060 cjni_callback_info_t *cbi;
2067 ERROR ("java plugin: cjni_write: jvm == NULL");
2071 if ((ud == NULL) || (ud->data == NULL))
2073 ERROR ("java plugin: cjni_write: Invalid user data.");
2077 jvm_env = cjni_thread_attach ();
2078 if (jvm_env == NULL)
2081 cbi = (cjni_callback_info_t *) ud->data;
2083 vl_java = ctoj_value_list (jvm_env, ds, vl);
2084 if (vl_java == NULL)
2086 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2090 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2091 cbi->object, cbi->method, vl_java);
2093 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2095 status = cjni_thread_detach ();
2098 ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2102 return (ret_status);
2103 } /* }}} int cjni_write */
2105 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2106 static int cjni_flush (int timeout, const char *identifier, /* {{{ */
2110 cjni_callback_info_t *cbi;
2111 jobject o_identifier;
2117 ERROR ("java plugin: cjni_flush: jvm == NULL");
2121 if ((ud == NULL) || (ud->data == NULL))
2123 ERROR ("java plugin: cjni_flush: Invalid user data.");
2127 jvm_env = cjni_thread_attach ();
2128 if (jvm_env == NULL)
2131 cbi = (cjni_callback_info_t *) ud->data;
2133 o_identifier = NULL;
2134 if (identifier != NULL)
2136 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2137 if (o_identifier == NULL)
2139 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2144 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2145 cbi->object, cbi->method, (jint) timeout, o_identifier);
2147 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2149 status = cjni_thread_detach ();
2152 ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2156 return (ret_status);
2157 } /* }}} int cjni_flush */
2159 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2160 static void cjni_log (int severity, const char *message, /* {{{ */
2164 cjni_callback_info_t *cbi;
2170 if ((ud == NULL) || (ud->data == NULL))
2173 jvm_env = cjni_thread_attach ();
2174 if (jvm_env == NULL)
2177 cbi = (cjni_callback_info_t *) ud->data;
2179 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2180 if (o_message == NULL)
2183 (*jvm_env)->CallVoidMethod (jvm_env,
2184 cbi->object, cbi->method, (jint) severity, o_message);
2186 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2188 cjni_thread_detach ();
2189 } /* }}} void cjni_log */
2191 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2193 static int cjni_notification (const notification_t *n, /* {{{ */
2197 cjni_callback_info_t *cbi;
2198 jobject o_notification;
2204 ERROR ("java plugin: cjni_read: jvm == NULL");
2208 if ((ud == NULL) || (ud->data == NULL))
2210 ERROR ("java plugin: cjni_read: Invalid user data.");
2214 jvm_env = cjni_thread_attach ();
2215 if (jvm_env == NULL)
2218 cbi = (cjni_callback_info_t *) ud->data;
2220 o_notification = ctoj_notification (jvm_env, n);
2221 if (o_notification == NULL)
2223 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2227 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2228 cbi->object, cbi->method, o_notification);
2230 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2232 status = cjni_thread_detach ();
2235 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2239 return (ret_status);
2240 } /* }}} int cjni_notification */
2242 /* Iterate over `java_classes_list' and create one object of each class. This
2243 * will trigger the object's constructors, to the objects can register callback
2245 static int cjni_load_plugins (JNIEnv *jvm_env) /* {{{ */
2249 for (i = 0; i < java_classes_list_len; i++)
2251 java_plugin_class_t *class;
2252 jmethodID constructor_id;
2254 class = java_classes_list + i;
2256 DEBUG ("java plugin: Loading class %s", class->name);
2258 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2259 if (class->class == NULL)
2261 ERROR ("java plugin: cjni_load_plugins: FindClass (%s) failed.",
2266 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2268 if (constructor_id == NULL)
2270 ERROR ("java plugin: cjni_load_plugins: Could not find the constructor for `%s'.",
2275 class->object = (*jvm_env)->NewObject (jvm_env, class->class,
2277 if (class->object == NULL)
2279 ERROR ("java plugin: cjni_load_plugins: Could create a new `%s' object.",
2284 (*jvm_env)->NewGlobalRef (jvm_env, class->object);
2285 } /* for (i = 0; i < java_classes_list_len; i++) */
2288 } /* }}} int cjni_load_plugins */
2290 /* Iterate over `java_plugin_configs' and `java_callbacks' and call all
2291 * `config' callback methods for which a configuration is available. */
2292 static int cjni_config_plugins (JNIEnv *jvm_env) /* {{{ */
2298 for (i = 0; i < java_plugin_configs_num; i++)
2302 if (java_plugin_configs[i].ci == NULL)
2305 for (j = 0; j < java_callbacks_num; j++)
2307 if (java_callbacks[j].type != CB_TYPE_CONFIG)
2310 if (strcmp (java_plugin_configs[i].name, java_callbacks[j].name) == 0)
2314 if (j >= java_callbacks_num)
2316 NOTICE ("java plugin: Configuration for `%s' is present, but no such "
2317 "configuration callback has been registered.",
2318 java_plugin_configs[i].name);
2322 DEBUG ("java plugin: Configuring %s", java_plugin_configs[i].name);
2324 o_ocitem = ctoj_oconfig_item (jvm_env, java_plugin_configs[i].ci);
2325 if (o_ocitem == NULL)
2327 ERROR ("java plugin: cjni_config_plugins: ctoj_oconfig_item failed.");
2331 status = (*jvm_env)->CallIntMethod (jvm_env,
2332 java_callbacks[j].object, java_callbacks[j].method, o_ocitem);
2333 WARNING ("java plugin: Config callback for `%s' returned status %i.",
2334 java_plugin_configs[i].name, status);
2335 } /* for (i = 0; i < java_plugin_configs; i++) */
2338 } /* }}} int cjni_config_plugins */
2340 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2341 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2346 for (i = 0; i < java_callbacks_num; i++)
2348 if (java_callbacks[i].type != CB_TYPE_INIT)
2351 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2353 status = (*jvm_env)->CallIntMethod (jvm_env,
2354 java_callbacks[i].object, java_callbacks[i].method);
2357 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2358 "Removing read function.",
2359 java_callbacks[i].name, status);
2360 plugin_unregister_read (java_callbacks[i].name);
2365 } /* }}} int cjni_init_plugins */
2367 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2368 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2373 for (i = 0; i < java_callbacks_num; i++)
2375 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2378 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2380 status = (*jvm_env)->CallIntMethod (jvm_env,
2381 java_callbacks[i].object, java_callbacks[i].method);
2384 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2385 java_callbacks[i].name, status);
2390 } /* }}} int cjni_shutdown_plugins */
2393 static int cjni_shutdown (void) /* {{{ */
2396 JavaVMAttachArgs args;
2404 memset (&args, 0, sizeof (args));
2405 args.version = JNI_VERSION_1_2;
2407 status = (*jvm)->AttachCurrentThread (jvm, (void **) &jvm_env, &args);
2410 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2415 /* Execute all the shutdown functions registered by plugins. */
2416 cjni_shutdown_plugins (jvm_env);
2418 /* Release all the global references to callback functions */
2419 for (i = 0; i < java_callbacks_num; i++)
2421 if (java_callbacks[i].object != NULL)
2423 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
2424 java_callbacks[i].object = NULL;
2426 sfree (java_callbacks[i].name);
2428 java_callbacks_num = 0;
2429 sfree (java_callbacks);
2431 /* Release all the global references to directly loaded classes. */
2432 for (i = 0; i < java_classes_list_len; i++)
2434 if (java_classes_list[i].object != NULL)
2436 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
2437 java_classes_list[i].object = NULL;
2439 sfree (java_classes_list[i].name);
2441 java_classes_list_len = 0;
2442 sfree (java_classes_list);
2444 /* Destroy the JVM */
2445 DEBUG ("java plugin: Destroying the JVM.");
2446 (*jvm)->DestroyJavaVM (jvm);
2450 pthread_key_delete (jvm_env_key);
2452 /* Free the JVM argument list */
2453 for (i = 0; i < jvm_argc; i++)
2454 sfree (jvm_argv[i]);
2458 /* Free the copied configuration */
2459 for (i = 0; i < java_plugin_configs_num; i++)
2461 sfree (java_plugin_configs[i].name);
2462 oconfig_free (java_plugin_configs[i].ci);
2464 java_plugin_configs_num = 0;
2465 sfree (java_plugin_configs);
2468 } /* }}} int cjni_shutdown */
2470 /* Register ``native'' functions with the JVM. Native functions are C-functions
2471 * that can be called by Java code. */
2472 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
2474 jclass api_class_ptr;
2477 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.Collectd");
2478 if (api_class_ptr == NULL)
2480 ERROR ("cjni_init_native: Cannot find API class `org.collectd.api.Collectd'.");
2484 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
2485 jni_api_functions, (jint) jni_api_functions_num);
2488 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
2493 } /* }}} int cjni_init_native */
2495 /* Initialization: Create a JVM, load all configured classes and call their
2496 * `config' and `init' callback methods. */
2497 static int cjni_init (void) /* {{{ */
2500 JavaVMInitArgs vm_args;
2501 JavaVMOption vm_options[jvm_argc];
2509 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
2512 ERROR ("java plugin: cjni_init: pthread_key_create failed "
2513 "with status %i.", status);
2519 memset (&vm_args, 0, sizeof (vm_args));
2520 vm_args.version = JNI_VERSION_1_2;
2521 vm_args.options = vm_options;
2522 vm_args.nOptions = (jint) jvm_argc;
2524 for (i = 0; i < jvm_argc; i++)
2526 DEBUG ("java plugin: cjni_init: jvm_argv[%zu] = %s", i, jvm_argv[i]);
2527 vm_args.options[i].optionString = jvm_argv[i];
2530 vm_args.options[0].optionString = "-verbose:jni";
2531 vm_args.options[1].optionString = "-Djava.class.path=/home/octo/collectd/bindings/java";
2534 status = JNI_CreateJavaVM (&jvm, (void **) &jvm_env, (void **) &vm_args);
2537 ERROR ("cjni_init: JNI_CreateJavaVM failed with status %i.",
2541 assert (jvm != NULL);
2542 assert (jvm_env != NULL);
2544 /* Call RegisterNatives */
2545 status = cjni_init_native (jvm_env);
2548 ERROR ("cjni_init: cjni_init_native failed.");
2552 cjni_load_plugins (jvm_env);
2553 cjni_config_plugins (jvm_env);
2554 cjni_init_plugins (jvm_env);
2557 } /* }}} int cjni_init */
2559 void module_register (void)
2561 plugin_register_complex_config ("java", cjni_config);
2562 plugin_register_init ("java", cjni_init);
2563 plugin_register_shutdown ("java", cjni_shutdown);
2564 } /* void module_register (void) */
2566 /* vim: set sw=2 sts=2 et fdm=marker : */