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 "plugin.h"
27 #include "common.h"
28 #include "filter_chain.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, jobject o_callback, int type);
107 static int cjni_callback_register (JNIEnv *jvm_env, jobject o_name,
108 jobject o_callback, int type);
109 static int cjni_read (user_data_t *user_data);
110 static int cjni_write (const data_set_t *ds, const value_list_t *vl,
111 user_data_t *ud);
112 static int cjni_flush (cdtime_t timeout, const char *identifier, user_data_t *ud);
113 static void cjni_log (int severity, const char *message, user_data_t *ud);
114 static int cjni_notification (const notification_t *n, user_data_t *ud);
116 /* Create, destroy, and match/invoke functions, used by both, matches AND
117 * targets. */
118 static int cjni_match_target_create (const oconfig_item_t *ci, void **user_data);
119 static int cjni_match_target_destroy (void **user_data);
120 static int cjni_match_target_invoke (const data_set_t *ds, value_list_t *vl,
121 notification_meta_t **meta, void **user_data);
123 /*
124 * C to Java conversion functions
125 */
126 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
127 const char *string,
128 jclass class_ptr, jobject object_ptr, const char *method_name)
129 {
130 jmethodID m_set;
131 jstring o_string;
133 /* Create a java.lang.String */
134 o_string = (*jvm_env)->NewStringUTF (jvm_env,
135 (string != NULL) ? string : "");
136 if (o_string == NULL)
137 {
138 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
139 return (-1);
140 }
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");
145 if (m_set == NULL)
146 {
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 {
165 jstring o_string;
167 /* Create a java.lang.String */
168 o_string = (*jvm_env)->NewStringUTF (jvm_env,
169 (string != NULL) ? string : "");
170 if (o_string == NULL)
171 {
172 ERROR ("java plugin: ctoj_output_string: NewStringUTF failed.");
173 return NULL;
174 }
176 return (o_string);
177 } /* }}} int ctoj_output_string */
179 static int ctoj_int (JNIEnv *jvm_env, /* {{{ */
180 jint value,
181 jclass class_ptr, jobject object_ptr, const char *method_name)
182 {
183 jmethodID m_set;
185 /* Search for the `void setFoo (int i)' method. */
186 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
187 method_name, "(I)V");
188 if (m_set == NULL)
189 {
190 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
191 method_name);
192 return (-1);
193 }
195 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
197 return (0);
198 } /* }}} int ctoj_int */
200 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
201 jlong value,
202 jclass class_ptr, jobject object_ptr, const char *method_name)
203 {
204 jmethodID m_set;
206 /* Search for the `void setFoo (long l)' method. */
207 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
208 method_name, "(J)V");
209 if (m_set == NULL)
210 {
211 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
212 method_name);
213 return (-1);
214 }
216 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
218 return (0);
219 } /* }}} int ctoj_long */
221 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
222 jdouble value,
223 jclass class_ptr, jobject object_ptr, const char *method_name)
224 {
225 jmethodID m_set;
227 /* Search for the `void setFoo (double d)' method. */
228 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
229 method_name, "(D)V");
230 if (m_set == NULL)
231 {
232 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
233 method_name);
234 return (-1);
235 }
237 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
239 return (0);
240 } /* }}} int ctoj_double */
242 /* Convert a jlong to a java.lang.Number */
243 static jobject ctoj_jlong_to_number (JNIEnv *jvm_env, jlong value) /* {{{ */
244 {
245 jclass c_long;
246 jmethodID m_long_constructor;
248 /* Look up the java.lang.Long class */
249 c_long = (*jvm_env)->FindClass (jvm_env, "java/lang/Long");
250 if (c_long == NULL)
251 {
252 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
253 "java.lang.Long class failed.");
254 return (NULL);
255 }
257 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
258 c_long, "<init>", "(J)V");
259 if (m_long_constructor == NULL)
260 {
261 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
262 "`Long (long)' constructor failed.");
263 return (NULL);
264 }
266 return ((*jvm_env)->NewObject (jvm_env,
267 c_long, m_long_constructor, value));
268 } /* }}} jobject ctoj_jlong_to_number */
270 /* Convert a jdouble to a java.lang.Number */
271 static jobject ctoj_jdouble_to_number (JNIEnv *jvm_env, jdouble value) /* {{{ */
272 {
273 jclass c_double;
274 jmethodID m_double_constructor;
276 /* Look up the java.lang.Long class */
277 c_double = (*jvm_env)->FindClass (jvm_env, "java/lang/Double");
278 if (c_double == NULL)
279 {
280 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
281 "java.lang.Double class failed.");
282 return (NULL);
283 }
285 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
286 c_double, "<init>", "(D)V");
287 if (m_double_constructor == NULL)
288 {
289 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
290 "`Double (double)' constructor failed.");
291 return (NULL);
292 }
294 return ((*jvm_env)->NewObject (jvm_env,
295 c_double, m_double_constructor, value));
296 } /* }}} jobject ctoj_jdouble_to_number */
298 /* Convert a value_t to a java.lang.Number */
299 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
300 value_t value, int ds_type)
301 {
302 if (ds_type == DS_TYPE_COUNTER)
303 return (ctoj_jlong_to_number (jvm_env, (jlong) value.counter));
304 else if (ds_type == DS_TYPE_GAUGE)
305 return (ctoj_jdouble_to_number (jvm_env, (jdouble) value.gauge));
306 if (ds_type == DS_TYPE_DERIVE)
307 return (ctoj_jlong_to_number (jvm_env, (jlong) value.derive));
308 if (ds_type == DS_TYPE_ABSOLUTE)
309 return (ctoj_jlong_to_number (jvm_env, (jlong) value.absolute));
310 else
311 return (NULL);
312 } /* }}} jobject ctoj_value_to_number */
314 /* Convert a data_source_t to a org/collectd/api/DataSource */
315 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
316 const data_source_t *dsrc)
317 {
318 jclass c_datasource;
319 jmethodID m_datasource_constructor;
320 jobject o_datasource;
321 int status;
323 /* Look up the DataSource class */
324 c_datasource = (*jvm_env)->FindClass (jvm_env,
325 "org/collectd/api/DataSource");
326 if (c_datasource == NULL)
327 {
328 ERROR ("java plugin: ctoj_data_source: "
329 "FindClass (org/collectd/api/DataSource) failed.");
330 return (NULL);
331 }
333 /* Lookup the `ValueList ()' constructor. */
334 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
335 "<init>", "()V");
336 if (m_datasource_constructor == NULL)
337 {
338 ERROR ("java plugin: ctoj_data_source: Cannot find the "
339 "`DataSource ()' constructor.");
340 return (NULL);
341 }
343 /* Create a new instance. */
344 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
345 m_datasource_constructor);
346 if (o_datasource == NULL)
347 {
348 ERROR ("java plugin: ctoj_data_source: "
349 "Creating a new DataSource instance failed.");
350 return (NULL);
351 }
353 /* Set name via `void setName (String name)' */
354 status = ctoj_string (jvm_env, dsrc->name,
355 c_datasource, o_datasource, "setName");
356 if (status != 0)
357 {
358 ERROR ("java plugin: ctoj_data_source: "
359 "ctoj_string (setName) failed.");
360 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
361 return (NULL);
362 }
364 /* Set type via `void setType (int type)' */
365 status = ctoj_int (jvm_env, dsrc->type,
366 c_datasource, o_datasource, "setType");
367 if (status != 0)
368 {
369 ERROR ("java plugin: ctoj_data_source: "
370 "ctoj_int (setType) failed.");
371 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
372 return (NULL);
373 }
375 /* Set min via `void setMin (double min)' */
376 status = ctoj_double (jvm_env, dsrc->min,
377 c_datasource, o_datasource, "setMin");
378 if (status != 0)
379 {
380 ERROR ("java plugin: ctoj_data_source: "
381 "ctoj_double (setMin) failed.");
382 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
383 return (NULL);
384 }
386 /* Set max via `void setMax (double max)' */
387 status = ctoj_double (jvm_env, dsrc->max,
388 c_datasource, o_datasource, "setMax");
389 if (status != 0)
390 {
391 ERROR ("java plugin: ctoj_data_source: "
392 "ctoj_double (setMax) failed.");
393 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
394 return (NULL);
395 }
397 return (o_datasource);
398 } /* }}} jobject ctoj_data_source */
400 /* Convert a oconfig_value_t to a org/collectd/api/OConfigValue */
401 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
402 oconfig_value_t ocvalue)
403 {
404 jclass c_ocvalue;
405 jmethodID m_ocvalue_constructor;
406 jobject o_argument;
407 jobject o_ocvalue;
409 m_ocvalue_constructor = NULL;
410 o_argument = NULL;
412 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
413 "org/collectd/api/OConfigValue");
414 if (c_ocvalue == NULL)
415 {
416 ERROR ("java plugin: ctoj_oconfig_value: "
417 "FindClass (org/collectd/api/OConfigValue) failed.");
418 return (NULL);
419 }
421 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
422 {
423 jboolean tmp_boolean;
425 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
427 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
428 "<init>", "(Z)V");
429 if (m_ocvalue_constructor == NULL)
430 {
431 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
432 "`OConfigValue (boolean)' constructor.");
433 return (NULL);
434 }
436 return ((*jvm_env)->NewObject (jvm_env,
437 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
438 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
439 else if (ocvalue.type == OCONFIG_TYPE_STRING)
440 {
441 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
442 "<init>", "(Ljava/lang/String;)V");
443 if (m_ocvalue_constructor == NULL)
444 {
445 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
446 "`OConfigValue (String)' constructor.");
447 return (NULL);
448 }
450 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
451 if (o_argument == NULL)
452 {
453 ERROR ("java plugin: ctoj_oconfig_value: "
454 "Creating a String object failed.");
455 return (NULL);
456 }
457 }
458 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
459 {
460 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
461 "<init>", "(Ljava/lang/Number;)V");
462 if (m_ocvalue_constructor == NULL)
463 {
464 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
465 "`OConfigValue (Number)' constructor.");
466 return (NULL);
467 }
469 o_argument = ctoj_jdouble_to_number (jvm_env,
470 (jdouble) ocvalue.value.number);
471 if (o_argument == NULL)
472 {
473 ERROR ("java plugin: ctoj_oconfig_value: "
474 "Creating a Number object failed.");
475 return (NULL);
476 }
477 }
478 else
479 {
480 return (NULL);
481 }
483 assert (m_ocvalue_constructor != NULL);
484 assert (o_argument != NULL);
486 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
487 c_ocvalue, m_ocvalue_constructor, o_argument);
488 if (o_ocvalue == NULL)
489 {
490 ERROR ("java plugin: ctoj_oconfig_value: "
491 "Creating an OConfigValue object failed.");
492 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
493 return (NULL);
494 }
496 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
497 return (o_ocvalue);
498 } /* }}} jobject ctoj_oconfig_value */
500 /* Convert a oconfig_item_t to a org/collectd/api/OConfigItem */
501 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
502 const oconfig_item_t *ci)
503 {
504 jclass c_ocitem;
505 jmethodID m_ocitem_constructor;
506 jmethodID m_addvalue;
507 jmethodID m_addchild;
508 jobject o_key;
509 jobject o_ocitem;
511 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/OConfigItem");
512 if (c_ocitem == NULL)
513 {
514 ERROR ("java plugin: ctoj_oconfig_item: "
515 "FindClass (org/collectd/api/OConfigItem) failed.");
516 return (NULL);
517 }
519 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
520 * {{{ */
521 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
522 "<init>", "(Ljava/lang/String;)V");
523 if (m_ocitem_constructor == NULL)
524 {
525 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
526 "`OConfigItem (String)' constructor.");
527 return (NULL);
528 }
530 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
531 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
532 if (m_addvalue == NULL)
533 {
534 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
535 "`addValue (OConfigValue)' method.");
536 return (NULL);
537 }
539 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
540 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
541 if (m_addchild == NULL)
542 {
543 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
544 "`addChild (OConfigItem)' method.");
545 return (NULL);
546 }
547 /* }}} */
549 /* Create a String object with the key.
550 * Needed for calling the constructor. */
551 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
552 if (o_key == NULL)
553 {
554 ERROR ("java plugin: ctoj_oconfig_item: "
555 "Creating String object failed.");
556 return (NULL);
557 }
559 /* Create an OConfigItem object */
560 o_ocitem = (*jvm_env)->NewObject (jvm_env,
561 c_ocitem, m_ocitem_constructor, o_key);
562 if (o_ocitem == NULL)
563 {
564 ERROR ("java plugin: ctoj_oconfig_item: "
565 "Creating an OConfigItem object failed.");
566 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
567 return (NULL);
568 }
570 /* We don't need the String object any longer.. */
571 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
573 /* Call OConfigItem.addValue for each value */
574 for (int i = 0; i < ci->values_num; i++) /* {{{ */
575 {
576 jobject o_value;
578 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
579 if (o_value == NULL)
580 {
581 ERROR ("java plugin: ctoj_oconfig_item: "
582 "Creating an OConfigValue object failed.");
583 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
584 return (NULL);
585 }
587 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
588 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
589 } /* }}} for (i = 0; i < ci->values_num; i++) */
591 /* Call OConfigItem.addChild for each child */
592 for (int i = 0; i < ci->children_num; i++) /* {{{ */
593 {
594 jobject o_child;
596 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
597 if (o_child == NULL)
598 {
599 ERROR ("java plugin: ctoj_oconfig_item: "
600 "Creating an OConfigItem object failed.");
601 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
602 return (NULL);
603 }
605 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addchild, o_child);
606 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
607 } /* }}} for (i = 0; i < ci->children_num; i++) */
609 return (o_ocitem);
610 } /* }}} jobject ctoj_oconfig_item */
612 /* Convert a data_set_t to a org/collectd/api/DataSet */
613 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
614 {
615 jclass c_dataset;
616 jmethodID m_constructor;
617 jmethodID m_add;
618 jobject o_type;
619 jobject o_dataset;
621 /* Look up the org/collectd/api/DataSet class */
622 c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet");
623 if (c_dataset == NULL)
624 {
625 ERROR ("java plugin: ctoj_data_set: Looking up the "
626 "org/collectd/api/DataSet class failed.");
627 return (NULL);
628 }
630 /* Search for the `DataSet (String type)' constructor. */
631 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
632 c_dataset, "<init>", "(Ljava/lang/String;)V");
633 if (m_constructor == NULL)
634 {
635 ERROR ("java plugin: ctoj_data_set: Looking up the "
636 "`DataSet (String)' constructor failed.");
637 return (NULL);
638 }
640 /* Search for the `void addDataSource (DataSource)' method. */
641 m_add = (*jvm_env)->GetMethodID (jvm_env,
642 c_dataset, "addDataSource", "(Lorg/collectd/api/DataSource;)V");
643 if (m_add == NULL)
644 {
645 ERROR ("java plugin: ctoj_data_set: Looking up the "
646 "`addDataSource (DataSource)' method failed.");
647 return (NULL);
648 }
650 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
651 if (o_type == NULL)
652 {
653 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
654 return (NULL);
655 }
657 o_dataset = (*jvm_env)->NewObject (jvm_env,
658 c_dataset, m_constructor, o_type);
659 if (o_dataset == NULL)
660 {
661 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
662 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
663 return (NULL);
664 }
666 /* Decrease reference counter on the java.lang.String object. */
667 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
669 for (size_t i = 0; i < ds->ds_num; i++)
670 {
671 jobject o_datasource;
673 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
674 if (o_datasource == NULL)
675 {
676 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
677 ds->type, ds->ds[i].name);
678 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
679 return (NULL);
680 }
682 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
684 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
685 } /* for (i = 0; i < ds->ds_num; i++) */
687 return (o_dataset);
688 } /* }}} jobject ctoj_data_set */
690 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
691 value_t value, int ds_type,
692 jclass class_ptr, jobject object_ptr)
693 {
694 jmethodID m_addvalue;
695 jobject o_number;
697 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
698 "addValue", "(Ljava/lang/Number;)V");
699 if (m_addvalue == NULL)
700 {
701 ERROR ("java plugin: ctoj_value_list_add_value: "
702 "Cannot find method `void addValue (Number)'.");
703 return (-1);
704 }
706 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
707 if (o_number == NULL)
708 {
709 ERROR ("java plugin: ctoj_value_list_add_value: "
710 "ctoj_value_to_number failed.");
711 return (-1);
712 }
714 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
716 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
718 return (0);
719 } /* }}} int ctoj_value_list_add_value */
721 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
722 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
723 {
724 jmethodID m_setdataset;
725 jobject o_dataset;
727 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
728 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
729 "setDataSet", "(Lorg/collectd/api/DataSet;)V");
730 if (m_setdataset == NULL)
731 {
732 ERROR ("java plugin: ctoj_value_list_add_data_set: "
733 "Cannot find the `void setDataSet (DataSet)' method.");
734 return (-1);
735 }
737 /* Create a DataSet object. */
738 o_dataset = ctoj_data_set (jvm_env, ds);
739 if (o_dataset == NULL)
740 {
741 ERROR ("java plugin: ctoj_value_list_add_data_set: "
742 "ctoj_data_set (%s) failed.", ds->type);
743 return (-1);
744 }
746 /* Actually call the method. */
747 (*jvm_env)->CallVoidMethod (jvm_env,
748 o_valuelist, m_setdataset, o_dataset);
750 /* Decrease reference counter on the List<DataSource> object. */
751 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
753 return (0);
754 } /* }}} int ctoj_value_list_add_data_set */
756 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
757 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
758 const data_set_t *ds, const value_list_t *vl)
759 {
760 jclass c_valuelist;
761 jmethodID m_valuelist_constructor;
762 jobject o_valuelist;
763 int status;
765 /* First, create a new ValueList instance..
766 * Look up the class.. */
767 c_valuelist = (*jvm_env)->FindClass (jvm_env,
768 "org/collectd/api/ValueList");
769 if (c_valuelist == NULL)
770 {
771 ERROR ("java plugin: ctoj_value_list: "
772 "FindClass (org/collectd/api/ValueList) failed.");
773 return (NULL);
774 }
776 /* Lookup the `ValueList ()' constructor. */
777 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
778 "<init>", "()V");
779 if (m_valuelist_constructor == NULL)
780 {
781 ERROR ("java plugin: ctoj_value_list: Cannot find the "
782 "`ValueList ()' constructor.");
783 return (NULL);
784 }
786 /* Create a new instance. */
787 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
788 m_valuelist_constructor);
789 if (o_valuelist == NULL)
790 {
791 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
792 "failed.");
793 return (NULL);
794 }
796 status = ctoj_value_list_add_data_set (jvm_env,
797 c_valuelist, o_valuelist, ds);
798 if (status != 0)
799 {
800 ERROR ("java plugin: ctoj_value_list: "
801 "ctoj_value_list_add_data_set failed.");
802 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
803 return (NULL);
804 }
806 /* Set the strings.. */
807 #define SET_STRING(str,method_name) do { \
808 status = ctoj_string (jvm_env, str, \
809 c_valuelist, o_valuelist, method_name); \
810 if (status != 0) { \
811 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
812 method_name); \
813 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
814 return (NULL); \
815 } } while (0)
817 SET_STRING (vl->host, "setHost");
818 SET_STRING (vl->plugin, "setPlugin");
819 SET_STRING (vl->plugin_instance, "setPluginInstance");
820 SET_STRING (vl->type, "setType");
821 SET_STRING (vl->type_instance, "setTypeInstance");
823 #undef SET_STRING
825 /* Set the `time' member. Java stores time in milliseconds. */
826 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (vl->time),
827 c_valuelist, o_valuelist, "setTime");
828 if (status != 0)
829 {
830 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
831 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
832 return (NULL);
833 }
835 /* Set the `interval' member.. */
836 status = ctoj_long (jvm_env,
837 (jlong) CDTIME_T_TO_MS (vl->interval),
838 c_valuelist, o_valuelist, "setInterval");
839 if (status != 0)
840 {
841 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
842 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
843 return (NULL);
844 }
846 for (size_t i = 0; i < vl->values_len; i++)
847 {
848 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
849 c_valuelist, o_valuelist);
850 if (status != 0)
851 {
852 ERROR ("java plugin: ctoj_value_list: "
853 "ctoj_value_list_add_value failed.");
854 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
855 return (NULL);
856 }
857 }
859 return (o_valuelist);
860 } /* }}} jobject ctoj_value_list */
862 /* Convert a notification_t to a org/collectd/api/Notification */
863 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
864 const notification_t *n)
865 {
866 jclass c_notification;
867 jmethodID m_constructor;
868 jobject o_notification;
869 int status;
871 /* First, create a new Notification instance..
872 * Look up the class.. */
873 c_notification = (*jvm_env)->FindClass (jvm_env,
874 "org/collectd/api/Notification");
875 if (c_notification == NULL)
876 {
877 ERROR ("java plugin: ctoj_notification: "
878 "FindClass (org/collectd/api/Notification) failed.");
879 return (NULL);
880 }
882 /* Lookup the `Notification ()' constructor. */
883 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
884 "<init>", "()V");
885 if (m_constructor == NULL)
886 {
887 ERROR ("java plugin: ctoj_notification: Cannot find the "
888 "`Notification ()' constructor.");
889 return (NULL);
890 }
892 /* Create a new instance. */
893 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
894 m_constructor);
895 if (o_notification == NULL)
896 {
897 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
898 "instance failed.");
899 return (NULL);
900 }
902 /* Set the strings.. */
903 #define SET_STRING(str,method_name) do { \
904 status = ctoj_string (jvm_env, str, \
905 c_notification, o_notification, method_name); \
906 if (status != 0) { \
907 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
908 method_name); \
909 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
910 return (NULL); \
911 } } while (0)
913 SET_STRING (n->host, "setHost");
914 SET_STRING (n->plugin, "setPlugin");
915 SET_STRING (n->plugin_instance, "setPluginInstance");
916 SET_STRING (n->type, "setType");
917 SET_STRING (n->type_instance, "setTypeInstance");
918 SET_STRING (n->message, "setMessage");
920 #undef SET_STRING
922 /* Set the `time' member. Java stores time in milliseconds. */
923 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (n->time),
924 c_notification, o_notification, "setTime");
925 if (status != 0)
926 {
927 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
928 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
929 return (NULL);
930 }
932 /* Set the `severity' member.. */
933 status = ctoj_int (jvm_env, (jint) n->severity,
934 c_notification, o_notification, "setSeverity");
935 if (status != 0)
936 {
937 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
938 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
939 return (NULL);
940 }
942 return (o_notification);
943 } /* }}} jobject ctoj_notification */
945 /*
946 * Java to C conversion functions
947 */
948 /* Call a `String <method> ()' method. */
949 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
950 char *buffer, size_t buffer_size, int empty_okay,
951 jclass class_ptr, jobject object_ptr, const char *method_name)
952 {
953 jmethodID method_id;
954 jobject string_obj;
955 const char *c_str;
957 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
958 method_name, "()Ljava/lang/String;");
959 if (method_id == NULL)
960 {
961 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
962 method_name);
963 return (-1);
964 }
966 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
967 if ((string_obj == NULL) && (empty_okay == 0))
968 {
969 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
970 method_name);
971 return (-1);
972 }
973 else if ((string_obj == NULL) && (empty_okay != 0))
974 {
975 memset (buffer, 0, buffer_size);
976 return (0);
977 }
979 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
980 if (c_str == NULL)
981 {
982 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
983 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
984 return (-1);
985 }
987 sstrncpy (buffer, c_str, buffer_size);
989 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
990 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
992 return (0);
993 } /* }}} int jtoc_string */
995 /* Call an `int <method> ()' method. */
996 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
997 jint *ret_value,
998 jclass class_ptr, jobject object_ptr, const char *method_name)
999 {
1000 jmethodID method_id;
1002 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1003 method_name, "()I");
1004 if (method_id == NULL)
1005 {
1006 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
1007 method_name);
1008 return (-1);
1009 }
1011 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
1013 return (0);
1014 } /* }}} int jtoc_int */
1016 /* Call a `long <method> ()' method. */
1017 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
1018 jlong *ret_value,
1019 jclass class_ptr, jobject object_ptr, const char *method_name)
1020 {
1021 jmethodID method_id;
1023 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1024 method_name, "()J");
1025 if (method_id == NULL)
1026 {
1027 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1028 method_name);
1029 return (-1);
1030 }
1032 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1034 return (0);
1035 } /* }}} int jtoc_long */
1037 /* Call a `double <method> ()' method. */
1038 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1039 jdouble *ret_value,
1040 jclass class_ptr, jobject object_ptr, const char *method_name)
1041 {
1042 jmethodID method_id;
1044 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1045 method_name, "()D");
1046 if (method_id == NULL)
1047 {
1048 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1049 method_name);
1050 return (-1);
1051 }
1053 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1055 return (0);
1056 } /* }}} int jtoc_double */
1058 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1059 value_t *ret_value, int ds_type, jobject object_ptr)
1060 {
1061 jclass class_ptr;
1062 int status;
1064 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1066 if (ds_type == DS_TYPE_GAUGE)
1067 {
1068 jdouble tmp_double;
1070 status = jtoc_double (jvm_env, &tmp_double,
1071 class_ptr, object_ptr, "doubleValue");
1072 if (status != 0)
1073 {
1074 ERROR ("java plugin: jtoc_value: "
1075 "jtoc_double failed.");
1076 return (-1);
1077 }
1078 (*ret_value).gauge = (gauge_t) tmp_double;
1079 }
1080 else
1081 {
1082 jlong tmp_long;
1084 status = jtoc_long (jvm_env, &tmp_long,
1085 class_ptr, object_ptr, "longValue");
1086 if (status != 0)
1087 {
1088 ERROR ("java plugin: jtoc_value: "
1089 "jtoc_long failed.");
1090 return (-1);
1091 }
1093 if (ds_type == DS_TYPE_DERIVE)
1094 (*ret_value).derive = (derive_t) tmp_long;
1095 else if (ds_type == DS_TYPE_ABSOLUTE)
1096 (*ret_value).absolute = (absolute_t) tmp_long;
1097 else
1098 (*ret_value).counter = (counter_t) tmp_long;
1099 }
1101 return (0);
1102 } /* }}} int jtoc_value */
1104 /* Read a List<Number>, convert it to `value_t' and add it to the given
1105 * `value_list_t'. */
1106 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1107 const data_set_t *ds, value_list_t *vl,
1108 jclass class_ptr, jobject object_ptr)
1109 {
1110 jmethodID m_getvalues;
1111 jmethodID m_toarray;
1112 jobject o_list;
1113 jobjectArray o_number_array;
1115 value_t *values;
1116 int values_num;
1118 values_num = ds->ds_num;
1120 values = NULL;
1121 o_number_array = NULL;
1122 o_list = NULL;
1124 #define BAIL_OUT(status) \
1125 free (values); \
1126 if (o_number_array != NULL) \
1127 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1128 if (o_list != NULL) \
1129 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1130 return (status);
1132 /* Call: List<Number> ValueList.getValues () */
1133 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1134 "getValues", "()Ljava/util/List;");
1135 if (m_getvalues == NULL)
1136 {
1137 ERROR ("java plugin: jtoc_values_array: "
1138 "Cannot find method `List getValues ()'.");
1139 BAIL_OUT (-1);
1140 }
1142 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1143 if (o_list == NULL)
1144 {
1145 ERROR ("java plugin: jtoc_values_array: "
1146 "CallObjectMethod (getValues) failed.");
1147 BAIL_OUT (-1);
1148 }
1150 /* Call: Number[] List.toArray () */
1151 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1152 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1153 "toArray", "()[Ljava/lang/Object;");
1154 if (m_toarray == NULL)
1155 {
1156 ERROR ("java plugin: jtoc_values_array: "
1157 "Cannot find method `Object[] toArray ()'.");
1158 BAIL_OUT (-1);
1159 }
1161 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1162 if (o_number_array == NULL)
1163 {
1164 ERROR ("java plugin: jtoc_values_array: "
1165 "CallObjectMethod (toArray) failed.");
1166 BAIL_OUT (-1);
1167 }
1169 values = (value_t *) calloc (values_num, sizeof (value_t));
1170 if (values == NULL)
1171 {
1172 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1173 BAIL_OUT (-1);
1174 }
1176 for (int i = 0; i < values_num; i++)
1177 {
1178 jobject o_number;
1179 int status;
1181 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1182 o_number_array, (jsize) i);
1183 if (o_number == NULL)
1184 {
1185 ERROR ("java plugin: jtoc_values_array: "
1186 "GetObjectArrayElement (%i) failed.", i);
1187 BAIL_OUT (-1);
1188 }
1190 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1191 if (status != 0)
1192 {
1193 ERROR ("java plugin: jtoc_values_array: "
1194 "jtoc_value (%i) failed.", i);
1195 BAIL_OUT (-1);
1196 }
1197 } /* for (i = 0; i < values_num; i++) */
1199 vl->values = values;
1200 vl->values_len = values_num;
1202 #undef BAIL_OUT
1203 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1204 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1205 return (0);
1206 } /* }}} int jtoc_values_array */
1208 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1209 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1210 jobject object_ptr)
1211 {
1212 jclass class_ptr;
1213 int status;
1214 jlong tmp_long;
1215 const data_set_t *ds;
1217 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1218 if (class_ptr == NULL)
1219 {
1220 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1221 return (-1);
1222 }
1224 /* eo == empty okay */
1225 #define SET_STRING(buffer,method, eo) do { \
1226 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1227 class_ptr, object_ptr, method); \
1228 if (status != 0) { \
1229 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1230 method); \
1231 return (-1); \
1232 } } while (0)
1234 SET_STRING(vl->type, "getType", /* empty = */ 0);
1236 ds = plugin_get_ds (vl->type);
1237 if (ds == NULL)
1238 {
1239 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1240 "Please consult the types.db(5) manpage for mor information.",
1241 vl->type);
1242 return (-1);
1243 }
1245 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1246 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1247 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1248 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1250 #undef SET_STRING
1252 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1253 if (status != 0)
1254 {
1255 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1256 return (-1);
1257 }
1258 /* Java measures time in milliseconds. */
1259 vl->time = MS_TO_CDTIME_T (tmp_long);
1261 status = jtoc_long (jvm_env, &tmp_long,
1262 class_ptr, object_ptr, "getInterval");
1263 if (status != 0)
1264 {
1265 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1266 return (-1);
1267 }
1268 vl->interval = MS_TO_CDTIME_T (tmp_long);
1270 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1271 if (status != 0)
1272 {
1273 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1274 return (-1);
1275 }
1277 return (0);
1278 } /* }}} int jtoc_value_list */
1280 /* Convert a org/collectd/api/Notification to a notification_t. */
1281 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1282 jobject object_ptr)
1283 {
1284 jclass class_ptr;
1285 int status;
1286 jlong tmp_long;
1287 jint tmp_int;
1289 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1290 if (class_ptr == NULL)
1291 {
1292 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1293 return (-1);
1294 }
1296 /* eo == empty okay */
1297 #define SET_STRING(buffer,method, eo) do { \
1298 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1299 class_ptr, object_ptr, method); \
1300 if (status != 0) { \
1301 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1302 method); \
1303 return (-1); \
1304 } } while (0)
1306 SET_STRING (n->host, "getHost", /* empty = */ 1);
1307 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1308 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1309 SET_STRING (n->type, "getType", /* empty = */ 1);
1310 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1311 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1313 #undef SET_STRING
1315 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1316 if (status != 0)
1317 {
1318 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1319 return (-1);
1320 }
1321 /* Java measures time in milliseconds. */
1322 n->time = MS_TO_CDTIME_T(tmp_long);
1324 status = jtoc_int (jvm_env, &tmp_int,
1325 class_ptr, object_ptr, "getSeverity");
1326 if (status != 0)
1327 {
1328 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1329 return (-1);
1330 }
1331 n->severity = (int) tmp_int;
1333 return (0);
1334 } /* }}} int jtoc_notification */
1335 /*
1336 * Functions accessible from Java
1337 */
1338 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1339 jobject this, jobject java_vl)
1340 {
1341 value_list_t vl = VALUE_LIST_INIT;
1342 int status;
1344 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1346 status = jtoc_value_list (jvm_env, &vl, java_vl);
1347 if (status != 0)
1348 {
1349 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1350 return (-1);
1351 }
1353 status = plugin_dispatch_values (&vl);
1355 sfree (vl.values);
1357 return (status);
1358 } /* }}} jint cjni_api_dispatch_values */
1360 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1361 jobject this, jobject o_notification)
1362 {
1363 notification_t n = { 0 };
1364 int status;
1366 status = jtoc_notification (jvm_env, &n, o_notification);
1367 if (status != 0)
1368 {
1369 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1370 return (-1);
1371 }
1373 status = plugin_dispatch_notification (&n);
1375 return (status);
1376 } /* }}} jint cjni_api_dispatch_notification */
1378 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1379 jobject this, jobject o_string_type)
1380 {
1381 const char *ds_name;
1382 const data_set_t *ds;
1383 jobject o_dataset;
1385 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1386 if (ds_name == NULL)
1387 {
1388 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1389 return (NULL);
1390 }
1392 ds = plugin_get_ds (ds_name);
1393 DEBUG ("java plugin: cjni_api_get_ds: "
1394 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1396 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1398 if (ds == NULL)
1399 return (NULL);
1401 o_dataset = ctoj_data_set (jvm_env, ds);
1402 return (o_dataset);
1403 } /* }}} jint cjni_api_get_ds */
1405 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1406 jobject this, jobject o_name, jobject o_config)
1407 {
1408 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1409 } /* }}} jint cjni_api_register_config */
1411 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1412 jobject this, jobject o_name, jobject o_config)
1413 {
1414 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1415 } /* }}} jint cjni_api_register_init */
1417 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1418 jobject this, jobject o_name, jobject o_read)
1419 {
1420 cjni_callback_info_t *cbi;
1422 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1423 if (cbi == NULL)
1424 return (-1);
1426 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1428 user_data_t ud = {
1429 .data = cbi,
1430 .free_func = cjni_callback_info_destroy
1431 };
1433 plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read,
1434 /* interval = */ 0, &ud);
1436 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1438 return (0);
1439 } /* }}} jint cjni_api_register_read */
1441 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1442 jobject this, jobject o_name, jobject o_write)
1443 {
1444 cjni_callback_info_t *cbi;
1446 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1447 if (cbi == NULL)
1448 return (-1);
1450 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1452 user_data_t ud = {
1453 .data = cbi,
1454 .free_func = cjni_callback_info_destroy
1455 };
1457 plugin_register_write (cbi->name, cjni_write, &ud);
1459 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1461 return (0);
1462 } /* }}} jint cjni_api_register_write */
1464 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1465 jobject this, jobject o_name, jobject o_flush)
1466 {
1467 cjni_callback_info_t *cbi;
1469 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1470 if (cbi == NULL)
1471 return (-1);
1473 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1475 user_data_t ud = {
1476 .data = cbi,
1477 .free_func = cjni_callback_info_destroy
1478 };
1480 plugin_register_flush (cbi->name, cjni_flush, &ud);
1482 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1484 return (0);
1485 } /* }}} jint cjni_api_register_flush */
1487 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1488 jobject this, jobject o_name, jobject o_shutdown)
1489 {
1490 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1491 CB_TYPE_SHUTDOWN));
1492 } /* }}} jint cjni_api_register_shutdown */
1494 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1495 jobject this, jobject o_name, jobject o_log)
1496 {
1497 cjni_callback_info_t *cbi;
1499 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1500 if (cbi == NULL)
1501 return (-1);
1503 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1505 user_data_t ud = {
1506 .data = cbi,
1507 .free_func = cjni_callback_info_destroy
1508 };
1510 plugin_register_log (cbi->name, cjni_log, &ud);
1512 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1514 return (0);
1515 } /* }}} jint cjni_api_register_log */
1517 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1518 jobject this, jobject o_name, jobject o_notification)
1519 {
1520 cjni_callback_info_t *cbi;
1522 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1523 CB_TYPE_NOTIFICATION);
1524 if (cbi == NULL)
1525 return (-1);
1527 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1529 user_data_t ud = {
1530 .data = cbi,
1531 .free_func = cjni_callback_info_destroy
1532 };
1534 plugin_register_notification (cbi->name, cjni_notification, &ud);
1536 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1538 return (0);
1539 } /* }}} jint cjni_api_register_notification */
1541 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1542 jobject this, jobject o_name, jobject o_match, int type)
1543 {
1544 int status;
1545 const char *c_name;
1547 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1548 if (c_name == NULL)
1549 {
1550 ERROR ("java plugin: cjni_api_register_match_target: "
1551 "GetStringUTFChars failed.");
1552 return (-1);
1553 }
1555 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1556 if (status != 0)
1557 {
1558 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1559 return (-1);
1560 }
1562 if (type == CB_TYPE_MATCH)
1563 {
1564 match_proc_t m_proc = { 0 };
1566 m_proc.create = cjni_match_target_create;
1567 m_proc.destroy = cjni_match_target_destroy;
1568 m_proc.match = (void *) cjni_match_target_invoke;
1570 status = fc_register_match (c_name, m_proc);
1571 }
1572 else if (type == CB_TYPE_TARGET)
1573 {
1574 target_proc_t t_proc = { 0 };
1576 t_proc.create = cjni_match_target_create;
1577 t_proc.destroy = cjni_match_target_destroy;
1578 t_proc.invoke = cjni_match_target_invoke;
1580 status = fc_register_target (c_name, t_proc);
1581 }
1582 else
1583 {
1584 ERROR ("java plugin: cjni_api_register_match_target: "
1585 "Don't know whether to create a match or a target.");
1586 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1587 return (-1);
1588 }
1590 if (status != 0)
1591 {
1592 ERROR ("java plugin: cjni_api_register_match_target: "
1593 "%s failed.",
1594 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1595 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1596 return (-1);
1597 }
1599 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1601 return (0);
1602 } /* }}} jint cjni_api_register_match_target */
1604 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1605 jobject this, jobject o_name, jobject o_match)
1606 {
1607 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1608 CB_TYPE_MATCH));
1609 } /* }}} jint cjni_api_register_match */
1611 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1612 jobject this, jobject o_name, jobject o_target)
1613 {
1614 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1615 CB_TYPE_TARGET));
1616 } /* }}} jint cjni_api_register_target */
1618 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1619 jobject this, jint severity, jobject o_message)
1620 {
1621 const char *c_str;
1623 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1624 if (c_str == NULL)
1625 {
1626 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1627 return;
1628 }
1630 if (severity < LOG_ERR)
1631 severity = LOG_ERR;
1632 if (severity > LOG_DEBUG)
1633 severity = LOG_DEBUG;
1635 plugin_log (severity, "%s", c_str);
1637 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1638 } /* }}} void cjni_api_log */
1640 static jstring JNICALL cjni_api_get_hostname (JNIEnv *jvm_env, jobject this)
1641 {
1642 return ctoj_output_string(jvm_env, hostname_g);
1643 }
1645 /* List of ``native'' functions, i. e. C-functions that can be called from
1646 * Java. */
1647 static JNINativeMethod jni_api_functions[] = /* {{{ */
1648 {
1649 { "dispatchValues",
1650 "(Lorg/collectd/api/ValueList;)I",
1651 cjni_api_dispatch_values },
1653 { "dispatchNotification",
1654 "(Lorg/collectd/api/Notification;)I",
1655 cjni_api_dispatch_notification },
1657 { "getDS",
1658 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1659 cjni_api_get_ds },
1661 { "registerConfig",
1662 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1663 cjni_api_register_config },
1665 { "registerInit",
1666 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1667 cjni_api_register_init },
1669 { "registerRead",
1670 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1671 cjni_api_register_read },
1673 { "registerWrite",
1674 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1675 cjni_api_register_write },
1677 { "registerFlush",
1678 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1679 cjni_api_register_flush },
1681 { "registerShutdown",
1682 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1683 cjni_api_register_shutdown },
1685 { "registerLog",
1686 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1687 cjni_api_register_log },
1689 { "registerNotification",
1690 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1691 cjni_api_register_notification },
1693 { "registerMatch",
1694 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1695 cjni_api_register_match },
1697 { "registerTarget",
1698 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1699 cjni_api_register_target },
1701 { "log",
1702 "(ILjava/lang/String;)V",
1703 cjni_api_log },
1705 { "getHostname",
1706 "()Ljava/lang/String;",
1707 cjni_api_get_hostname },
1709 };
1710 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1711 / sizeof (jni_api_functions[0]);
1712 /* }}} */
1714 /*
1715 * Functions
1716 */
1717 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1718 * all registration functions. */
1719 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1720 jobject o_name, jobject o_callback, int type)
1721 {
1722 const char *c_name;
1723 cjni_callback_info_t *cbi;
1724 const char *method_name;
1725 const char *method_signature;
1727 switch (type)
1728 {
1729 case CB_TYPE_CONFIG:
1730 method_name = "config";
1731 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1732 break;
1734 case CB_TYPE_INIT:
1735 method_name = "init";
1736 method_signature = "()I";
1737 break;
1739 case CB_TYPE_READ:
1740 method_name = "read";
1741 method_signature = "()I";
1742 break;
1744 case CB_TYPE_WRITE:
1745 method_name = "write";
1746 method_signature = "(Lorg/collectd/api/ValueList;)I";
1747 break;
1749 case CB_TYPE_FLUSH:
1750 method_name = "flush";
1751 method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I";
1752 break;
1754 case CB_TYPE_SHUTDOWN:
1755 method_name = "shutdown";
1756 method_signature = "()I";
1757 break;
1759 case CB_TYPE_LOG:
1760 method_name = "log";
1761 method_signature = "(ILjava/lang/String;)V";
1762 break;
1764 case CB_TYPE_NOTIFICATION:
1765 method_name = "notification";
1766 method_signature = "(Lorg/collectd/api/Notification;)I";
1767 break;
1769 case CB_TYPE_MATCH:
1770 method_name = "createMatch";
1771 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1772 "Lorg/collectd/api/CollectdMatchInterface;";
1773 break;
1775 case CB_TYPE_TARGET:
1776 method_name = "createTarget";
1777 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1778 "Lorg/collectd/api/CollectdTargetInterface;";
1779 break;
1781 default:
1782 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1783 type);
1784 return (NULL);
1785 }
1787 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1788 if (c_name == NULL)
1789 {
1790 ERROR ("java plugin: cjni_callback_info_create: "
1791 "GetStringUTFChars failed.");
1792 return (NULL);
1793 }
1795 cbi = calloc (1, sizeof (*cbi));
1796 if (cbi == NULL)
1797 {
1798 ERROR ("java plugin: cjni_callback_info_create: calloc failed.");
1799 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1800 return (NULL);
1801 }
1802 cbi->type = type;
1804 cbi->name = strdup (c_name);
1805 if (cbi->name == NULL)
1806 {
1807 pthread_mutex_unlock (&java_callbacks_lock);
1808 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1809 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1810 sfree (cbi);
1811 return (NULL);
1812 }
1814 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1816 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1817 if (cbi->object == NULL)
1818 {
1819 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1820 sfree (cbi->name);
1821 sfree (cbi);
1822 return (NULL);
1823 }
1825 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1826 if (cbi->class == NULL)
1827 {
1828 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1829 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1830 sfree (cbi->name);
1831 sfree (cbi);
1832 return (NULL);
1833 }
1835 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1836 method_name, method_signature);
1837 if (cbi->method == NULL)
1838 {
1839 ERROR ("java plugin: cjni_callback_info_create: "
1840 "Cannot find the `%s' method with signature `%s'.",
1841 method_name, method_signature);
1842 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1843 sfree (cbi->name);
1844 sfree (cbi);
1845 return (NULL);
1846 }
1848 return (cbi);
1849 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1851 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1852 * to the global `java_callbacks' variable. This is used for `config', `init',
1853 * and `shutdown' callbacks. */
1854 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1855 jobject o_name, jobject o_callback, int type)
1856 {
1857 cjni_callback_info_t *cbi;
1858 cjni_callback_info_t *tmp;
1859 #if COLLECT_DEBUG
1860 const char *type_str;
1861 #endif
1863 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1864 if (cbi == NULL)
1865 return (-1);
1867 #if COLLECT_DEBUG
1868 switch (type)
1869 {
1870 case CB_TYPE_CONFIG:
1871 type_str = "config";
1872 break;
1874 case CB_TYPE_INIT:
1875 type_str = "init";
1876 break;
1878 case CB_TYPE_SHUTDOWN:
1879 type_str = "shutdown";
1880 break;
1882 case CB_TYPE_MATCH:
1883 type_str = "match";
1884 break;
1886 case CB_TYPE_TARGET:
1887 type_str = "target";
1888 break;
1890 default:
1891 type_str = "<unknown>";
1892 }
1893 DEBUG ("java plugin: Registering new %s callback: %s",
1894 type_str, cbi->name);
1895 #endif
1897 pthread_mutex_lock (&java_callbacks_lock);
1899 tmp = realloc (java_callbacks,
1900 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1901 if (tmp == NULL)
1902 {
1903 pthread_mutex_unlock (&java_callbacks_lock);
1904 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1906 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1907 free (cbi);
1909 return (-1);
1910 }
1911 java_callbacks = tmp;
1912 java_callbacks[java_callbacks_num] = *cbi;
1913 java_callbacks_num++;
1915 pthread_mutex_unlock (&java_callbacks_lock);
1917 free (cbi);
1918 return (0);
1919 } /* }}} int cjni_callback_register */
1921 /* Callback for `pthread_key_create'. It frees the data contained in
1922 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1923 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1924 {
1925 cjni_jvm_env_t *cjni_env;
1927 if (args == NULL)
1928 return;
1930 cjni_env = (cjni_jvm_env_t *) args;
1932 if (cjni_env->reference_counter > 0)
1933 {
1934 ERROR ("java plugin: cjni_jvm_env_destroy: "
1935 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1936 }
1938 if (cjni_env->jvm_env != NULL)
1939 {
1940 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1941 (void *) cjni_env->jvm_env);
1942 }
1944 /* The pointer is allocated in `cjni_thread_attach' */
1945 free (cjni_env);
1946 } /* }}} void cjni_jvm_env_destroy */
1948 /* Register ``native'' functions with the JVM. Native functions are C-functions
1949 * that can be called by Java code. */
1950 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1951 {
1952 jclass api_class_ptr;
1953 int status;
1955 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1956 if (api_class_ptr == NULL)
1957 {
1958 ERROR ("cjni_init_native: Cannot find the API class \"org.collectd.api"
1959 ".Collectd\". Please set the correct class path "
1960 "using 'JVMArg \"-Djava.class.path=...\"'.");
1961 return (-1);
1962 }
1964 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1965 jni_api_functions, (jint) jni_api_functions_num);
1966 if (status != 0)
1967 {
1968 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1969 return (-1);
1970 }
1972 return (0);
1973 } /* }}} int cjni_init_native */
1975 /* Create the JVM. This is called when the first thread tries to access the JVM
1976 * via cjni_thread_attach. */
1977 static int cjni_create_jvm (void) /* {{{ */
1978 {
1979 JNIEnv *jvm_env;
1980 JavaVMInitArgs vm_args = { 0 };
1981 JavaVMOption vm_options[jvm_argc];
1983 int status;
1985 if (jvm != NULL)
1986 return (0);
1988 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1989 if (status != 0)
1990 {
1991 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1992 "with status %i.", status);
1993 return (-1);
1994 }
1996 jvm_env = NULL;
1998 vm_args.version = JNI_VERSION_1_2;
1999 vm_args.options = vm_options;
2000 vm_args.nOptions = (jint) jvm_argc;
2002 for (size_t i = 0; i < jvm_argc; i++)
2003 {
2004 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
2005 i, jvm_argv[i]);
2006 vm_args.options[i].optionString = jvm_argv[i];
2007 }
2009 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
2010 if (status != 0)
2011 {
2012 ERROR ("java plugin: cjni_create_jvm: "
2013 "JNI_CreateJavaVM failed with status %i.",
2014 status);
2015 return (-1);
2016 }
2017 assert (jvm != NULL);
2018 assert (jvm_env != NULL);
2020 /* Call RegisterNatives */
2021 status = cjni_init_native (jvm_env);
2022 if (status != 0)
2023 {
2024 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
2025 return (-1);
2026 }
2028 DEBUG ("java plugin: The JVM has been created.");
2029 return (0);
2030 } /* }}} int cjni_create_jvm */
2032 /* Increase the reference counter to the JVM for this thread. If it was zero,
2033 * attach the JVM first. */
2034 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2035 {
2036 cjni_jvm_env_t *cjni_env;
2037 JNIEnv *jvm_env;
2039 /* If we're the first thread to access the JVM, we'll have to create it
2040 * first.. */
2041 if (jvm == NULL)
2042 {
2043 int status;
2045 status = cjni_create_jvm ();
2046 if (status != 0)
2047 {
2048 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2049 return (NULL);
2050 }
2051 }
2052 assert (jvm != NULL);
2054 cjni_env = pthread_getspecific (jvm_env_key);
2055 if (cjni_env == NULL)
2056 {
2057 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2058 cjni_env = calloc (1, sizeof (*cjni_env));
2059 if (cjni_env == NULL)
2060 {
2061 ERROR ("java plugin: cjni_thread_attach: calloc failed.");
2062 return (NULL);
2063 }
2064 cjni_env->reference_counter = 0;
2065 cjni_env->jvm_env = NULL;
2067 pthread_setspecific (jvm_env_key, cjni_env);
2068 }
2070 if (cjni_env->reference_counter > 0)
2071 {
2072 cjni_env->reference_counter++;
2073 jvm_env = cjni_env->jvm_env;
2074 }
2075 else
2076 {
2077 int status;
2078 JavaVMAttachArgs args = { 0 };
2080 assert (cjni_env->jvm_env == NULL);
2082 args.version = JNI_VERSION_1_2;
2084 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2085 if (status != 0)
2086 {
2087 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2088 "with status %i.", status);
2089 return (NULL);
2090 }
2092 cjni_env->reference_counter = 1;
2093 cjni_env->jvm_env = jvm_env;
2094 }
2096 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2097 cjni_env->reference_counter);
2098 assert (jvm_env != NULL);
2099 return (jvm_env);
2100 } /* }}} JNIEnv *cjni_thread_attach */
2102 /* Decrease the reference counter of this thread. If it reaches zero, detach
2103 * from the JVM. */
2104 static int cjni_thread_detach (void) /* {{{ */
2105 {
2106 cjni_jvm_env_t *cjni_env;
2107 int status;
2109 cjni_env = pthread_getspecific (jvm_env_key);
2110 if (cjni_env == NULL)
2111 {
2112 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2113 return (-1);
2114 }
2116 assert (cjni_env->reference_counter > 0);
2117 assert (cjni_env->jvm_env != NULL);
2119 cjni_env->reference_counter--;
2120 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2121 cjni_env->reference_counter);
2123 if (cjni_env->reference_counter > 0)
2124 return (0);
2126 status = (*jvm)->DetachCurrentThread (jvm);
2127 if (status != 0)
2128 {
2129 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2130 "with status %i.", status);
2131 }
2133 cjni_env->reference_counter = 0;
2134 cjni_env->jvm_env = NULL;
2136 return (0);
2137 } /* }}} int cjni_thread_detach */
2139 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2140 {
2141 char **tmp;
2143 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2144 {
2145 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2146 return (-1);
2147 }
2149 if (jvm != NULL)
2150 {
2151 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2152 "`LoadPlugin' options! The JVM is already started and I have to "
2153 "ignore this argument: %s",
2154 ci->values[0].value.string);
2155 return (-1);
2156 }
2158 tmp = realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2159 if (tmp == NULL)
2160 {
2161 ERROR ("java plugin: realloc failed.");
2162 return (-1);
2163 }
2164 jvm_argv = tmp;
2166 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2167 if (jvm_argv[jvm_argc] == NULL)
2168 {
2169 ERROR ("java plugin: strdup failed.");
2170 return (-1);
2171 }
2172 jvm_argc++;
2174 return (0);
2175 } /* }}} int cjni_config_add_jvm_arg */
2177 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2178 {
2179 JNIEnv *jvm_env;
2180 java_plugin_class_t *class;
2181 jmethodID constructor_id;
2182 jobject tmp_object;
2184 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2185 {
2186 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2187 return (-1);
2188 }
2190 jvm_env = cjni_thread_attach ();
2191 if (jvm_env == NULL)
2192 return (-1);
2194 class = realloc (java_classes_list,
2195 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2196 if (class == NULL)
2197 {
2198 ERROR ("java plugin: realloc failed.");
2199 cjni_thread_detach ();
2200 return (-1);
2201 }
2202 java_classes_list = class;
2203 class = java_classes_list + java_classes_list_len;
2205 memset (class, 0, sizeof (*class));
2206 class->name = strdup (ci->values[0].value.string);
2207 if (class->name == NULL)
2208 {
2209 ERROR ("java plugin: strdup failed.");
2210 cjni_thread_detach ();
2211 return (-1);
2212 }
2213 class->class = NULL;
2214 class->object = NULL;
2216 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2217 thorough the Java community, but (Sun's) `FindClass' and friends need
2218 slashes. */
2219 for (size_t i = 0; class->name[i] != 0; i++)
2220 if (class->name[i] == '.')
2221 class->name[i] = '/';
2222 }
2224 DEBUG ("java plugin: Loading class %s", class->name);
2226 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2227 if (class->class == NULL)
2228 {
2229 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2230 class->name);
2231 cjni_thread_detach ();
2232 free (class->name);
2233 return (-1);
2234 }
2236 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2237 "<init>", "()V");
2238 if (constructor_id == NULL)
2239 {
2240 ERROR ("java plugin: cjni_config_load_plugin: "
2241 "Could not find the constructor for `%s'.",
2242 class->name);
2243 cjni_thread_detach ();
2244 free (class->name);
2245 return (-1);
2246 }
2248 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2249 constructor_id);
2250 if (tmp_object != NULL)
2251 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2252 else
2253 class->object = NULL;
2254 if (class->object == NULL)
2255 {
2256 ERROR ("java plugin: cjni_config_load_plugin: "
2257 "Could create a new `%s' object.",
2258 class->name);
2259 cjni_thread_detach ();
2260 free (class->name);
2261 return (-1);
2262 }
2264 cjni_thread_detach ();
2266 java_classes_list_len++;
2268 return (0);
2269 } /* }}} int cjni_config_load_plugin */
2271 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2272 {
2273 JNIEnv *jvm_env;
2274 cjni_callback_info_t *cbi;
2275 jobject o_ocitem;
2276 const char *name;
2278 jclass class;
2279 jmethodID method;
2281 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2282 {
2283 WARNING ("java plugin: `Plugin' blocks "
2284 "need exactly one string argument.");
2285 return (-1);
2286 }
2288 name = ci->values[0].value.string;
2290 cbi = NULL;
2291 for (size_t i = 0; i < java_callbacks_num; i++)
2292 {
2293 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2294 continue;
2296 if (strcmp (name, java_callbacks[i].name) != 0)
2297 continue;
2299 cbi = java_callbacks + i;
2300 break;
2301 }
2303 if (cbi == NULL)
2304 {
2305 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2306 "configuration callback has been registered. Please make sure, the "
2307 "`LoadPlugin' lines precede the `Plugin' blocks.",
2308 name);
2309 return (0);
2310 }
2312 DEBUG ("java plugin: Configuring %s", name);
2314 jvm_env = cjni_thread_attach ();
2315 if (jvm_env == NULL)
2316 return (-1);
2318 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2319 if (o_ocitem == NULL)
2320 {
2321 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2322 cjni_thread_detach ();
2323 return (-1);
2324 }
2326 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2327 method = (*jvm_env)->GetMethodID (jvm_env, class,
2328 "config", "(Lorg/collectd/api/OConfigItem;)I");
2330 (*jvm_env)->CallIntMethod (jvm_env,
2331 cbi->object, method, o_ocitem);
2333 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2334 cjni_thread_detach ();
2335 return (0);
2336 } /* }}} int cjni_config_plugin_block */
2338 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2339 {
2340 int success;
2341 int errors;
2342 int status;
2344 success = 0;
2345 errors = 0;
2347 for (int i = 0; i < ci->children_num; i++)
2348 {
2349 oconfig_item_t *child = ci->children + i;
2351 if (strcasecmp ("JVMArg", child->key) == 0)
2352 {
2353 status = cjni_config_add_jvm_arg (child);
2354 if (status == 0)
2355 success++;
2356 else
2357 errors++;
2358 }
2359 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2360 {
2361 status = cjni_config_load_plugin (child);
2362 if (status == 0)
2363 success++;
2364 else
2365 errors++;
2366 }
2367 else if (strcasecmp ("Plugin", child->key) == 0)
2368 {
2369 status = cjni_config_plugin_block (child);
2370 if (status == 0)
2371 success++;
2372 else
2373 errors++;
2374 }
2375 else
2376 {
2377 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2378 errors++;
2379 }
2380 }
2382 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2383 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2385 if ((success == 0) && (errors > 0))
2386 {
2387 ERROR ("java plugin: All statements failed.");
2388 return (-1);
2389 }
2391 return (0);
2392 } /* }}} int cjni_config_perform */
2394 /* Copy the children of `ci' to the global `config_block' variable. */
2395 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2396 {
2397 oconfig_item_t *ci_copy;
2398 oconfig_item_t *tmp;
2400 assert (ci != NULL);
2401 if (ci->children_num == 0)
2402 return (0); /* nothing to do */
2404 ci_copy = oconfig_clone (ci);
2405 if (ci_copy == NULL)
2406 {
2407 ERROR ("java plugin: oconfig_clone failed.");
2408 return (-1);
2409 }
2411 if (config_block == NULL)
2412 {
2413 config_block = ci_copy;
2414 return (0);
2415 }
2417 tmp = realloc (config_block->children,
2418 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2419 if (tmp == NULL)
2420 {
2421 ERROR ("java plugin: realloc failed.");
2422 oconfig_free (ci_copy);
2423 return (-1);
2424 }
2425 config_block->children = tmp;
2427 /* Copy the pointers */
2428 memcpy (config_block->children + config_block->children_num,
2429 ci_copy->children,
2430 ci_copy->children_num * sizeof (*ci_copy->children));
2431 config_block->children_num += ci_copy->children_num;
2433 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2434 memset (ci_copy->children, 0,
2435 ci_copy->children_num * sizeof (*ci_copy->children));
2436 ci_copy->children_num = 0;
2438 oconfig_free (ci_copy);
2440 return (0);
2441 } /* }}} int cjni_config_callback */
2443 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2444 * and `cjni_write'. In particular, delete the global reference to the Java
2445 * object. */
2446 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2447 {
2448 JNIEnv *jvm_env;
2449 cjni_callback_info_t *cbi;
2451 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2453 cbi = (cjni_callback_info_t *) arg;
2455 /* This condition can occur when shutting down. */
2456 if (jvm == NULL)
2457 {
2458 sfree (cbi);
2459 return;
2460 }
2462 if (arg == NULL)
2463 return;
2465 jvm_env = cjni_thread_attach ();
2466 if (jvm_env == NULL)
2467 {
2468 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2469 return;
2470 }
2472 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2474 cbi->method = NULL;
2475 cbi->object = NULL;
2476 cbi->class = NULL;
2477 free (cbi);
2479 cjni_thread_detach ();
2480 } /* }}} void cjni_callback_info_destroy */
2482 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2483 static int cjni_read (user_data_t *ud) /* {{{ */
2484 {
2485 JNIEnv *jvm_env;
2486 cjni_callback_info_t *cbi;
2487 int ret_status;
2489 if (jvm == NULL)
2490 {
2491 ERROR ("java plugin: cjni_read: jvm == NULL");
2492 return (-1);
2493 }
2495 if ((ud == NULL) || (ud->data == NULL))
2496 {
2497 ERROR ("java plugin: cjni_read: Invalid user data.");
2498 return (-1);
2499 }
2501 jvm_env = cjni_thread_attach ();
2502 if (jvm_env == NULL)
2503 return (-1);
2505 cbi = (cjni_callback_info_t *) ud->data;
2507 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2508 cbi->method);
2510 cjni_thread_detach ();
2511 return (ret_status);
2512 } /* }}} int cjni_read */
2514 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2515 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2516 user_data_t *ud)
2517 {
2518 JNIEnv *jvm_env;
2519 cjni_callback_info_t *cbi;
2520 jobject vl_java;
2521 int ret_status;
2523 if (jvm == NULL)
2524 {
2525 ERROR ("java plugin: cjni_write: jvm == NULL");
2526 return (-1);
2527 }
2529 if ((ud == NULL) || (ud->data == NULL))
2530 {
2531 ERROR ("java plugin: cjni_write: Invalid user data.");
2532 return (-1);
2533 }
2535 jvm_env = cjni_thread_attach ();
2536 if (jvm_env == NULL)
2537 return (-1);
2539 cbi = (cjni_callback_info_t *) ud->data;
2541 vl_java = ctoj_value_list (jvm_env, ds, vl);
2542 if (vl_java == NULL)
2543 {
2544 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2545 cjni_thread_detach ();
2546 return (-1);
2547 }
2549 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2550 cbi->object, cbi->method, vl_java);
2552 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2554 cjni_thread_detach ();
2555 return (ret_status);
2556 } /* }}} int cjni_write */
2558 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2559 static int cjni_flush (cdtime_t timeout, const char *identifier, /* {{{ */
2560 user_data_t *ud)
2561 {
2562 JNIEnv *jvm_env;
2563 cjni_callback_info_t *cbi;
2564 jobject o_timeout;
2565 jobject o_identifier;
2566 int ret_status;
2568 if (jvm == NULL)
2569 {
2570 ERROR ("java plugin: cjni_flush: jvm == NULL");
2571 return (-1);
2572 }
2574 if ((ud == NULL) || (ud->data == NULL))
2575 {
2576 ERROR ("java plugin: cjni_flush: Invalid user data.");
2577 return (-1);
2578 }
2580 jvm_env = cjni_thread_attach ();
2581 if (jvm_env == NULL)
2582 return (-1);
2584 cbi = (cjni_callback_info_t *) ud->data;
2586 o_timeout = ctoj_jdouble_to_number (jvm_env,
2587 (jdouble) CDTIME_T_TO_DOUBLE (timeout));
2588 if (o_timeout == NULL)
2589 {
2590 ERROR ("java plugin: cjni_flush: Converting double "
2591 "to Number object failed.");
2592 cjni_thread_detach ();
2593 return (-1);
2594 }
2596 o_identifier = NULL;
2597 if (identifier != NULL)
2598 {
2599 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2600 if (o_identifier == NULL)
2601 {
2602 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2603 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2604 cjni_thread_detach ();
2605 return (-1);
2606 }
2607 }
2609 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2610 cbi->object, cbi->method, o_timeout, o_identifier);
2612 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2613 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2615 cjni_thread_detach ();
2616 return (ret_status);
2617 } /* }}} int cjni_flush */
2619 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2620 static void cjni_log (int severity, const char *message, /* {{{ */
2621 user_data_t *ud)
2622 {
2623 JNIEnv *jvm_env;
2624 cjni_callback_info_t *cbi;
2625 jobject o_message;
2627 if (jvm == NULL)
2628 return;
2630 if ((ud == NULL) || (ud->data == NULL))
2631 return;
2633 jvm_env = cjni_thread_attach ();
2634 if (jvm_env == NULL)
2635 return;
2637 cbi = (cjni_callback_info_t *) ud->data;
2639 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2640 if (o_message == NULL)
2641 {
2642 cjni_thread_detach ();
2643 return;
2644 }
2646 (*jvm_env)->CallVoidMethod (jvm_env,
2647 cbi->object, cbi->method, (jint) severity, o_message);
2649 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2651 cjni_thread_detach ();
2652 } /* }}} void cjni_log */
2654 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2655 * pointer. */
2656 static int cjni_notification (const notification_t *n, /* {{{ */
2657 user_data_t *ud)
2658 {
2659 JNIEnv *jvm_env;
2660 cjni_callback_info_t *cbi;
2661 jobject o_notification;
2662 int ret_status;
2664 if (jvm == NULL)
2665 {
2666 ERROR ("java plugin: cjni_read: jvm == NULL");
2667 return (-1);
2668 }
2670 if ((ud == NULL) || (ud->data == NULL))
2671 {
2672 ERROR ("java plugin: cjni_read: Invalid user data.");
2673 return (-1);
2674 }
2676 jvm_env = cjni_thread_attach ();
2677 if (jvm_env == NULL)
2678 return (-1);
2680 cbi = (cjni_callback_info_t *) ud->data;
2682 o_notification = ctoj_notification (jvm_env, n);
2683 if (o_notification == NULL)
2684 {
2685 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2686 cjni_thread_detach ();
2687 return (-1);
2688 }
2690 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2691 cbi->object, cbi->method, o_notification);
2693 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2695 cjni_thread_detach ();
2696 return (ret_status);
2697 } /* }}} int cjni_notification */
2699 /* Callbacks for matches implemented in Java */
2700 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2701 void **user_data)
2702 {
2703 JNIEnv *jvm_env;
2704 cjni_callback_info_t *cbi_ret;
2705 cjni_callback_info_t *cbi_factory;
2706 const char *name;
2707 jobject o_ci;
2708 jobject o_tmp;
2709 int type;
2711 cbi_ret = NULL;
2712 o_ci = NULL;
2713 jvm_env = NULL;
2715 #define BAIL_OUT(status) \
2716 if (cbi_ret != NULL) { \
2717 free (cbi_ret->name); \
2718 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2719 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2720 } \
2721 free (cbi_ret); \
2722 if (o_ci != NULL) \
2723 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2724 cjni_thread_detach (); \
2725 return (status)
2727 if (jvm == NULL)
2728 {
2729 ERROR ("java plugin: cjni_read: jvm == NULL");
2730 return (-1);
2731 }
2733 jvm_env = cjni_thread_attach ();
2734 if (jvm_env == NULL)
2735 return (-1);
2737 /* Find out whether to create a match or a target. */
2738 if (strcasecmp ("Match", ci->key) == 0)
2739 type = CB_TYPE_MATCH;
2740 else if (strcasecmp ("Target", ci->key) == 0)
2741 type = CB_TYPE_TARGET;
2742 else
2743 {
2744 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2745 "to create a match or a target.");
2746 BAIL_OUT (-1);
2747 }
2749 /* This is the name of the match we should create. */
2750 name = ci->values[0].value.string;
2752 /* Lets see if we have a matching factory here.. */
2753 cbi_factory = NULL;
2754 for (size_t i = 0; i < java_callbacks_num; i++)
2755 {
2756 if (java_callbacks[i].type != type)
2757 continue;
2759 if (strcmp (name, java_callbacks[i].name) != 0)
2760 continue;
2762 cbi_factory = java_callbacks + i;
2763 break;
2764 }
2766 /* Nope, no factory for that name.. */
2767 if (cbi_factory == NULL)
2768 {
2769 ERROR ("java plugin: cjni_match_target_create: "
2770 "No such match factory registered: %s",
2771 name);
2772 BAIL_OUT (-1);
2773 }
2775 /* We convert `ci' to its Java equivalent.. */
2776 o_ci = ctoj_oconfig_item (jvm_env, ci);
2777 if (o_ci == NULL)
2778 {
2779 ERROR ("java plugin: cjni_match_target_create: "
2780 "ctoj_oconfig_item failed.");
2781 BAIL_OUT (-1);
2782 }
2784 /* Allocate a new callback info structure. This is going to be our user_data
2785 * pointer. */
2786 cbi_ret = calloc (1, sizeof (*cbi_ret));
2787 if (cbi_ret == NULL)
2788 {
2789 ERROR ("java plugin: cjni_match_target_create: calloc failed.");
2790 BAIL_OUT (-1);
2791 }
2793 cbi_ret->object = NULL;
2794 cbi_ret->type = type;
2796 /* Lets fill the callback info structure.. First, the name: */
2797 cbi_ret->name = strdup (name);
2798 if (cbi_ret->name == NULL)
2799 {
2800 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2801 BAIL_OUT (-1);
2802 }
2804 /* Then call the factory method so it creates a new object for us. */
2805 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2806 cbi_factory->object, cbi_factory->method, o_ci);
2807 if (o_tmp == NULL)
2808 {
2809 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2810 BAIL_OUT (-1);
2811 }
2813 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2814 if (o_tmp == NULL)
2815 {
2816 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2817 BAIL_OUT (-1);
2818 }
2820 /* This is the class of the match. It is possibly different from the class of
2821 * the match-factory! */
2822 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2823 if (cbi_ret->class == NULL)
2824 {
2825 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2826 BAIL_OUT (-1);
2827 }
2829 /* Lookup the `int match (DataSet, ValueList)' method. */
2830 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2831 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2832 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2833 if (cbi_ret->method == NULL)
2834 {
2835 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2836 BAIL_OUT (-1);
2837 }
2839 /* Return the newly created match via the user_data pointer. */
2840 *user_data = (void *) cbi_ret;
2842 cjni_thread_detach ();
2844 DEBUG ("java plugin: cjni_match_target_create: "
2845 "Successfully created a `%s' %s.",
2846 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2848 /* Success! */
2849 return (0);
2850 #undef BAIL_OUT
2851 } /* }}} int cjni_match_target_create */
2853 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2854 {
2855 cjni_callback_info_destroy (*user_data);
2856 *user_data = NULL;
2858 return (0);
2859 } /* }}} int cjni_match_target_destroy */
2861 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2862 value_list_t *vl, notification_meta_t **meta, void **user_data)
2863 {
2864 JNIEnv *jvm_env;
2865 cjni_callback_info_t *cbi;
2866 jobject o_vl;
2867 jobject o_ds;
2868 int ret_status;
2869 int status;
2871 if (jvm == NULL)
2872 {
2873 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2874 return (-1);
2875 }
2877 jvm_env = cjni_thread_attach ();
2878 if (jvm_env == NULL)
2879 return (-1);
2881 cbi = (cjni_callback_info_t *) *user_data;
2883 o_vl = ctoj_value_list (jvm_env, ds, vl);
2884 if (o_vl == NULL)
2885 {
2886 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2887 cjni_thread_detach ();
2888 return (-1);
2889 }
2891 o_ds = ctoj_data_set (jvm_env, ds);
2892 if (o_ds == NULL)
2893 {
2894 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2895 cjni_thread_detach ();
2896 return (-1);
2897 }
2899 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2900 o_ds, o_vl);
2902 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2904 /* If we're executing a target, copy the `ValueList' back to our
2905 * `value_list_t'. */
2906 if (cbi->type == CB_TYPE_TARGET)
2907 {
2908 value_list_t new_vl = { 0 };
2910 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2911 if (status != 0)
2912 {
2913 ERROR ("java plugin: cjni_match_target_invoke: "
2914 "jtoc_value_list failed.");
2915 }
2916 else /* if (status == 0) */
2917 {
2918 /* plugin_dispatch_values assures that this is dynamically allocated
2919 * memory. */
2920 sfree (vl->values);
2922 /* This will replace the vl->values pointer to a new, dynamically
2923 * allocated piece of memory. */
2924 memcpy (vl, &new_vl, sizeof (*vl));
2925 }
2926 } /* if (cbi->type == CB_TYPE_TARGET) */
2928 cjni_thread_detach ();
2929 return (ret_status);
2930 } /* }}} int cjni_match_target_invoke */
2932 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2933 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2934 {
2935 int status;
2937 for (size_t i = 0; i < java_callbacks_num; i++)
2938 {
2939 if (java_callbacks[i].type != CB_TYPE_INIT)
2940 continue;
2942 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2944 status = (*jvm_env)->CallIntMethod (jvm_env,
2945 java_callbacks[i].object, java_callbacks[i].method);
2946 if (status != 0)
2947 {
2948 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2949 "Removing read function.",
2950 java_callbacks[i].name, status);
2951 plugin_unregister_read (java_callbacks[i].name);
2952 }
2953 }
2955 return (0);
2956 } /* }}} int cjni_init_plugins */
2958 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2959 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2960 {
2961 int status;
2963 for (size_t i = 0; i < java_callbacks_num; i++)
2964 {
2965 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2966 continue;
2968 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2970 status = (*jvm_env)->CallIntMethod (jvm_env,
2971 java_callbacks[i].object, java_callbacks[i].method);
2972 if (status != 0)
2973 {
2974 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2975 java_callbacks[i].name, status);
2976 }
2977 }
2979 return (0);
2980 } /* }}} int cjni_shutdown_plugins */
2983 static int cjni_shutdown (void) /* {{{ */
2984 {
2985 JNIEnv *jvm_env;
2986 JavaVMAttachArgs args = { 0 };
2987 int status;
2989 if (jvm == NULL)
2990 return (0);
2992 jvm_env = NULL;
2993 args.version = JNI_VERSION_1_2;
2995 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
2996 if (status != 0)
2997 {
2998 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2999 status);
3000 return (-1);
3001 }
3003 /* Execute all the shutdown functions registered by plugins. */
3004 cjni_shutdown_plugins (jvm_env);
3006 /* Release all the global references to callback functions */
3007 for (size_t i = 0; i < java_callbacks_num; i++)
3008 {
3009 if (java_callbacks[i].object != NULL)
3010 {
3011 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3012 java_callbacks[i].object = NULL;
3013 }
3014 sfree (java_callbacks[i].name);
3015 }
3016 java_callbacks_num = 0;
3017 sfree (java_callbacks);
3019 /* Release all the global references to directly loaded classes. */
3020 for (size_t i = 0; i < java_classes_list_len; i++)
3021 {
3022 if (java_classes_list[i].object != NULL)
3023 {
3024 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3025 java_classes_list[i].object = NULL;
3026 }
3027 sfree (java_classes_list[i].name);
3028 }
3029 java_classes_list_len = 0;
3030 sfree (java_classes_list);
3032 /* Destroy the JVM */
3033 DEBUG ("java plugin: Destroying the JVM.");
3034 (*jvm)->DestroyJavaVM (jvm);
3035 jvm = NULL;
3036 jvm_env = NULL;
3038 pthread_key_delete (jvm_env_key);
3040 /* Free the JVM argument list */
3041 for (size_t i = 0; i < jvm_argc; i++)
3042 sfree (jvm_argv[i]);
3043 jvm_argc = 0;
3044 sfree (jvm_argv);
3046 return (0);
3047 } /* }}} int cjni_shutdown */
3049 /* Initialization: Create a JVM, load all configured classes and call their
3050 * `config' and `init' callback methods. */
3051 static int cjni_init (void) /* {{{ */
3052 {
3053 JNIEnv *jvm_env;
3055 if ((config_block == NULL) && (jvm == NULL))
3056 {
3057 ERROR ("java plugin: cjni_init: No configuration block for "
3058 "the java plugin was found.");
3059 return (-1);
3060 }
3062 if (config_block != NULL)
3063 {
3064 cjni_config_perform (config_block);
3065 oconfig_free (config_block);
3066 }
3068 if (jvm == NULL)
3069 {
3070 ERROR ("java plugin: cjni_init: jvm == NULL");
3071 return (-1);
3072 }
3074 jvm_env = cjni_thread_attach ();
3075 if (jvm_env == NULL)
3076 return (-1);
3078 cjni_init_plugins (jvm_env);
3080 cjni_thread_detach ();
3081 return (0);
3082 } /* }}} int cjni_init */
3084 void module_register (void)
3085 {
3086 plugin_register_complex_config ("java", cjni_config_callback);
3087 plugin_register_init ("java", cjni_init);
3088 plugin_register_shutdown ("java", cjni_shutdown);
3089 } /* void module_register (void) */
3091 /* vim: set sw=2 sts=2 et fdm=marker : */