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>
31 #if !defined(JNI_VERSION_1_2)
32 # error "Need JNI 1.2 compatible interface!"
38 struct java_plugin_s /* {{{ */
46 #define CJNI_FLAG_ENABLED 0x0001
55 typedef struct java_plugin_s java_plugin_t;
58 struct cjni_jvm_env_s /* {{{ */
61 int reference_counter;
63 typedef struct cjni_jvm_env_s cjni_jvm_env_t;
69 static JavaVM *jvm = NULL;
70 static pthread_key_t jvm_env_key;
72 static char **jvm_argv = NULL;
73 static size_t jvm_argc = 0;
75 static java_plugin_t *java_plugins = NULL;
76 static size_t java_plugins_num = 0;
81 * Mostly functions that are needed by the Java interface functions.
83 static void cjni_objectref_destroy (void *arg);
84 static int cjni_read (user_data_t *user_data);
87 * C to Java conversion functions
89 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
91 jclass class_ptr, jobject object_ptr, const char *method_name)
96 /* Create a java.lang.String */
97 o_string = (*jvm_env)->NewStringUTF (jvm_env,
98 (string != NULL) ? string : "");
101 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
105 /* Search for the `void setFoo (String s)' method. */
106 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
107 method_name, "(Ljava/lang/String;)V");
110 ERROR ("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
112 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
116 /* Call the method. */
117 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, o_string);
119 /* Decrease reference counter on the java.lang.String object. */
120 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
123 } /* }}} int ctoj_string */
125 static int ctoj_int (JNIEnv *jvm_env, /* {{{ */
127 jclass class_ptr, jobject object_ptr, const char *method_name)
131 /* Search for the `void setFoo (int i)' method. */
132 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
133 method_name, "(I)V");
136 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
141 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
144 } /* }}} int ctoj_int */
146 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
148 jclass class_ptr, jobject object_ptr, const char *method_name)
152 /* Search for the `void setFoo (long l)' method. */
153 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
154 method_name, "(J)V");
157 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
162 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
165 } /* }}} int ctoj_long */
167 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
169 jclass class_ptr, jobject object_ptr, const char *method_name)
173 /* Search for the `void setFoo (double d)' method. */
174 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
175 method_name, "(D)V");
178 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
183 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
186 } /* }}} int ctoj_double */
188 /* Convert a jlong to a java.lang.Number */
189 static jobject ctoj_jlong_to_number (JNIEnv *jvm_env, jlong value) /* {{{ */
192 jmethodID m_long_constructor;
194 /* Look up the java.lang.Long class */
195 c_long = (*jvm_env)->FindClass (jvm_env, "java.lang.Long");
198 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
199 "java.lang.Long class failed.");
203 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
204 c_long, "<init>", "(J)V");
205 if (m_long_constructor == NULL)
207 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
208 "`Long (long)' constructor failed.");
212 return ((*jvm_env)->NewObject (jvm_env,
213 c_long, m_long_constructor, value));
214 } /* }}} jobject ctoj_jlong_to_number */
216 /* Convert a jdouble to a java.lang.Number */
217 static jobject ctoj_jdouble_to_number (JNIEnv *jvm_env, jdouble value) /* {{{ */
220 jmethodID m_double_constructor;
222 /* Look up the java.lang.Long class */
223 c_double = (*jvm_env)->FindClass (jvm_env, "java.lang.Double");
224 if (c_double == NULL)
226 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
227 "java.lang.Double class failed.");
231 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
232 c_double, "<init>", "(D)V");
233 if (m_double_constructor == NULL)
235 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
236 "`Double (double)' constructor failed.");
240 return ((*jvm_env)->NewObject (jvm_env,
241 c_double, m_double_constructor, value));
242 } /* }}} jobject ctoj_jdouble_to_number */
244 /* Convert a value_t to a java.lang.Number */
245 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
246 value_t value, int ds_type)
248 if (ds_type == DS_TYPE_COUNTER)
249 return (ctoj_jlong_to_number (jvm_env, (jlong) value.counter));
250 else if (ds_type == DS_TYPE_GAUGE)
251 return (ctoj_jdouble_to_number (jvm_env, (jdouble) value.gauge));
254 } /* }}} jobject ctoj_value_to_number */
256 /* Convert a data_source_t to a org.collectd.api.DataSource */
257 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
258 const data_source_t *dsrc)
261 jmethodID m_datasource_constructor;
262 jobject o_datasource;
265 /* Look up the DataSource class */
266 c_datasource = (*jvm_env)->FindClass (jvm_env,
267 "org.collectd.api.DataSource");
268 if (c_datasource == NULL)
270 ERROR ("java plugin: ctoj_data_source: "
271 "FindClass (org.collectd.api.DataSource) failed.");
275 /* Lookup the `ValueList ()' constructor. */
276 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
278 if (m_datasource_constructor == NULL)
280 ERROR ("java plugin: ctoj_data_source: Cannot find the "
281 "`DataSource ()' constructor.");
285 /* Create a new instance. */
286 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
287 m_datasource_constructor);
288 if (o_datasource == NULL)
290 ERROR ("java plugin: ctoj_data_source: "
291 "Creating a new DataSource instance failed.");
295 /* Set name via `void setName (String name)' */
296 status = ctoj_string (jvm_env, dsrc->name,
297 c_datasource, o_datasource, "setName");
300 ERROR ("java plugin: ctoj_data_source: "
301 "ctoj_string (setName) failed.");
302 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
306 /* Set type via `void setType (int type)' */
307 status = ctoj_int (jvm_env, dsrc->type,
308 c_datasource, o_datasource, "setType");
311 ERROR ("java plugin: ctoj_data_source: "
312 "ctoj_int (setType) failed.");
313 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
317 /* Set min via `void setMin (double min)' */
318 status = ctoj_double (jvm_env, dsrc->min,
319 c_datasource, o_datasource, "setMin");
322 ERROR ("java plugin: ctoj_data_source: "
323 "ctoj_double (setMin) failed.");
324 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
328 /* Set max via `void setMax (double max)' */
329 status = ctoj_double (jvm_env, dsrc->max,
330 c_datasource, o_datasource, "setMax");
333 ERROR ("java plugin: ctoj_data_source: "
334 "ctoj_double (setMax) failed.");
335 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
339 return (o_datasource);
340 } /* }}} jobject ctoj_data_source */
342 /* Convert a oconfig_value_t to a org.collectd.api.OConfigValue */
343 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
344 oconfig_value_t ocvalue)
347 jmethodID m_ocvalue_constructor;
351 m_ocvalue_constructor = NULL;
354 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
355 "org.collectd.api.OConfigValue");
356 if (c_ocvalue == NULL)
358 ERROR ("java plugin: ctoj_oconfig_value: "
359 "FindClass (org.collectd.api.OConfigValue) failed.");
363 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
365 jboolean tmp_boolean;
367 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
369 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
371 if (m_ocvalue_constructor == NULL)
373 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
374 "`OConfigValue (boolean)' constructor.");
378 return ((*jvm_env)->NewObject (jvm_env,
379 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
380 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
381 else if (ocvalue.type == OCONFIG_TYPE_STRING)
383 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
384 "<init>", "(Ljava/lang/String;)V");
385 if (m_ocvalue_constructor == NULL)
387 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
388 "`OConfigValue (String)' constructor.");
392 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
393 if (o_argument == NULL)
395 ERROR ("java plugin: ctoj_oconfig_value: "
396 "Creating a String object failed.");
400 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
402 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
403 "<init>", "(Ljava/lang/Number;)V");
404 if (m_ocvalue_constructor == NULL)
406 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
407 "`OConfigValue (Number)' constructor.");
411 o_argument = ctoj_jdouble_to_number (jvm_env,
412 (jdouble) ocvalue.value.number);
413 if (o_argument == NULL)
415 ERROR ("java plugin: ctoj_oconfig_value: "
416 "Creating a Number object failed.");
425 assert (m_ocvalue_constructor != NULL);
426 assert (o_argument != NULL);
428 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
429 c_ocvalue, m_ocvalue_constructor, o_argument);
430 if (o_ocvalue == NULL)
432 ERROR ("java plugin: ctoj_oconfig_value: "
433 "Creating an OConfigValue object failed.");
434 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
438 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
440 } /* }}} jobject ctoj_oconfig_value */
442 /* Convert a oconfig_item_t to a org.collectd.api.OConfigItem */
443 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
444 const oconfig_item_t *ci)
447 jmethodID m_ocitem_constructor;
448 jmethodID m_addvalue;
449 jmethodID m_addchild;
454 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.OConfigItem");
455 if (c_ocitem == NULL)
457 ERROR ("java plugin: ctoj_oconfig_item: "
458 "FindClass (org.collectd.api.OConfigItem) failed.");
462 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
464 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
465 "<init>", "(Ljava/lang/String;)V");
466 if (m_ocitem_constructor == NULL)
468 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
469 "`OConfigItem (String)' constructor.");
473 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
474 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
475 if (m_addvalue == NULL)
477 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
478 "`addValue (OConfigValue)' method.");
482 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
483 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
484 if (m_addchild == NULL)
486 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
487 "`addChild (OConfigItem)' method.");
492 /* Create a String object with the key.
493 * Needed for calling the constructor. */
494 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
497 ERROR ("java plugin: ctoj_oconfig_item: "
498 "Creating String object failed.");
502 /* Create an OConfigItem object */
503 o_ocitem = (*jvm_env)->NewObject (jvm_env,
504 c_ocitem, m_ocitem_constructor, o_key);
505 if (o_ocitem == NULL)
507 ERROR ("java plugin: ctoj_oconfig_item: "
508 "Creating an OConfigItem object failed.");
509 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
513 /* We don't need the String object any longer.. */
514 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
516 /* Call OConfigItem.addValue for each value */
517 for (i = 0; i < ci->values_num; i++) /* {{{ */
521 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
524 ERROR ("java plugin: ctoj_oconfig_item: "
525 "Creating an OConfigValue object failed.");
526 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
530 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
531 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
532 } /* }}} for (i = 0; i < ci->values_num; i++) */
534 /* Call OConfigItem.addChild for each child */
535 for (i = 0; i < ci->children_num; i++) /* {{{ */
539 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
542 ERROR ("java plugin: ctoj_oconfig_item: "
543 "Creating an OConfigItem object failed.");
544 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
548 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_child);
549 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
550 } /* }}} for (i = 0; i < ci->children_num; i++) */
553 } /* }}} jobject ctoj_oconfig_item */
555 /* Convert a data_set_t to a org.collectd.api.DataSet */
556 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
559 jmethodID m_constructor;
565 /* Look up the org.collectd.api.DataSet class */
566 c_dataset = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.DataSet");
567 if (c_dataset == NULL)
569 ERROR ("java plugin: ctoj_data_set: Looking up the "
570 "org.collectd.api.DataSet class failed.");
574 /* Search for the `DataSet (String type)' constructor. */
575 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
576 c_dataset, "<init>", "(Ljava.lang.String;)V");
577 if (m_constructor == NULL)
579 ERROR ("java plugin: ctoj_data_set: Looking up the "
580 "`DataSet (String)' constructor failed.");
584 /* Search for the `void addDataSource (DataSource)' method. */
585 m_add = (*jvm_env)->GetMethodID (jvm_env,
586 c_dataset, "addDataSource", "(Lorg.collectd.api.DataSource;)V");
589 ERROR ("java plugin: ctoj_data_set: Looking up the "
590 "`addDataSource (DataSource)' method failed.");
594 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
597 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
601 o_dataset = (*jvm_env)->NewObject (jvm_env,
602 c_dataset, m_constructor, o_type);
603 if (o_dataset == NULL)
605 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
606 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
610 /* Decrease reference counter on the java.lang.String object. */
611 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
613 for (i = 0; i < ds->ds_num; i++)
615 jobject o_datasource;
617 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
618 if (o_datasource == NULL)
620 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
621 ds->type, ds->ds[i].name);
622 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
626 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
628 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
629 } /* for (i = 0; i < ds->ds_num; i++) */
632 } /* }}} jobject ctoj_data_set */
634 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
635 value_t value, int ds_type,
636 jclass class_ptr, jobject object_ptr)
638 jmethodID m_addvalue;
641 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
642 "addValue", "(Ljava/lang/Number;)V");
643 if (m_addvalue == NULL)
645 ERROR ("java plugin: ctoj_value_list_add_value: "
646 "Cannot find method `void addValue (Number)'.");
650 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
651 if (o_number == NULL)
653 ERROR ("java plugin: ctoj_value_list_add_value: "
654 "ctoj_value_to_number failed.");
658 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
660 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
663 } /* }}} int ctoj_value_list_add_value */
665 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
666 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
668 jmethodID m_setdataset;
671 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
672 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
673 "setDataSet", "(Lorg.collectd.api.DataSet;)V");
674 if (m_setdataset == NULL)
676 ERROR ("java plugin: ctoj_value_list_add_data_set: "
677 "Cannot find the `void setDataSet (DataSet)' method.");
681 /* Create a DataSet object. */
682 o_dataset = ctoj_data_set (jvm_env, ds);
683 if (o_dataset == NULL)
685 ERROR ("java plugin: ctoj_value_list_add_data_set: "
686 "ctoj_data_set (%s) failed.", ds->type);
690 /* Actually call the method. */
691 (*jvm_env)->CallVoidMethod (jvm_env,
692 o_valuelist, m_setdataset, o_dataset);
694 /* Decrease reference counter on the List<DataSource> object. */
695 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
698 } /* }}} int ctoj_value_list_add_data_set */
700 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
701 const data_set_t *ds, const value_list_t *vl)
704 jmethodID m_valuelist_constructor;
709 /* First, create a new ValueList instance..
710 * Look up the class.. */
711 c_valuelist = (*jvm_env)->FindClass (jvm_env,
712 "org.collectd.api.ValueList");
713 if (c_valuelist == NULL)
715 ERROR ("java plugin: ctoj_value_list: "
716 "FindClass (org.collectd.api.ValueList) failed.");
720 /* Lookup the `ValueList ()' constructor. */
721 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
723 if (m_valuelist_constructor == NULL)
725 ERROR ("java plugin: ctoj_value_list: Cannot find the "
726 "`ValueList ()' constructor.");
730 /* Create a new instance. */
731 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
732 m_valuelist_constructor);
733 if (o_valuelist == NULL)
735 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
740 status = ctoj_value_list_add_data_set (jvm_env,
741 c_valuelist, o_valuelist, ds);
744 ERROR ("java plugin: ctoj_value_list: "
745 "ctoj_value_list_add_data_set failed.");
746 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
750 /* Set the strings.. */
751 #define SET_STRING(str,method_name) do { \
752 status = ctoj_string (jvm_env, str, \
753 c_valuelist, o_valuelist, method_name); \
755 ERROR ("java plugin: ctoj_value_list: jtoc_string (%s) failed.", \
757 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
761 SET_STRING (vl->host, "setHost");
762 SET_STRING (vl->plugin, "setPlugin");
763 SET_STRING (vl->plugin_instance, "setPluginInstance");
764 SET_STRING (vl->type, "setType");
765 SET_STRING (vl->type_instance, "setTypeInstance");
769 /* Set the `time' member. Java stores time in milliseconds. */
770 status = ctoj_long (jvm_env, ((jlong) vl->time) * ((jlong) 1000),
771 c_valuelist, o_valuelist, "setTime");
774 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
775 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
779 /* Set the `interval' member.. */
780 status = ctoj_long (jvm_env, (jlong) vl->interval,
781 c_valuelist, o_valuelist, "setInterval");
784 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
785 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
789 for (i = 0; i < vl->values_len; i++)
791 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
792 c_valuelist, o_valuelist);
795 ERROR ("java plugin: ctoj_value_list: "
796 "ctoj_value_list_add_value failed.");
797 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
802 return (o_valuelist);
803 } /* }}} int ctoj_value_list */
806 * Java to C conversion functions
808 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
809 char *buffer, size_t buffer_size,
810 jclass class_ptr, jobject object_ptr, const char *method_name)
816 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
817 method_name, "()Ljava/lang/String;");
818 if (method_id == NULL)
820 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
825 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
826 if (string_obj == NULL)
828 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
833 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
836 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
837 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
841 sstrncpy (buffer, c_str, buffer_size);
843 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
844 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
847 } /* }}} int jtoc_string */
849 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
851 jclass class_ptr, jobject object_ptr, const char *method_name)
855 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
857 if (method_id == NULL)
859 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
864 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
867 } /* }}} int jtoc_long */
869 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
871 jclass class_ptr, jobject object_ptr, const char *method_name)
875 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
877 if (method_id == NULL)
879 ERROR ("java plugin: jtoc_string: Cannot find method `double %s ()'.",
884 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
887 } /* }}} int jtoc_double */
889 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
890 value_t *ret_value, int ds_type, jobject object_ptr)
895 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
897 if (ds_type == DS_TYPE_COUNTER)
901 status = jtoc_long (jvm_env, &tmp_long,
902 class_ptr, object_ptr, "longValue");
905 ERROR ("java plugin: jtoc_value: "
906 "jtoc_long failed.");
909 (*ret_value).counter = (counter_t) tmp_long;
915 status = jtoc_double (jvm_env, &tmp_double,
916 class_ptr, object_ptr, "doubleValue");
919 ERROR ("java plugin: jtoc_value: "
920 "jtoc_double failed.");
923 (*ret_value).gauge = (gauge_t) tmp_double;
927 } /* }}} int jtoc_value */
929 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
930 const data_set_t *ds, value_list_t *vl,
931 jclass class_ptr, jobject object_ptr)
933 jmethodID m_getvalues;
936 jobjectArray o_number_array;
942 values_num = ds->ds_num;
945 o_number_array = NULL;
948 #define BAIL_OUT(status) \
950 if (o_number_array != NULL) \
951 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
952 if (o_list != NULL) \
953 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
956 /* Call: List<Number> ValueList.getValues () */
957 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
958 "getValues", "()Ljava/util/List;");
959 if (m_getvalues == NULL)
961 ERROR ("java plugin: jtoc_values_array: "
962 "Cannot find method `List getValues ()'.");
966 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
969 ERROR ("java plugin: jtoc_values_array: "
970 "CallObjectMethod (getValues) failed.");
974 /* Call: Number[] List.toArray () */
975 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
976 (*jvm_env)->GetObjectClass (jvm_env, o_list),
977 "toArray", "()[Ljava/lang/Object;");
978 if (m_toarray == NULL)
980 ERROR ("java plugin: jtoc_values_array: "
981 "Cannot find method `Object[] toArray ()'.");
985 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
986 if (o_number_array == NULL)
988 ERROR ("java plugin: jtoc_values_array: "
989 "CallObjectMethod (toArray) failed.");
993 values = calloc (values_num, sizeof (value_t));
996 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1000 for (i = 0; i < values_num; i++)
1005 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1006 o_number_array, (jsize) i);
1007 if (o_number == NULL)
1009 ERROR ("java plugin: jtoc_values_array: "
1010 "GetObjectArrayElement (%i) failed.", i);
1014 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1017 ERROR ("java plugin: jtoc_values_array: "
1018 "jtoc_value (%i) failed.", i);
1021 } /* for (i = 0; i < values_num; i++) */
1023 vl->values = values;
1024 vl->values_len = values_num;
1027 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1028 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1030 } /* }}} int jtoc_values_array */
1032 /* Convert a org.collectd.api.ValueList to a value_list_t. */
1033 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1039 const data_set_t *ds;
1041 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1042 if (class_ptr == NULL)
1044 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1048 #define SET_STRING(buffer,method) do { \
1049 status = jtoc_string (jvm_env, buffer, sizeof (buffer), \
1050 class_ptr, object_ptr, method); \
1051 if (status != 0) { \
1052 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1057 SET_STRING(vl->type, "getType");
1059 ds = plugin_get_ds (vl->type);
1062 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1063 "Please consult the types.db(5) manpage for mor information.",
1068 SET_STRING(vl->host, "getHost");
1069 SET_STRING(vl->plugin, "getPlugin");
1070 SET_STRING(vl->plugin_instance, "getPluginInstance");
1071 SET_STRING(vl->type_instance, "getTypeInstance");
1075 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1078 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1081 /* Java measures time in milliseconds. */
1082 vl->time = (time_t) (tmp_long / ((jlong) 1000));
1084 status = jtoc_long (jvm_env, &tmp_long,
1085 class_ptr, object_ptr, "getInterval");
1088 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1091 vl->interval = (int) tmp_long;
1093 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1096 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1101 } /* }}} int jtoc_value_list */
1104 * Functions accessible from Java
1106 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1107 jobject this, jobject java_vl)
1109 value_list_t vl = VALUE_LIST_INIT;
1112 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1114 status = jtoc_value_list (jvm_env, &vl, java_vl);
1117 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1121 status = plugin_dispatch_values (&vl);
1126 } /* }}} jint cjni_api_dispatch_values */
1128 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1129 jobject this, jobject o_string_type)
1131 const char *ds_name;
1132 const data_set_t *ds;
1135 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1136 if (ds_name == NULL)
1138 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1142 ds = plugin_get_ds (ds_name);
1143 DEBUG ("java plugin: cjni_api_get_ds: "
1144 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1146 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1151 o_dataset = ctoj_data_set (jvm_env, ds);
1153 } /* }}} jint cjni_api_get_ds */
1155 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1156 jobject this, jobject o_name, jobject o_read)
1161 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1164 ERROR ("java plugin: cjni_api_register_read: GetStringUTFChars failed.");
1168 DEBUG ("java plugin: cjni_api_register_read: c_name = %s;", c_name);
1170 memset (&ud, 0, sizeof (ud));
1171 ud.data = (void *) o_read;
1172 ud.free_func = cjni_objectref_destroy;
1174 (*jvm_env)->NewGlobalRef (jvm_env, o_read);
1176 plugin_register_complex_read (c_name, cjni_read, &ud);
1178 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1179 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1182 } /* }}} jint cjni_api_register_read */
1184 static JNINativeMethod jni_api_functions[] =
1187 "(Lorg/collectd/api/ValueList;)I",
1188 cjni_api_dispatch_values },
1191 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1195 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1196 cjni_api_register_read }
1198 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1199 / sizeof (jni_api_functions[0]);
1204 static JNIEnv *cjni_thread_attach (void) /* {{{ */
1206 cjni_jvm_env_t *cjni_env;
1209 cjni_env = pthread_getspecific (jvm_env_key);
1210 if (cjni_env == NULL)
1212 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
1213 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
1214 if (cjni_env == NULL)
1216 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
1219 memset (cjni_env, 0, sizeof (*cjni_env));
1220 cjni_env->reference_counter = 0;
1221 cjni_env->jvm_env = NULL;
1223 pthread_setspecific (jvm_env_key, cjni_env);
1226 if (cjni_env->reference_counter > 0)
1228 cjni_env->reference_counter++;
1229 jvm_env = cjni_env->jvm_env;
1234 JavaVMAttachArgs args;
1236 assert (cjni_env->jvm_env == NULL);
1238 memset (&args, 0, sizeof (args));
1239 args.version = JNI_VERSION_1_2;
1241 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
1244 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
1245 "with status %i.", status);
1249 cjni_env->reference_counter = 1;
1250 cjni_env->jvm_env = jvm_env;
1253 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
1254 cjni_env->reference_counter);
1255 assert (jvm_env != NULL);
1257 } /* }}} JNIEnv *cjni_thread_attach */
1259 static int cjni_thread_detach (void) /* {{{ */
1261 cjni_jvm_env_t *cjni_env;
1264 cjni_env = pthread_getspecific (jvm_env_key);
1265 if (cjni_env == NULL)
1267 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
1271 assert (cjni_env->reference_counter > 0);
1272 assert (cjni_env->jvm_env != NULL);
1274 cjni_env->reference_counter--;
1275 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
1276 cjni_env->reference_counter);
1278 if (cjni_env->reference_counter > 0)
1281 status = (*jvm)->DetachCurrentThread (jvm);
1284 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
1285 "with status %i.", status);
1288 cjni_env->reference_counter = 0;
1289 cjni_env->jvm_env = NULL;
1292 } /* }}} JNIEnv *cjni_thread_attach */
1294 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1296 cjni_jvm_env_t *cjni_env;
1301 cjni_env = (cjni_jvm_env_t *) args;
1303 if (cjni_env->reference_counter > 0)
1305 ERROR ("java plugin: cjni_jvm_env_destroy: "
1306 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1309 if (cjni_env->jvm_env != NULL)
1311 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1312 (void *) cjni_env->jvm_env);
1315 /* The pointer is allocated in `cjni_thread_attach' */
1317 } /* }}} void cjni_jvm_env_destroy */
1320 * Delete a global reference, set by the various `Register*' functions.
1322 static void cjni_objectref_destroy (void *arg) /* {{{ */
1327 DEBUG ("java plugin: cjni_objectref_destroy (arg = %p);", arg);
1332 jni_env = cjni_thread_attach ();
1333 if (jni_env == NULL)
1335 ERROR ("java plugin: cjni_objectref_destroy: cjni_thread_attach failed.");
1339 obj = (jobject) arg;
1341 (*jni_env)->DeleteGlobalRef (jni_env, obj);
1343 cjni_thread_detach ();
1344 } /* }}} void cjni_objectref_destroy */
1346 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
1350 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1352 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
1356 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
1359 ERROR ("java plugin: realloc failed.");
1364 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
1365 if (jvm_argv[jvm_argc] == NULL)
1367 ERROR ("java plugin: strdup failed.");
1373 } /* }}} int cjni_config_add_jvm_arg */
1375 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
1379 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1381 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
1385 jp = (java_plugin_t *) realloc (java_plugins,
1386 sizeof (*java_plugins) * (java_plugins_num + 1));
1389 ERROR ("java plugin: realloc failed.");
1393 jp = java_plugins + java_plugins_num;
1395 memset (jp, 0, sizeof (*jp));
1396 jp->class_name = strdup (ci->values[0].value.string);
1397 if (jp->class_name == NULL)
1399 ERROR ("java plugin: strdup failed.");
1403 jp->class_ptr = NULL;
1404 jp->object_ptr = NULL;
1407 jp->m_config = NULL;
1411 jp->m_shutdown = NULL;
1416 } /* }}} int cjni_config_load_plugin */
1418 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
1421 const char *class_name;
1423 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1425 WARNING ("java plugin: `Plugin' blocks "
1426 "need exactly one string argument.");
1430 class_name = ci->values[0].value.string;
1431 for (i = 0; i < java_plugins_num; i++)
1432 if (strcmp (java_plugins[i].class_name, class_name) == 0)
1435 if (i >= java_plugins_num)
1437 WARNING ("java plugin: Configuration block for the `%s' plugin found, "
1438 "but the plugin has not been loaded. Please note, that the class "
1439 "name is case-sensitive!",
1444 if (java_plugins[i].ci != NULL)
1446 WARNING ("java plugin: There are more than one <Plugin> blocks for the "
1447 "`%s' plugin. This is currently not supported - only the first block "
1453 java_plugins[i].ci = oconfig_clone (ci);
1454 if (java_plugins[i].ci == NULL)
1456 ERROR ("java plugin: cjni_config_plugin_block: "
1457 "oconfig_clone failed for `%s'.",
1462 DEBUG ("java plugin: cjni_config_plugin_block: "
1463 "Successfully copied config for `%s'.",
1467 } /* }}} int cjni_config_plugin_block */
1469 static int cjni_config (oconfig_item_t *ci) /* {{{ */
1479 for (i = 0; i < ci->children_num; i++)
1481 oconfig_item_t *child = ci->children + i;
1483 if (strcasecmp ("JVMArg", child->key) == 0)
1485 status = cjni_config_add_jvm_arg (child);
1491 else if (strcasecmp ("LoadPlugin", child->key) == 0)
1493 status = cjni_config_load_plugin (child);
1499 else if (strcasecmp ("Plugin", child->key) == 0)
1501 status = cjni_config_plugin_block (child);
1509 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
1514 if ((success == 0) && (errors > 0))
1516 ERROR ("java plugin: All statements failed.");
1521 } /* }}} int cjni_config */
1523 static int cjni_write_one_plugin (JNIEnv *jvm_env, /* {{{ */
1524 java_plugin_t *jp, jobject vl_java)
1529 || ((jp->flags & CJNI_FLAG_ENABLED) == 0)
1530 || (jp->m_write == NULL))
1533 DEBUG ("java plugin: Calling: %s.Write(ValueList)", jp->class_name);
1535 status = (*jvm_env)->CallIntMethod (jvm_env, jp->object_ptr,
1536 jp->m_write, vl_java);
1539 ERROR ("java plugin: cjni_write_one_plugin: "
1540 "Calling `Write' on an `%s' object failed with status %i.",
1541 jp->class_name, status);
1546 } /* }}} int cjni_write_one_plugin */
1548 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
1549 user_data_t __attribute__((unused)) *user_data)
1558 ERROR ("java plugin: cjni_write: jvm == NULL");
1562 if ((user_data == NULL) || (user_data->data == NULL))
1564 ERROR ("java plugin: cjni_read: Invalid user data.");
1568 jvm_env = cjni_thread_attach ();
1569 if (jvm_env == NULL)
1572 vl_java = ctoj_value_list (jvm_env, ds, vl);
1573 if (vl_java == NULL)
1575 ERROR ("java plugin: cjni_write_plugins: ctoj_value_list failed.");
1579 jp = (java_plugin_t *) user_data->data;
1581 cjni_write_one_plugin (jvm_env, jp, vl_java);
1583 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
1585 status = cjni_thread_detach ();
1590 } /* }}} int cjni_write */
1592 static int cjni_shutdown_one_plugin (JNIEnv *jvm_env, /* {{{ */
1598 || ((jp->flags & CJNI_FLAG_ENABLED) == 0)
1599 || (jp->m_shutdown == NULL))
1602 status = (*jvm_env)->CallIntMethod (jvm_env, jp->object_ptr,
1606 ERROR ("cjni_shutdown_one_plugin: Destroying an `%s' object failed "
1607 "with status %i.", jp->class_name, status);
1610 jp->flags &= ~CJNI_FLAG_ENABLED;
1613 } /* }}} int cjni_shutdown_one_plugin */
1615 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
1619 for (j = 0; j < java_plugins_num; j++)
1620 cjni_shutdown_one_plugin (jvm_env, &java_plugins[j]);
1623 } /* }}} int cjni_shutdown_plugins */
1625 static int cjni_shutdown (void) /* {{{ */
1628 JavaVMAttachArgs args;
1636 memset (&args, 0, sizeof (args));
1637 args.version = JNI_VERSION_1_2;
1639 status = (*jvm)->AttachCurrentThread (jvm, (void **) &jvm_env, &args);
1642 ERROR ("java plugin: cjni_read: AttachCurrentThread failed with status %i.",
1647 cjni_shutdown_plugins (jvm_env);
1649 (*jvm)->DestroyJavaVM (jvm);
1653 pthread_key_delete (jvm_env_key);
1655 for (i = 0; i < jvm_argc; i++)
1657 sfree (jvm_argv[i]);
1662 for (i = 0; i < java_plugins_num; i++)
1664 sfree (java_plugins[i].class_name);
1665 oconfig_free (java_plugins[i].ci);
1667 sfree (java_plugins);
1668 java_plugins_num = 0;
1671 } /* }}} int cjni_shutdown */
1673 static int cjni_read_one_plugin (JNIEnv *jvm_env, java_plugin_t *jp) /* {{{ */
1678 || ((jp->flags & CJNI_FLAG_ENABLED) == 0)
1679 || (jp->m_read == NULL))
1682 DEBUG ("java plugin: Calling: %s.Read()", jp->class_name);
1684 status = (*jvm_env)->CallIntMethod (jvm_env, jp->object_ptr,
1688 ERROR ("java plugin: cjni_read_one_plugin: "
1689 "Calling `Read' on an `%s' object failed with status %i.",
1690 jp->class_name, status);
1695 } /* }}} int cjni_read_one_plugin */
1697 static int cjni_read (user_data_t *user_data) /* {{{ */
1705 ERROR ("java plugin: cjni_read: jvm == NULL");
1709 if ((user_data == NULL) || (user_data->data == NULL))
1711 ERROR ("java plugin: cjni_read: Invalid user data.");
1715 jvm_env = cjni_thread_attach ();
1716 if (jvm_env == NULL)
1719 jp = (java_plugin_t *) user_data->data;
1721 cjni_read_one_plugin (jvm_env, jp);
1723 status = cjni_thread_detach ();
1728 } /* }}} int cjni_read */
1730 static int cjni_init_one_plugin (JNIEnv *jvm_env, java_plugin_t *jp) /* {{{ */
1732 char plugin_name[128];
1733 jmethodID constructor_id;
1736 jp->class_ptr = (*jvm_env)->FindClass (jvm_env, jp->class_name);
1737 if (jp->class_ptr == NULL)
1739 ERROR ("cjni_init_one_plugin: FindClass (%s) failed.",
1744 constructor_id = (*jvm_env)->GetMethodID (jvm_env, jp->class_ptr,
1746 if (constructor_id == NULL)
1748 ERROR ("cjni_init_one_plugin: Could not find the constructor for `%s'.",
1753 jp->object_ptr = (*jvm_env)->NewObject (jvm_env, jp->class_ptr,
1755 if (jp->object_ptr == NULL)
1757 ERROR ("cjni_init_one_plugin: Could create a new `%s' object.",
1762 jp->m_config = (*jvm_env)->GetMethodID (jvm_env, jp->class_ptr,
1763 "Config", "(Lorg/collectd/api/OConfigItem;)I");
1764 DEBUG ("java plugin: cjni_init_one_plugin: "
1765 "jp->class_name = %s; jp->m_config = %p;",
1766 jp->class_name, (void *) jp->m_config);
1768 jp->m_init = (*jvm_env)->GetMethodID (jvm_env, jp->class_ptr,
1770 DEBUG ("java plugin: cjni_init_one_plugin: "
1771 "jp->class_name = %s; jp->m_init = %p;",
1772 jp->class_name, (void *) jp->m_init);
1774 jp->m_read = (*jvm_env)->GetMethodID (jvm_env, jp->class_ptr,
1776 DEBUG ("java plugin: cjni_init_one_plugin: "
1777 "jp->class_name = %s; jp->m_read = %p;",
1778 jp->class_name, (void *) jp->m_read);
1780 jp->m_write = (*jvm_env)->GetMethodID (jvm_env, jp->class_ptr,
1781 "Write", "(Lorg/collectd/api/ValueList;)I");
1782 DEBUG ("java plugin: cjni_init_one_plugin: "
1783 "jp->class_name = %s; jp->m_write = %p;",
1784 jp->class_name, (void *) jp->m_write);
1786 jp->m_shutdown = (*jvm_env)->GetMethodID (jvm_env, jp->class_ptr,
1788 DEBUG ("java plugin: cjni_init_one_plugin: "
1789 "jp->class_name = %s; jp->m_shutdown = %p;",
1790 jp->class_name, (void *) jp->m_shutdown);
1794 if (jp->m_config == NULL)
1796 WARNING ("java plugin: Configuration for the `%s' plugin is present, "
1797 "but plugin doesn't provide a configuration method.",
1800 else /* if (jp->m_config != NULL) */
1804 o_ocitem = ctoj_oconfig_item (jvm_env, jp->ci);
1805 if (o_ocitem == NULL)
1807 ERROR ("java plugin: Creating an OConfigItem object failed. "
1808 "Can't pass configuration information to the `%s' plugin!",
1811 else /* if (o_ocitem != NULL) */
1813 status = (*jvm_env)->CallIntMethod (jvm_env,
1814 jp->object_ptr, jp->m_config, o_ocitem);
1817 ERROR ("java plugin: cjni_init_one_plugin: "
1818 "Configuring the `%s' object failed with status %i.",
1819 jp->class_name, status);
1820 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
1823 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
1824 } /* if (o_ocitem != NULL) */
1825 } /* if (jp->m_config != NULL) */
1826 } /* if (jp->ci != NULL) */
1828 if (jp->m_init != NULL)
1830 status = (*jvm_env)->CallIntMethod (jvm_env, jp->object_ptr,
1834 ERROR ("java plugin: cjni_init_one_plugin: "
1835 "Initializing `%s' object failed with status %i.",
1836 jp->class_name, status);
1840 jp->flags |= CJNI_FLAG_ENABLED;
1842 ssnprintf (plugin_name, sizeof (plugin_name), "java:%s", jp->class_name);
1844 if (jp->m_write != NULL)
1848 memset (&ud, 0, sizeof (ud));
1850 ud.free_func = NULL;
1852 plugin_register_write (plugin_name, cjni_write, &ud);
1856 } /* }}} int cjni_init_one_plugin */
1858 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
1865 for (j = 0; j < java_plugins_num; j++)
1867 cjni_init_one_plugin (jvm_env, &java_plugins[j]);
1869 if (java_plugins[j].m_shutdown != NULL)
1873 if (have_shutdown > 0)
1874 plugin_register_shutdown ("java", cjni_shutdown);
1877 } /* }}} int cjni_init_plugins */
1879 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1881 jclass api_class_ptr;
1884 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.CollectdAPI");
1885 if (api_class_ptr == NULL)
1887 ERROR ("cjni_init_native: Cannot find API class `org.collectd.api.CollectdAPI'.");
1891 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1892 jni_api_functions, (jint) jni_api_functions_num);
1895 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1900 } /* }}} int cjni_init_native */
1902 static int cjni_init (void) /* {{{ */
1905 JavaVMInitArgs vm_args;
1906 JavaVMOption vm_options[jvm_argc];
1914 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1917 ERROR ("java plugin: cjni_init: pthread_key_create failed "
1918 "with status %i.", status);
1924 memset (&vm_args, 0, sizeof (vm_args));
1925 vm_args.version = JNI_VERSION_1_2;
1926 vm_args.options = vm_options;
1927 vm_args.nOptions = (jint) jvm_argc;
1929 for (i = 0; i < jvm_argc; i++)
1931 DEBUG ("java plugin: cjni_init: jvm_argv[%zu] = %s", i, jvm_argv[i]);
1932 vm_args.options[i].optionString = jvm_argv[i];
1935 vm_args.options[0].optionString = "-verbose:jni";
1936 vm_args.options[1].optionString = "-Djava.class.path=/home/octo/collectd/bindings/java";
1939 status = JNI_CreateJavaVM (&jvm, (void **) &jvm_env, (void **) &vm_args);
1942 ERROR ("cjni_init: JNI_CreateJavaVM failed with status %i.",
1946 assert (jvm != NULL);
1947 assert (jvm_env != NULL);
1949 /* Call RegisterNatives */
1950 status = cjni_init_native (jvm_env);
1953 ERROR ("cjni_init: cjni_init_native failed.");
1957 cjni_init_plugins (jvm_env);
1960 } /* }}} int cjni_init */
1962 void module_register (void)
1964 plugin_register_complex_config ("java", cjni_config);
1965 plugin_register_init ("java", cjni_init);
1966 } /* void module_register (void) */
1968 /* vim: set sw=2 sts=2 et fdm=marker : */