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>
27 #include "filter_chain.h"
32 #if !defined(JNI_VERSION_1_2)
33 # error "Need JNI 1.2 compatible interface!"
39 struct cjni_jvm_env_s /* {{{ */
42 int reference_counter;
44 typedef struct cjni_jvm_env_s cjni_jvm_env_t;
47 struct java_plugin_class_s /* {{{ */
53 typedef struct java_plugin_class_s java_plugin_class_t;
56 #define CB_TYPE_CONFIG 1
57 #define CB_TYPE_INIT 2
58 #define CB_TYPE_READ 3
59 #define CB_TYPE_WRITE 4
60 #define CB_TYPE_FLUSH 5
61 #define CB_TYPE_SHUTDOWN 6
63 #define CB_TYPE_NOTIFICATION 8
64 #define CB_TYPE_MATCH 9
65 #define CB_TYPE_TARGET 10
66 struct cjni_callback_info_s /* {{{ */
74 typedef struct cjni_callback_info_s cjni_callback_info_t;
80 static JavaVM *jvm = NULL;
81 static pthread_key_t jvm_env_key;
83 /* Configuration options for the JVM. */
84 static char **jvm_argv = NULL;
85 static size_t jvm_argc = 0;
87 /* List of class names to load */
88 static java_plugin_class_t *java_classes_list = NULL;
89 static size_t java_classes_list_len;
91 /* List of config, init, and shutdown callbacks. */
92 static cjni_callback_info_t *java_callbacks = NULL;
93 static size_t java_callbacks_num = 0;
94 static pthread_mutex_t java_callbacks_lock = PTHREAD_MUTEX_INITIALIZER;
96 static oconfig_item_t *config_block = NULL;
101 * Mostly functions that are needed by the Java interface (``native'')
104 static void cjni_callback_info_destroy (void *arg);
105 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env,
106 jobject o_name, jobject o_callback, int type);
107 static int cjni_callback_register (JNIEnv *jvm_env, jobject o_name,
108 jobject o_callback, int type);
109 static int cjni_read (user_data_t *user_data);
110 static int cjni_write (const data_set_t *ds, const value_list_t *vl,
112 static int cjni_flush (int timeout, const char *identifier, user_data_t *ud);
113 static void cjni_log (int severity, const char *message, user_data_t *ud);
114 static int cjni_notification (const notification_t *n, user_data_t *ud);
116 /* Create, destroy, and match/invoke functions, used by both, matches AND
118 static int cjni_match_target_create (const oconfig_item_t *ci, void **user_data);
119 static int cjni_match_target_destroy (void **user_data);
120 static int cjni_match_target_invoke (const data_set_t *ds, value_list_t *vl,
121 notification_meta_t **meta, void **user_data);
124 * C to Java conversion functions
126 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
128 jclass class_ptr, jobject object_ptr, const char *method_name)
133 /* Create a java.lang.String */
134 o_string = (*jvm_env)->NewStringUTF (jvm_env,
135 (string != NULL) ? string : "");
136 if (o_string == NULL)
138 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
142 /* Search for the `void setFoo (String s)' method. */
143 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
144 method_name, "(Ljava/lang/String;)V");
147 ERROR ("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
149 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
153 /* Call the method. */
154 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, o_string);
156 /* Decrease reference counter on the java.lang.String object. */
157 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
160 } /* }}} int ctoj_string */
162 static 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_addchild, 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: ctoj_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: ctoj_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, int empty_okay,
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) && (empty_okay == 0))
950 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
954 else if ((string_obj == NULL) && (empty_okay != 0))
956 memset (buffer, 0, buffer_size);
960 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
963 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
964 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
968 sstrncpy (buffer, c_str, buffer_size);
970 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
971 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
974 } /* }}} int jtoc_string */
976 /* Call an `int <method> ()' method. */
977 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
979 jclass class_ptr, jobject object_ptr, const char *method_name)
983 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
985 if (method_id == NULL)
987 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
992 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
995 } /* }}} int jtoc_int */
997 /* Call a `long <method> ()' method. */
998 static int jtoc_long (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, "()J");
1006 if (method_id == NULL)
1008 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1013 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1016 } /* }}} int jtoc_long */
1018 /* Call a `double <method> ()' method. */
1019 static int jtoc_double (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, "()D");
1027 if (method_id == NULL)
1029 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1034 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1037 } /* }}} int jtoc_double */
1039 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1040 value_t *ret_value, int ds_type, jobject object_ptr)
1045 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1047 if (ds_type == DS_TYPE_COUNTER)
1051 status = jtoc_long (jvm_env, &tmp_long,
1052 class_ptr, object_ptr, "longValue");
1055 ERROR ("java plugin: jtoc_value: "
1056 "jtoc_long failed.");
1059 (*ret_value).counter = (counter_t) tmp_long;
1065 status = jtoc_double (jvm_env, &tmp_double,
1066 class_ptr, object_ptr, "doubleValue");
1069 ERROR ("java plugin: jtoc_value: "
1070 "jtoc_double failed.");
1073 (*ret_value).gauge = (gauge_t) tmp_double;
1077 } /* }}} int jtoc_value */
1079 /* Read a List<Number>, convert it to `value_t' and add it to the given
1080 * `value_list_t'. */
1081 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1082 const data_set_t *ds, value_list_t *vl,
1083 jclass class_ptr, jobject object_ptr)
1085 jmethodID m_getvalues;
1086 jmethodID m_toarray;
1088 jobjectArray o_number_array;
1094 values_num = ds->ds_num;
1097 o_number_array = NULL;
1100 #define BAIL_OUT(status) \
1102 if (o_number_array != NULL) \
1103 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1104 if (o_list != NULL) \
1105 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1108 /* Call: List<Number> ValueList.getValues () */
1109 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1110 "getValues", "()Ljava/util/List;");
1111 if (m_getvalues == NULL)
1113 ERROR ("java plugin: jtoc_values_array: "
1114 "Cannot find method `List getValues ()'.");
1118 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1121 ERROR ("java plugin: jtoc_values_array: "
1122 "CallObjectMethod (getValues) failed.");
1126 /* Call: Number[] List.toArray () */
1127 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1128 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1129 "toArray", "()[Ljava/lang/Object;");
1130 if (m_toarray == NULL)
1132 ERROR ("java plugin: jtoc_values_array: "
1133 "Cannot find method `Object[] toArray ()'.");
1137 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1138 if (o_number_array == NULL)
1140 ERROR ("java plugin: jtoc_values_array: "
1141 "CallObjectMethod (toArray) failed.");
1145 values = (value_t *) calloc (values_num, sizeof (value_t));
1148 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1152 for (i = 0; i < values_num; i++)
1157 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1158 o_number_array, (jsize) i);
1159 if (o_number == NULL)
1161 ERROR ("java plugin: jtoc_values_array: "
1162 "GetObjectArrayElement (%i) failed.", i);
1166 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1169 ERROR ("java plugin: jtoc_values_array: "
1170 "jtoc_value (%i) failed.", i);
1173 } /* for (i = 0; i < values_num; i++) */
1175 vl->values = values;
1176 vl->values_len = values_num;
1179 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1180 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1182 } /* }}} int jtoc_values_array */
1184 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1185 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1191 const data_set_t *ds;
1193 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1194 if (class_ptr == NULL)
1196 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1200 /* eo == empty okay */
1201 #define SET_STRING(buffer,method, eo) do { \
1202 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1203 class_ptr, object_ptr, method); \
1204 if (status != 0) { \
1205 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1210 SET_STRING(vl->type, "getType", /* empty = */ 0);
1212 ds = plugin_get_ds (vl->type);
1215 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1216 "Please consult the types.db(5) manpage for mor information.",
1221 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1222 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1223 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1224 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1228 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1231 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1234 /* Java measures time in milliseconds. */
1235 vl->time = (time_t) (tmp_long / ((jlong) 1000));
1237 status = jtoc_long (jvm_env, &tmp_long,
1238 class_ptr, object_ptr, "getInterval");
1241 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1244 vl->interval = (int) tmp_long;
1246 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1249 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1254 } /* }}} int jtoc_value_list */
1256 /* Convert a org/collectd/api/Notification to a notification_t. */
1257 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1265 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1266 if (class_ptr == NULL)
1268 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1272 /* eo == empty okay */
1273 #define SET_STRING(buffer,method, eo) do { \
1274 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1275 class_ptr, object_ptr, method); \
1276 if (status != 0) { \
1277 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1282 SET_STRING (n->host, "getHost", /* empty = */ 1);
1283 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1284 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1285 SET_STRING (n->type, "getType", /* empty = */ 1);
1286 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1287 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1291 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1294 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1297 /* Java measures time in milliseconds. */
1298 n->time = (time_t) (tmp_long / ((jlong) 1000));
1300 status = jtoc_int (jvm_env, &tmp_int,
1301 class_ptr, object_ptr, "getSeverity");
1304 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1307 n->severity = (int) tmp_int;
1310 } /* }}} int jtoc_notification */
1312 * Functions accessible from Java
1314 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1315 jobject this, jobject java_vl)
1317 value_list_t vl = VALUE_LIST_INIT;
1320 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1322 status = jtoc_value_list (jvm_env, &vl, java_vl);
1325 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1329 status = plugin_dispatch_values (&vl);
1334 } /* }}} jint cjni_api_dispatch_values */
1336 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1337 jobject this, jobject o_notification)
1342 memset (&n, 0, sizeof (n));
1345 status = jtoc_notification (jvm_env, &n, o_notification);
1348 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1352 status = plugin_dispatch_notification (&n);
1355 } /* }}} jint cjni_api_dispatch_notification */
1357 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1358 jobject this, jobject o_string_type)
1360 const char *ds_name;
1361 const data_set_t *ds;
1364 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1365 if (ds_name == NULL)
1367 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1371 ds = plugin_get_ds (ds_name);
1372 DEBUG ("java plugin: cjni_api_get_ds: "
1373 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1375 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1380 o_dataset = ctoj_data_set (jvm_env, ds);
1382 } /* }}} jint cjni_api_get_ds */
1384 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1385 jobject this, jobject o_name, jobject o_config)
1387 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1388 } /* }}} jint cjni_api_register_config */
1390 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1391 jobject this, jobject o_name, jobject o_config)
1393 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1394 } /* }}} jint cjni_api_register_init */
1396 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1397 jobject this, jobject o_name, jobject o_read)
1400 cjni_callback_info_t *cbi;
1402 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1406 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1408 memset (&ud, 0, sizeof (ud));
1409 ud.data = (void *) cbi;
1410 ud.free_func = cjni_callback_info_destroy;
1412 plugin_register_complex_read (cbi->name, cjni_read,
1413 /* interval = */ NULL, &ud);
1415 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1418 } /* }}} jint cjni_api_register_read */
1420 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1421 jobject this, jobject o_name, jobject o_write)
1424 cjni_callback_info_t *cbi;
1426 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1430 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1432 memset (&ud, 0, sizeof (ud));
1433 ud.data = (void *) cbi;
1434 ud.free_func = cjni_callback_info_destroy;
1436 plugin_register_write (cbi->name, cjni_write, &ud);
1438 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1441 } /* }}} jint cjni_api_register_write */
1443 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1444 jobject this, jobject o_name, jobject o_flush)
1447 cjni_callback_info_t *cbi;
1449 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1453 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1455 memset (&ud, 0, sizeof (ud));
1456 ud.data = (void *) cbi;
1457 ud.free_func = cjni_callback_info_destroy;
1459 plugin_register_flush (cbi->name, cjni_flush, &ud);
1461 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1464 } /* }}} jint cjni_api_register_flush */
1466 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1467 jobject this, jobject o_name, jobject o_shutdown)
1469 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1471 } /* }}} jint cjni_api_register_shutdown */
1473 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1474 jobject this, jobject o_name, jobject o_log)
1477 cjni_callback_info_t *cbi;
1479 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1483 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1485 memset (&ud, 0, sizeof (ud));
1486 ud.data = (void *) cbi;
1487 ud.free_func = cjni_callback_info_destroy;
1489 plugin_register_log (cbi->name, cjni_log, &ud);
1491 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1494 } /* }}} jint cjni_api_register_log */
1496 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1497 jobject this, jobject o_name, jobject o_notification)
1500 cjni_callback_info_t *cbi;
1502 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1503 CB_TYPE_NOTIFICATION);
1507 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1509 memset (&ud, 0, sizeof (ud));
1510 ud.data = (void *) cbi;
1511 ud.free_func = cjni_callback_info_destroy;
1513 plugin_register_notification (cbi->name, cjni_notification, &ud);
1515 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1518 } /* }}} jint cjni_api_register_notification */
1520 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1521 jobject this, jobject o_name, jobject o_match, int type)
1526 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1529 ERROR ("java plugin: cjni_api_register_match_target: "
1530 "GetStringUTFChars failed.");
1534 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1537 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1541 if (type == CB_TYPE_MATCH)
1543 match_proc_t m_proc;
1545 memset (&m_proc, 0, sizeof (m_proc));
1546 m_proc.create = cjni_match_target_create;
1547 m_proc.destroy = cjni_match_target_destroy;
1548 m_proc.match = (void *) cjni_match_target_invoke;
1550 status = fc_register_match (c_name, m_proc);
1552 else if (type == CB_TYPE_TARGET)
1554 target_proc_t t_proc;
1556 memset (&t_proc, 0, sizeof (t_proc));
1557 t_proc.create = cjni_match_target_create;
1558 t_proc.destroy = cjni_match_target_destroy;
1559 t_proc.invoke = cjni_match_target_invoke;
1561 status = fc_register_target (c_name, t_proc);
1565 ERROR ("java plugin: cjni_api_register_match_target: "
1566 "Don't know whether to create a match or a target.");
1567 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1573 ERROR ("java plugin: cjni_api_register_match_target: "
1575 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1576 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1580 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1583 } /* }}} jint cjni_api_register_match_target */
1585 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1586 jobject this, jobject o_name, jobject o_match)
1588 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1590 } /* }}} jint cjni_api_register_match */
1592 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1593 jobject this, jobject o_name, jobject o_target)
1595 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1597 } /* }}} jint cjni_api_register_target */
1599 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1600 jobject this, jint severity, jobject o_message)
1604 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1607 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1611 if (severity < LOG_ERR)
1613 if (severity > LOG_DEBUG)
1614 severity = LOG_DEBUG;
1616 plugin_log (severity, "%s", c_str);
1618 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1619 } /* }}} void cjni_api_log */
1621 /* List of ``native'' functions, i. e. C-functions that can be called from
1623 static JNINativeMethod jni_api_functions[] = /* {{{ */
1626 "(Lorg/collectd/api/ValueList;)I",
1627 cjni_api_dispatch_values },
1629 { "dispatchNotification",
1630 "(Lorg/collectd/api/Notification;)I",
1631 cjni_api_dispatch_notification },
1634 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1638 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1639 cjni_api_register_config },
1642 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1643 cjni_api_register_init },
1646 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1647 cjni_api_register_read },
1650 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1651 cjni_api_register_write },
1654 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1655 cjni_api_register_flush },
1657 { "registerShutdown",
1658 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1659 cjni_api_register_shutdown },
1662 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1663 cjni_api_register_log },
1665 { "registerNotification",
1666 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1667 cjni_api_register_notification },
1670 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1671 cjni_api_register_match },
1674 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1675 cjni_api_register_target },
1678 "(ILjava/lang/String;)V",
1681 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1682 / sizeof (jni_api_functions[0]);
1688 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1689 * all registration functions. */
1690 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1691 jobject o_name, jobject o_callback, int type)
1694 cjni_callback_info_t *cbi;
1695 const char *method_name;
1696 const char *method_signature;
1700 case CB_TYPE_CONFIG:
1701 method_name = "config";
1702 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1706 method_name = "init";
1707 method_signature = "()I";
1711 method_name = "read";
1712 method_signature = "()I";
1716 method_name = "write";
1717 method_signature = "(Lorg/collectd/api/ValueList;)I";
1721 method_name = "flush";
1722 method_signature = "(ILjava/lang/String;)I";
1725 case CB_TYPE_SHUTDOWN:
1726 method_name = "shutdown";
1727 method_signature = "()I";
1731 method_name = "log";
1732 method_signature = "(ILjava/lang/String;)V";
1735 case CB_TYPE_NOTIFICATION:
1736 method_name = "notification";
1737 method_signature = "(Lorg/collectd/api/Notification;)I";
1741 method_name = "createMatch";
1742 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1743 "Lorg/collectd/api/CollectdMatchInterface;";
1746 case CB_TYPE_TARGET:
1747 method_name = "createTarget";
1748 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1749 "Lorg/collectd/api/CollectdTargetInterface;";
1753 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1758 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1761 ERROR ("java plugin: cjni_callback_info_create: "
1762 "GetStringUTFChars failed.");
1766 cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1769 ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1770 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1773 memset (cbi, 0, sizeof (*cbi));
1776 cbi->name = strdup (c_name);
1777 if (cbi->name == NULL)
1779 pthread_mutex_unlock (&java_callbacks_lock);
1780 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1781 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1785 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1787 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1788 if (cbi->object == NULL)
1790 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1795 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1796 if (cbi->class == NULL)
1798 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1803 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1804 method_name, method_signature);
1805 if (cbi->method == NULL)
1807 ERROR ("java plugin: cjni_callback_info_create: "
1808 "Cannot find the `%s' method with signature `%s'.",
1809 method_name, method_signature);
1815 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1817 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1818 * to the global `java_callbacks' variable. This is used for `config', `init',
1819 * and `shutdown' callbacks. */
1820 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1821 jobject o_name, jobject o_callback, int type)
1823 cjni_callback_info_t *cbi;
1824 cjni_callback_info_t *tmp;
1826 const char *type_str;
1829 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1836 case CB_TYPE_CONFIG:
1837 type_str = "config";
1844 case CB_TYPE_SHUTDOWN:
1845 type_str = "shutdown";
1852 case CB_TYPE_TARGET:
1853 type_str = "target";
1857 type_str = "<unknown>";
1859 DEBUG ("java plugin: Registering new %s callback: %s",
1860 type_str, cbi->name);
1863 pthread_mutex_lock (&java_callbacks_lock);
1865 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1866 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1869 pthread_mutex_unlock (&java_callbacks_lock);
1870 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1872 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1877 java_callbacks = tmp;
1878 java_callbacks[java_callbacks_num] = *cbi;
1879 java_callbacks_num++;
1881 pthread_mutex_unlock (&java_callbacks_lock);
1885 } /* }}} int cjni_callback_register */
1887 /* Callback for `pthread_key_create'. It frees the data contained in
1888 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1889 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1891 cjni_jvm_env_t *cjni_env;
1896 cjni_env = (cjni_jvm_env_t *) args;
1898 if (cjni_env->reference_counter > 0)
1900 ERROR ("java plugin: cjni_jvm_env_destroy: "
1901 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1904 if (cjni_env->jvm_env != NULL)
1906 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1907 (void *) cjni_env->jvm_env);
1910 /* The pointer is allocated in `cjni_thread_attach' */
1912 } /* }}} void cjni_jvm_env_destroy */
1914 /* Register ``native'' functions with the JVM. Native functions are C-functions
1915 * that can be called by Java code. */
1916 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1918 jclass api_class_ptr;
1921 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1922 if (api_class_ptr == NULL)
1924 ERROR ("cjni_init_native: Cannot find API class `org/collectd/api/Collectd'.");
1928 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1929 jni_api_functions, (jint) jni_api_functions_num);
1932 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1937 } /* }}} int cjni_init_native */
1939 /* Create the JVM. This is called when the first thread tries to access the JVM
1940 * via cjni_thread_attach. */
1941 static int cjni_create_jvm (void) /* {{{ */
1944 JavaVMInitArgs vm_args;
1945 JavaVMOption vm_options[jvm_argc];
1953 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1956 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1957 "with status %i.", status);
1963 memset (&vm_args, 0, sizeof (vm_args));
1964 vm_args.version = JNI_VERSION_1_2;
1965 vm_args.options = vm_options;
1966 vm_args.nOptions = (jint) jvm_argc;
1968 for (i = 0; i < jvm_argc; i++)
1970 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
1972 vm_args.options[i].optionString = jvm_argv[i];
1975 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
1978 ERROR ("java plugin: cjni_create_jvm: "
1979 "JNI_CreateJavaVM failed with status %i.",
1983 assert (jvm != NULL);
1984 assert (jvm_env != NULL);
1986 /* Call RegisterNatives */
1987 status = cjni_init_native (jvm_env);
1990 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
1994 DEBUG ("java plugin: The JVM has been created.");
1996 } /* }}} int cjni_create_jvm */
1998 /* Increase the reference counter to the JVM for this thread. If it was zero,
1999 * attach the JVM first. */
2000 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2002 cjni_jvm_env_t *cjni_env;
2005 /* If we're the first thread to access the JVM, we'll have to create it
2011 status = cjni_create_jvm ();
2014 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2018 assert (jvm != NULL);
2020 cjni_env = pthread_getspecific (jvm_env_key);
2021 if (cjni_env == NULL)
2023 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2024 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
2025 if (cjni_env == NULL)
2027 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
2030 memset (cjni_env, 0, sizeof (*cjni_env));
2031 cjni_env->reference_counter = 0;
2032 cjni_env->jvm_env = NULL;
2034 pthread_setspecific (jvm_env_key, cjni_env);
2037 if (cjni_env->reference_counter > 0)
2039 cjni_env->reference_counter++;
2040 jvm_env = cjni_env->jvm_env;
2045 JavaVMAttachArgs args;
2047 assert (cjni_env->jvm_env == NULL);
2049 memset (&args, 0, sizeof (args));
2050 args.version = JNI_VERSION_1_2;
2052 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2055 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2056 "with status %i.", status);
2060 cjni_env->reference_counter = 1;
2061 cjni_env->jvm_env = jvm_env;
2064 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2065 cjni_env->reference_counter);
2066 assert (jvm_env != NULL);
2068 } /* }}} JNIEnv *cjni_thread_attach */
2070 /* Decrease the reference counter of this thread. If it reaches zero, detach
2072 static int cjni_thread_detach (void) /* {{{ */
2074 cjni_jvm_env_t *cjni_env;
2077 cjni_env = pthread_getspecific (jvm_env_key);
2078 if (cjni_env == NULL)
2080 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2084 assert (cjni_env->reference_counter > 0);
2085 assert (cjni_env->jvm_env != NULL);
2087 cjni_env->reference_counter--;
2088 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2089 cjni_env->reference_counter);
2091 if (cjni_env->reference_counter > 0)
2094 status = (*jvm)->DetachCurrentThread (jvm);
2097 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2098 "with status %i.", status);
2101 cjni_env->reference_counter = 0;
2102 cjni_env->jvm_env = NULL;
2105 } /* }}} JNIEnv *cjni_thread_attach */
2107 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2111 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2113 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2119 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2120 "`LoadPlugin' options! The JVM is already started and I have to "
2121 "ignore this argument: %s",
2122 ci->values[0].value.string);
2126 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2129 ERROR ("java plugin: realloc failed.");
2134 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2135 if (jvm_argv[jvm_argc] == NULL)
2137 ERROR ("java plugin: strdup failed.");
2143 } /* }}} int cjni_config_add_jvm_arg */
2145 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2148 java_plugin_class_t *class;
2149 jmethodID constructor_id;
2152 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2154 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2158 jvm_env = cjni_thread_attach ();
2159 if (jvm_env == NULL)
2162 class = (java_plugin_class_t *) realloc (java_classes_list,
2163 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2166 ERROR ("java plugin: realloc failed.");
2167 cjni_thread_detach ();
2170 java_classes_list = class;
2171 class = java_classes_list + java_classes_list_len;
2173 memset (class, 0, sizeof (*class));
2174 class->name = strdup (ci->values[0].value.string);
2175 if (class->name == NULL)
2177 ERROR ("java plugin: strdup failed.");
2178 cjni_thread_detach ();
2181 class->class = NULL;
2182 class->object = NULL;
2184 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2185 thorough the Java community, but (Sun's) `FindClass' and friends need
2188 for (i = 0; class->name[i] != 0; i++)
2189 if (class->name[i] == '.')
2190 class->name[i] = '/';
2193 DEBUG ("java plugin: Loading class %s", class->name);
2195 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2196 if (class->class == NULL)
2198 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2200 cjni_thread_detach ();
2205 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2207 if (constructor_id == NULL)
2209 ERROR ("java plugin: cjni_config_load_plugin: "
2210 "Could not find the constructor for `%s'.",
2212 cjni_thread_detach ();
2217 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2219 if (tmp_object != NULL)
2220 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2222 class->object = NULL;
2223 if (class->object == NULL)
2225 ERROR ("java plugin: cjni_config_load_plugin: "
2226 "Could create a new `%s' object.",
2228 cjni_thread_detach ();
2233 cjni_thread_detach ();
2235 java_classes_list_len++;
2238 } /* }}} int cjni_config_load_plugin */
2240 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2243 cjni_callback_info_t *cbi;
2252 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2254 WARNING ("java plugin: `Plugin' blocks "
2255 "need exactly one string argument.");
2259 name = ci->values[0].value.string;
2262 for (i = 0; i < java_callbacks_num; i++)
2264 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2267 if (strcmp (name, java_callbacks[i].name) != 0)
2270 cbi = java_callbacks + i;
2276 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2277 "configuration callback has been registered. Please make sure, the "
2278 "`LoadPlugin' lines precede the `Plugin' blocks.",
2283 DEBUG ("java plugin: Configuring %s", name);
2285 jvm_env = cjni_thread_attach ();
2286 if (jvm_env == NULL)
2289 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2290 if (o_ocitem == NULL)
2292 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2293 cjni_thread_detach ();
2297 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2298 method = (*jvm_env)->GetMethodID (jvm_env, class,
2299 "config", "(Lorg/collectd/api/OConfigItem;)I");
2301 status = (*jvm_env)->CallIntMethod (jvm_env,
2302 cbi->object, method, o_ocitem);
2304 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2305 cjni_thread_detach ();
2307 } /* }}} int cjni_config_plugin_block */
2309 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2319 for (i = 0; i < ci->children_num; i++)
2321 oconfig_item_t *child = ci->children + i;
2323 if (strcasecmp ("JVMArg", child->key) == 0)
2325 status = cjni_config_add_jvm_arg (child);
2331 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2333 status = cjni_config_load_plugin (child);
2339 else if (strcasecmp ("Plugin", child->key) == 0)
2341 status = cjni_config_plugin_block (child);
2349 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2354 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2355 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2357 if ((success == 0) && (errors > 0))
2359 ERROR ("java plugin: All statements failed.");
2364 } /* }}} int cjni_config_perform */
2366 /* Copy the children of `ci' to the global `config_block' variable. */
2367 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2369 oconfig_item_t *ci_copy;
2370 oconfig_item_t *tmp;
2372 assert (ci != NULL);
2373 if (ci->children_num == 0)
2374 return (0); /* nothing to do */
2376 ci_copy = oconfig_clone (ci);
2377 if (ci_copy == NULL)
2379 ERROR ("java plugin: oconfig_clone failed.");
2383 if (config_block == NULL)
2385 config_block = ci_copy;
2389 tmp = realloc (config_block->children,
2390 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2393 ERROR ("java plugin: realloc failed.");
2394 oconfig_free (ci_copy);
2397 config_block->children = tmp;
2399 /* Copy the pointers */
2400 memcpy (config_block->children + config_block->children_num,
2402 ci_copy->children_num * sizeof (*ci_copy->children));
2404 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2405 memset (ci_copy->children, 0,
2406 ci_copy->children_num * sizeof (*ci_copy->children));
2407 ci_copy->children_num = 0;
2409 oconfig_free (ci_copy);
2412 } /* }}} int cjni_config_callback */
2414 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2415 * and `cjni_write'. In particular, delete the global reference to the Java
2417 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2420 cjni_callback_info_t *cbi;
2422 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2424 cbi = (cjni_callback_info_t *) arg;
2426 /* This condition can occurr when shutting down. */
2436 jvm_env = cjni_thread_attach ();
2437 if (jvm_env == NULL)
2439 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2443 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2450 cjni_thread_detach ();
2451 } /* }}} void cjni_callback_info_destroy */
2453 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2454 static int cjni_read (user_data_t *ud) /* {{{ */
2457 cjni_callback_info_t *cbi;
2463 ERROR ("java plugin: cjni_read: jvm == NULL");
2467 if ((ud == NULL) || (ud->data == NULL))
2469 ERROR ("java plugin: cjni_read: Invalid user data.");
2473 jvm_env = cjni_thread_attach ();
2474 if (jvm_env == NULL)
2477 cbi = (cjni_callback_info_t *) ud->data;
2479 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2482 status = cjni_thread_detach ();
2485 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2489 return (ret_status);
2490 } /* }}} int cjni_read */
2492 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2493 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2497 cjni_callback_info_t *cbi;
2504 ERROR ("java plugin: cjni_write: jvm == NULL");
2508 if ((ud == NULL) || (ud->data == NULL))
2510 ERROR ("java plugin: cjni_write: Invalid user data.");
2514 jvm_env = cjni_thread_attach ();
2515 if (jvm_env == NULL)
2518 cbi = (cjni_callback_info_t *) ud->data;
2520 vl_java = ctoj_value_list (jvm_env, ds, vl);
2521 if (vl_java == NULL)
2523 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2527 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2528 cbi->object, cbi->method, vl_java);
2530 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2532 status = cjni_thread_detach ();
2535 ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2539 return (ret_status);
2540 } /* }}} int cjni_write */
2542 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2543 static int cjni_flush (int timeout, const char *identifier, /* {{{ */
2547 cjni_callback_info_t *cbi;
2548 jobject o_identifier;
2554 ERROR ("java plugin: cjni_flush: jvm == NULL");
2558 if ((ud == NULL) || (ud->data == NULL))
2560 ERROR ("java plugin: cjni_flush: Invalid user data.");
2564 jvm_env = cjni_thread_attach ();
2565 if (jvm_env == NULL)
2568 cbi = (cjni_callback_info_t *) ud->data;
2570 o_identifier = NULL;
2571 if (identifier != NULL)
2573 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2574 if (o_identifier == NULL)
2576 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2581 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2582 cbi->object, cbi->method, (jint) timeout, o_identifier);
2584 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2586 status = cjni_thread_detach ();
2589 ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2593 return (ret_status);
2594 } /* }}} int cjni_flush */
2596 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2597 static void cjni_log (int severity, const char *message, /* {{{ */
2601 cjni_callback_info_t *cbi;
2607 if ((ud == NULL) || (ud->data == NULL))
2610 jvm_env = cjni_thread_attach ();
2611 if (jvm_env == NULL)
2614 cbi = (cjni_callback_info_t *) ud->data;
2616 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2617 if (o_message == NULL)
2620 (*jvm_env)->CallVoidMethod (jvm_env,
2621 cbi->object, cbi->method, (jint) severity, o_message);
2623 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2625 cjni_thread_detach ();
2626 } /* }}} void cjni_log */
2628 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2630 static int cjni_notification (const notification_t *n, /* {{{ */
2634 cjni_callback_info_t *cbi;
2635 jobject o_notification;
2641 ERROR ("java plugin: cjni_read: jvm == NULL");
2645 if ((ud == NULL) || (ud->data == NULL))
2647 ERROR ("java plugin: cjni_read: Invalid user data.");
2651 jvm_env = cjni_thread_attach ();
2652 if (jvm_env == NULL)
2655 cbi = (cjni_callback_info_t *) ud->data;
2657 o_notification = ctoj_notification (jvm_env, n);
2658 if (o_notification == NULL)
2660 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2664 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2665 cbi->object, cbi->method, o_notification);
2667 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2669 status = cjni_thread_detach ();
2672 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2676 return (ret_status);
2677 } /* }}} int cjni_notification */
2679 /* Callbacks for matches implemented in Java */
2680 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2684 cjni_callback_info_t *cbi_ret;
2685 cjni_callback_info_t *cbi_factory;
2696 #define BAIL_OUT(status) \
2697 if (cbi_ret != NULL) { \
2698 free (cbi_ret->name); \
2699 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2700 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2703 if (jvm_env != NULL) { \
2705 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2706 cjni_thread_detach (); \
2712 ERROR ("java plugin: cjni_read: jvm == NULL");
2716 jvm_env = cjni_thread_attach ();
2717 if (jvm_env == NULL)
2722 /* Find out whether to create a match or a target. */
2723 if (strcasecmp ("Match", ci->key) == 0)
2724 type = CB_TYPE_MATCH;
2725 else if (strcasecmp ("Target", ci->key) == 0)
2726 type = CB_TYPE_TARGET;
2729 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2730 "to create a match or a target.");
2734 /* This is the name of the match we should create. */
2735 name = ci->values[0].value.string;
2737 /* Lets see if we have a matching factory here.. */
2739 for (i = 0; i < java_callbacks_num; i++)
2741 if (java_callbacks[i].type != type)
2744 if (strcmp (name, java_callbacks[i].name) != 0)
2747 cbi_factory = java_callbacks + i;
2751 /* Nope, no factory for that name.. */
2752 if (cbi_factory == NULL)
2754 ERROR ("java plugin: cjni_match_target_create: "
2755 "No such match factory registered: %s",
2760 /* We convert `ci' to its Java equivalent.. */
2761 o_ci = ctoj_oconfig_item (jvm_env, ci);
2764 ERROR ("java plugin: cjni_match_target_create: "
2765 "ctoj_oconfig_item failed.");
2769 /* Allocate a new callback info structure. This is going to be our user_data
2771 cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
2772 if (cbi_ret == NULL)
2774 ERROR ("java plugin: cjni_match_target_create: malloc failed.");
2777 memset (cbi_ret, 0, sizeof (*cbi_ret));
2778 cbi_ret->object = NULL;
2779 cbi_ret->type = type;
2781 /* Lets fill the callback info structure.. First, the name: */
2782 cbi_ret->name = strdup (name);
2783 if (cbi_ret->name == NULL)
2785 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2789 /* Then call the factory method so it creates a new object for us. */
2790 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2791 cbi_factory->object, cbi_factory->method, o_ci);
2794 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2798 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2801 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2805 /* This is the class of the match. It is possibly different from the class of
2806 * the match-factory! */
2807 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2808 if (cbi_ret->class == NULL)
2810 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2814 /* Lookup the `int match (DataSet, ValueList)' method. */
2815 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2816 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2817 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2818 if (cbi_ret->method == NULL)
2820 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2824 /* Return the newly created match via the user_data pointer. */
2825 *user_data = (void *) cbi_ret;
2827 cjni_thread_detach ();
2829 DEBUG ("java plugin: cjni_match_target_create: "
2830 "Successfully created a `%s' %s.",
2831 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2836 } /* }}} int cjni_match_target_create */
2838 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2840 cjni_callback_info_destroy (*user_data);
2844 } /* }}} int cjni_match_target_destroy */
2846 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2847 value_list_t *vl, notification_meta_t **meta, void **user_data)
2850 cjni_callback_info_t *cbi;
2858 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2862 jvm_env = cjni_thread_attach ();
2863 if (jvm_env == NULL)
2866 cbi = (cjni_callback_info_t *) *user_data;
2868 o_vl = ctoj_value_list (jvm_env, ds, vl);
2871 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2872 cjni_thread_detach ();
2876 o_ds = ctoj_data_set (jvm_env, ds);
2879 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2880 cjni_thread_detach ();
2884 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2887 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2889 /* If we're executing a target, copy the `ValueList' back to our
2890 * `value_list_t'. */
2891 if (cbi->type == CB_TYPE_TARGET)
2893 value_list_t new_vl;
2895 memset (&new_vl, 0, sizeof (new_vl));
2896 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2899 ERROR ("java plugin: cjni_match_target_invoke: "
2900 "jtoc_value_list failed.");
2902 else /* if (status == 0) */
2904 /* plugin_dispatch_values assures that this is dynamically allocated
2908 /* This will replace the vl->values pointer to a new, dynamically
2909 * allocated piece of memory. */
2910 memcpy (vl, &new_vl, sizeof (*vl));
2912 } /* if (cbi->type == CB_TYPE_TARGET) */
2914 status = cjni_thread_detach ();
2916 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2918 return (ret_status);
2919 } /* }}} int cjni_match_target_invoke */
2921 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2922 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2927 for (i = 0; i < java_callbacks_num; i++)
2929 if (java_callbacks[i].type != CB_TYPE_INIT)
2932 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2934 status = (*jvm_env)->CallIntMethod (jvm_env,
2935 java_callbacks[i].object, java_callbacks[i].method);
2938 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2939 "Removing read function.",
2940 java_callbacks[i].name, status);
2941 plugin_unregister_read (java_callbacks[i].name);
2946 } /* }}} int cjni_init_plugins */
2948 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2949 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2954 for (i = 0; i < java_callbacks_num; i++)
2956 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2959 DEBUG ("java plugin: Shutting down %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: Shutting down `%s' failed with status %i. ",
2966 java_callbacks[i].name, status);
2971 } /* }}} int cjni_shutdown_plugins */
2974 static int cjni_shutdown (void) /* {{{ */
2977 JavaVMAttachArgs args;
2985 memset (&args, 0, sizeof (args));
2986 args.version = JNI_VERSION_1_2;
2988 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
2991 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2996 /* Execute all the shutdown functions registered by plugins. */
2997 cjni_shutdown_plugins (jvm_env);
2999 /* Release all the global references to callback functions */
3000 for (i = 0; i < java_callbacks_num; i++)
3002 if (java_callbacks[i].object != NULL)
3004 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3005 java_callbacks[i].object = NULL;
3007 sfree (java_callbacks[i].name);
3009 java_callbacks_num = 0;
3010 sfree (java_callbacks);
3012 /* Release all the global references to directly loaded classes. */
3013 for (i = 0; i < java_classes_list_len; i++)
3015 if (java_classes_list[i].object != NULL)
3017 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3018 java_classes_list[i].object = NULL;
3020 sfree (java_classes_list[i].name);
3022 java_classes_list_len = 0;
3023 sfree (java_classes_list);
3025 /* Destroy the JVM */
3026 DEBUG ("java plugin: Destroying the JVM.");
3027 (*jvm)->DestroyJavaVM (jvm);
3031 pthread_key_delete (jvm_env_key);
3033 /* Free the JVM argument list */
3034 for (i = 0; i < jvm_argc; i++)
3035 sfree (jvm_argv[i]);
3040 } /* }}} int cjni_shutdown */
3042 /* Initialization: Create a JVM, load all configured classes and call their
3043 * `config' and `init' callback methods. */
3044 static int cjni_init (void) /* {{{ */
3048 if ((config_block == NULL) && (jvm == NULL))
3050 ERROR ("java plugin: cjni_init: No configuration block for "
3051 "the java plugin was found.");
3055 if (config_block != NULL)
3059 status = cjni_config_perform (config_block);
3060 oconfig_free (config_block);
3061 config_block = NULL;
3066 ERROR ("java plugin: cjni_init: jvm == NULL");
3070 jvm_env = cjni_thread_attach ();
3071 if (jvm_env == NULL)
3074 cjni_init_plugins (jvm_env);
3076 cjni_thread_detach ();
3078 } /* }}} int cjni_init */
3080 void module_register (void)
3082 plugin_register_complex_config ("java", cjni_config_callback);
3083 plugin_register_init ("java", cjni_init);
3084 plugin_register_shutdown ("java", cjni_shutdown);
3085 } /* void module_register (void) */
3087 /* vim: set sw=2 sts=2 et fdm=marker : */