0a9bb9e5a8fb2f628aefcd1dd0c7e39e7623e148
1 /**
2 * collectd - src/java.c
3 * Copyright (C) 2009 Florian octo Forster
4 * Copyright (C) 2008 Justo Alonso Achaques
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at collectd.org>
21 * Justo Alonso Achaques <justo.alonso at gmail.com>
22 **/
24 #include "collectd.h"
26 #include "common.h"
27 #include "filter_chain.h"
28 #include "plugin.h"
30 #include <jni.h>
32 #if !defined(JNI_VERSION_1_2)
33 #error "Need JNI 1.2 compatible interface!"
34 #endif
36 /*
37 * Types
38 */
39 struct cjni_jvm_env_s /* {{{ */
40 {
41 JNIEnv *jvm_env;
42 int reference_counter;
43 };
44 typedef struct cjni_jvm_env_s cjni_jvm_env_t;
45 /* }}} */
47 struct java_plugin_class_s /* {{{ */
48 {
49 char *name;
50 jclass class;
51 jobject object;
52 };
53 typedef struct java_plugin_class_s java_plugin_class_t;
54 /* }}} */
56 #define CB_TYPE_CONFIG 1
57 #define CB_TYPE_INIT 2
58 #define CB_TYPE_READ 3
59 #define CB_TYPE_WRITE 4
60 #define CB_TYPE_FLUSH 5
61 #define CB_TYPE_SHUTDOWN 6
62 #define CB_TYPE_LOG 7
63 #define CB_TYPE_NOTIFICATION 8
64 #define CB_TYPE_MATCH 9
65 #define CB_TYPE_TARGET 10
66 struct cjni_callback_info_s /* {{{ */
67 {
68 char *name;
69 int type;
70 jclass class;
71 jobject object;
72 jmethodID method;
73 };
74 typedef struct cjni_callback_info_s cjni_callback_info_t;
75 /* }}} */
77 /*
78 * Global variables
79 */
80 static JavaVM *jvm = NULL;
81 static pthread_key_t jvm_env_key;
83 /* Configuration options for the JVM. */
84 static char **jvm_argv = NULL;
85 static size_t jvm_argc = 0;
87 /* List of class names to load */
88 static java_plugin_class_t *java_classes_list = NULL;
89 static size_t java_classes_list_len;
91 /* List of config, init, and shutdown callbacks. */
92 static cjni_callback_info_t *java_callbacks = NULL;
93 static size_t java_callbacks_num = 0;
94 static pthread_mutex_t java_callbacks_lock = PTHREAD_MUTEX_INITIALIZER;
96 static oconfig_item_t *config_block = NULL;
98 /*
99 * Prototypes
100 *
101 * Mostly functions that are needed by the Java interface (``native'')
102 * functions.
103 */
104 static void cjni_callback_info_destroy(void *arg);
105 static cjni_callback_info_t *cjni_callback_info_create(JNIEnv *jvm_env,
106 jobject o_name,
107 jobject o_callback,
108 int type);
109 static int cjni_callback_register(JNIEnv *jvm_env, jobject o_name,
110 jobject o_callback, int type);
111 static int cjni_read(user_data_t *user_data);
112 static int cjni_write(const data_set_t *ds, const value_list_t *vl,
113 user_data_t *ud);
114 static int cjni_flush(cdtime_t timeout, const char *identifier,
115 user_data_t *ud);
116 static void cjni_log(int severity, const char *message, user_data_t *ud);
117 static int cjni_notification(const notification_t *n, user_data_t *ud);
119 /* Create, destroy, and match/invoke functions, used by both, matches AND
120 * targets. */
121 static int cjni_match_target_create(const oconfig_item_t *ci, void **user_data);
122 static int cjni_match_target_destroy(void **user_data);
123 static int cjni_match_target_invoke(const data_set_t *ds, value_list_t *vl,
124 notification_meta_t **meta,
125 void **user_data);
127 /*
128 * C to Java conversion functions
129 */
130 static int ctoj_string(JNIEnv *jvm_env, /* {{{ */
131 const char *string, jclass class_ptr, jobject object_ptr,
132 const char *method_name) {
133 jmethodID m_set;
134 jstring o_string;
136 /* Create a java.lang.String */
137 o_string = (*jvm_env)->NewStringUTF(jvm_env, (string != NULL) ? string : "");
138 if (o_string == NULL) {
139 ERROR("java plugin: ctoj_string: NewStringUTF failed.");
140 return (-1);
141 }
143 /* Search for the `void setFoo (String s)' method. */
144 m_set = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name,
145 "(Ljava/lang/String;)V");
146 if (m_set == NULL) {
147 ERROR("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
148 method_name);
149 (*jvm_env)->DeleteLocalRef(jvm_env, o_string);
150 return (-1);
151 }
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);
159 return (0);
160 } /* }}} int ctoj_string */
162 static jstring ctoj_output_string(JNIEnv *jvm_env, /* {{{ */
163 const char *string) {
164 jstring o_string;
166 /* Create a java.lang.String */
167 o_string = (*jvm_env)->NewStringUTF(jvm_env, (string != NULL) ? string : "");
168 if (o_string == NULL) {
169 ERROR("java plugin: ctoj_output_string: NewStringUTF failed.");
170 return NULL;
171 }
173 return (o_string);
174 } /* }}} int ctoj_output_string */
176 static int ctoj_int(JNIEnv *jvm_env, /* {{{ */
177 jint value, jclass class_ptr, jobject object_ptr,
178 const char *method_name) {
179 jmethodID m_set;
181 /* Search for the `void setFoo (int i)' method. */
182 m_set = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "(I)V");
183 if (m_set == NULL) {
184 ERROR("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
185 method_name);
186 return (-1);
187 }
189 (*jvm_env)->CallVoidMethod(jvm_env, object_ptr, m_set, value);
191 return (0);
192 } /* }}} int ctoj_int */
194 static int ctoj_long(JNIEnv *jvm_env, /* {{{ */
195 jlong value, jclass class_ptr, jobject object_ptr,
196 const char *method_name) {
197 jmethodID m_set;
199 /* Search for the `void setFoo (long l)' method. */
200 m_set = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "(J)V");
201 if (m_set == NULL) {
202 ERROR("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
203 method_name);
204 return (-1);
205 }
207 (*jvm_env)->CallVoidMethod(jvm_env, object_ptr, m_set, value);
209 return (0);
210 } /* }}} int ctoj_long */
212 static int ctoj_double(JNIEnv *jvm_env, /* {{{ */
213 jdouble value, jclass class_ptr, jobject object_ptr,
214 const char *method_name) {
215 jmethodID m_set;
217 /* Search for the `void setFoo (double d)' method. */
218 m_set = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "(D)V");
219 if (m_set == NULL) {
220 ERROR("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
221 method_name);
222 return (-1);
223 }
225 (*jvm_env)->CallVoidMethod(jvm_env, object_ptr, m_set, value);
227 return (0);
228 } /* }}} int ctoj_double */
230 /* Convert a jlong to a java.lang.Number */
231 static jobject ctoj_jlong_to_number(JNIEnv *jvm_env, jlong value) /* {{{ */
232 {
233 jclass c_long;
234 jmethodID m_long_constructor;
236 /* Look up the java.lang.Long class */
237 c_long = (*jvm_env)->FindClass(jvm_env, "java/lang/Long");
238 if (c_long == NULL) {
239 ERROR("java plugin: ctoj_jlong_to_number: Looking up the "
240 "java.lang.Long class failed.");
241 return (NULL);
242 }
244 m_long_constructor =
245 (*jvm_env)->GetMethodID(jvm_env, c_long, "<init>", "(J)V");
246 if (m_long_constructor == NULL) {
247 ERROR("java plugin: ctoj_jlong_to_number: Looking up the "
248 "`Long (long)' constructor failed.");
249 return (NULL);
250 }
252 return ((*jvm_env)->NewObject(jvm_env, c_long, m_long_constructor, value));
253 } /* }}} jobject ctoj_jlong_to_number */
255 /* Convert a jdouble to a java.lang.Number */
256 static jobject ctoj_jdouble_to_number(JNIEnv *jvm_env, jdouble value) /* {{{ */
257 {
258 jclass c_double;
259 jmethodID m_double_constructor;
261 /* Look up the java.lang.Long class */
262 c_double = (*jvm_env)->FindClass(jvm_env, "java/lang/Double");
263 if (c_double == NULL) {
264 ERROR("java plugin: ctoj_jdouble_to_number: Looking up the "
265 "java.lang.Double class failed.");
266 return (NULL);
267 }
269 m_double_constructor =
270 (*jvm_env)->GetMethodID(jvm_env, c_double, "<init>", "(D)V");
271 if (m_double_constructor == NULL) {
272 ERROR("java plugin: ctoj_jdouble_to_number: Looking up the "
273 "`Double (double)' constructor failed.");
274 return (NULL);
275 }
277 return (
278 (*jvm_env)->NewObject(jvm_env, 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) {
284 if (ds_type == DS_TYPE_COUNTER)
285 return (ctoj_jlong_to_number(jvm_env, (jlong)value.counter));
286 else if (ds_type == DS_TYPE_GAUGE)
287 return (ctoj_jdouble_to_number(jvm_env, (jdouble)value.gauge));
288 if (ds_type == DS_TYPE_DERIVE)
289 return (ctoj_jlong_to_number(jvm_env, (jlong)value.derive));
290 if (ds_type == DS_TYPE_ABSOLUTE)
291 return (ctoj_jlong_to_number(jvm_env, (jlong)value.absolute));
292 else
293 return (NULL);
294 } /* }}} jobject ctoj_value_to_number */
296 /* Convert a data_source_t to a org/collectd/api/DataSource */
297 static jobject ctoj_data_source(JNIEnv *jvm_env, /* {{{ */
298 const data_source_t *dsrc) {
299 jclass c_datasource;
300 jmethodID m_datasource_constructor;
301 jobject o_datasource;
302 int status;
304 /* Look up the DataSource class */
305 c_datasource = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/DataSource");
306 if (c_datasource == NULL) {
307 ERROR("java plugin: ctoj_data_source: "
308 "FindClass (org/collectd/api/DataSource) failed.");
309 return (NULL);
310 }
312 /* Lookup the `ValueList ()' constructor. */
313 m_datasource_constructor =
314 (*jvm_env)->GetMethodID(jvm_env, c_datasource, "<init>", "()V");
315 if (m_datasource_constructor == NULL) {
316 ERROR("java plugin: ctoj_data_source: Cannot find the "
317 "`DataSource ()' constructor.");
318 return (NULL);
319 }
321 /* Create a new instance. */
322 o_datasource =
323 (*jvm_env)->NewObject(jvm_env, c_datasource, m_datasource_constructor);
324 if (o_datasource == NULL) {
325 ERROR("java plugin: ctoj_data_source: "
326 "Creating a new DataSource instance failed.");
327 return (NULL);
328 }
330 /* Set name via `void setName (String name)' */
331 status =
332 ctoj_string(jvm_env, dsrc->name, c_datasource, o_datasource, "setName");
333 if (status != 0) {
334 ERROR("java plugin: ctoj_data_source: "
335 "ctoj_string (setName) failed.");
336 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
337 return (NULL);
338 }
340 /* Set type via `void setType (int type)' */
341 status = ctoj_int(jvm_env, dsrc->type, c_datasource, o_datasource, "setType");
342 if (status != 0) {
343 ERROR("java plugin: ctoj_data_source: "
344 "ctoj_int (setType) failed.");
345 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
346 return (NULL);
347 }
349 /* Set min via `void setMin (double min)' */
350 status =
351 ctoj_double(jvm_env, dsrc->min, c_datasource, o_datasource, "setMin");
352 if (status != 0) {
353 ERROR("java plugin: ctoj_data_source: "
354 "ctoj_double (setMin) failed.");
355 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
356 return (NULL);
357 }
359 /* Set max via `void setMax (double max)' */
360 status =
361 ctoj_double(jvm_env, dsrc->max, c_datasource, o_datasource, "setMax");
362 if (status != 0) {
363 ERROR("java plugin: ctoj_data_source: "
364 "ctoj_double (setMax) failed.");
365 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
366 return (NULL);
367 }
369 return (o_datasource);
370 } /* }}} jobject ctoj_data_source */
372 /* Convert a oconfig_value_t to a org/collectd/api/OConfigValue */
373 static jobject ctoj_oconfig_value(JNIEnv *jvm_env, /* {{{ */
374 oconfig_value_t ocvalue) {
375 jclass c_ocvalue;
376 jmethodID m_ocvalue_constructor;
377 jobject o_argument;
378 jobject o_ocvalue;
380 m_ocvalue_constructor = NULL;
381 o_argument = NULL;
383 c_ocvalue = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/OConfigValue");
384 if (c_ocvalue == NULL) {
385 ERROR("java plugin: ctoj_oconfig_value: "
386 "FindClass (org/collectd/api/OConfigValue) failed.");
387 return (NULL);
388 }
390 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) {
391 jboolean tmp_boolean;
393 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
395 m_ocvalue_constructor =
396 (*jvm_env)->GetMethodID(jvm_env, c_ocvalue, "<init>", "(Z)V");
397 if (m_ocvalue_constructor == NULL) {
398 ERROR("java plugin: ctoj_oconfig_value: Cannot find the "
399 "`OConfigValue (boolean)' constructor.");
400 return (NULL);
401 }
403 return ((*jvm_env)->NewObject(jvm_env, c_ocvalue, m_ocvalue_constructor,
404 tmp_boolean));
405 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
406 else if (ocvalue.type == OCONFIG_TYPE_STRING) {
407 m_ocvalue_constructor = (*jvm_env)->GetMethodID(
408 jvm_env, c_ocvalue, "<init>", "(Ljava/lang/String;)V");
409 if (m_ocvalue_constructor == NULL) {
410 ERROR("java plugin: ctoj_oconfig_value: Cannot find the "
411 "`OConfigValue (String)' constructor.");
412 return (NULL);
413 }
415 o_argument = (*jvm_env)->NewStringUTF(jvm_env, ocvalue.value.string);
416 if (o_argument == NULL) {
417 ERROR("java plugin: ctoj_oconfig_value: "
418 "Creating a String object failed.");
419 return (NULL);
420 }
421 } else if (ocvalue.type == OCONFIG_TYPE_NUMBER) {
422 m_ocvalue_constructor = (*jvm_env)->GetMethodID(
423 jvm_env, c_ocvalue, "<init>", "(Ljava/lang/Number;)V");
424 if (m_ocvalue_constructor == NULL) {
425 ERROR("java plugin: ctoj_oconfig_value: Cannot find the "
426 "`OConfigValue (Number)' constructor.");
427 return (NULL);
428 }
430 o_argument = ctoj_jdouble_to_number(jvm_env, (jdouble)ocvalue.value.number);
431 if (o_argument == NULL) {
432 ERROR("java plugin: ctoj_oconfig_value: "
433 "Creating a Number object failed.");
434 return (NULL);
435 }
436 } else {
437 return (NULL);
438 }
440 assert(m_ocvalue_constructor != NULL);
441 assert(o_argument != NULL);
443 o_ocvalue = (*jvm_env)->NewObject(jvm_env, c_ocvalue, m_ocvalue_constructor,
444 o_argument);
445 if (o_ocvalue == NULL) {
446 ERROR("java plugin: ctoj_oconfig_value: "
447 "Creating an OConfigValue object failed.");
448 (*jvm_env)->DeleteLocalRef(jvm_env, o_argument);
449 return (NULL);
450 }
452 (*jvm_env)->DeleteLocalRef(jvm_env, o_argument);
453 return (o_ocvalue);
454 } /* }}} jobject ctoj_oconfig_value */
456 /* Convert a oconfig_item_t to a org/collectd/api/OConfigItem */
457 static jobject ctoj_oconfig_item(JNIEnv *jvm_env, /* {{{ */
458 const oconfig_item_t *ci) {
459 jclass c_ocitem;
460 jmethodID m_ocitem_constructor;
461 jmethodID m_addvalue;
462 jmethodID m_addchild;
463 jobject o_key;
464 jobject o_ocitem;
466 c_ocitem = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/OConfigItem");
467 if (c_ocitem == NULL) {
468 ERROR("java plugin: ctoj_oconfig_item: "
469 "FindClass (org/collectd/api/OConfigItem) failed.");
470 return (NULL);
471 }
473 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
474 * {{{ */
475 m_ocitem_constructor = (*jvm_env)->GetMethodID(jvm_env, c_ocitem, "<init>",
476 "(Ljava/lang/String;)V");
477 if (m_ocitem_constructor == NULL) {
478 ERROR("java plugin: ctoj_oconfig_item: Cannot find the "
479 "`OConfigItem (String)' constructor.");
480 return (NULL);
481 }
483 m_addvalue = (*jvm_env)->GetMethodID(jvm_env, c_ocitem, "addValue",
484 "(Lorg/collectd/api/OConfigValue;)V");
485 if (m_addvalue == NULL) {
486 ERROR("java plugin: ctoj_oconfig_item: Cannot find the "
487 "`addValue (OConfigValue)' method.");
488 return (NULL);
489 }
491 m_addchild = (*jvm_env)->GetMethodID(jvm_env, c_ocitem, "addChild",
492 "(Lorg/collectd/api/OConfigItem;)V");
493 if (m_addchild == NULL) {
494 ERROR("java plugin: ctoj_oconfig_item: Cannot find the "
495 "`addChild (OConfigItem)' method.");
496 return (NULL);
497 }
498 /* }}} */
500 /* Create a String object with the key.
501 * Needed for calling the constructor. */
502 o_key = (*jvm_env)->NewStringUTF(jvm_env, ci->key);
503 if (o_key == NULL) {
504 ERROR("java plugin: ctoj_oconfig_item: "
505 "Creating String object failed.");
506 return (NULL);
507 }
509 /* Create an OConfigItem object */
510 o_ocitem =
511 (*jvm_env)->NewObject(jvm_env, c_ocitem, m_ocitem_constructor, o_key);
512 if (o_ocitem == NULL) {
513 ERROR("java plugin: ctoj_oconfig_item: "
514 "Creating an OConfigItem object failed.");
515 (*jvm_env)->DeleteLocalRef(jvm_env, o_key);
516 return (NULL);
517 }
519 /* We don't need the String object any longer.. */
520 (*jvm_env)->DeleteLocalRef(jvm_env, o_key);
522 /* Call OConfigItem.addValue for each value */
523 for (int i = 0; i < ci->values_num; i++) /* {{{ */
524 {
525 jobject o_value;
527 o_value = ctoj_oconfig_value(jvm_env, ci->values[i]);
528 if (o_value == NULL) {
529 ERROR("java plugin: ctoj_oconfig_item: "
530 "Creating an OConfigValue object failed.");
531 (*jvm_env)->DeleteLocalRef(jvm_env, o_ocitem);
532 return (NULL);
533 }
535 (*jvm_env)->CallVoidMethod(jvm_env, o_ocitem, m_addvalue, o_value);
536 (*jvm_env)->DeleteLocalRef(jvm_env, o_value);
537 } /* }}} for (i = 0; i < ci->values_num; i++) */
539 /* Call OConfigItem.addChild for each child */
540 for (int i = 0; i < ci->children_num; i++) /* {{{ */
541 {
542 jobject o_child;
544 o_child = ctoj_oconfig_item(jvm_env, ci->children + i);
545 if (o_child == NULL) {
546 ERROR("java plugin: ctoj_oconfig_item: "
547 "Creating an OConfigItem object failed.");
548 (*jvm_env)->DeleteLocalRef(jvm_env, o_ocitem);
549 return (NULL);
550 }
552 (*jvm_env)->CallVoidMethod(jvm_env, o_ocitem, m_addchild, o_child);
553 (*jvm_env)->DeleteLocalRef(jvm_env, o_child);
554 } /* }}} for (i = 0; i < ci->children_num; i++) */
556 return (o_ocitem);
557 } /* }}} jobject ctoj_oconfig_item */
559 /* Convert a data_set_t to a org/collectd/api/DataSet */
560 static jobject ctoj_data_set(JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
561 {
562 jclass c_dataset;
563 jmethodID m_constructor;
564 jmethodID m_add;
565 jobject o_type;
566 jobject o_dataset;
568 /* Look up the org/collectd/api/DataSet class */
569 c_dataset = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/DataSet");
570 if (c_dataset == NULL) {
571 ERROR("java plugin: ctoj_data_set: Looking up the "
572 "org/collectd/api/DataSet class failed.");
573 return (NULL);
574 }
576 /* Search for the `DataSet (String type)' constructor. */
577 m_constructor = (*jvm_env)->GetMethodID(jvm_env, c_dataset, "<init>",
578 "(Ljava/lang/String;)V");
579 if (m_constructor == NULL) {
580 ERROR("java plugin: ctoj_data_set: Looking up the "
581 "`DataSet (String)' constructor failed.");
582 return (NULL);
583 }
585 /* Search for the `void addDataSource (DataSource)' method. */
586 m_add = (*jvm_env)->GetMethodID(jvm_env, c_dataset, "addDataSource",
587 "(Lorg/collectd/api/DataSource;)V");
588 if (m_add == NULL) {
589 ERROR("java plugin: ctoj_data_set: Looking up the "
590 "`addDataSource (DataSource)' method failed.");
591 return (NULL);
592 }
594 o_type = (*jvm_env)->NewStringUTF(jvm_env, ds->type);
595 if (o_type == NULL) {
596 ERROR("java plugin: ctoj_data_set: Creating a String object failed.");
597 return (NULL);
598 }
600 o_dataset = (*jvm_env)->NewObject(jvm_env, c_dataset, m_constructor, o_type);
601 if (o_dataset == NULL) {
602 ERROR("java plugin: ctoj_data_set: Creating a DataSet object failed.");
603 (*jvm_env)->DeleteLocalRef(jvm_env, o_type);
604 return (NULL);
605 }
607 /* Decrease reference counter on the java.lang.String object. */
608 (*jvm_env)->DeleteLocalRef(jvm_env, o_type);
610 for (size_t i = 0; i < ds->ds_num; i++) {
611 jobject o_datasource;
613 o_datasource = ctoj_data_source(jvm_env, ds->ds + i);
614 if (o_datasource == NULL) {
615 ERROR("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
616 ds->type, ds->ds[i].name);
617 (*jvm_env)->DeleteLocalRef(jvm_env, o_dataset);
618 return (NULL);
619 }
621 (*jvm_env)->CallVoidMethod(jvm_env, o_dataset, m_add, o_datasource);
623 (*jvm_env)->DeleteLocalRef(jvm_env, o_datasource);
624 } /* for (i = 0; i < ds->ds_num; i++) */
626 return (o_dataset);
627 } /* }}} jobject ctoj_data_set */
629 static int ctoj_value_list_add_value(JNIEnv *jvm_env, /* {{{ */
630 value_t value, int ds_type,
631 jclass class_ptr, jobject object_ptr) {
632 jmethodID m_addvalue;
633 jobject o_number;
635 m_addvalue = (*jvm_env)->GetMethodID(jvm_env, class_ptr, "addValue",
636 "(Ljava/lang/Number;)V");
637 if (m_addvalue == NULL) {
638 ERROR("java plugin: ctoj_value_list_add_value: "
639 "Cannot find method `void addValue (Number)'.");
640 return (-1);
641 }
643 o_number = ctoj_value_to_number(jvm_env, value, ds_type);
644 if (o_number == NULL) {
645 ERROR("java plugin: ctoj_value_list_add_value: "
646 "ctoj_value_to_number failed.");
647 return (-1);
648 }
650 (*jvm_env)->CallVoidMethod(jvm_env, object_ptr, m_addvalue, o_number);
652 (*jvm_env)->DeleteLocalRef(jvm_env, o_number);
654 return (0);
655 } /* }}} int ctoj_value_list_add_value */
657 static int ctoj_value_list_add_data_set(JNIEnv *jvm_env, /* {{{ */
658 jclass c_valuelist, jobject o_valuelist,
659 const data_set_t *ds) {
660 jmethodID m_setdataset;
661 jobject o_dataset;
663 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
664 m_setdataset = (*jvm_env)->GetMethodID(jvm_env, c_valuelist, "setDataSet",
665 "(Lorg/collectd/api/DataSet;)V");
666 if (m_setdataset == NULL) {
667 ERROR("java plugin: ctoj_value_list_add_data_set: "
668 "Cannot find the `void setDataSet (DataSet)' method.");
669 return (-1);
670 }
672 /* Create a DataSet object. */
673 o_dataset = ctoj_data_set(jvm_env, ds);
674 if (o_dataset == NULL) {
675 ERROR("java plugin: ctoj_value_list_add_data_set: "
676 "ctoj_data_set (%s) failed.",
677 ds->type);
678 return (-1);
679 }
681 /* Actually call the method. */
682 (*jvm_env)->CallVoidMethod(jvm_env, o_valuelist, m_setdataset, o_dataset);
684 /* Decrease reference counter on the List<DataSource> object. */
685 (*jvm_env)->DeleteLocalRef(jvm_env, o_dataset);
687 return (0);
688 } /* }}} int ctoj_value_list_add_data_set */
690 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
691 static jobject ctoj_value_list(JNIEnv *jvm_env, /* {{{ */
692 const data_set_t *ds, const value_list_t *vl) {
693 jclass c_valuelist;
694 jmethodID m_valuelist_constructor;
695 jobject o_valuelist;
696 int status;
698 /* First, create a new ValueList instance..
699 * Look up the class.. */
700 c_valuelist = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/ValueList");
701 if (c_valuelist == NULL) {
702 ERROR("java plugin: ctoj_value_list: "
703 "FindClass (org/collectd/api/ValueList) failed.");
704 return (NULL);
705 }
707 /* Lookup the `ValueList ()' constructor. */
708 m_valuelist_constructor =
709 (*jvm_env)->GetMethodID(jvm_env, c_valuelist, "<init>", "()V");
710 if (m_valuelist_constructor == NULL) {
711 ERROR("java plugin: ctoj_value_list: Cannot find the "
712 "`ValueList ()' constructor.");
713 return (NULL);
714 }
716 /* Create a new instance. */
717 o_valuelist =
718 (*jvm_env)->NewObject(jvm_env, c_valuelist, m_valuelist_constructor);
719 if (o_valuelist == NULL) {
720 ERROR("java plugin: ctoj_value_list: Creating a new ValueList instance "
721 "failed.");
722 return (NULL);
723 }
725 status = ctoj_value_list_add_data_set(jvm_env, c_valuelist, o_valuelist, ds);
726 if (status != 0) {
727 ERROR("java plugin: ctoj_value_list: "
728 "ctoj_value_list_add_data_set failed.");
729 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist);
730 return (NULL);
731 }
733 /* Set the strings.. */
734 #define SET_STRING(str, method_name) \
735 do { \
736 status = ctoj_string(jvm_env, str, c_valuelist, o_valuelist, method_name); \
737 if (status != 0) { \
738 ERROR("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
739 method_name); \
740 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist); \
741 return (NULL); \
742 } \
743 } while (0)
745 SET_STRING(vl->host, "setHost");
746 SET_STRING(vl->plugin, "setPlugin");
747 SET_STRING(vl->plugin_instance, "setPluginInstance");
748 SET_STRING(vl->type, "setType");
749 SET_STRING(vl->type_instance, "setTypeInstance");
751 #undef SET_STRING
753 /* Set the `time' member. Java stores time in milliseconds. */
754 status = ctoj_long(jvm_env, (jlong)CDTIME_T_TO_MS(vl->time), c_valuelist,
755 o_valuelist, "setTime");
756 if (status != 0) {
757 ERROR("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
758 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist);
759 return (NULL);
760 }
762 /* Set the `interval' member.. */
763 status = ctoj_long(jvm_env, (jlong)CDTIME_T_TO_MS(vl->interval), c_valuelist,
764 o_valuelist, "setInterval");
765 if (status != 0) {
766 ERROR("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
767 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist);
768 return (NULL);
769 }
771 for (size_t i = 0; i < vl->values_len; i++) {
772 status = ctoj_value_list_add_value(jvm_env, vl->values[i], ds->ds[i].type,
773 c_valuelist, o_valuelist);
774 if (status != 0) {
775 ERROR("java plugin: ctoj_value_list: "
776 "ctoj_value_list_add_value failed.");
777 (*jvm_env)->DeleteLocalRef(jvm_env, o_valuelist);
778 return (NULL);
779 }
780 }
782 return (o_valuelist);
783 } /* }}} jobject ctoj_value_list */
785 /* Convert a notification_t to a org/collectd/api/Notification */
786 static jobject ctoj_notification(JNIEnv *jvm_env, /* {{{ */
787 const notification_t *n) {
788 jclass c_notification;
789 jmethodID m_constructor;
790 jobject o_notification;
791 int status;
793 /* First, create a new Notification instance..
794 * Look up the class.. */
795 c_notification =
796 (*jvm_env)->FindClass(jvm_env, "org/collectd/api/Notification");
797 if (c_notification == NULL) {
798 ERROR("java plugin: ctoj_notification: "
799 "FindClass (org/collectd/api/Notification) failed.");
800 return (NULL);
801 }
803 /* Lookup the `Notification ()' constructor. */
804 m_constructor =
805 (*jvm_env)->GetMethodID(jvm_env, c_notification, "<init>", "()V");
806 if (m_constructor == NULL) {
807 ERROR("java plugin: ctoj_notification: Cannot find the "
808 "`Notification ()' constructor.");
809 return (NULL);
810 }
812 /* Create a new instance. */
813 o_notification =
814 (*jvm_env)->NewObject(jvm_env, c_notification, m_constructor);
815 if (o_notification == NULL) {
816 ERROR("java plugin: ctoj_notification: Creating a new Notification "
817 "instance failed.");
818 return (NULL);
819 }
821 /* Set the strings.. */
822 #define SET_STRING(str, method_name) \
823 do { \
824 status = ctoj_string(jvm_env, str, c_notification, o_notification, \
825 method_name); \
826 if (status != 0) { \
827 ERROR("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
828 method_name); \
829 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification); \
830 return (NULL); \
831 } \
832 } while (0)
834 SET_STRING(n->host, "setHost");
835 SET_STRING(n->plugin, "setPlugin");
836 SET_STRING(n->plugin_instance, "setPluginInstance");
837 SET_STRING(n->type, "setType");
838 SET_STRING(n->type_instance, "setTypeInstance");
839 SET_STRING(n->message, "setMessage");
841 #undef SET_STRING
843 /* Set the `time' member. Java stores time in milliseconds. */
844 status = ctoj_long(jvm_env, (jlong)CDTIME_T_TO_MS(n->time), c_notification,
845 o_notification, "setTime");
846 if (status != 0) {
847 ERROR("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
848 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification);
849 return (NULL);
850 }
852 /* Set the `severity' member.. */
853 status = ctoj_int(jvm_env, (jint)n->severity, c_notification, o_notification,
854 "setSeverity");
855 if (status != 0) {
856 ERROR("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
857 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification);
858 return (NULL);
859 }
861 return (o_notification);
862 } /* }}} jobject ctoj_notification */
864 /*
865 * Java to C conversion functions
866 */
867 /* Call a `String <method> ()' method. */
868 static int jtoc_string(JNIEnv *jvm_env, /* {{{ */
869 char *buffer, size_t buffer_size, int empty_okay,
870 jclass class_ptr, jobject object_ptr,
871 const char *method_name) {
872 jmethodID method_id;
873 jobject string_obj;
874 const char *c_str;
876 method_id = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name,
877 "()Ljava/lang/String;");
878 if (method_id == NULL) {
879 ERROR("java plugin: jtoc_string: Cannot find method `String %s ()'.",
880 method_name);
881 return (-1);
882 }
884 string_obj = (*jvm_env)->CallObjectMethod(jvm_env, object_ptr, method_id);
885 if ((string_obj == NULL) && (empty_okay == 0)) {
886 ERROR("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
887 method_name);
888 return (-1);
889 } else if ((string_obj == NULL) && (empty_okay != 0)) {
890 memset(buffer, 0, buffer_size);
891 return (0);
892 }
894 c_str = (*jvm_env)->GetStringUTFChars(jvm_env, string_obj, 0);
895 if (c_str == NULL) {
896 ERROR("java plugin: jtoc_string: GetStringUTFChars failed.");
897 (*jvm_env)->DeleteLocalRef(jvm_env, string_obj);
898 return (-1);
899 }
901 sstrncpy(buffer, c_str, buffer_size);
903 (*jvm_env)->ReleaseStringUTFChars(jvm_env, string_obj, c_str);
904 (*jvm_env)->DeleteLocalRef(jvm_env, string_obj);
906 return (0);
907 } /* }}} int jtoc_string */
909 /* Call an `int <method> ()' method. */
910 static int jtoc_int(JNIEnv *jvm_env, /* {{{ */
911 jint *ret_value, jclass class_ptr, jobject object_ptr,
912 const char *method_name) {
913 jmethodID method_id;
915 method_id = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "()I");
916 if (method_id == NULL) {
917 ERROR("java plugin: jtoc_int: Cannot find method `int %s ()'.",
918 method_name);
919 return (-1);
920 }
922 *ret_value = (*jvm_env)->CallIntMethod(jvm_env, object_ptr, method_id);
924 return (0);
925 } /* }}} int jtoc_int */
927 /* Call a `long <method> ()' method. */
928 static int jtoc_long(JNIEnv *jvm_env, /* {{{ */
929 jlong *ret_value, jclass class_ptr, jobject object_ptr,
930 const char *method_name) {
931 jmethodID method_id;
933 method_id = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "()J");
934 if (method_id == NULL) {
935 ERROR("java plugin: jtoc_long: Cannot find method `long %s ()'.",
936 method_name);
937 return (-1);
938 }
940 *ret_value = (*jvm_env)->CallLongMethod(jvm_env, object_ptr, method_id);
942 return (0);
943 } /* }}} int jtoc_long */
945 /* Call a `double <method> ()' method. */
946 static int jtoc_double(JNIEnv *jvm_env, /* {{{ */
947 jdouble *ret_value, jclass class_ptr, jobject object_ptr,
948 const char *method_name) {
949 jmethodID method_id;
951 method_id = (*jvm_env)->GetMethodID(jvm_env, class_ptr, method_name, "()D");
952 if (method_id == NULL) {
953 ERROR("java plugin: jtoc_double: Cannot find method `double %s ()'.",
954 method_name);
955 return (-1);
956 }
958 *ret_value = (*jvm_env)->CallDoubleMethod(jvm_env, object_ptr, method_id);
960 return (0);
961 } /* }}} int jtoc_double */
963 static int jtoc_value(JNIEnv *jvm_env, /* {{{ */
964 value_t *ret_value, int ds_type, jobject object_ptr) {
965 jclass class_ptr;
966 int status;
968 class_ptr = (*jvm_env)->GetObjectClass(jvm_env, object_ptr);
970 if (ds_type == DS_TYPE_GAUGE) {
971 jdouble tmp_double;
973 status =
974 jtoc_double(jvm_env, &tmp_double, class_ptr, object_ptr, "doubleValue");
975 if (status != 0) {
976 ERROR("java plugin: jtoc_value: "
977 "jtoc_double failed.");
978 return (-1);
979 }
980 (*ret_value).gauge = (gauge_t)tmp_double;
981 } else {
982 jlong tmp_long;
984 status = jtoc_long(jvm_env, &tmp_long, class_ptr, object_ptr, "longValue");
985 if (status != 0) {
986 ERROR("java plugin: jtoc_value: "
987 "jtoc_long failed.");
988 return (-1);
989 }
991 if (ds_type == DS_TYPE_DERIVE)
992 (*ret_value).derive = (derive_t)tmp_long;
993 else if (ds_type == DS_TYPE_ABSOLUTE)
994 (*ret_value).absolute = (absolute_t)tmp_long;
995 else
996 (*ret_value).counter = (counter_t)tmp_long;
997 }
999 return (0);
1000 } /* }}} int jtoc_value */
1002 /* Read a List<Number>, convert it to `value_t' and add it to the given
1003 * `value_list_t'. */
1004 static int jtoc_values_array(JNIEnv *jvm_env, /* {{{ */
1005 const data_set_t *ds, value_list_t *vl,
1006 jclass class_ptr, jobject object_ptr) {
1007 jmethodID m_getvalues;
1008 jmethodID m_toarray;
1009 jobject o_list;
1010 jobjectArray o_number_array;
1012 value_t *values;
1013 int values_num;
1015 values_num = ds->ds_num;
1017 values = NULL;
1018 o_number_array = NULL;
1019 o_list = NULL;
1021 #define BAIL_OUT(status) \
1022 free(values); \
1023 if (o_number_array != NULL) \
1024 (*jvm_env)->DeleteLocalRef(jvm_env, o_number_array); \
1025 if (o_list != NULL) \
1026 (*jvm_env)->DeleteLocalRef(jvm_env, o_list); \
1027 return (status);
1029 /* Call: List<Number> ValueList.getValues () */
1030 m_getvalues = (*jvm_env)->GetMethodID(jvm_env, class_ptr, "getValues",
1031 "()Ljava/util/List;");
1032 if (m_getvalues == NULL) {
1033 ERROR("java plugin: jtoc_values_array: "
1034 "Cannot find method `List getValues ()'.");
1035 BAIL_OUT(-1);
1036 }
1038 o_list = (*jvm_env)->CallObjectMethod(jvm_env, object_ptr, m_getvalues);
1039 if (o_list == NULL) {
1040 ERROR("java plugin: jtoc_values_array: "
1041 "CallObjectMethod (getValues) failed.");
1042 BAIL_OUT(-1);
1043 }
1045 /* Call: Number[] List.toArray () */
1046 m_toarray = (*jvm_env)->GetMethodID(
1047 jvm_env, (*jvm_env)->GetObjectClass(jvm_env, o_list), "toArray",
1048 "()[Ljava/lang/Object;");
1049 if (m_toarray == NULL) {
1050 ERROR("java plugin: jtoc_values_array: "
1051 "Cannot find method `Object[] toArray ()'.");
1052 BAIL_OUT(-1);
1053 }
1055 o_number_array = (*jvm_env)->CallObjectMethod(jvm_env, o_list, m_toarray);
1056 if (o_number_array == NULL) {
1057 ERROR("java plugin: jtoc_values_array: "
1058 "CallObjectMethod (toArray) failed.");
1059 BAIL_OUT(-1);
1060 }
1062 values = (value_t *)calloc(values_num, sizeof(value_t));
1063 if (values == NULL) {
1064 ERROR("java plugin: jtoc_values_array: calloc failed.");
1065 BAIL_OUT(-1);
1066 }
1068 for (int i = 0; i < values_num; i++) {
1069 jobject o_number;
1070 int status;
1072 o_number =
1073 (*jvm_env)->GetObjectArrayElement(jvm_env, o_number_array, (jsize)i);
1074 if (o_number == NULL) {
1075 ERROR("java plugin: jtoc_values_array: "
1076 "GetObjectArrayElement (%i) failed.",
1077 i);
1078 BAIL_OUT(-1);
1079 }
1081 status = jtoc_value(jvm_env, values + i, ds->ds[i].type, o_number);
1082 if (status != 0) {
1083 ERROR("java plugin: jtoc_values_array: "
1084 "jtoc_value (%i) failed.",
1085 i);
1086 BAIL_OUT(-1);
1087 }
1088 } /* for (i = 0; i < values_num; i++) */
1090 vl->values = values;
1091 vl->values_len = values_num;
1093 #undef BAIL_OUT
1094 (*jvm_env)->DeleteLocalRef(jvm_env, o_number_array);
1095 (*jvm_env)->DeleteLocalRef(jvm_env, o_list);
1096 return (0);
1097 } /* }}} int jtoc_values_array */
1099 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1100 static int jtoc_value_list(JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1101 jobject object_ptr) {
1102 jclass class_ptr;
1103 int status;
1104 jlong tmp_long;
1105 const data_set_t *ds;
1107 class_ptr = (*jvm_env)->GetObjectClass(jvm_env, object_ptr);
1108 if (class_ptr == NULL) {
1109 ERROR("java plugin: jtoc_value_list: GetObjectClass failed.");
1110 return (-1);
1111 }
1113 /* eo == empty okay */
1114 #define SET_STRING(buffer, method, eo) \
1115 do { \
1116 status = jtoc_string(jvm_env, buffer, sizeof(buffer), eo, class_ptr, \
1117 object_ptr, method); \
1118 if (status != 0) { \
1119 ERROR("java plugin: jtoc_value_list: jtoc_string (%s) failed.", method); \
1120 return (-1); \
1121 } \
1122 } while (0)
1124 SET_STRING(vl->type, "getType", /* empty = */ 0);
1126 ds = plugin_get_ds(vl->type);
1127 if (ds == NULL) {
1128 ERROR("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1129 "Please consult the types.db(5) manpage for mor information.",
1130 vl->type);
1131 return (-1);
1132 }
1134 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1135 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1136 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1137 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1139 #undef SET_STRING
1141 status = jtoc_long(jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1142 if (status != 0) {
1143 ERROR("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1144 return (-1);
1145 }
1146 /* Java measures time in milliseconds. */
1147 vl->time = MS_TO_CDTIME_T(tmp_long);
1149 status = jtoc_long(jvm_env, &tmp_long, class_ptr, object_ptr, "getInterval");
1150 if (status != 0) {
1151 ERROR("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1152 return (-1);
1153 }
1154 vl->interval = MS_TO_CDTIME_T(tmp_long);
1156 status = jtoc_values_array(jvm_env, ds, vl, class_ptr, object_ptr);
1157 if (status != 0) {
1158 ERROR("java plugin: jtoc_value_list: jtoc_values_array failed.");
1159 return (-1);
1160 }
1162 return (0);
1163 } /* }}} int jtoc_value_list */
1165 /* Convert a org/collectd/api/Notification to a notification_t. */
1166 static int jtoc_notification(JNIEnv *jvm_env, notification_t *n, /* {{{ */
1167 jobject object_ptr) {
1168 jclass class_ptr;
1169 int status;
1170 jlong tmp_long;
1171 jint tmp_int;
1173 class_ptr = (*jvm_env)->GetObjectClass(jvm_env, object_ptr);
1174 if (class_ptr == NULL) {
1175 ERROR("java plugin: jtoc_notification: GetObjectClass failed.");
1176 return (-1);
1177 }
1179 /* eo == empty okay */
1180 #define SET_STRING(buffer, method, eo) \
1181 do { \
1182 status = jtoc_string(jvm_env, buffer, sizeof(buffer), eo, class_ptr, \
1183 object_ptr, method); \
1184 if (status != 0) { \
1185 ERROR("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1186 method); \
1187 return (-1); \
1188 } \
1189 } while (0)
1191 SET_STRING(n->host, "getHost", /* empty = */ 1);
1192 SET_STRING(n->plugin, "getPlugin", /* empty = */ 1);
1193 SET_STRING(n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1194 SET_STRING(n->type, "getType", /* empty = */ 1);
1195 SET_STRING(n->type_instance, "getTypeInstance", /* empty = */ 1);
1196 SET_STRING(n->message, "getMessage", /* empty = */ 0);
1198 #undef SET_STRING
1200 status = jtoc_long(jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1201 if (status != 0) {
1202 ERROR("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1203 return (-1);
1204 }
1205 /* Java measures time in milliseconds. */
1206 n->time = MS_TO_CDTIME_T(tmp_long);
1208 status = jtoc_int(jvm_env, &tmp_int, class_ptr, object_ptr, "getSeverity");
1209 if (status != 0) {
1210 ERROR("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1211 return (-1);
1212 }
1213 n->severity = (int)tmp_int;
1215 return (0);
1216 } /* }}} int jtoc_notification */
1217 /*
1218 * Functions accessible from Java
1219 */
1220 static jint JNICALL cjni_api_dispatch_values(JNIEnv *jvm_env, /* {{{ */
1221 jobject this, jobject java_vl) {
1222 value_list_t vl = VALUE_LIST_INIT;
1223 int status;
1225 DEBUG("cjni_api_dispatch_values: java_vl = %p;", (void *)java_vl);
1227 status = jtoc_value_list(jvm_env, &vl, java_vl);
1228 if (status != 0) {
1229 ERROR("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1230 return (-1);
1231 }
1233 status = plugin_dispatch_values(&vl);
1235 sfree(vl.values);
1237 return (status);
1238 } /* }}} jint cjni_api_dispatch_values */
1240 static jint JNICALL cjni_api_dispatch_notification(JNIEnv *jvm_env, /* {{{ */
1241 jobject this,
1242 jobject o_notification) {
1243 notification_t n = {0};
1244 int status;
1246 status = jtoc_notification(jvm_env, &n, o_notification);
1247 if (status != 0) {
1248 ERROR("java plugin: cjni_api_dispatch_notification: jtoc_notification "
1249 "failed.");
1250 return (-1);
1251 }
1253 status = plugin_dispatch_notification(&n);
1255 return (status);
1256 } /* }}} jint cjni_api_dispatch_notification */
1258 static jobject JNICALL cjni_api_get_ds(JNIEnv *jvm_env, /* {{{ */
1259 jobject this, jobject o_string_type) {
1260 const char *ds_name;
1261 const data_set_t *ds;
1262 jobject o_dataset;
1264 ds_name = (*jvm_env)->GetStringUTFChars(jvm_env, o_string_type, 0);
1265 if (ds_name == NULL) {
1266 ERROR("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1267 return (NULL);
1268 }
1270 ds = plugin_get_ds(ds_name);
1271 DEBUG("java plugin: cjni_api_get_ds: "
1272 "plugin_get_ds (%s) = %p;",
1273 ds_name, (void *)ds);
1275 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_string_type, ds_name);
1277 if (ds == NULL)
1278 return (NULL);
1280 o_dataset = ctoj_data_set(jvm_env, ds);
1281 return (o_dataset);
1282 } /* }}} jint cjni_api_get_ds */
1284 static jint JNICALL cjni_api_register_config(JNIEnv *jvm_env, /* {{{ */
1285 jobject this, jobject o_name,
1286 jobject o_config) {
1287 return (cjni_callback_register(jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1288 } /* }}} jint cjni_api_register_config */
1290 static jint JNICALL cjni_api_register_init(JNIEnv *jvm_env, /* {{{ */
1291 jobject this, jobject o_name,
1292 jobject o_config) {
1293 return (cjni_callback_register(jvm_env, o_name, o_config, CB_TYPE_INIT));
1294 } /* }}} jint cjni_api_register_init */
1296 static jint JNICALL cjni_api_register_read(JNIEnv *jvm_env, /* {{{ */
1297 jobject this, jobject o_name,
1298 jobject o_read) {
1299 cjni_callback_info_t *cbi;
1301 cbi = cjni_callback_info_create(jvm_env, o_name, o_read, CB_TYPE_READ);
1302 if (cbi == NULL)
1303 return (-1);
1305 DEBUG("java plugin: Registering new read callback: %s", cbi->name);
1307 user_data_t ud = {.data = cbi, .free_func = cjni_callback_info_destroy};
1309 plugin_register_complex_read(/* group = */ NULL, cbi->name, cjni_read,
1310 /* interval = */ 0, &ud);
1312 (*jvm_env)->DeleteLocalRef(jvm_env, o_read);
1314 return (0);
1315 } /* }}} jint cjni_api_register_read */
1317 static jint JNICALL cjni_api_register_write(JNIEnv *jvm_env, /* {{{ */
1318 jobject this, jobject o_name,
1319 jobject o_write) {
1320 cjni_callback_info_t *cbi;
1322 cbi = cjni_callback_info_create(jvm_env, o_name, o_write, CB_TYPE_WRITE);
1323 if (cbi == NULL)
1324 return (-1);
1326 DEBUG("java plugin: Registering new write callback: %s", cbi->name);
1328 user_data_t ud = {.data = cbi, .free_func = cjni_callback_info_destroy};
1330 plugin_register_write(cbi->name, cjni_write, &ud);
1332 (*jvm_env)->DeleteLocalRef(jvm_env, o_write);
1334 return (0);
1335 } /* }}} jint cjni_api_register_write */
1337 static jint JNICALL cjni_api_register_flush(JNIEnv *jvm_env, /* {{{ */
1338 jobject this, jobject o_name,
1339 jobject o_flush) {
1340 cjni_callback_info_t *cbi;
1342 cbi = cjni_callback_info_create(jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1343 if (cbi == NULL)
1344 return (-1);
1346 DEBUG("java plugin: Registering new flush callback: %s", cbi->name);
1348 user_data_t ud = {.data = cbi, .free_func = cjni_callback_info_destroy};
1350 plugin_register_flush(cbi->name, cjni_flush, &ud);
1352 (*jvm_env)->DeleteLocalRef(jvm_env, o_flush);
1354 return (0);
1355 } /* }}} jint cjni_api_register_flush */
1357 static jint JNICALL cjni_api_register_shutdown(JNIEnv *jvm_env, /* {{{ */
1358 jobject this, jobject o_name,
1359 jobject o_shutdown) {
1360 return (
1361 cjni_callback_register(jvm_env, o_name, o_shutdown, CB_TYPE_SHUTDOWN));
1362 } /* }}} jint cjni_api_register_shutdown */
1364 static jint JNICALL cjni_api_register_log(JNIEnv *jvm_env, /* {{{ */
1365 jobject this, jobject o_name,
1366 jobject o_log) {
1367 cjni_callback_info_t *cbi;
1369 cbi = cjni_callback_info_create(jvm_env, o_name, o_log, CB_TYPE_LOG);
1370 if (cbi == NULL)
1371 return (-1);
1373 DEBUG("java plugin: Registering new log callback: %s", cbi->name);
1375 user_data_t ud = {.data = cbi, .free_func = cjni_callback_info_destroy};
1377 plugin_register_log(cbi->name, cjni_log, &ud);
1379 (*jvm_env)->DeleteLocalRef(jvm_env, o_log);
1381 return (0);
1382 } /* }}} jint cjni_api_register_log */
1384 static jint JNICALL cjni_api_register_notification(JNIEnv *jvm_env, /* {{{ */
1385 jobject this, jobject o_name,
1386 jobject o_notification) {
1387 cjni_callback_info_t *cbi;
1389 cbi = cjni_callback_info_create(jvm_env, o_name, o_notification,
1390 CB_TYPE_NOTIFICATION);
1391 if (cbi == NULL)
1392 return (-1);
1394 DEBUG("java plugin: Registering new notification callback: %s", cbi->name);
1396 user_data_t ud = {.data = cbi, .free_func = cjni_callback_info_destroy};
1398 plugin_register_notification(cbi->name, cjni_notification, &ud);
1400 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification);
1402 return (0);
1403 } /* }}} jint cjni_api_register_notification */
1405 static jint JNICALL cjni_api_register_match_target(JNIEnv *jvm_env, /* {{{ */
1406 jobject this, jobject o_name,
1407 jobject o_match, int type) {
1408 int status;
1409 const char *c_name;
1411 c_name = (*jvm_env)->GetStringUTFChars(jvm_env, o_name, 0);
1412 if (c_name == NULL) {
1413 ERROR("java plugin: cjni_api_register_match_target: "
1414 "GetStringUTFChars failed.");
1415 return (-1);
1416 }
1418 status = cjni_callback_register(jvm_env, o_name, o_match, type);
1419 if (status != 0) {
1420 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1421 return (-1);
1422 }
1424 if (type == CB_TYPE_MATCH) {
1425 match_proc_t m_proc = {0};
1427 m_proc.create = cjni_match_target_create;
1428 m_proc.destroy = cjni_match_target_destroy;
1429 m_proc.match = (void *)cjni_match_target_invoke;
1431 status = fc_register_match(c_name, m_proc);
1432 } else if (type == CB_TYPE_TARGET) {
1433 target_proc_t t_proc = {0};
1435 t_proc.create = cjni_match_target_create;
1436 t_proc.destroy = cjni_match_target_destroy;
1437 t_proc.invoke = cjni_match_target_invoke;
1439 status = fc_register_target(c_name, t_proc);
1440 } else {
1441 ERROR("java plugin: cjni_api_register_match_target: "
1442 "Don't know whether to create a match or a target.");
1443 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1444 return (-1);
1445 }
1447 if (status != 0) {
1448 ERROR("java plugin: cjni_api_register_match_target: "
1449 "%s failed.",
1450 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1451 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1452 return (-1);
1453 }
1455 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1457 return (0);
1458 } /* }}} jint cjni_api_register_match_target */
1460 static jint JNICALL cjni_api_register_match(JNIEnv *jvm_env, /* {{{ */
1461 jobject this, jobject o_name,
1462 jobject o_match) {
1463 return (cjni_api_register_match_target(jvm_env, this, o_name, o_match,
1464 CB_TYPE_MATCH));
1465 } /* }}} jint cjni_api_register_match */
1467 static jint JNICALL cjni_api_register_target(JNIEnv *jvm_env, /* {{{ */
1468 jobject this, jobject o_name,
1469 jobject o_target) {
1470 return (cjni_api_register_match_target(jvm_env, this, o_name, o_target,
1471 CB_TYPE_TARGET));
1472 } /* }}} jint cjni_api_register_target */
1474 static void JNICALL cjni_api_log(JNIEnv *jvm_env, /* {{{ */
1475 jobject this, jint severity,
1476 jobject o_message) {
1477 const char *c_str;
1479 c_str = (*jvm_env)->GetStringUTFChars(jvm_env, o_message, 0);
1480 if (c_str == NULL) {
1481 ERROR("java plugin: cjni_api_log: GetStringUTFChars failed.");
1482 return;
1483 }
1485 if (severity < LOG_ERR)
1486 severity = LOG_ERR;
1487 if (severity > LOG_DEBUG)
1488 severity = LOG_DEBUG;
1490 plugin_log(severity, "%s", c_str);
1492 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_message, c_str);
1493 } /* }}} void cjni_api_log */
1495 static jstring JNICALL cjni_api_get_hostname(JNIEnv *jvm_env, jobject this) {
1496 return ctoj_output_string(jvm_env, hostname_g);
1497 }
1499 /* List of ``native'' functions, i. e. C-functions that can be called from
1500 * Java. */
1501 static JNINativeMethod jni_api_functions[] = /* {{{ */
1502 {
1503 {"dispatchValues", "(Lorg/collectd/api/ValueList;)I",
1504 cjni_api_dispatch_values},
1506 {"dispatchNotification", "(Lorg/collectd/api/Notification;)I",
1507 cjni_api_dispatch_notification},
1509 {"getDS", "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1510 cjni_api_get_ds},
1512 {"registerConfig",
1513 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1514 cjni_api_register_config},
1516 {"registerInit",
1517 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1518 cjni_api_register_init},
1520 {"registerRead",
1521 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1522 cjni_api_register_read},
1524 {"registerWrite",
1525 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1526 cjni_api_register_write},
1528 {"registerFlush",
1529 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1530 cjni_api_register_flush},
1532 {"registerShutdown",
1533 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1534 cjni_api_register_shutdown},
1536 {"registerLog",
1537 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1538 cjni_api_register_log},
1540 {"registerNotification", "(Ljava/lang/String;Lorg/collectd/api/"
1541 "CollectdNotificationInterface;)I",
1542 cjni_api_register_notification},
1544 {"registerMatch", "(Ljava/lang/String;Lorg/collectd/api/"
1545 "CollectdMatchFactoryInterface;)I",
1546 cjni_api_register_match},
1548 {"registerTarget", "(Ljava/lang/String;Lorg/collectd/api/"
1549 "CollectdTargetFactoryInterface;)I",
1550 cjni_api_register_target},
1552 {"log", "(ILjava/lang/String;)V", cjni_api_log},
1554 {"getHostname", "()Ljava/lang/String;", cjni_api_get_hostname},
1556 };
1557 static size_t jni_api_functions_num =
1558 sizeof(jni_api_functions) / sizeof(jni_api_functions[0]);
1559 /* }}} */
1561 /*
1562 * Functions
1563 */
1564 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1565 * all registration functions. */
1566 static cjni_callback_info_t *
1567 cjni_callback_info_create(JNIEnv *jvm_env, /* {{{ */
1568 jobject o_name, jobject o_callback, int type) {
1569 const char *c_name;
1570 cjni_callback_info_t *cbi;
1571 const char *method_name;
1572 const char *method_signature;
1574 switch (type) {
1575 case CB_TYPE_CONFIG:
1576 method_name = "config";
1577 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1578 break;
1580 case CB_TYPE_INIT:
1581 method_name = "init";
1582 method_signature = "()I";
1583 break;
1585 case CB_TYPE_READ:
1586 method_name = "read";
1587 method_signature = "()I";
1588 break;
1590 case CB_TYPE_WRITE:
1591 method_name = "write";
1592 method_signature = "(Lorg/collectd/api/ValueList;)I";
1593 break;
1595 case CB_TYPE_FLUSH:
1596 method_name = "flush";
1597 method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I";
1598 break;
1600 case CB_TYPE_SHUTDOWN:
1601 method_name = "shutdown";
1602 method_signature = "()I";
1603 break;
1605 case CB_TYPE_LOG:
1606 method_name = "log";
1607 method_signature = "(ILjava/lang/String;)V";
1608 break;
1610 case CB_TYPE_NOTIFICATION:
1611 method_name = "notification";
1612 method_signature = "(Lorg/collectd/api/Notification;)I";
1613 break;
1615 case CB_TYPE_MATCH:
1616 method_name = "createMatch";
1617 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1618 "Lorg/collectd/api/CollectdMatchInterface;";
1619 break;
1621 case CB_TYPE_TARGET:
1622 method_name = "createTarget";
1623 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1624 "Lorg/collectd/api/CollectdTargetInterface;";
1625 break;
1627 default:
1628 ERROR("java plugin: cjni_callback_info_create: Unknown type: %#x", type);
1629 return (NULL);
1630 }
1632 c_name = (*jvm_env)->GetStringUTFChars(jvm_env, o_name, 0);
1633 if (c_name == NULL) {
1634 ERROR("java plugin: cjni_callback_info_create: "
1635 "GetStringUTFChars failed.");
1636 return (NULL);
1637 }
1639 cbi = calloc(1, sizeof(*cbi));
1640 if (cbi == NULL) {
1641 ERROR("java plugin: cjni_callback_info_create: calloc failed.");
1642 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1643 return (NULL);
1644 }
1645 cbi->type = type;
1647 cbi->name = strdup(c_name);
1648 if (cbi->name == NULL) {
1649 pthread_mutex_unlock(&java_callbacks_lock);
1650 ERROR("java plugin: cjni_callback_info_create: strdup failed.");
1651 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1652 sfree(cbi);
1653 return (NULL);
1654 }
1656 (*jvm_env)->ReleaseStringUTFChars(jvm_env, o_name, c_name);
1658 cbi->object = (*jvm_env)->NewGlobalRef(jvm_env, o_callback);
1659 if (cbi->object == NULL) {
1660 ERROR("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1661 sfree(cbi->name);
1662 sfree(cbi);
1663 return (NULL);
1664 }
1666 cbi->class = (*jvm_env)->GetObjectClass(jvm_env, cbi->object);
1667 if (cbi->class == NULL) {
1668 ERROR("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1669 (*jvm_env)->DeleteGlobalRef(jvm_env, cbi->object);
1670 sfree(cbi->name);
1671 sfree(cbi);
1672 return (NULL);
1673 }
1675 cbi->method = (*jvm_env)->GetMethodID(jvm_env, cbi->class, method_name,
1676 method_signature);
1677 if (cbi->method == NULL) {
1678 ERROR("java plugin: cjni_callback_info_create: "
1679 "Cannot find the `%s' method with signature `%s'.",
1680 method_name, method_signature);
1681 (*jvm_env)->DeleteGlobalRef(jvm_env, cbi->object);
1682 sfree(cbi->name);
1683 sfree(cbi);
1684 return (NULL);
1685 }
1687 return (cbi);
1688 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1690 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1691 * to the global `java_callbacks' variable. This is used for `config', `init',
1692 * and `shutdown' callbacks. */
1693 static int cjni_callback_register(JNIEnv *jvm_env, /* {{{ */
1694 jobject o_name, jobject o_callback,
1695 int type) {
1696 cjni_callback_info_t *cbi;
1697 cjni_callback_info_t *tmp;
1698 #if COLLECT_DEBUG
1699 const char *type_str;
1700 #endif
1702 cbi = cjni_callback_info_create(jvm_env, o_name, o_callback, type);
1703 if (cbi == NULL)
1704 return (-1);
1706 #if COLLECT_DEBUG
1707 switch (type) {
1708 case CB_TYPE_CONFIG:
1709 type_str = "config";
1710 break;
1712 case CB_TYPE_INIT:
1713 type_str = "init";
1714 break;
1716 case CB_TYPE_SHUTDOWN:
1717 type_str = "shutdown";
1718 break;
1720 case CB_TYPE_MATCH:
1721 type_str = "match";
1722 break;
1724 case CB_TYPE_TARGET:
1725 type_str = "target";
1726 break;
1728 default:
1729 type_str = "<unknown>";
1730 }
1731 DEBUG("java plugin: Registering new %s callback: %s", type_str, cbi->name);
1732 #endif
1734 pthread_mutex_lock(&java_callbacks_lock);
1736 tmp = realloc(java_callbacks,
1737 (java_callbacks_num + 1) * sizeof(*java_callbacks));
1738 if (tmp == NULL) {
1739 pthread_mutex_unlock(&java_callbacks_lock);
1740 ERROR("java plugin: cjni_callback_register: realloc failed.");
1742 (*jvm_env)->DeleteGlobalRef(jvm_env, cbi->object);
1743 free(cbi);
1745 return (-1);
1746 }
1747 java_callbacks = tmp;
1748 java_callbacks[java_callbacks_num] = *cbi;
1749 java_callbacks_num++;
1751 pthread_mutex_unlock(&java_callbacks_lock);
1753 free(cbi);
1754 return (0);
1755 } /* }}} int cjni_callback_register */
1757 /* Callback for `pthread_key_create'. It frees the data contained in
1758 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1759 static void cjni_jvm_env_destroy(void *args) /* {{{ */
1760 {
1761 cjni_jvm_env_t *cjni_env;
1763 if (args == NULL)
1764 return;
1766 cjni_env = (cjni_jvm_env_t *)args;
1768 if (cjni_env->reference_counter > 0) {
1769 ERROR("java plugin: cjni_jvm_env_destroy: "
1770 "cjni_env->reference_counter = %i;",
1771 cjni_env->reference_counter);
1772 }
1774 if (cjni_env->jvm_env != NULL) {
1775 ERROR("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1776 (void *)cjni_env->jvm_env);
1777 }
1779 /* The pointer is allocated in `cjni_thread_attach' */
1780 free(cjni_env);
1781 } /* }}} void cjni_jvm_env_destroy */
1783 /* Register ``native'' functions with the JVM. Native functions are C-functions
1784 * that can be called by Java code. */
1785 static int cjni_init_native(JNIEnv *jvm_env) /* {{{ */
1786 {
1787 jclass api_class_ptr;
1788 int status;
1790 api_class_ptr = (*jvm_env)->FindClass(jvm_env, "org/collectd/api/Collectd");
1791 if (api_class_ptr == NULL) {
1792 ERROR("cjni_init_native: Cannot find the API class \"org.collectd.api"
1793 ".Collectd\". Please set the correct class path "
1794 "using 'JVMArg \"-Djava.class.path=...\"'.");
1795 return (-1);
1796 }
1798 status = (*jvm_env)->RegisterNatives(
1799 jvm_env, api_class_ptr, jni_api_functions, (jint)jni_api_functions_num);
1800 if (status != 0) {
1801 ERROR("cjni_init_native: RegisterNatives failed with status %i.", status);
1802 return (-1);
1803 }
1805 return (0);
1806 } /* }}} int cjni_init_native */
1808 /* Create the JVM. This is called when the first thread tries to access the JVM
1809 * via cjni_thread_attach. */
1810 static int cjni_create_jvm(void) /* {{{ */
1811 {
1812 JNIEnv *jvm_env;
1813 JavaVMInitArgs vm_args = {0};
1814 JavaVMOption vm_options[jvm_argc];
1816 int status;
1818 if (jvm != NULL)
1819 return (0);
1821 status = pthread_key_create(&jvm_env_key, cjni_jvm_env_destroy);
1822 if (status != 0) {
1823 ERROR("java plugin: cjni_create_jvm: pthread_key_create failed "
1824 "with status %i.",
1825 status);
1826 return (-1);
1827 }
1829 jvm_env = NULL;
1831 vm_args.version = JNI_VERSION_1_2;
1832 vm_args.options = vm_options;
1833 vm_args.nOptions = (jint)jvm_argc;
1835 for (size_t i = 0; i < jvm_argc; i++) {
1836 DEBUG("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s", i, jvm_argv[i]);
1837 vm_args.options[i].optionString = jvm_argv[i];
1838 }
1840 status = JNI_CreateJavaVM(&jvm, (void *)&jvm_env, (void *)&vm_args);
1841 if (status != 0) {
1842 ERROR("java plugin: cjni_create_jvm: "
1843 "JNI_CreateJavaVM failed with status %i.",
1844 status);
1845 return (-1);
1846 }
1847 assert(jvm != NULL);
1848 assert(jvm_env != NULL);
1850 /* Call RegisterNatives */
1851 status = cjni_init_native(jvm_env);
1852 if (status != 0) {
1853 ERROR("java plugin: cjni_create_jvm: cjni_init_native failed.");
1854 return (-1);
1855 }
1857 DEBUG("java plugin: The JVM has been created.");
1858 return (0);
1859 } /* }}} int cjni_create_jvm */
1861 /* Increase the reference counter to the JVM for this thread. If it was zero,
1862 * attach the JVM first. */
1863 static JNIEnv *cjni_thread_attach(void) /* {{{ */
1864 {
1865 cjni_jvm_env_t *cjni_env;
1866 JNIEnv *jvm_env;
1868 /* If we're the first thread to access the JVM, we'll have to create it
1869 * first.. */
1870 if (jvm == NULL) {
1871 int status;
1873 status = cjni_create_jvm();
1874 if (status != 0) {
1875 ERROR("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
1876 return (NULL);
1877 }
1878 }
1879 assert(jvm != NULL);
1881 cjni_env = pthread_getspecific(jvm_env_key);
1882 if (cjni_env == NULL) {
1883 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
1884 cjni_env = calloc(1, sizeof(*cjni_env));
1885 if (cjni_env == NULL) {
1886 ERROR("java plugin: cjni_thread_attach: calloc failed.");
1887 return (NULL);
1888 }
1889 cjni_env->reference_counter = 0;
1890 cjni_env->jvm_env = NULL;
1892 pthread_setspecific(jvm_env_key, cjni_env);
1893 }
1895 if (cjni_env->reference_counter > 0) {
1896 cjni_env->reference_counter++;
1897 jvm_env = cjni_env->jvm_env;
1898 } else {
1899 int status;
1900 JavaVMAttachArgs args = {0};
1902 assert(cjni_env->jvm_env == NULL);
1904 args.version = JNI_VERSION_1_2;
1906 status = (*jvm)->AttachCurrentThread(jvm, (void *)&jvm_env, (void *)&args);
1907 if (status != 0) {
1908 ERROR("java plugin: cjni_thread_attach: AttachCurrentThread failed "
1909 "with status %i.",
1910 status);
1911 return (NULL);
1912 }
1914 cjni_env->reference_counter = 1;
1915 cjni_env->jvm_env = jvm_env;
1916 }
1918 DEBUG("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
1919 cjni_env->reference_counter);
1920 assert(jvm_env != NULL);
1921 return (jvm_env);
1922 } /* }}} JNIEnv *cjni_thread_attach */
1924 /* Decrease the reference counter of this thread. If it reaches zero, detach
1925 * from the JVM. */
1926 static int cjni_thread_detach(void) /* {{{ */
1927 {
1928 cjni_jvm_env_t *cjni_env;
1929 int status;
1931 cjni_env = pthread_getspecific(jvm_env_key);
1932 if (cjni_env == NULL) {
1933 ERROR("java plugin: cjni_thread_detach: pthread_getspecific failed.");
1934 return (-1);
1935 }
1937 assert(cjni_env->reference_counter > 0);
1938 assert(cjni_env->jvm_env != NULL);
1940 cjni_env->reference_counter--;
1941 DEBUG("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
1942 cjni_env->reference_counter);
1944 if (cjni_env->reference_counter > 0)
1945 return (0);
1947 status = (*jvm)->DetachCurrentThread(jvm);
1948 if (status != 0) {
1949 ERROR("java plugin: cjni_thread_detach: DetachCurrentThread failed "
1950 "with status %i.",
1951 status);
1952 }
1954 cjni_env->reference_counter = 0;
1955 cjni_env->jvm_env = NULL;
1957 return (0);
1958 } /* }}} int cjni_thread_detach */
1960 static int cjni_config_add_jvm_arg(oconfig_item_t *ci) /* {{{ */
1961 {
1962 char **tmp;
1964 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
1965 WARNING("java plugin: `JVMArg' needs exactly one string argument.");
1966 return (-1);
1967 }
1969 if (jvm != NULL) {
1970 ERROR("java plugin: All `JVMArg' options MUST appear before all "
1971 "`LoadPlugin' options! The JVM is already started and I have to "
1972 "ignore this argument: %s",
1973 ci->values[0].value.string);
1974 return (-1);
1975 }
1977 tmp = realloc(jvm_argv, sizeof(char *) * (jvm_argc + 1));
1978 if (tmp == NULL) {
1979 ERROR("java plugin: realloc failed.");
1980 return (-1);
1981 }
1982 jvm_argv = tmp;
1984 jvm_argv[jvm_argc] = strdup(ci->values[0].value.string);
1985 if (jvm_argv[jvm_argc] == NULL) {
1986 ERROR("java plugin: strdup failed.");
1987 return (-1);
1988 }
1989 jvm_argc++;
1991 return (0);
1992 } /* }}} int cjni_config_add_jvm_arg */
1994 static int cjni_config_load_plugin(oconfig_item_t *ci) /* {{{ */
1995 {
1996 JNIEnv *jvm_env;
1997 java_plugin_class_t *class;
1998 jmethodID constructor_id;
1999 jobject tmp_object;
2001 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2002 WARNING("java plugin: `LoadPlugin' needs exactly one string argument.");
2003 return (-1);
2004 }
2006 jvm_env = cjni_thread_attach();
2007 if (jvm_env == NULL)
2008 return (-1);
2010 class = realloc(java_classes_list,
2011 (java_classes_list_len + 1) * sizeof(*java_classes_list));
2012 if (class == NULL) {
2013 ERROR("java plugin: realloc failed.");
2014 cjni_thread_detach();
2015 return (-1);
2016 }
2017 java_classes_list = class;
2018 class = java_classes_list + java_classes_list_len;
2020 memset(class, 0, sizeof(*class));
2021 class->name = strdup(ci->values[0].value.string);
2022 if (class->name == NULL) {
2023 ERROR("java plugin: strdup failed.");
2024 cjni_thread_detach();
2025 return (-1);
2026 }
2027 class->class = NULL;
2028 class->object = NULL;
2030 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2031 thorough the Java community, but (Sun's) `FindClass' and friends need
2032 slashes. */
2033 for (size_t i = 0; class->name[i] != 0; i++)
2034 if (class->name[i] == '.')
2035 class->name[i] = '/';
2036 }
2038 DEBUG("java plugin: Loading class %s", class->name);
2040 class->class = (*jvm_env)->FindClass(jvm_env, class->name);
2041 if (class->class == NULL) {
2042 ERROR("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2043 class->name);
2044 cjni_thread_detach();
2045 free(class->name);
2046 return (-1);
2047 }
2049 constructor_id =
2050 (*jvm_env)->GetMethodID(jvm_env, class->class, "<init>", "()V");
2051 if (constructor_id == NULL) {
2052 ERROR("java plugin: cjni_config_load_plugin: "
2053 "Could not find the constructor for `%s'.",
2054 class->name);
2055 cjni_thread_detach();
2056 free(class->name);
2057 return (-1);
2058 }
2060 tmp_object = (*jvm_env)->NewObject(jvm_env, class->class, constructor_id);
2061 if (tmp_object != NULL)
2062 class->object = (*jvm_env)->NewGlobalRef(jvm_env, tmp_object);
2063 else
2064 class->object = NULL;
2065 if (class->object == NULL) {
2066 ERROR("java plugin: cjni_config_load_plugin: "
2067 "Could create a new `%s' object.",
2068 class->name);
2069 cjni_thread_detach();
2070 free(class->name);
2071 return (-1);
2072 }
2074 cjni_thread_detach();
2076 java_classes_list_len++;
2078 return (0);
2079 } /* }}} int cjni_config_load_plugin */
2081 static int cjni_config_plugin_block(oconfig_item_t *ci) /* {{{ */
2082 {
2083 JNIEnv *jvm_env;
2084 cjni_callback_info_t *cbi;
2085 jobject o_ocitem;
2086 const char *name;
2088 jclass class;
2089 jmethodID method;
2091 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
2092 WARNING("java plugin: `Plugin' blocks "
2093 "need exactly one string argument.");
2094 return (-1);
2095 }
2097 name = ci->values[0].value.string;
2099 cbi = NULL;
2100 for (size_t i = 0; i < java_callbacks_num; i++) {
2101 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2102 continue;
2104 if (strcmp(name, java_callbacks[i].name) != 0)
2105 continue;
2107 cbi = java_callbacks + i;
2108 break;
2109 }
2111 if (cbi == NULL) {
2112 NOTICE("java plugin: Configuration block for `%s' found, but no such "
2113 "configuration callback has been registered. Please make sure, the "
2114 "`LoadPlugin' lines precede the `Plugin' blocks.",
2115 name);
2116 return (0);
2117 }
2119 DEBUG("java plugin: Configuring %s", name);
2121 jvm_env = cjni_thread_attach();
2122 if (jvm_env == NULL)
2123 return (-1);
2125 o_ocitem = ctoj_oconfig_item(jvm_env, ci);
2126 if (o_ocitem == NULL) {
2127 ERROR("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2128 cjni_thread_detach();
2129 return (-1);
2130 }
2132 class = (*jvm_env)->GetObjectClass(jvm_env, cbi->object);
2133 method = (*jvm_env)->GetMethodID(jvm_env, class, "config",
2134 "(Lorg/collectd/api/OConfigItem;)I");
2136 (*jvm_env)->CallIntMethod(jvm_env, cbi->object, method, o_ocitem);
2138 (*jvm_env)->DeleteLocalRef(jvm_env, o_ocitem);
2139 cjni_thread_detach();
2140 return (0);
2141 } /* }}} int cjni_config_plugin_block */
2143 static int cjni_config_perform(oconfig_item_t *ci) /* {{{ */
2144 {
2145 int success;
2146 int errors;
2147 int status;
2149 success = 0;
2150 errors = 0;
2152 for (int i = 0; i < ci->children_num; i++) {
2153 oconfig_item_t *child = ci->children + i;
2155 if (strcasecmp("JVMArg", child->key) == 0) {
2156 status = cjni_config_add_jvm_arg(child);
2157 if (status == 0)
2158 success++;
2159 else
2160 errors++;
2161 } else if (strcasecmp("LoadPlugin", child->key) == 0) {
2162 status = cjni_config_load_plugin(child);
2163 if (status == 0)
2164 success++;
2165 else
2166 errors++;
2167 } else if (strcasecmp("Plugin", child->key) == 0) {
2168 status = cjni_config_plugin_block(child);
2169 if (status == 0)
2170 success++;
2171 else
2172 errors++;
2173 } else {
2174 WARNING("java plugin: Option `%s' not allowed here.", child->key);
2175 errors++;
2176 }
2177 }
2179 DEBUG("java plugin: jvm_argc = %zu;", jvm_argc);
2180 DEBUG("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2182 if ((success == 0) && (errors > 0)) {
2183 ERROR("java plugin: All statements failed.");
2184 return (-1);
2185 }
2187 return (0);
2188 } /* }}} int cjni_config_perform */
2190 /* Copy the children of `ci' to the global `config_block' variable. */
2191 static int cjni_config_callback(oconfig_item_t *ci) /* {{{ */
2192 {
2193 oconfig_item_t *ci_copy;
2194 oconfig_item_t *tmp;
2196 assert(ci != NULL);
2197 if (ci->children_num == 0)
2198 return (0); /* nothing to do */
2200 ci_copy = oconfig_clone(ci);
2201 if (ci_copy == NULL) {
2202 ERROR("java plugin: oconfig_clone failed.");
2203 return (-1);
2204 }
2206 if (config_block == NULL) {
2207 config_block = ci_copy;
2208 return (0);
2209 }
2211 tmp = realloc(config_block->children,
2212 (config_block->children_num + ci_copy->children_num) *
2213 sizeof(*tmp));
2214 if (tmp == NULL) {
2215 ERROR("java plugin: realloc failed.");
2216 oconfig_free(ci_copy);
2217 return (-1);
2218 }
2219 config_block->children = tmp;
2221 /* Copy the pointers */
2222 memcpy(config_block->children + config_block->children_num, ci_copy->children,
2223 ci_copy->children_num * sizeof(*ci_copy->children));
2224 config_block->children_num += ci_copy->children_num;
2226 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2227 memset(ci_copy->children, 0,
2228 ci_copy->children_num * sizeof(*ci_copy->children));
2229 ci_copy->children_num = 0;
2231 oconfig_free(ci_copy);
2233 return (0);
2234 } /* }}} int cjni_config_callback */
2236 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2237 * and `cjni_write'. In particular, delete the global reference to the Java
2238 * object. */
2239 static void cjni_callback_info_destroy(void *arg) /* {{{ */
2240 {
2241 JNIEnv *jvm_env;
2242 cjni_callback_info_t *cbi;
2244 DEBUG("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2246 cbi = (cjni_callback_info_t *)arg;
2248 /* This condition can occur when shutting down. */
2249 if (jvm == NULL) {
2250 sfree(cbi);
2251 return;
2252 }
2254 if (arg == NULL)
2255 return;
2257 jvm_env = cjni_thread_attach();
2258 if (jvm_env == NULL) {
2259 ERROR(
2260 "java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2261 return;
2262 }
2264 (*jvm_env)->DeleteGlobalRef(jvm_env, cbi->object);
2266 cbi->method = NULL;
2267 cbi->object = NULL;
2268 cbi->class = NULL;
2269 free(cbi);
2271 cjni_thread_detach();
2272 } /* }}} void cjni_callback_info_destroy */
2274 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2275 static int cjni_read(user_data_t *ud) /* {{{ */
2276 {
2277 JNIEnv *jvm_env;
2278 cjni_callback_info_t *cbi;
2279 int ret_status;
2281 if (jvm == NULL) {
2282 ERROR("java plugin: cjni_read: jvm == NULL");
2283 return (-1);
2284 }
2286 if ((ud == NULL) || (ud->data == NULL)) {
2287 ERROR("java plugin: cjni_read: Invalid user data.");
2288 return (-1);
2289 }
2291 jvm_env = cjni_thread_attach();
2292 if (jvm_env == NULL)
2293 return (-1);
2295 cbi = (cjni_callback_info_t *)ud->data;
2297 ret_status = (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method);
2299 cjni_thread_detach();
2300 return (ret_status);
2301 } /* }}} int cjni_read */
2303 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2304 static int cjni_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */
2305 user_data_t *ud) {
2306 JNIEnv *jvm_env;
2307 cjni_callback_info_t *cbi;
2308 jobject vl_java;
2309 int ret_status;
2311 if (jvm == NULL) {
2312 ERROR("java plugin: cjni_write: jvm == NULL");
2313 return (-1);
2314 }
2316 if ((ud == NULL) || (ud->data == NULL)) {
2317 ERROR("java plugin: cjni_write: Invalid user data.");
2318 return (-1);
2319 }
2321 jvm_env = cjni_thread_attach();
2322 if (jvm_env == NULL)
2323 return (-1);
2325 cbi = (cjni_callback_info_t *)ud->data;
2327 vl_java = ctoj_value_list(jvm_env, ds, vl);
2328 if (vl_java == NULL) {
2329 ERROR("java plugin: cjni_write: ctoj_value_list failed.");
2330 cjni_thread_detach();
2331 return (-1);
2332 }
2334 ret_status =
2335 (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method, vl_java);
2337 (*jvm_env)->DeleteLocalRef(jvm_env, vl_java);
2339 cjni_thread_detach();
2340 return (ret_status);
2341 } /* }}} int cjni_write */
2343 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2344 static int cjni_flush(cdtime_t timeout, const char *identifier, /* {{{ */
2345 user_data_t *ud) {
2346 JNIEnv *jvm_env;
2347 cjni_callback_info_t *cbi;
2348 jobject o_timeout;
2349 jobject o_identifier;
2350 int ret_status;
2352 if (jvm == NULL) {
2353 ERROR("java plugin: cjni_flush: jvm == NULL");
2354 return (-1);
2355 }
2357 if ((ud == NULL) || (ud->data == NULL)) {
2358 ERROR("java plugin: cjni_flush: Invalid user data.");
2359 return (-1);
2360 }
2362 jvm_env = cjni_thread_attach();
2363 if (jvm_env == NULL)
2364 return (-1);
2366 cbi = (cjni_callback_info_t *)ud->data;
2368 o_timeout =
2369 ctoj_jdouble_to_number(jvm_env, (jdouble)CDTIME_T_TO_DOUBLE(timeout));
2370 if (o_timeout == NULL) {
2371 ERROR("java plugin: cjni_flush: Converting double "
2372 "to Number object failed.");
2373 cjni_thread_detach();
2374 return (-1);
2375 }
2377 o_identifier = NULL;
2378 if (identifier != NULL) {
2379 o_identifier = (*jvm_env)->NewStringUTF(jvm_env, identifier);
2380 if (o_identifier == NULL) {
2381 (*jvm_env)->DeleteLocalRef(jvm_env, o_timeout);
2382 ERROR("java plugin: cjni_flush: NewStringUTF failed.");
2383 cjni_thread_detach();
2384 return (-1);
2385 }
2386 }
2388 ret_status = (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method,
2389 o_timeout, o_identifier);
2391 (*jvm_env)->DeleteLocalRef(jvm_env, o_identifier);
2392 (*jvm_env)->DeleteLocalRef(jvm_env, o_timeout);
2394 cjni_thread_detach();
2395 return (ret_status);
2396 } /* }}} int cjni_flush */
2398 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2399 static void cjni_log(int severity, const char *message, /* {{{ */
2400 user_data_t *ud) {
2401 JNIEnv *jvm_env;
2402 cjni_callback_info_t *cbi;
2403 jobject o_message;
2405 if (jvm == NULL)
2406 return;
2408 if ((ud == NULL) || (ud->data == NULL))
2409 return;
2411 jvm_env = cjni_thread_attach();
2412 if (jvm_env == NULL)
2413 return;
2415 cbi = (cjni_callback_info_t *)ud->data;
2417 o_message = (*jvm_env)->NewStringUTF(jvm_env, message);
2418 if (o_message == NULL) {
2419 cjni_thread_detach();
2420 return;
2421 }
2423 (*jvm_env)->CallVoidMethod(jvm_env, cbi->object, cbi->method, (jint)severity,
2424 o_message);
2426 (*jvm_env)->DeleteLocalRef(jvm_env, o_message);
2428 cjni_thread_detach();
2429 } /* }}} void cjni_log */
2431 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2432 * pointer. */
2433 static int cjni_notification(const notification_t *n, /* {{{ */
2434 user_data_t *ud) {
2435 JNIEnv *jvm_env;
2436 cjni_callback_info_t *cbi;
2437 jobject o_notification;
2438 int ret_status;
2440 if (jvm == NULL) {
2441 ERROR("java plugin: cjni_read: jvm == NULL");
2442 return (-1);
2443 }
2445 if ((ud == NULL) || (ud->data == NULL)) {
2446 ERROR("java plugin: cjni_read: Invalid user data.");
2447 return (-1);
2448 }
2450 jvm_env = cjni_thread_attach();
2451 if (jvm_env == NULL)
2452 return (-1);
2454 cbi = (cjni_callback_info_t *)ud->data;
2456 o_notification = ctoj_notification(jvm_env, n);
2457 if (o_notification == NULL) {
2458 ERROR("java plugin: cjni_notification: ctoj_notification failed.");
2459 cjni_thread_detach();
2460 return (-1);
2461 }
2463 ret_status = (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method,
2464 o_notification);
2466 (*jvm_env)->DeleteLocalRef(jvm_env, o_notification);
2468 cjni_thread_detach();
2469 return (ret_status);
2470 } /* }}} int cjni_notification */
2472 /* Callbacks for matches implemented in Java */
2473 static int cjni_match_target_create(const oconfig_item_t *ci, /* {{{ */
2474 void **user_data) {
2475 JNIEnv *jvm_env;
2476 cjni_callback_info_t *cbi_ret;
2477 cjni_callback_info_t *cbi_factory;
2478 const char *name;
2479 jobject o_ci;
2480 jobject o_tmp;
2481 int type;
2483 cbi_ret = NULL;
2484 o_ci = NULL;
2485 jvm_env = NULL;
2487 #define BAIL_OUT(status) \
2488 if (cbi_ret != NULL) { \
2489 free(cbi_ret->name); \
2490 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2491 (*jvm_env)->DeleteLocalRef(jvm_env, cbi_ret->object); \
2492 } \
2493 free(cbi_ret); \
2494 if (o_ci != NULL) \
2495 (*jvm_env)->DeleteLocalRef(jvm_env, o_ci); \
2496 cjni_thread_detach(); \
2497 return (status)
2499 if (jvm == NULL) {
2500 ERROR("java plugin: cjni_read: jvm == NULL");
2501 return (-1);
2502 }
2504 jvm_env = cjni_thread_attach();
2505 if (jvm_env == NULL)
2506 return (-1);
2508 /* Find out whether to create a match or a target. */
2509 if (strcasecmp("Match", ci->key) == 0)
2510 type = CB_TYPE_MATCH;
2511 else if (strcasecmp("Target", ci->key) == 0)
2512 type = CB_TYPE_TARGET;
2513 else {
2514 ERROR("java plugin: cjni_match_target_create: Can't figure out whether "
2515 "to create a match or a target.");
2516 BAIL_OUT(-1);
2517 }
2519 /* This is the name of the match we should create. */
2520 name = ci->values[0].value.string;
2522 /* Lets see if we have a matching factory here.. */
2523 cbi_factory = NULL;
2524 for (size_t i = 0; i < java_callbacks_num; i++) {
2525 if (java_callbacks[i].type != type)
2526 continue;
2528 if (strcmp(name, java_callbacks[i].name) != 0)
2529 continue;
2531 cbi_factory = java_callbacks + i;
2532 break;
2533 }
2535 /* Nope, no factory for that name.. */
2536 if (cbi_factory == NULL) {
2537 ERROR("java plugin: cjni_match_target_create: "
2538 "No such match factory registered: %s",
2539 name);
2540 BAIL_OUT(-1);
2541 }
2543 /* We convert `ci' to its Java equivalent.. */
2544 o_ci = ctoj_oconfig_item(jvm_env, ci);
2545 if (o_ci == NULL) {
2546 ERROR("java plugin: cjni_match_target_create: "
2547 "ctoj_oconfig_item failed.");
2548 BAIL_OUT(-1);
2549 }
2551 /* Allocate a new callback info structure. This is going to be our user_data
2552 * pointer. */
2553 cbi_ret = calloc(1, sizeof(*cbi_ret));
2554 if (cbi_ret == NULL) {
2555 ERROR("java plugin: cjni_match_target_create: calloc failed.");
2556 BAIL_OUT(-1);
2557 }
2559 cbi_ret->object = NULL;
2560 cbi_ret->type = type;
2562 /* Lets fill the callback info structure.. First, the name: */
2563 cbi_ret->name = strdup(name);
2564 if (cbi_ret->name == NULL) {
2565 ERROR("java plugin: cjni_match_target_create: strdup failed.");
2566 BAIL_OUT(-1);
2567 }
2569 /* Then call the factory method so it creates a new object for us. */
2570 o_tmp = (*jvm_env)->CallObjectMethod(jvm_env, cbi_factory->object,
2571 cbi_factory->method, o_ci);
2572 if (o_tmp == NULL) {
2573 ERROR("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2574 BAIL_OUT(-1);
2575 }
2577 cbi_ret->object = (*jvm_env)->NewGlobalRef(jvm_env, o_tmp);
2578 if (o_tmp == NULL) {
2579 ERROR("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2580 BAIL_OUT(-1);
2581 }
2583 /* This is the class of the match. It is possibly different from the class of
2584 * the match-factory! */
2585 cbi_ret->class = (*jvm_env)->GetObjectClass(jvm_env, cbi_ret->object);
2586 if (cbi_ret->class == NULL) {
2587 ERROR("java plugin: cjni_match_target_create: GetObjectClass failed.");
2588 BAIL_OUT(-1);
2589 }
2591 /* Lookup the `int match (DataSet, ValueList)' method. */
2592 cbi_ret->method = (*jvm_env)->GetMethodID(
2593 jvm_env, cbi_ret->class,
2594 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2595 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2596 if (cbi_ret->method == NULL) {
2597 ERROR("java plugin: cjni_match_target_create: GetMethodID failed.");
2598 BAIL_OUT(-1);
2599 }
2601 /* Return the newly created match via the user_data pointer. */
2602 *user_data = (void *)cbi_ret;
2604 cjni_thread_detach();
2606 DEBUG("java plugin: cjni_match_target_create: "
2607 "Successfully created a `%s' %s.",
2608 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2610 /* Success! */
2611 return (0);
2612 #undef BAIL_OUT
2613 } /* }}} int cjni_match_target_create */
2615 static int cjni_match_target_destroy(void **user_data) /* {{{ */
2616 {
2617 cjni_callback_info_destroy(*user_data);
2618 *user_data = NULL;
2620 return (0);
2621 } /* }}} int cjni_match_target_destroy */
2623 static int cjni_match_target_invoke(const data_set_t *ds, /* {{{ */
2624 value_list_t *vl,
2625 notification_meta_t **meta,
2626 void **user_data) {
2627 JNIEnv *jvm_env;
2628 cjni_callback_info_t *cbi;
2629 jobject o_vl;
2630 jobject o_ds;
2631 int ret_status;
2632 int status;
2634 if (jvm == NULL) {
2635 ERROR("java plugin: cjni_match_target_invoke: jvm == NULL");
2636 return (-1);
2637 }
2639 jvm_env = cjni_thread_attach();
2640 if (jvm_env == NULL)
2641 return (-1);
2643 cbi = (cjni_callback_info_t *)*user_data;
2645 o_vl = ctoj_value_list(jvm_env, ds, vl);
2646 if (o_vl == NULL) {
2647 ERROR("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2648 cjni_thread_detach();
2649 return (-1);
2650 }
2652 o_ds = ctoj_data_set(jvm_env, ds);
2653 if (o_ds == NULL) {
2654 ERROR("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2655 cjni_thread_detach();
2656 return (-1);
2657 }
2659 ret_status =
2660 (*jvm_env)->CallIntMethod(jvm_env, cbi->object, cbi->method, o_ds, o_vl);
2662 DEBUG("java plugin: cjni_match_target_invoke: Method returned %i.",
2663 ret_status);
2665 /* If we're executing a target, copy the `ValueList' back to our
2666 * `value_list_t'. */
2667 if (cbi->type == CB_TYPE_TARGET) {
2668 value_list_t new_vl = {0};
2670 status = jtoc_value_list(jvm_env, &new_vl, o_vl);
2671 if (status != 0) {
2672 ERROR("java plugin: cjni_match_target_invoke: "
2673 "jtoc_value_list failed.");
2674 } else /* if (status == 0) */
2675 {
2676 /* plugin_dispatch_values assures that this is dynamically allocated
2677 * memory. */
2678 sfree(vl->values);
2680 /* This will replace the vl->values pointer to a new, dynamically
2681 * allocated piece of memory. */
2682 memcpy(vl, &new_vl, sizeof(*vl));
2683 }
2684 } /* if (cbi->type == CB_TYPE_TARGET) */
2686 cjni_thread_detach();
2687 return (ret_status);
2688 } /* }}} int cjni_match_target_invoke */
2690 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2691 static int cjni_init_plugins(JNIEnv *jvm_env) /* {{{ */
2692 {
2693 int status;
2695 for (size_t i = 0; i < java_callbacks_num; i++) {
2696 if (java_callbacks[i].type != CB_TYPE_INIT)
2697 continue;
2699 DEBUG("java plugin: Initializing %s", java_callbacks[i].name);
2701 status = (*jvm_env)->CallIntMethod(jvm_env, java_callbacks[i].object,
2702 java_callbacks[i].method);
2703 if (status != 0) {
2704 ERROR("java plugin: Initializing `%s' failed with status %i. "
2705 "Removing read function.",
2706 java_callbacks[i].name, status);
2707 plugin_unregister_read(java_callbacks[i].name);
2708 }
2709 }
2711 return (0);
2712 } /* }}} int cjni_init_plugins */
2714 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2715 static int cjni_shutdown_plugins(JNIEnv *jvm_env) /* {{{ */
2716 {
2717 int status;
2719 for (size_t i = 0; i < java_callbacks_num; i++) {
2720 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2721 continue;
2723 DEBUG("java plugin: Shutting down %s", java_callbacks[i].name);
2725 status = (*jvm_env)->CallIntMethod(jvm_env, java_callbacks[i].object,
2726 java_callbacks[i].method);
2727 if (status != 0) {
2728 ERROR("java plugin: Shutting down `%s' failed with status %i. ",
2729 java_callbacks[i].name, status);
2730 }
2731 }
2733 return (0);
2734 } /* }}} int cjni_shutdown_plugins */
2736 static int cjni_shutdown(void) /* {{{ */
2737 {
2738 JNIEnv *jvm_env;
2739 JavaVMAttachArgs args = {0};
2740 int status;
2742 if (jvm == NULL)
2743 return (0);
2745 jvm_env = NULL;
2746 args.version = JNI_VERSION_1_2;
2748 status = (*jvm)->AttachCurrentThread(jvm, (void *)&jvm_env, &args);
2749 if (status != 0) {
2750 ERROR("java plugin: cjni_shutdown: AttachCurrentThread failed with status "
2751 "%i.",
2752 status);
2753 return (-1);
2754 }
2756 /* Execute all the shutdown functions registered by plugins. */
2757 cjni_shutdown_plugins(jvm_env);
2759 /* Release all the global references to callback functions */
2760 for (size_t i = 0; i < java_callbacks_num; i++) {
2761 if (java_callbacks[i].object != NULL) {
2762 (*jvm_env)->DeleteGlobalRef(jvm_env, java_callbacks[i].object);
2763 java_callbacks[i].object = NULL;
2764 }
2765 sfree(java_callbacks[i].name);
2766 }
2767 java_callbacks_num = 0;
2768 sfree(java_callbacks);
2770 /* Release all the global references to directly loaded classes. */
2771 for (size_t i = 0; i < java_classes_list_len; i++) {
2772 if (java_classes_list[i].object != NULL) {
2773 (*jvm_env)->DeleteGlobalRef(jvm_env, java_classes_list[i].object);
2774 java_classes_list[i].object = NULL;
2775 }
2776 sfree(java_classes_list[i].name);
2777 }
2778 java_classes_list_len = 0;
2779 sfree(java_classes_list);
2781 /* Destroy the JVM */
2782 DEBUG("java plugin: Destroying the JVM.");
2783 (*jvm)->DestroyJavaVM(jvm);
2784 jvm = NULL;
2785 jvm_env = NULL;
2787 pthread_key_delete(jvm_env_key);
2789 /* Free the JVM argument list */
2790 for (size_t i = 0; i < jvm_argc; i++)
2791 sfree(jvm_argv[i]);
2792 jvm_argc = 0;
2793 sfree(jvm_argv);
2795 return (0);
2796 } /* }}} int cjni_shutdown */
2798 /* Initialization: Create a JVM, load all configured classes and call their
2799 * `config' and `init' callback methods. */
2800 static int cjni_init(void) /* {{{ */
2801 {
2802 JNIEnv *jvm_env;
2804 if ((config_block == NULL) && (jvm == NULL)) {
2805 ERROR("java plugin: cjni_init: No configuration block for "
2806 "the java plugin was found.");
2807 return (-1);
2808 }
2810 if (config_block != NULL) {
2811 cjni_config_perform(config_block);
2812 oconfig_free(config_block);
2813 }
2815 if (jvm == NULL) {
2816 ERROR("java plugin: cjni_init: jvm == NULL");
2817 return (-1);
2818 }
2820 jvm_env = cjni_thread_attach();
2821 if (jvm_env == NULL)
2822 return (-1);
2824 cjni_init_plugins(jvm_env);
2826 cjni_thread_detach();
2827 return (0);
2828 } /* }}} int cjni_init */
2830 void module_register(void) {
2831 plugin_register_complex_config("java", cjni_config_callback);
2832 plugin_register_init("java", cjni_init);
2833 plugin_register_shutdown("java", cjni_shutdown);
2834 } /* void module_register (void) */
2836 /* vim: set sw=2 sts=2 et fdm=marker : */