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;
510 int i;
512 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/OConfigItem");
513 if (c_ocitem == NULL)
514 {
515 ERROR ("java plugin: ctoj_oconfig_item: "
516 "FindClass (org/collectd/api/OConfigItem) failed.");
517 return (NULL);
518 }
520 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
521 * {{{ */
522 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
523 "<init>", "(Ljava/lang/String;)V");
524 if (m_ocitem_constructor == NULL)
525 {
526 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
527 "`OConfigItem (String)' constructor.");
528 return (NULL);
529 }
531 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
532 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
533 if (m_addvalue == NULL)
534 {
535 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
536 "`addValue (OConfigValue)' method.");
537 return (NULL);
538 }
540 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
541 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
542 if (m_addchild == NULL)
543 {
544 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
545 "`addChild (OConfigItem)' method.");
546 return (NULL);
547 }
548 /* }}} */
550 /* Create a String object with the key.
551 * Needed for calling the constructor. */
552 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
553 if (o_key == NULL)
554 {
555 ERROR ("java plugin: ctoj_oconfig_item: "
556 "Creating String object failed.");
557 return (NULL);
558 }
560 /* Create an OConfigItem object */
561 o_ocitem = (*jvm_env)->NewObject (jvm_env,
562 c_ocitem, m_ocitem_constructor, o_key);
563 if (o_ocitem == NULL)
564 {
565 ERROR ("java plugin: ctoj_oconfig_item: "
566 "Creating an OConfigItem object failed.");
567 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
568 return (NULL);
569 }
571 /* We don't need the String object any longer.. */
572 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
574 /* Call OConfigItem.addValue for each value */
575 for (i = 0; i < ci->values_num; i++) /* {{{ */
576 {
577 jobject o_value;
579 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
580 if (o_value == NULL)
581 {
582 ERROR ("java plugin: ctoj_oconfig_item: "
583 "Creating an OConfigValue object failed.");
584 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
585 return (NULL);
586 }
588 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
589 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
590 } /* }}} for (i = 0; i < ci->values_num; i++) */
592 /* Call OConfigItem.addChild for each child */
593 for (i = 0; i < ci->children_num; i++) /* {{{ */
594 {
595 jobject o_child;
597 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
598 if (o_child == NULL)
599 {
600 ERROR ("java plugin: ctoj_oconfig_item: "
601 "Creating an OConfigItem object failed.");
602 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
603 return (NULL);
604 }
606 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addchild, o_child);
607 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
608 } /* }}} for (i = 0; i < ci->children_num; i++) */
610 return (o_ocitem);
611 } /* }}} jobject ctoj_oconfig_item */
613 /* Convert a data_set_t to a org/collectd/api/DataSet */
614 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
615 {
616 jclass c_dataset;
617 jmethodID m_constructor;
618 jmethodID m_add;
619 jobject o_type;
620 jobject o_dataset;
621 size_t i;
623 /* Look up the org/collectd/api/DataSet class */
624 c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet");
625 if (c_dataset == NULL)
626 {
627 ERROR ("java plugin: ctoj_data_set: Looking up the "
628 "org/collectd/api/DataSet class failed.");
629 return (NULL);
630 }
632 /* Search for the `DataSet (String type)' constructor. */
633 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
634 c_dataset, "<init>", "(Ljava/lang/String;)V");
635 if (m_constructor == NULL)
636 {
637 ERROR ("java plugin: ctoj_data_set: Looking up the "
638 "`DataSet (String)' constructor failed.");
639 return (NULL);
640 }
642 /* Search for the `void addDataSource (DataSource)' method. */
643 m_add = (*jvm_env)->GetMethodID (jvm_env,
644 c_dataset, "addDataSource", "(Lorg/collectd/api/DataSource;)V");
645 if (m_add == NULL)
646 {
647 ERROR ("java plugin: ctoj_data_set: Looking up the "
648 "`addDataSource (DataSource)' method failed.");
649 return (NULL);
650 }
652 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
653 if (o_type == NULL)
654 {
655 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
656 return (NULL);
657 }
659 o_dataset = (*jvm_env)->NewObject (jvm_env,
660 c_dataset, m_constructor, o_type);
661 if (o_dataset == NULL)
662 {
663 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
664 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
665 return (NULL);
666 }
668 /* Decrease reference counter on the java.lang.String object. */
669 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
671 for (i = 0; i < ds->ds_num; i++)
672 {
673 jobject o_datasource;
675 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
676 if (o_datasource == NULL)
677 {
678 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
679 ds->type, ds->ds[i].name);
680 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
681 return (NULL);
682 }
684 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
686 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
687 } /* for (i = 0; i < ds->ds_num; i++) */
689 return (o_dataset);
690 } /* }}} jobject ctoj_data_set */
692 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
693 value_t value, int ds_type,
694 jclass class_ptr, jobject object_ptr)
695 {
696 jmethodID m_addvalue;
697 jobject o_number;
699 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
700 "addValue", "(Ljava/lang/Number;)V");
701 if (m_addvalue == NULL)
702 {
703 ERROR ("java plugin: ctoj_value_list_add_value: "
704 "Cannot find method `void addValue (Number)'.");
705 return (-1);
706 }
708 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
709 if (o_number == NULL)
710 {
711 ERROR ("java plugin: ctoj_value_list_add_value: "
712 "ctoj_value_to_number failed.");
713 return (-1);
714 }
716 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
718 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
720 return (0);
721 } /* }}} int ctoj_value_list_add_value */
723 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
724 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
725 {
726 jmethodID m_setdataset;
727 jobject o_dataset;
729 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
730 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
731 "setDataSet", "(Lorg/collectd/api/DataSet;)V");
732 if (m_setdataset == NULL)
733 {
734 ERROR ("java plugin: ctoj_value_list_add_data_set: "
735 "Cannot find the `void setDataSet (DataSet)' method.");
736 return (-1);
737 }
739 /* Create a DataSet object. */
740 o_dataset = ctoj_data_set (jvm_env, ds);
741 if (o_dataset == NULL)
742 {
743 ERROR ("java plugin: ctoj_value_list_add_data_set: "
744 "ctoj_data_set (%s) failed.", ds->type);
745 return (-1);
746 }
748 /* Actually call the method. */
749 (*jvm_env)->CallVoidMethod (jvm_env,
750 o_valuelist, m_setdataset, o_dataset);
752 /* Decrease reference counter on the List<DataSource> object. */
753 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
755 return (0);
756 } /* }}} int ctoj_value_list_add_data_set */
758 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
759 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
760 const data_set_t *ds, const value_list_t *vl)
761 {
762 jclass c_valuelist;
763 jmethodID m_valuelist_constructor;
764 jobject o_valuelist;
765 int status;
766 size_t i;
768 /* First, create a new ValueList instance..
769 * Look up the class.. */
770 c_valuelist = (*jvm_env)->FindClass (jvm_env,
771 "org/collectd/api/ValueList");
772 if (c_valuelist == NULL)
773 {
774 ERROR ("java plugin: ctoj_value_list: "
775 "FindClass (org/collectd/api/ValueList) failed.");
776 return (NULL);
777 }
779 /* Lookup the `ValueList ()' constructor. */
780 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
781 "<init>", "()V");
782 if (m_valuelist_constructor == NULL)
783 {
784 ERROR ("java plugin: ctoj_value_list: Cannot find the "
785 "`ValueList ()' constructor.");
786 return (NULL);
787 }
789 /* Create a new instance. */
790 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
791 m_valuelist_constructor);
792 if (o_valuelist == NULL)
793 {
794 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
795 "failed.");
796 return (NULL);
797 }
799 status = ctoj_value_list_add_data_set (jvm_env,
800 c_valuelist, o_valuelist, ds);
801 if (status != 0)
802 {
803 ERROR ("java plugin: ctoj_value_list: "
804 "ctoj_value_list_add_data_set failed.");
805 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
806 return (NULL);
807 }
809 /* Set the strings.. */
810 #define SET_STRING(str,method_name) do { \
811 status = ctoj_string (jvm_env, str, \
812 c_valuelist, o_valuelist, method_name); \
813 if (status != 0) { \
814 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
815 method_name); \
816 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
817 return (NULL); \
818 } } while (0)
820 SET_STRING (vl->host, "setHost");
821 SET_STRING (vl->plugin, "setPlugin");
822 SET_STRING (vl->plugin_instance, "setPluginInstance");
823 SET_STRING (vl->type, "setType");
824 SET_STRING (vl->type_instance, "setTypeInstance");
826 #undef SET_STRING
828 /* Set the `time' member. Java stores time in milliseconds. */
829 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (vl->time),
830 c_valuelist, o_valuelist, "setTime");
831 if (status != 0)
832 {
833 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
834 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
835 return (NULL);
836 }
838 /* Set the `interval' member.. */
839 status = ctoj_long (jvm_env,
840 (jlong) CDTIME_T_TO_MS (vl->interval),
841 c_valuelist, o_valuelist, "setInterval");
842 if (status != 0)
843 {
844 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
845 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
846 return (NULL);
847 }
849 for (i = 0; i < vl->values_len; i++)
850 {
851 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
852 c_valuelist, o_valuelist);
853 if (status != 0)
854 {
855 ERROR ("java plugin: ctoj_value_list: "
856 "ctoj_value_list_add_value failed.");
857 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
858 return (NULL);
859 }
860 }
862 return (o_valuelist);
863 } /* }}} jobject ctoj_value_list */
865 /* Convert a notification_t to a org/collectd/api/Notification */
866 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
867 const notification_t *n)
868 {
869 jclass c_notification;
870 jmethodID m_constructor;
871 jobject o_notification;
872 int status;
874 /* First, create a new Notification instance..
875 * Look up the class.. */
876 c_notification = (*jvm_env)->FindClass (jvm_env,
877 "org/collectd/api/Notification");
878 if (c_notification == NULL)
879 {
880 ERROR ("java plugin: ctoj_notification: "
881 "FindClass (org/collectd/api/Notification) failed.");
882 return (NULL);
883 }
885 /* Lookup the `Notification ()' constructor. */
886 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
887 "<init>", "()V");
888 if (m_constructor == NULL)
889 {
890 ERROR ("java plugin: ctoj_notification: Cannot find the "
891 "`Notification ()' constructor.");
892 return (NULL);
893 }
895 /* Create a new instance. */
896 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
897 m_constructor);
898 if (o_notification == NULL)
899 {
900 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
901 "instance failed.");
902 return (NULL);
903 }
905 /* Set the strings.. */
906 #define SET_STRING(str,method_name) do { \
907 status = ctoj_string (jvm_env, str, \
908 c_notification, o_notification, method_name); \
909 if (status != 0) { \
910 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
911 method_name); \
912 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
913 return (NULL); \
914 } } while (0)
916 SET_STRING (n->host, "setHost");
917 SET_STRING (n->plugin, "setPlugin");
918 SET_STRING (n->plugin_instance, "setPluginInstance");
919 SET_STRING (n->type, "setType");
920 SET_STRING (n->type_instance, "setTypeInstance");
921 SET_STRING (n->message, "setMessage");
923 #undef SET_STRING
925 /* Set the `time' member. Java stores time in milliseconds. */
926 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (n->time),
927 c_notification, o_notification, "setTime");
928 if (status != 0)
929 {
930 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
931 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
932 return (NULL);
933 }
935 /* Set the `severity' member.. */
936 status = ctoj_int (jvm_env, (jint) n->severity,
937 c_notification, o_notification, "setSeverity");
938 if (status != 0)
939 {
940 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
941 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
942 return (NULL);
943 }
945 return (o_notification);
946 } /* }}} jobject ctoj_notification */
948 /*
949 * Java to C conversion functions
950 */
951 /* Call a `String <method> ()' method. */
952 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
953 char *buffer, size_t buffer_size, int empty_okay,
954 jclass class_ptr, jobject object_ptr, const char *method_name)
955 {
956 jmethodID method_id;
957 jobject string_obj;
958 const char *c_str;
960 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
961 method_name, "()Ljava/lang/String;");
962 if (method_id == NULL)
963 {
964 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
965 method_name);
966 return (-1);
967 }
969 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
970 if ((string_obj == NULL) && (empty_okay == 0))
971 {
972 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
973 method_name);
974 return (-1);
975 }
976 else if ((string_obj == NULL) && (empty_okay != 0))
977 {
978 memset (buffer, 0, buffer_size);
979 return (0);
980 }
982 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
983 if (c_str == NULL)
984 {
985 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
986 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
987 return (-1);
988 }
990 sstrncpy (buffer, c_str, buffer_size);
992 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
993 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
995 return (0);
996 } /* }}} int jtoc_string */
998 /* Call an `int <method> ()' method. */
999 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
1000 jint *ret_value,
1001 jclass class_ptr, jobject object_ptr, const char *method_name)
1002 {
1003 jmethodID method_id;
1005 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1006 method_name, "()I");
1007 if (method_id == NULL)
1008 {
1009 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
1010 method_name);
1011 return (-1);
1012 }
1014 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
1016 return (0);
1017 } /* }}} int jtoc_int */
1019 /* Call a `long <method> ()' method. */
1020 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
1021 jlong *ret_value,
1022 jclass class_ptr, jobject object_ptr, const char *method_name)
1023 {
1024 jmethodID method_id;
1026 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1027 method_name, "()J");
1028 if (method_id == NULL)
1029 {
1030 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1031 method_name);
1032 return (-1);
1033 }
1035 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1037 return (0);
1038 } /* }}} int jtoc_long */
1040 /* Call a `double <method> ()' method. */
1041 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1042 jdouble *ret_value,
1043 jclass class_ptr, jobject object_ptr, const char *method_name)
1044 {
1045 jmethodID method_id;
1047 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1048 method_name, "()D");
1049 if (method_id == NULL)
1050 {
1051 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1052 method_name);
1053 return (-1);
1054 }
1056 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1058 return (0);
1059 } /* }}} int jtoc_double */
1061 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1062 value_t *ret_value, int ds_type, jobject object_ptr)
1063 {
1064 jclass class_ptr;
1065 int status;
1067 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1069 if (ds_type == DS_TYPE_GAUGE)
1070 {
1071 jdouble tmp_double;
1073 status = jtoc_double (jvm_env, &tmp_double,
1074 class_ptr, object_ptr, "doubleValue");
1075 if (status != 0)
1076 {
1077 ERROR ("java plugin: jtoc_value: "
1078 "jtoc_double failed.");
1079 return (-1);
1080 }
1081 (*ret_value).gauge = (gauge_t) tmp_double;
1082 }
1083 else
1084 {
1085 jlong tmp_long;
1087 status = jtoc_long (jvm_env, &tmp_long,
1088 class_ptr, object_ptr, "longValue");
1089 if (status != 0)
1090 {
1091 ERROR ("java plugin: jtoc_value: "
1092 "jtoc_long failed.");
1093 return (-1);
1094 }
1096 if (ds_type == DS_TYPE_DERIVE)
1097 (*ret_value).derive = (derive_t) tmp_long;
1098 else if (ds_type == DS_TYPE_ABSOLUTE)
1099 (*ret_value).absolute = (absolute_t) tmp_long;
1100 else
1101 (*ret_value).counter = (counter_t) tmp_long;
1102 }
1104 return (0);
1105 } /* }}} int jtoc_value */
1107 /* Read a List<Number>, convert it to `value_t' and add it to the given
1108 * `value_list_t'. */
1109 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1110 const data_set_t *ds, value_list_t *vl,
1111 jclass class_ptr, jobject object_ptr)
1112 {
1113 jmethodID m_getvalues;
1114 jmethodID m_toarray;
1115 jobject o_list;
1116 jobjectArray o_number_array;
1118 value_t *values;
1119 int values_num;
1120 int i;
1122 values_num = ds->ds_num;
1124 values = NULL;
1125 o_number_array = NULL;
1126 o_list = NULL;
1128 #define BAIL_OUT(status) \
1129 free (values); \
1130 if (o_number_array != NULL) \
1131 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1132 if (o_list != NULL) \
1133 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1134 return (status);
1136 /* Call: List<Number> ValueList.getValues () */
1137 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1138 "getValues", "()Ljava/util/List;");
1139 if (m_getvalues == NULL)
1140 {
1141 ERROR ("java plugin: jtoc_values_array: "
1142 "Cannot find method `List getValues ()'.");
1143 BAIL_OUT (-1);
1144 }
1146 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1147 if (o_list == NULL)
1148 {
1149 ERROR ("java plugin: jtoc_values_array: "
1150 "CallObjectMethod (getValues) failed.");
1151 BAIL_OUT (-1);
1152 }
1154 /* Call: Number[] List.toArray () */
1155 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1156 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1157 "toArray", "()[Ljava/lang/Object;");
1158 if (m_toarray == NULL)
1159 {
1160 ERROR ("java plugin: jtoc_values_array: "
1161 "Cannot find method `Object[] toArray ()'.");
1162 BAIL_OUT (-1);
1163 }
1165 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1166 if (o_number_array == NULL)
1167 {
1168 ERROR ("java plugin: jtoc_values_array: "
1169 "CallObjectMethod (toArray) failed.");
1170 BAIL_OUT (-1);
1171 }
1173 values = (value_t *) calloc (values_num, sizeof (value_t));
1174 if (values == NULL)
1175 {
1176 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1177 BAIL_OUT (-1);
1178 }
1180 for (i = 0; i < values_num; i++)
1181 {
1182 jobject o_number;
1183 int status;
1185 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1186 o_number_array, (jsize) i);
1187 if (o_number == NULL)
1188 {
1189 ERROR ("java plugin: jtoc_values_array: "
1190 "GetObjectArrayElement (%i) failed.", i);
1191 BAIL_OUT (-1);
1192 }
1194 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1195 if (status != 0)
1196 {
1197 ERROR ("java plugin: jtoc_values_array: "
1198 "jtoc_value (%i) failed.", i);
1199 BAIL_OUT (-1);
1200 }
1201 } /* for (i = 0; i < values_num; i++) */
1203 vl->values = values;
1204 vl->values_len = values_num;
1206 #undef BAIL_OUT
1207 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1208 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1209 return (0);
1210 } /* }}} int jtoc_values_array */
1212 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1213 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1214 jobject object_ptr)
1215 {
1216 jclass class_ptr;
1217 int status;
1218 jlong tmp_long;
1219 const data_set_t *ds;
1221 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1222 if (class_ptr == NULL)
1223 {
1224 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1225 return (-1);
1226 }
1228 /* eo == empty okay */
1229 #define SET_STRING(buffer,method, eo) do { \
1230 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1231 class_ptr, object_ptr, method); \
1232 if (status != 0) { \
1233 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1234 method); \
1235 return (-1); \
1236 } } while (0)
1238 SET_STRING(vl->type, "getType", /* empty = */ 0);
1240 ds = plugin_get_ds (vl->type);
1241 if (ds == NULL)
1242 {
1243 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1244 "Please consult the types.db(5) manpage for mor information.",
1245 vl->type);
1246 return (-1);
1247 }
1249 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1250 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1251 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1252 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1254 #undef SET_STRING
1256 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1257 if (status != 0)
1258 {
1259 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1260 return (-1);
1261 }
1262 /* Java measures time in milliseconds. */
1263 vl->time = MS_TO_CDTIME_T (tmp_long);
1265 status = jtoc_long (jvm_env, &tmp_long,
1266 class_ptr, object_ptr, "getInterval");
1267 if (status != 0)
1268 {
1269 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1270 return (-1);
1271 }
1272 vl->interval = MS_TO_CDTIME_T (tmp_long);
1274 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1275 if (status != 0)
1276 {
1277 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1278 return (-1);
1279 }
1281 return (0);
1282 } /* }}} int jtoc_value_list */
1284 /* Convert a org/collectd/api/Notification to a notification_t. */
1285 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1286 jobject object_ptr)
1287 {
1288 jclass class_ptr;
1289 int status;
1290 jlong tmp_long;
1291 jint tmp_int;
1293 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1294 if (class_ptr == NULL)
1295 {
1296 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1297 return (-1);
1298 }
1300 /* eo == empty okay */
1301 #define SET_STRING(buffer,method, eo) do { \
1302 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1303 class_ptr, object_ptr, method); \
1304 if (status != 0) { \
1305 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1306 method); \
1307 return (-1); \
1308 } } while (0)
1310 SET_STRING (n->host, "getHost", /* empty = */ 1);
1311 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1312 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1313 SET_STRING (n->type, "getType", /* empty = */ 1);
1314 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1315 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1317 #undef SET_STRING
1319 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1320 if (status != 0)
1321 {
1322 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1323 return (-1);
1324 }
1325 /* Java measures time in milliseconds. */
1326 n->time = MS_TO_CDTIME_T(tmp_long);
1328 status = jtoc_int (jvm_env, &tmp_int,
1329 class_ptr, object_ptr, "getSeverity");
1330 if (status != 0)
1331 {
1332 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1333 return (-1);
1334 }
1335 n->severity = (int) tmp_int;
1337 return (0);
1338 } /* }}} int jtoc_notification */
1339 /*
1340 * Functions accessible from Java
1341 */
1342 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1343 jobject this, jobject java_vl)
1344 {
1345 value_list_t vl = VALUE_LIST_INIT;
1346 int status;
1348 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1350 status = jtoc_value_list (jvm_env, &vl, java_vl);
1351 if (status != 0)
1352 {
1353 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1354 return (-1);
1355 }
1357 status = plugin_dispatch_values (&vl);
1359 sfree (vl.values);
1361 return (status);
1362 } /* }}} jint cjni_api_dispatch_values */
1364 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1365 jobject this, jobject o_notification)
1366 {
1367 notification_t n = { 0 };
1368 int status;
1370 status = jtoc_notification (jvm_env, &n, o_notification);
1371 if (status != 0)
1372 {
1373 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1374 return (-1);
1375 }
1377 status = plugin_dispatch_notification (&n);
1379 return (status);
1380 } /* }}} jint cjni_api_dispatch_notification */
1382 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1383 jobject this, jobject o_string_type)
1384 {
1385 const char *ds_name;
1386 const data_set_t *ds;
1387 jobject o_dataset;
1389 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1390 if (ds_name == NULL)
1391 {
1392 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1393 return (NULL);
1394 }
1396 ds = plugin_get_ds (ds_name);
1397 DEBUG ("java plugin: cjni_api_get_ds: "
1398 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1400 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1402 if (ds == NULL)
1403 return (NULL);
1405 o_dataset = ctoj_data_set (jvm_env, ds);
1406 return (o_dataset);
1407 } /* }}} jint cjni_api_get_ds */
1409 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1410 jobject this, jobject o_name, jobject o_config)
1411 {
1412 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1413 } /* }}} jint cjni_api_register_config */
1415 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1416 jobject this, jobject o_name, jobject o_config)
1417 {
1418 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1419 } /* }}} jint cjni_api_register_init */
1421 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1422 jobject this, jobject o_name, jobject o_read)
1423 {
1424 user_data_t ud = { 0 };
1425 cjni_callback_info_t *cbi;
1427 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1428 if (cbi == NULL)
1429 return (-1);
1431 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1433 ud.data = (void *) cbi;
1434 ud.free_func = cjni_callback_info_destroy;
1436 plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read,
1437 /* interval = */ 0, &ud);
1439 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1441 return (0);
1442 } /* }}} jint cjni_api_register_read */
1444 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1445 jobject this, jobject o_name, jobject o_write)
1446 {
1447 user_data_t ud = { 0 };
1448 cjni_callback_info_t *cbi;
1450 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1451 if (cbi == NULL)
1452 return (-1);
1454 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1456 ud.data = (void *) cbi;
1457 ud.free_func = cjni_callback_info_destroy;
1459 plugin_register_write (cbi->name, cjni_write, &ud);
1461 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1463 return (0);
1464 } /* }}} jint cjni_api_register_write */
1466 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1467 jobject this, jobject o_name, jobject o_flush)
1468 {
1469 user_data_t ud = { 0 };
1470 cjni_callback_info_t *cbi;
1472 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1473 if (cbi == NULL)
1474 return (-1);
1476 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1478 ud.data = (void *) cbi;
1479 ud.free_func = cjni_callback_info_destroy;
1481 plugin_register_flush (cbi->name, cjni_flush, &ud);
1483 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1485 return (0);
1486 } /* }}} jint cjni_api_register_flush */
1488 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1489 jobject this, jobject o_name, jobject o_shutdown)
1490 {
1491 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1492 CB_TYPE_SHUTDOWN));
1493 } /* }}} jint cjni_api_register_shutdown */
1495 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1496 jobject this, jobject o_name, jobject o_log)
1497 {
1498 user_data_t ud = { 0 };
1499 cjni_callback_info_t *cbi;
1501 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1502 if (cbi == NULL)
1503 return (-1);
1505 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1507 ud.data = (void *) cbi;
1508 ud.free_func = cjni_callback_info_destroy;
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 user_data_t ud = { 0 };
1521 cjni_callback_info_t *cbi;
1523 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1524 CB_TYPE_NOTIFICATION);
1525 if (cbi == NULL)
1526 return (-1);
1528 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1530 ud.data = (void *) cbi;
1531 ud.free_func = cjni_callback_info_destroy;
1533 plugin_register_notification (cbi->name, cjni_notification, &ud);
1535 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1537 return (0);
1538 } /* }}} jint cjni_api_register_notification */
1540 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1541 jobject this, jobject o_name, jobject o_match, int type)
1542 {
1543 int status;
1544 const char *c_name;
1546 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1547 if (c_name == NULL)
1548 {
1549 ERROR ("java plugin: cjni_api_register_match_target: "
1550 "GetStringUTFChars failed.");
1551 return (-1);
1552 }
1554 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1555 if (status != 0)
1556 {
1557 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1558 return (-1);
1559 }
1561 if (type == CB_TYPE_MATCH)
1562 {
1563 match_proc_t m_proc = { 0 };
1565 m_proc.create = cjni_match_target_create;
1566 m_proc.destroy = cjni_match_target_destroy;
1567 m_proc.match = (void *) cjni_match_target_invoke;
1569 status = fc_register_match (c_name, m_proc);
1570 }
1571 else if (type == CB_TYPE_TARGET)
1572 {
1573 target_proc_t t_proc = { 0 };
1575 t_proc.create = cjni_match_target_create;
1576 t_proc.destroy = cjni_match_target_destroy;
1577 t_proc.invoke = cjni_match_target_invoke;
1579 status = fc_register_target (c_name, t_proc);
1580 }
1581 else
1582 {
1583 ERROR ("java plugin: cjni_api_register_match_target: "
1584 "Don't know whether to create a match or a target.");
1585 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1586 return (-1);
1587 }
1589 if (status != 0)
1590 {
1591 ERROR ("java plugin: cjni_api_register_match_target: "
1592 "%s failed.",
1593 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1594 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1595 return (-1);
1596 }
1598 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1600 return (0);
1601 } /* }}} jint cjni_api_register_match_target */
1603 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1604 jobject this, jobject o_name, jobject o_match)
1605 {
1606 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1607 CB_TYPE_MATCH));
1608 } /* }}} jint cjni_api_register_match */
1610 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1611 jobject this, jobject o_name, jobject o_target)
1612 {
1613 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1614 CB_TYPE_TARGET));
1615 } /* }}} jint cjni_api_register_target */
1617 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1618 jobject this, jint severity, jobject o_message)
1619 {
1620 const char *c_str;
1622 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1623 if (c_str == NULL)
1624 {
1625 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1626 return;
1627 }
1629 if (severity < LOG_ERR)
1630 severity = LOG_ERR;
1631 if (severity > LOG_DEBUG)
1632 severity = LOG_DEBUG;
1634 plugin_log (severity, "%s", c_str);
1636 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1637 } /* }}} void cjni_api_log */
1639 static jstring JNICALL cjni_api_get_hostname (JNIEnv *jvm_env, jobject this)
1640 {
1641 return ctoj_output_string(jvm_env, hostname_g);
1642 }
1644 /* List of ``native'' functions, i. e. C-functions that can be called from
1645 * Java. */
1646 static JNINativeMethod jni_api_functions[] = /* {{{ */
1647 {
1648 { "dispatchValues",
1649 "(Lorg/collectd/api/ValueList;)I",
1650 cjni_api_dispatch_values },
1652 { "dispatchNotification",
1653 "(Lorg/collectd/api/Notification;)I",
1654 cjni_api_dispatch_notification },
1656 { "getDS",
1657 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1658 cjni_api_get_ds },
1660 { "registerConfig",
1661 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1662 cjni_api_register_config },
1664 { "registerInit",
1665 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1666 cjni_api_register_init },
1668 { "registerRead",
1669 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1670 cjni_api_register_read },
1672 { "registerWrite",
1673 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1674 cjni_api_register_write },
1676 { "registerFlush",
1677 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1678 cjni_api_register_flush },
1680 { "registerShutdown",
1681 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1682 cjni_api_register_shutdown },
1684 { "registerLog",
1685 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1686 cjni_api_register_log },
1688 { "registerNotification",
1689 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1690 cjni_api_register_notification },
1692 { "registerMatch",
1693 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1694 cjni_api_register_match },
1696 { "registerTarget",
1697 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1698 cjni_api_register_target },
1700 { "log",
1701 "(ILjava/lang/String;)V",
1702 cjni_api_log },
1704 { "getHostname",
1705 "()Ljava/lang/String;",
1706 cjni_api_get_hostname },
1708 };
1709 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1710 / sizeof (jni_api_functions[0]);
1711 /* }}} */
1713 /*
1714 * Functions
1715 */
1716 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1717 * all registration functions. */
1718 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1719 jobject o_name, jobject o_callback, int type)
1720 {
1721 const char *c_name;
1722 cjni_callback_info_t *cbi;
1723 const char *method_name;
1724 const char *method_signature;
1726 switch (type)
1727 {
1728 case CB_TYPE_CONFIG:
1729 method_name = "config";
1730 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1731 break;
1733 case CB_TYPE_INIT:
1734 method_name = "init";
1735 method_signature = "()I";
1736 break;
1738 case CB_TYPE_READ:
1739 method_name = "read";
1740 method_signature = "()I";
1741 break;
1743 case CB_TYPE_WRITE:
1744 method_name = "write";
1745 method_signature = "(Lorg/collectd/api/ValueList;)I";
1746 break;
1748 case CB_TYPE_FLUSH:
1749 method_name = "flush";
1750 method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I";
1751 break;
1753 case CB_TYPE_SHUTDOWN:
1754 method_name = "shutdown";
1755 method_signature = "()I";
1756 break;
1758 case CB_TYPE_LOG:
1759 method_name = "log";
1760 method_signature = "(ILjava/lang/String;)V";
1761 break;
1763 case CB_TYPE_NOTIFICATION:
1764 method_name = "notification";
1765 method_signature = "(Lorg/collectd/api/Notification;)I";
1766 break;
1768 case CB_TYPE_MATCH:
1769 method_name = "createMatch";
1770 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1771 "Lorg/collectd/api/CollectdMatchInterface;";
1772 break;
1774 case CB_TYPE_TARGET:
1775 method_name = "createTarget";
1776 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1777 "Lorg/collectd/api/CollectdTargetInterface;";
1778 break;
1780 default:
1781 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1782 type);
1783 return (NULL);
1784 }
1786 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1787 if (c_name == NULL)
1788 {
1789 ERROR ("java plugin: cjni_callback_info_create: "
1790 "GetStringUTFChars failed.");
1791 return (NULL);
1792 }
1794 cbi = calloc (1, sizeof (*cbi));
1795 if (cbi == NULL)
1796 {
1797 ERROR ("java plugin: cjni_callback_info_create: calloc failed.");
1798 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1799 return (NULL);
1800 }
1801 cbi->type = type;
1803 cbi->name = strdup (c_name);
1804 if (cbi->name == NULL)
1805 {
1806 pthread_mutex_unlock (&java_callbacks_lock);
1807 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1808 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1809 sfree (cbi);
1810 return (NULL);
1811 }
1813 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1815 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1816 if (cbi->object == NULL)
1817 {
1818 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1819 sfree (cbi->name);
1820 sfree (cbi);
1821 return (NULL);
1822 }
1824 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1825 if (cbi->class == NULL)
1826 {
1827 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1828 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1829 sfree (cbi->name);
1830 sfree (cbi);
1831 return (NULL);
1832 }
1834 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1835 method_name, method_signature);
1836 if (cbi->method == NULL)
1837 {
1838 ERROR ("java plugin: cjni_callback_info_create: "
1839 "Cannot find the `%s' method with signature `%s'.",
1840 method_name, method_signature);
1841 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1842 sfree (cbi->name);
1843 sfree (cbi);
1844 return (NULL);
1845 }
1847 return (cbi);
1848 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1850 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1851 * to the global `java_callbacks' variable. This is used for `config', `init',
1852 * and `shutdown' callbacks. */
1853 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1854 jobject o_name, jobject o_callback, int type)
1855 {
1856 cjni_callback_info_t *cbi;
1857 cjni_callback_info_t *tmp;
1858 #if COLLECT_DEBUG
1859 const char *type_str;
1860 #endif
1862 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1863 if (cbi == NULL)
1864 return (-1);
1866 #if COLLECT_DEBUG
1867 switch (type)
1868 {
1869 case CB_TYPE_CONFIG:
1870 type_str = "config";
1871 break;
1873 case CB_TYPE_INIT:
1874 type_str = "init";
1875 break;
1877 case CB_TYPE_SHUTDOWN:
1878 type_str = "shutdown";
1879 break;
1881 case CB_TYPE_MATCH:
1882 type_str = "match";
1883 break;
1885 case CB_TYPE_TARGET:
1886 type_str = "target";
1887 break;
1889 default:
1890 type_str = "<unknown>";
1891 }
1892 DEBUG ("java plugin: Registering new %s callback: %s",
1893 type_str, cbi->name);
1894 #endif
1896 pthread_mutex_lock (&java_callbacks_lock);
1898 tmp = realloc (java_callbacks,
1899 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1900 if (tmp == NULL)
1901 {
1902 pthread_mutex_unlock (&java_callbacks_lock);
1903 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1905 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1906 free (cbi);
1908 return (-1);
1909 }
1910 java_callbacks = tmp;
1911 java_callbacks[java_callbacks_num] = *cbi;
1912 java_callbacks_num++;
1914 pthread_mutex_unlock (&java_callbacks_lock);
1916 free (cbi);
1917 return (0);
1918 } /* }}} int cjni_callback_register */
1920 /* Callback for `pthread_key_create'. It frees the data contained in
1921 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1922 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1923 {
1924 cjni_jvm_env_t *cjni_env;
1926 if (args == NULL)
1927 return;
1929 cjni_env = (cjni_jvm_env_t *) args;
1931 if (cjni_env->reference_counter > 0)
1932 {
1933 ERROR ("java plugin: cjni_jvm_env_destroy: "
1934 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1935 }
1937 if (cjni_env->jvm_env != NULL)
1938 {
1939 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1940 (void *) cjni_env->jvm_env);
1941 }
1943 /* The pointer is allocated in `cjni_thread_attach' */
1944 free (cjni_env);
1945 } /* }}} void cjni_jvm_env_destroy */
1947 /* Register ``native'' functions with the JVM. Native functions are C-functions
1948 * that can be called by Java code. */
1949 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1950 {
1951 jclass api_class_ptr;
1952 int status;
1954 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1955 if (api_class_ptr == NULL)
1956 {
1957 ERROR ("cjni_init_native: Cannot find the API class \"org.collectd.api"
1958 ".Collectd\". Please set the correct class path "
1959 "using 'JVMArg \"-Djava.class.path=...\"'.");
1960 return (-1);
1961 }
1963 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1964 jni_api_functions, (jint) jni_api_functions_num);
1965 if (status != 0)
1966 {
1967 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1968 return (-1);
1969 }
1971 return (0);
1972 } /* }}} int cjni_init_native */
1974 /* Create the JVM. This is called when the first thread tries to access the JVM
1975 * via cjni_thread_attach. */
1976 static int cjni_create_jvm (void) /* {{{ */
1977 {
1978 JNIEnv *jvm_env;
1979 JavaVMInitArgs vm_args = { 0 };
1980 JavaVMOption vm_options[jvm_argc];
1982 int status;
1983 size_t i;
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 (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 size_t i;
2220 for (i = 0; class->name[i] != 0; i++)
2221 if (class->name[i] == '.')
2222 class->name[i] = '/';
2223 }
2225 DEBUG ("java plugin: Loading class %s", class->name);
2227 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2228 if (class->class == NULL)
2229 {
2230 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2231 class->name);
2232 cjni_thread_detach ();
2233 free (class->name);
2234 return (-1);
2235 }
2237 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2238 "<init>", "()V");
2239 if (constructor_id == NULL)
2240 {
2241 ERROR ("java plugin: cjni_config_load_plugin: "
2242 "Could not find the constructor for `%s'.",
2243 class->name);
2244 cjni_thread_detach ();
2245 free (class->name);
2246 return (-1);
2247 }
2249 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2250 constructor_id);
2251 if (tmp_object != NULL)
2252 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2253 else
2254 class->object = NULL;
2255 if (class->object == NULL)
2256 {
2257 ERROR ("java plugin: cjni_config_load_plugin: "
2258 "Could create a new `%s' object.",
2259 class->name);
2260 cjni_thread_detach ();
2261 free (class->name);
2262 return (-1);
2263 }
2265 cjni_thread_detach ();
2267 java_classes_list_len++;
2269 return (0);
2270 } /* }}} int cjni_config_load_plugin */
2272 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2273 {
2274 JNIEnv *jvm_env;
2275 cjni_callback_info_t *cbi;
2276 jobject o_ocitem;
2277 const char *name;
2278 size_t i;
2280 jclass class;
2281 jmethodID method;
2283 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2284 {
2285 WARNING ("java plugin: `Plugin' blocks "
2286 "need exactly one string argument.");
2287 return (-1);
2288 }
2290 name = ci->values[0].value.string;
2292 cbi = NULL;
2293 for (i = 0; i < java_callbacks_num; i++)
2294 {
2295 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2296 continue;
2298 if (strcmp (name, java_callbacks[i].name) != 0)
2299 continue;
2301 cbi = java_callbacks + i;
2302 break;
2303 }
2305 if (cbi == NULL)
2306 {
2307 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2308 "configuration callback has been registered. Please make sure, the "
2309 "`LoadPlugin' lines precede the `Plugin' blocks.",
2310 name);
2311 return (0);
2312 }
2314 DEBUG ("java plugin: Configuring %s", name);
2316 jvm_env = cjni_thread_attach ();
2317 if (jvm_env == NULL)
2318 return (-1);
2320 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2321 if (o_ocitem == NULL)
2322 {
2323 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2324 cjni_thread_detach ();
2325 return (-1);
2326 }
2328 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2329 method = (*jvm_env)->GetMethodID (jvm_env, class,
2330 "config", "(Lorg/collectd/api/OConfigItem;)I");
2332 (*jvm_env)->CallIntMethod (jvm_env,
2333 cbi->object, method, o_ocitem);
2335 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2336 cjni_thread_detach ();
2337 return (0);
2338 } /* }}} int cjni_config_plugin_block */
2340 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2341 {
2342 int success;
2343 int errors;
2344 int status;
2345 int i;
2347 success = 0;
2348 errors = 0;
2350 for (i = 0; i < ci->children_num; i++)
2351 {
2352 oconfig_item_t *child = ci->children + i;
2354 if (strcasecmp ("JVMArg", child->key) == 0)
2355 {
2356 status = cjni_config_add_jvm_arg (child);
2357 if (status == 0)
2358 success++;
2359 else
2360 errors++;
2361 }
2362 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2363 {
2364 status = cjni_config_load_plugin (child);
2365 if (status == 0)
2366 success++;
2367 else
2368 errors++;
2369 }
2370 else if (strcasecmp ("Plugin", child->key) == 0)
2371 {
2372 status = cjni_config_plugin_block (child);
2373 if (status == 0)
2374 success++;
2375 else
2376 errors++;
2377 }
2378 else
2379 {
2380 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2381 errors++;
2382 }
2383 }
2385 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2386 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2388 if ((success == 0) && (errors > 0))
2389 {
2390 ERROR ("java plugin: All statements failed.");
2391 return (-1);
2392 }
2394 return (0);
2395 } /* }}} int cjni_config_perform */
2397 /* Copy the children of `ci' to the global `config_block' variable. */
2398 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2399 {
2400 oconfig_item_t *ci_copy;
2401 oconfig_item_t *tmp;
2403 assert (ci != NULL);
2404 if (ci->children_num == 0)
2405 return (0); /* nothing to do */
2407 ci_copy = oconfig_clone (ci);
2408 if (ci_copy == NULL)
2409 {
2410 ERROR ("java plugin: oconfig_clone failed.");
2411 return (-1);
2412 }
2414 if (config_block == NULL)
2415 {
2416 config_block = ci_copy;
2417 return (0);
2418 }
2420 tmp = realloc (config_block->children,
2421 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2422 if (tmp == NULL)
2423 {
2424 ERROR ("java plugin: realloc failed.");
2425 oconfig_free (ci_copy);
2426 return (-1);
2427 }
2428 config_block->children = tmp;
2430 /* Copy the pointers */
2431 memcpy (config_block->children + config_block->children_num,
2432 ci_copy->children,
2433 ci_copy->children_num * sizeof (*ci_copy->children));
2434 config_block->children_num += ci_copy->children_num;
2436 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2437 memset (ci_copy->children, 0,
2438 ci_copy->children_num * sizeof (*ci_copy->children));
2439 ci_copy->children_num = 0;
2441 oconfig_free (ci_copy);
2443 return (0);
2444 } /* }}} int cjni_config_callback */
2446 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2447 * and `cjni_write'. In particular, delete the global reference to the Java
2448 * object. */
2449 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2450 {
2451 JNIEnv *jvm_env;
2452 cjni_callback_info_t *cbi;
2454 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2456 cbi = (cjni_callback_info_t *) arg;
2458 /* This condition can occur when shutting down. */
2459 if (jvm == NULL)
2460 {
2461 sfree (cbi);
2462 return;
2463 }
2465 if (arg == NULL)
2466 return;
2468 jvm_env = cjni_thread_attach ();
2469 if (jvm_env == NULL)
2470 {
2471 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2472 return;
2473 }
2475 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2477 cbi->method = NULL;
2478 cbi->object = NULL;
2479 cbi->class = NULL;
2480 free (cbi);
2482 cjni_thread_detach ();
2483 } /* }}} void cjni_callback_info_destroy */
2485 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2486 static int cjni_read (user_data_t *ud) /* {{{ */
2487 {
2488 JNIEnv *jvm_env;
2489 cjni_callback_info_t *cbi;
2490 int ret_status;
2492 if (jvm == NULL)
2493 {
2494 ERROR ("java plugin: cjni_read: jvm == NULL");
2495 return (-1);
2496 }
2498 if ((ud == NULL) || (ud->data == NULL))
2499 {
2500 ERROR ("java plugin: cjni_read: Invalid user data.");
2501 return (-1);
2502 }
2504 jvm_env = cjni_thread_attach ();
2505 if (jvm_env == NULL)
2506 return (-1);
2508 cbi = (cjni_callback_info_t *) ud->data;
2510 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2511 cbi->method);
2513 cjni_thread_detach ();
2514 return (ret_status);
2515 } /* }}} int cjni_read */
2517 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2518 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2519 user_data_t *ud)
2520 {
2521 JNIEnv *jvm_env;
2522 cjni_callback_info_t *cbi;
2523 jobject vl_java;
2524 int ret_status;
2526 if (jvm == NULL)
2527 {
2528 ERROR ("java plugin: cjni_write: jvm == NULL");
2529 return (-1);
2530 }
2532 if ((ud == NULL) || (ud->data == NULL))
2533 {
2534 ERROR ("java plugin: cjni_write: Invalid user data.");
2535 return (-1);
2536 }
2538 jvm_env = cjni_thread_attach ();
2539 if (jvm_env == NULL)
2540 return (-1);
2542 cbi = (cjni_callback_info_t *) ud->data;
2544 vl_java = ctoj_value_list (jvm_env, ds, vl);
2545 if (vl_java == NULL)
2546 {
2547 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2548 cjni_thread_detach ();
2549 return (-1);
2550 }
2552 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2553 cbi->object, cbi->method, vl_java);
2555 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2557 cjni_thread_detach ();
2558 return (ret_status);
2559 } /* }}} int cjni_write */
2561 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2562 static int cjni_flush (cdtime_t timeout, const char *identifier, /* {{{ */
2563 user_data_t *ud)
2564 {
2565 JNIEnv *jvm_env;
2566 cjni_callback_info_t *cbi;
2567 jobject o_timeout;
2568 jobject o_identifier;
2569 int ret_status;
2571 if (jvm == NULL)
2572 {
2573 ERROR ("java plugin: cjni_flush: jvm == NULL");
2574 return (-1);
2575 }
2577 if ((ud == NULL) || (ud->data == NULL))
2578 {
2579 ERROR ("java plugin: cjni_flush: Invalid user data.");
2580 return (-1);
2581 }
2583 jvm_env = cjni_thread_attach ();
2584 if (jvm_env == NULL)
2585 return (-1);
2587 cbi = (cjni_callback_info_t *) ud->data;
2589 o_timeout = ctoj_jdouble_to_number (jvm_env,
2590 (jdouble) CDTIME_T_TO_DOUBLE (timeout));
2591 if (o_timeout == NULL)
2592 {
2593 ERROR ("java plugin: cjni_flush: Converting double "
2594 "to Number object failed.");
2595 cjni_thread_detach ();
2596 return (-1);
2597 }
2599 o_identifier = NULL;
2600 if (identifier != NULL)
2601 {
2602 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2603 if (o_identifier == NULL)
2604 {
2605 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2606 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2607 cjni_thread_detach ();
2608 return (-1);
2609 }
2610 }
2612 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2613 cbi->object, cbi->method, o_timeout, o_identifier);
2615 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2616 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2618 cjni_thread_detach ();
2619 return (ret_status);
2620 } /* }}} int cjni_flush */
2622 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2623 static void cjni_log (int severity, const char *message, /* {{{ */
2624 user_data_t *ud)
2625 {
2626 JNIEnv *jvm_env;
2627 cjni_callback_info_t *cbi;
2628 jobject o_message;
2630 if (jvm == NULL)
2631 return;
2633 if ((ud == NULL) || (ud->data == NULL))
2634 return;
2636 jvm_env = cjni_thread_attach ();
2637 if (jvm_env == NULL)
2638 return;
2640 cbi = (cjni_callback_info_t *) ud->data;
2642 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2643 if (o_message == NULL)
2644 {
2645 cjni_thread_detach ();
2646 return;
2647 }
2649 (*jvm_env)->CallVoidMethod (jvm_env,
2650 cbi->object, cbi->method, (jint) severity, o_message);
2652 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2654 cjni_thread_detach ();
2655 } /* }}} void cjni_log */
2657 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2658 * pointer. */
2659 static int cjni_notification (const notification_t *n, /* {{{ */
2660 user_data_t *ud)
2661 {
2662 JNIEnv *jvm_env;
2663 cjni_callback_info_t *cbi;
2664 jobject o_notification;
2665 int ret_status;
2667 if (jvm == NULL)
2668 {
2669 ERROR ("java plugin: cjni_read: jvm == NULL");
2670 return (-1);
2671 }
2673 if ((ud == NULL) || (ud->data == NULL))
2674 {
2675 ERROR ("java plugin: cjni_read: Invalid user data.");
2676 return (-1);
2677 }
2679 jvm_env = cjni_thread_attach ();
2680 if (jvm_env == NULL)
2681 return (-1);
2683 cbi = (cjni_callback_info_t *) ud->data;
2685 o_notification = ctoj_notification (jvm_env, n);
2686 if (o_notification == NULL)
2687 {
2688 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2689 cjni_thread_detach ();
2690 return (-1);
2691 }
2693 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2694 cbi->object, cbi->method, o_notification);
2696 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2698 cjni_thread_detach ();
2699 return (ret_status);
2700 } /* }}} int cjni_notification */
2702 /* Callbacks for matches implemented in Java */
2703 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2704 void **user_data)
2705 {
2706 JNIEnv *jvm_env;
2707 cjni_callback_info_t *cbi_ret;
2708 cjni_callback_info_t *cbi_factory;
2709 const char *name;
2710 jobject o_ci;
2711 jobject o_tmp;
2712 int type;
2713 size_t i;
2715 cbi_ret = NULL;
2716 o_ci = NULL;
2717 jvm_env = NULL;
2719 #define BAIL_OUT(status) \
2720 if (cbi_ret != NULL) { \
2721 free (cbi_ret->name); \
2722 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2723 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2724 } \
2725 free (cbi_ret); \
2726 if (o_ci != NULL) \
2727 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2728 cjni_thread_detach (); \
2729 return (status)
2731 if (jvm == NULL)
2732 {
2733 ERROR ("java plugin: cjni_read: jvm == NULL");
2734 return (-1);
2735 }
2737 jvm_env = cjni_thread_attach ();
2738 if (jvm_env == NULL)
2739 return (-1);
2741 /* Find out whether to create a match or a target. */
2742 if (strcasecmp ("Match", ci->key) == 0)
2743 type = CB_TYPE_MATCH;
2744 else if (strcasecmp ("Target", ci->key) == 0)
2745 type = CB_TYPE_TARGET;
2746 else
2747 {
2748 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2749 "to create a match or a target.");
2750 BAIL_OUT (-1);
2751 }
2753 /* This is the name of the match we should create. */
2754 name = ci->values[0].value.string;
2756 /* Lets see if we have a matching factory here.. */
2757 cbi_factory = NULL;
2758 for (i = 0; i < java_callbacks_num; i++)
2759 {
2760 if (java_callbacks[i].type != type)
2761 continue;
2763 if (strcmp (name, java_callbacks[i].name) != 0)
2764 continue;
2766 cbi_factory = java_callbacks + i;
2767 break;
2768 }
2770 /* Nope, no factory for that name.. */
2771 if (cbi_factory == NULL)
2772 {
2773 ERROR ("java plugin: cjni_match_target_create: "
2774 "No such match factory registered: %s",
2775 name);
2776 BAIL_OUT (-1);
2777 }
2779 /* We convert `ci' to its Java equivalent.. */
2780 o_ci = ctoj_oconfig_item (jvm_env, ci);
2781 if (o_ci == NULL)
2782 {
2783 ERROR ("java plugin: cjni_match_target_create: "
2784 "ctoj_oconfig_item failed.");
2785 BAIL_OUT (-1);
2786 }
2788 /* Allocate a new callback info structure. This is going to be our user_data
2789 * pointer. */
2790 cbi_ret = calloc (1, sizeof (*cbi_ret));
2791 if (cbi_ret == NULL)
2792 {
2793 ERROR ("java plugin: cjni_match_target_create: calloc failed.");
2794 BAIL_OUT (-1);
2795 }
2797 cbi_ret->object = NULL;
2798 cbi_ret->type = type;
2800 /* Lets fill the callback info structure.. First, the name: */
2801 cbi_ret->name = strdup (name);
2802 if (cbi_ret->name == NULL)
2803 {
2804 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2805 BAIL_OUT (-1);
2806 }
2808 /* Then call the factory method so it creates a new object for us. */
2809 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2810 cbi_factory->object, cbi_factory->method, o_ci);
2811 if (o_tmp == NULL)
2812 {
2813 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2814 BAIL_OUT (-1);
2815 }
2817 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2818 if (o_tmp == NULL)
2819 {
2820 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2821 BAIL_OUT (-1);
2822 }
2824 /* This is the class of the match. It is possibly different from the class of
2825 * the match-factory! */
2826 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2827 if (cbi_ret->class == NULL)
2828 {
2829 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2830 BAIL_OUT (-1);
2831 }
2833 /* Lookup the `int match (DataSet, ValueList)' method. */
2834 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2835 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2836 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2837 if (cbi_ret->method == NULL)
2838 {
2839 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2840 BAIL_OUT (-1);
2841 }
2843 /* Return the newly created match via the user_data pointer. */
2844 *user_data = (void *) cbi_ret;
2846 cjni_thread_detach ();
2848 DEBUG ("java plugin: cjni_match_target_create: "
2849 "Successfully created a `%s' %s.",
2850 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2852 /* Success! */
2853 return (0);
2854 #undef BAIL_OUT
2855 } /* }}} int cjni_match_target_create */
2857 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2858 {
2859 cjni_callback_info_destroy (*user_data);
2860 *user_data = NULL;
2862 return (0);
2863 } /* }}} int cjni_match_target_destroy */
2865 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2866 value_list_t *vl, notification_meta_t **meta, void **user_data)
2867 {
2868 JNIEnv *jvm_env;
2869 cjni_callback_info_t *cbi;
2870 jobject o_vl;
2871 jobject o_ds;
2872 int ret_status;
2873 int status;
2875 if (jvm == NULL)
2876 {
2877 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2878 return (-1);
2879 }
2881 jvm_env = cjni_thread_attach ();
2882 if (jvm_env == NULL)
2883 return (-1);
2885 cbi = (cjni_callback_info_t *) *user_data;
2887 o_vl = ctoj_value_list (jvm_env, ds, vl);
2888 if (o_vl == NULL)
2889 {
2890 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2891 cjni_thread_detach ();
2892 return (-1);
2893 }
2895 o_ds = ctoj_data_set (jvm_env, ds);
2896 if (o_ds == NULL)
2897 {
2898 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2899 cjni_thread_detach ();
2900 return (-1);
2901 }
2903 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2904 o_ds, o_vl);
2906 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2908 /* If we're executing a target, copy the `ValueList' back to our
2909 * `value_list_t'. */
2910 if (cbi->type == CB_TYPE_TARGET)
2911 {
2912 value_list_t new_vl = { 0 };
2914 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2915 if (status != 0)
2916 {
2917 ERROR ("java plugin: cjni_match_target_invoke: "
2918 "jtoc_value_list failed.");
2919 }
2920 else /* if (status == 0) */
2921 {
2922 /* plugin_dispatch_values assures that this is dynamically allocated
2923 * memory. */
2924 sfree (vl->values);
2926 /* This will replace the vl->values pointer to a new, dynamically
2927 * allocated piece of memory. */
2928 memcpy (vl, &new_vl, sizeof (*vl));
2929 }
2930 } /* if (cbi->type == CB_TYPE_TARGET) */
2932 cjni_thread_detach ();
2933 return (ret_status);
2934 } /* }}} int cjni_match_target_invoke */
2936 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2937 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2938 {
2939 int status;
2940 size_t i;
2942 for (i = 0; i < java_callbacks_num; i++)
2943 {
2944 if (java_callbacks[i].type != CB_TYPE_INIT)
2945 continue;
2947 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2949 status = (*jvm_env)->CallIntMethod (jvm_env,
2950 java_callbacks[i].object, java_callbacks[i].method);
2951 if (status != 0)
2952 {
2953 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2954 "Removing read function.",
2955 java_callbacks[i].name, status);
2956 plugin_unregister_read (java_callbacks[i].name);
2957 }
2958 }
2960 return (0);
2961 } /* }}} int cjni_init_plugins */
2963 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2964 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2965 {
2966 int status;
2967 size_t i;
2969 for (i = 0; i < java_callbacks_num; i++)
2970 {
2971 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2972 continue;
2974 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2976 status = (*jvm_env)->CallIntMethod (jvm_env,
2977 java_callbacks[i].object, java_callbacks[i].method);
2978 if (status != 0)
2979 {
2980 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2981 java_callbacks[i].name, status);
2982 }
2983 }
2985 return (0);
2986 } /* }}} int cjni_shutdown_plugins */
2989 static int cjni_shutdown (void) /* {{{ */
2990 {
2991 JNIEnv *jvm_env;
2992 JavaVMAttachArgs args = { 0 };
2993 int status;
2994 size_t i;
2996 if (jvm == NULL)
2997 return (0);
2999 jvm_env = NULL;
3000 args.version = JNI_VERSION_1_2;
3002 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
3003 if (status != 0)
3004 {
3005 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
3006 status);
3007 return (-1);
3008 }
3010 /* Execute all the shutdown functions registered by plugins. */
3011 cjni_shutdown_plugins (jvm_env);
3013 /* Release all the global references to callback functions */
3014 for (i = 0; i < java_callbacks_num; i++)
3015 {
3016 if (java_callbacks[i].object != NULL)
3017 {
3018 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3019 java_callbacks[i].object = NULL;
3020 }
3021 sfree (java_callbacks[i].name);
3022 }
3023 java_callbacks_num = 0;
3024 sfree (java_callbacks);
3026 /* Release all the global references to directly loaded classes. */
3027 for (i = 0; i < java_classes_list_len; i++)
3028 {
3029 if (java_classes_list[i].object != NULL)
3030 {
3031 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3032 java_classes_list[i].object = NULL;
3033 }
3034 sfree (java_classes_list[i].name);
3035 }
3036 java_classes_list_len = 0;
3037 sfree (java_classes_list);
3039 /* Destroy the JVM */
3040 DEBUG ("java plugin: Destroying the JVM.");
3041 (*jvm)->DestroyJavaVM (jvm);
3042 jvm = NULL;
3043 jvm_env = NULL;
3045 pthread_key_delete (jvm_env_key);
3047 /* Free the JVM argument list */
3048 for (i = 0; i < jvm_argc; i++)
3049 sfree (jvm_argv[i]);
3050 jvm_argc = 0;
3051 sfree (jvm_argv);
3053 return (0);
3054 } /* }}} int cjni_shutdown */
3056 /* Initialization: Create a JVM, load all configured classes and call their
3057 * `config' and `init' callback methods. */
3058 static int cjni_init (void) /* {{{ */
3059 {
3060 JNIEnv *jvm_env;
3062 if ((config_block == NULL) && (jvm == NULL))
3063 {
3064 ERROR ("java plugin: cjni_init: No configuration block for "
3065 "the java plugin was found.");
3066 return (-1);
3067 }
3069 if (config_block != NULL)
3070 {
3071 cjni_config_perform (config_block);
3072 oconfig_free (config_block);
3073 }
3075 if (jvm == NULL)
3076 {
3077 ERROR ("java plugin: cjni_init: jvm == NULL");
3078 return (-1);
3079 }
3081 jvm_env = cjni_thread_attach ();
3082 if (jvm_env == NULL)
3083 return (-1);
3085 cjni_init_plugins (jvm_env);
3087 cjni_thread_detach ();
3088 return (0);
3089 } /* }}} int cjni_init */
3091 void module_register (void)
3092 {
3093 plugin_register_complex_config ("java", cjni_config_callback);
3094 plugin_register_init ("java", cjni_init);
3095 plugin_register_shutdown ("java", cjni_shutdown);
3096 } /* void module_register (void) */
3098 /* vim: set sw=2 sts=2 et fdm=marker : */