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 /* {{{ */
44 #define CJNI_FLAG_ENABLED 0x0001
52 typedef struct java_plugin_s java_plugin_t;
58 static JavaVM *jvm = NULL;
60 static java_plugin_t java_plugins[] =
62 { "org.collectd.java.Foobar", NULL, NULL, 0, NULL, NULL, NULL, NULL }
64 static size_t java_plugins_num = sizeof (java_plugins) / sizeof (java_plugins[0]);
67 * C to Java conversion functions
69 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
71 jclass class_ptr, jobject object_ptr, const char *method_name)
76 /* Create a java.lang.String */
77 o_string = (*jvm_env)->NewStringUTF (jvm_env,
78 (string != NULL) ? string : "");
81 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
85 /* Search for the `void setFoo (String s)' method. */
86 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
87 method_name, "(Ljava/lang/String;)V");
90 ERROR ("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
92 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
96 /* Call the method. */
97 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, o_string);
99 /* Decrease reference counter on the java.lang.String object. */
100 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
102 DEBUG ("java plugin: ctoj_string: ->%s (%s);",
103 method_name, (string != NULL) ? string : "");
106 } /* }}} int ctoj_string */
108 static int ctoj_int (JNIEnv *jvm_env, /* {{{ */
110 jclass class_ptr, jobject object_ptr, const char *method_name)
114 /* Search for the `void setFoo (int i)' method. */
115 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
116 method_name, "(I)V");
119 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
124 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
126 DEBUG ("java plugin: ctoj_int: ->%s (%i);",
127 method_name, (int) value);
130 } /* }}} int ctoj_int */
132 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
134 jclass class_ptr, jobject object_ptr, const char *method_name)
138 /* Search for the `void setFoo (long l)' method. */
139 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
140 method_name, "(J)V");
143 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
148 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
150 DEBUG ("java plugin: ctoj_long: ->%s (%"PRIi64");",
151 method_name, (int64_t) value);
154 } /* }}} int ctoj_long */
156 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
158 jclass class_ptr, jobject object_ptr, const char *method_name)
162 /* Search for the `void setFoo (double d)' method. */
163 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
164 method_name, "(D)V");
167 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
172 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
174 DEBUG ("java plugin: ctoj_double: ->%s (%g);",
175 method_name, (double) value);
178 } /* }}} int ctoj_double */
180 /* Convert a value_t to a java.lang.Number */
181 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
182 value_t value, int ds_type)
184 if (ds_type == DS_TYPE_COUNTER)
187 jmethodID m_long_constructor;
191 /* Look up the java.lang.Long class */
192 c_long = (*jvm_env)->FindClass (jvm_env, "java.lang.Long");
195 ERROR ("java plugin: ctoj_value_to_number: Looking up the "
196 "java.lang.Long class failed.");
200 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
201 c_long, "<init>", "(J)V");
202 if (m_long_constructor == NULL)
204 ERROR ("java plugin: ctoj_value_to_number: Looking up the "
205 "`Long (long)' constructor failed.");
209 tmp_long = (jlong) value.counter;
210 return ((*jvm_env)->NewObject (jvm_env,
211 c_long, m_long_constructor, tmp_long));
213 else if (ds_type == DS_TYPE_GAUGE)
216 jmethodID m_double_constructor;
220 /* Look up the java.lang.Long class */
221 c_double = (*jvm_env)->FindClass (jvm_env, "java.lang.Double");
222 if (c_double == NULL)
224 ERROR ("java plugin: ctoj_value_to_number: Looking up the "
225 "java.lang.Double class failed.");
229 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
230 c_double, "<init>", "(D)V");
231 if (m_double_constructor == NULL)
233 ERROR ("java plugin: ctoj_value_to_number: Looking up the "
234 "`Double (double)' constructor failed.");
238 tmp_double = (jdouble) value.gauge;
239 return ((*jvm_env)->NewObject (jvm_env,
240 c_double, m_double_constructor, tmp_double));
246 } /* }}} jobject ctoj_value_to_number */
248 /* Convert a data_source_t to a org.collectd.protocol.DataSource */
249 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
250 const data_source_t *dsrc)
253 jmethodID m_datasource_constructor;
254 jobject o_datasource;
257 /* Look up the DataSource class */
258 c_datasource = (*jvm_env)->FindClass (jvm_env,
259 "org.collectd.protocol.DataSource");
260 if (c_datasource == NULL)
262 ERROR ("java plugin: ctoj_data_source: "
263 "FindClass (org.collectd.protocol.DataSource) failed.");
267 /* Lookup the `ValueList ()' constructor. */
268 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
270 if (m_datasource_constructor == NULL)
272 ERROR ("java plugin: ctoj_data_source: Cannot find the "
273 "`DataSource ()' constructor.");
277 /* Create a new instance. */
278 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
279 m_datasource_constructor);
280 if (o_datasource == NULL)
282 ERROR ("java plugin: ctoj_data_source: "
283 "Creating a new DataSource instance failed.");
287 /* Set name via `void setName (String name)' */
288 status = ctoj_string (jvm_env, dsrc->name,
289 c_datasource, o_datasource, "setName");
292 ERROR ("java plugin: ctoj_data_source: "
293 "ctoj_string (setName) failed.");
294 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
298 /* Set type via `void setType (int type)' */
299 status = ctoj_int (jvm_env, dsrc->type,
300 c_datasource, o_datasource, "setType");
303 ERROR ("java plugin: ctoj_data_source: "
304 "ctoj_int (setType) failed.");
305 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
309 /* Set min via `void setMin (double min)' */
310 status = ctoj_double (jvm_env, dsrc->min,
311 c_datasource, o_datasource, "setMin");
314 ERROR ("java plugin: ctoj_data_source: "
315 "ctoj_double (setMin) failed.");
316 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
320 /* Set max via `void setMax (double max)' */
321 status = ctoj_double (jvm_env, dsrc->max,
322 c_datasource, o_datasource, "setMax");
325 ERROR ("java plugin: ctoj_data_source: "
326 "ctoj_double (setMax) failed.");
327 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
331 return (o_datasource);
332 } /* }}} jobject ctoj_data_source */
334 /* Convert a data_set_t to a java.util.List<DataSource> */
335 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
338 jmethodID m_constructor;
343 /* Look up the java.util.ArrayList class */
344 c_arraylist = (*jvm_env)->FindClass (jvm_env, "java.util.ArrayList");
345 if (c_arraylist == NULL)
347 ERROR ("java plugin: ctoj_data_set: Looking up the "
348 "java.util.ArrayList class failed.");
352 /* Search for the `ArrayList (int capacity)' constructor. */
353 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
354 c_arraylist, "<init>", "()V");
355 if (m_constructor == NULL)
357 ERROR ("java plugin: ctoj_data_set: Looking up the "
358 "`ArrayList (void)' constructor failed.");
362 /* Search for the `boolean add (Object element)' method. */
363 m_add = (*jvm_env)->GetMethodID (jvm_env,
364 c_arraylist, "add", "(Ljava/lang/Object;)Z");
367 ERROR ("java plugin: ctoj_data_set: Looking up the "
368 "`add (Object)' method failed.");
372 o_dataset = (*jvm_env)->NewObject (jvm_env, c_arraylist, m_constructor);
373 if (o_dataset == NULL)
375 ERROR ("java plugin: ctoj_data_set: "
376 "Creating an ArrayList object failed.");
380 for (i = 0; i < ds->ds_num; i++)
382 jobject o_datasource;
385 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
386 if (o_datasource == NULL)
388 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
389 ds->type, ds->ds[i].name);
390 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
394 status = (*jvm_env)->CallBooleanMethod (jvm_env,
395 o_dataset, m_add, o_datasource);
398 ERROR ("java plugin: ctoj_data_set: ArrayList.add returned FALSE.");
399 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
400 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
404 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
405 } /* for (i = 0; i < ds->ds_num; i++) */
408 } /* }}} jobject ctoj_data_set */
410 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
411 value_t value, int ds_type,
412 jclass class_ptr, jobject object_ptr)
414 jmethodID m_addvalue;
417 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
418 "addValue", "(Ljava/lang/Number;)V");
419 if (m_addvalue == NULL)
421 ERROR ("java plugin: ctoj_value_list_add_value: "
422 "Cannot find method `void addValue (Number)'.");
426 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
427 if (o_number == NULL)
429 ERROR ("java plugin: ctoj_value_list_add_value: "
430 "ctoj_value_to_number failed.");
434 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
436 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
439 } /* }}} int ctoj_value_list_add_value */
441 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
442 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
444 jmethodID m_setdatasource;
447 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
448 m_setdatasource = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
449 "setDataSource", "(Ljava/util/List;)V");
450 if (m_setdatasource == NULL)
452 ERROR ("java plugin: ctoj_value_list_add_data_set: "
453 "Cannot find the `void setDataSource (List<DataSource> ds)' method.");
457 /* Create a List<DataSource> object. */
458 o_dataset = ctoj_data_set (jvm_env, ds);
459 if (o_dataset == NULL)
461 ERROR ("java plugin: ctoj_value_list_add_data_set: "
462 "ctoj_data_set (%s) failed.", ds->type);
466 /* Actually call the method. */
467 (*jvm_env)->CallVoidMethod (jvm_env,
468 o_valuelist, m_setdatasource, o_dataset);
470 /* Decrease reference counter on the List<DataSource> object. */
471 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
474 } /* }}} int ctoj_value_list_add_data_set */
476 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
477 const data_set_t *ds, const value_list_t *vl)
480 jmethodID m_valuelist_constructor;
485 /* First, create a new ValueList instance..
486 * Look up the class.. */
487 c_valuelist = (*jvm_env)->FindClass (jvm_env,
488 "org.collectd.protocol.ValueList");
489 if (c_valuelist == NULL)
491 ERROR ("java plugin: ctoj_value_list: "
492 "FindClass (org.collectd.protocol.ValueList) failed.");
496 /* Lookup the `ValueList ()' constructor. */
497 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
499 if (m_valuelist_constructor == NULL)
501 ERROR ("java plugin: ctoj_value_list: Cannot find the "
502 "`ValueList ()' constructor.");
506 /* Create a new instance. */
507 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
508 m_valuelist_constructor);
509 if (o_valuelist == NULL)
511 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
516 status = ctoj_value_list_add_data_set (jvm_env,
517 c_valuelist, o_valuelist, ds);
520 ERROR ("java plugin: ctoj_value_list: "
521 "ctoj_value_list_add_data_set failed.");
522 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
526 /* Set the strings.. */
527 #define SET_STRING(str,method_name) do { \
528 status = ctoj_string (jvm_env, str, \
529 c_valuelist, o_valuelist, method_name); \
531 ERROR ("java plugin: ctoj_value_list: jtoc_string (%s) failed.", \
533 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
537 SET_STRING (vl->host, "setHost");
538 SET_STRING (vl->plugin, "setPlugin");
539 SET_STRING (vl->plugin_instance, "setPluginInstance");
540 SET_STRING (vl->type, "setType");
541 SET_STRING (vl->type_instance, "setTypeInstance");
545 /* Set the `time' member. Java stores time in milliseconds. */
546 status = ctoj_long (jvm_env, ((jlong) vl->time) * ((jlong) 1000),
547 c_valuelist, o_valuelist, "setTime");
550 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
551 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
555 /* Set the `interval' member.. */
556 status = ctoj_long (jvm_env, (jlong) vl->interval,
557 c_valuelist, o_valuelist, "setInterval");
560 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
561 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
565 for (i = 0; i < vl->values_len; i++)
567 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
568 c_valuelist, o_valuelist);
571 ERROR ("java plugin: ctoj_value_list: "
572 "ctoj_value_list_add_value failed.");
573 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
578 return (o_valuelist);
579 } /* }}} int ctoj_value_list */
582 * Java to C conversion functions
584 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
585 char *buffer, size_t buffer_size,
586 jclass class_ptr, jobject object_ptr, const char *method_name)
592 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
593 method_name, "()Ljava/lang/String;");
594 if (method_id == NULL)
596 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
601 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
602 if (string_obj == NULL)
604 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
609 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
612 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
613 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
617 DEBUG ("java plugin: jtoc_string: ->%s() = %s", method_name, c_str);
619 sstrncpy (buffer, c_str, buffer_size);
621 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
622 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
625 } /* }}} int jtoc_string */
627 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
629 jclass class_ptr, jobject object_ptr, const char *method_name)
633 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
635 if (method_id == NULL)
637 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
642 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
644 DEBUG ("java plugin: jtoc_long: ->%s() = %li",
645 method_name, (long int) *ret_value);
648 } /* }}} int jtoc_long */
650 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
652 jclass class_ptr, jobject object_ptr, const char *method_name)
656 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
658 if (method_id == NULL)
660 ERROR ("java plugin: jtoc_string: Cannot find method `double %s ()'.",
665 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
667 DEBUG ("java plugin: jtoc_double: ->%s() = %g",
668 method_name, (double) *ret_value);
671 } /* }}} int jtoc_double */
673 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
674 value_t *ret_value, int ds_type, jobject object_ptr)
679 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
681 if (ds_type == DS_TYPE_COUNTER)
685 status = jtoc_long (jvm_env, &tmp_long,
686 class_ptr, object_ptr, "longValue");
689 ERROR ("java plugin: jtoc_value: "
690 "jtoc_long failed.");
693 (*ret_value).counter = (counter_t) tmp_long;
699 status = jtoc_double (jvm_env, &tmp_double,
700 class_ptr, object_ptr, "doubleValue");
703 ERROR ("java plugin: jtoc_value: "
704 "jtoc_double failed.");
707 (*ret_value).gauge = (gauge_t) tmp_double;
711 } /* }}} int jtoc_value */
713 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
714 const data_set_t *ds, value_list_t *vl,
715 jclass class_ptr, jobject object_ptr)
717 jmethodID m_getvalues;
720 jobjectArray o_number_array;
726 values_num = ds->ds_num;
729 o_number_array = NULL;
732 #define BAIL_OUT(status) \
734 if (o_number_array != NULL) \
735 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
736 if (o_list != NULL) \
737 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
740 /* Call: List<Number> ValueList.getValues () */
741 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
742 "getValues", "()Ljava/util/List;");
743 if (m_getvalues == NULL)
745 ERROR ("java plugin: jtoc_values_array: "
746 "Cannot find method `List getValues ()'.");
750 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
753 ERROR ("java plugin: jtoc_values_array: "
754 "CallObjectMethod (getValues) failed.");
758 /* Call: Number[] List.toArray () */
759 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
760 (*jvm_env)->GetObjectClass (jvm_env, o_list),
761 "toArray", "()[Ljava/lang/Object;");
762 if (m_toarray == NULL)
764 ERROR ("java plugin: jtoc_values_array: "
765 "Cannot find method `Object[] toArray ()'.");
769 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
770 if (o_number_array == NULL)
772 ERROR ("java plugin: jtoc_values_array: "
773 "CallObjectMethod (toArray) failed.");
777 values = calloc (values_num, sizeof (value_t));
780 ERROR ("java plugin: jtoc_values_array: calloc failed.");
784 for (i = 0; i < values_num; i++)
789 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
790 o_number_array, (jsize) i);
791 if (o_number == NULL)
793 ERROR ("java plugin: jtoc_values_array: "
794 "GetObjectArrayElement (%i) failed.", i);
798 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
801 ERROR ("java plugin: jtoc_values_array: "
802 "jtoc_value (%i) failed.", i);
805 } /* for (i = 0; i < values_num; i++) */
808 vl->values_len = values_num;
811 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
812 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
814 } /* }}} int jtoc_values_array */
816 /* Convert a org.collectd.protocol.ValueList to a value_list_t. */
817 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
823 const data_set_t *ds;
825 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
826 if (class_ptr == NULL)
828 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
832 #define SET_STRING(buffer,method) do { \
833 status = jtoc_string (jvm_env, buffer, sizeof (buffer), \
834 class_ptr, object_ptr, method); \
836 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
841 SET_STRING(vl->type, "getType");
843 ds = plugin_get_ds (vl->type);
846 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
847 "Please consult the types.db(5) manpage for mor information.",
852 SET_STRING(vl->host, "getHost");
853 SET_STRING(vl->plugin, "getPlugin");
854 SET_STRING(vl->plugin_instance, "getPluginInstance");
855 SET_STRING(vl->type_instance, "getTypeInstance");
859 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
862 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
865 vl->time = (time_t) tmp_long;
867 status = jtoc_long (jvm_env, &tmp_long,
868 class_ptr, object_ptr, "getInterval");
871 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
874 vl->interval = (int) tmp_long;
876 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
879 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
884 } /* }}} int jtoc_value_list */
887 * Functions accessible from Java
889 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
890 jobject this, jobject java_vl)
892 value_list_t vl = VALUE_LIST_INIT;
895 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
897 status = jtoc_value_list (jvm_env, &vl, java_vl);
900 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
904 plugin_dispatch_values (&vl);
909 } /* }}} jint cjni_api_dispatch_values */
911 static JNINativeMethod jni_api_functions[] =
913 { "DispatchValues", "(Lorg/collectd/protocol/ValueList;)I", cjni_api_dispatch_values }
915 static size_t jni_api_functions_num = sizeof (jni_api_functions)
916 / sizeof (jni_api_functions[0]);
921 static int cjni_init_one_plugin (JNIEnv *jvm_env, java_plugin_t *jp) /* {{{ */
923 jmethodID constructor_id;
926 jp->class_ptr = (*jvm_env)->FindClass (jvm_env, jp->class_name);
927 if (jp->class_ptr == NULL)
929 ERROR ("cjni_init_one_plugin: FindClass (%s) failed.",
934 constructor_id = (*jvm_env)->GetMethodID (jvm_env, jp->class_ptr,
936 if (constructor_id == NULL)
938 ERROR ("cjni_init_one_plugin: Could not find the constructor for `%s'.",
943 jp->object_ptr = (*jvm_env)->NewObject (jvm_env, jp->class_ptr,
945 if (jp->object_ptr == NULL)
947 ERROR ("cjni_init_one_plugin: Could create a new `%s' object.",
952 jp->m_init = (*jvm_env)->GetMethodID (jvm_env, jp->class_ptr,
954 DEBUG ("jp->m_init = %p;", (void *) jp->m_init);
956 jp->m_read = (*jvm_env)->GetMethodID (jvm_env, jp->class_ptr,
958 DEBUG ("jp->m_read = %p;", (void *) jp->m_read);
960 jp->m_write = (*jvm_env)->GetMethodID (jvm_env, jp->class_ptr,
961 "Write", "(Lorg/collectd/protocol/ValueList;)I");
962 DEBUG ("jp->m_write = %p;", (void *) jp->m_write);
964 jp->m_shutdown = (*jvm_env)->GetMethodID (jvm_env, jp->class_ptr,
966 DEBUG ("jp->m_shutdown = %p;", (void *) jp->m_shutdown);
968 if (jp->m_init != NULL)
970 status = (*jvm_env)->CallIntMethod (jvm_env, jp->object_ptr,
974 ERROR ("cjni_init_one_plugin: Initializing `%s' object failed "
975 "with status %i.", jp->class_name, status);
979 jp->flags |= CJNI_FLAG_ENABLED;
982 } /* }}} int cjni_init_one_plugin */
984 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
988 for (j = 0; j < java_plugins_num; j++)
989 cjni_init_one_plugin (jvm_env, &java_plugins[j]);
992 } /* }}} int cjni_init_plugins */
994 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
996 jclass api_class_ptr;
999 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org.collectd.java.CollectdAPI");
1000 if (api_class_ptr == NULL)
1002 ERROR ("cjni_init_native: Cannot find API class `org.collectd.java.CollectdAPI'.");
1006 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1007 jni_api_functions, (jint) jni_api_functions_num);
1010 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1015 } /* }}} int cjni_init_native */
1017 static int cjni_init (void) /* {{{ */
1020 JavaVMInitArgs vm_args;
1021 JavaVMOption vm_options[2];
1030 memset (&vm_args, 0, sizeof (vm_args));
1031 vm_args.version = JNI_VERSION_1_2;
1032 vm_args.options = vm_options;
1033 vm_args.nOptions = sizeof (vm_options) / sizeof (vm_options[0]);
1035 vm_args.options[0].optionString = "-verbose:jni";
1036 vm_args.options[1].optionString = "-Djava.class.path=/home/octo/collectd/bindings/java";
1038 status = JNI_CreateJavaVM (&jvm, (void **) &jvm_env, (void **) &vm_args);
1041 ERROR ("cjni_init: JNI_CreateJavaVM failed with status %i.",
1045 assert (jvm != NULL);
1046 assert (jvm_env != NULL);
1048 /* Call RegisterNatives */
1049 status = cjni_init_native (jvm_env);
1052 ERROR ("cjni_init: cjni_init_native failed.");
1056 cjni_init_plugins (jvm_env);
1059 } /* }}} int cjni_init */
1061 static int cjni_read_one_plugin (JNIEnv *jvm_env, java_plugin_t *jp) /* {{{ */
1066 || ((jp->flags & CJNI_FLAG_ENABLED) == 0)
1067 || (jp->m_read == NULL))
1070 DEBUG ("java plugin: Calling: %s.Read()", jp->class_name);
1072 status = (*jvm_env)->CallIntMethod (jvm_env, jp->object_ptr,
1076 ERROR ("java plugin: cjni_read_one_plugin: "
1077 "Calling `Read' on an `%s' object failed with status %i.",
1078 jp->class_name, status);
1083 } /* }}} int cjni_read_one_plugin */
1085 static int cjni_read_plugins (JNIEnv *jvm_env) /* {{{ */
1089 for (j = 0; j < java_plugins_num; j++)
1090 cjni_read_one_plugin (jvm_env, &java_plugins[j]);
1093 } /* }}} int cjni_read_plugins */
1095 static int cjni_read (void) /* {{{ */
1098 JavaVMAttachArgs args;
1103 ERROR ("java plugin: cjni_read: jvm == NULL");
1108 memset (&args, 0, sizeof (args));
1109 args.version = JNI_VERSION_1_2;
1111 status = (*jvm)->AttachCurrentThread (jvm, (void **) &jvm_env, &args);
1114 ERROR ("java plugin: cjni_read: AttachCurrentThread failed with status %i.",
1119 cjni_read_plugins (jvm_env);
1121 status = (*jvm)->DetachCurrentThread (jvm);
1124 ERROR ("java plugin: cjni_read: DetachCurrentThread failed with status %i.",
1130 } /* }}} int cjni_read */
1132 static int cjni_write_one_plugin (JNIEnv *jvm_env, /* {{{ */
1133 java_plugin_t *jp, jobject vl_java)
1138 || ((jp->flags & CJNI_FLAG_ENABLED) == 0)
1139 || (jp->m_write == NULL))
1142 DEBUG ("java plugin: Calling: %s.Write(ValueList)", jp->class_name);
1144 status = (*jvm_env)->CallIntMethod (jvm_env, jp->object_ptr,
1145 jp->m_write, vl_java);
1148 ERROR ("java plugin: cjni_write_one_plugin: "
1149 "Calling `Write' on an `%s' object failed with status %i.",
1150 jp->class_name, status);
1155 } /* }}} int cjni_write_one_plugin */
1157 static int cjni_write_plugins (JNIEnv *jvm_env, /* {{{ */
1158 const data_set_t *ds, const value_list_t *vl)
1164 vl_java = ctoj_value_list (jvm_env, ds, vl);
1165 if (vl_java == NULL)
1167 ERROR ("java plugin: cjni_write_plugins: ctoj_value_list failed.");
1171 for (j = 0; j < java_plugins_num; j++)
1172 cjni_write_one_plugin (jvm_env, &java_plugins[j], vl_java);
1174 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
1177 } /* }}} int cjni_write_plugins */
1179 static int cjni_write (const data_set_t *ds, const value_list_t *vl) /* {{{ */
1182 JavaVMAttachArgs args;
1187 ERROR ("java plugin: cjni_write: jvm == NULL");
1192 memset (&args, 0, sizeof (args));
1193 args.version = JNI_VERSION_1_2;
1195 status = (*jvm)->AttachCurrentThread (jvm, (void **) &jvm_env, &args);
1198 ERROR ("java plugin: cjni_write: AttachCurrentThread failed with status %i.",
1203 cjni_write_plugins (jvm_env, ds, vl);
1205 status = (*jvm)->DetachCurrentThread (jvm);
1208 ERROR ("java plugin: cjni_write: DetachCurrentThread failed with status %i.",
1214 } /* }}} int cjni_write */
1216 static int cjni_shutdown_one_plugin (JNIEnv *jvm_env, /* {{{ */
1222 || ((jp->flags & CJNI_FLAG_ENABLED) == 0)
1223 || (jp->m_shutdown == NULL))
1226 status = (*jvm_env)->CallIntMethod (jvm_env, jp->object_ptr,
1230 ERROR ("cjni_shutdown_one_plugin: Destroying an `%s' object failed "
1231 "with status %i.", jp->class_name, status);
1234 jp->flags &= ~CJNI_FLAG_ENABLED;
1237 } /* }}} int cjni_shutdown_one_plugin */
1239 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
1243 for (j = 0; j < java_plugins_num; j++)
1244 cjni_shutdown_one_plugin (jvm_env, &java_plugins[j]);
1247 } /* }}} int cjni_shutdown_plugins */
1249 static int cjni_shutdown (void) /* {{{ */
1252 JavaVMAttachArgs args;
1259 memset (&args, 0, sizeof (args));
1260 args.version = JNI_VERSION_1_2;
1262 status = (*jvm)->AttachCurrentThread (jvm, (void **) &jvm_env, &args);
1265 ERROR ("java plugin: cjni_read: AttachCurrentThread failed with status %i.",
1270 cjni_shutdown_plugins (jvm_env);
1272 (*jvm)->DestroyJavaVM (jvm);
1277 } /* }}} int cjni_shutdown */
1279 void module_register (void)
1281 plugin_register_init ("java", cjni_init);
1282 plugin_register_read ("java", cjni_read);
1283 plugin_register_write ("java", cjni_write);
1284 plugin_register_shutdown ("java", cjni_shutdown);
1285 } /* void module_register (void) */
1287 /* vim: set sw=2 sts=2 et fdm=marker : */