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 (cdtime_t timeout, const char *identifier, user_data_t *ud);
113 static void cjni_log (int severity, const char *message, user_data_t *ud);
114 static int cjni_notification (const notification_t *n, user_data_t *ud);
116 /* Create, destroy, and match/invoke functions, used by both, matches AND
118 static int cjni_match_target_create (const oconfig_item_t *ci, void **user_data);
119 static int cjni_match_target_destroy (void **user_data);
120 static int cjni_match_target_invoke (const data_set_t *ds, value_list_t *vl,
121 notification_meta_t **meta, void **user_data);
124 * C to Java conversion functions
126 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
128 jclass class_ptr, jobject object_ptr, const char *method_name)
133 /* Create a java.lang.String */
134 o_string = (*jvm_env)->NewStringUTF (jvm_env,
135 (string != NULL) ? string : "");
136 if (o_string == NULL)
138 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
142 /* Search for the `void setFoo (String s)' method. */
143 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
144 method_name, "(Ljava/lang/String;)V");
147 ERROR ("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
149 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
153 /* Call the method. */
154 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, o_string);
156 /* Decrease reference counter on the java.lang.String object. */
157 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
160 } /* }}} int ctoj_string */
162 static 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));
289 if (ds_type == DS_TYPE_DERIVE)
290 return (ctoj_jlong_to_number (jvm_env, (jlong) value.derive));
291 if (ds_type == DS_TYPE_ABSOLUTE)
292 return (ctoj_jlong_to_number (jvm_env, (jlong) value.absolute));
295 } /* }}} jobject ctoj_value_to_number */
297 /* Convert a data_source_t to a org/collectd/api/DataSource */
298 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
299 const data_source_t *dsrc)
302 jmethodID m_datasource_constructor;
303 jobject o_datasource;
306 /* Look up the DataSource class */
307 c_datasource = (*jvm_env)->FindClass (jvm_env,
308 "org/collectd/api/DataSource");
309 if (c_datasource == NULL)
311 ERROR ("java plugin: ctoj_data_source: "
312 "FindClass (org/collectd/api/DataSource) failed.");
316 /* Lookup the `ValueList ()' constructor. */
317 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
319 if (m_datasource_constructor == NULL)
321 ERROR ("java plugin: ctoj_data_source: Cannot find the "
322 "`DataSource ()' constructor.");
326 /* Create a new instance. */
327 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
328 m_datasource_constructor);
329 if (o_datasource == NULL)
331 ERROR ("java plugin: ctoj_data_source: "
332 "Creating a new DataSource instance failed.");
336 /* Set name via `void setName (String name)' */
337 status = ctoj_string (jvm_env, dsrc->name,
338 c_datasource, o_datasource, "setName");
341 ERROR ("java plugin: ctoj_data_source: "
342 "ctoj_string (setName) failed.");
343 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
347 /* Set type via `void setType (int type)' */
348 status = ctoj_int (jvm_env, dsrc->type,
349 c_datasource, o_datasource, "setType");
352 ERROR ("java plugin: ctoj_data_source: "
353 "ctoj_int (setType) failed.");
354 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
358 /* Set min via `void setMin (double min)' */
359 status = ctoj_double (jvm_env, dsrc->min,
360 c_datasource, o_datasource, "setMin");
363 ERROR ("java plugin: ctoj_data_source: "
364 "ctoj_double (setMin) failed.");
365 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
369 /* Set max via `void setMax (double max)' */
370 status = ctoj_double (jvm_env, dsrc->max,
371 c_datasource, o_datasource, "setMax");
374 ERROR ("java plugin: ctoj_data_source: "
375 "ctoj_double (setMax) failed.");
376 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
380 return (o_datasource);
381 } /* }}} jobject ctoj_data_source */
383 /* Convert a oconfig_value_t to a org/collectd/api/OConfigValue */
384 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
385 oconfig_value_t ocvalue)
388 jmethodID m_ocvalue_constructor;
392 m_ocvalue_constructor = NULL;
395 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
396 "org/collectd/api/OConfigValue");
397 if (c_ocvalue == NULL)
399 ERROR ("java plugin: ctoj_oconfig_value: "
400 "FindClass (org/collectd/api/OConfigValue) failed.");
404 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
406 jboolean tmp_boolean;
408 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
410 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
412 if (m_ocvalue_constructor == NULL)
414 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
415 "`OConfigValue (boolean)' constructor.");
419 return ((*jvm_env)->NewObject (jvm_env,
420 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
421 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
422 else if (ocvalue.type == OCONFIG_TYPE_STRING)
424 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
425 "<init>", "(Ljava/lang/String;)V");
426 if (m_ocvalue_constructor == NULL)
428 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
429 "`OConfigValue (String)' constructor.");
433 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
434 if (o_argument == NULL)
436 ERROR ("java plugin: ctoj_oconfig_value: "
437 "Creating a String object failed.");
441 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
443 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
444 "<init>", "(Ljava/lang/Number;)V");
445 if (m_ocvalue_constructor == NULL)
447 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
448 "`OConfigValue (Number)' constructor.");
452 o_argument = ctoj_jdouble_to_number (jvm_env,
453 (jdouble) ocvalue.value.number);
454 if (o_argument == NULL)
456 ERROR ("java plugin: ctoj_oconfig_value: "
457 "Creating a Number object failed.");
466 assert (m_ocvalue_constructor != NULL);
467 assert (o_argument != NULL);
469 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
470 c_ocvalue, m_ocvalue_constructor, o_argument);
471 if (o_ocvalue == NULL)
473 ERROR ("java plugin: ctoj_oconfig_value: "
474 "Creating an OConfigValue object failed.");
475 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
479 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
481 } /* }}} jobject ctoj_oconfig_value */
483 /* Convert a oconfig_item_t to a org/collectd/api/OConfigItem */
484 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
485 const oconfig_item_t *ci)
488 jmethodID m_ocitem_constructor;
489 jmethodID m_addvalue;
490 jmethodID m_addchild;
495 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/OConfigItem");
496 if (c_ocitem == NULL)
498 ERROR ("java plugin: ctoj_oconfig_item: "
499 "FindClass (org/collectd/api/OConfigItem) failed.");
503 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
505 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
506 "<init>", "(Ljava/lang/String;)V");
507 if (m_ocitem_constructor == NULL)
509 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
510 "`OConfigItem (String)' constructor.");
514 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
515 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
516 if (m_addvalue == NULL)
518 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
519 "`addValue (OConfigValue)' method.");
523 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
524 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
525 if (m_addchild == NULL)
527 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
528 "`addChild (OConfigItem)' method.");
533 /* Create a String object with the key.
534 * Needed for calling the constructor. */
535 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
538 ERROR ("java plugin: ctoj_oconfig_item: "
539 "Creating String object failed.");
543 /* Create an OConfigItem object */
544 o_ocitem = (*jvm_env)->NewObject (jvm_env,
545 c_ocitem, m_ocitem_constructor, o_key);
546 if (o_ocitem == NULL)
548 ERROR ("java plugin: ctoj_oconfig_item: "
549 "Creating an OConfigItem object failed.");
550 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
554 /* We don't need the String object any longer.. */
555 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
557 /* Call OConfigItem.addValue for each value */
558 for (i = 0; i < ci->values_num; i++) /* {{{ */
562 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
565 ERROR ("java plugin: ctoj_oconfig_item: "
566 "Creating an OConfigValue object failed.");
567 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
571 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
572 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
573 } /* }}} for (i = 0; i < ci->values_num; i++) */
575 /* Call OConfigItem.addChild for each child */
576 for (i = 0; i < ci->children_num; i++) /* {{{ */
580 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
583 ERROR ("java plugin: ctoj_oconfig_item: "
584 "Creating an OConfigItem object failed.");
585 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
589 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addchild, o_child);
590 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
591 } /* }}} for (i = 0; i < ci->children_num; i++) */
594 } /* }}} jobject ctoj_oconfig_item */
596 /* Convert a data_set_t to a org/collectd/api/DataSet */
597 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
600 jmethodID m_constructor;
606 /* Look up the org/collectd/api/DataSet class */
607 c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet");
608 if (c_dataset == NULL)
610 ERROR ("java plugin: ctoj_data_set: Looking up the "
611 "org/collectd/api/DataSet class failed.");
615 /* Search for the `DataSet (String type)' constructor. */
616 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
617 c_dataset, "<init>", "(Ljava/lang/String;)V");
618 if (m_constructor == NULL)
620 ERROR ("java plugin: ctoj_data_set: Looking up the "
621 "`DataSet (String)' constructor failed.");
625 /* Search for the `void addDataSource (DataSource)' method. */
626 m_add = (*jvm_env)->GetMethodID (jvm_env,
627 c_dataset, "addDataSource", "(Lorg/collectd/api/DataSource;)V");
630 ERROR ("java plugin: ctoj_data_set: Looking up the "
631 "`addDataSource (DataSource)' method failed.");
635 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
638 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
642 o_dataset = (*jvm_env)->NewObject (jvm_env,
643 c_dataset, m_constructor, o_type);
644 if (o_dataset == NULL)
646 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
647 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
651 /* Decrease reference counter on the java.lang.String object. */
652 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
654 for (i = 0; i < ds->ds_num; i++)
656 jobject o_datasource;
658 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
659 if (o_datasource == NULL)
661 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
662 ds->type, ds->ds[i].name);
663 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
667 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
669 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
670 } /* for (i = 0; i < ds->ds_num; i++) */
673 } /* }}} jobject ctoj_data_set */
675 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
676 value_t value, int ds_type,
677 jclass class_ptr, jobject object_ptr)
679 jmethodID m_addvalue;
682 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
683 "addValue", "(Ljava/lang/Number;)V");
684 if (m_addvalue == NULL)
686 ERROR ("java plugin: ctoj_value_list_add_value: "
687 "Cannot find method `void addValue (Number)'.");
691 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
692 if (o_number == NULL)
694 ERROR ("java plugin: ctoj_value_list_add_value: "
695 "ctoj_value_to_number failed.");
699 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
701 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
704 } /* }}} int ctoj_value_list_add_value */
706 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
707 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
709 jmethodID m_setdataset;
712 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
713 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
714 "setDataSet", "(Lorg/collectd/api/DataSet;)V");
715 if (m_setdataset == NULL)
717 ERROR ("java plugin: ctoj_value_list_add_data_set: "
718 "Cannot find the `void setDataSet (DataSet)' method.");
722 /* Create a DataSet object. */
723 o_dataset = ctoj_data_set (jvm_env, ds);
724 if (o_dataset == NULL)
726 ERROR ("java plugin: ctoj_value_list_add_data_set: "
727 "ctoj_data_set (%s) failed.", ds->type);
731 /* Actually call the method. */
732 (*jvm_env)->CallVoidMethod (jvm_env,
733 o_valuelist, m_setdataset, o_dataset);
735 /* Decrease reference counter on the List<DataSource> object. */
736 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
739 } /* }}} int ctoj_value_list_add_data_set */
741 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
742 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
743 const data_set_t *ds, const value_list_t *vl)
746 jmethodID m_valuelist_constructor;
751 /* First, create a new ValueList instance..
752 * Look up the class.. */
753 c_valuelist = (*jvm_env)->FindClass (jvm_env,
754 "org/collectd/api/ValueList");
755 if (c_valuelist == NULL)
757 ERROR ("java plugin: ctoj_value_list: "
758 "FindClass (org/collectd/api/ValueList) failed.");
762 /* Lookup the `ValueList ()' constructor. */
763 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
765 if (m_valuelist_constructor == NULL)
767 ERROR ("java plugin: ctoj_value_list: Cannot find the "
768 "`ValueList ()' constructor.");
772 /* Create a new instance. */
773 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
774 m_valuelist_constructor);
775 if (o_valuelist == NULL)
777 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
782 status = ctoj_value_list_add_data_set (jvm_env,
783 c_valuelist, o_valuelist, ds);
786 ERROR ("java plugin: ctoj_value_list: "
787 "ctoj_value_list_add_data_set failed.");
788 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
792 /* Set the strings.. */
793 #define SET_STRING(str,method_name) do { \
794 status = ctoj_string (jvm_env, str, \
795 c_valuelist, o_valuelist, method_name); \
797 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
799 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
803 SET_STRING (vl->host, "setHost");
804 SET_STRING (vl->plugin, "setPlugin");
805 SET_STRING (vl->plugin_instance, "setPluginInstance");
806 SET_STRING (vl->type, "setType");
807 SET_STRING (vl->type_instance, "setTypeInstance");
811 /* Set the `time' member. Java stores time in milliseconds. */
812 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (vl->time),
813 c_valuelist, o_valuelist, "setTime");
816 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
817 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
821 /* Set the `interval' member.. */
822 status = ctoj_long (jvm_env,
823 (jlong) CDTIME_T_TO_MS (vl->interval),
824 c_valuelist, o_valuelist, "setInterval");
827 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
828 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
832 for (i = 0; i < vl->values_len; i++)
834 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
835 c_valuelist, o_valuelist);
838 ERROR ("java plugin: ctoj_value_list: "
839 "ctoj_value_list_add_value failed.");
840 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
845 return (o_valuelist);
846 } /* }}} jobject ctoj_value_list */
848 /* Convert a notification_t to a org/collectd/api/Notification */
849 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
850 const notification_t *n)
852 jclass c_notification;
853 jmethodID m_constructor;
854 jobject o_notification;
857 /* First, create a new Notification instance..
858 * Look up the class.. */
859 c_notification = (*jvm_env)->FindClass (jvm_env,
860 "org/collectd/api/Notification");
861 if (c_notification == NULL)
863 ERROR ("java plugin: ctoj_notification: "
864 "FindClass (org/collectd/api/Notification) failed.");
868 /* Lookup the `Notification ()' constructor. */
869 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
871 if (m_constructor == NULL)
873 ERROR ("java plugin: ctoj_notification: Cannot find the "
874 "`Notification ()' constructor.");
878 /* Create a new instance. */
879 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
881 if (o_notification == NULL)
883 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
888 /* Set the strings.. */
889 #define SET_STRING(str,method_name) do { \
890 status = ctoj_string (jvm_env, str, \
891 c_notification, o_notification, method_name); \
893 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
895 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
899 SET_STRING (n->host, "setHost");
900 SET_STRING (n->plugin, "setPlugin");
901 SET_STRING (n->plugin_instance, "setPluginInstance");
902 SET_STRING (n->type, "setType");
903 SET_STRING (n->type_instance, "setTypeInstance");
904 SET_STRING (n->message, "setMessage");
908 /* Set the `time' member. Java stores time in milliseconds. */
909 status = ctoj_long (jvm_env, ((jlong) n->time) * ((jlong) 1000),
910 c_notification, o_notification, "setTime");
913 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
914 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
918 /* Set the `severity' member.. */
919 status = ctoj_int (jvm_env, (jint) n->severity,
920 c_notification, o_notification, "setSeverity");
923 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
924 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
928 return (o_notification);
929 } /* }}} jobject ctoj_notification */
932 * Java to C conversion functions
934 /* Call a `String <method> ()' method. */
935 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
936 char *buffer, size_t buffer_size, int empty_okay,
937 jclass class_ptr, jobject object_ptr, const char *method_name)
943 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
944 method_name, "()Ljava/lang/String;");
945 if (method_id == NULL)
947 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
952 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
953 if ((string_obj == NULL) && (empty_okay == 0))
955 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
959 else if ((string_obj == NULL) && (empty_okay != 0))
961 memset (buffer, 0, buffer_size);
965 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
968 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
969 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
973 sstrncpy (buffer, c_str, buffer_size);
975 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
976 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
979 } /* }}} int jtoc_string */
981 /* Call an `int <method> ()' method. */
982 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
984 jclass class_ptr, jobject object_ptr, const char *method_name)
988 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
990 if (method_id == NULL)
992 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
997 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
1000 } /* }}} int jtoc_int */
1002 /* Call a `long <method> ()' method. */
1003 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
1005 jclass class_ptr, jobject object_ptr, const char *method_name)
1007 jmethodID method_id;
1009 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1010 method_name, "()J");
1011 if (method_id == NULL)
1013 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1018 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1021 } /* }}} int jtoc_long */
1023 /* Call a `double <method> ()' method. */
1024 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1026 jclass class_ptr, jobject object_ptr, const char *method_name)
1028 jmethodID method_id;
1030 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1031 method_name, "()D");
1032 if (method_id == NULL)
1034 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1039 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1042 } /* }}} int jtoc_double */
1044 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1045 value_t *ret_value, int ds_type, jobject object_ptr)
1050 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1052 if (ds_type == DS_TYPE_GAUGE)
1056 status = jtoc_double (jvm_env, &tmp_double,
1057 class_ptr, object_ptr, "doubleValue");
1060 ERROR ("java plugin: jtoc_value: "
1061 "jtoc_double failed.");
1064 (*ret_value).gauge = (gauge_t) tmp_double;
1070 status = jtoc_long (jvm_env, &tmp_long,
1071 class_ptr, object_ptr, "longValue");
1074 ERROR ("java plugin: jtoc_value: "
1075 "jtoc_long failed.");
1079 if (ds_type == DS_TYPE_DERIVE)
1080 (*ret_value).derive = (derive_t) tmp_long;
1081 else if (ds_type == DS_TYPE_ABSOLUTE)
1082 (*ret_value).absolute = (absolute_t) tmp_long;
1084 (*ret_value).counter = (counter_t) tmp_long;
1088 } /* }}} int jtoc_value */
1090 /* Read a List<Number>, convert it to `value_t' and add it to the given
1091 * `value_list_t'. */
1092 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1093 const data_set_t *ds, value_list_t *vl,
1094 jclass class_ptr, jobject object_ptr)
1096 jmethodID m_getvalues;
1097 jmethodID m_toarray;
1099 jobjectArray o_number_array;
1105 values_num = ds->ds_num;
1108 o_number_array = NULL;
1111 #define BAIL_OUT(status) \
1113 if (o_number_array != NULL) \
1114 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1115 if (o_list != NULL) \
1116 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1119 /* Call: List<Number> ValueList.getValues () */
1120 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1121 "getValues", "()Ljava/util/List;");
1122 if (m_getvalues == NULL)
1124 ERROR ("java plugin: jtoc_values_array: "
1125 "Cannot find method `List getValues ()'.");
1129 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1132 ERROR ("java plugin: jtoc_values_array: "
1133 "CallObjectMethod (getValues) failed.");
1137 /* Call: Number[] List.toArray () */
1138 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1139 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1140 "toArray", "()[Ljava/lang/Object;");
1141 if (m_toarray == NULL)
1143 ERROR ("java plugin: jtoc_values_array: "
1144 "Cannot find method `Object[] toArray ()'.");
1148 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1149 if (o_number_array == NULL)
1151 ERROR ("java plugin: jtoc_values_array: "
1152 "CallObjectMethod (toArray) failed.");
1156 values = (value_t *) calloc (values_num, sizeof (value_t));
1159 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1163 for (i = 0; i < values_num; i++)
1168 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1169 o_number_array, (jsize) i);
1170 if (o_number == NULL)
1172 ERROR ("java plugin: jtoc_values_array: "
1173 "GetObjectArrayElement (%i) failed.", i);
1177 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1180 ERROR ("java plugin: jtoc_values_array: "
1181 "jtoc_value (%i) failed.", i);
1184 } /* for (i = 0; i < values_num; i++) */
1186 vl->values = values;
1187 vl->values_len = values_num;
1190 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1191 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1193 } /* }}} int jtoc_values_array */
1195 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1196 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1202 const data_set_t *ds;
1204 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1205 if (class_ptr == NULL)
1207 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1211 /* eo == empty okay */
1212 #define SET_STRING(buffer,method, eo) do { \
1213 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1214 class_ptr, object_ptr, method); \
1215 if (status != 0) { \
1216 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1221 SET_STRING(vl->type, "getType", /* empty = */ 0);
1223 ds = plugin_get_ds (vl->type);
1226 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1227 "Please consult the types.db(5) manpage for mor information.",
1232 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1233 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1234 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1235 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1239 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1242 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1245 /* Java measures time in milliseconds. */
1246 vl->time = MS_TO_CDTIME_T (tmp_long);
1248 status = jtoc_long (jvm_env, &tmp_long,
1249 class_ptr, object_ptr, "getInterval");
1252 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1255 vl->interval = MS_TO_CDTIME_T (tmp_long);
1257 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1260 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1265 } /* }}} int jtoc_value_list */
1267 /* Convert a org/collectd/api/Notification to a notification_t. */
1268 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1276 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1277 if (class_ptr == NULL)
1279 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1283 /* eo == empty okay */
1284 #define SET_STRING(buffer,method, eo) do { \
1285 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1286 class_ptr, object_ptr, method); \
1287 if (status != 0) { \
1288 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1293 SET_STRING (n->host, "getHost", /* empty = */ 1);
1294 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1295 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1296 SET_STRING (n->type, "getType", /* empty = */ 1);
1297 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1298 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1302 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1305 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1308 /* Java measures time in milliseconds. */
1309 n->time = (time_t) (tmp_long / ((jlong) 1000));
1311 status = jtoc_int (jvm_env, &tmp_int,
1312 class_ptr, object_ptr, "getSeverity");
1315 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1318 n->severity = (int) tmp_int;
1321 } /* }}} int jtoc_notification */
1323 * Functions accessible from Java
1325 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1326 jobject this, jobject java_vl)
1328 value_list_t vl = VALUE_LIST_INIT;
1331 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1333 status = jtoc_value_list (jvm_env, &vl, java_vl);
1336 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1340 status = plugin_dispatch_values (&vl);
1345 } /* }}} jint cjni_api_dispatch_values */
1347 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1348 jobject this, jobject o_notification)
1353 memset (&n, 0, sizeof (n));
1356 status = jtoc_notification (jvm_env, &n, o_notification);
1359 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1363 status = plugin_dispatch_notification (&n);
1366 } /* }}} jint cjni_api_dispatch_notification */
1368 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1369 jobject this, jobject o_string_type)
1371 const char *ds_name;
1372 const data_set_t *ds;
1375 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1376 if (ds_name == NULL)
1378 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1382 ds = plugin_get_ds (ds_name);
1383 DEBUG ("java plugin: cjni_api_get_ds: "
1384 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1386 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1391 o_dataset = ctoj_data_set (jvm_env, ds);
1393 } /* }}} jint cjni_api_get_ds */
1395 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1396 jobject this, jobject o_name, jobject o_config)
1398 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1399 } /* }}} jint cjni_api_register_config */
1401 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1402 jobject this, jobject o_name, jobject o_config)
1404 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1405 } /* }}} jint cjni_api_register_init */
1407 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1408 jobject this, jobject o_name, jobject o_read)
1411 cjni_callback_info_t *cbi;
1413 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1417 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1419 memset (&ud, 0, sizeof (ud));
1420 ud.data = (void *) cbi;
1421 ud.free_func = cjni_callback_info_destroy;
1423 plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read,
1424 /* interval = */ NULL, &ud);
1426 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1429 } /* }}} jint cjni_api_register_read */
1431 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1432 jobject this, jobject o_name, jobject o_write)
1435 cjni_callback_info_t *cbi;
1437 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1441 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1443 memset (&ud, 0, sizeof (ud));
1444 ud.data = (void *) cbi;
1445 ud.free_func = cjni_callback_info_destroy;
1447 plugin_register_write (cbi->name, cjni_write, &ud);
1449 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1452 } /* }}} jint cjni_api_register_write */
1454 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1455 jobject this, jobject o_name, jobject o_flush)
1458 cjni_callback_info_t *cbi;
1460 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1464 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1466 memset (&ud, 0, sizeof (ud));
1467 ud.data = (void *) cbi;
1468 ud.free_func = cjni_callback_info_destroy;
1470 plugin_register_flush (cbi->name, cjni_flush, &ud);
1472 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1475 } /* }}} jint cjni_api_register_flush */
1477 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1478 jobject this, jobject o_name, jobject o_shutdown)
1480 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1482 } /* }}} jint cjni_api_register_shutdown */
1484 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1485 jobject this, jobject o_name, jobject o_log)
1488 cjni_callback_info_t *cbi;
1490 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1494 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1496 memset (&ud, 0, sizeof (ud));
1497 ud.data = (void *) cbi;
1498 ud.free_func = cjni_callback_info_destroy;
1500 plugin_register_log (cbi->name, cjni_log, &ud);
1502 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1505 } /* }}} jint cjni_api_register_log */
1507 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1508 jobject this, jobject o_name, jobject o_notification)
1511 cjni_callback_info_t *cbi;
1513 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1514 CB_TYPE_NOTIFICATION);
1518 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1520 memset (&ud, 0, sizeof (ud));
1521 ud.data = (void *) cbi;
1522 ud.free_func = cjni_callback_info_destroy;
1524 plugin_register_notification (cbi->name, cjni_notification, &ud);
1526 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1529 } /* }}} jint cjni_api_register_notification */
1531 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1532 jobject this, jobject o_name, jobject o_match, int type)
1537 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1540 ERROR ("java plugin: cjni_api_register_match_target: "
1541 "GetStringUTFChars failed.");
1545 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1548 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1552 if (type == CB_TYPE_MATCH)
1554 match_proc_t m_proc;
1556 memset (&m_proc, 0, sizeof (m_proc));
1557 m_proc.create = cjni_match_target_create;
1558 m_proc.destroy = cjni_match_target_destroy;
1559 m_proc.match = (void *) cjni_match_target_invoke;
1561 status = fc_register_match (c_name, m_proc);
1563 else if (type == CB_TYPE_TARGET)
1565 target_proc_t t_proc;
1567 memset (&t_proc, 0, sizeof (t_proc));
1568 t_proc.create = cjni_match_target_create;
1569 t_proc.destroy = cjni_match_target_destroy;
1570 t_proc.invoke = cjni_match_target_invoke;
1572 status = fc_register_target (c_name, t_proc);
1576 ERROR ("java plugin: cjni_api_register_match_target: "
1577 "Don't know whether to create a match or a target.");
1578 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1584 ERROR ("java plugin: cjni_api_register_match_target: "
1586 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1587 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1591 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1594 } /* }}} jint cjni_api_register_match_target */
1596 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1597 jobject this, jobject o_name, jobject o_match)
1599 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1601 } /* }}} jint cjni_api_register_match */
1603 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1604 jobject this, jobject o_name, jobject o_target)
1606 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1608 } /* }}} jint cjni_api_register_target */
1610 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1611 jobject this, jint severity, jobject o_message)
1615 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1618 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1622 if (severity < LOG_ERR)
1624 if (severity > LOG_DEBUG)
1625 severity = LOG_DEBUG;
1627 plugin_log (severity, "%s", c_str);
1629 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1630 } /* }}} void cjni_api_log */
1632 /* List of ``native'' functions, i. e. C-functions that can be called from
1634 static JNINativeMethod jni_api_functions[] = /* {{{ */
1637 "(Lorg/collectd/api/ValueList;)I",
1638 cjni_api_dispatch_values },
1640 { "dispatchNotification",
1641 "(Lorg/collectd/api/Notification;)I",
1642 cjni_api_dispatch_notification },
1645 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1649 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1650 cjni_api_register_config },
1653 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1654 cjni_api_register_init },
1657 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1658 cjni_api_register_read },
1661 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1662 cjni_api_register_write },
1665 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1666 cjni_api_register_flush },
1668 { "registerShutdown",
1669 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1670 cjni_api_register_shutdown },
1673 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1674 cjni_api_register_log },
1676 { "registerNotification",
1677 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1678 cjni_api_register_notification },
1681 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1682 cjni_api_register_match },
1685 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1686 cjni_api_register_target },
1689 "(ILjava/lang/String;)V",
1692 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1693 / sizeof (jni_api_functions[0]);
1699 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1700 * all registration functions. */
1701 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1702 jobject o_name, jobject o_callback, int type)
1705 cjni_callback_info_t *cbi;
1706 const char *method_name;
1707 const char *method_signature;
1711 case CB_TYPE_CONFIG:
1712 method_name = "config";
1713 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1717 method_name = "init";
1718 method_signature = "()I";
1722 method_name = "read";
1723 method_signature = "()I";
1727 method_name = "write";
1728 method_signature = "(Lorg/collectd/api/ValueList;)I";
1732 method_name = "flush";
1733 method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I";
1736 case CB_TYPE_SHUTDOWN:
1737 method_name = "shutdown";
1738 method_signature = "()I";
1742 method_name = "log";
1743 method_signature = "(ILjava/lang/String;)V";
1746 case CB_TYPE_NOTIFICATION:
1747 method_name = "notification";
1748 method_signature = "(Lorg/collectd/api/Notification;)I";
1752 method_name = "createMatch";
1753 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1754 "Lorg/collectd/api/CollectdMatchInterface;";
1757 case CB_TYPE_TARGET:
1758 method_name = "createTarget";
1759 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1760 "Lorg/collectd/api/CollectdTargetInterface;";
1764 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1769 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1772 ERROR ("java plugin: cjni_callback_info_create: "
1773 "GetStringUTFChars failed.");
1777 cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1780 ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1781 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1784 memset (cbi, 0, sizeof (*cbi));
1787 cbi->name = strdup (c_name);
1788 if (cbi->name == NULL)
1790 pthread_mutex_unlock (&java_callbacks_lock);
1791 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1792 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1796 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1798 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1799 if (cbi->object == NULL)
1801 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1806 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1807 if (cbi->class == NULL)
1809 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1814 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1815 method_name, method_signature);
1816 if (cbi->method == NULL)
1818 ERROR ("java plugin: cjni_callback_info_create: "
1819 "Cannot find the `%s' method with signature `%s'.",
1820 method_name, method_signature);
1826 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1828 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1829 * to the global `java_callbacks' variable. This is used for `config', `init',
1830 * and `shutdown' callbacks. */
1831 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1832 jobject o_name, jobject o_callback, int type)
1834 cjni_callback_info_t *cbi;
1835 cjni_callback_info_t *tmp;
1837 const char *type_str;
1840 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1847 case CB_TYPE_CONFIG:
1848 type_str = "config";
1855 case CB_TYPE_SHUTDOWN:
1856 type_str = "shutdown";
1863 case CB_TYPE_TARGET:
1864 type_str = "target";
1868 type_str = "<unknown>";
1870 DEBUG ("java plugin: Registering new %s callback: %s",
1871 type_str, cbi->name);
1874 pthread_mutex_lock (&java_callbacks_lock);
1876 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1877 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1880 pthread_mutex_unlock (&java_callbacks_lock);
1881 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1883 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1888 java_callbacks = tmp;
1889 java_callbacks[java_callbacks_num] = *cbi;
1890 java_callbacks_num++;
1892 pthread_mutex_unlock (&java_callbacks_lock);
1896 } /* }}} int cjni_callback_register */
1898 /* Callback for `pthread_key_create'. It frees the data contained in
1899 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1900 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1902 cjni_jvm_env_t *cjni_env;
1907 cjni_env = (cjni_jvm_env_t *) args;
1909 if (cjni_env->reference_counter > 0)
1911 ERROR ("java plugin: cjni_jvm_env_destroy: "
1912 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1915 if (cjni_env->jvm_env != NULL)
1917 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1918 (void *) cjni_env->jvm_env);
1921 /* The pointer is allocated in `cjni_thread_attach' */
1923 } /* }}} void cjni_jvm_env_destroy */
1925 /* Register ``native'' functions with the JVM. Native functions are C-functions
1926 * that can be called by Java code. */
1927 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1929 jclass api_class_ptr;
1932 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1933 if (api_class_ptr == NULL)
1935 ERROR ("cjni_init_native: Cannot find the API class \"org.collectd.api"
1936 ".Collectd\". Please set the correct class path "
1937 "using 'JVMArg \"-Djava.class.path=...\"'.");
1941 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1942 jni_api_functions, (jint) jni_api_functions_num);
1945 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1950 } /* }}} int cjni_init_native */
1952 /* Create the JVM. This is called when the first thread tries to access the JVM
1953 * via cjni_thread_attach. */
1954 static int cjni_create_jvm (void) /* {{{ */
1957 JavaVMInitArgs vm_args;
1958 JavaVMOption vm_options[jvm_argc];
1966 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1969 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1970 "with status %i.", status);
1976 memset (&vm_args, 0, sizeof (vm_args));
1977 vm_args.version = JNI_VERSION_1_2;
1978 vm_args.options = vm_options;
1979 vm_args.nOptions = (jint) jvm_argc;
1981 for (i = 0; i < jvm_argc; i++)
1983 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
1985 vm_args.options[i].optionString = jvm_argv[i];
1988 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
1991 ERROR ("java plugin: cjni_create_jvm: "
1992 "JNI_CreateJavaVM failed with status %i.",
1996 assert (jvm != NULL);
1997 assert (jvm_env != NULL);
1999 /* Call RegisterNatives */
2000 status = cjni_init_native (jvm_env);
2003 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
2007 DEBUG ("java plugin: The JVM has been created.");
2009 } /* }}} int cjni_create_jvm */
2011 /* Increase the reference counter to the JVM for this thread. If it was zero,
2012 * attach the JVM first. */
2013 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2015 cjni_jvm_env_t *cjni_env;
2018 /* If we're the first thread to access the JVM, we'll have to create it
2024 status = cjni_create_jvm ();
2027 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2031 assert (jvm != NULL);
2033 cjni_env = pthread_getspecific (jvm_env_key);
2034 if (cjni_env == NULL)
2036 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2037 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
2038 if (cjni_env == NULL)
2040 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
2043 memset (cjni_env, 0, sizeof (*cjni_env));
2044 cjni_env->reference_counter = 0;
2045 cjni_env->jvm_env = NULL;
2047 pthread_setspecific (jvm_env_key, cjni_env);
2050 if (cjni_env->reference_counter > 0)
2052 cjni_env->reference_counter++;
2053 jvm_env = cjni_env->jvm_env;
2058 JavaVMAttachArgs args;
2060 assert (cjni_env->jvm_env == NULL);
2062 memset (&args, 0, sizeof (args));
2063 args.version = JNI_VERSION_1_2;
2065 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2068 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2069 "with status %i.", status);
2073 cjni_env->reference_counter = 1;
2074 cjni_env->jvm_env = jvm_env;
2077 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2078 cjni_env->reference_counter);
2079 assert (jvm_env != NULL);
2081 } /* }}} JNIEnv *cjni_thread_attach */
2083 /* Decrease the reference counter of this thread. If it reaches zero, detach
2085 static int cjni_thread_detach (void) /* {{{ */
2087 cjni_jvm_env_t *cjni_env;
2090 cjni_env = pthread_getspecific (jvm_env_key);
2091 if (cjni_env == NULL)
2093 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2097 assert (cjni_env->reference_counter > 0);
2098 assert (cjni_env->jvm_env != NULL);
2100 cjni_env->reference_counter--;
2101 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2102 cjni_env->reference_counter);
2104 if (cjni_env->reference_counter > 0)
2107 status = (*jvm)->DetachCurrentThread (jvm);
2110 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2111 "with status %i.", status);
2114 cjni_env->reference_counter = 0;
2115 cjni_env->jvm_env = NULL;
2118 } /* }}} JNIEnv *cjni_thread_attach */
2120 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2124 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2126 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2132 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2133 "`LoadPlugin' options! The JVM is already started and I have to "
2134 "ignore this argument: %s",
2135 ci->values[0].value.string);
2139 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2142 ERROR ("java plugin: realloc failed.");
2147 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2148 if (jvm_argv[jvm_argc] == NULL)
2150 ERROR ("java plugin: strdup failed.");
2156 } /* }}} int cjni_config_add_jvm_arg */
2158 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2161 java_plugin_class_t *class;
2162 jmethodID constructor_id;
2165 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2167 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2171 jvm_env = cjni_thread_attach ();
2172 if (jvm_env == NULL)
2175 class = (java_plugin_class_t *) realloc (java_classes_list,
2176 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2179 ERROR ("java plugin: realloc failed.");
2180 cjni_thread_detach ();
2183 java_classes_list = class;
2184 class = java_classes_list + java_classes_list_len;
2186 memset (class, 0, sizeof (*class));
2187 class->name = strdup (ci->values[0].value.string);
2188 if (class->name == NULL)
2190 ERROR ("java plugin: strdup failed.");
2191 cjni_thread_detach ();
2194 class->class = NULL;
2195 class->object = NULL;
2197 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2198 thorough the Java community, but (Sun's) `FindClass' and friends need
2201 for (i = 0; class->name[i] != 0; i++)
2202 if (class->name[i] == '.')
2203 class->name[i] = '/';
2206 DEBUG ("java plugin: Loading class %s", class->name);
2208 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2209 if (class->class == NULL)
2211 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2213 cjni_thread_detach ();
2218 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2220 if (constructor_id == NULL)
2222 ERROR ("java plugin: cjni_config_load_plugin: "
2223 "Could not find the constructor for `%s'.",
2225 cjni_thread_detach ();
2230 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2232 if (tmp_object != NULL)
2233 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2235 class->object = NULL;
2236 if (class->object == NULL)
2238 ERROR ("java plugin: cjni_config_load_plugin: "
2239 "Could create a new `%s' object.",
2241 cjni_thread_detach ();
2246 cjni_thread_detach ();
2248 java_classes_list_len++;
2251 } /* }}} int cjni_config_load_plugin */
2253 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2256 cjni_callback_info_t *cbi;
2264 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2266 WARNING ("java plugin: `Plugin' blocks "
2267 "need exactly one string argument.");
2271 name = ci->values[0].value.string;
2274 for (i = 0; i < java_callbacks_num; i++)
2276 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2279 if (strcmp (name, java_callbacks[i].name) != 0)
2282 cbi = java_callbacks + i;
2288 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2289 "configuration callback has been registered. Please make sure, the "
2290 "`LoadPlugin' lines precede the `Plugin' blocks.",
2295 DEBUG ("java plugin: Configuring %s", name);
2297 jvm_env = cjni_thread_attach ();
2298 if (jvm_env == NULL)
2301 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2302 if (o_ocitem == NULL)
2304 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2305 cjni_thread_detach ();
2309 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2310 method = (*jvm_env)->GetMethodID (jvm_env, class,
2311 "config", "(Lorg/collectd/api/OConfigItem;)I");
2313 (*jvm_env)->CallIntMethod (jvm_env,
2314 cbi->object, method, o_ocitem);
2316 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2317 cjni_thread_detach ();
2319 } /* }}} int cjni_config_plugin_block */
2321 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2331 for (i = 0; i < ci->children_num; i++)
2333 oconfig_item_t *child = ci->children + i;
2335 if (strcasecmp ("JVMArg", child->key) == 0)
2337 status = cjni_config_add_jvm_arg (child);
2343 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2345 status = cjni_config_load_plugin (child);
2351 else if (strcasecmp ("Plugin", child->key) == 0)
2353 status = cjni_config_plugin_block (child);
2361 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2366 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2367 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2369 if ((success == 0) && (errors > 0))
2371 ERROR ("java plugin: All statements failed.");
2376 } /* }}} int cjni_config_perform */
2378 /* Copy the children of `ci' to the global `config_block' variable. */
2379 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2381 oconfig_item_t *ci_copy;
2382 oconfig_item_t *tmp;
2384 assert (ci != NULL);
2385 if (ci->children_num == 0)
2386 return (0); /* nothing to do */
2388 ci_copy = oconfig_clone (ci);
2389 if (ci_copy == NULL)
2391 ERROR ("java plugin: oconfig_clone failed.");
2395 if (config_block == NULL)
2397 config_block = ci_copy;
2401 tmp = realloc (config_block->children,
2402 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2405 ERROR ("java plugin: realloc failed.");
2406 oconfig_free (ci_copy);
2409 config_block->children = tmp;
2411 /* Copy the pointers */
2412 memcpy (config_block->children + config_block->children_num,
2414 ci_copy->children_num * sizeof (*ci_copy->children));
2415 config_block->children_num += ci_copy->children_num;
2417 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2418 memset (ci_copy->children, 0,
2419 ci_copy->children_num * sizeof (*ci_copy->children));
2420 ci_copy->children_num = 0;
2422 oconfig_free (ci_copy);
2425 } /* }}} int cjni_config_callback */
2427 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2428 * and `cjni_write'. In particular, delete the global reference to the Java
2430 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2433 cjni_callback_info_t *cbi;
2435 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2437 cbi = (cjni_callback_info_t *) arg;
2439 /* This condition can occurr when shutting down. */
2449 jvm_env = cjni_thread_attach ();
2450 if (jvm_env == NULL)
2452 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2456 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2463 cjni_thread_detach ();
2464 } /* }}} void cjni_callback_info_destroy */
2466 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2467 static int cjni_read (user_data_t *ud) /* {{{ */
2470 cjni_callback_info_t *cbi;
2476 ERROR ("java plugin: cjni_read: jvm == NULL");
2480 if ((ud == NULL) || (ud->data == NULL))
2482 ERROR ("java plugin: cjni_read: Invalid user data.");
2486 jvm_env = cjni_thread_attach ();
2487 if (jvm_env == NULL)
2490 cbi = (cjni_callback_info_t *) ud->data;
2492 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2495 status = cjni_thread_detach ();
2498 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2502 return (ret_status);
2503 } /* }}} int cjni_read */
2505 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2506 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2510 cjni_callback_info_t *cbi;
2517 ERROR ("java plugin: cjni_write: jvm == NULL");
2521 if ((ud == NULL) || (ud->data == NULL))
2523 ERROR ("java plugin: cjni_write: Invalid user data.");
2527 jvm_env = cjni_thread_attach ();
2528 if (jvm_env == NULL)
2531 cbi = (cjni_callback_info_t *) ud->data;
2533 vl_java = ctoj_value_list (jvm_env, ds, vl);
2534 if (vl_java == NULL)
2536 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2540 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2541 cbi->object, cbi->method, vl_java);
2543 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2545 status = cjni_thread_detach ();
2548 ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2552 return (ret_status);
2553 } /* }}} int cjni_write */
2555 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2556 static int cjni_flush (cdtime_t timeout, const char *identifier, /* {{{ */
2560 cjni_callback_info_t *cbi;
2562 jobject o_identifier;
2568 ERROR ("java plugin: cjni_flush: jvm == NULL");
2572 if ((ud == NULL) || (ud->data == NULL))
2574 ERROR ("java plugin: cjni_flush: Invalid user data.");
2578 jvm_env = cjni_thread_attach ();
2579 if (jvm_env == NULL)
2582 cbi = (cjni_callback_info_t *) ud->data;
2584 o_timeout = ctoj_jdouble_to_number (jvm_env,
2585 (jdouble) CDTIME_T_TO_DOUBLE (timeout));
2586 if (o_timeout == NULL)
2588 ERROR ("java plugin: cjni_flush: Converting double "
2589 "to Number object failed.");
2593 o_identifier = NULL;
2594 if (identifier != NULL)
2596 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2597 if (o_identifier == NULL)
2599 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2600 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2605 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2606 cbi->object, cbi->method, o_timeout, o_identifier);
2608 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2609 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2611 status = cjni_thread_detach ();
2614 ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2618 return (ret_status);
2619 } /* }}} int cjni_flush */
2621 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2622 static void cjni_log (int severity, const char *message, /* {{{ */
2626 cjni_callback_info_t *cbi;
2632 if ((ud == NULL) || (ud->data == NULL))
2635 jvm_env = cjni_thread_attach ();
2636 if (jvm_env == NULL)
2639 cbi = (cjni_callback_info_t *) ud->data;
2641 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2642 if (o_message == NULL)
2645 (*jvm_env)->CallVoidMethod (jvm_env,
2646 cbi->object, cbi->method, (jint) severity, o_message);
2648 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2650 cjni_thread_detach ();
2651 } /* }}} void cjni_log */
2653 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2655 static int cjni_notification (const notification_t *n, /* {{{ */
2659 cjni_callback_info_t *cbi;
2660 jobject o_notification;
2666 ERROR ("java plugin: cjni_read: jvm == NULL");
2670 if ((ud == NULL) || (ud->data == NULL))
2672 ERROR ("java plugin: cjni_read: Invalid user data.");
2676 jvm_env = cjni_thread_attach ();
2677 if (jvm_env == NULL)
2680 cbi = (cjni_callback_info_t *) ud->data;
2682 o_notification = ctoj_notification (jvm_env, n);
2683 if (o_notification == NULL)
2685 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2689 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2690 cbi->object, cbi->method, o_notification);
2692 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2694 status = cjni_thread_detach ();
2697 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2701 return (ret_status);
2702 } /* }}} int cjni_notification */
2704 /* Callbacks for matches implemented in Java */
2705 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2709 cjni_callback_info_t *cbi_ret;
2710 cjni_callback_info_t *cbi_factory;
2721 #define BAIL_OUT(status) \
2722 if (cbi_ret != NULL) { \
2723 free (cbi_ret->name); \
2724 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2725 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2728 if (jvm_env != NULL) { \
2730 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2731 cjni_thread_detach (); \
2737 ERROR ("java plugin: cjni_read: jvm == NULL");
2741 jvm_env = cjni_thread_attach ();
2742 if (jvm_env == NULL)
2747 /* Find out whether to create a match or a target. */
2748 if (strcasecmp ("Match", ci->key) == 0)
2749 type = CB_TYPE_MATCH;
2750 else if (strcasecmp ("Target", ci->key) == 0)
2751 type = CB_TYPE_TARGET;
2754 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2755 "to create a match or a target.");
2759 /* This is the name of the match we should create. */
2760 name = ci->values[0].value.string;
2762 /* Lets see if we have a matching factory here.. */
2764 for (i = 0; i < java_callbacks_num; i++)
2766 if (java_callbacks[i].type != type)
2769 if (strcmp (name, java_callbacks[i].name) != 0)
2772 cbi_factory = java_callbacks + i;
2776 /* Nope, no factory for that name.. */
2777 if (cbi_factory == NULL)
2779 ERROR ("java plugin: cjni_match_target_create: "
2780 "No such match factory registered: %s",
2785 /* We convert `ci' to its Java equivalent.. */
2786 o_ci = ctoj_oconfig_item (jvm_env, ci);
2789 ERROR ("java plugin: cjni_match_target_create: "
2790 "ctoj_oconfig_item failed.");
2794 /* Allocate a new callback info structure. This is going to be our user_data
2796 cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
2797 if (cbi_ret == NULL)
2799 ERROR ("java plugin: cjni_match_target_create: malloc failed.");
2802 memset (cbi_ret, 0, sizeof (*cbi_ret));
2803 cbi_ret->object = NULL;
2804 cbi_ret->type = type;
2806 /* Lets fill the callback info structure.. First, the name: */
2807 cbi_ret->name = strdup (name);
2808 if (cbi_ret->name == NULL)
2810 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2814 /* Then call the factory method so it creates a new object for us. */
2815 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2816 cbi_factory->object, cbi_factory->method, o_ci);
2819 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2823 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2826 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2830 /* This is the class of the match. It is possibly different from the class of
2831 * the match-factory! */
2832 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2833 if (cbi_ret->class == NULL)
2835 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2839 /* Lookup the `int match (DataSet, ValueList)' method. */
2840 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2841 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2842 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2843 if (cbi_ret->method == NULL)
2845 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2849 /* Return the newly created match via the user_data pointer. */
2850 *user_data = (void *) cbi_ret;
2852 cjni_thread_detach ();
2854 DEBUG ("java plugin: cjni_match_target_create: "
2855 "Successfully created a `%s' %s.",
2856 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2861 } /* }}} int cjni_match_target_create */
2863 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2865 cjni_callback_info_destroy (*user_data);
2869 } /* }}} int cjni_match_target_destroy */
2871 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2872 value_list_t *vl, notification_meta_t **meta, void **user_data)
2875 cjni_callback_info_t *cbi;
2883 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2887 jvm_env = cjni_thread_attach ();
2888 if (jvm_env == NULL)
2891 cbi = (cjni_callback_info_t *) *user_data;
2893 o_vl = ctoj_value_list (jvm_env, ds, vl);
2896 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2897 cjni_thread_detach ();
2901 o_ds = ctoj_data_set (jvm_env, ds);
2904 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2905 cjni_thread_detach ();
2909 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2912 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2914 /* If we're executing a target, copy the `ValueList' back to our
2915 * `value_list_t'. */
2916 if (cbi->type == CB_TYPE_TARGET)
2918 value_list_t new_vl;
2920 memset (&new_vl, 0, sizeof (new_vl));
2921 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2924 ERROR ("java plugin: cjni_match_target_invoke: "
2925 "jtoc_value_list failed.");
2927 else /* if (status == 0) */
2929 /* plugin_dispatch_values assures that this is dynamically allocated
2933 /* This will replace the vl->values pointer to a new, dynamically
2934 * allocated piece of memory. */
2935 memcpy (vl, &new_vl, sizeof (*vl));
2937 } /* if (cbi->type == CB_TYPE_TARGET) */
2939 status = cjni_thread_detach ();
2941 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2943 return (ret_status);
2944 } /* }}} int cjni_match_target_invoke */
2946 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2947 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2952 for (i = 0; i < java_callbacks_num; i++)
2954 if (java_callbacks[i].type != CB_TYPE_INIT)
2957 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2959 status = (*jvm_env)->CallIntMethod (jvm_env,
2960 java_callbacks[i].object, java_callbacks[i].method);
2963 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2964 "Removing read function.",
2965 java_callbacks[i].name, status);
2966 plugin_unregister_read (java_callbacks[i].name);
2971 } /* }}} int cjni_init_plugins */
2973 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2974 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2979 for (i = 0; i < java_callbacks_num; i++)
2981 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2984 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2986 status = (*jvm_env)->CallIntMethod (jvm_env,
2987 java_callbacks[i].object, java_callbacks[i].method);
2990 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2991 java_callbacks[i].name, status);
2996 } /* }}} int cjni_shutdown_plugins */
2999 static int cjni_shutdown (void) /* {{{ */
3002 JavaVMAttachArgs args;
3010 memset (&args, 0, sizeof (args));
3011 args.version = JNI_VERSION_1_2;
3013 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
3016 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
3021 /* Execute all the shutdown functions registered by plugins. */
3022 cjni_shutdown_plugins (jvm_env);
3024 /* Release all the global references to callback functions */
3025 for (i = 0; i < java_callbacks_num; i++)
3027 if (java_callbacks[i].object != NULL)
3029 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3030 java_callbacks[i].object = NULL;
3032 sfree (java_callbacks[i].name);
3034 java_callbacks_num = 0;
3035 sfree (java_callbacks);
3037 /* Release all the global references to directly loaded classes. */
3038 for (i = 0; i < java_classes_list_len; i++)
3040 if (java_classes_list[i].object != NULL)
3042 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3043 java_classes_list[i].object = NULL;
3045 sfree (java_classes_list[i].name);
3047 java_classes_list_len = 0;
3048 sfree (java_classes_list);
3050 /* Destroy the JVM */
3051 DEBUG ("java plugin: Destroying the JVM.");
3052 (*jvm)->DestroyJavaVM (jvm);
3056 pthread_key_delete (jvm_env_key);
3058 /* Free the JVM argument list */
3059 for (i = 0; i < jvm_argc; i++)
3060 sfree (jvm_argv[i]);
3065 } /* }}} int cjni_shutdown */
3067 /* Initialization: Create a JVM, load all configured classes and call their
3068 * `config' and `init' callback methods. */
3069 static int cjni_init (void) /* {{{ */
3073 if ((config_block == NULL) && (jvm == NULL))
3075 ERROR ("java plugin: cjni_init: No configuration block for "
3076 "the java plugin was found.");
3080 if (config_block != NULL)
3083 cjni_config_perform (config_block);
3084 oconfig_free (config_block);
3085 config_block = NULL;
3090 ERROR ("java plugin: cjni_init: jvm == NULL");
3094 jvm_env = cjni_thread_attach ();
3095 if (jvm_env == NULL)
3098 cjni_init_plugins (jvm_env);
3100 cjni_thread_detach ();
3102 } /* }}} int cjni_init */
3104 void module_register (void)
3106 plugin_register_complex_config ("java", cjni_config_callback);
3107 plugin_register_init ("java", cjni_init);
3108 plugin_register_shutdown ("java", cjni_shutdown);
3109 } /* void module_register (void) */
3111 /* vim: set sw=2 sts=2 et fdm=marker : */