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"
25 #include "plugin.h"
26 #include "common.h"
27 #include "filter_chain.h"
29 #include <pthread.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;
1368 int status;
1370 memset (&n, 0, sizeof (n));
1371 n.meta = NULL;
1373 status = jtoc_notification (jvm_env, &n, o_notification);
1374 if (status != 0)
1375 {
1376 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1377 return (-1);
1378 }
1380 status = plugin_dispatch_notification (&n);
1382 return (status);
1383 } /* }}} jint cjni_api_dispatch_notification */
1385 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1386 jobject this, jobject o_string_type)
1387 {
1388 const char *ds_name;
1389 const data_set_t *ds;
1390 jobject o_dataset;
1392 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1393 if (ds_name == NULL)
1394 {
1395 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1396 return (NULL);
1397 }
1399 ds = plugin_get_ds (ds_name);
1400 DEBUG ("java plugin: cjni_api_get_ds: "
1401 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1403 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1405 if (ds == NULL)
1406 return (NULL);
1408 o_dataset = ctoj_data_set (jvm_env, ds);
1409 return (o_dataset);
1410 } /* }}} jint cjni_api_get_ds */
1412 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1413 jobject this, jobject o_name, jobject o_config)
1414 {
1415 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1416 } /* }}} jint cjni_api_register_config */
1418 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1419 jobject this, jobject o_name, jobject o_config)
1420 {
1421 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1422 } /* }}} jint cjni_api_register_init */
1424 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1425 jobject this, jobject o_name, jobject o_read)
1426 {
1427 user_data_t ud;
1428 cjni_callback_info_t *cbi;
1430 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1431 if (cbi == NULL)
1432 return (-1);
1434 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1436 memset (&ud, 0, sizeof (ud));
1437 ud.data = (void *) cbi;
1438 ud.free_func = cjni_callback_info_destroy;
1440 plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read,
1441 /* interval = */ 0, &ud);
1443 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1445 return (0);
1446 } /* }}} jint cjni_api_register_read */
1448 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1449 jobject this, jobject o_name, jobject o_write)
1450 {
1451 user_data_t ud;
1452 cjni_callback_info_t *cbi;
1454 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1455 if (cbi == NULL)
1456 return (-1);
1458 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1460 memset (&ud, 0, sizeof (ud));
1461 ud.data = (void *) cbi;
1462 ud.free_func = cjni_callback_info_destroy;
1464 plugin_register_write (cbi->name, cjni_write, &ud);
1466 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1468 return (0);
1469 } /* }}} jint cjni_api_register_write */
1471 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1472 jobject this, jobject o_name, jobject o_flush)
1473 {
1474 user_data_t ud;
1475 cjni_callback_info_t *cbi;
1477 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1478 if (cbi == NULL)
1479 return (-1);
1481 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1483 memset (&ud, 0, sizeof (ud));
1484 ud.data = (void *) cbi;
1485 ud.free_func = cjni_callback_info_destroy;
1487 plugin_register_flush (cbi->name, cjni_flush, &ud);
1489 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1491 return (0);
1492 } /* }}} jint cjni_api_register_flush */
1494 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1495 jobject this, jobject o_name, jobject o_shutdown)
1496 {
1497 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1498 CB_TYPE_SHUTDOWN));
1499 } /* }}} jint cjni_api_register_shutdown */
1501 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1502 jobject this, jobject o_name, jobject o_log)
1503 {
1504 user_data_t ud;
1505 cjni_callback_info_t *cbi;
1507 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1508 if (cbi == NULL)
1509 return (-1);
1511 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1513 memset (&ud, 0, sizeof (ud));
1514 ud.data = (void *) cbi;
1515 ud.free_func = cjni_callback_info_destroy;
1517 plugin_register_log (cbi->name, cjni_log, &ud);
1519 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1521 return (0);
1522 } /* }}} jint cjni_api_register_log */
1524 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1525 jobject this, jobject o_name, jobject o_notification)
1526 {
1527 user_data_t ud;
1528 cjni_callback_info_t *cbi;
1530 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1531 CB_TYPE_NOTIFICATION);
1532 if (cbi == NULL)
1533 return (-1);
1535 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1537 memset (&ud, 0, sizeof (ud));
1538 ud.data = (void *) cbi;
1539 ud.free_func = cjni_callback_info_destroy;
1541 plugin_register_notification (cbi->name, cjni_notification, &ud);
1543 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1545 return (0);
1546 } /* }}} jint cjni_api_register_notification */
1548 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1549 jobject this, jobject o_name, jobject o_match, int type)
1550 {
1551 int status;
1552 const char *c_name;
1554 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1555 if (c_name == NULL)
1556 {
1557 ERROR ("java plugin: cjni_api_register_match_target: "
1558 "GetStringUTFChars failed.");
1559 return (-1);
1560 }
1562 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1563 if (status != 0)
1564 {
1565 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1566 return (-1);
1567 }
1569 if (type == CB_TYPE_MATCH)
1570 {
1571 match_proc_t m_proc;
1573 memset (&m_proc, 0, sizeof (m_proc));
1574 m_proc.create = cjni_match_target_create;
1575 m_proc.destroy = cjni_match_target_destroy;
1576 m_proc.match = (void *) cjni_match_target_invoke;
1578 status = fc_register_match (c_name, m_proc);
1579 }
1580 else if (type == CB_TYPE_TARGET)
1581 {
1582 target_proc_t t_proc;
1584 memset (&t_proc, 0, sizeof (t_proc));
1585 t_proc.create = cjni_match_target_create;
1586 t_proc.destroy = cjni_match_target_destroy;
1587 t_proc.invoke = cjni_match_target_invoke;
1589 status = fc_register_target (c_name, t_proc);
1590 }
1591 else
1592 {
1593 ERROR ("java plugin: cjni_api_register_match_target: "
1594 "Don't know whether to create a match or a target.");
1595 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1596 return (-1);
1597 }
1599 if (status != 0)
1600 {
1601 ERROR ("java plugin: cjni_api_register_match_target: "
1602 "%s failed.",
1603 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1604 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1605 return (-1);
1606 }
1608 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1610 return (0);
1611 } /* }}} jint cjni_api_register_match_target */
1613 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1614 jobject this, jobject o_name, jobject o_match)
1615 {
1616 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1617 CB_TYPE_MATCH));
1618 } /* }}} jint cjni_api_register_match */
1620 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1621 jobject this, jobject o_name, jobject o_target)
1622 {
1623 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1624 CB_TYPE_TARGET));
1625 } /* }}} jint cjni_api_register_target */
1627 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1628 jobject this, jint severity, jobject o_message)
1629 {
1630 const char *c_str;
1632 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1633 if (c_str == NULL)
1634 {
1635 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1636 return;
1637 }
1639 if (severity < LOG_ERR)
1640 severity = LOG_ERR;
1641 if (severity > LOG_DEBUG)
1642 severity = LOG_DEBUG;
1644 plugin_log (severity, "%s", c_str);
1646 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1647 } /* }}} void cjni_api_log */
1649 static jstring JNICALL cjni_api_get_hostname (JNIEnv *jvm_env, jobject this)
1650 {
1651 return ctoj_output_string(jvm_env, hostname_g);
1652 }
1654 /* List of ``native'' functions, i. e. C-functions that can be called from
1655 * Java. */
1656 static JNINativeMethod jni_api_functions[] = /* {{{ */
1657 {
1658 { "dispatchValues",
1659 "(Lorg/collectd/api/ValueList;)I",
1660 cjni_api_dispatch_values },
1662 { "dispatchNotification",
1663 "(Lorg/collectd/api/Notification;)I",
1664 cjni_api_dispatch_notification },
1666 { "getDS",
1667 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1668 cjni_api_get_ds },
1670 { "registerConfig",
1671 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1672 cjni_api_register_config },
1674 { "registerInit",
1675 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1676 cjni_api_register_init },
1678 { "registerRead",
1679 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1680 cjni_api_register_read },
1682 { "registerWrite",
1683 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1684 cjni_api_register_write },
1686 { "registerFlush",
1687 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1688 cjni_api_register_flush },
1690 { "registerShutdown",
1691 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1692 cjni_api_register_shutdown },
1694 { "registerLog",
1695 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1696 cjni_api_register_log },
1698 { "registerNotification",
1699 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1700 cjni_api_register_notification },
1702 { "registerMatch",
1703 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1704 cjni_api_register_match },
1706 { "registerTarget",
1707 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1708 cjni_api_register_target },
1710 { "log",
1711 "(ILjava/lang/String;)V",
1712 cjni_api_log },
1714 { "getHostname",
1715 "()Ljava/lang/String;",
1716 cjni_api_get_hostname },
1718 };
1719 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1720 / sizeof (jni_api_functions[0]);
1721 /* }}} */
1723 /*
1724 * Functions
1725 */
1726 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1727 * all registration functions. */
1728 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1729 jobject o_name, jobject o_callback, int type)
1730 {
1731 const char *c_name;
1732 cjni_callback_info_t *cbi;
1733 const char *method_name;
1734 const char *method_signature;
1736 switch (type)
1737 {
1738 case CB_TYPE_CONFIG:
1739 method_name = "config";
1740 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1741 break;
1743 case CB_TYPE_INIT:
1744 method_name = "init";
1745 method_signature = "()I";
1746 break;
1748 case CB_TYPE_READ:
1749 method_name = "read";
1750 method_signature = "()I";
1751 break;
1753 case CB_TYPE_WRITE:
1754 method_name = "write";
1755 method_signature = "(Lorg/collectd/api/ValueList;)I";
1756 break;
1758 case CB_TYPE_FLUSH:
1759 method_name = "flush";
1760 method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I";
1761 break;
1763 case CB_TYPE_SHUTDOWN:
1764 method_name = "shutdown";
1765 method_signature = "()I";
1766 break;
1768 case CB_TYPE_LOG:
1769 method_name = "log";
1770 method_signature = "(ILjava/lang/String;)V";
1771 break;
1773 case CB_TYPE_NOTIFICATION:
1774 method_name = "notification";
1775 method_signature = "(Lorg/collectd/api/Notification;)I";
1776 break;
1778 case CB_TYPE_MATCH:
1779 method_name = "createMatch";
1780 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1781 "Lorg/collectd/api/CollectdMatchInterface;";
1782 break;
1784 case CB_TYPE_TARGET:
1785 method_name = "createTarget";
1786 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1787 "Lorg/collectd/api/CollectdTargetInterface;";
1788 break;
1790 default:
1791 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1792 type);
1793 return (NULL);
1794 }
1796 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1797 if (c_name == NULL)
1798 {
1799 ERROR ("java plugin: cjni_callback_info_create: "
1800 "GetStringUTFChars failed.");
1801 return (NULL);
1802 }
1804 cbi = calloc (1, sizeof (*cbi));
1805 if (cbi == NULL)
1806 {
1807 ERROR ("java plugin: cjni_callback_info_create: calloc failed.");
1808 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1809 return (NULL);
1810 }
1811 cbi->type = type;
1813 cbi->name = strdup (c_name);
1814 if (cbi->name == NULL)
1815 {
1816 pthread_mutex_unlock (&java_callbacks_lock);
1817 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1818 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1819 sfree (cbi);
1820 return (NULL);
1821 }
1823 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1825 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1826 if (cbi->object == NULL)
1827 {
1828 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1829 sfree (cbi->name);
1830 sfree (cbi);
1831 return (NULL);
1832 }
1834 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1835 if (cbi->class == NULL)
1836 {
1837 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1838 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1839 sfree (cbi->name);
1840 sfree (cbi);
1841 return (NULL);
1842 }
1844 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1845 method_name, method_signature);
1846 if (cbi->method == NULL)
1847 {
1848 ERROR ("java plugin: cjni_callback_info_create: "
1849 "Cannot find the `%s' method with signature `%s'.",
1850 method_name, method_signature);
1851 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1852 sfree (cbi->name);
1853 sfree (cbi);
1854 return (NULL);
1855 }
1857 return (cbi);
1858 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1860 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1861 * to the global `java_callbacks' variable. This is used for `config', `init',
1862 * and `shutdown' callbacks. */
1863 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1864 jobject o_name, jobject o_callback, int type)
1865 {
1866 cjni_callback_info_t *cbi;
1867 cjni_callback_info_t *tmp;
1868 #if COLLECT_DEBUG
1869 const char *type_str;
1870 #endif
1872 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1873 if (cbi == NULL)
1874 return (-1);
1876 #if COLLECT_DEBUG
1877 switch (type)
1878 {
1879 case CB_TYPE_CONFIG:
1880 type_str = "config";
1881 break;
1883 case CB_TYPE_INIT:
1884 type_str = "init";
1885 break;
1887 case CB_TYPE_SHUTDOWN:
1888 type_str = "shutdown";
1889 break;
1891 case CB_TYPE_MATCH:
1892 type_str = "match";
1893 break;
1895 case CB_TYPE_TARGET:
1896 type_str = "target";
1897 break;
1899 default:
1900 type_str = "<unknown>";
1901 }
1902 DEBUG ("java plugin: Registering new %s callback: %s",
1903 type_str, cbi->name);
1904 #endif
1906 pthread_mutex_lock (&java_callbacks_lock);
1908 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1909 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1910 if (tmp == NULL)
1911 {
1912 pthread_mutex_unlock (&java_callbacks_lock);
1913 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1915 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1916 free (cbi);
1918 return (-1);
1919 }
1920 java_callbacks = tmp;
1921 java_callbacks[java_callbacks_num] = *cbi;
1922 java_callbacks_num++;
1924 pthread_mutex_unlock (&java_callbacks_lock);
1926 free (cbi);
1927 return (0);
1928 } /* }}} int cjni_callback_register */
1930 /* Callback for `pthread_key_create'. It frees the data contained in
1931 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1932 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1933 {
1934 cjni_jvm_env_t *cjni_env;
1936 if (args == NULL)
1937 return;
1939 cjni_env = (cjni_jvm_env_t *) args;
1941 if (cjni_env->reference_counter > 0)
1942 {
1943 ERROR ("java plugin: cjni_jvm_env_destroy: "
1944 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1945 }
1947 if (cjni_env->jvm_env != NULL)
1948 {
1949 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1950 (void *) cjni_env->jvm_env);
1951 }
1953 /* The pointer is allocated in `cjni_thread_attach' */
1954 free (cjni_env);
1955 } /* }}} void cjni_jvm_env_destroy */
1957 /* Register ``native'' functions with the JVM. Native functions are C-functions
1958 * that can be called by Java code. */
1959 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1960 {
1961 jclass api_class_ptr;
1962 int status;
1964 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1965 if (api_class_ptr == NULL)
1966 {
1967 ERROR ("cjni_init_native: Cannot find the API class \"org.collectd.api"
1968 ".Collectd\". Please set the correct class path "
1969 "using 'JVMArg \"-Djava.class.path=...\"'.");
1970 return (-1);
1971 }
1973 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1974 jni_api_functions, (jint) jni_api_functions_num);
1975 if (status != 0)
1976 {
1977 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1978 return (-1);
1979 }
1981 return (0);
1982 } /* }}} int cjni_init_native */
1984 /* Create the JVM. This is called when the first thread tries to access the JVM
1985 * via cjni_thread_attach. */
1986 static int cjni_create_jvm (void) /* {{{ */
1987 {
1988 JNIEnv *jvm_env;
1989 JavaVMInitArgs vm_args;
1990 JavaVMOption vm_options[jvm_argc];
1992 int status;
1993 size_t i;
1995 if (jvm != NULL)
1996 return (0);
1998 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1999 if (status != 0)
2000 {
2001 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
2002 "with status %i.", status);
2003 return (-1);
2004 }
2006 jvm_env = NULL;
2008 memset (&vm_args, 0, sizeof (vm_args));
2009 vm_args.version = JNI_VERSION_1_2;
2010 vm_args.options = vm_options;
2011 vm_args.nOptions = (jint) jvm_argc;
2013 for (i = 0; i < jvm_argc; i++)
2014 {
2015 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
2016 i, jvm_argv[i]);
2017 vm_args.options[i].optionString = jvm_argv[i];
2018 }
2020 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
2021 if (status != 0)
2022 {
2023 ERROR ("java plugin: cjni_create_jvm: "
2024 "JNI_CreateJavaVM failed with status %i.",
2025 status);
2026 return (-1);
2027 }
2028 assert (jvm != NULL);
2029 assert (jvm_env != NULL);
2031 /* Call RegisterNatives */
2032 status = cjni_init_native (jvm_env);
2033 if (status != 0)
2034 {
2035 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
2036 return (-1);
2037 }
2039 DEBUG ("java plugin: The JVM has been created.");
2040 return (0);
2041 } /* }}} int cjni_create_jvm */
2043 /* Increase the reference counter to the JVM for this thread. If it was zero,
2044 * attach the JVM first. */
2045 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2046 {
2047 cjni_jvm_env_t *cjni_env;
2048 JNIEnv *jvm_env;
2050 /* If we're the first thread to access the JVM, we'll have to create it
2051 * first.. */
2052 if (jvm == NULL)
2053 {
2054 int status;
2056 status = cjni_create_jvm ();
2057 if (status != 0)
2058 {
2059 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2060 return (NULL);
2061 }
2062 }
2063 assert (jvm != NULL);
2065 cjni_env = pthread_getspecific (jvm_env_key);
2066 if (cjni_env == NULL)
2067 {
2068 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2069 cjni_env = calloc (1, sizeof (*cjni_env));
2070 if (cjni_env == NULL)
2071 {
2072 ERROR ("java plugin: cjni_thread_attach: calloc failed.");
2073 return (NULL);
2074 }
2075 cjni_env->reference_counter = 0;
2076 cjni_env->jvm_env = NULL;
2078 pthread_setspecific (jvm_env_key, cjni_env);
2079 }
2081 if (cjni_env->reference_counter > 0)
2082 {
2083 cjni_env->reference_counter++;
2084 jvm_env = cjni_env->jvm_env;
2085 }
2086 else
2087 {
2088 int status;
2089 JavaVMAttachArgs args;
2091 assert (cjni_env->jvm_env == NULL);
2093 memset (&args, 0, sizeof (args));
2094 args.version = JNI_VERSION_1_2;
2096 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2097 if (status != 0)
2098 {
2099 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2100 "with status %i.", status);
2101 return (NULL);
2102 }
2104 cjni_env->reference_counter = 1;
2105 cjni_env->jvm_env = jvm_env;
2106 }
2108 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2109 cjni_env->reference_counter);
2110 assert (jvm_env != NULL);
2111 return (jvm_env);
2112 } /* }}} JNIEnv *cjni_thread_attach */
2114 /* Decrease the reference counter of this thread. If it reaches zero, detach
2115 * from the JVM. */
2116 static int cjni_thread_detach (void) /* {{{ */
2117 {
2118 cjni_jvm_env_t *cjni_env;
2119 int status;
2121 cjni_env = pthread_getspecific (jvm_env_key);
2122 if (cjni_env == NULL)
2123 {
2124 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2125 return (-1);
2126 }
2128 assert (cjni_env->reference_counter > 0);
2129 assert (cjni_env->jvm_env != NULL);
2131 cjni_env->reference_counter--;
2132 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2133 cjni_env->reference_counter);
2135 if (cjni_env->reference_counter > 0)
2136 return (0);
2138 status = (*jvm)->DetachCurrentThread (jvm);
2139 if (status != 0)
2140 {
2141 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2142 "with status %i.", status);
2143 }
2145 cjni_env->reference_counter = 0;
2146 cjni_env->jvm_env = NULL;
2148 return (0);
2149 } /* }}} int cjni_thread_detach */
2151 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2152 {
2153 char **tmp;
2155 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2156 {
2157 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2158 return (-1);
2159 }
2161 if (jvm != NULL)
2162 {
2163 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2164 "`LoadPlugin' options! The JVM is already started and I have to "
2165 "ignore this argument: %s",
2166 ci->values[0].value.string);
2167 return (-1);
2168 }
2170 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2171 if (tmp == NULL)
2172 {
2173 ERROR ("java plugin: realloc failed.");
2174 return (-1);
2175 }
2176 jvm_argv = tmp;
2178 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2179 if (jvm_argv[jvm_argc] == NULL)
2180 {
2181 ERROR ("java plugin: strdup failed.");
2182 return (-1);
2183 }
2184 jvm_argc++;
2186 return (0);
2187 } /* }}} int cjni_config_add_jvm_arg */
2189 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2190 {
2191 JNIEnv *jvm_env;
2192 java_plugin_class_t *class;
2193 jmethodID constructor_id;
2194 jobject tmp_object;
2196 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2197 {
2198 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2199 return (-1);
2200 }
2202 jvm_env = cjni_thread_attach ();
2203 if (jvm_env == NULL)
2204 return (-1);
2206 class = (java_plugin_class_t *) realloc (java_classes_list,
2207 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2208 if (class == NULL)
2209 {
2210 ERROR ("java plugin: realloc failed.");
2211 cjni_thread_detach ();
2212 return (-1);
2213 }
2214 java_classes_list = class;
2215 class = java_classes_list + java_classes_list_len;
2217 memset (class, 0, sizeof (*class));
2218 class->name = strdup (ci->values[0].value.string);
2219 if (class->name == NULL)
2220 {
2221 ERROR ("java plugin: strdup failed.");
2222 cjni_thread_detach ();
2223 return (-1);
2224 }
2225 class->class = NULL;
2226 class->object = NULL;
2228 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2229 thorough the Java community, but (Sun's) `FindClass' and friends need
2230 slashes. */
2231 size_t i;
2232 for (i = 0; class->name[i] != 0; i++)
2233 if (class->name[i] == '.')
2234 class->name[i] = '/';
2235 }
2237 DEBUG ("java plugin: Loading class %s", class->name);
2239 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2240 if (class->class == NULL)
2241 {
2242 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2243 class->name);
2244 cjni_thread_detach ();
2245 free (class->name);
2246 return (-1);
2247 }
2249 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2250 "<init>", "()V");
2251 if (constructor_id == NULL)
2252 {
2253 ERROR ("java plugin: cjni_config_load_plugin: "
2254 "Could not find the constructor for `%s'.",
2255 class->name);
2256 cjni_thread_detach ();
2257 free (class->name);
2258 return (-1);
2259 }
2261 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2262 constructor_id);
2263 if (tmp_object != NULL)
2264 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2265 else
2266 class->object = NULL;
2267 if (class->object == NULL)
2268 {
2269 ERROR ("java plugin: cjni_config_load_plugin: "
2270 "Could create a new `%s' object.",
2271 class->name);
2272 cjni_thread_detach ();
2273 free (class->name);
2274 return (-1);
2275 }
2277 cjni_thread_detach ();
2279 java_classes_list_len++;
2281 return (0);
2282 } /* }}} int cjni_config_load_plugin */
2284 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2285 {
2286 JNIEnv *jvm_env;
2287 cjni_callback_info_t *cbi;
2288 jobject o_ocitem;
2289 const char *name;
2290 size_t i;
2292 jclass class;
2293 jmethodID method;
2295 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2296 {
2297 WARNING ("java plugin: `Plugin' blocks "
2298 "need exactly one string argument.");
2299 return (-1);
2300 }
2302 name = ci->values[0].value.string;
2304 cbi = NULL;
2305 for (i = 0; i < java_callbacks_num; i++)
2306 {
2307 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2308 continue;
2310 if (strcmp (name, java_callbacks[i].name) != 0)
2311 continue;
2313 cbi = java_callbacks + i;
2314 break;
2315 }
2317 if (cbi == NULL)
2318 {
2319 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2320 "configuration callback has been registered. Please make sure, the "
2321 "`LoadPlugin' lines precede the `Plugin' blocks.",
2322 name);
2323 return (0);
2324 }
2326 DEBUG ("java plugin: Configuring %s", name);
2328 jvm_env = cjni_thread_attach ();
2329 if (jvm_env == NULL)
2330 return (-1);
2332 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2333 if (o_ocitem == NULL)
2334 {
2335 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2336 cjni_thread_detach ();
2337 return (-1);
2338 }
2340 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2341 method = (*jvm_env)->GetMethodID (jvm_env, class,
2342 "config", "(Lorg/collectd/api/OConfigItem;)I");
2344 (*jvm_env)->CallIntMethod (jvm_env,
2345 cbi->object, method, o_ocitem);
2347 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2348 cjni_thread_detach ();
2349 return (0);
2350 } /* }}} int cjni_config_plugin_block */
2352 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2353 {
2354 int success;
2355 int errors;
2356 int status;
2357 int i;
2359 success = 0;
2360 errors = 0;
2362 for (i = 0; i < ci->children_num; i++)
2363 {
2364 oconfig_item_t *child = ci->children + i;
2366 if (strcasecmp ("JVMArg", child->key) == 0)
2367 {
2368 status = cjni_config_add_jvm_arg (child);
2369 if (status == 0)
2370 success++;
2371 else
2372 errors++;
2373 }
2374 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2375 {
2376 status = cjni_config_load_plugin (child);
2377 if (status == 0)
2378 success++;
2379 else
2380 errors++;
2381 }
2382 else if (strcasecmp ("Plugin", child->key) == 0)
2383 {
2384 status = cjni_config_plugin_block (child);
2385 if (status == 0)
2386 success++;
2387 else
2388 errors++;
2389 }
2390 else
2391 {
2392 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2393 errors++;
2394 }
2395 }
2397 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2398 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2400 if ((success == 0) && (errors > 0))
2401 {
2402 ERROR ("java plugin: All statements failed.");
2403 return (-1);
2404 }
2406 return (0);
2407 } /* }}} int cjni_config_perform */
2409 /* Copy the children of `ci' to the global `config_block' variable. */
2410 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2411 {
2412 oconfig_item_t *ci_copy;
2413 oconfig_item_t *tmp;
2415 assert (ci != NULL);
2416 if (ci->children_num == 0)
2417 return (0); /* nothing to do */
2419 ci_copy = oconfig_clone (ci);
2420 if (ci_copy == NULL)
2421 {
2422 ERROR ("java plugin: oconfig_clone failed.");
2423 return (-1);
2424 }
2426 if (config_block == NULL)
2427 {
2428 config_block = ci_copy;
2429 return (0);
2430 }
2432 tmp = realloc (config_block->children,
2433 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2434 if (tmp == NULL)
2435 {
2436 ERROR ("java plugin: realloc failed.");
2437 oconfig_free (ci_copy);
2438 return (-1);
2439 }
2440 config_block->children = tmp;
2442 /* Copy the pointers */
2443 memcpy (config_block->children + config_block->children_num,
2444 ci_copy->children,
2445 ci_copy->children_num * sizeof (*ci_copy->children));
2446 config_block->children_num += ci_copy->children_num;
2448 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2449 memset (ci_copy->children, 0,
2450 ci_copy->children_num * sizeof (*ci_copy->children));
2451 ci_copy->children_num = 0;
2453 oconfig_free (ci_copy);
2455 return (0);
2456 } /* }}} int cjni_config_callback */
2458 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2459 * and `cjni_write'. In particular, delete the global reference to the Java
2460 * object. */
2461 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2462 {
2463 JNIEnv *jvm_env;
2464 cjni_callback_info_t *cbi;
2466 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2468 cbi = (cjni_callback_info_t *) arg;
2470 /* This condition can occur when shutting down. */
2471 if (jvm == NULL)
2472 {
2473 sfree (cbi);
2474 return;
2475 }
2477 if (arg == NULL)
2478 return;
2480 jvm_env = cjni_thread_attach ();
2481 if (jvm_env == NULL)
2482 {
2483 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2484 return;
2485 }
2487 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2489 cbi->method = NULL;
2490 cbi->object = NULL;
2491 cbi->class = NULL;
2492 free (cbi);
2494 cjni_thread_detach ();
2495 } /* }}} void cjni_callback_info_destroy */
2497 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2498 static int cjni_read (user_data_t *ud) /* {{{ */
2499 {
2500 JNIEnv *jvm_env;
2501 cjni_callback_info_t *cbi;
2502 int ret_status;
2504 if (jvm == NULL)
2505 {
2506 ERROR ("java plugin: cjni_read: jvm == NULL");
2507 return (-1);
2508 }
2510 if ((ud == NULL) || (ud->data == NULL))
2511 {
2512 ERROR ("java plugin: cjni_read: Invalid user data.");
2513 return (-1);
2514 }
2516 jvm_env = cjni_thread_attach ();
2517 if (jvm_env == NULL)
2518 return (-1);
2520 cbi = (cjni_callback_info_t *) ud->data;
2522 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2523 cbi->method);
2525 cjni_thread_detach ();
2526 return (ret_status);
2527 } /* }}} int cjni_read */
2529 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2530 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2531 user_data_t *ud)
2532 {
2533 JNIEnv *jvm_env;
2534 cjni_callback_info_t *cbi;
2535 jobject vl_java;
2536 int ret_status;
2538 if (jvm == NULL)
2539 {
2540 ERROR ("java plugin: cjni_write: jvm == NULL");
2541 return (-1);
2542 }
2544 if ((ud == NULL) || (ud->data == NULL))
2545 {
2546 ERROR ("java plugin: cjni_write: Invalid user data.");
2547 return (-1);
2548 }
2550 jvm_env = cjni_thread_attach ();
2551 if (jvm_env == NULL)
2552 return (-1);
2554 cbi = (cjni_callback_info_t *) ud->data;
2556 vl_java = ctoj_value_list (jvm_env, ds, vl);
2557 if (vl_java == NULL)
2558 {
2559 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2560 cjni_thread_detach ();
2561 return (-1);
2562 }
2564 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2565 cbi->object, cbi->method, vl_java);
2567 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2569 cjni_thread_detach ();
2570 return (ret_status);
2571 } /* }}} int cjni_write */
2573 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2574 static int cjni_flush (cdtime_t timeout, const char *identifier, /* {{{ */
2575 user_data_t *ud)
2576 {
2577 JNIEnv *jvm_env;
2578 cjni_callback_info_t *cbi;
2579 jobject o_timeout;
2580 jobject o_identifier;
2581 int ret_status;
2583 if (jvm == NULL)
2584 {
2585 ERROR ("java plugin: cjni_flush: jvm == NULL");
2586 return (-1);
2587 }
2589 if ((ud == NULL) || (ud->data == NULL))
2590 {
2591 ERROR ("java plugin: cjni_flush: Invalid user data.");
2592 return (-1);
2593 }
2595 jvm_env = cjni_thread_attach ();
2596 if (jvm_env == NULL)
2597 return (-1);
2599 cbi = (cjni_callback_info_t *) ud->data;
2601 o_timeout = ctoj_jdouble_to_number (jvm_env,
2602 (jdouble) CDTIME_T_TO_DOUBLE (timeout));
2603 if (o_timeout == NULL)
2604 {
2605 ERROR ("java plugin: cjni_flush: Converting double "
2606 "to Number object failed.");
2607 cjni_thread_detach ();
2608 return (-1);
2609 }
2611 o_identifier = NULL;
2612 if (identifier != NULL)
2613 {
2614 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2615 if (o_identifier == NULL)
2616 {
2617 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2618 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2619 cjni_thread_detach ();
2620 return (-1);
2621 }
2622 }
2624 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2625 cbi->object, cbi->method, o_timeout, o_identifier);
2627 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2628 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2630 cjni_thread_detach ();
2631 return (ret_status);
2632 } /* }}} int cjni_flush */
2634 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2635 static void cjni_log (int severity, const char *message, /* {{{ */
2636 user_data_t *ud)
2637 {
2638 JNIEnv *jvm_env;
2639 cjni_callback_info_t *cbi;
2640 jobject o_message;
2642 if (jvm == NULL)
2643 return;
2645 if ((ud == NULL) || (ud->data == NULL))
2646 return;
2648 jvm_env = cjni_thread_attach ();
2649 if (jvm_env == NULL)
2650 return;
2652 cbi = (cjni_callback_info_t *) ud->data;
2654 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2655 if (o_message == NULL)
2656 {
2657 cjni_thread_detach ();
2658 return;
2659 }
2661 (*jvm_env)->CallVoidMethod (jvm_env,
2662 cbi->object, cbi->method, (jint) severity, o_message);
2664 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2666 cjni_thread_detach ();
2667 } /* }}} void cjni_log */
2669 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2670 * pointer. */
2671 static int cjni_notification (const notification_t *n, /* {{{ */
2672 user_data_t *ud)
2673 {
2674 JNIEnv *jvm_env;
2675 cjni_callback_info_t *cbi;
2676 jobject o_notification;
2677 int ret_status;
2679 if (jvm == NULL)
2680 {
2681 ERROR ("java plugin: cjni_read: jvm == NULL");
2682 return (-1);
2683 }
2685 if ((ud == NULL) || (ud->data == NULL))
2686 {
2687 ERROR ("java plugin: cjni_read: Invalid user data.");
2688 return (-1);
2689 }
2691 jvm_env = cjni_thread_attach ();
2692 if (jvm_env == NULL)
2693 return (-1);
2695 cbi = (cjni_callback_info_t *) ud->data;
2697 o_notification = ctoj_notification (jvm_env, n);
2698 if (o_notification == NULL)
2699 {
2700 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2701 cjni_thread_detach ();
2702 return (-1);
2703 }
2705 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2706 cbi->object, cbi->method, o_notification);
2708 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2710 cjni_thread_detach ();
2711 return (ret_status);
2712 } /* }}} int cjni_notification */
2714 /* Callbacks for matches implemented in Java */
2715 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2716 void **user_data)
2717 {
2718 JNIEnv *jvm_env;
2719 cjni_callback_info_t *cbi_ret;
2720 cjni_callback_info_t *cbi_factory;
2721 const char *name;
2722 jobject o_ci;
2723 jobject o_tmp;
2724 int type;
2725 size_t i;
2727 cbi_ret = NULL;
2728 o_ci = NULL;
2729 jvm_env = NULL;
2731 #define BAIL_OUT(status) \
2732 if (cbi_ret != NULL) { \
2733 free (cbi_ret->name); \
2734 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2735 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2736 } \
2737 free (cbi_ret); \
2738 if (o_ci != NULL) \
2739 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2740 cjni_thread_detach (); \
2741 return (status)
2743 if (jvm == NULL)
2744 {
2745 ERROR ("java plugin: cjni_read: jvm == NULL");
2746 return (-1);
2747 }
2749 jvm_env = cjni_thread_attach ();
2750 if (jvm_env == NULL)
2751 return (-1);
2753 /* Find out whether to create a match or a target. */
2754 if (strcasecmp ("Match", ci->key) == 0)
2755 type = CB_TYPE_MATCH;
2756 else if (strcasecmp ("Target", ci->key) == 0)
2757 type = CB_TYPE_TARGET;
2758 else
2759 {
2760 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2761 "to create a match or a target.");
2762 BAIL_OUT (-1);
2763 }
2765 /* This is the name of the match we should create. */
2766 name = ci->values[0].value.string;
2768 /* Lets see if we have a matching factory here.. */
2769 cbi_factory = NULL;
2770 for (i = 0; i < java_callbacks_num; i++)
2771 {
2772 if (java_callbacks[i].type != type)
2773 continue;
2775 if (strcmp (name, java_callbacks[i].name) != 0)
2776 continue;
2778 cbi_factory = java_callbacks + i;
2779 break;
2780 }
2782 /* Nope, no factory for that name.. */
2783 if (cbi_factory == NULL)
2784 {
2785 ERROR ("java plugin: cjni_match_target_create: "
2786 "No such match factory registered: %s",
2787 name);
2788 BAIL_OUT (-1);
2789 }
2791 /* We convert `ci' to its Java equivalent.. */
2792 o_ci = ctoj_oconfig_item (jvm_env, ci);
2793 if (o_ci == NULL)
2794 {
2795 ERROR ("java plugin: cjni_match_target_create: "
2796 "ctoj_oconfig_item failed.");
2797 BAIL_OUT (-1);
2798 }
2800 /* Allocate a new callback info structure. This is going to be our user_data
2801 * pointer. */
2802 cbi_ret = calloc (1, sizeof (*cbi_ret));
2803 if (cbi_ret == NULL)
2804 {
2805 ERROR ("java plugin: cjni_match_target_create: calloc failed.");
2806 BAIL_OUT (-1);
2807 }
2809 cbi_ret->object = NULL;
2810 cbi_ret->type = type;
2812 /* Lets fill the callback info structure.. First, the name: */
2813 cbi_ret->name = strdup (name);
2814 if (cbi_ret->name == NULL)
2815 {
2816 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2817 BAIL_OUT (-1);
2818 }
2820 /* Then call the factory method so it creates a new object for us. */
2821 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2822 cbi_factory->object, cbi_factory->method, o_ci);
2823 if (o_tmp == NULL)
2824 {
2825 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2826 BAIL_OUT (-1);
2827 }
2829 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2830 if (o_tmp == NULL)
2831 {
2832 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2833 BAIL_OUT (-1);
2834 }
2836 /* This is the class of the match. It is possibly different from the class of
2837 * the match-factory! */
2838 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2839 if (cbi_ret->class == NULL)
2840 {
2841 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2842 BAIL_OUT (-1);
2843 }
2845 /* Lookup the `int match (DataSet, ValueList)' method. */
2846 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2847 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2848 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2849 if (cbi_ret->method == NULL)
2850 {
2851 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2852 BAIL_OUT (-1);
2853 }
2855 /* Return the newly created match via the user_data pointer. */
2856 *user_data = (void *) cbi_ret;
2858 cjni_thread_detach ();
2860 DEBUG ("java plugin: cjni_match_target_create: "
2861 "Successfully created a `%s' %s.",
2862 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2864 /* Success! */
2865 return (0);
2866 #undef BAIL_OUT
2867 } /* }}} int cjni_match_target_create */
2869 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2870 {
2871 cjni_callback_info_destroy (*user_data);
2872 *user_data = NULL;
2874 return (0);
2875 } /* }}} int cjni_match_target_destroy */
2877 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2878 value_list_t *vl, notification_meta_t **meta, void **user_data)
2879 {
2880 JNIEnv *jvm_env;
2881 cjni_callback_info_t *cbi;
2882 jobject o_vl;
2883 jobject o_ds;
2884 int ret_status;
2885 int status;
2887 if (jvm == NULL)
2888 {
2889 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2890 return (-1);
2891 }
2893 jvm_env = cjni_thread_attach ();
2894 if (jvm_env == NULL)
2895 return (-1);
2897 cbi = (cjni_callback_info_t *) *user_data;
2899 o_vl = ctoj_value_list (jvm_env, ds, vl);
2900 if (o_vl == NULL)
2901 {
2902 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2903 cjni_thread_detach ();
2904 return (-1);
2905 }
2907 o_ds = ctoj_data_set (jvm_env, ds);
2908 if (o_ds == NULL)
2909 {
2910 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2911 cjni_thread_detach ();
2912 return (-1);
2913 }
2915 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2916 o_ds, o_vl);
2918 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2920 /* If we're executing a target, copy the `ValueList' back to our
2921 * `value_list_t'. */
2922 if (cbi->type == CB_TYPE_TARGET)
2923 {
2924 value_list_t new_vl;
2926 memset (&new_vl, 0, sizeof (new_vl));
2927 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2928 if (status != 0)
2929 {
2930 ERROR ("java plugin: cjni_match_target_invoke: "
2931 "jtoc_value_list failed.");
2932 }
2933 else /* if (status == 0) */
2934 {
2935 /* plugin_dispatch_values assures that this is dynamically allocated
2936 * memory. */
2937 sfree (vl->values);
2939 /* This will replace the vl->values pointer to a new, dynamically
2940 * allocated piece of memory. */
2941 memcpy (vl, &new_vl, sizeof (*vl));
2942 }
2943 } /* if (cbi->type == CB_TYPE_TARGET) */
2945 cjni_thread_detach ();
2946 return (ret_status);
2947 } /* }}} int cjni_match_target_invoke */
2949 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2950 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2951 {
2952 int status;
2953 size_t i;
2955 for (i = 0; i < java_callbacks_num; i++)
2956 {
2957 if (java_callbacks[i].type != CB_TYPE_INIT)
2958 continue;
2960 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2962 status = (*jvm_env)->CallIntMethod (jvm_env,
2963 java_callbacks[i].object, java_callbacks[i].method);
2964 if (status != 0)
2965 {
2966 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2967 "Removing read function.",
2968 java_callbacks[i].name, status);
2969 plugin_unregister_read (java_callbacks[i].name);
2970 }
2971 }
2973 return (0);
2974 } /* }}} int cjni_init_plugins */
2976 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2977 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2978 {
2979 int status;
2980 size_t i;
2982 for (i = 0; i < java_callbacks_num; i++)
2983 {
2984 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2985 continue;
2987 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2989 status = (*jvm_env)->CallIntMethod (jvm_env,
2990 java_callbacks[i].object, java_callbacks[i].method);
2991 if (status != 0)
2992 {
2993 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2994 java_callbacks[i].name, status);
2995 }
2996 }
2998 return (0);
2999 } /* }}} int cjni_shutdown_plugins */
3002 static int cjni_shutdown (void) /* {{{ */
3003 {
3004 JNIEnv *jvm_env;
3005 JavaVMAttachArgs args;
3006 int status;
3007 size_t i;
3009 if (jvm == NULL)
3010 return (0);
3012 jvm_env = NULL;
3013 memset (&args, 0, sizeof (args));
3014 args.version = JNI_VERSION_1_2;
3016 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
3017 if (status != 0)
3018 {
3019 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
3020 status);
3021 return (-1);
3022 }
3024 /* Execute all the shutdown functions registered by plugins. */
3025 cjni_shutdown_plugins (jvm_env);
3027 /* Release all the global references to callback functions */
3028 for (i = 0; i < java_callbacks_num; i++)
3029 {
3030 if (java_callbacks[i].object != NULL)
3031 {
3032 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3033 java_callbacks[i].object = NULL;
3034 }
3035 sfree (java_callbacks[i].name);
3036 }
3037 java_callbacks_num = 0;
3038 sfree (java_callbacks);
3040 /* Release all the global references to directly loaded classes. */
3041 for (i = 0; i < java_classes_list_len; i++)
3042 {
3043 if (java_classes_list[i].object != NULL)
3044 {
3045 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3046 java_classes_list[i].object = NULL;
3047 }
3048 sfree (java_classes_list[i].name);
3049 }
3050 java_classes_list_len = 0;
3051 sfree (java_classes_list);
3053 /* Destroy the JVM */
3054 DEBUG ("java plugin: Destroying the JVM.");
3055 (*jvm)->DestroyJavaVM (jvm);
3056 jvm = NULL;
3057 jvm_env = NULL;
3059 pthread_key_delete (jvm_env_key);
3061 /* Free the JVM argument list */
3062 for (i = 0; i < jvm_argc; i++)
3063 sfree (jvm_argv[i]);
3064 jvm_argc = 0;
3065 sfree (jvm_argv);
3067 return (0);
3068 } /* }}} int cjni_shutdown */
3070 /* Initialization: Create a JVM, load all configured classes and call their
3071 * `config' and `init' callback methods. */
3072 static int cjni_init (void) /* {{{ */
3073 {
3074 JNIEnv *jvm_env;
3076 if ((config_block == NULL) && (jvm == NULL))
3077 {
3078 ERROR ("java plugin: cjni_init: No configuration block for "
3079 "the java plugin was found.");
3080 return (-1);
3081 }
3083 if (config_block != NULL)
3084 {
3085 cjni_config_perform (config_block);
3086 oconfig_free (config_block);
3087 }
3089 if (jvm == NULL)
3090 {
3091 ERROR ("java plugin: cjni_init: jvm == NULL");
3092 return (-1);
3093 }
3095 jvm_env = cjni_thread_attach ();
3096 if (jvm_env == NULL)
3097 return (-1);
3099 cjni_init_plugins (jvm_env);
3101 cjni_thread_detach ();
3102 return (0);
3103 } /* }}} int cjni_init */
3105 void module_register (void)
3106 {
3107 plugin_register_complex_config ("java", cjni_config_callback);
3108 plugin_register_init ("java", cjni_init);
3109 plugin_register_shutdown ("java", cjni_shutdown);
3110 } /* void module_register (void) */
3112 /* vim: set sw=2 sts=2 et fdm=marker : */