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 /* Create, destroy, and match/invoke functions, used by both, matches AND
116 static int cjni_match_target_create (const oconfig_item_t *ci, void **user_data);
117 static int cjni_match_target_destroy (void **user_data);
118 static int cjni_match_target_invoke (const data_set_t *ds, value_list_t *vl,
119 notification_meta_t **meta, void **user_data);
122 * C to Java conversion functions
124 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
126 jclass class_ptr, jobject object_ptr, const char *method_name)
131 /* Create a java.lang.String */
132 o_string = (*jvm_env)->NewStringUTF (jvm_env,
133 (string != NULL) ? string : "");
134 if (o_string == NULL)
136 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
140 /* Search for the `void setFoo (String s)' method. */
141 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
142 method_name, "(Ljava/lang/String;)V");
145 ERROR ("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
147 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
151 /* Call the method. */
152 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, o_string);
154 /* Decrease reference counter on the java.lang.String object. */
155 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
158 } /* }}} int ctoj_string */
160 static int ctoj_int (JNIEnv *jvm_env, /* {{{ */
162 jclass class_ptr, jobject object_ptr, const char *method_name)
166 /* Search for the `void setFoo (int i)' method. */
167 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
168 method_name, "(I)V");
171 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
176 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
179 } /* }}} int ctoj_int */
181 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
183 jclass class_ptr, jobject object_ptr, const char *method_name)
187 /* Search for the `void setFoo (long l)' method. */
188 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
189 method_name, "(J)V");
192 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
197 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
200 } /* }}} int ctoj_long */
202 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
204 jclass class_ptr, jobject object_ptr, const char *method_name)
208 /* Search for the `void setFoo (double d)' method. */
209 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
210 method_name, "(D)V");
213 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
218 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
221 } /* }}} int ctoj_double */
223 /* Convert a jlong to a java.lang.Number */
224 static jobject ctoj_jlong_to_number (JNIEnv *jvm_env, jlong value) /* {{{ */
227 jmethodID m_long_constructor;
229 /* Look up the java.lang.Long class */
230 c_long = (*jvm_env)->FindClass (jvm_env, "java.lang.Long");
233 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
234 "java.lang.Long class failed.");
238 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
239 c_long, "<init>", "(J)V");
240 if (m_long_constructor == NULL)
242 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
243 "`Long (long)' constructor failed.");
247 return ((*jvm_env)->NewObject (jvm_env,
248 c_long, m_long_constructor, value));
249 } /* }}} jobject ctoj_jlong_to_number */
251 /* Convert a jdouble to a java.lang.Number */
252 static jobject ctoj_jdouble_to_number (JNIEnv *jvm_env, jdouble value) /* {{{ */
255 jmethodID m_double_constructor;
257 /* Look up the java.lang.Long class */
258 c_double = (*jvm_env)->FindClass (jvm_env, "java.lang.Double");
259 if (c_double == NULL)
261 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
262 "java.lang.Double class failed.");
266 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
267 c_double, "<init>", "(D)V");
268 if (m_double_constructor == NULL)
270 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
271 "`Double (double)' constructor failed.");
275 return ((*jvm_env)->NewObject (jvm_env,
276 c_double, m_double_constructor, value));
277 } /* }}} jobject ctoj_jdouble_to_number */
279 /* Convert a value_t to a java.lang.Number */
280 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
281 value_t value, int ds_type)
283 if (ds_type == DS_TYPE_COUNTER)
284 return (ctoj_jlong_to_number (jvm_env, (jlong) value.counter));
285 else if (ds_type == DS_TYPE_GAUGE)
286 return (ctoj_jdouble_to_number (jvm_env, (jdouble) value.gauge));
289 } /* }}} jobject ctoj_value_to_number */
291 /* Convert a data_source_t to a org.collectd.api.DataSource */
292 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
293 const data_source_t *dsrc)
296 jmethodID m_datasource_constructor;
297 jobject o_datasource;
300 /* Look up the DataSource class */
301 c_datasource = (*jvm_env)->FindClass (jvm_env,
302 "org.collectd.api.DataSource");
303 if (c_datasource == NULL)
305 ERROR ("java plugin: ctoj_data_source: "
306 "FindClass (org.collectd.api.DataSource) failed.");
310 /* Lookup the `ValueList ()' constructor. */
311 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
313 if (m_datasource_constructor == NULL)
315 ERROR ("java plugin: ctoj_data_source: Cannot find the "
316 "`DataSource ()' constructor.");
320 /* Create a new instance. */
321 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
322 m_datasource_constructor);
323 if (o_datasource == NULL)
325 ERROR ("java plugin: ctoj_data_source: "
326 "Creating a new DataSource instance failed.");
330 /* Set name via `void setName (String name)' */
331 status = ctoj_string (jvm_env, dsrc->name,
332 c_datasource, o_datasource, "setName");
335 ERROR ("java plugin: ctoj_data_source: "
336 "ctoj_string (setName) failed.");
337 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
341 /* Set type via `void setType (int type)' */
342 status = ctoj_int (jvm_env, dsrc->type,
343 c_datasource, o_datasource, "setType");
346 ERROR ("java plugin: ctoj_data_source: "
347 "ctoj_int (setType) failed.");
348 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
352 /* Set min via `void setMin (double min)' */
353 status = ctoj_double (jvm_env, dsrc->min,
354 c_datasource, o_datasource, "setMin");
357 ERROR ("java plugin: ctoj_data_source: "
358 "ctoj_double (setMin) failed.");
359 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
363 /* Set max via `void setMax (double max)' */
364 status = ctoj_double (jvm_env, dsrc->max,
365 c_datasource, o_datasource, "setMax");
368 ERROR ("java plugin: ctoj_data_source: "
369 "ctoj_double (setMax) failed.");
370 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
374 return (o_datasource);
375 } /* }}} jobject ctoj_data_source */
377 /* Convert a oconfig_value_t to a org.collectd.api.OConfigValue */
378 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
379 oconfig_value_t ocvalue)
382 jmethodID m_ocvalue_constructor;
386 m_ocvalue_constructor = NULL;
389 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
390 "org.collectd.api.OConfigValue");
391 if (c_ocvalue == NULL)
393 ERROR ("java plugin: ctoj_oconfig_value: "
394 "FindClass (org.collectd.api.OConfigValue) failed.");
398 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
400 jboolean tmp_boolean;
402 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
404 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
406 if (m_ocvalue_constructor == NULL)
408 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
409 "`OConfigValue (boolean)' constructor.");
413 return ((*jvm_env)->NewObject (jvm_env,
414 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
415 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
416 else if (ocvalue.type == OCONFIG_TYPE_STRING)
418 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
419 "<init>", "(Ljava/lang/String;)V");
420 if (m_ocvalue_constructor == NULL)
422 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
423 "`OConfigValue (String)' constructor.");
427 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
428 if (o_argument == NULL)
430 ERROR ("java plugin: ctoj_oconfig_value: "
431 "Creating a String object failed.");
435 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
437 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
438 "<init>", "(Ljava/lang/Number;)V");
439 if (m_ocvalue_constructor == NULL)
441 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
442 "`OConfigValue (Number)' constructor.");
446 o_argument = ctoj_jdouble_to_number (jvm_env,
447 (jdouble) ocvalue.value.number);
448 if (o_argument == NULL)
450 ERROR ("java plugin: ctoj_oconfig_value: "
451 "Creating a Number object failed.");
460 assert (m_ocvalue_constructor != NULL);
461 assert (o_argument != NULL);
463 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
464 c_ocvalue, m_ocvalue_constructor, o_argument);
465 if (o_ocvalue == NULL)
467 ERROR ("java plugin: ctoj_oconfig_value: "
468 "Creating an OConfigValue object failed.");
469 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
473 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
475 } /* }}} jobject ctoj_oconfig_value */
477 /* Convert a oconfig_item_t to a org.collectd.api.OConfigItem */
478 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
479 const oconfig_item_t *ci)
482 jmethodID m_ocitem_constructor;
483 jmethodID m_addvalue;
484 jmethodID m_addchild;
489 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.OConfigItem");
490 if (c_ocitem == NULL)
492 ERROR ("java plugin: ctoj_oconfig_item: "
493 "FindClass (org.collectd.api.OConfigItem) failed.");
497 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
499 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
500 "<init>", "(Ljava/lang/String;)V");
501 if (m_ocitem_constructor == NULL)
503 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
504 "`OConfigItem (String)' constructor.");
508 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
509 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
510 if (m_addvalue == NULL)
512 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
513 "`addValue (OConfigValue)' method.");
517 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
518 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
519 if (m_addchild == NULL)
521 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
522 "`addChild (OConfigItem)' method.");
527 /* Create a String object with the key.
528 * Needed for calling the constructor. */
529 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
532 ERROR ("java plugin: ctoj_oconfig_item: "
533 "Creating String object failed.");
537 /* Create an OConfigItem object */
538 o_ocitem = (*jvm_env)->NewObject (jvm_env,
539 c_ocitem, m_ocitem_constructor, o_key);
540 if (o_ocitem == NULL)
542 ERROR ("java plugin: ctoj_oconfig_item: "
543 "Creating an OConfigItem object failed.");
544 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
548 /* We don't need the String object any longer.. */
549 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
551 /* Call OConfigItem.addValue for each value */
552 for (i = 0; i < ci->values_num; i++) /* {{{ */
556 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
559 ERROR ("java plugin: ctoj_oconfig_item: "
560 "Creating an OConfigValue object failed.");
561 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
565 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
566 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
567 } /* }}} for (i = 0; i < ci->values_num; i++) */
569 /* Call OConfigItem.addChild for each child */
570 for (i = 0; i < ci->children_num; i++) /* {{{ */
574 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
577 ERROR ("java plugin: ctoj_oconfig_item: "
578 "Creating an OConfigItem object failed.");
579 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
583 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_child);
584 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
585 } /* }}} for (i = 0; i < ci->children_num; i++) */
588 } /* }}} jobject ctoj_oconfig_item */
590 /* Convert a data_set_t to a org.collectd.api.DataSet */
591 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
594 jmethodID m_constructor;
600 /* Look up the org.collectd.api.DataSet class */
601 c_dataset = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.DataSet");
602 if (c_dataset == NULL)
604 ERROR ("java plugin: ctoj_data_set: Looking up the "
605 "org.collectd.api.DataSet class failed.");
609 /* Search for the `DataSet (String type)' constructor. */
610 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
611 c_dataset, "<init>", "(Ljava.lang.String;)V");
612 if (m_constructor == NULL)
614 ERROR ("java plugin: ctoj_data_set: Looking up the "
615 "`DataSet (String)' constructor failed.");
619 /* Search for the `void addDataSource (DataSource)' method. */
620 m_add = (*jvm_env)->GetMethodID (jvm_env,
621 c_dataset, "addDataSource", "(Lorg.collectd.api.DataSource;)V");
624 ERROR ("java plugin: ctoj_data_set: Looking up the "
625 "`addDataSource (DataSource)' method failed.");
629 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
632 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
636 o_dataset = (*jvm_env)->NewObject (jvm_env,
637 c_dataset, m_constructor, o_type);
638 if (o_dataset == NULL)
640 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
641 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
645 /* Decrease reference counter on the java.lang.String object. */
646 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
648 for (i = 0; i < ds->ds_num; i++)
650 jobject o_datasource;
652 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
653 if (o_datasource == NULL)
655 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
656 ds->type, ds->ds[i].name);
657 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
661 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
663 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
664 } /* for (i = 0; i < ds->ds_num; i++) */
667 } /* }}} jobject ctoj_data_set */
669 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
670 value_t value, int ds_type,
671 jclass class_ptr, jobject object_ptr)
673 jmethodID m_addvalue;
676 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
677 "addValue", "(Ljava/lang/Number;)V");
678 if (m_addvalue == NULL)
680 ERROR ("java plugin: ctoj_value_list_add_value: "
681 "Cannot find method `void addValue (Number)'.");
685 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
686 if (o_number == NULL)
688 ERROR ("java plugin: ctoj_value_list_add_value: "
689 "ctoj_value_to_number failed.");
693 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
695 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
698 } /* }}} int ctoj_value_list_add_value */
700 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
701 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
703 jmethodID m_setdataset;
706 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
707 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
708 "setDataSet", "(Lorg.collectd.api.DataSet;)V");
709 if (m_setdataset == NULL)
711 ERROR ("java plugin: ctoj_value_list_add_data_set: "
712 "Cannot find the `void setDataSet (DataSet)' method.");
716 /* Create a DataSet object. */
717 o_dataset = ctoj_data_set (jvm_env, ds);
718 if (o_dataset == NULL)
720 ERROR ("java plugin: ctoj_value_list_add_data_set: "
721 "ctoj_data_set (%s) failed.", ds->type);
725 /* Actually call the method. */
726 (*jvm_env)->CallVoidMethod (jvm_env,
727 o_valuelist, m_setdataset, o_dataset);
729 /* Decrease reference counter on the List<DataSource> object. */
730 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
733 } /* }}} int ctoj_value_list_add_data_set */
735 /* Convert a value_list_t (and data_set_t) to a org.collectd.api.ValueList */
736 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
737 const data_set_t *ds, const value_list_t *vl)
740 jmethodID m_valuelist_constructor;
745 /* First, create a new ValueList instance..
746 * Look up the class.. */
747 c_valuelist = (*jvm_env)->FindClass (jvm_env,
748 "org.collectd.api.ValueList");
749 if (c_valuelist == NULL)
751 ERROR ("java plugin: ctoj_value_list: "
752 "FindClass (org.collectd.api.ValueList) failed.");
756 /* Lookup the `ValueList ()' constructor. */
757 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
759 if (m_valuelist_constructor == NULL)
761 ERROR ("java plugin: ctoj_value_list: Cannot find the "
762 "`ValueList ()' constructor.");
766 /* Create a new instance. */
767 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
768 m_valuelist_constructor);
769 if (o_valuelist == NULL)
771 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
776 status = ctoj_value_list_add_data_set (jvm_env,
777 c_valuelist, o_valuelist, ds);
780 ERROR ("java plugin: ctoj_value_list: "
781 "ctoj_value_list_add_data_set failed.");
782 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
786 /* Set the strings.. */
787 #define SET_STRING(str,method_name) do { \
788 status = ctoj_string (jvm_env, str, \
789 c_valuelist, o_valuelist, method_name); \
791 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
793 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
797 SET_STRING (vl->host, "setHost");
798 SET_STRING (vl->plugin, "setPlugin");
799 SET_STRING (vl->plugin_instance, "setPluginInstance");
800 SET_STRING (vl->type, "setType");
801 SET_STRING (vl->type_instance, "setTypeInstance");
805 /* Set the `time' member. Java stores time in milliseconds. */
806 status = ctoj_long (jvm_env, ((jlong) vl->time) * ((jlong) 1000),
807 c_valuelist, o_valuelist, "setTime");
810 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
811 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
815 /* Set the `interval' member.. */
816 status = ctoj_long (jvm_env, (jlong) vl->interval,
817 c_valuelist, o_valuelist, "setInterval");
820 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
821 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
825 for (i = 0; i < vl->values_len; i++)
827 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
828 c_valuelist, o_valuelist);
831 ERROR ("java plugin: ctoj_value_list: "
832 "ctoj_value_list_add_value failed.");
833 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
838 return (o_valuelist);
839 } /* }}} jobject ctoj_value_list */
841 /* Convert a notification_t to a org.collectd.api.Notification */
842 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
843 const notification_t *n)
845 jclass c_notification;
846 jmethodID m_constructor;
847 jobject o_notification;
850 /* First, create a new Notification instance..
851 * Look up the class.. */
852 c_notification = (*jvm_env)->FindClass (jvm_env,
853 "org.collectd.api.Notification");
854 if (c_notification == NULL)
856 ERROR ("java plugin: ctoj_notification: "
857 "FindClass (org.collectd.api.Notification) failed.");
861 /* Lookup the `Notification ()' constructor. */
862 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
864 if (m_constructor == NULL)
866 ERROR ("java plugin: ctoj_notification: Cannot find the "
867 "`Notification ()' constructor.");
871 /* Create a new instance. */
872 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
874 if (o_notification == NULL)
876 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
881 /* Set the strings.. */
882 #define SET_STRING(str,method_name) do { \
883 status = ctoj_string (jvm_env, str, \
884 c_notification, o_notification, method_name); \
886 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
888 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
892 SET_STRING (n->host, "setHost");
893 SET_STRING (n->plugin, "setPlugin");
894 SET_STRING (n->plugin_instance, "setPluginInstance");
895 SET_STRING (n->type, "setType");
896 SET_STRING (n->type_instance, "setTypeInstance");
897 SET_STRING (n->message, "setMessage");
901 /* Set the `time' member. Java stores time in milliseconds. */
902 status = ctoj_long (jvm_env, ((jlong) n->time) * ((jlong) 1000),
903 c_notification, o_notification, "setTime");
906 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
907 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
911 /* Set the `interval' member.. */
912 status = ctoj_int (jvm_env, (jint) n->severity,
913 c_notification, o_notification, "setSeverity");
916 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
917 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
921 return (o_notification);
922 } /* }}} jobject ctoj_notification */
925 * Java to C conversion functions
927 /* Call a `String <method> ()' method. */
928 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
929 char *buffer, size_t buffer_size, int empty_okay,
930 jclass class_ptr, jobject object_ptr, const char *method_name)
936 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
937 method_name, "()Ljava/lang/String;");
938 if (method_id == NULL)
940 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
945 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
946 if ((string_obj == NULL) && (empty_okay == 0))
948 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
952 else if ((string_obj == NULL) && (empty_okay != 0))
954 memset (buffer, 0, buffer_size);
958 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
961 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
962 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
966 sstrncpy (buffer, c_str, buffer_size);
968 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
969 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
972 } /* }}} int jtoc_string */
974 /* Call an `int <method> ()' method. */
975 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
977 jclass class_ptr, jobject object_ptr, const char *method_name)
981 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
983 if (method_id == NULL)
985 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
990 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
993 } /* }}} int jtoc_int */
995 /* Call a `long <method> ()' method. */
996 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
998 jclass class_ptr, jobject object_ptr, const char *method_name)
1000 jmethodID method_id;
1002 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1003 method_name, "()J");
1004 if (method_id == NULL)
1006 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1011 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1014 } /* }}} int jtoc_long */
1016 /* Call a `double <method> ()' method. */
1017 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1019 jclass class_ptr, jobject object_ptr, const char *method_name)
1021 jmethodID method_id;
1023 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1024 method_name, "()D");
1025 if (method_id == NULL)
1027 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1032 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1035 } /* }}} int jtoc_double */
1037 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1038 value_t *ret_value, int ds_type, jobject object_ptr)
1043 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1045 if (ds_type == DS_TYPE_COUNTER)
1049 status = jtoc_long (jvm_env, &tmp_long,
1050 class_ptr, object_ptr, "longValue");
1053 ERROR ("java plugin: jtoc_value: "
1054 "jtoc_long failed.");
1057 (*ret_value).counter = (counter_t) tmp_long;
1063 status = jtoc_double (jvm_env, &tmp_double,
1064 class_ptr, object_ptr, "doubleValue");
1067 ERROR ("java plugin: jtoc_value: "
1068 "jtoc_double failed.");
1071 (*ret_value).gauge = (gauge_t) tmp_double;
1075 } /* }}} int jtoc_value */
1077 /* Read a List<Number>, convert it to `value_t' and add it to the given
1078 * `value_list_t'. */
1079 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1080 const data_set_t *ds, value_list_t *vl,
1081 jclass class_ptr, jobject object_ptr)
1083 jmethodID m_getvalues;
1084 jmethodID m_toarray;
1086 jobjectArray o_number_array;
1092 values_num = ds->ds_num;
1095 o_number_array = NULL;
1098 #define BAIL_OUT(status) \
1100 if (o_number_array != NULL) \
1101 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1102 if (o_list != NULL) \
1103 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1106 /* Call: List<Number> ValueList.getValues () */
1107 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1108 "getValues", "()Ljava/util/List;");
1109 if (m_getvalues == NULL)
1111 ERROR ("java plugin: jtoc_values_array: "
1112 "Cannot find method `List getValues ()'.");
1116 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1119 ERROR ("java plugin: jtoc_values_array: "
1120 "CallObjectMethod (getValues) failed.");
1124 /* Call: Number[] List.toArray () */
1125 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1126 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1127 "toArray", "()[Ljava/lang/Object;");
1128 if (m_toarray == NULL)
1130 ERROR ("java plugin: jtoc_values_array: "
1131 "Cannot find method `Object[] toArray ()'.");
1135 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1136 if (o_number_array == NULL)
1138 ERROR ("java plugin: jtoc_values_array: "
1139 "CallObjectMethod (toArray) failed.");
1143 values = (value_t *) calloc (values_num, sizeof (value_t));
1146 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1150 for (i = 0; i < values_num; i++)
1155 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1156 o_number_array, (jsize) i);
1157 if (o_number == NULL)
1159 ERROR ("java plugin: jtoc_values_array: "
1160 "GetObjectArrayElement (%i) failed.", i);
1164 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1167 ERROR ("java plugin: jtoc_values_array: "
1168 "jtoc_value (%i) failed.", i);
1171 } /* for (i = 0; i < values_num; i++) */
1173 vl->values = values;
1174 vl->values_len = values_num;
1177 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1178 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1180 } /* }}} int jtoc_values_array */
1182 /* Convert a org.collectd.api.ValueList to a value_list_t. */
1183 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1189 const data_set_t *ds;
1191 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1192 if (class_ptr == NULL)
1194 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1198 /* eo == empty okay */
1199 #define SET_STRING(buffer,method, eo) do { \
1200 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1201 class_ptr, object_ptr, method); \
1202 if (status != 0) { \
1203 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1208 SET_STRING(vl->type, "getType", /* empty = */ 0);
1210 ds = plugin_get_ds (vl->type);
1213 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1214 "Please consult the types.db(5) manpage for mor information.",
1219 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1220 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1221 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1222 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1226 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1229 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1232 /* Java measures time in milliseconds. */
1233 vl->time = (time_t) (tmp_long / ((jlong) 1000));
1235 status = jtoc_long (jvm_env, &tmp_long,
1236 class_ptr, object_ptr, "getInterval");
1239 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1242 vl->interval = (int) tmp_long;
1244 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1247 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1252 } /* }}} int jtoc_value_list */
1254 /* Convert a org.collectd.api.Notification to a notification_t. */
1255 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1263 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1264 if (class_ptr == NULL)
1266 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1270 /* eo == empty okay */
1271 #define SET_STRING(buffer,method, eo) do { \
1272 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1273 class_ptr, object_ptr, method); \
1274 if (status != 0) { \
1275 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1280 SET_STRING (n->host, "getHost", /* empty = */ 1);
1281 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1282 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1283 SET_STRING (n->type, "getType", /* empty = */ 1);
1284 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1285 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1289 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1292 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1295 /* Java measures time in milliseconds. */
1296 n->time = (time_t) (tmp_long / ((jlong) 1000));
1298 status = jtoc_int (jvm_env, &tmp_int,
1299 class_ptr, object_ptr, "getSeverity");
1302 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1305 n->severity = (int) tmp_int;
1308 } /* }}} int jtoc_notification */
1310 * Functions accessible from Java
1312 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1313 jobject this, jobject java_vl)
1315 value_list_t vl = VALUE_LIST_INIT;
1318 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1320 status = jtoc_value_list (jvm_env, &vl, java_vl);
1323 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1327 status = plugin_dispatch_values (&vl);
1332 } /* }}} jint cjni_api_dispatch_values */
1334 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1335 jobject this, jobject o_notification)
1340 memset (&n, 0, sizeof (n));
1343 status = jtoc_notification (jvm_env, &n, o_notification);
1346 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1350 status = plugin_dispatch_notification (&n);
1353 } /* }}} jint cjni_api_dispatch_notification */
1355 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1356 jobject this, jobject o_string_type)
1358 const char *ds_name;
1359 const data_set_t *ds;
1362 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1363 if (ds_name == NULL)
1365 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1369 ds = plugin_get_ds (ds_name);
1370 DEBUG ("java plugin: cjni_api_get_ds: "
1371 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1373 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1378 o_dataset = ctoj_data_set (jvm_env, ds);
1380 } /* }}} jint cjni_api_get_ds */
1382 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1383 jobject this, jobject o_name, jobject o_config)
1385 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1386 } /* }}} jint cjni_api_register_config */
1388 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1389 jobject this, jobject o_name, jobject o_config)
1391 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1392 } /* }}} jint cjni_api_register_init */
1394 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1395 jobject this, jobject o_name, jobject o_read)
1398 cjni_callback_info_t *cbi;
1400 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1404 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1406 memset (&ud, 0, sizeof (ud));
1407 ud.data = (void *) cbi;
1408 ud.free_func = cjni_callback_info_destroy;
1410 plugin_register_complex_read (cbi->name, cjni_read, &ud);
1412 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1415 } /* }}} jint cjni_api_register_read */
1417 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1418 jobject this, jobject o_name, jobject o_write)
1421 cjni_callback_info_t *cbi;
1423 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1427 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1429 memset (&ud, 0, sizeof (ud));
1430 ud.data = (void *) cbi;
1431 ud.free_func = cjni_callback_info_destroy;
1433 plugin_register_write (cbi->name, cjni_write, &ud);
1435 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1438 } /* }}} jint cjni_api_register_write */
1440 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1441 jobject this, jobject o_name, jobject o_flush)
1444 cjni_callback_info_t *cbi;
1446 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1450 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1452 memset (&ud, 0, sizeof (ud));
1453 ud.data = (void *) cbi;
1454 ud.free_func = cjni_callback_info_destroy;
1456 plugin_register_flush (cbi->name, cjni_flush, &ud);
1458 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1461 } /* }}} jint cjni_api_register_flush */
1463 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1464 jobject this, jobject o_name, jobject o_shutdown)
1466 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1468 } /* }}} jint cjni_api_register_shutdown */
1470 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1471 jobject this, jobject o_name, jobject o_log)
1474 cjni_callback_info_t *cbi;
1476 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1480 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1482 memset (&ud, 0, sizeof (ud));
1483 ud.data = (void *) cbi;
1484 ud.free_func = cjni_callback_info_destroy;
1486 plugin_register_log (cbi->name, cjni_log, &ud);
1488 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1491 } /* }}} jint cjni_api_register_log */
1493 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1494 jobject this, jobject o_name, jobject o_notification)
1497 cjni_callback_info_t *cbi;
1499 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1500 CB_TYPE_NOTIFICATION);
1504 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1506 memset (&ud, 0, sizeof (ud));
1507 ud.data = (void *) cbi;
1508 ud.free_func = cjni_callback_info_destroy;
1510 plugin_register_notification (cbi->name, cjni_notification, &ud);
1512 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1515 } /* }}} jint cjni_api_register_notification */
1517 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1518 jobject this, jobject o_name, jobject o_match, int type)
1523 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1526 ERROR ("java plugin: cjni_api_register_match_target: "
1527 "GetStringUTFChars failed.");
1531 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1534 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1538 if (type == CB_TYPE_MATCH)
1540 match_proc_t m_proc;
1542 memset (&m_proc, 0, sizeof (m_proc));
1543 m_proc.create = cjni_match_target_create;
1544 m_proc.destroy = cjni_match_target_destroy;
1545 m_proc.match = (void *) cjni_match_target_invoke;
1547 status = fc_register_match (c_name, m_proc);
1549 else if (type == CB_TYPE_TARGET)
1551 target_proc_t t_proc;
1553 memset (&t_proc, 0, sizeof (t_proc));
1554 t_proc.create = cjni_match_target_create;
1555 t_proc.destroy = cjni_match_target_destroy;
1556 t_proc.invoke = cjni_match_target_invoke;
1558 status = fc_register_target (c_name, t_proc);
1562 ERROR ("java plugin: cjni_api_register_match_target: "
1563 "Don't know whether to create a match or a target.");
1564 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1570 ERROR ("java plugin: cjni_api_register_match_target: "
1572 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1573 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1577 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1580 } /* }}} jint cjni_api_register_match_target */
1582 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1583 jobject this, jobject o_name, jobject o_match)
1585 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1587 } /* }}} jint cjni_api_register_match */
1589 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1590 jobject this, jobject o_name, jobject o_target)
1592 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1594 } /* }}} jint cjni_api_register_target */
1596 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1597 jobject this, jint severity, jobject o_message)
1601 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1604 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1608 if (severity < LOG_ERR)
1610 if (severity > LOG_DEBUG)
1611 severity = LOG_DEBUG;
1613 plugin_log (severity, "%s", c_str);
1615 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1616 } /* }}} void cjni_api_log */
1618 /* List of ``native'' functions, i. e. C-functions that can be called from
1620 static JNINativeMethod jni_api_functions[] = /* {{{ */
1623 "(Lorg/collectd/api/ValueList;)I",
1624 cjni_api_dispatch_values },
1626 { "dispatchNotification",
1627 "(Lorg/collectd/api/Notification;)I",
1628 cjni_api_dispatch_notification },
1631 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1635 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1636 cjni_api_register_config },
1639 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1640 cjni_api_register_init },
1643 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1644 cjni_api_register_read },
1647 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1648 cjni_api_register_write },
1651 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1652 cjni_api_register_flush },
1654 { "registerShutdown",
1655 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1656 cjni_api_register_shutdown },
1659 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1660 cjni_api_register_log },
1662 { "registerNotification",
1663 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1664 cjni_api_register_notification },
1667 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1668 cjni_api_register_match },
1671 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1672 cjni_api_register_target },
1675 "(ILjava/lang/String;)V",
1678 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1679 / sizeof (jni_api_functions[0]);
1685 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1686 * all registration functions. */
1687 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1688 jobject o_name, jobject o_callback, int type)
1691 cjni_callback_info_t *cbi;
1692 const char *method_name;
1693 const char *method_signature;
1697 case CB_TYPE_CONFIG:
1698 method_name = "config";
1699 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1703 method_name = "init";
1704 method_signature = "()I";
1708 method_name = "read";
1709 method_signature = "()I";
1713 method_name = "write";
1714 method_signature = "(Lorg/collectd/api/ValueList;)I";
1718 method_name = "flush";
1719 method_signature = "(ILjava/lang/String;)I";
1722 case CB_TYPE_SHUTDOWN:
1723 method_name = "shutdown";
1724 method_signature = "()I";
1728 method_name = "log";
1729 method_signature = "(ILjava/lang/String;)V";
1732 case CB_TYPE_NOTIFICATION:
1733 method_name = "notification";
1734 method_signature = "(Lorg/collectd/api/Notification;)I";
1738 method_name = "createMatch";
1739 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1740 "Lorg/collectd/api/CollectdMatchInterface;";
1743 case CB_TYPE_TARGET:
1744 method_name = "createTarget";
1745 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1746 "Lorg/collectd/api/CollectdTargetInterface;";
1750 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1755 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1758 ERROR ("java plugin: cjni_callback_info_create: "
1759 "GetStringUTFChars failed.");
1763 cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1766 ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1767 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1770 memset (cbi, 0, sizeof (*cbi));
1773 cbi->name = strdup (c_name);
1774 if (cbi->name == NULL)
1776 pthread_mutex_unlock (&java_callbacks_lock);
1777 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1778 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1782 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1784 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, o_callback);
1785 if (cbi->class == NULL)
1787 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1792 cbi->object = o_callback;
1794 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1795 method_name, method_signature);
1796 if (cbi->method == NULL)
1798 ERROR ("java plugin: cjni_callback_info_create: "
1799 "Cannot find the `%s' method with signature `%s'.",
1800 method_name, method_signature);
1805 (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1808 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1810 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1811 * to the global `java_callbacks' variable. This is used for `config', `init',
1812 * and `shutdown' callbacks. */
1813 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1814 jobject o_name, jobject o_callback, int type)
1816 cjni_callback_info_t *cbi;
1817 cjni_callback_info_t *tmp;
1819 const char *type_str;
1822 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1829 case CB_TYPE_CONFIG:
1830 type_str = "config";
1837 case CB_TYPE_SHUTDOWN:
1838 type_str = "shutdown";
1845 case CB_TYPE_TARGET:
1846 type_str = "target";
1850 type_str = "<unknown>";
1852 DEBUG ("java plugin: Registering new %s callback: %s",
1853 type_str, cbi->name);
1856 pthread_mutex_lock (&java_callbacks_lock);
1858 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1859 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1862 pthread_mutex_unlock (&java_callbacks_lock);
1863 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1865 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1870 java_callbacks = tmp;
1871 java_callbacks[java_callbacks_num] = *cbi;
1872 java_callbacks_num++;
1874 pthread_mutex_unlock (&java_callbacks_lock);
1878 } /* }}} int cjni_callback_register */
1880 /* Callback for `pthread_key_create'. It frees the data contained in
1881 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1882 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1884 cjni_jvm_env_t *cjni_env;
1889 cjni_env = (cjni_jvm_env_t *) args;
1891 if (cjni_env->reference_counter > 0)
1893 ERROR ("java plugin: cjni_jvm_env_destroy: "
1894 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1897 if (cjni_env->jvm_env != NULL)
1899 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1900 (void *) cjni_env->jvm_env);
1903 /* The pointer is allocated in `cjni_thread_attach' */
1905 } /* }}} void cjni_jvm_env_destroy */
1907 /* Register ``native'' functions with the JVM. Native functions are C-functions
1908 * that can be called by Java code. */
1909 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1911 jclass api_class_ptr;
1914 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.Collectd");
1915 if (api_class_ptr == NULL)
1917 ERROR ("cjni_init_native: Cannot find API class `org.collectd.api.Collectd'.");
1921 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1922 jni_api_functions, (jint) jni_api_functions_num);
1925 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1930 } /* }}} int cjni_init_native */
1932 /* Create the JVM. This is called when the first thread tries to access the JVM
1933 * via cjni_thread_attach. */
1934 static int cjni_create_jvm (void) /* {{{ */
1937 JavaVMInitArgs vm_args;
1938 JavaVMOption vm_options[jvm_argc];
1946 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1949 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1950 "with status %i.", status);
1956 memset (&vm_args, 0, sizeof (vm_args));
1957 vm_args.version = JNI_VERSION_1_2;
1958 vm_args.options = vm_options;
1959 vm_args.nOptions = (jint) jvm_argc;
1961 for (i = 0; i < jvm_argc; i++)
1963 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
1965 vm_args.options[i].optionString = jvm_argv[i];
1968 vm_args.options[0].optionString = "-verbose:jni";
1969 vm_args.options[1].optionString = "-Djava.class.path=/home/octo/collectd/bindings/java";
1972 status = JNI_CreateJavaVM (&jvm, (void **) &jvm_env, (void **) &vm_args);
1975 ERROR ("java plugin: cjni_create_jvm: "
1976 "JNI_CreateJavaVM failed with status %i.",
1980 assert (jvm != NULL);
1981 assert (jvm_env != NULL);
1983 /* Call RegisterNatives */
1984 status = cjni_init_native (jvm_env);
1987 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
1991 DEBUG ("java plugin: The JVM has been created.");
1993 } /* }}} int cjni_create_jvm */
1995 /* Increase the reference counter to the JVM for this thread. If it was zero,
1996 * attach the JVM first. */
1997 static JNIEnv *cjni_thread_attach (void) /* {{{ */
1999 cjni_jvm_env_t *cjni_env;
2002 /* If we're the first thread to access the JVM, we'll have to create it
2008 status = cjni_create_jvm ();
2011 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2015 assert (jvm != NULL);
2017 cjni_env = pthread_getspecific (jvm_env_key);
2018 if (cjni_env == NULL)
2020 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2021 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
2022 if (cjni_env == NULL)
2024 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
2027 memset (cjni_env, 0, sizeof (*cjni_env));
2028 cjni_env->reference_counter = 0;
2029 cjni_env->jvm_env = NULL;
2031 pthread_setspecific (jvm_env_key, cjni_env);
2034 if (cjni_env->reference_counter > 0)
2036 cjni_env->reference_counter++;
2037 jvm_env = cjni_env->jvm_env;
2042 JavaVMAttachArgs args;
2044 assert (cjni_env->jvm_env == NULL);
2046 memset (&args, 0, sizeof (args));
2047 args.version = JNI_VERSION_1_2;
2049 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2052 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2053 "with status %i.", status);
2057 cjni_env->reference_counter = 1;
2058 cjni_env->jvm_env = jvm_env;
2061 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2062 cjni_env->reference_counter);
2063 assert (jvm_env != NULL);
2065 } /* }}} JNIEnv *cjni_thread_attach */
2067 /* Decrease the reference counter of this thread. If it reaches zero, detach
2069 static int cjni_thread_detach (void) /* {{{ */
2071 cjni_jvm_env_t *cjni_env;
2074 cjni_env = pthread_getspecific (jvm_env_key);
2075 if (cjni_env == NULL)
2077 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2081 assert (cjni_env->reference_counter > 0);
2082 assert (cjni_env->jvm_env != NULL);
2084 cjni_env->reference_counter--;
2085 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2086 cjni_env->reference_counter);
2088 if (cjni_env->reference_counter > 0)
2091 status = (*jvm)->DetachCurrentThread (jvm);
2094 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2095 "with status %i.", status);
2098 cjni_env->reference_counter = 0;
2099 cjni_env->jvm_env = NULL;
2102 } /* }}} JNIEnv *cjni_thread_attach */
2104 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2108 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2110 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2116 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2117 "`LoadPlugin' options! The JVM is already started and I have to "
2118 "ignore this argument: %s",
2119 ci->values[0].value.string);
2123 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2126 ERROR ("java plugin: realloc failed.");
2131 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2132 if (jvm_argv[jvm_argc] == NULL)
2134 ERROR ("java plugin: strdup failed.");
2140 } /* }}} int cjni_config_add_jvm_arg */
2142 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2145 java_plugin_class_t *class;
2146 jmethodID constructor_id;
2148 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2150 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2154 jvm_env = cjni_thread_attach ();
2155 if (jvm_env == NULL)
2158 class = (java_plugin_class_t *) realloc (java_classes_list,
2159 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2162 ERROR ("java plugin: realloc failed.");
2163 cjni_thread_detach ();
2166 java_classes_list = class;
2167 class = java_classes_list + java_classes_list_len;
2169 memset (class, 0, sizeof (*class));
2170 class->name = strdup (ci->values[0].value.string);
2171 if (class->name == NULL)
2173 ERROR ("java plugin: strdup failed.");
2174 cjni_thread_detach ();
2177 class->class = NULL;
2178 class->object = NULL;
2180 DEBUG ("java plugin: Loading class %s", class->name);
2182 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2183 if (class->class == NULL)
2185 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2187 cjni_thread_detach ();
2192 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2194 if (constructor_id == NULL)
2196 ERROR ("java plugin: cjni_config_load_plugin: "
2197 "Could not find the constructor for `%s'.",
2199 cjni_thread_detach ();
2204 class->object = (*jvm_env)->NewObject (jvm_env, class->class,
2206 if (class->object == NULL)
2208 ERROR ("java plugin: cjni_config_load_plugin: "
2209 "Could create a new `%s' object.",
2211 cjni_thread_detach ();
2216 (*jvm_env)->NewGlobalRef (jvm_env, class->object);
2217 cjni_thread_detach ();
2219 java_classes_list_len++;
2222 } /* }}} int cjni_config_load_plugin */
2224 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2227 cjni_callback_info_t *cbi;
2233 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2235 WARNING ("java plugin: `Plugin' blocks "
2236 "need exactly one string argument.");
2240 name = ci->values[0].value.string;
2243 for (i = 0; i < java_callbacks_num; i++)
2245 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2248 if (strcmp (name, java_callbacks[i].name) != 0)
2251 cbi = java_callbacks + i;
2257 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2258 "configuration callback has been registered. Please make sure, the "
2259 "`LoadPlugin' lines precede the `Plugin' blocks.",
2264 DEBUG ("java plugin: Configuring %s", name);
2266 jvm_env = cjni_thread_attach ();
2267 if (jvm_env == NULL)
2270 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2271 if (o_ocitem == NULL)
2273 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2274 cjni_thread_detach ();
2278 status = (*jvm_env)->CallIntMethod (jvm_env,
2279 cbi->object, cbi->method, o_ocitem);
2281 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2282 cjni_thread_detach ();
2284 } /* }}} int cjni_config_plugin_block */
2286 static int cjni_config (oconfig_item_t *ci) /* {{{ */
2296 for (i = 0; i < ci->children_num; i++)
2298 oconfig_item_t *child = ci->children + i;
2300 if (strcasecmp ("JVMArg", child->key) == 0)
2302 status = cjni_config_add_jvm_arg (child);
2308 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2310 status = cjni_config_load_plugin (child);
2316 else if (strcasecmp ("Plugin", child->key) == 0)
2318 status = cjni_config_plugin_block (child);
2326 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2331 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2332 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2334 if ((success == 0) && (errors > 0))
2336 ERROR ("java plugin: All statements failed.");
2341 } /* }}} int cjni_config */
2343 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2344 * and `cjni_write'. In particular, delete the global reference to the Java
2346 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2349 cjni_callback_info_t *cbi;
2351 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2353 cbi = (cjni_callback_info_t *) arg;
2355 /* This condition can occurr when shutting down. */
2365 jvm_env = cjni_thread_attach ();
2366 if (jvm_env == NULL)
2368 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2372 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2379 cjni_thread_detach ();
2380 } /* }}} void cjni_callback_info_destroy */
2382 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2383 static int cjni_read (user_data_t *ud) /* {{{ */
2386 cjni_callback_info_t *cbi;
2392 ERROR ("java plugin: cjni_read: jvm == NULL");
2396 if ((ud == NULL) || (ud->data == NULL))
2398 ERROR ("java plugin: cjni_read: Invalid user data.");
2402 jvm_env = cjni_thread_attach ();
2403 if (jvm_env == NULL)
2406 cbi = (cjni_callback_info_t *) ud->data;
2408 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2411 status = cjni_thread_detach ();
2414 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2418 return (ret_status);
2419 } /* }}} int cjni_read */
2421 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2422 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2426 cjni_callback_info_t *cbi;
2433 ERROR ("java plugin: cjni_write: jvm == NULL");
2437 if ((ud == NULL) || (ud->data == NULL))
2439 ERROR ("java plugin: cjni_write: Invalid user data.");
2443 jvm_env = cjni_thread_attach ();
2444 if (jvm_env == NULL)
2447 cbi = (cjni_callback_info_t *) ud->data;
2449 vl_java = ctoj_value_list (jvm_env, ds, vl);
2450 if (vl_java == NULL)
2452 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2456 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2457 cbi->object, cbi->method, vl_java);
2459 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2461 status = cjni_thread_detach ();
2464 ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2468 return (ret_status);
2469 } /* }}} int cjni_write */
2471 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2472 static int cjni_flush (int timeout, const char *identifier, /* {{{ */
2476 cjni_callback_info_t *cbi;
2477 jobject o_identifier;
2483 ERROR ("java plugin: cjni_flush: jvm == NULL");
2487 if ((ud == NULL) || (ud->data == NULL))
2489 ERROR ("java plugin: cjni_flush: Invalid user data.");
2493 jvm_env = cjni_thread_attach ();
2494 if (jvm_env == NULL)
2497 cbi = (cjni_callback_info_t *) ud->data;
2499 o_identifier = NULL;
2500 if (identifier != NULL)
2502 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2503 if (o_identifier == NULL)
2505 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2510 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2511 cbi->object, cbi->method, (jint) timeout, o_identifier);
2513 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2515 status = cjni_thread_detach ();
2518 ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2522 return (ret_status);
2523 } /* }}} int cjni_flush */
2525 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2526 static void cjni_log (int severity, const char *message, /* {{{ */
2530 cjni_callback_info_t *cbi;
2536 if ((ud == NULL) || (ud->data == NULL))
2539 jvm_env = cjni_thread_attach ();
2540 if (jvm_env == NULL)
2543 cbi = (cjni_callback_info_t *) ud->data;
2545 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2546 if (o_message == NULL)
2549 (*jvm_env)->CallVoidMethod (jvm_env,
2550 cbi->object, cbi->method, (jint) severity, o_message);
2552 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2554 cjni_thread_detach ();
2555 } /* }}} void cjni_log */
2557 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2559 static int cjni_notification (const notification_t *n, /* {{{ */
2563 cjni_callback_info_t *cbi;
2564 jobject o_notification;
2570 ERROR ("java plugin: cjni_read: jvm == NULL");
2574 if ((ud == NULL) || (ud->data == NULL))
2576 ERROR ("java plugin: cjni_read: Invalid user data.");
2580 jvm_env = cjni_thread_attach ();
2581 if (jvm_env == NULL)
2584 cbi = (cjni_callback_info_t *) ud->data;
2586 o_notification = ctoj_notification (jvm_env, n);
2587 if (o_notification == NULL)
2589 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2593 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2594 cbi->object, cbi->method, o_notification);
2596 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2598 status = cjni_thread_detach ();
2601 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2605 return (ret_status);
2606 } /* }}} int cjni_notification */
2608 /* Callbacks for matches implemented in Java */
2609 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2613 cjni_callback_info_t *cbi_ret;
2614 cjni_callback_info_t *cbi_factory;
2624 #define BAIL_OUT(status) \
2625 if (cbi_ret != NULL) { \
2626 free (cbi_ret->name); \
2627 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2628 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2631 if (jvm_env != NULL) { \
2633 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2634 cjni_thread_detach (); \
2640 ERROR ("java plugin: cjni_read: jvm == NULL");
2644 jvm_env = cjni_thread_attach ();
2645 if (jvm_env == NULL)
2650 /* Find out whether to create a match or a target. */
2651 if (strcasecmp ("Match", ci->key) == 0)
2652 type = CB_TYPE_MATCH;
2653 else if (strcasecmp ("Target", ci->key) == 0)
2654 type = CB_TYPE_TARGET;
2657 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2658 "to create a match or a target.");
2662 /* This is the name of the match we should create. */
2663 name = ci->values[0].value.string;
2665 /* Lets see if we have a matching factory here.. */
2667 for (i = 0; i < java_callbacks_num; i++)
2669 if (java_callbacks[i].type != type)
2672 if (strcmp (name, java_callbacks[i].name) != 0)
2675 cbi_factory = java_callbacks + i;
2679 /* Nope, no factory for that name.. */
2680 if (cbi_factory == NULL)
2682 ERROR ("java plugin: cjni_match_target_create: "
2683 "No such match factory registered: %s",
2688 /* We convert `ci' to its Java equivalent.. */
2689 o_ci = ctoj_oconfig_item (jvm_env, ci);
2692 ERROR ("java plugin: cjni_match_target_create: "
2693 "ctoj_oconfig_item failed.");
2697 /* Allocate a new callback info structure. This is going to be our user_data
2699 cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
2700 if (cbi_ret == NULL)
2702 ERROR ("java plugin: cjni_match_target_create: malloc failed.");
2705 memset (cbi_ret, 0, sizeof (*cbi_ret));
2706 cbi_ret->object = NULL;
2707 cbi_ret->type = type;
2709 /* Lets fill the callback info structure.. First, the name: */
2710 cbi_ret->name = strdup (name);
2711 if (cbi_ret->name == NULL)
2713 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2717 /* Then call the factory method so it creates a new object for us. */
2718 cbi_ret->object = (*jvm_env)->CallObjectMethod (jvm_env,
2719 cbi_factory->object, cbi_factory->method, o_ci);
2720 if (cbi_ret->object == NULL)
2722 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2726 /* This is the class of the match. It is possibly different from the class of
2727 * the match-factory! */
2728 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2729 if (cbi_ret->class == NULL)
2731 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2735 /* Lookup the `int match (DataSet, ValueList)' method. */
2736 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2737 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2738 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2739 if (cbi_ret->method == NULL)
2741 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2745 /* We have everything we hoped for. Now we add a new global reference so this
2746 * match isn't freed immediately after we return.. */
2747 (*jvm_env)->NewGlobalRef (jvm_env, cbi_ret->object);
2749 /* Return the newly created match via the user_data pointer. */
2750 *user_data = (void *) cbi_ret;
2752 cjni_thread_detach ();
2754 DEBUG ("java plugin: cjni_match_target_create: "
2755 "Successfully created a `%s' %s.",
2756 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2761 } /* }}} int cjni_match_target_create */
2763 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2765 cjni_callback_info_destroy (*user_data);
2769 } /* }}} int cjni_match_target_destroy */
2771 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2772 value_list_t *vl, notification_meta_t **meta, void **user_data)
2775 cjni_callback_info_t *cbi;
2783 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2787 jvm_env = cjni_thread_attach ();
2788 if (jvm_env == NULL)
2791 cbi = (cjni_callback_info_t *) *user_data;
2793 o_vl = ctoj_value_list (jvm_env, ds, vl);
2796 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2797 cjni_thread_detach ();
2801 o_ds = ctoj_data_set (jvm_env, ds);
2804 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2805 cjni_thread_detach ();
2809 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2812 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2814 /* If we're executing a target, copy the `ValueList' back to our
2815 * `value_list_t'. */
2816 if (cbi->type == CB_TYPE_TARGET)
2818 value_list_t new_vl;
2820 memset (&new_vl, 0, sizeof (new_vl));
2821 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2824 ERROR ("java plugin: cjni_match_target_invoke: "
2825 "jtoc_value_list failed.");
2827 else /* if (status == 0) */
2829 /* plugin_dispatch_values assures that this is dynamically allocated
2833 /* This will replace the vl->values pointer to a new, dynamically
2834 * allocated piece of memory. */
2835 memcpy (vl, &new_vl, sizeof (*vl));
2837 } /* if (cbi->type == CB_TYPE_TARGET) */
2839 status = cjni_thread_detach ();
2841 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2843 return (ret_status);
2844 } /* }}} int cjni_match_target_invoke */
2846 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2847 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2852 for (i = 0; i < java_callbacks_num; i++)
2854 if (java_callbacks[i].type != CB_TYPE_INIT)
2857 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2859 status = (*jvm_env)->CallIntMethod (jvm_env,
2860 java_callbacks[i].object, java_callbacks[i].method);
2863 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2864 "Removing read function.",
2865 java_callbacks[i].name, status);
2866 plugin_unregister_read (java_callbacks[i].name);
2871 } /* }}} int cjni_init_plugins */
2873 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2874 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2879 for (i = 0; i < java_callbacks_num; i++)
2881 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2884 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2886 status = (*jvm_env)->CallIntMethod (jvm_env,
2887 java_callbacks[i].object, java_callbacks[i].method);
2890 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2891 java_callbacks[i].name, status);
2896 } /* }}} int cjni_shutdown_plugins */
2899 static int cjni_shutdown (void) /* {{{ */
2902 JavaVMAttachArgs args;
2910 memset (&args, 0, sizeof (args));
2911 args.version = JNI_VERSION_1_2;
2913 status = (*jvm)->AttachCurrentThread (jvm, (void **) &jvm_env, &args);
2916 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2921 /* Execute all the shutdown functions registered by plugins. */
2922 cjni_shutdown_plugins (jvm_env);
2924 /* Release all the global references to callback functions */
2925 for (i = 0; i < java_callbacks_num; i++)
2927 if (java_callbacks[i].object != NULL)
2929 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
2930 java_callbacks[i].object = NULL;
2932 sfree (java_callbacks[i].name);
2934 java_callbacks_num = 0;
2935 sfree (java_callbacks);
2937 /* Release all the global references to directly loaded classes. */
2938 for (i = 0; i < java_classes_list_len; i++)
2940 if (java_classes_list[i].object != NULL)
2942 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
2943 java_classes_list[i].object = NULL;
2945 sfree (java_classes_list[i].name);
2947 java_classes_list_len = 0;
2948 sfree (java_classes_list);
2950 /* Destroy the JVM */
2951 DEBUG ("java plugin: Destroying the JVM.");
2952 (*jvm)->DestroyJavaVM (jvm);
2956 pthread_key_delete (jvm_env_key);
2958 /* Free the JVM argument list */
2959 for (i = 0; i < jvm_argc; i++)
2960 sfree (jvm_argv[i]);
2965 } /* }}} int cjni_shutdown */
2967 /* Initialization: Create a JVM, load all configured classes and call their
2968 * `config' and `init' callback methods. */
2969 static int cjni_init (void) /* {{{ */
2975 ERROR ("java plugin: cjni_init: jvm == NULL");
2979 jvm_env = cjni_thread_attach ();
2980 if (jvm_env == NULL)
2983 cjni_init_plugins (jvm_env);
2985 cjni_thread_detach ();
2987 } /* }}} int cjni_init */
2989 void module_register (void)
2991 plugin_register_complex_config ("java", cjni_config);
2992 plugin_register_init ("java", cjni_init);
2993 plugin_register_shutdown ("java", cjni_shutdown);
2994 } /* void module_register (void) */
2996 /* vim: set sw=2 sts=2 et fdm=marker : */