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;
99 * Mostly functions that are needed by the Java interface (``native'')
102 static void cjni_callback_info_destroy (void *arg);
103 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env,
104 jobject o_name, jobject o_callback, int type);
105 static int cjni_callback_register (JNIEnv *jvm_env, jobject o_name,
106 jobject o_callback, int type);
107 static int cjni_read (user_data_t *user_data);
108 static int cjni_write (const data_set_t *ds, const value_list_t *vl,
110 static int cjni_flush (int timeout, const char *identifier, user_data_t *ud);
111 static void cjni_log (int severity, const char *message, user_data_t *ud);
112 static int cjni_notification (const notification_t *n, user_data_t *ud);
114 static int cjni_match_create (const oconfig_item_t *ci, void **user_data);
115 static int cjni_match_destroy (void **user_data);
116 static int cjni_match_match (const data_set_t *ds, const value_list_t *vl,
117 notification_meta_t **meta, void **user_data);
120 * C to Java conversion functions
122 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
124 jclass class_ptr, jobject object_ptr, const char *method_name)
129 /* Create a java.lang.String */
130 o_string = (*jvm_env)->NewStringUTF (jvm_env,
131 (string != NULL) ? string : "");
132 if (o_string == NULL)
134 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
138 /* Search for the `void setFoo (String s)' method. */
139 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
140 method_name, "(Ljava/lang/String;)V");
143 ERROR ("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
145 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
149 /* Call the method. */
150 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, o_string);
152 /* Decrease reference counter on the java.lang.String object. */
153 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
156 } /* }}} int ctoj_string */
158 static int ctoj_int (JNIEnv *jvm_env, /* {{{ */
160 jclass class_ptr, jobject object_ptr, const char *method_name)
164 /* Search for the `void setFoo (int i)' method. */
165 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
166 method_name, "(I)V");
169 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
174 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
177 } /* }}} int ctoj_int */
179 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
181 jclass class_ptr, jobject object_ptr, const char *method_name)
185 /* Search for the `void setFoo (long l)' method. */
186 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
187 method_name, "(J)V");
190 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
195 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
198 } /* }}} int ctoj_long */
200 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
202 jclass class_ptr, jobject object_ptr, const char *method_name)
206 /* Search for the `void setFoo (double d)' method. */
207 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
208 method_name, "(D)V");
211 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
216 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
219 } /* }}} int ctoj_double */
221 /* Convert a jlong to a java.lang.Number */
222 static jobject ctoj_jlong_to_number (JNIEnv *jvm_env, jlong value) /* {{{ */
225 jmethodID m_long_constructor;
227 /* Look up the java.lang.Long class */
228 c_long = (*jvm_env)->FindClass (jvm_env, "java.lang.Long");
231 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
232 "java.lang.Long class failed.");
236 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
237 c_long, "<init>", "(J)V");
238 if (m_long_constructor == NULL)
240 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
241 "`Long (long)' constructor failed.");
245 return ((*jvm_env)->NewObject (jvm_env,
246 c_long, m_long_constructor, value));
247 } /* }}} jobject ctoj_jlong_to_number */
249 /* Convert a jdouble to a java.lang.Number */
250 static jobject ctoj_jdouble_to_number (JNIEnv *jvm_env, jdouble value) /* {{{ */
253 jmethodID m_double_constructor;
255 /* Look up the java.lang.Long class */
256 c_double = (*jvm_env)->FindClass (jvm_env, "java.lang.Double");
257 if (c_double == NULL)
259 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
260 "java.lang.Double class failed.");
264 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
265 c_double, "<init>", "(D)V");
266 if (m_double_constructor == NULL)
268 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
269 "`Double (double)' constructor failed.");
273 return ((*jvm_env)->NewObject (jvm_env,
274 c_double, m_double_constructor, value));
275 } /* }}} jobject ctoj_jdouble_to_number */
277 /* Convert a value_t to a java.lang.Number */
278 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
279 value_t value, int ds_type)
281 if (ds_type == DS_TYPE_COUNTER)
282 return (ctoj_jlong_to_number (jvm_env, (jlong) value.counter));
283 else if (ds_type == DS_TYPE_GAUGE)
284 return (ctoj_jdouble_to_number (jvm_env, (jdouble) value.gauge));
287 } /* }}} jobject ctoj_value_to_number */
289 /* Convert a data_source_t to a org.collectd.api.DataSource */
290 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
291 const data_source_t *dsrc)
294 jmethodID m_datasource_constructor;
295 jobject o_datasource;
298 /* Look up the DataSource class */
299 c_datasource = (*jvm_env)->FindClass (jvm_env,
300 "org.collectd.api.DataSource");
301 if (c_datasource == NULL)
303 ERROR ("java plugin: ctoj_data_source: "
304 "FindClass (org.collectd.api.DataSource) failed.");
308 /* Lookup the `ValueList ()' constructor. */
309 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
311 if (m_datasource_constructor == NULL)
313 ERROR ("java plugin: ctoj_data_source: Cannot find the "
314 "`DataSource ()' constructor.");
318 /* Create a new instance. */
319 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
320 m_datasource_constructor);
321 if (o_datasource == NULL)
323 ERROR ("java plugin: ctoj_data_source: "
324 "Creating a new DataSource instance failed.");
328 /* Set name via `void setName (String name)' */
329 status = ctoj_string (jvm_env, dsrc->name,
330 c_datasource, o_datasource, "setName");
333 ERROR ("java plugin: ctoj_data_source: "
334 "ctoj_string (setName) failed.");
335 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
339 /* Set type via `void setType (int type)' */
340 status = ctoj_int (jvm_env, dsrc->type,
341 c_datasource, o_datasource, "setType");
344 ERROR ("java plugin: ctoj_data_source: "
345 "ctoj_int (setType) failed.");
346 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
350 /* Set min via `void setMin (double min)' */
351 status = ctoj_double (jvm_env, dsrc->min,
352 c_datasource, o_datasource, "setMin");
355 ERROR ("java plugin: ctoj_data_source: "
356 "ctoj_double (setMin) failed.");
357 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
361 /* Set max via `void setMax (double max)' */
362 status = ctoj_double (jvm_env, dsrc->max,
363 c_datasource, o_datasource, "setMax");
366 ERROR ("java plugin: ctoj_data_source: "
367 "ctoj_double (setMax) failed.");
368 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
372 return (o_datasource);
373 } /* }}} jobject ctoj_data_source */
375 /* Convert a oconfig_value_t to a org.collectd.api.OConfigValue */
376 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
377 oconfig_value_t ocvalue)
380 jmethodID m_ocvalue_constructor;
384 m_ocvalue_constructor = NULL;
387 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
388 "org.collectd.api.OConfigValue");
389 if (c_ocvalue == NULL)
391 ERROR ("java plugin: ctoj_oconfig_value: "
392 "FindClass (org.collectd.api.OConfigValue) failed.");
396 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
398 jboolean tmp_boolean;
400 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
402 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
404 if (m_ocvalue_constructor == NULL)
406 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
407 "`OConfigValue (boolean)' constructor.");
411 return ((*jvm_env)->NewObject (jvm_env,
412 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
413 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
414 else if (ocvalue.type == OCONFIG_TYPE_STRING)
416 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
417 "<init>", "(Ljava/lang/String;)V");
418 if (m_ocvalue_constructor == NULL)
420 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
421 "`OConfigValue (String)' constructor.");
425 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
426 if (o_argument == NULL)
428 ERROR ("java plugin: ctoj_oconfig_value: "
429 "Creating a String object failed.");
433 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
435 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
436 "<init>", "(Ljava/lang/Number;)V");
437 if (m_ocvalue_constructor == NULL)
439 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
440 "`OConfigValue (Number)' constructor.");
444 o_argument = ctoj_jdouble_to_number (jvm_env,
445 (jdouble) ocvalue.value.number);
446 if (o_argument == NULL)
448 ERROR ("java plugin: ctoj_oconfig_value: "
449 "Creating a Number object failed.");
458 assert (m_ocvalue_constructor != NULL);
459 assert (o_argument != NULL);
461 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
462 c_ocvalue, m_ocvalue_constructor, o_argument);
463 if (o_ocvalue == NULL)
465 ERROR ("java plugin: ctoj_oconfig_value: "
466 "Creating an OConfigValue object failed.");
467 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
471 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
473 } /* }}} jobject ctoj_oconfig_value */
475 /* Convert a oconfig_item_t to a org.collectd.api.OConfigItem */
476 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
477 const oconfig_item_t *ci)
480 jmethodID m_ocitem_constructor;
481 jmethodID m_addvalue;
482 jmethodID m_addchild;
487 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.OConfigItem");
488 if (c_ocitem == NULL)
490 ERROR ("java plugin: ctoj_oconfig_item: "
491 "FindClass (org.collectd.api.OConfigItem) failed.");
495 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
497 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
498 "<init>", "(Ljava/lang/String;)V");
499 if (m_ocitem_constructor == NULL)
501 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
502 "`OConfigItem (String)' constructor.");
506 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
507 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
508 if (m_addvalue == NULL)
510 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
511 "`addValue (OConfigValue)' method.");
515 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
516 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
517 if (m_addchild == NULL)
519 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
520 "`addChild (OConfigItem)' method.");
525 /* Create a String object with the key.
526 * Needed for calling the constructor. */
527 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
530 ERROR ("java plugin: ctoj_oconfig_item: "
531 "Creating String object failed.");
535 /* Create an OConfigItem object */
536 o_ocitem = (*jvm_env)->NewObject (jvm_env,
537 c_ocitem, m_ocitem_constructor, o_key);
538 if (o_ocitem == NULL)
540 ERROR ("java plugin: ctoj_oconfig_item: "
541 "Creating an OConfigItem object failed.");
542 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
546 /* We don't need the String object any longer.. */
547 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
549 /* Call OConfigItem.addValue for each value */
550 for (i = 0; i < ci->values_num; i++) /* {{{ */
554 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
557 ERROR ("java plugin: ctoj_oconfig_item: "
558 "Creating an OConfigValue object failed.");
559 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
563 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
564 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
565 } /* }}} for (i = 0; i < ci->values_num; i++) */
567 /* Call OConfigItem.addChild for each child */
568 for (i = 0; i < ci->children_num; i++) /* {{{ */
572 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
575 ERROR ("java plugin: ctoj_oconfig_item: "
576 "Creating an OConfigItem object failed.");
577 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
581 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_child);
582 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
583 } /* }}} for (i = 0; i < ci->children_num; i++) */
586 } /* }}} jobject ctoj_oconfig_item */
588 /* Convert a data_set_t to a org.collectd.api.DataSet */
589 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
592 jmethodID m_constructor;
598 /* Look up the org.collectd.api.DataSet class */
599 c_dataset = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.DataSet");
600 if (c_dataset == NULL)
602 ERROR ("java plugin: ctoj_data_set: Looking up the "
603 "org.collectd.api.DataSet class failed.");
607 /* Search for the `DataSet (String type)' constructor. */
608 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
609 c_dataset, "<init>", "(Ljava.lang.String;)V");
610 if (m_constructor == NULL)
612 ERROR ("java plugin: ctoj_data_set: Looking up the "
613 "`DataSet (String)' constructor failed.");
617 /* Search for the `void addDataSource (DataSource)' method. */
618 m_add = (*jvm_env)->GetMethodID (jvm_env,
619 c_dataset, "addDataSource", "(Lorg.collectd.api.DataSource;)V");
622 ERROR ("java plugin: ctoj_data_set: Looking up the "
623 "`addDataSource (DataSource)' method failed.");
627 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
630 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
634 o_dataset = (*jvm_env)->NewObject (jvm_env,
635 c_dataset, m_constructor, o_type);
636 if (o_dataset == NULL)
638 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
639 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
643 /* Decrease reference counter on the java.lang.String object. */
644 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
646 for (i = 0; i < ds->ds_num; i++)
648 jobject o_datasource;
650 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
651 if (o_datasource == NULL)
653 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
654 ds->type, ds->ds[i].name);
655 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
659 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
661 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
662 } /* for (i = 0; i < ds->ds_num; i++) */
665 } /* }}} jobject ctoj_data_set */
667 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
668 value_t value, int ds_type,
669 jclass class_ptr, jobject object_ptr)
671 jmethodID m_addvalue;
674 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
675 "addValue", "(Ljava/lang/Number;)V");
676 if (m_addvalue == NULL)
678 ERROR ("java plugin: ctoj_value_list_add_value: "
679 "Cannot find method `void addValue (Number)'.");
683 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
684 if (o_number == NULL)
686 ERROR ("java plugin: ctoj_value_list_add_value: "
687 "ctoj_value_to_number failed.");
691 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
693 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
696 } /* }}} int ctoj_value_list_add_value */
698 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
699 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
701 jmethodID m_setdataset;
704 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
705 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
706 "setDataSet", "(Lorg.collectd.api.DataSet;)V");
707 if (m_setdataset == NULL)
709 ERROR ("java plugin: ctoj_value_list_add_data_set: "
710 "Cannot find the `void setDataSet (DataSet)' method.");
714 /* Create a DataSet object. */
715 o_dataset = ctoj_data_set (jvm_env, ds);
716 if (o_dataset == NULL)
718 ERROR ("java plugin: ctoj_value_list_add_data_set: "
719 "ctoj_data_set (%s) failed.", ds->type);
723 /* Actually call the method. */
724 (*jvm_env)->CallVoidMethod (jvm_env,
725 o_valuelist, m_setdataset, o_dataset);
727 /* Decrease reference counter on the List<DataSource> object. */
728 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
731 } /* }}} int ctoj_value_list_add_data_set */
733 /* Convert a value_list_t (and data_set_t) to a org.collectd.api.ValueList */
734 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
735 const data_set_t *ds, const value_list_t *vl)
738 jmethodID m_valuelist_constructor;
743 /* First, create a new ValueList instance..
744 * Look up the class.. */
745 c_valuelist = (*jvm_env)->FindClass (jvm_env,
746 "org.collectd.api.ValueList");
747 if (c_valuelist == NULL)
749 ERROR ("java plugin: ctoj_value_list: "
750 "FindClass (org.collectd.api.ValueList) failed.");
754 /* Lookup the `ValueList ()' constructor. */
755 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
757 if (m_valuelist_constructor == NULL)
759 ERROR ("java plugin: ctoj_value_list: Cannot find the "
760 "`ValueList ()' constructor.");
764 /* Create a new instance. */
765 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
766 m_valuelist_constructor);
767 if (o_valuelist == NULL)
769 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
774 status = ctoj_value_list_add_data_set (jvm_env,
775 c_valuelist, o_valuelist, ds);
778 ERROR ("java plugin: ctoj_value_list: "
779 "ctoj_value_list_add_data_set failed.");
780 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
784 /* Set the strings.. */
785 #define SET_STRING(str,method_name) do { \
786 status = ctoj_string (jvm_env, str, \
787 c_valuelist, o_valuelist, method_name); \
789 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
791 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
795 SET_STRING (vl->host, "setHost");
796 SET_STRING (vl->plugin, "setPlugin");
797 SET_STRING (vl->plugin_instance, "setPluginInstance");
798 SET_STRING (vl->type, "setType");
799 SET_STRING (vl->type_instance, "setTypeInstance");
803 /* Set the `time' member. Java stores time in milliseconds. */
804 status = ctoj_long (jvm_env, ((jlong) vl->time) * ((jlong) 1000),
805 c_valuelist, o_valuelist, "setTime");
808 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
809 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
813 /* Set the `interval' member.. */
814 status = ctoj_long (jvm_env, (jlong) vl->interval,
815 c_valuelist, o_valuelist, "setInterval");
818 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
819 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
823 for (i = 0; i < vl->values_len; i++)
825 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
826 c_valuelist, o_valuelist);
829 ERROR ("java plugin: ctoj_value_list: "
830 "ctoj_value_list_add_value failed.");
831 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
836 return (o_valuelist);
837 } /* }}} jobject ctoj_value_list */
839 /* Convert a notification_t to a org.collectd.api.Notification */
840 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
841 const notification_t *n)
843 jclass c_notification;
844 jmethodID m_constructor;
845 jobject o_notification;
848 /* First, create a new Notification instance..
849 * Look up the class.. */
850 c_notification = (*jvm_env)->FindClass (jvm_env,
851 "org.collectd.api.Notification");
852 if (c_notification == NULL)
854 ERROR ("java plugin: ctoj_notification: "
855 "FindClass (org.collectd.api.Notification) failed.");
859 /* Lookup the `Notification ()' constructor. */
860 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
862 if (m_constructor == NULL)
864 ERROR ("java plugin: ctoj_notification: Cannot find the "
865 "`Notification ()' constructor.");
869 /* Create a new instance. */
870 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
872 if (o_notification == NULL)
874 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
879 /* Set the strings.. */
880 #define SET_STRING(str,method_name) do { \
881 status = ctoj_string (jvm_env, str, \
882 c_notification, o_notification, method_name); \
884 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
886 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
890 SET_STRING (n->host, "setHost");
891 SET_STRING (n->plugin, "setPlugin");
892 SET_STRING (n->plugin_instance, "setPluginInstance");
893 SET_STRING (n->type, "setType");
894 SET_STRING (n->type_instance, "setTypeInstance");
895 SET_STRING (n->message, "setMessage");
899 /* Set the `time' member. Java stores time in milliseconds. */
900 status = ctoj_long (jvm_env, ((jlong) n->time) * ((jlong) 1000),
901 c_notification, o_notification, "setTime");
904 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
905 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
909 /* Set the `interval' member.. */
910 status = ctoj_int (jvm_env, (jint) n->severity,
911 c_notification, o_notification, "setSeverity");
914 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
915 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
919 return (o_notification);
920 } /* }}} jobject ctoj_notification */
923 * Java to C conversion functions
925 /* Call a `String <method> ()' method. */
926 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
927 char *buffer, size_t buffer_size, int empty_okay,
928 jclass class_ptr, jobject object_ptr, const char *method_name)
934 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
935 method_name, "()Ljava/lang/String;");
936 if (method_id == NULL)
938 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
943 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
944 if ((string_obj == NULL) && (empty_okay == 0))
946 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
950 else if ((string_obj == NULL) && (empty_okay != 0))
952 memset (buffer, 0, buffer_size);
956 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
959 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
960 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
964 sstrncpy (buffer, c_str, buffer_size);
966 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
967 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
970 } /* }}} int jtoc_string */
972 /* Call an `int <method> ()' method. */
973 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
975 jclass class_ptr, jobject object_ptr, const char *method_name)
979 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
981 if (method_id == NULL)
983 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
988 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
991 } /* }}} int jtoc_int */
993 /* Call a `long <method> ()' method. */
994 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
996 jclass class_ptr, jobject object_ptr, const char *method_name)
1000 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1001 method_name, "()J");
1002 if (method_id == NULL)
1004 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1009 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1012 } /* }}} int jtoc_long */
1014 /* Call a `double <method> ()' method. */
1015 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1017 jclass class_ptr, jobject object_ptr, const char *method_name)
1019 jmethodID method_id;
1021 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1022 method_name, "()D");
1023 if (method_id == NULL)
1025 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1030 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1033 } /* }}} int jtoc_double */
1035 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1036 value_t *ret_value, int ds_type, jobject object_ptr)
1041 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1043 if (ds_type == DS_TYPE_COUNTER)
1047 status = jtoc_long (jvm_env, &tmp_long,
1048 class_ptr, object_ptr, "longValue");
1051 ERROR ("java plugin: jtoc_value: "
1052 "jtoc_long failed.");
1055 (*ret_value).counter = (counter_t) tmp_long;
1061 status = jtoc_double (jvm_env, &tmp_double,
1062 class_ptr, object_ptr, "doubleValue");
1065 ERROR ("java plugin: jtoc_value: "
1066 "jtoc_double failed.");
1069 (*ret_value).gauge = (gauge_t) tmp_double;
1073 } /* }}} int jtoc_value */
1075 /* Read a List<Number>, convert it to `value_t' and add it to the given
1076 * `value_list_t'. */
1077 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1078 const data_set_t *ds, value_list_t *vl,
1079 jclass class_ptr, jobject object_ptr)
1081 jmethodID m_getvalues;
1082 jmethodID m_toarray;
1084 jobjectArray o_number_array;
1090 values_num = ds->ds_num;
1093 o_number_array = NULL;
1096 #define BAIL_OUT(status) \
1098 if (o_number_array != NULL) \
1099 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1100 if (o_list != NULL) \
1101 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1104 /* Call: List<Number> ValueList.getValues () */
1105 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1106 "getValues", "()Ljava/util/List;");
1107 if (m_getvalues == NULL)
1109 ERROR ("java plugin: jtoc_values_array: "
1110 "Cannot find method `List getValues ()'.");
1114 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1117 ERROR ("java plugin: jtoc_values_array: "
1118 "CallObjectMethod (getValues) failed.");
1122 /* Call: Number[] List.toArray () */
1123 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1124 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1125 "toArray", "()[Ljava/lang/Object;");
1126 if (m_toarray == NULL)
1128 ERROR ("java plugin: jtoc_values_array: "
1129 "Cannot find method `Object[] toArray ()'.");
1133 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1134 if (o_number_array == NULL)
1136 ERROR ("java plugin: jtoc_values_array: "
1137 "CallObjectMethod (toArray) failed.");
1141 values = (value_t *) calloc (values_num, sizeof (value_t));
1144 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1148 for (i = 0; i < values_num; i++)
1153 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1154 o_number_array, (jsize) i);
1155 if (o_number == NULL)
1157 ERROR ("java plugin: jtoc_values_array: "
1158 "GetObjectArrayElement (%i) failed.", i);
1162 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1165 ERROR ("java plugin: jtoc_values_array: "
1166 "jtoc_value (%i) failed.", i);
1169 } /* for (i = 0; i < values_num; i++) */
1171 vl->values = values;
1172 vl->values_len = values_num;
1175 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1176 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1178 } /* }}} int jtoc_values_array */
1180 /* Convert a org.collectd.api.ValueList to a value_list_t. */
1181 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1187 const data_set_t *ds;
1189 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1190 if (class_ptr == NULL)
1192 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1196 /* eo == empty okay */
1197 #define SET_STRING(buffer,method, eo) do { \
1198 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1199 class_ptr, object_ptr, method); \
1200 if (status != 0) { \
1201 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1206 SET_STRING(vl->type, "getType", /* empty = */ 0);
1208 ds = plugin_get_ds (vl->type);
1211 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1212 "Please consult the types.db(5) manpage for mor information.",
1217 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1218 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1219 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1220 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1224 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1227 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1230 /* Java measures time in milliseconds. */
1231 vl->time = (time_t) (tmp_long / ((jlong) 1000));
1233 status = jtoc_long (jvm_env, &tmp_long,
1234 class_ptr, object_ptr, "getInterval");
1237 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1240 vl->interval = (int) tmp_long;
1242 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1245 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1250 } /* }}} int jtoc_value_list */
1252 /* Convert a org.collectd.api.Notification to a notification_t. */
1253 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1261 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1262 if (class_ptr == NULL)
1264 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1268 /* eo == empty okay */
1269 #define SET_STRING(buffer,method, eo) do { \
1270 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1271 class_ptr, object_ptr, method); \
1272 if (status != 0) { \
1273 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1278 SET_STRING (n->host, "getHost", /* empty = */ 1);
1279 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1280 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1281 SET_STRING (n->type, "getType", /* empty = */ 1);
1282 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1283 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1287 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1290 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1293 /* Java measures time in milliseconds. */
1294 n->time = (time_t) (tmp_long / ((jlong) 1000));
1296 status = jtoc_int (jvm_env, &tmp_int,
1297 class_ptr, object_ptr, "getSeverity");
1300 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1303 n->severity = (int) tmp_int;
1306 } /* }}} int jtoc_notification */
1308 * Functions accessible from Java
1310 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1311 jobject this, jobject java_vl)
1313 value_list_t vl = VALUE_LIST_INIT;
1316 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1318 status = jtoc_value_list (jvm_env, &vl, java_vl);
1321 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1325 status = plugin_dispatch_values (&vl);
1330 } /* }}} jint cjni_api_dispatch_values */
1332 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1333 jobject this, jobject o_notification)
1338 memset (&n, 0, sizeof (n));
1341 status = jtoc_notification (jvm_env, &n, o_notification);
1344 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1348 status = plugin_dispatch_notification (&n);
1351 } /* }}} jint cjni_api_dispatch_notification */
1353 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1354 jobject this, jobject o_string_type)
1356 const char *ds_name;
1357 const data_set_t *ds;
1360 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1361 if (ds_name == NULL)
1363 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1367 ds = plugin_get_ds (ds_name);
1368 DEBUG ("java plugin: cjni_api_get_ds: "
1369 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1371 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1376 o_dataset = ctoj_data_set (jvm_env, ds);
1378 } /* }}} jint cjni_api_get_ds */
1380 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1381 jobject this, jobject o_name, jobject o_config)
1383 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1384 } /* }}} jint cjni_api_register_config */
1386 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1387 jobject this, jobject o_name, jobject o_config)
1389 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1390 } /* }}} jint cjni_api_register_init */
1392 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1393 jobject this, jobject o_name, jobject o_read)
1396 cjni_callback_info_t *cbi;
1398 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1402 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1404 memset (&ud, 0, sizeof (ud));
1405 ud.data = (void *) cbi;
1406 ud.free_func = cjni_callback_info_destroy;
1408 plugin_register_complex_read (cbi->name, cjni_read, &ud);
1410 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1413 } /* }}} jint cjni_api_register_read */
1415 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1416 jobject this, jobject o_name, jobject o_write)
1419 cjni_callback_info_t *cbi;
1421 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1425 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1427 memset (&ud, 0, sizeof (ud));
1428 ud.data = (void *) cbi;
1429 ud.free_func = cjni_callback_info_destroy;
1431 plugin_register_write (cbi->name, cjni_write, &ud);
1433 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1436 } /* }}} jint cjni_api_register_write */
1438 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1439 jobject this, jobject o_name, jobject o_flush)
1442 cjni_callback_info_t *cbi;
1444 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1448 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1450 memset (&ud, 0, sizeof (ud));
1451 ud.data = (void *) cbi;
1452 ud.free_func = cjni_callback_info_destroy;
1454 plugin_register_flush (cbi->name, cjni_flush, &ud);
1456 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1459 } /* }}} jint cjni_api_register_flush */
1461 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1462 jobject this, jobject o_name, jobject o_shutdown)
1464 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1466 } /* }}} jint cjni_api_register_shutdown */
1468 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1469 jobject this, jobject o_name, jobject o_log)
1472 cjni_callback_info_t *cbi;
1474 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1478 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1480 memset (&ud, 0, sizeof (ud));
1481 ud.data = (void *) cbi;
1482 ud.free_func = cjni_callback_info_destroy;
1484 plugin_register_log (cbi->name, cjni_log, &ud);
1486 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1489 } /* }}} jint cjni_api_register_log */
1491 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1492 jobject this, jobject o_name, jobject o_notification)
1495 cjni_callback_info_t *cbi;
1497 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1498 CB_TYPE_NOTIFICATION);
1502 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1504 memset (&ud, 0, sizeof (ud));
1505 ud.data = (void *) cbi;
1506 ud.free_func = cjni_callback_info_destroy;
1508 plugin_register_notification (cbi->name, cjni_notification, &ud);
1510 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1513 } /* }}} jint cjni_api_register_notification */
1515 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1516 jobject this, jobject o_name, jobject o_match)
1522 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1525 ERROR ("java plugin: cjni_api_register_match: "
1526 "GetStringUTFChars failed.");
1530 status = cjni_callback_register (jvm_env, o_name, o_match, CB_TYPE_MATCH);
1533 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1537 memset (&proc, 0, sizeof (proc));
1538 proc.create = cjni_match_create;
1539 proc.destroy = cjni_match_destroy;
1540 proc.match = cjni_match_match;
1542 status = fc_register_match (c_name, proc);
1545 ERROR ("java plugin: cjni_api_register_match: "
1546 "fc_register_match failed.");
1547 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1551 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1554 } /* }}} jint cjni_api_register_match */
1556 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1557 jobject this, jint severity, jobject o_message)
1561 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1564 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1568 if (severity < LOG_ERR)
1570 if (severity > LOG_DEBUG)
1571 severity = LOG_DEBUG;
1573 plugin_log (severity, "%s", c_str);
1575 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1576 } /* }}} void cjni_api_log */
1578 /* List of ``native'' functions, i. e. C-functions that can be called from
1580 static JNINativeMethod jni_api_functions[] = /* {{{ */
1583 "(Lorg/collectd/api/ValueList;)I",
1584 cjni_api_dispatch_values },
1586 { "dispatchNotification",
1587 "(Lorg/collectd/api/Notification;)I",
1588 cjni_api_dispatch_notification },
1591 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1595 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1596 cjni_api_register_config },
1599 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1600 cjni_api_register_init },
1603 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1604 cjni_api_register_read },
1607 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1608 cjni_api_register_write },
1611 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1612 cjni_api_register_flush },
1614 { "registerShutdown",
1615 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1616 cjni_api_register_shutdown },
1619 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1620 cjni_api_register_log },
1622 { "registerNotification",
1623 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1624 cjni_api_register_notification },
1627 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1628 cjni_api_register_match },
1631 "(ILjava/lang/String;)V",
1634 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1635 / sizeof (jni_api_functions[0]);
1641 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1642 * all registration functions. */
1643 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1644 jobject o_name, jobject o_callback, int type)
1647 cjni_callback_info_t *cbi;
1648 const char *method_name;
1649 const char *method_signature;
1653 case CB_TYPE_CONFIG:
1654 method_name = "config";
1655 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1659 method_name = "init";
1660 method_signature = "()I";
1664 method_name = "read";
1665 method_signature = "()I";
1669 method_name = "write";
1670 method_signature = "(Lorg/collectd/api/ValueList;)I";
1674 method_name = "flush";
1675 method_signature = "(ILjava/lang/String;)I";
1678 case CB_TYPE_SHUTDOWN:
1679 method_name = "shutdown";
1680 method_signature = "()I";
1684 method_name = "log";
1685 method_signature = "(ILjava/lang/String;)V";
1688 case CB_TYPE_NOTIFICATION:
1689 method_name = "notification";
1690 method_signature = "(Lorg/collectd/api/Notification;)I";
1694 method_name = "createMatch";
1695 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1696 "Lorg/collectd/api/CollectdMatchInterface;";
1700 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1705 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1708 ERROR ("java plugin: cjni_callback_info_create: "
1709 "GetStringUTFChars failed.");
1713 cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1716 ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1717 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1720 memset (cbi, 0, sizeof (*cbi));
1723 cbi->name = strdup (c_name);
1724 if (cbi->name == NULL)
1726 pthread_mutex_unlock (&java_callbacks_lock);
1727 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1728 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1732 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1734 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, o_callback);
1735 if (cbi->class == NULL)
1737 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1742 cbi->object = o_callback;
1744 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1745 method_name, method_signature);
1746 if (cbi->method == NULL)
1748 ERROR ("java plugin: cjni_callback_info_create: "
1749 "Cannot find the `%s' method with signature `%s'.",
1750 method_name, method_signature);
1755 (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1758 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1760 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1761 * to the global `java_callbacks' variable. This is used for `config', `init',
1762 * and `shutdown' callbacks. */
1763 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1764 jobject o_name, jobject o_callback, int type)
1766 cjni_callback_info_t *cbi;
1767 cjni_callback_info_t *tmp;
1769 const char *type_str;
1772 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1779 case CB_TYPE_CONFIG:
1780 type_str = "config";
1787 case CB_TYPE_SHUTDOWN:
1788 type_str = "shutdown";
1796 type_str = "<unknown>";
1798 DEBUG ("java plugin: Registering new %s callback: %s",
1799 type_str, cbi->name);
1802 pthread_mutex_lock (&java_callbacks_lock);
1804 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1805 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1808 pthread_mutex_unlock (&java_callbacks_lock);
1809 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1811 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1816 java_callbacks = tmp;
1817 java_callbacks[java_callbacks_num] = *cbi;
1818 java_callbacks_num++;
1820 pthread_mutex_unlock (&java_callbacks_lock);
1824 } /* }}} int cjni_callback_register */
1826 /* Callback for `pthread_key_create'. It frees the data contained in
1827 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1828 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1830 cjni_jvm_env_t *cjni_env;
1835 cjni_env = (cjni_jvm_env_t *) args;
1837 if (cjni_env->reference_counter > 0)
1839 ERROR ("java plugin: cjni_jvm_env_destroy: "
1840 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1843 if (cjni_env->jvm_env != NULL)
1845 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1846 (void *) cjni_env->jvm_env);
1849 /* The pointer is allocated in `cjni_thread_attach' */
1851 } /* }}} void cjni_jvm_env_destroy */
1853 /* Register ``native'' functions with the JVM. Native functions are C-functions
1854 * that can be called by Java code. */
1855 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1857 jclass api_class_ptr;
1860 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.Collectd");
1861 if (api_class_ptr == NULL)
1863 ERROR ("cjni_init_native: Cannot find API class `org.collectd.api.Collectd'.");
1867 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1868 jni_api_functions, (jint) jni_api_functions_num);
1871 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1876 } /* }}} int cjni_init_native */
1878 /* Create the JVM. This is called when the first thread tries to access the JVM
1879 * via cjni_thread_attach. */
1880 static int cjni_create_jvm (void) /* {{{ */
1883 JavaVMInitArgs vm_args;
1884 JavaVMOption vm_options[jvm_argc];
1892 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1895 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1896 "with status %i.", status);
1902 memset (&vm_args, 0, sizeof (vm_args));
1903 vm_args.version = JNI_VERSION_1_2;
1904 vm_args.options = vm_options;
1905 vm_args.nOptions = (jint) jvm_argc;
1907 for (i = 0; i < jvm_argc; i++)
1909 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
1911 vm_args.options[i].optionString = jvm_argv[i];
1914 vm_args.options[0].optionString = "-verbose:jni";
1915 vm_args.options[1].optionString = "-Djava.class.path=/home/octo/collectd/bindings/java";
1918 status = JNI_CreateJavaVM (&jvm, (void **) &jvm_env, (void **) &vm_args);
1921 ERROR ("java plugin: cjni_create_jvm: "
1922 "JNI_CreateJavaVM failed with status %i.",
1926 assert (jvm != NULL);
1927 assert (jvm_env != NULL);
1929 /* Call RegisterNatives */
1930 status = cjni_init_native (jvm_env);
1933 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
1937 DEBUG ("java plugin: The JVM has been created.");
1939 } /* }}} int cjni_create_jvm */
1941 /* Increase the reference counter to the JVM for this thread. If it was zero,
1942 * attach the JVM first. */
1943 static JNIEnv *cjni_thread_attach (void) /* {{{ */
1945 cjni_jvm_env_t *cjni_env;
1948 /* If we're the first thread to access the JVM, we'll have to create it
1954 status = cjni_create_jvm ();
1957 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
1961 assert (jvm != NULL);
1963 cjni_env = pthread_getspecific (jvm_env_key);
1964 if (cjni_env == NULL)
1966 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
1967 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
1968 if (cjni_env == NULL)
1970 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
1973 memset (cjni_env, 0, sizeof (*cjni_env));
1974 cjni_env->reference_counter = 0;
1975 cjni_env->jvm_env = NULL;
1977 pthread_setspecific (jvm_env_key, cjni_env);
1980 if (cjni_env->reference_counter > 0)
1982 cjni_env->reference_counter++;
1983 jvm_env = cjni_env->jvm_env;
1988 JavaVMAttachArgs args;
1990 assert (cjni_env->jvm_env == NULL);
1992 memset (&args, 0, sizeof (args));
1993 args.version = JNI_VERSION_1_2;
1995 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
1998 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
1999 "with status %i.", status);
2003 cjni_env->reference_counter = 1;
2004 cjni_env->jvm_env = jvm_env;
2007 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2008 cjni_env->reference_counter);
2009 assert (jvm_env != NULL);
2011 } /* }}} JNIEnv *cjni_thread_attach */
2013 /* Decrease the reference counter of this thread. If it reaches zero, detach
2015 static int cjni_thread_detach (void) /* {{{ */
2017 cjni_jvm_env_t *cjni_env;
2020 cjni_env = pthread_getspecific (jvm_env_key);
2021 if (cjni_env == NULL)
2023 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2027 assert (cjni_env->reference_counter > 0);
2028 assert (cjni_env->jvm_env != NULL);
2030 cjni_env->reference_counter--;
2031 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2032 cjni_env->reference_counter);
2034 if (cjni_env->reference_counter > 0)
2037 status = (*jvm)->DetachCurrentThread (jvm);
2040 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2041 "with status %i.", status);
2044 cjni_env->reference_counter = 0;
2045 cjni_env->jvm_env = NULL;
2048 } /* }}} JNIEnv *cjni_thread_attach */
2050 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2054 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2056 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2062 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2063 "`LoadPlugin' options! The JVM is already started and I have to "
2064 "ignore this argument: %s",
2065 ci->values[0].value.string);
2069 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2072 ERROR ("java plugin: realloc failed.");
2077 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2078 if (jvm_argv[jvm_argc] == NULL)
2080 ERROR ("java plugin: strdup failed.");
2086 } /* }}} int cjni_config_add_jvm_arg */
2088 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2091 java_plugin_class_t *class;
2092 jmethodID constructor_id;
2094 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2096 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2100 jvm_env = cjni_thread_attach ();
2101 if (jvm_env == NULL)
2104 class = (java_plugin_class_t *) realloc (java_classes_list,
2105 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2108 ERROR ("java plugin: realloc failed.");
2109 cjni_thread_detach ();
2112 java_classes_list = class;
2113 class = java_classes_list + java_classes_list_len;
2115 memset (class, 0, sizeof (*class));
2116 class->name = strdup (ci->values[0].value.string);
2117 if (class->name == NULL)
2119 ERROR ("java plugin: strdup failed.");
2120 cjni_thread_detach ();
2123 class->class = NULL;
2124 class->object = NULL;
2126 DEBUG ("java plugin: Loading class %s", class->name);
2128 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2129 if (class->class == NULL)
2131 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2133 cjni_thread_detach ();
2138 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2140 if (constructor_id == NULL)
2142 ERROR ("java plugin: cjni_config_load_plugin: "
2143 "Could not find the constructor for `%s'.",
2145 cjni_thread_detach ();
2150 class->object = (*jvm_env)->NewObject (jvm_env, class->class,
2152 if (class->object == NULL)
2154 ERROR ("java plugin: cjni_config_load_plugin: "
2155 "Could create a new `%s' object.",
2157 cjni_thread_detach ();
2162 (*jvm_env)->NewGlobalRef (jvm_env, class->object);
2163 cjni_thread_detach ();
2165 java_classes_list_len++;
2168 } /* }}} int cjni_config_load_plugin */
2170 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2173 cjni_callback_info_t *cbi;
2179 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2181 WARNING ("java plugin: `Plugin' blocks "
2182 "need exactly one string argument.");
2186 name = ci->values[0].value.string;
2189 for (i = 0; i < java_callbacks_num; i++)
2191 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2194 if (strcmp (name, java_callbacks[i].name) != 0)
2197 cbi = java_callbacks + i;
2203 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2204 "configuration callback has been registered. Please make sure, the "
2205 "`LoadPlugin' lines precede the `Plugin' blocks.",
2210 DEBUG ("java plugin: Configuring %s", name);
2212 jvm_env = cjni_thread_attach ();
2213 if (jvm_env == NULL)
2216 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2217 if (o_ocitem == NULL)
2219 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2220 cjni_thread_detach ();
2224 status = (*jvm_env)->CallIntMethod (jvm_env,
2225 cbi->object, cbi->method, o_ocitem);
2227 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2228 cjni_thread_detach ();
2230 } /* }}} int cjni_config_plugin_block */
2232 static int cjni_config (oconfig_item_t *ci) /* {{{ */
2242 for (i = 0; i < ci->children_num; i++)
2244 oconfig_item_t *child = ci->children + i;
2246 if (strcasecmp ("JVMArg", child->key) == 0)
2248 status = cjni_config_add_jvm_arg (child);
2254 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2256 status = cjni_config_load_plugin (child);
2262 else if (strcasecmp ("Plugin", child->key) == 0)
2264 status = cjni_config_plugin_block (child);
2272 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2277 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2278 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2280 if ((success == 0) && (errors > 0))
2282 ERROR ("java plugin: All statements failed.");
2287 } /* }}} int cjni_config */
2289 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2290 * and `cjni_write'. In particular, delete the global reference to the Java
2292 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2295 cjni_callback_info_t *cbi;
2297 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2299 cbi = (cjni_callback_info_t *) arg;
2301 /* This condition can occurr when shutting down. */
2311 jvm_env = cjni_thread_attach ();
2312 if (jvm_env == NULL)
2314 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2318 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2325 cjni_thread_detach ();
2326 } /* }}} void cjni_callback_info_destroy */
2328 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2329 static int cjni_read (user_data_t *ud) /* {{{ */
2332 cjni_callback_info_t *cbi;
2338 ERROR ("java plugin: cjni_read: jvm == NULL");
2342 if ((ud == NULL) || (ud->data == NULL))
2344 ERROR ("java plugin: cjni_read: Invalid user data.");
2348 jvm_env = cjni_thread_attach ();
2349 if (jvm_env == NULL)
2352 cbi = (cjni_callback_info_t *) ud->data;
2354 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2357 status = cjni_thread_detach ();
2360 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2364 return (ret_status);
2365 } /* }}} int cjni_read */
2367 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2368 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2372 cjni_callback_info_t *cbi;
2379 ERROR ("java plugin: cjni_write: jvm == NULL");
2383 if ((ud == NULL) || (ud->data == NULL))
2385 ERROR ("java plugin: cjni_write: Invalid user data.");
2389 jvm_env = cjni_thread_attach ();
2390 if (jvm_env == NULL)
2393 cbi = (cjni_callback_info_t *) ud->data;
2395 vl_java = ctoj_value_list (jvm_env, ds, vl);
2396 if (vl_java == NULL)
2398 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2402 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2403 cbi->object, cbi->method, vl_java);
2405 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2407 status = cjni_thread_detach ();
2410 ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2414 return (ret_status);
2415 } /* }}} int cjni_write */
2417 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2418 static int cjni_flush (int timeout, const char *identifier, /* {{{ */
2422 cjni_callback_info_t *cbi;
2423 jobject o_identifier;
2429 ERROR ("java plugin: cjni_flush: jvm == NULL");
2433 if ((ud == NULL) || (ud->data == NULL))
2435 ERROR ("java plugin: cjni_flush: Invalid user data.");
2439 jvm_env = cjni_thread_attach ();
2440 if (jvm_env == NULL)
2443 cbi = (cjni_callback_info_t *) ud->data;
2445 o_identifier = NULL;
2446 if (identifier != NULL)
2448 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2449 if (o_identifier == NULL)
2451 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2456 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2457 cbi->object, cbi->method, (jint) timeout, o_identifier);
2459 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2461 status = cjni_thread_detach ();
2464 ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2468 return (ret_status);
2469 } /* }}} int cjni_flush */
2471 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2472 static void cjni_log (int severity, const char *message, /* {{{ */
2476 cjni_callback_info_t *cbi;
2482 if ((ud == NULL) || (ud->data == NULL))
2485 jvm_env = cjni_thread_attach ();
2486 if (jvm_env == NULL)
2489 cbi = (cjni_callback_info_t *) ud->data;
2491 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2492 if (o_message == NULL)
2495 (*jvm_env)->CallVoidMethod (jvm_env,
2496 cbi->object, cbi->method, (jint) severity, o_message);
2498 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2500 cjni_thread_detach ();
2501 } /* }}} void cjni_log */
2503 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2505 static int cjni_notification (const notification_t *n, /* {{{ */
2509 cjni_callback_info_t *cbi;
2510 jobject o_notification;
2516 ERROR ("java plugin: cjni_read: jvm == NULL");
2520 if ((ud == NULL) || (ud->data == NULL))
2522 ERROR ("java plugin: cjni_read: Invalid user data.");
2526 jvm_env = cjni_thread_attach ();
2527 if (jvm_env == NULL)
2530 cbi = (cjni_callback_info_t *) ud->data;
2532 o_notification = ctoj_notification (jvm_env, n);
2533 if (o_notification == NULL)
2535 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2539 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2540 cbi->object, cbi->method, o_notification);
2542 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2544 status = cjni_thread_detach ();
2547 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2551 return (ret_status);
2552 } /* }}} int cjni_notification */
2554 /* Callbacks for matches implemented in Java */
2555 static int cjni_match_create (const oconfig_item_t *ci, /* {{{ */
2559 cjni_callback_info_t *cbi_ret;
2560 cjni_callback_info_t *cbi_factory;
2569 #define BAIL_OUT(status) \
2570 if (cbi_ret != NULL) { \
2571 free (cbi_ret->name); \
2572 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2573 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2576 if (jvm_env != NULL) { \
2578 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2579 cjni_thread_detach (); \
2585 ERROR ("java plugin: cjni_read: jvm == NULL");
2589 jvm_env = cjni_thread_attach ();
2590 if (jvm_env == NULL)
2595 /* This is the name of the match we should create. */
2596 name = ci->values[0].value.string;
2598 /* Lets see if we have a matching factory here.. */
2600 for (i = 0; i < java_callbacks_num; i++)
2602 if (java_callbacks[i].type != CB_TYPE_MATCH)
2605 if (strcmp (name, java_callbacks[i].name) != 0)
2608 cbi_factory = java_callbacks + i;
2612 /* Nope, no factory for that name.. */
2613 if (cbi_factory == NULL)
2615 ERROR ("java plugin: cjni_match_create: "
2616 "No such match factory registered: %s",
2621 /* We convert `ci' to its Java equivalent.. */
2622 o_ci = ctoj_oconfig_item (jvm_env, ci);
2625 ERROR ("java plugin: cjni_match_create: ctoj_oconfig_item failed.");
2629 /* Allocate a new callback info structure. This is going to be our user_data
2631 cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
2632 if (cbi_ret == NULL)
2634 ERROR ("java plugin: cjni_match_create: ctoj_oconfig_item failed.");
2637 memset (cbi_ret, 0, sizeof (*cbi_ret));
2638 cbi_ret->object = NULL;
2640 /* Lets fill the callback info structure.. First, the name: */
2641 cbi_ret->name = strdup (name);
2642 if (cbi_ret->name == NULL)
2644 ERROR ("java plugin: cjni_match_create: strdup failed.");
2648 /* Then call the factory method so it creates a new object for us. */
2649 cbi_ret->object = (*jvm_env)->CallObjectMethod (jvm_env,
2650 cbi_factory->object, cbi_factory->method, o_ci);
2651 if (cbi_ret->object == NULL)
2653 ERROR ("java plugin: cjni_match_create: CallObjectMethod failed.");
2657 /* This is the class of the match. It is possibly different from the class of
2658 * the match-factory! */
2659 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2660 if (cbi_ret->class == NULL)
2662 ERROR ("java plugin: cjni_match_create: GetObjectClass failed.");
2666 /* Lookup the `int match (DataSet, ValueList)' method. */
2667 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2668 "match", "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2669 if (cbi_ret->method == NULL)
2671 ERROR ("java plugin: cjni_match_create: GetMethodID failed.");
2675 /* We have everything we hoped for. Now we add a new global reference so this
2676 * match isn't freed immediately after we return.. */
2677 (*jvm_env)->NewGlobalRef (jvm_env, cbi_ret->object);
2679 /* Return the newly created match via the user_data pointer. */
2680 *user_data = (void *) cbi_ret;
2682 cjni_thread_detach ();
2684 DEBUG ("java plugin: cjni_match_create: Successfully created a `%s' match.",
2690 } /* }}} int cjni_match_create */
2692 static int cjni_match_destroy (void **user_data) /* {{{ */
2694 cjni_callback_info_destroy (*user_data);
2698 } /* }}} int cjni_match_destroy */
2700 static int cjni_match_match (const data_set_t *ds, /* {{{ */
2701 const value_list_t *vl, notification_meta_t **meta, void **user_data)
2704 cjni_callback_info_t *cbi;
2712 ERROR ("java plugin: cjni_match_match: jvm == NULL");
2716 jvm_env = cjni_thread_attach ();
2717 if (jvm_env == NULL)
2720 cbi = (cjni_callback_info_t *) *user_data;
2722 o_vl = ctoj_value_list (jvm_env, ds, vl);
2725 ERROR ("java plugin: cjni_match_match: ctoj_value_list failed.");
2726 cjni_thread_detach ();
2730 o_ds = ctoj_data_set (jvm_env, ds);
2733 ERROR ("java plugin: cjni_match_match: ctoj_value_list failed.");
2734 cjni_thread_detach ();
2738 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2741 DEBUG ("java plugin: cjni_match_match: Method returned %i.", ret_status);
2743 status = cjni_thread_detach ();
2745 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2747 return (ret_status);
2748 } /* }}} int cjni_match_match */
2750 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2751 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2756 for (i = 0; i < java_callbacks_num; i++)
2758 if (java_callbacks[i].type != CB_TYPE_INIT)
2761 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2763 status = (*jvm_env)->CallIntMethod (jvm_env,
2764 java_callbacks[i].object, java_callbacks[i].method);
2767 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2768 "Removing read function.",
2769 java_callbacks[i].name, status);
2770 plugin_unregister_read (java_callbacks[i].name);
2775 } /* }}} int cjni_init_plugins */
2777 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2778 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2783 for (i = 0; i < java_callbacks_num; i++)
2785 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2788 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2790 status = (*jvm_env)->CallIntMethod (jvm_env,
2791 java_callbacks[i].object, java_callbacks[i].method);
2794 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2795 java_callbacks[i].name, status);
2800 } /* }}} int cjni_shutdown_plugins */
2803 static int cjni_shutdown (void) /* {{{ */
2806 JavaVMAttachArgs args;
2814 memset (&args, 0, sizeof (args));
2815 args.version = JNI_VERSION_1_2;
2817 status = (*jvm)->AttachCurrentThread (jvm, (void **) &jvm_env, &args);
2820 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2825 /* Execute all the shutdown functions registered by plugins. */
2826 cjni_shutdown_plugins (jvm_env);
2828 /* Release all the global references to callback functions */
2829 for (i = 0; i < java_callbacks_num; i++)
2831 if (java_callbacks[i].object != NULL)
2833 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
2834 java_callbacks[i].object = NULL;
2836 sfree (java_callbacks[i].name);
2838 java_callbacks_num = 0;
2839 sfree (java_callbacks);
2841 /* Release all the global references to directly loaded classes. */
2842 for (i = 0; i < java_classes_list_len; i++)
2844 if (java_classes_list[i].object != NULL)
2846 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
2847 java_classes_list[i].object = NULL;
2849 sfree (java_classes_list[i].name);
2851 java_classes_list_len = 0;
2852 sfree (java_classes_list);
2854 /* Destroy the JVM */
2855 DEBUG ("java plugin: Destroying the JVM.");
2856 (*jvm)->DestroyJavaVM (jvm);
2860 pthread_key_delete (jvm_env_key);
2862 /* Free the JVM argument list */
2863 for (i = 0; i < jvm_argc; i++)
2864 sfree (jvm_argv[i]);
2869 } /* }}} int cjni_shutdown */
2871 /* Initialization: Create a JVM, load all configured classes and call their
2872 * `config' and `init' callback methods. */
2873 static int cjni_init (void) /* {{{ */
2879 ERROR ("java plugin: cjni_match_match: jvm == NULL");
2883 jvm_env = cjni_thread_attach ();
2884 if (jvm_env == NULL)
2887 cjni_init_plugins (jvm_env);
2889 cjni_thread_detach ();
2891 } /* }}} int cjni_init */
2893 void module_register (void)
2895 plugin_register_complex_config ("java", cjni_config);
2896 plugin_register_init ("java", cjni_init);
2897 plugin_register_shutdown ("java", cjni_shutdown);
2898 } /* void module_register (void) */
2900 /* vim: set sw=2 sts=2 et fdm=marker : */