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_addchild, 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,
1411 /* interval = */ NULL, &ud);
1413 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1416 } /* }}} jint cjni_api_register_read */
1418 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1419 jobject this, jobject o_name, jobject o_write)
1422 cjni_callback_info_t *cbi;
1424 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1428 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1430 memset (&ud, 0, sizeof (ud));
1431 ud.data = (void *) cbi;
1432 ud.free_func = cjni_callback_info_destroy;
1434 plugin_register_write (cbi->name, cjni_write, &ud);
1436 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1439 } /* }}} jint cjni_api_register_write */
1441 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1442 jobject this, jobject o_name, jobject o_flush)
1445 cjni_callback_info_t *cbi;
1447 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1451 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1453 memset (&ud, 0, sizeof (ud));
1454 ud.data = (void *) cbi;
1455 ud.free_func = cjni_callback_info_destroy;
1457 plugin_register_flush (cbi->name, cjni_flush, &ud);
1459 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1462 } /* }}} jint cjni_api_register_flush */
1464 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1465 jobject this, jobject o_name, jobject o_shutdown)
1467 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1469 } /* }}} jint cjni_api_register_shutdown */
1471 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1472 jobject this, jobject o_name, jobject o_log)
1475 cjni_callback_info_t *cbi;
1477 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1481 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1483 memset (&ud, 0, sizeof (ud));
1484 ud.data = (void *) cbi;
1485 ud.free_func = cjni_callback_info_destroy;
1487 plugin_register_log (cbi->name, cjni_log, &ud);
1489 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1492 } /* }}} jint cjni_api_register_log */
1494 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1495 jobject this, jobject o_name, jobject o_notification)
1498 cjni_callback_info_t *cbi;
1500 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1501 CB_TYPE_NOTIFICATION);
1505 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1507 memset (&ud, 0, sizeof (ud));
1508 ud.data = (void *) cbi;
1509 ud.free_func = cjni_callback_info_destroy;
1511 plugin_register_notification (cbi->name, cjni_notification, &ud);
1513 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1516 } /* }}} jint cjni_api_register_notification */
1518 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1519 jobject this, jobject o_name, jobject o_match, int type)
1524 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1527 ERROR ("java plugin: cjni_api_register_match_target: "
1528 "GetStringUTFChars failed.");
1532 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1535 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1539 if (type == CB_TYPE_MATCH)
1541 match_proc_t m_proc;
1543 memset (&m_proc, 0, sizeof (m_proc));
1544 m_proc.create = cjni_match_target_create;
1545 m_proc.destroy = cjni_match_target_destroy;
1546 m_proc.match = (void *) cjni_match_target_invoke;
1548 status = fc_register_match (c_name, m_proc);
1550 else if (type == CB_TYPE_TARGET)
1552 target_proc_t t_proc;
1554 memset (&t_proc, 0, sizeof (t_proc));
1555 t_proc.create = cjni_match_target_create;
1556 t_proc.destroy = cjni_match_target_destroy;
1557 t_proc.invoke = cjni_match_target_invoke;
1559 status = fc_register_target (c_name, t_proc);
1563 ERROR ("java plugin: cjni_api_register_match_target: "
1564 "Don't know whether to create a match or a target.");
1565 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1571 ERROR ("java plugin: cjni_api_register_match_target: "
1573 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1574 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1578 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1581 } /* }}} jint cjni_api_register_match_target */
1583 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1584 jobject this, jobject o_name, jobject o_match)
1586 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1588 } /* }}} jint cjni_api_register_match */
1590 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1591 jobject this, jobject o_name, jobject o_target)
1593 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1595 } /* }}} jint cjni_api_register_target */
1597 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1598 jobject this, jint severity, jobject o_message)
1602 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1605 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1609 if (severity < LOG_ERR)
1611 if (severity > LOG_DEBUG)
1612 severity = LOG_DEBUG;
1614 plugin_log (severity, "%s", c_str);
1616 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1617 } /* }}} void cjni_api_log */
1619 /* List of ``native'' functions, i. e. C-functions that can be called from
1621 static JNINativeMethod jni_api_functions[] = /* {{{ */
1624 "(Lorg/collectd/api/ValueList;)I",
1625 cjni_api_dispatch_values },
1627 { "dispatchNotification",
1628 "(Lorg/collectd/api/Notification;)I",
1629 cjni_api_dispatch_notification },
1632 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1636 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1637 cjni_api_register_config },
1640 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1641 cjni_api_register_init },
1644 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1645 cjni_api_register_read },
1648 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1649 cjni_api_register_write },
1652 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1653 cjni_api_register_flush },
1655 { "registerShutdown",
1656 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1657 cjni_api_register_shutdown },
1660 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1661 cjni_api_register_log },
1663 { "registerNotification",
1664 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1665 cjni_api_register_notification },
1668 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1669 cjni_api_register_match },
1672 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1673 cjni_api_register_target },
1676 "(ILjava/lang/String;)V",
1679 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1680 / sizeof (jni_api_functions[0]);
1686 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1687 * all registration functions. */
1688 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1689 jobject o_name, jobject o_callback, int type)
1692 cjni_callback_info_t *cbi;
1693 const char *method_name;
1694 const char *method_signature;
1698 case CB_TYPE_CONFIG:
1699 method_name = "config";
1700 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1704 method_name = "init";
1705 method_signature = "()I";
1709 method_name = "read";
1710 method_signature = "()I";
1714 method_name = "write";
1715 method_signature = "(Lorg/collectd/api/ValueList;)I";
1719 method_name = "flush";
1720 method_signature = "(ILjava/lang/String;)I";
1723 case CB_TYPE_SHUTDOWN:
1724 method_name = "shutdown";
1725 method_signature = "()I";
1729 method_name = "log";
1730 method_signature = "(ILjava/lang/String;)V";
1733 case CB_TYPE_NOTIFICATION:
1734 method_name = "notification";
1735 method_signature = "(Lorg/collectd/api/Notification;)I";
1739 method_name = "createMatch";
1740 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1741 "Lorg/collectd/api/CollectdMatchInterface;";
1744 case CB_TYPE_TARGET:
1745 method_name = "createTarget";
1746 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1747 "Lorg/collectd/api/CollectdTargetInterface;";
1751 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1756 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1759 ERROR ("java plugin: cjni_callback_info_create: "
1760 "GetStringUTFChars failed.");
1764 cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1767 ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1768 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1771 memset (cbi, 0, sizeof (*cbi));
1774 cbi->name = strdup (c_name);
1775 if (cbi->name == NULL)
1777 pthread_mutex_unlock (&java_callbacks_lock);
1778 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1779 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1783 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1785 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1786 if (cbi->object == NULL)
1788 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1793 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1794 if (cbi->class == NULL)
1796 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1801 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1802 method_name, method_signature);
1803 if (cbi->method == NULL)
1805 ERROR ("java plugin: cjni_callback_info_create: "
1806 "Cannot find the `%s' method with signature `%s'.",
1807 method_name, method_signature);
1813 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1815 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1816 * to the global `java_callbacks' variable. This is used for `config', `init',
1817 * and `shutdown' callbacks. */
1818 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1819 jobject o_name, jobject o_callback, int type)
1821 cjni_callback_info_t *cbi;
1822 cjni_callback_info_t *tmp;
1824 const char *type_str;
1827 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1834 case CB_TYPE_CONFIG:
1835 type_str = "config";
1842 case CB_TYPE_SHUTDOWN:
1843 type_str = "shutdown";
1850 case CB_TYPE_TARGET:
1851 type_str = "target";
1855 type_str = "<unknown>";
1857 DEBUG ("java plugin: Registering new %s callback: %s",
1858 type_str, cbi->name);
1861 pthread_mutex_lock (&java_callbacks_lock);
1863 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1864 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1867 pthread_mutex_unlock (&java_callbacks_lock);
1868 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1870 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1875 java_callbacks = tmp;
1876 java_callbacks[java_callbacks_num] = *cbi;
1877 java_callbacks_num++;
1879 pthread_mutex_unlock (&java_callbacks_lock);
1883 } /* }}} int cjni_callback_register */
1885 /* Callback for `pthread_key_create'. It frees the data contained in
1886 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1887 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1889 cjni_jvm_env_t *cjni_env;
1894 cjni_env = (cjni_jvm_env_t *) args;
1896 if (cjni_env->reference_counter > 0)
1898 ERROR ("java plugin: cjni_jvm_env_destroy: "
1899 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1902 if (cjni_env->jvm_env != NULL)
1904 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1905 (void *) cjni_env->jvm_env);
1908 /* The pointer is allocated in `cjni_thread_attach' */
1910 } /* }}} void cjni_jvm_env_destroy */
1912 /* Register ``native'' functions with the JVM. Native functions are C-functions
1913 * that can be called by Java code. */
1914 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1916 jclass api_class_ptr;
1919 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1920 if (api_class_ptr == NULL)
1922 ERROR ("cjni_init_native: Cannot find API class `org/collectd/api/Collectd'.");
1926 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1927 jni_api_functions, (jint) jni_api_functions_num);
1930 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1935 } /* }}} int cjni_init_native */
1937 /* Create the JVM. This is called when the first thread tries to access the JVM
1938 * via cjni_thread_attach. */
1939 static int cjni_create_jvm (void) /* {{{ */
1942 JavaVMInitArgs vm_args;
1943 JavaVMOption vm_options[jvm_argc];
1951 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1954 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1955 "with status %i.", status);
1961 memset (&vm_args, 0, sizeof (vm_args));
1962 vm_args.version = JNI_VERSION_1_2;
1963 vm_args.options = vm_options;
1964 vm_args.nOptions = (jint) jvm_argc;
1966 for (i = 0; i < jvm_argc; i++)
1968 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
1970 vm_args.options[i].optionString = jvm_argv[i];
1973 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
1976 ERROR ("java plugin: cjni_create_jvm: "
1977 "JNI_CreateJavaVM failed with status %i.",
1981 assert (jvm != NULL);
1982 assert (jvm_env != NULL);
1984 /* Call RegisterNatives */
1985 status = cjni_init_native (jvm_env);
1988 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
1992 DEBUG ("java plugin: The JVM has been created.");
1994 } /* }}} int cjni_create_jvm */
1996 /* Increase the reference counter to the JVM for this thread. If it was zero,
1997 * attach the JVM first. */
1998 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2000 cjni_jvm_env_t *cjni_env;
2003 /* If we're the first thread to access the JVM, we'll have to create it
2009 status = cjni_create_jvm ();
2012 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2016 assert (jvm != NULL);
2018 cjni_env = pthread_getspecific (jvm_env_key);
2019 if (cjni_env == NULL)
2021 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2022 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
2023 if (cjni_env == NULL)
2025 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
2028 memset (cjni_env, 0, sizeof (*cjni_env));
2029 cjni_env->reference_counter = 0;
2030 cjni_env->jvm_env = NULL;
2032 pthread_setspecific (jvm_env_key, cjni_env);
2035 if (cjni_env->reference_counter > 0)
2037 cjni_env->reference_counter++;
2038 jvm_env = cjni_env->jvm_env;
2043 JavaVMAttachArgs args;
2045 assert (cjni_env->jvm_env == NULL);
2047 memset (&args, 0, sizeof (args));
2048 args.version = JNI_VERSION_1_2;
2050 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2053 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2054 "with status %i.", status);
2058 cjni_env->reference_counter = 1;
2059 cjni_env->jvm_env = jvm_env;
2062 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2063 cjni_env->reference_counter);
2064 assert (jvm_env != NULL);
2066 } /* }}} JNIEnv *cjni_thread_attach */
2068 /* Decrease the reference counter of this thread. If it reaches zero, detach
2070 static int cjni_thread_detach (void) /* {{{ */
2072 cjni_jvm_env_t *cjni_env;
2075 cjni_env = pthread_getspecific (jvm_env_key);
2076 if (cjni_env == NULL)
2078 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2082 assert (cjni_env->reference_counter > 0);
2083 assert (cjni_env->jvm_env != NULL);
2085 cjni_env->reference_counter--;
2086 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2087 cjni_env->reference_counter);
2089 if (cjni_env->reference_counter > 0)
2092 status = (*jvm)->DetachCurrentThread (jvm);
2095 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2096 "with status %i.", status);
2099 cjni_env->reference_counter = 0;
2100 cjni_env->jvm_env = NULL;
2103 } /* }}} JNIEnv *cjni_thread_attach */
2105 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2109 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2111 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2117 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2118 "`LoadPlugin' options! The JVM is already started and I have to "
2119 "ignore this argument: %s",
2120 ci->values[0].value.string);
2124 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2127 ERROR ("java plugin: realloc failed.");
2132 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2133 if (jvm_argv[jvm_argc] == NULL)
2135 ERROR ("java plugin: strdup failed.");
2141 } /* }}} int cjni_config_add_jvm_arg */
2143 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2146 java_plugin_class_t *class;
2147 jmethodID constructor_id;
2150 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2152 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2156 jvm_env = cjni_thread_attach ();
2157 if (jvm_env == NULL)
2160 class = (java_plugin_class_t *) realloc (java_classes_list,
2161 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2164 ERROR ("java plugin: realloc failed.");
2165 cjni_thread_detach ();
2168 java_classes_list = class;
2169 class = java_classes_list + java_classes_list_len;
2171 memset (class, 0, sizeof (*class));
2172 class->name = strdup (ci->values[0].value.string);
2173 if (class->name == NULL)
2175 ERROR ("java plugin: strdup failed.");
2176 cjni_thread_detach ();
2179 class->class = NULL;
2180 class->object = NULL;
2182 DEBUG ("java plugin: Loading class %s", class->name);
2184 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2185 if (class->class == NULL)
2187 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2189 cjni_thread_detach ();
2194 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2196 if (constructor_id == NULL)
2198 ERROR ("java plugin: cjni_config_load_plugin: "
2199 "Could not find the constructor for `%s'.",
2201 cjni_thread_detach ();
2206 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2208 if (tmp_object != NULL)
2209 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2211 class->object = NULL;
2212 if (class->object == NULL)
2214 ERROR ("java plugin: cjni_config_load_plugin: "
2215 "Could create a new `%s' object.",
2217 cjni_thread_detach ();
2222 cjni_thread_detach ();
2224 java_classes_list_len++;
2227 } /* }}} int cjni_config_load_plugin */
2229 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2232 cjni_callback_info_t *cbi;
2241 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2243 WARNING ("java plugin: `Plugin' blocks "
2244 "need exactly one string argument.");
2248 name = ci->values[0].value.string;
2251 for (i = 0; i < java_callbacks_num; i++)
2253 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2256 if (strcmp (name, java_callbacks[i].name) != 0)
2259 cbi = java_callbacks + i;
2265 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2266 "configuration callback has been registered. Please make sure, the "
2267 "`LoadPlugin' lines precede the `Plugin' blocks.",
2272 DEBUG ("java plugin: Configuring %s", name);
2274 jvm_env = cjni_thread_attach ();
2275 if (jvm_env == NULL)
2278 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2279 if (o_ocitem == NULL)
2281 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2282 cjni_thread_detach ();
2286 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2287 method = (*jvm_env)->GetMethodID (jvm_env, class,
2288 "config", "(Lorg/collectd/api/OConfigItem;)I");
2290 status = (*jvm_env)->CallIntMethod (jvm_env,
2291 cbi->object, method, o_ocitem);
2293 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2294 cjni_thread_detach ();
2296 } /* }}} int cjni_config_plugin_block */
2298 static int cjni_config (oconfig_item_t *ci) /* {{{ */
2308 for (i = 0; i < ci->children_num; i++)
2310 oconfig_item_t *child = ci->children + i;
2312 if (strcasecmp ("JVMArg", child->key) == 0)
2314 status = cjni_config_add_jvm_arg (child);
2320 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2322 status = cjni_config_load_plugin (child);
2328 else if (strcasecmp ("Plugin", child->key) == 0)
2330 status = cjni_config_plugin_block (child);
2338 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2343 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2344 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2346 if ((success == 0) && (errors > 0))
2348 ERROR ("java plugin: All statements failed.");
2353 } /* }}} int cjni_config */
2355 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2356 * and `cjni_write'. In particular, delete the global reference to the Java
2358 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2361 cjni_callback_info_t *cbi;
2363 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2365 cbi = (cjni_callback_info_t *) arg;
2367 /* This condition can occurr when shutting down. */
2377 jvm_env = cjni_thread_attach ();
2378 if (jvm_env == NULL)
2380 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2384 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2391 cjni_thread_detach ();
2392 } /* }}} void cjni_callback_info_destroy */
2394 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2395 static int cjni_read (user_data_t *ud) /* {{{ */
2398 cjni_callback_info_t *cbi;
2404 ERROR ("java plugin: cjni_read: jvm == NULL");
2408 if ((ud == NULL) || (ud->data == NULL))
2410 ERROR ("java plugin: cjni_read: Invalid user data.");
2414 jvm_env = cjni_thread_attach ();
2415 if (jvm_env == NULL)
2418 cbi = (cjni_callback_info_t *) ud->data;
2420 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2423 status = cjni_thread_detach ();
2426 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2430 return (ret_status);
2431 } /* }}} int cjni_read */
2433 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2434 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2438 cjni_callback_info_t *cbi;
2445 ERROR ("java plugin: cjni_write: jvm == NULL");
2449 if ((ud == NULL) || (ud->data == NULL))
2451 ERROR ("java plugin: cjni_write: Invalid user data.");
2455 jvm_env = cjni_thread_attach ();
2456 if (jvm_env == NULL)
2459 cbi = (cjni_callback_info_t *) ud->data;
2461 vl_java = ctoj_value_list (jvm_env, ds, vl);
2462 if (vl_java == NULL)
2464 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2468 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2469 cbi->object, cbi->method, vl_java);
2471 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2473 status = cjni_thread_detach ();
2476 ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2480 return (ret_status);
2481 } /* }}} int cjni_write */
2483 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2484 static int cjni_flush (int timeout, const char *identifier, /* {{{ */
2488 cjni_callback_info_t *cbi;
2489 jobject o_identifier;
2495 ERROR ("java plugin: cjni_flush: jvm == NULL");
2499 if ((ud == NULL) || (ud->data == NULL))
2501 ERROR ("java plugin: cjni_flush: Invalid user data.");
2505 jvm_env = cjni_thread_attach ();
2506 if (jvm_env == NULL)
2509 cbi = (cjni_callback_info_t *) ud->data;
2511 o_identifier = NULL;
2512 if (identifier != NULL)
2514 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2515 if (o_identifier == NULL)
2517 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2522 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2523 cbi->object, cbi->method, (jint) timeout, o_identifier);
2525 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2527 status = cjni_thread_detach ();
2530 ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2534 return (ret_status);
2535 } /* }}} int cjni_flush */
2537 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2538 static void cjni_log (int severity, const char *message, /* {{{ */
2542 cjni_callback_info_t *cbi;
2548 if ((ud == NULL) || (ud->data == NULL))
2551 jvm_env = cjni_thread_attach ();
2552 if (jvm_env == NULL)
2555 cbi = (cjni_callback_info_t *) ud->data;
2557 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2558 if (o_message == NULL)
2561 (*jvm_env)->CallVoidMethod (jvm_env,
2562 cbi->object, cbi->method, (jint) severity, o_message);
2564 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2566 cjni_thread_detach ();
2567 } /* }}} void cjni_log */
2569 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2571 static int cjni_notification (const notification_t *n, /* {{{ */
2575 cjni_callback_info_t *cbi;
2576 jobject o_notification;
2582 ERROR ("java plugin: cjni_read: jvm == NULL");
2586 if ((ud == NULL) || (ud->data == NULL))
2588 ERROR ("java plugin: cjni_read: Invalid user data.");
2592 jvm_env = cjni_thread_attach ();
2593 if (jvm_env == NULL)
2596 cbi = (cjni_callback_info_t *) ud->data;
2598 o_notification = ctoj_notification (jvm_env, n);
2599 if (o_notification == NULL)
2601 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2605 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2606 cbi->object, cbi->method, o_notification);
2608 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2610 status = cjni_thread_detach ();
2613 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2617 return (ret_status);
2618 } /* }}} int cjni_notification */
2620 /* Callbacks for matches implemented in Java */
2621 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2625 cjni_callback_info_t *cbi_ret;
2626 cjni_callback_info_t *cbi_factory;
2637 #define BAIL_OUT(status) \
2638 if (cbi_ret != NULL) { \
2639 free (cbi_ret->name); \
2640 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2641 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2644 if (jvm_env != NULL) { \
2646 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2647 cjni_thread_detach (); \
2653 ERROR ("java plugin: cjni_read: jvm == NULL");
2657 jvm_env = cjni_thread_attach ();
2658 if (jvm_env == NULL)
2663 /* Find out whether to create a match or a target. */
2664 if (strcasecmp ("Match", ci->key) == 0)
2665 type = CB_TYPE_MATCH;
2666 else if (strcasecmp ("Target", ci->key) == 0)
2667 type = CB_TYPE_TARGET;
2670 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2671 "to create a match or a target.");
2675 /* This is the name of the match we should create. */
2676 name = ci->values[0].value.string;
2678 /* Lets see if we have a matching factory here.. */
2680 for (i = 0; i < java_callbacks_num; i++)
2682 if (java_callbacks[i].type != type)
2685 if (strcmp (name, java_callbacks[i].name) != 0)
2688 cbi_factory = java_callbacks + i;
2692 /* Nope, no factory for that name.. */
2693 if (cbi_factory == NULL)
2695 ERROR ("java plugin: cjni_match_target_create: "
2696 "No such match factory registered: %s",
2701 /* We convert `ci' to its Java equivalent.. */
2702 o_ci = ctoj_oconfig_item (jvm_env, ci);
2705 ERROR ("java plugin: cjni_match_target_create: "
2706 "ctoj_oconfig_item failed.");
2710 /* Allocate a new callback info structure. This is going to be our user_data
2712 cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
2713 if (cbi_ret == NULL)
2715 ERROR ("java plugin: cjni_match_target_create: malloc failed.");
2718 memset (cbi_ret, 0, sizeof (*cbi_ret));
2719 cbi_ret->object = NULL;
2720 cbi_ret->type = type;
2722 /* Lets fill the callback info structure.. First, the name: */
2723 cbi_ret->name = strdup (name);
2724 if (cbi_ret->name == NULL)
2726 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2730 /* Then call the factory method so it creates a new object for us. */
2731 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2732 cbi_factory->object, cbi_factory->method, o_ci);
2735 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2739 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2742 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2746 /* This is the class of the match. It is possibly different from the class of
2747 * the match-factory! */
2748 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2749 if (cbi_ret->class == NULL)
2751 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2755 /* Lookup the `int match (DataSet, ValueList)' method. */
2756 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2757 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2758 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2759 if (cbi_ret->method == NULL)
2761 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2765 /* Return the newly created match via the user_data pointer. */
2766 *user_data = (void *) cbi_ret;
2768 cjni_thread_detach ();
2770 DEBUG ("java plugin: cjni_match_target_create: "
2771 "Successfully created a `%s' %s.",
2772 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2777 } /* }}} int cjni_match_target_create */
2779 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2781 cjni_callback_info_destroy (*user_data);
2785 } /* }}} int cjni_match_target_destroy */
2787 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2788 value_list_t *vl, notification_meta_t **meta, void **user_data)
2791 cjni_callback_info_t *cbi;
2799 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2803 jvm_env = cjni_thread_attach ();
2804 if (jvm_env == NULL)
2807 cbi = (cjni_callback_info_t *) *user_data;
2809 o_vl = ctoj_value_list (jvm_env, ds, vl);
2812 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2813 cjni_thread_detach ();
2817 o_ds = ctoj_data_set (jvm_env, ds);
2820 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2821 cjni_thread_detach ();
2825 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2828 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2830 /* If we're executing a target, copy the `ValueList' back to our
2831 * `value_list_t'. */
2832 if (cbi->type == CB_TYPE_TARGET)
2834 value_list_t new_vl;
2836 memset (&new_vl, 0, sizeof (new_vl));
2837 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2840 ERROR ("java plugin: cjni_match_target_invoke: "
2841 "jtoc_value_list failed.");
2843 else /* if (status == 0) */
2845 /* plugin_dispatch_values assures that this is dynamically allocated
2849 /* This will replace the vl->values pointer to a new, dynamically
2850 * allocated piece of memory. */
2851 memcpy (vl, &new_vl, sizeof (*vl));
2853 } /* if (cbi->type == CB_TYPE_TARGET) */
2855 status = cjni_thread_detach ();
2857 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2859 return (ret_status);
2860 } /* }}} int cjni_match_target_invoke */
2862 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2863 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2868 for (i = 0; i < java_callbacks_num; i++)
2870 if (java_callbacks[i].type != CB_TYPE_INIT)
2873 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2875 status = (*jvm_env)->CallIntMethod (jvm_env,
2876 java_callbacks[i].object, java_callbacks[i].method);
2879 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2880 "Removing read function.",
2881 java_callbacks[i].name, status);
2882 plugin_unregister_read (java_callbacks[i].name);
2887 } /* }}} int cjni_init_plugins */
2889 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2890 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2895 for (i = 0; i < java_callbacks_num; i++)
2897 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2900 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2902 status = (*jvm_env)->CallIntMethod (jvm_env,
2903 java_callbacks[i].object, java_callbacks[i].method);
2906 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2907 java_callbacks[i].name, status);
2912 } /* }}} int cjni_shutdown_plugins */
2915 static int cjni_shutdown (void) /* {{{ */
2918 JavaVMAttachArgs args;
2926 memset (&args, 0, sizeof (args));
2927 args.version = JNI_VERSION_1_2;
2929 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
2932 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2937 /* Execute all the shutdown functions registered by plugins. */
2938 cjni_shutdown_plugins (jvm_env);
2940 /* Release all the global references to callback functions */
2941 for (i = 0; i < java_callbacks_num; i++)
2943 if (java_callbacks[i].object != NULL)
2945 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
2946 java_callbacks[i].object = NULL;
2948 sfree (java_callbacks[i].name);
2950 java_callbacks_num = 0;
2951 sfree (java_callbacks);
2953 /* Release all the global references to directly loaded classes. */
2954 for (i = 0; i < java_classes_list_len; i++)
2956 if (java_classes_list[i].object != NULL)
2958 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
2959 java_classes_list[i].object = NULL;
2961 sfree (java_classes_list[i].name);
2963 java_classes_list_len = 0;
2964 sfree (java_classes_list);
2966 /* Destroy the JVM */
2967 DEBUG ("java plugin: Destroying the JVM.");
2968 (*jvm)->DestroyJavaVM (jvm);
2972 pthread_key_delete (jvm_env_key);
2974 /* Free the JVM argument list */
2975 for (i = 0; i < jvm_argc; i++)
2976 sfree (jvm_argv[i]);
2981 } /* }}} int cjni_shutdown */
2983 /* Initialization: Create a JVM, load all configured classes and call their
2984 * `config' and `init' callback methods. */
2985 static int cjni_init (void) /* {{{ */
2991 ERROR ("java plugin: cjni_init: jvm == NULL");
2995 jvm_env = cjni_thread_attach ();
2996 if (jvm_env == NULL)
2999 cjni_init_plugins (jvm_env);
3001 cjni_thread_detach ();
3003 } /* }}} int cjni_init */
3005 void module_register (void)
3007 plugin_register_complex_config ("java", cjni_config);
3008 plugin_register_init ("java", cjni_init);
3009 plugin_register_shutdown ("java", cjni_shutdown);
3010 } /* void module_register (void) */
3012 /* vim: set sw=2 sts=2 et fdm=marker : */