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 cjni_jvm_env_s /* {{{ */
41 int reference_counter;
43 typedef struct cjni_jvm_env_s cjni_jvm_env_t;
46 struct java_plugin_class_s /* {{{ */
52 typedef struct java_plugin_class_s java_plugin_class_t;
55 struct java_plugin_config_s /* {{{ */
60 typedef struct java_plugin_config_s java_plugin_config_t;
63 #define CB_TYPE_CONFIG 1
64 #define CB_TYPE_INIT 2
65 #define CB_TYPE_READ 3
66 #define CB_TYPE_WRITE 4
67 #define CB_TYPE_FLUSH 5
68 #define CB_TYPE_SHUTDOWN 6
70 #define CB_TYPE_NOTIFICATION 8
71 struct cjni_callback_info_s /* {{{ */
79 typedef struct cjni_callback_info_s cjni_callback_info_t;
85 static JavaVM *jvm = NULL;
86 static pthread_key_t jvm_env_key;
88 /* Configuration options for the JVM. */
89 static char **jvm_argv = NULL;
90 static size_t jvm_argc = 0;
92 /* List of class names to load */
93 static java_plugin_class_t *java_classes_list = NULL;
94 static size_t java_classes_list_len;
96 /* List of `config_item_t's for Java plugins */
97 static java_plugin_config_t *java_plugin_configs = NULL;
98 static size_t java_plugin_configs_num = 0;
100 /* List of config, init, and shutdown callbacks. */
101 static cjni_callback_info_t *java_callbacks = NULL;
102 static size_t java_callbacks_num = 0;
103 static pthread_mutex_t java_callbacks_lock = PTHREAD_MUTEX_INITIALIZER;
108 * Mostly functions that are needed by the Java interface (``native'')
111 static void cjni_callback_info_destroy (void *arg);
112 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env,
113 jobject o_name, jobject o_callback, int type);
114 static int cjni_callback_register (JNIEnv *jvm_env, jobject o_name,
115 jobject o_callback, int type);
116 static int cjni_read (user_data_t *user_data);
117 static int cjni_write (const data_set_t *ds, const value_list_t *vl,
119 static int cjni_flush (int timeout, const char *identifier, user_data_t *ud);
120 static void cjni_log (int severity, const char *message, user_data_t *ud);
121 static int cjni_notification (const notification_t *n, user_data_t *ud);
124 * C to Java conversion functions
126 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
128 jclass class_ptr, jobject object_ptr, const char *method_name)
133 /* Create a java.lang.String */
134 o_string = (*jvm_env)->NewStringUTF (jvm_env,
135 (string != NULL) ? string : "");
136 if (o_string == NULL)
138 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
142 /* Search for the `void setFoo (String s)' method. */
143 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
144 method_name, "(Ljava/lang/String;)V");
147 ERROR ("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
149 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
153 /* Call the method. */
154 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, o_string);
156 /* Decrease reference counter on the java.lang.String object. */
157 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
160 } /* }}} int ctoj_string */
162 static int ctoj_int (JNIEnv *jvm_env, /* {{{ */
164 jclass class_ptr, jobject object_ptr, const char *method_name)
168 /* Search for the `void setFoo (int i)' method. */
169 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
170 method_name, "(I)V");
173 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
178 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
181 } /* }}} int ctoj_int */
183 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
185 jclass class_ptr, jobject object_ptr, const char *method_name)
189 /* Search for the `void setFoo (long l)' method. */
190 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
191 method_name, "(J)V");
194 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
199 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
202 } /* }}} int ctoj_long */
204 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
206 jclass class_ptr, jobject object_ptr, const char *method_name)
210 /* Search for the `void setFoo (double d)' method. */
211 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
212 method_name, "(D)V");
215 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
220 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
223 } /* }}} int ctoj_double */
225 /* Convert a jlong to a java.lang.Number */
226 static jobject ctoj_jlong_to_number (JNIEnv *jvm_env, jlong value) /* {{{ */
229 jmethodID m_long_constructor;
231 /* Look up the java.lang.Long class */
232 c_long = (*jvm_env)->FindClass (jvm_env, "java.lang.Long");
235 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
236 "java.lang.Long class failed.");
240 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
241 c_long, "<init>", "(J)V");
242 if (m_long_constructor == NULL)
244 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
245 "`Long (long)' constructor failed.");
249 return ((*jvm_env)->NewObject (jvm_env,
250 c_long, m_long_constructor, value));
251 } /* }}} jobject ctoj_jlong_to_number */
253 /* Convert a jdouble to a java.lang.Number */
254 static jobject ctoj_jdouble_to_number (JNIEnv *jvm_env, jdouble value) /* {{{ */
257 jmethodID m_double_constructor;
259 /* Look up the java.lang.Long class */
260 c_double = (*jvm_env)->FindClass (jvm_env, "java.lang.Double");
261 if (c_double == NULL)
263 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
264 "java.lang.Double class failed.");
268 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
269 c_double, "<init>", "(D)V");
270 if (m_double_constructor == NULL)
272 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
273 "`Double (double)' constructor failed.");
277 return ((*jvm_env)->NewObject (jvm_env,
278 c_double, m_double_constructor, value));
279 } /* }}} jobject ctoj_jdouble_to_number */
281 /* Convert a value_t to a java.lang.Number */
282 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
283 value_t value, int ds_type)
285 if (ds_type == DS_TYPE_COUNTER)
286 return (ctoj_jlong_to_number (jvm_env, (jlong) value.counter));
287 else if (ds_type == DS_TYPE_GAUGE)
288 return (ctoj_jdouble_to_number (jvm_env, (jdouble) value.gauge));
291 } /* }}} jobject ctoj_value_to_number */
293 /* Convert a data_source_t to a org.collectd.api.DataSource */
294 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
295 const data_source_t *dsrc)
298 jmethodID m_datasource_constructor;
299 jobject o_datasource;
302 /* Look up the DataSource class */
303 c_datasource = (*jvm_env)->FindClass (jvm_env,
304 "org.collectd.api.DataSource");
305 if (c_datasource == NULL)
307 ERROR ("java plugin: ctoj_data_source: "
308 "FindClass (org.collectd.api.DataSource) failed.");
312 /* Lookup the `ValueList ()' constructor. */
313 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
315 if (m_datasource_constructor == NULL)
317 ERROR ("java plugin: ctoj_data_source: Cannot find the "
318 "`DataSource ()' constructor.");
322 /* Create a new instance. */
323 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
324 m_datasource_constructor);
325 if (o_datasource == NULL)
327 ERROR ("java plugin: ctoj_data_source: "
328 "Creating a new DataSource instance failed.");
332 /* Set name via `void setName (String name)' */
333 status = ctoj_string (jvm_env, dsrc->name,
334 c_datasource, o_datasource, "setName");
337 ERROR ("java plugin: ctoj_data_source: "
338 "ctoj_string (setName) failed.");
339 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
343 /* Set type via `void setType (int type)' */
344 status = ctoj_int (jvm_env, dsrc->type,
345 c_datasource, o_datasource, "setType");
348 ERROR ("java plugin: ctoj_data_source: "
349 "ctoj_int (setType) failed.");
350 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
354 /* Set min via `void setMin (double min)' */
355 status = ctoj_double (jvm_env, dsrc->min,
356 c_datasource, o_datasource, "setMin");
359 ERROR ("java plugin: ctoj_data_source: "
360 "ctoj_double (setMin) failed.");
361 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
365 /* Set max via `void setMax (double max)' */
366 status = ctoj_double (jvm_env, dsrc->max,
367 c_datasource, o_datasource, "setMax");
370 ERROR ("java plugin: ctoj_data_source: "
371 "ctoj_double (setMax) failed.");
372 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
376 return (o_datasource);
377 } /* }}} jobject ctoj_data_source */
379 /* Convert a oconfig_value_t to a org.collectd.api.OConfigValue */
380 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
381 oconfig_value_t ocvalue)
384 jmethodID m_ocvalue_constructor;
388 m_ocvalue_constructor = NULL;
391 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
392 "org.collectd.api.OConfigValue");
393 if (c_ocvalue == NULL)
395 ERROR ("java plugin: ctoj_oconfig_value: "
396 "FindClass (org.collectd.api.OConfigValue) failed.");
400 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
402 jboolean tmp_boolean;
404 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
406 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
408 if (m_ocvalue_constructor == NULL)
410 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
411 "`OConfigValue (boolean)' constructor.");
415 return ((*jvm_env)->NewObject (jvm_env,
416 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
417 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
418 else if (ocvalue.type == OCONFIG_TYPE_STRING)
420 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
421 "<init>", "(Ljava/lang/String;)V");
422 if (m_ocvalue_constructor == NULL)
424 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
425 "`OConfigValue (String)' constructor.");
429 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
430 if (o_argument == NULL)
432 ERROR ("java plugin: ctoj_oconfig_value: "
433 "Creating a String object failed.");
437 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
439 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
440 "<init>", "(Ljava/lang/Number;)V");
441 if (m_ocvalue_constructor == NULL)
443 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
444 "`OConfigValue (Number)' constructor.");
448 o_argument = ctoj_jdouble_to_number (jvm_env,
449 (jdouble) ocvalue.value.number);
450 if (o_argument == NULL)
452 ERROR ("java plugin: ctoj_oconfig_value: "
453 "Creating a Number object failed.");
462 assert (m_ocvalue_constructor != NULL);
463 assert (o_argument != NULL);
465 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
466 c_ocvalue, m_ocvalue_constructor, o_argument);
467 if (o_ocvalue == NULL)
469 ERROR ("java plugin: ctoj_oconfig_value: "
470 "Creating an OConfigValue object failed.");
471 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
475 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
477 } /* }}} jobject ctoj_oconfig_value */
479 /* Convert a oconfig_item_t to a org.collectd.api.OConfigItem */
480 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
481 const oconfig_item_t *ci)
484 jmethodID m_ocitem_constructor;
485 jmethodID m_addvalue;
486 jmethodID m_addchild;
491 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.OConfigItem");
492 if (c_ocitem == NULL)
494 ERROR ("java plugin: ctoj_oconfig_item: "
495 "FindClass (org.collectd.api.OConfigItem) failed.");
499 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
501 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
502 "<init>", "(Ljava/lang/String;)V");
503 if (m_ocitem_constructor == NULL)
505 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
506 "`OConfigItem (String)' constructor.");
510 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
511 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
512 if (m_addvalue == NULL)
514 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
515 "`addValue (OConfigValue)' method.");
519 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
520 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
521 if (m_addchild == NULL)
523 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
524 "`addChild (OConfigItem)' method.");
529 /* Create a String object with the key.
530 * Needed for calling the constructor. */
531 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
534 ERROR ("java plugin: ctoj_oconfig_item: "
535 "Creating String object failed.");
539 /* Create an OConfigItem object */
540 o_ocitem = (*jvm_env)->NewObject (jvm_env,
541 c_ocitem, m_ocitem_constructor, o_key);
542 if (o_ocitem == NULL)
544 ERROR ("java plugin: ctoj_oconfig_item: "
545 "Creating an OConfigItem object failed.");
546 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
550 /* We don't need the String object any longer.. */
551 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
553 /* Call OConfigItem.addValue for each value */
554 for (i = 0; i < ci->values_num; i++) /* {{{ */
558 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
561 ERROR ("java plugin: ctoj_oconfig_item: "
562 "Creating an OConfigValue object failed.");
563 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
567 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
568 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
569 } /* }}} for (i = 0; i < ci->values_num; i++) */
571 /* Call OConfigItem.addChild for each child */
572 for (i = 0; i < ci->children_num; i++) /* {{{ */
576 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
579 ERROR ("java plugin: ctoj_oconfig_item: "
580 "Creating an OConfigItem object failed.");
581 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
585 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_child);
586 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
587 } /* }}} for (i = 0; i < ci->children_num; i++) */
590 } /* }}} jobject ctoj_oconfig_item */
592 /* Convert a data_set_t to a org.collectd.api.DataSet */
593 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
596 jmethodID m_constructor;
602 /* Look up the org.collectd.api.DataSet class */
603 c_dataset = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.DataSet");
604 if (c_dataset == NULL)
606 ERROR ("java plugin: ctoj_data_set: Looking up the "
607 "org.collectd.api.DataSet class failed.");
611 /* Search for the `DataSet (String type)' constructor. */
612 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
613 c_dataset, "<init>", "(Ljava.lang.String;)V");
614 if (m_constructor == NULL)
616 ERROR ("java plugin: ctoj_data_set: Looking up the "
617 "`DataSet (String)' constructor failed.");
621 /* Search for the `void addDataSource (DataSource)' method. */
622 m_add = (*jvm_env)->GetMethodID (jvm_env,
623 c_dataset, "addDataSource", "(Lorg.collectd.api.DataSource;)V");
626 ERROR ("java plugin: ctoj_data_set: Looking up the "
627 "`addDataSource (DataSource)' method failed.");
631 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
634 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
638 o_dataset = (*jvm_env)->NewObject (jvm_env,
639 c_dataset, m_constructor, o_type);
640 if (o_dataset == NULL)
642 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
643 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
647 /* Decrease reference counter on the java.lang.String object. */
648 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
650 for (i = 0; i < ds->ds_num; i++)
652 jobject o_datasource;
654 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
655 if (o_datasource == NULL)
657 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
658 ds->type, ds->ds[i].name);
659 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
663 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
665 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
666 } /* for (i = 0; i < ds->ds_num; i++) */
669 } /* }}} jobject ctoj_data_set */
671 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
672 value_t value, int ds_type,
673 jclass class_ptr, jobject object_ptr)
675 jmethodID m_addvalue;
678 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
679 "addValue", "(Ljava/lang/Number;)V");
680 if (m_addvalue == NULL)
682 ERROR ("java plugin: ctoj_value_list_add_value: "
683 "Cannot find method `void addValue (Number)'.");
687 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
688 if (o_number == NULL)
690 ERROR ("java plugin: ctoj_value_list_add_value: "
691 "ctoj_value_to_number failed.");
695 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
697 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
700 } /* }}} int ctoj_value_list_add_value */
702 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
703 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
705 jmethodID m_setdataset;
708 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
709 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
710 "setDataSet", "(Lorg.collectd.api.DataSet;)V");
711 if (m_setdataset == NULL)
713 ERROR ("java plugin: ctoj_value_list_add_data_set: "
714 "Cannot find the `void setDataSet (DataSet)' method.");
718 /* Create a DataSet object. */
719 o_dataset = ctoj_data_set (jvm_env, ds);
720 if (o_dataset == NULL)
722 ERROR ("java plugin: ctoj_value_list_add_data_set: "
723 "ctoj_data_set (%s) failed.", ds->type);
727 /* Actually call the method. */
728 (*jvm_env)->CallVoidMethod (jvm_env,
729 o_valuelist, m_setdataset, o_dataset);
731 /* Decrease reference counter on the List<DataSource> object. */
732 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
735 } /* }}} int ctoj_value_list_add_data_set */
737 /* Convert a value_list_t (and data_set_t) to a org.collectd.api.ValueList */
738 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
739 const data_set_t *ds, const value_list_t *vl)
742 jmethodID m_valuelist_constructor;
747 /* First, create a new ValueList instance..
748 * Look up the class.. */
749 c_valuelist = (*jvm_env)->FindClass (jvm_env,
750 "org.collectd.api.ValueList");
751 if (c_valuelist == NULL)
753 ERROR ("java plugin: ctoj_value_list: "
754 "FindClass (org.collectd.api.ValueList) failed.");
758 /* Lookup the `ValueList ()' constructor. */
759 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
761 if (m_valuelist_constructor == NULL)
763 ERROR ("java plugin: ctoj_value_list: Cannot find the "
764 "`ValueList ()' constructor.");
768 /* Create a new instance. */
769 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
770 m_valuelist_constructor);
771 if (o_valuelist == NULL)
773 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
778 status = ctoj_value_list_add_data_set (jvm_env,
779 c_valuelist, o_valuelist, ds);
782 ERROR ("java plugin: ctoj_value_list: "
783 "ctoj_value_list_add_data_set failed.");
784 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
788 /* Set the strings.. */
789 #define SET_STRING(str,method_name) do { \
790 status = ctoj_string (jvm_env, str, \
791 c_valuelist, o_valuelist, method_name); \
793 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
795 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
799 SET_STRING (vl->host, "setHost");
800 SET_STRING (vl->plugin, "setPlugin");
801 SET_STRING (vl->plugin_instance, "setPluginInstance");
802 SET_STRING (vl->type, "setType");
803 SET_STRING (vl->type_instance, "setTypeInstance");
807 /* Set the `time' member. Java stores time in milliseconds. */
808 status = ctoj_long (jvm_env, ((jlong) vl->time) * ((jlong) 1000),
809 c_valuelist, o_valuelist, "setTime");
812 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
813 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
817 /* Set the `interval' member.. */
818 status = ctoj_long (jvm_env, (jlong) vl->interval,
819 c_valuelist, o_valuelist, "setInterval");
822 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
823 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
827 for (i = 0; i < vl->values_len; i++)
829 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
830 c_valuelist, o_valuelist);
833 ERROR ("java plugin: ctoj_value_list: "
834 "ctoj_value_list_add_value failed.");
835 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
840 return (o_valuelist);
841 } /* }}} jobject ctoj_value_list */
843 /* Convert a notification_t to a org.collectd.api.Notification */
844 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
845 const notification_t *n)
847 jclass c_notification;
848 jmethodID m_constructor;
849 jobject o_notification;
852 /* First, create a new Notification instance..
853 * Look up the class.. */
854 c_notification = (*jvm_env)->FindClass (jvm_env,
855 "org.collectd.api.Notification");
856 if (c_notification == NULL)
858 ERROR ("java plugin: ctoj_notification: "
859 "FindClass (org.collectd.api.Notification) failed.");
863 /* Lookup the `Notification ()' constructor. */
864 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
866 if (m_constructor == NULL)
868 ERROR ("java plugin: ctoj_notification: Cannot find the "
869 "`Notification ()' constructor.");
873 /* Create a new instance. */
874 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
876 if (o_notification == NULL)
878 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
883 /* Set the strings.. */
884 #define SET_STRING(str,method_name) do { \
885 status = ctoj_string (jvm_env, str, \
886 c_notification, o_notification, method_name); \
888 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
890 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
894 SET_STRING (n->host, "setHost");
895 SET_STRING (n->plugin, "setPlugin");
896 SET_STRING (n->plugin_instance, "setPluginInstance");
897 SET_STRING (n->type, "setType");
898 SET_STRING (n->type_instance, "setTypeInstance");
899 SET_STRING (n->message, "setMessage");
903 /* Set the `time' member. Java stores time in milliseconds. */
904 status = ctoj_long (jvm_env, ((jlong) n->time) * ((jlong) 1000),
905 c_notification, o_notification, "setTime");
908 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
909 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
913 /* Set the `interval' member.. */
914 status = ctoj_int (jvm_env, (jint) n->severity,
915 c_notification, o_notification, "setSeverity");
918 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
919 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
923 return (o_notification);
924 } /* }}} jobject ctoj_notification */
927 * Java to C conversion functions
929 /* Call a `String <method> ()' method. */
930 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
931 char *buffer, size_t buffer_size, int empty_okay,
932 jclass class_ptr, jobject object_ptr, const char *method_name)
938 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
939 method_name, "()Ljava/lang/String;");
940 if (method_id == NULL)
942 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
947 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
948 if ((string_obj == NULL) && (empty_okay == 0))
950 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
954 else if ((string_obj == NULL) && (empty_okay != 0))
956 memset (buffer, 0, buffer_size);
960 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
963 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
964 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
968 sstrncpy (buffer, c_str, buffer_size);
970 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
971 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
974 } /* }}} int jtoc_string */
976 /* Call an `int <method> ()' method. */
977 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
979 jclass class_ptr, jobject object_ptr, const char *method_name)
983 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
985 if (method_id == NULL)
987 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
992 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
995 } /* }}} int jtoc_int */
997 /* Call a `long <method> ()' method. */
998 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
1000 jclass class_ptr, jobject object_ptr, const char *method_name)
1002 jmethodID method_id;
1004 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1005 method_name, "()J");
1006 if (method_id == NULL)
1008 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1013 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1016 } /* }}} int jtoc_long */
1018 /* Call a `double <method> ()' method. */
1019 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1021 jclass class_ptr, jobject object_ptr, const char *method_name)
1023 jmethodID method_id;
1025 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1026 method_name, "()D");
1027 if (method_id == NULL)
1029 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1034 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1037 } /* }}} int jtoc_double */
1039 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1040 value_t *ret_value, int ds_type, jobject object_ptr)
1045 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1047 if (ds_type == DS_TYPE_COUNTER)
1051 status = jtoc_long (jvm_env, &tmp_long,
1052 class_ptr, object_ptr, "longValue");
1055 ERROR ("java plugin: jtoc_value: "
1056 "jtoc_long failed.");
1059 (*ret_value).counter = (counter_t) tmp_long;
1065 status = jtoc_double (jvm_env, &tmp_double,
1066 class_ptr, object_ptr, "doubleValue");
1069 ERROR ("java plugin: jtoc_value: "
1070 "jtoc_double failed.");
1073 (*ret_value).gauge = (gauge_t) tmp_double;
1077 } /* }}} int jtoc_value */
1079 /* Read a List<Number>, convert it to `value_t' and add it to the given
1080 * `value_list_t'. */
1081 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1082 const data_set_t *ds, value_list_t *vl,
1083 jclass class_ptr, jobject object_ptr)
1085 jmethodID m_getvalues;
1086 jmethodID m_toarray;
1088 jobjectArray o_number_array;
1094 values_num = ds->ds_num;
1097 o_number_array = NULL;
1100 #define BAIL_OUT(status) \
1102 if (o_number_array != NULL) \
1103 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1104 if (o_list != NULL) \
1105 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1108 /* Call: List<Number> ValueList.getValues () */
1109 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1110 "getValues", "()Ljava/util/List;");
1111 if (m_getvalues == NULL)
1113 ERROR ("java plugin: jtoc_values_array: "
1114 "Cannot find method `List getValues ()'.");
1118 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1121 ERROR ("java plugin: jtoc_values_array: "
1122 "CallObjectMethod (getValues) failed.");
1126 /* Call: Number[] List.toArray () */
1127 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1128 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1129 "toArray", "()[Ljava/lang/Object;");
1130 if (m_toarray == NULL)
1132 ERROR ("java plugin: jtoc_values_array: "
1133 "Cannot find method `Object[] toArray ()'.");
1137 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1138 if (o_number_array == NULL)
1140 ERROR ("java plugin: jtoc_values_array: "
1141 "CallObjectMethod (toArray) failed.");
1145 values = (value_t *) calloc (values_num, sizeof (value_t));
1148 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1152 for (i = 0; i < values_num; i++)
1157 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1158 o_number_array, (jsize) i);
1159 if (o_number == NULL)
1161 ERROR ("java plugin: jtoc_values_array: "
1162 "GetObjectArrayElement (%i) failed.", i);
1166 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1169 ERROR ("java plugin: jtoc_values_array: "
1170 "jtoc_value (%i) failed.", i);
1173 } /* for (i = 0; i < values_num; i++) */
1175 vl->values = values;
1176 vl->values_len = values_num;
1179 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1180 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1182 } /* }}} int jtoc_values_array */
1184 /* Convert a org.collectd.api.ValueList to a value_list_t. */
1185 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1191 const data_set_t *ds;
1193 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1194 if (class_ptr == NULL)
1196 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1200 /* eo == empty okay */
1201 #define SET_STRING(buffer,method, eo) do { \
1202 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1203 class_ptr, object_ptr, method); \
1204 if (status != 0) { \
1205 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1210 SET_STRING(vl->type, "getType", /* empty = */ 0);
1212 ds = plugin_get_ds (vl->type);
1215 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1216 "Please consult the types.db(5) manpage for mor information.",
1221 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1222 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1223 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1224 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1228 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1231 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1234 /* Java measures time in milliseconds. */
1235 vl->time = (time_t) (tmp_long / ((jlong) 1000));
1237 status = jtoc_long (jvm_env, &tmp_long,
1238 class_ptr, object_ptr, "getInterval");
1241 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1244 vl->interval = (int) tmp_long;
1246 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1249 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1254 } /* }}} int jtoc_value_list */
1256 /* Convert a org.collectd.api.Notification to a notification_t. */
1257 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1265 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1266 if (class_ptr == NULL)
1268 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1272 /* eo == empty okay */
1273 #define SET_STRING(buffer,method, eo) do { \
1274 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1275 class_ptr, object_ptr, method); \
1276 if (status != 0) { \
1277 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1282 SET_STRING (n->host, "getHost", /* empty = */ 1);
1283 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1284 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1285 SET_STRING (n->type, "getType", /* empty = */ 1);
1286 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1287 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1291 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1294 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1297 /* Java measures time in milliseconds. */
1298 n->time = (time_t) (tmp_long / ((jlong) 1000));
1300 status = jtoc_int (jvm_env, &tmp_int,
1301 class_ptr, object_ptr, "getSeverity");
1304 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1307 n->severity = (int) tmp_int;
1310 } /* }}} int jtoc_notification */
1312 * Functions accessible from Java
1314 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1315 jobject this, jobject java_vl)
1317 value_list_t vl = VALUE_LIST_INIT;
1320 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1322 status = jtoc_value_list (jvm_env, &vl, java_vl);
1325 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1329 status = plugin_dispatch_values (&vl);
1334 } /* }}} jint cjni_api_dispatch_values */
1336 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1337 jobject this, jobject o_notification)
1342 memset (&n, 0, sizeof (n));
1345 status = jtoc_notification (jvm_env, &n, o_notification);
1348 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1352 status = plugin_dispatch_notification (&n);
1355 } /* }}} jint cjni_api_dispatch_notification */
1357 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1358 jobject this, jobject o_string_type)
1360 const char *ds_name;
1361 const data_set_t *ds;
1364 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1365 if (ds_name == NULL)
1367 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1371 ds = plugin_get_ds (ds_name);
1372 DEBUG ("java plugin: cjni_api_get_ds: "
1373 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1375 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1380 o_dataset = ctoj_data_set (jvm_env, ds);
1382 } /* }}} jint cjni_api_get_ds */
1384 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1385 jobject this, jobject o_name, jobject o_config)
1387 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1388 } /* }}} jint cjni_api_register_config */
1390 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1391 jobject this, jobject o_name, jobject o_config)
1393 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1394 } /* }}} jint cjni_api_register_init */
1396 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1397 jobject this, jobject o_name, jobject o_read)
1400 cjni_callback_info_t *cbi;
1402 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1406 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1408 memset (&ud, 0, sizeof (ud));
1409 ud.data = (void *) cbi;
1410 ud.free_func = cjni_callback_info_destroy;
1412 plugin_register_complex_read (cbi->name, cjni_read, &ud);
1414 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1417 } /* }}} jint cjni_api_register_read */
1419 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1420 jobject this, jobject o_name, jobject o_write)
1423 cjni_callback_info_t *cbi;
1425 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1429 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1431 memset (&ud, 0, sizeof (ud));
1432 ud.data = (void *) cbi;
1433 ud.free_func = cjni_callback_info_destroy;
1435 plugin_register_write (cbi->name, cjni_write, &ud);
1437 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1440 } /* }}} jint cjni_api_register_write */
1442 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1443 jobject this, jobject o_name, jobject o_flush)
1446 cjni_callback_info_t *cbi;
1448 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1452 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1454 memset (&ud, 0, sizeof (ud));
1455 ud.data = (void *) cbi;
1456 ud.free_func = cjni_callback_info_destroy;
1458 plugin_register_flush (cbi->name, cjni_flush, &ud);
1460 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1463 } /* }}} jint cjni_api_register_flush */
1465 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1466 jobject this, jobject o_name, jobject o_shutdown)
1468 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1470 } /* }}} jint cjni_api_register_shutdown */
1472 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1473 jobject this, jobject o_name, jobject o_log)
1476 cjni_callback_info_t *cbi;
1478 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1482 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1484 memset (&ud, 0, sizeof (ud));
1485 ud.data = (void *) cbi;
1486 ud.free_func = cjni_callback_info_destroy;
1488 plugin_register_log (cbi->name, cjni_log, &ud);
1490 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1493 } /* }}} jint cjni_api_register_log */
1495 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1496 jobject this, jobject o_name, jobject o_notification)
1499 cjni_callback_info_t *cbi;
1501 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1502 CB_TYPE_NOTIFICATION);
1506 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1508 memset (&ud, 0, sizeof (ud));
1509 ud.data = (void *) cbi;
1510 ud.free_func = cjni_callback_info_destroy;
1512 plugin_register_notification (cbi->name, cjni_notification, &ud);
1514 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1517 } /* }}} jint cjni_api_register_notification */
1519 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1520 jobject this, jint severity, jobject o_message)
1524 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1527 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1531 if (severity < LOG_ERR)
1533 if (severity > LOG_DEBUG)
1534 severity = LOG_DEBUG;
1536 plugin_log (severity, "%s", c_str);
1538 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1539 } /* }}} void cjni_api_log */
1541 /* List of ``native'' functions, i. e. C-functions that can be called from
1543 static JNINativeMethod jni_api_functions[] = /* {{{ */
1546 "(Lorg/collectd/api/ValueList;)I",
1547 cjni_api_dispatch_values },
1549 { "dispatchNotification",
1550 "(Lorg/collectd/api/Notification;)I",
1551 cjni_api_dispatch_notification },
1554 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1558 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1559 cjni_api_register_config },
1562 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1563 cjni_api_register_init },
1566 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1567 cjni_api_register_read },
1570 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1571 cjni_api_register_write },
1574 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1575 cjni_api_register_flush },
1577 { "registerShutdown",
1578 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1579 cjni_api_register_shutdown },
1582 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1583 cjni_api_register_log },
1585 { "registerNotification",
1586 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1587 cjni_api_register_notification },
1590 "(ILjava/lang/String;)V",
1593 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1594 / sizeof (jni_api_functions[0]);
1600 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1601 * all registration functions. */
1602 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1603 jobject o_name, jobject o_callback, int type)
1606 cjni_callback_info_t *cbi;
1607 const char *method_name;
1608 const char *method_signature;
1612 case CB_TYPE_CONFIG:
1613 method_name = "config";
1614 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1618 method_name = "init";
1619 method_signature = "()I";
1623 method_name = "read";
1624 method_signature = "()I";
1628 method_name = "write";
1629 method_signature = "(Lorg/collectd/api/ValueList;)I";
1633 method_name = "flush";
1634 method_signature = "(ILjava/lang/String;)I";
1637 case CB_TYPE_SHUTDOWN:
1638 method_name = "shutdown";
1639 method_signature = "()I";
1643 method_name = "log";
1644 method_signature = "(ILjava/lang/String;)V";
1647 case CB_TYPE_NOTIFICATION:
1648 method_name = "notification";
1649 method_signature = "(LLorg/collectd/api/Notification;)I";
1653 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1658 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1661 ERROR ("java plugin: cjni_callback_info_create: "
1662 "GetStringUTFChars failed.");
1666 cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1669 ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1670 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1673 memset (cbi, 0, sizeof (*cbi));
1676 cbi->name = strdup (c_name);
1677 if (cbi->name == NULL)
1679 pthread_mutex_unlock (&java_callbacks_lock);
1680 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1681 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1685 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1687 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, o_callback);
1688 if (cbi->class == NULL)
1690 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1695 cbi->object = o_callback;
1697 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1698 method_name, method_signature);
1699 if (cbi->method == NULL)
1701 ERROR ("java plugin: cjni_callback_info_create: "
1702 "Cannot find the `%s' method with signature `%s'.",
1703 method_name, method_signature);
1708 (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1711 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1713 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1714 * to the global `java_callbacks' variable. This is used for `config', `init',
1715 * and `shutdown' callbacks. */
1716 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1717 jobject o_name, jobject o_callback, int type)
1719 cjni_callback_info_t *cbi;
1720 cjni_callback_info_t *tmp;
1722 const char *type_str;
1725 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1732 case CB_TYPE_CONFIG:
1733 type_str = "config";
1740 case CB_TYPE_SHUTDOWN:
1741 type_str = "shutdown";
1745 type_str = "<unknown>";
1747 DEBUG ("java plugin: Registering new %s callback: %s",
1748 type_str, cbi->name);
1751 pthread_mutex_lock (&java_callbacks_lock);
1753 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1754 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1757 pthread_mutex_unlock (&java_callbacks_lock);
1758 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1760 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1765 java_callbacks = tmp;
1766 java_callbacks[java_callbacks_num] = *cbi;
1767 java_callbacks_num++;
1769 pthread_mutex_unlock (&java_callbacks_lock);
1773 } /* }}} int cjni_callback_register */
1775 /* Increase the reference counter to the JVM for this thread. If it was zero,
1776 * attach the JVM first. */
1777 static JNIEnv *cjni_thread_attach (void) /* {{{ */
1779 cjni_jvm_env_t *cjni_env;
1782 cjni_env = pthread_getspecific (jvm_env_key);
1783 if (cjni_env == NULL)
1785 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
1786 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
1787 if (cjni_env == NULL)
1789 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
1792 memset (cjni_env, 0, sizeof (*cjni_env));
1793 cjni_env->reference_counter = 0;
1794 cjni_env->jvm_env = NULL;
1796 pthread_setspecific (jvm_env_key, cjni_env);
1799 if (cjni_env->reference_counter > 0)
1801 cjni_env->reference_counter++;
1802 jvm_env = cjni_env->jvm_env;
1807 JavaVMAttachArgs args;
1809 assert (cjni_env->jvm_env == NULL);
1811 memset (&args, 0, sizeof (args));
1812 args.version = JNI_VERSION_1_2;
1814 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
1817 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
1818 "with status %i.", status);
1822 cjni_env->reference_counter = 1;
1823 cjni_env->jvm_env = jvm_env;
1826 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
1827 cjni_env->reference_counter);
1828 assert (jvm_env != NULL);
1830 } /* }}} JNIEnv *cjni_thread_attach */
1832 /* Decrease the reference counter of this thread. If it reaches zero, detach
1834 static int cjni_thread_detach (void) /* {{{ */
1836 cjni_jvm_env_t *cjni_env;
1839 cjni_env = pthread_getspecific (jvm_env_key);
1840 if (cjni_env == NULL)
1842 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
1846 assert (cjni_env->reference_counter > 0);
1847 assert (cjni_env->jvm_env != NULL);
1849 cjni_env->reference_counter--;
1850 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
1851 cjni_env->reference_counter);
1853 if (cjni_env->reference_counter > 0)
1856 status = (*jvm)->DetachCurrentThread (jvm);
1859 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
1860 "with status %i.", status);
1863 cjni_env->reference_counter = 0;
1864 cjni_env->jvm_env = NULL;
1867 } /* }}} JNIEnv *cjni_thread_attach */
1869 /* Callback for `pthread_key_create'. It frees the data contained in
1870 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1871 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1873 cjni_jvm_env_t *cjni_env;
1878 cjni_env = (cjni_jvm_env_t *) args;
1880 if (cjni_env->reference_counter > 0)
1882 ERROR ("java plugin: cjni_jvm_env_destroy: "
1883 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1886 if (cjni_env->jvm_env != NULL)
1888 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1889 (void *) cjni_env->jvm_env);
1892 /* The pointer is allocated in `cjni_thread_attach' */
1894 } /* }}} void cjni_jvm_env_destroy */
1896 /* Boring configuration functions.. {{{ */
1897 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
1901 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1903 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
1907 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
1910 ERROR ("java plugin: realloc failed.");
1915 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
1916 if (jvm_argv[jvm_argc] == NULL)
1918 ERROR ("java plugin: strdup failed.");
1924 } /* }}} int cjni_config_add_jvm_arg */
1926 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
1928 java_plugin_class_t *tmp;
1930 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1932 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
1936 tmp = (java_plugin_class_t *) realloc (java_classes_list,
1937 (java_classes_list_len + 1) * sizeof (*java_classes_list));
1940 ERROR ("java plugin: realloc failed.");
1943 java_classes_list = tmp;
1944 tmp = java_classes_list + java_classes_list_len;
1946 memset (tmp, 0, sizeof (*tmp));
1947 tmp->name = strdup (ci->values[0].value.string);
1948 if (tmp->name == NULL)
1950 ERROR ("java plugin: strdup failed.");
1956 java_classes_list_len++;
1959 } /* }}} int cjni_config_load_plugin */
1961 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
1963 java_plugin_config_t *tmp;
1967 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1969 WARNING ("java plugin: `Plugin' blocks "
1970 "need exactly one string argument.");
1974 name = strdup (ci->values[0].value.string);
1977 ERROR ("java plugin: cjni_config_plugin_block: strdup faiiled.");
1981 for (i = 0; i < java_plugin_configs_num; i++)
1983 if (strcmp (java_plugin_configs[i].name, name) == 0)
1985 WARNING ("java plugin: There is more than one <Plugin \"%s\"> block. "
1986 "This is currently not supported - "
1987 "only the first block will be used!",
1994 tmp = (java_plugin_config_t *) realloc (java_plugin_configs,
1995 (java_plugin_configs_num + 1) * sizeof (*java_plugin_configs));
1998 ERROR ("java plugin: cjni_config_plugin_block: realloc failed.");
2002 java_plugin_configs = tmp;
2003 tmp = java_plugin_configs + java_plugin_configs_num;
2006 tmp->ci = oconfig_clone (ci);
2007 if (tmp->ci == NULL)
2009 ERROR ("java plugin: cjni_config_plugin_block: "
2010 "oconfig_clone failed for `%s'.",
2016 DEBUG ("java plugin: cjni_config_plugin_block: "
2017 "Successfully copied config for `%s'.",
2020 java_plugin_configs_num++;
2022 } /* }}} int cjni_config_plugin_block */
2024 static int cjni_config (oconfig_item_t *ci) /* {{{ */
2034 for (i = 0; i < ci->children_num; i++)
2036 oconfig_item_t *child = ci->children + i;
2038 if (strcasecmp ("JVMArg", child->key) == 0)
2040 status = cjni_config_add_jvm_arg (child);
2046 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2048 status = cjni_config_load_plugin (child);
2054 else if (strcasecmp ("Plugin", child->key) == 0)
2056 status = cjni_config_plugin_block (child);
2064 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2069 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2070 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2071 DEBUG ("java plugin: java_plugin_configs_num = %zu;",
2072 java_plugin_configs_num);
2074 if ((success == 0) && (errors > 0))
2076 ERROR ("java plugin: All statements failed.");
2081 } /* }}} int cjni_config */
2084 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2085 * and `cjni_write'. In particular, delete the global reference to the Java
2087 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2090 cjni_callback_info_t *cbi;
2092 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2094 cbi = (cjni_callback_info_t *) arg;
2096 /* This condition can occurr when shutting down. */
2106 jvm_env = cjni_thread_attach ();
2107 if (jvm_env == NULL)
2109 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2113 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2120 cjni_thread_detach ();
2121 } /* }}} void cjni_callback_info_destroy */
2123 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2124 static int cjni_read (user_data_t *ud) /* {{{ */
2127 cjni_callback_info_t *cbi;
2133 ERROR ("java plugin: cjni_read: jvm == NULL");
2137 if ((ud == NULL) || (ud->data == NULL))
2139 ERROR ("java plugin: cjni_read: Invalid user data.");
2143 jvm_env = cjni_thread_attach ();
2144 if (jvm_env == NULL)
2147 cbi = (cjni_callback_info_t *) ud->data;
2149 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2152 status = cjni_thread_detach ();
2155 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2159 return (ret_status);
2160 } /* }}} int cjni_read */
2162 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2163 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2167 cjni_callback_info_t *cbi;
2174 ERROR ("java plugin: cjni_write: jvm == NULL");
2178 if ((ud == NULL) || (ud->data == NULL))
2180 ERROR ("java plugin: cjni_write: Invalid user data.");
2184 jvm_env = cjni_thread_attach ();
2185 if (jvm_env == NULL)
2188 cbi = (cjni_callback_info_t *) ud->data;
2190 vl_java = ctoj_value_list (jvm_env, ds, vl);
2191 if (vl_java == NULL)
2193 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2197 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2198 cbi->object, cbi->method, vl_java);
2200 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2202 status = cjni_thread_detach ();
2205 ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2209 return (ret_status);
2210 } /* }}} int cjni_write */
2212 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2213 static int cjni_flush (int timeout, const char *identifier, /* {{{ */
2217 cjni_callback_info_t *cbi;
2218 jobject o_identifier;
2224 ERROR ("java plugin: cjni_flush: jvm == NULL");
2228 if ((ud == NULL) || (ud->data == NULL))
2230 ERROR ("java plugin: cjni_flush: Invalid user data.");
2234 jvm_env = cjni_thread_attach ();
2235 if (jvm_env == NULL)
2238 cbi = (cjni_callback_info_t *) ud->data;
2240 o_identifier = NULL;
2241 if (identifier != NULL)
2243 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2244 if (o_identifier == NULL)
2246 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2251 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2252 cbi->object, cbi->method, (jint) timeout, o_identifier);
2254 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2256 status = cjni_thread_detach ();
2259 ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2263 return (ret_status);
2264 } /* }}} int cjni_flush */
2266 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2267 static void cjni_log (int severity, const char *message, /* {{{ */
2271 cjni_callback_info_t *cbi;
2277 if ((ud == NULL) || (ud->data == NULL))
2280 jvm_env = cjni_thread_attach ();
2281 if (jvm_env == NULL)
2284 cbi = (cjni_callback_info_t *) ud->data;
2286 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2287 if (o_message == NULL)
2290 (*jvm_env)->CallVoidMethod (jvm_env,
2291 cbi->object, cbi->method, (jint) severity, o_message);
2293 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2295 cjni_thread_detach ();
2296 } /* }}} void cjni_log */
2298 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2300 static int cjni_notification (const notification_t *n, /* {{{ */
2304 cjni_callback_info_t *cbi;
2305 jobject o_notification;
2311 ERROR ("java plugin: cjni_read: jvm == NULL");
2315 if ((ud == NULL) || (ud->data == NULL))
2317 ERROR ("java plugin: cjni_read: Invalid user data.");
2321 jvm_env = cjni_thread_attach ();
2322 if (jvm_env == NULL)
2325 cbi = (cjni_callback_info_t *) ud->data;
2327 o_notification = ctoj_notification (jvm_env, n);
2328 if (o_notification == NULL)
2330 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2334 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2335 cbi->object, cbi->method, o_notification);
2337 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2339 status = cjni_thread_detach ();
2342 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2346 return (ret_status);
2347 } /* }}} int cjni_notification */
2349 /* Iterate over `java_classes_list' and create one object of each class. This
2350 * will trigger the object's constructors, to the objects can register callback
2352 static int cjni_load_plugins (JNIEnv *jvm_env) /* {{{ */
2356 for (i = 0; i < java_classes_list_len; i++)
2358 java_plugin_class_t *class;
2359 jmethodID constructor_id;
2361 class = java_classes_list + i;
2363 DEBUG ("java plugin: Loading class %s", class->name);
2365 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2366 if (class->class == NULL)
2368 ERROR ("java plugin: cjni_load_plugins: FindClass (%s) failed.",
2373 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2375 if (constructor_id == NULL)
2377 ERROR ("java plugin: cjni_load_plugins: Could not find the constructor for `%s'.",
2382 class->object = (*jvm_env)->NewObject (jvm_env, class->class,
2384 if (class->object == NULL)
2386 ERROR ("java plugin: cjni_load_plugins: Could create a new `%s' object.",
2391 (*jvm_env)->NewGlobalRef (jvm_env, class->object);
2392 } /* for (i = 0; i < java_classes_list_len; i++) */
2395 } /* }}} int cjni_load_plugins */
2397 /* Iterate over `java_plugin_configs' and `java_callbacks' and call all
2398 * `config' callback methods for which a configuration is available. */
2399 static int cjni_config_plugins (JNIEnv *jvm_env) /* {{{ */
2405 for (i = 0; i < java_plugin_configs_num; i++)
2409 if (java_plugin_configs[i].ci == NULL)
2412 for (j = 0; j < java_callbacks_num; j++)
2414 if (java_callbacks[j].type != CB_TYPE_CONFIG)
2417 if (strcmp (java_plugin_configs[i].name, java_callbacks[j].name) == 0)
2421 if (j >= java_callbacks_num)
2423 NOTICE ("java plugin: Configuration for `%s' is present, but no such "
2424 "configuration callback has been registered.",
2425 java_plugin_configs[i].name);
2429 DEBUG ("java plugin: Configuring %s", java_plugin_configs[i].name);
2431 o_ocitem = ctoj_oconfig_item (jvm_env, java_plugin_configs[i].ci);
2432 if (o_ocitem == NULL)
2434 ERROR ("java plugin: cjni_config_plugins: ctoj_oconfig_item failed.");
2438 status = (*jvm_env)->CallIntMethod (jvm_env,
2439 java_callbacks[j].object, java_callbacks[j].method, o_ocitem);
2440 WARNING ("java plugin: Config callback for `%s' returned status %i.",
2441 java_plugin_configs[i].name, status);
2442 } /* for (i = 0; i < java_plugin_configs; i++) */
2445 } /* }}} int cjni_config_plugins */
2447 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2448 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2453 for (i = 0; i < java_callbacks_num; i++)
2455 if (java_callbacks[i].type != CB_TYPE_INIT)
2458 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2460 status = (*jvm_env)->CallIntMethod (jvm_env,
2461 java_callbacks[i].object, java_callbacks[i].method);
2464 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2465 "Removing read function.",
2466 java_callbacks[i].name, status);
2467 plugin_unregister_read (java_callbacks[i].name);
2472 } /* }}} int cjni_init_plugins */
2474 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2475 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2480 for (i = 0; i < java_callbacks_num; i++)
2482 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2485 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2487 status = (*jvm_env)->CallIntMethod (jvm_env,
2488 java_callbacks[i].object, java_callbacks[i].method);
2491 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2492 java_callbacks[i].name, status);
2497 } /* }}} int cjni_shutdown_plugins */
2500 static int cjni_shutdown (void) /* {{{ */
2503 JavaVMAttachArgs args;
2511 memset (&args, 0, sizeof (args));
2512 args.version = JNI_VERSION_1_2;
2514 status = (*jvm)->AttachCurrentThread (jvm, (void **) &jvm_env, &args);
2517 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2522 /* Execute all the shutdown functions registered by plugins. */
2523 cjni_shutdown_plugins (jvm_env);
2525 /* Release all the global references to callback functions */
2526 for (i = 0; i < java_callbacks_num; i++)
2528 if (java_callbacks[i].object != NULL)
2530 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
2531 java_callbacks[i].object = NULL;
2533 sfree (java_callbacks[i].name);
2535 java_callbacks_num = 0;
2536 sfree (java_callbacks);
2538 /* Release all the global references to directly loaded classes. */
2539 for (i = 0; i < java_classes_list_len; i++)
2541 if (java_classes_list[i].object != NULL)
2543 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
2544 java_classes_list[i].object = NULL;
2546 sfree (java_classes_list[i].name);
2548 java_classes_list_len = 0;
2549 sfree (java_classes_list);
2551 /* Destroy the JVM */
2552 DEBUG ("java plugin: Destroying the JVM.");
2553 (*jvm)->DestroyJavaVM (jvm);
2557 pthread_key_delete (jvm_env_key);
2559 /* Free the JVM argument list */
2560 for (i = 0; i < jvm_argc; i++)
2561 sfree (jvm_argv[i]);
2565 /* Free the copied configuration */
2566 for (i = 0; i < java_plugin_configs_num; i++)
2568 sfree (java_plugin_configs[i].name);
2569 oconfig_free (java_plugin_configs[i].ci);
2571 java_plugin_configs_num = 0;
2572 sfree (java_plugin_configs);
2575 } /* }}} int cjni_shutdown */
2577 /* Register ``native'' functions with the JVM. Native functions are C-functions
2578 * that can be called by Java code. */
2579 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
2581 jclass api_class_ptr;
2584 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.Collectd");
2585 if (api_class_ptr == NULL)
2587 ERROR ("cjni_init_native: Cannot find API class `org.collectd.api.Collectd'.");
2591 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
2592 jni_api_functions, (jint) jni_api_functions_num);
2595 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
2600 } /* }}} int cjni_init_native */
2602 /* Initialization: Create a JVM, load all configured classes and call their
2603 * `config' and `init' callback methods. */
2604 static int cjni_init (void) /* {{{ */
2607 JavaVMInitArgs vm_args;
2608 JavaVMOption vm_options[jvm_argc];
2616 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
2619 ERROR ("java plugin: cjni_init: pthread_key_create failed "
2620 "with status %i.", status);
2626 memset (&vm_args, 0, sizeof (vm_args));
2627 vm_args.version = JNI_VERSION_1_2;
2628 vm_args.options = vm_options;
2629 vm_args.nOptions = (jint) jvm_argc;
2631 for (i = 0; i < jvm_argc; i++)
2633 DEBUG ("java plugin: cjni_init: jvm_argv[%zu] = %s", i, jvm_argv[i]);
2634 vm_args.options[i].optionString = jvm_argv[i];
2637 vm_args.options[0].optionString = "-verbose:jni";
2638 vm_args.options[1].optionString = "-Djava.class.path=/home/octo/collectd/bindings/java";
2641 status = JNI_CreateJavaVM (&jvm, (void **) &jvm_env, (void **) &vm_args);
2644 ERROR ("cjni_init: JNI_CreateJavaVM failed with status %i.",
2648 assert (jvm != NULL);
2649 assert (jvm_env != NULL);
2651 /* Call RegisterNatives */
2652 status = cjni_init_native (jvm_env);
2655 ERROR ("cjni_init: cjni_init_native failed.");
2659 cjni_load_plugins (jvm_env);
2660 cjni_config_plugins (jvm_env);
2661 cjni_init_plugins (jvm_env);
2664 } /* }}} int cjni_init */
2666 void module_register (void)
2668 plugin_register_complex_config ("java", cjni_config);
2669 plugin_register_init ("java", cjni_init);
2670 plugin_register_shutdown ("java", cjni_shutdown);
2671 } /* void module_register (void) */
2673 /* vim: set sw=2 sts=2 et fdm=marker : */