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 verplant.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 /*
97 * Prototypes
98 *
99 * Mostly functions that are needed by the Java interface (``native'')
100 * functions.
101 */
102 static void cjni_callback_info_destroy (void *arg);
103 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env,
104 jobject o_name, jobject o_callback, int type);
105 static int cjni_callback_register (JNIEnv *jvm_env, jobject o_name,
106 jobject o_callback, int type);
107 static int cjni_read (user_data_t *user_data);
108 static int cjni_write (const data_set_t *ds, const value_list_t *vl,
109 user_data_t *ud);
110 static int cjni_flush (int timeout, const char *identifier, user_data_t *ud);
111 static void cjni_log (int severity, const char *message, user_data_t *ud);
112 static int cjni_notification (const notification_t *n, user_data_t *ud);
114 /* Create, destroy, and match/invoke functions, used by both, matches AND
115 * targets. */
116 static int cjni_match_target_create (const oconfig_item_t *ci, void **user_data);
117 static int cjni_match_target_destroy (void **user_data);
118 static int cjni_match_target_invoke (const data_set_t *ds, value_list_t *vl,
119 notification_meta_t **meta, void **user_data);
121 /*
122 * C to Java conversion functions
123 */
124 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
125 const char *string,
126 jclass class_ptr, jobject object_ptr, const char *method_name)
127 {
128 jmethodID m_set;
129 jstring o_string;
131 /* Create a java.lang.String */
132 o_string = (*jvm_env)->NewStringUTF (jvm_env,
133 (string != NULL) ? string : "");
134 if (o_string == NULL)
135 {
136 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
137 return (-1);
138 }
140 /* Search for the `void setFoo (String s)' method. */
141 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
142 method_name, "(Ljava/lang/String;)V");
143 if (m_set == NULL)
144 {
145 ERROR ("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
146 method_name);
147 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
148 return (-1);
149 }
151 /* Call the method. */
152 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, o_string);
154 /* Decrease reference counter on the java.lang.String object. */
155 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
157 return (0);
158 } /* }}} int ctoj_string */
160 static int ctoj_int (JNIEnv *jvm_env, /* {{{ */
161 jint value,
162 jclass class_ptr, jobject object_ptr, const char *method_name)
163 {
164 jmethodID m_set;
166 /* Search for the `void setFoo (int i)' method. */
167 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
168 method_name, "(I)V");
169 if (m_set == NULL)
170 {
171 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
172 method_name);
173 return (-1);
174 }
176 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
178 return (0);
179 } /* }}} int ctoj_int */
181 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
182 jlong value,
183 jclass class_ptr, jobject object_ptr, const char *method_name)
184 {
185 jmethodID m_set;
187 /* Search for the `void setFoo (long l)' method. */
188 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
189 method_name, "(J)V");
190 if (m_set == NULL)
191 {
192 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
193 method_name);
194 return (-1);
195 }
197 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
199 return (0);
200 } /* }}} int ctoj_long */
202 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
203 jdouble value,
204 jclass class_ptr, jobject object_ptr, const char *method_name)
205 {
206 jmethodID m_set;
208 /* Search for the `void setFoo (double d)' method. */
209 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
210 method_name, "(D)V");
211 if (m_set == NULL)
212 {
213 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
214 method_name);
215 return (-1);
216 }
218 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
220 return (0);
221 } /* }}} int ctoj_double */
223 /* Convert a jlong to a java.lang.Number */
224 static jobject ctoj_jlong_to_number (JNIEnv *jvm_env, jlong value) /* {{{ */
225 {
226 jclass c_long;
227 jmethodID m_long_constructor;
229 /* Look up the java.lang.Long class */
230 c_long = (*jvm_env)->FindClass (jvm_env, "java/lang/Long");
231 if (c_long == NULL)
232 {
233 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
234 "java.lang.Long class failed.");
235 return (NULL);
236 }
238 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
239 c_long, "<init>", "(J)V");
240 if (m_long_constructor == NULL)
241 {
242 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
243 "`Long (long)' constructor failed.");
244 return (NULL);
245 }
247 return ((*jvm_env)->NewObject (jvm_env,
248 c_long, m_long_constructor, value));
249 } /* }}} jobject ctoj_jlong_to_number */
251 /* Convert a jdouble to a java.lang.Number */
252 static jobject ctoj_jdouble_to_number (JNIEnv *jvm_env, jdouble value) /* {{{ */
253 {
254 jclass c_double;
255 jmethodID m_double_constructor;
257 /* Look up the java.lang.Long class */
258 c_double = (*jvm_env)->FindClass (jvm_env, "java/lang/Double");
259 if (c_double == NULL)
260 {
261 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
262 "java.lang.Double class failed.");
263 return (NULL);
264 }
266 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
267 c_double, "<init>", "(D)V");
268 if (m_double_constructor == NULL)
269 {
270 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
271 "`Double (double)' constructor failed.");
272 return (NULL);
273 }
275 return ((*jvm_env)->NewObject (jvm_env,
276 c_double, m_double_constructor, value));
277 } /* }}} jobject ctoj_jdouble_to_number */
279 /* Convert a value_t to a java.lang.Number */
280 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
281 value_t value, int ds_type)
282 {
283 if (ds_type == DS_TYPE_COUNTER)
284 return (ctoj_jlong_to_number (jvm_env, (jlong) value.counter));
285 else if (ds_type == DS_TYPE_GAUGE)
286 return (ctoj_jdouble_to_number (jvm_env, (jdouble) value.gauge));
287 if (ds_type == DS_TYPE_DERIVE)
288 return (ctoj_jlong_to_number (jvm_env, (jlong) value.derive));
289 if (ds_type == DS_TYPE_ABSOLUTE)
290 return (ctoj_jlong_to_number (jvm_env, (jlong) value.absolute));
291 else
292 return (NULL);
293 } /* }}} jobject ctoj_value_to_number */
295 /* Convert a data_source_t to a org/collectd/api/DataSource */
296 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
297 const data_source_t *dsrc)
298 {
299 jclass c_datasource;
300 jmethodID m_datasource_constructor;
301 jobject o_datasource;
302 int status;
304 /* Look up the DataSource class */
305 c_datasource = (*jvm_env)->FindClass (jvm_env,
306 "org/collectd/api/DataSource");
307 if (c_datasource == NULL)
308 {
309 ERROR ("java plugin: ctoj_data_source: "
310 "FindClass (org/collectd/api/DataSource) failed.");
311 return (NULL);
312 }
314 /* Lookup the `ValueList ()' constructor. */
315 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
316 "<init>", "()V");
317 if (m_datasource_constructor == NULL)
318 {
319 ERROR ("java plugin: ctoj_data_source: Cannot find the "
320 "`DataSource ()' constructor.");
321 return (NULL);
322 }
324 /* Create a new instance. */
325 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
326 m_datasource_constructor);
327 if (o_datasource == NULL)
328 {
329 ERROR ("java plugin: ctoj_data_source: "
330 "Creating a new DataSource instance failed.");
331 return (NULL);
332 }
334 /* Set name via `void setName (String name)' */
335 status = ctoj_string (jvm_env, dsrc->name,
336 c_datasource, o_datasource, "setName");
337 if (status != 0)
338 {
339 ERROR ("java plugin: ctoj_data_source: "
340 "ctoj_string (setName) failed.");
341 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
342 return (NULL);
343 }
345 /* Set type via `void setType (int type)' */
346 status = ctoj_int (jvm_env, dsrc->type,
347 c_datasource, o_datasource, "setType");
348 if (status != 0)
349 {
350 ERROR ("java plugin: ctoj_data_source: "
351 "ctoj_int (setType) failed.");
352 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
353 return (NULL);
354 }
356 /* Set min via `void setMin (double min)' */
357 status = ctoj_double (jvm_env, dsrc->min,
358 c_datasource, o_datasource, "setMin");
359 if (status != 0)
360 {
361 ERROR ("java plugin: ctoj_data_source: "
362 "ctoj_double (setMin) failed.");
363 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
364 return (NULL);
365 }
367 /* Set max via `void setMax (double max)' */
368 status = ctoj_double (jvm_env, dsrc->max,
369 c_datasource, o_datasource, "setMax");
370 if (status != 0)
371 {
372 ERROR ("java plugin: ctoj_data_source: "
373 "ctoj_double (setMax) failed.");
374 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
375 return (NULL);
376 }
378 return (o_datasource);
379 } /* }}} jobject ctoj_data_source */
381 /* Convert a oconfig_value_t to a org/collectd/api/OConfigValue */
382 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
383 oconfig_value_t ocvalue)
384 {
385 jclass c_ocvalue;
386 jmethodID m_ocvalue_constructor;
387 jobject o_argument;
388 jobject o_ocvalue;
390 m_ocvalue_constructor = NULL;
391 o_argument = NULL;
393 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
394 "org/collectd/api/OConfigValue");
395 if (c_ocvalue == NULL)
396 {
397 ERROR ("java plugin: ctoj_oconfig_value: "
398 "FindClass (org/collectd/api/OConfigValue) failed.");
399 return (NULL);
400 }
402 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
403 {
404 jboolean tmp_boolean;
406 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
408 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
409 "<init>", "(Z)V");
410 if (m_ocvalue_constructor == NULL)
411 {
412 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
413 "`OConfigValue (boolean)' constructor.");
414 return (NULL);
415 }
417 return ((*jvm_env)->NewObject (jvm_env,
418 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
419 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
420 else if (ocvalue.type == OCONFIG_TYPE_STRING)
421 {
422 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
423 "<init>", "(Ljava/lang/String;)V");
424 if (m_ocvalue_constructor == NULL)
425 {
426 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
427 "`OConfigValue (String)' constructor.");
428 return (NULL);
429 }
431 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
432 if (o_argument == NULL)
433 {
434 ERROR ("java plugin: ctoj_oconfig_value: "
435 "Creating a String object failed.");
436 return (NULL);
437 }
438 }
439 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
440 {
441 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
442 "<init>", "(Ljava/lang/Number;)V");
443 if (m_ocvalue_constructor == NULL)
444 {
445 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
446 "`OConfigValue (Number)' constructor.");
447 return (NULL);
448 }
450 o_argument = ctoj_jdouble_to_number (jvm_env,
451 (jdouble) ocvalue.value.number);
452 if (o_argument == NULL)
453 {
454 ERROR ("java plugin: ctoj_oconfig_value: "
455 "Creating a Number object failed.");
456 return (NULL);
457 }
458 }
459 else
460 {
461 return (NULL);
462 }
464 assert (m_ocvalue_constructor != NULL);
465 assert (o_argument != NULL);
467 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
468 c_ocvalue, m_ocvalue_constructor, o_argument);
469 if (o_ocvalue == NULL)
470 {
471 ERROR ("java plugin: ctoj_oconfig_value: "
472 "Creating an OConfigValue object failed.");
473 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
474 return (NULL);
475 }
477 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
478 return (o_ocvalue);
479 } /* }}} jobject ctoj_oconfig_value */
481 /* Convert a oconfig_item_t to a org/collectd/api/OConfigItem */
482 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
483 const oconfig_item_t *ci)
484 {
485 jclass c_ocitem;
486 jmethodID m_ocitem_constructor;
487 jmethodID m_addvalue;
488 jmethodID m_addchild;
489 jobject o_key;
490 jobject o_ocitem;
491 int i;
493 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/OConfigItem");
494 if (c_ocitem == NULL)
495 {
496 ERROR ("java plugin: ctoj_oconfig_item: "
497 "FindClass (org/collectd/api/OConfigItem) failed.");
498 return (NULL);
499 }
501 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
502 * {{{ */
503 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
504 "<init>", "(Ljava/lang/String;)V");
505 if (m_ocitem_constructor == NULL)
506 {
507 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
508 "`OConfigItem (String)' constructor.");
509 return (NULL);
510 }
512 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
513 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
514 if (m_addvalue == NULL)
515 {
516 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
517 "`addValue (OConfigValue)' method.");
518 return (NULL);
519 }
521 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
522 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
523 if (m_addchild == NULL)
524 {
525 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
526 "`addChild (OConfigItem)' method.");
527 return (NULL);
528 }
529 /* }}} */
531 /* Create a String object with the key.
532 * Needed for calling the constructor. */
533 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
534 if (o_key == NULL)
535 {
536 ERROR ("java plugin: ctoj_oconfig_item: "
537 "Creating String object failed.");
538 return (NULL);
539 }
541 /* Create an OConfigItem object */
542 o_ocitem = (*jvm_env)->NewObject (jvm_env,
543 c_ocitem, m_ocitem_constructor, o_key);
544 if (o_ocitem == NULL)
545 {
546 ERROR ("java plugin: ctoj_oconfig_item: "
547 "Creating an OConfigItem object failed.");
548 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
549 return (NULL);
550 }
552 /* We don't need the String object any longer.. */
553 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
555 /* Call OConfigItem.addValue for each value */
556 for (i = 0; i < ci->values_num; i++) /* {{{ */
557 {
558 jobject o_value;
560 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
561 if (o_value == NULL)
562 {
563 ERROR ("java plugin: ctoj_oconfig_item: "
564 "Creating an OConfigValue object failed.");
565 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
566 return (NULL);
567 }
569 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
570 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
571 } /* }}} for (i = 0; i < ci->values_num; i++) */
573 /* Call OConfigItem.addChild for each child */
574 for (i = 0; i < ci->children_num; i++) /* {{{ */
575 {
576 jobject o_child;
578 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
579 if (o_child == NULL)
580 {
581 ERROR ("java plugin: ctoj_oconfig_item: "
582 "Creating an OConfigItem object failed.");
583 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
584 return (NULL);
585 }
587 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addchild, o_child);
588 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
589 } /* }}} for (i = 0; i < ci->children_num; i++) */
591 return (o_ocitem);
592 } /* }}} jobject ctoj_oconfig_item */
594 /* Convert a data_set_t to a org/collectd/api/DataSet */
595 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
596 {
597 jclass c_dataset;
598 jmethodID m_constructor;
599 jmethodID m_add;
600 jobject o_type;
601 jobject o_dataset;
602 int i;
604 /* Look up the org/collectd/api/DataSet class */
605 c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet");
606 if (c_dataset == NULL)
607 {
608 ERROR ("java plugin: ctoj_data_set: Looking up the "
609 "org/collectd/api/DataSet class failed.");
610 return (NULL);
611 }
613 /* Search for the `DataSet (String type)' constructor. */
614 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
615 c_dataset, "<init>", "(Ljava/lang/String;)V");
616 if (m_constructor == NULL)
617 {
618 ERROR ("java plugin: ctoj_data_set: Looking up the "
619 "`DataSet (String)' constructor failed.");
620 return (NULL);
621 }
623 /* Search for the `void addDataSource (DataSource)' method. */
624 m_add = (*jvm_env)->GetMethodID (jvm_env,
625 c_dataset, "addDataSource", "(Lorg/collectd/api/DataSource;)V");
626 if (m_add == NULL)
627 {
628 ERROR ("java plugin: ctoj_data_set: Looking up the "
629 "`addDataSource (DataSource)' method failed.");
630 return (NULL);
631 }
633 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
634 if (o_type == NULL)
635 {
636 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
637 return (NULL);
638 }
640 o_dataset = (*jvm_env)->NewObject (jvm_env,
641 c_dataset, m_constructor, o_type);
642 if (o_dataset == NULL)
643 {
644 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
645 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
646 return (NULL);
647 }
649 /* Decrease reference counter on the java.lang.String object. */
650 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
652 for (i = 0; i < ds->ds_num; i++)
653 {
654 jobject o_datasource;
656 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
657 if (o_datasource == NULL)
658 {
659 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
660 ds->type, ds->ds[i].name);
661 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
662 return (NULL);
663 }
665 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
667 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
668 } /* for (i = 0; i < ds->ds_num; i++) */
670 return (o_dataset);
671 } /* }}} jobject ctoj_data_set */
673 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
674 value_t value, int ds_type,
675 jclass class_ptr, jobject object_ptr)
676 {
677 jmethodID m_addvalue;
678 jobject o_number;
680 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
681 "addValue", "(Ljava/lang/Number;)V");
682 if (m_addvalue == NULL)
683 {
684 ERROR ("java plugin: ctoj_value_list_add_value: "
685 "Cannot find method `void addValue (Number)'.");
686 return (-1);
687 }
689 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
690 if (o_number == NULL)
691 {
692 ERROR ("java plugin: ctoj_value_list_add_value: "
693 "ctoj_value_to_number failed.");
694 return (-1);
695 }
697 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
699 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
701 return (0);
702 } /* }}} int ctoj_value_list_add_value */
704 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
705 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
706 {
707 jmethodID m_setdataset;
708 jobject o_dataset;
710 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
711 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
712 "setDataSet", "(Lorg/collectd/api/DataSet;)V");
713 if (m_setdataset == NULL)
714 {
715 ERROR ("java plugin: ctoj_value_list_add_data_set: "
716 "Cannot find the `void setDataSet (DataSet)' method.");
717 return (-1);
718 }
720 /* Create a DataSet object. */
721 o_dataset = ctoj_data_set (jvm_env, ds);
722 if (o_dataset == NULL)
723 {
724 ERROR ("java plugin: ctoj_value_list_add_data_set: "
725 "ctoj_data_set (%s) failed.", ds->type);
726 return (-1);
727 }
729 /* Actually call the method. */
730 (*jvm_env)->CallVoidMethod (jvm_env,
731 o_valuelist, m_setdataset, o_dataset);
733 /* Decrease reference counter on the List<DataSource> object. */
734 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
736 return (0);
737 } /* }}} int ctoj_value_list_add_data_set */
739 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
740 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
741 const data_set_t *ds, const value_list_t *vl)
742 {
743 jclass c_valuelist;
744 jmethodID m_valuelist_constructor;
745 jobject o_valuelist;
746 int status;
747 int i;
749 /* First, create a new ValueList instance..
750 * Look up the class.. */
751 c_valuelist = (*jvm_env)->FindClass (jvm_env,
752 "org/collectd/api/ValueList");
753 if (c_valuelist == NULL)
754 {
755 ERROR ("java plugin: ctoj_value_list: "
756 "FindClass (org/collectd/api/ValueList) failed.");
757 return (NULL);
758 }
760 /* Lookup the `ValueList ()' constructor. */
761 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
762 "<init>", "()V");
763 if (m_valuelist_constructor == NULL)
764 {
765 ERROR ("java plugin: ctoj_value_list: Cannot find the "
766 "`ValueList ()' constructor.");
767 return (NULL);
768 }
770 /* Create a new instance. */
771 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
772 m_valuelist_constructor);
773 if (o_valuelist == NULL)
774 {
775 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
776 "failed.");
777 return (NULL);
778 }
780 status = ctoj_value_list_add_data_set (jvm_env,
781 c_valuelist, o_valuelist, ds);
782 if (status != 0)
783 {
784 ERROR ("java plugin: ctoj_value_list: "
785 "ctoj_value_list_add_data_set failed.");
786 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
787 return (NULL);
788 }
790 /* Set the strings.. */
791 #define SET_STRING(str,method_name) do { \
792 status = ctoj_string (jvm_env, str, \
793 c_valuelist, o_valuelist, method_name); \
794 if (status != 0) { \
795 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
796 method_name); \
797 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
798 return (NULL); \
799 } } while (0)
801 SET_STRING (vl->host, "setHost");
802 SET_STRING (vl->plugin, "setPlugin");
803 SET_STRING (vl->plugin_instance, "setPluginInstance");
804 SET_STRING (vl->type, "setType");
805 SET_STRING (vl->type_instance, "setTypeInstance");
807 #undef SET_STRING
809 /* Set the `time' member. Java stores time in milliseconds. */
810 status = ctoj_long (jvm_env, ((jlong) vl->time) * ((jlong) 1000),
811 c_valuelist, o_valuelist, "setTime");
812 if (status != 0)
813 {
814 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
815 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
816 return (NULL);
817 }
819 /* Set the `interval' member.. */
820 status = ctoj_long (jvm_env, (jlong) vl->interval,
821 c_valuelist, o_valuelist, "setInterval");
822 if (status != 0)
823 {
824 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
825 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
826 return (NULL);
827 }
829 for (i = 0; i < vl->values_len; i++)
830 {
831 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
832 c_valuelist, o_valuelist);
833 if (status != 0)
834 {
835 ERROR ("java plugin: ctoj_value_list: "
836 "ctoj_value_list_add_value failed.");
837 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
838 return (NULL);
839 }
840 }
842 return (o_valuelist);
843 } /* }}} jobject ctoj_value_list */
845 /* Convert a notification_t to a org/collectd/api/Notification */
846 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
847 const notification_t *n)
848 {
849 jclass c_notification;
850 jmethodID m_constructor;
851 jobject o_notification;
852 int status;
854 /* First, create a new Notification instance..
855 * Look up the class.. */
856 c_notification = (*jvm_env)->FindClass (jvm_env,
857 "org/collectd/api/Notification");
858 if (c_notification == NULL)
859 {
860 ERROR ("java plugin: ctoj_notification: "
861 "FindClass (org/collectd/api/Notification) failed.");
862 return (NULL);
863 }
865 /* Lookup the `Notification ()' constructor. */
866 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
867 "<init>", "()V");
868 if (m_constructor == NULL)
869 {
870 ERROR ("java plugin: ctoj_notification: Cannot find the "
871 "`Notification ()' constructor.");
872 return (NULL);
873 }
875 /* Create a new instance. */
876 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
877 m_constructor);
878 if (o_notification == NULL)
879 {
880 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
881 "instance failed.");
882 return (NULL);
883 }
885 /* Set the strings.. */
886 #define SET_STRING(str,method_name) do { \
887 status = ctoj_string (jvm_env, str, \
888 c_notification, o_notification, method_name); \
889 if (status != 0) { \
890 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
891 method_name); \
892 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
893 return (NULL); \
894 } } while (0)
896 SET_STRING (n->host, "setHost");
897 SET_STRING (n->plugin, "setPlugin");
898 SET_STRING (n->plugin_instance, "setPluginInstance");
899 SET_STRING (n->type, "setType");
900 SET_STRING (n->type_instance, "setTypeInstance");
901 SET_STRING (n->message, "setMessage");
903 #undef SET_STRING
905 /* Set the `time' member. Java stores time in milliseconds. */
906 status = ctoj_long (jvm_env, ((jlong) n->time) * ((jlong) 1000),
907 c_notification, o_notification, "setTime");
908 if (status != 0)
909 {
910 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
911 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
912 return (NULL);
913 }
915 /* Set the `interval' member.. */
916 status = ctoj_int (jvm_env, (jint) n->severity,
917 c_notification, o_notification, "setSeverity");
918 if (status != 0)
919 {
920 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
921 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
922 return (NULL);
923 }
925 return (o_notification);
926 } /* }}} jobject ctoj_notification */
928 /*
929 * Java to C conversion functions
930 */
931 /* Call a `String <method> ()' method. */
932 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
933 char *buffer, size_t buffer_size, int empty_okay,
934 jclass class_ptr, jobject object_ptr, const char *method_name)
935 {
936 jmethodID method_id;
937 jobject string_obj;
938 const char *c_str;
940 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
941 method_name, "()Ljava/lang/String;");
942 if (method_id == NULL)
943 {
944 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
945 method_name);
946 return (-1);
947 }
949 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
950 if ((string_obj == NULL) && (empty_okay == 0))
951 {
952 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
953 method_name);
954 return (-1);
955 }
956 else if ((string_obj == NULL) && (empty_okay != 0))
957 {
958 memset (buffer, 0, buffer_size);
959 return (0);
960 }
962 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
963 if (c_str == NULL)
964 {
965 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
966 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
967 return (-1);
968 }
970 sstrncpy (buffer, c_str, buffer_size);
972 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
973 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
975 return (0);
976 } /* }}} int jtoc_string */
978 /* Call an `int <method> ()' method. */
979 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
980 jint *ret_value,
981 jclass class_ptr, jobject object_ptr, const char *method_name)
982 {
983 jmethodID method_id;
985 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
986 method_name, "()I");
987 if (method_id == NULL)
988 {
989 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
990 method_name);
991 return (-1);
992 }
994 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
996 return (0);
997 } /* }}} int jtoc_int */
999 /* Call a `long <method> ()' method. */
1000 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
1001 jlong *ret_value,
1002 jclass class_ptr, jobject object_ptr, const char *method_name)
1003 {
1004 jmethodID method_id;
1006 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1007 method_name, "()J");
1008 if (method_id == NULL)
1009 {
1010 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1011 method_name);
1012 return (-1);
1013 }
1015 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1017 return (0);
1018 } /* }}} int jtoc_long */
1020 /* Call a `double <method> ()' method. */
1021 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1022 jdouble *ret_value,
1023 jclass class_ptr, jobject object_ptr, const char *method_name)
1024 {
1025 jmethodID method_id;
1027 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1028 method_name, "()D");
1029 if (method_id == NULL)
1030 {
1031 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1032 method_name);
1033 return (-1);
1034 }
1036 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1038 return (0);
1039 } /* }}} int jtoc_double */
1041 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1042 value_t *ret_value, int ds_type, jobject object_ptr)
1043 {
1044 jclass class_ptr;
1045 int status;
1047 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1049 if (ds_type == DS_TYPE_GAUGE)
1050 {
1051 jdouble tmp_double;
1053 status = jtoc_double (jvm_env, &tmp_double,
1054 class_ptr, object_ptr, "doubleValue");
1055 if (status != 0)
1056 {
1057 ERROR ("java plugin: jtoc_value: "
1058 "jtoc_double failed.");
1059 return (-1);
1060 }
1061 (*ret_value).gauge = (gauge_t) tmp_double;
1062 }
1063 else
1064 {
1065 jlong tmp_long;
1067 status = jtoc_long (jvm_env, &tmp_long,
1068 class_ptr, object_ptr, "longValue");
1069 if (status != 0)
1070 {
1071 ERROR ("java plugin: jtoc_value: "
1072 "jtoc_long failed.");
1073 return (-1);
1074 }
1076 if (ds_type == DS_TYPE_DERIVE)
1077 (*ret_value).derive = (derive_t) tmp_long;
1078 else if (ds_type == DS_TYPE_ABSOLUTE)
1079 (*ret_value).absolute = (absolute_t) tmp_long;
1080 else
1081 (*ret_value).counter = (counter_t) tmp_long;
1082 }
1084 return (0);
1085 } /* }}} int jtoc_value */
1087 /* Read a List<Number>, convert it to `value_t' and add it to the given
1088 * `value_list_t'. */
1089 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1090 const data_set_t *ds, value_list_t *vl,
1091 jclass class_ptr, jobject object_ptr)
1092 {
1093 jmethodID m_getvalues;
1094 jmethodID m_toarray;
1095 jobject o_list;
1096 jobjectArray o_number_array;
1098 value_t *values;
1099 int values_num;
1100 int i;
1102 values_num = ds->ds_num;
1104 values = NULL;
1105 o_number_array = NULL;
1106 o_list = NULL;
1108 #define BAIL_OUT(status) \
1109 free (values); \
1110 if (o_number_array != NULL) \
1111 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1112 if (o_list != NULL) \
1113 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1114 return (status);
1116 /* Call: List<Number> ValueList.getValues () */
1117 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1118 "getValues", "()Ljava/util/List;");
1119 if (m_getvalues == NULL)
1120 {
1121 ERROR ("java plugin: jtoc_values_array: "
1122 "Cannot find method `List getValues ()'.");
1123 BAIL_OUT (-1);
1124 }
1126 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1127 if (o_list == NULL)
1128 {
1129 ERROR ("java plugin: jtoc_values_array: "
1130 "CallObjectMethod (getValues) failed.");
1131 BAIL_OUT (-1);
1132 }
1134 /* Call: Number[] List.toArray () */
1135 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1136 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1137 "toArray", "()[Ljava/lang/Object;");
1138 if (m_toarray == NULL)
1139 {
1140 ERROR ("java plugin: jtoc_values_array: "
1141 "Cannot find method `Object[] toArray ()'.");
1142 BAIL_OUT (-1);
1143 }
1145 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1146 if (o_number_array == NULL)
1147 {
1148 ERROR ("java plugin: jtoc_values_array: "
1149 "CallObjectMethod (toArray) failed.");
1150 BAIL_OUT (-1);
1151 }
1153 values = (value_t *) calloc (values_num, sizeof (value_t));
1154 if (values == NULL)
1155 {
1156 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1157 BAIL_OUT (-1);
1158 }
1160 for (i = 0; i < values_num; i++)
1161 {
1162 jobject o_number;
1163 int status;
1165 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1166 o_number_array, (jsize) i);
1167 if (o_number == NULL)
1168 {
1169 ERROR ("java plugin: jtoc_values_array: "
1170 "GetObjectArrayElement (%i) failed.", i);
1171 BAIL_OUT (-1);
1172 }
1174 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1175 if (status != 0)
1176 {
1177 ERROR ("java plugin: jtoc_values_array: "
1178 "jtoc_value (%i) failed.", i);
1179 BAIL_OUT (-1);
1180 }
1181 } /* for (i = 0; i < values_num; i++) */
1183 vl->values = values;
1184 vl->values_len = values_num;
1186 #undef BAIL_OUT
1187 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1188 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1189 return (0);
1190 } /* }}} int jtoc_values_array */
1192 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1193 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1194 jobject object_ptr)
1195 {
1196 jclass class_ptr;
1197 int status;
1198 jlong tmp_long;
1199 const data_set_t *ds;
1201 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1202 if (class_ptr == NULL)
1203 {
1204 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1205 return (-1);
1206 }
1208 /* eo == empty okay */
1209 #define SET_STRING(buffer,method, eo) do { \
1210 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1211 class_ptr, object_ptr, method); \
1212 if (status != 0) { \
1213 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1214 method); \
1215 return (-1); \
1216 } } while (0)
1218 SET_STRING(vl->type, "getType", /* empty = */ 0);
1220 ds = plugin_get_ds (vl->type);
1221 if (ds == NULL)
1222 {
1223 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1224 "Please consult the types.db(5) manpage for mor information.",
1225 vl->type);
1226 return (-1);
1227 }
1229 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1230 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1231 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1232 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1234 #undef SET_STRING
1236 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1237 if (status != 0)
1238 {
1239 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1240 return (-1);
1241 }
1242 /* Java measures time in milliseconds. */
1243 vl->time = (time_t) (tmp_long / ((jlong) 1000));
1245 status = jtoc_long (jvm_env, &tmp_long,
1246 class_ptr, object_ptr, "getInterval");
1247 if (status != 0)
1248 {
1249 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1250 return (-1);
1251 }
1252 vl->interval = (int) tmp_long;
1254 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1255 if (status != 0)
1256 {
1257 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1258 return (-1);
1259 }
1261 return (0);
1262 } /* }}} int jtoc_value_list */
1264 /* Convert a org/collectd/api/Notification to a notification_t. */
1265 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1266 jobject object_ptr)
1267 {
1268 jclass class_ptr;
1269 int status;
1270 jlong tmp_long;
1271 jint tmp_int;
1273 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1274 if (class_ptr == NULL)
1275 {
1276 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1277 return (-1);
1278 }
1280 /* eo == empty okay */
1281 #define SET_STRING(buffer,method, eo) do { \
1282 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1283 class_ptr, object_ptr, method); \
1284 if (status != 0) { \
1285 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1286 method); \
1287 return (-1); \
1288 } } while (0)
1290 SET_STRING (n->host, "getHost", /* empty = */ 1);
1291 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1292 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1293 SET_STRING (n->type, "getType", /* empty = */ 1);
1294 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1295 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1297 #undef SET_STRING
1299 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1300 if (status != 0)
1301 {
1302 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1303 return (-1);
1304 }
1305 /* Java measures time in milliseconds. */
1306 n->time = (time_t) (tmp_long / ((jlong) 1000));
1308 status = jtoc_int (jvm_env, &tmp_int,
1309 class_ptr, object_ptr, "getSeverity");
1310 if (status != 0)
1311 {
1312 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1313 return (-1);
1314 }
1315 n->severity = (int) tmp_int;
1317 return (0);
1318 } /* }}} int jtoc_notification */
1319 /*
1320 * Functions accessible from Java
1321 */
1322 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1323 jobject this, jobject java_vl)
1324 {
1325 value_list_t vl = VALUE_LIST_INIT;
1326 int status;
1328 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1330 status = jtoc_value_list (jvm_env, &vl, java_vl);
1331 if (status != 0)
1332 {
1333 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1334 return (-1);
1335 }
1337 status = plugin_dispatch_values (&vl);
1339 sfree (vl.values);
1341 return (status);
1342 } /* }}} jint cjni_api_dispatch_values */
1344 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1345 jobject this, jobject o_notification)
1346 {
1347 notification_t n;
1348 int status;
1350 memset (&n, 0, sizeof (n));
1351 n.meta = NULL;
1353 status = jtoc_notification (jvm_env, &n, o_notification);
1354 if (status != 0)
1355 {
1356 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1357 return (-1);
1358 }
1360 status = plugin_dispatch_notification (&n);
1362 return (status);
1363 } /* }}} jint cjni_api_dispatch_notification */
1365 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1366 jobject this, jobject o_string_type)
1367 {
1368 const char *ds_name;
1369 const data_set_t *ds;
1370 jobject o_dataset;
1372 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1373 if (ds_name == NULL)
1374 {
1375 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1376 return (NULL);
1377 }
1379 ds = plugin_get_ds (ds_name);
1380 DEBUG ("java plugin: cjni_api_get_ds: "
1381 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1383 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1385 if (ds == NULL)
1386 return (NULL);
1388 o_dataset = ctoj_data_set (jvm_env, ds);
1389 return (o_dataset);
1390 } /* }}} jint cjni_api_get_ds */
1392 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1393 jobject this, jobject o_name, jobject o_config)
1394 {
1395 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1396 } /* }}} jint cjni_api_register_config */
1398 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1399 jobject this, jobject o_name, jobject o_config)
1400 {
1401 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1402 } /* }}} jint cjni_api_register_init */
1404 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1405 jobject this, jobject o_name, jobject o_read)
1406 {
1407 user_data_t ud;
1408 cjni_callback_info_t *cbi;
1410 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1411 if (cbi == NULL)
1412 return (-1);
1414 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1416 memset (&ud, 0, sizeof (ud));
1417 ud.data = (void *) cbi;
1418 ud.free_func = cjni_callback_info_destroy;
1420 plugin_register_complex_read (cbi->name, cjni_read,
1421 /* interval = */ NULL, &ud);
1423 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1425 return (0);
1426 } /* }}} jint cjni_api_register_read */
1428 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1429 jobject this, jobject o_name, jobject o_write)
1430 {
1431 user_data_t ud;
1432 cjni_callback_info_t *cbi;
1434 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1435 if (cbi == NULL)
1436 return (-1);
1438 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1440 memset (&ud, 0, sizeof (ud));
1441 ud.data = (void *) cbi;
1442 ud.free_func = cjni_callback_info_destroy;
1444 plugin_register_write (cbi->name, cjni_write, &ud);
1446 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1448 return (0);
1449 } /* }}} jint cjni_api_register_write */
1451 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1452 jobject this, jobject o_name, jobject o_flush)
1453 {
1454 user_data_t ud;
1455 cjni_callback_info_t *cbi;
1457 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1458 if (cbi == NULL)
1459 return (-1);
1461 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1463 memset (&ud, 0, sizeof (ud));
1464 ud.data = (void *) cbi;
1465 ud.free_func = cjni_callback_info_destroy;
1467 plugin_register_flush (cbi->name, cjni_flush, &ud);
1469 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1471 return (0);
1472 } /* }}} jint cjni_api_register_flush */
1474 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1475 jobject this, jobject o_name, jobject o_shutdown)
1476 {
1477 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1478 CB_TYPE_SHUTDOWN));
1479 } /* }}} jint cjni_api_register_shutdown */
1481 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1482 jobject this, jobject o_name, jobject o_log)
1483 {
1484 user_data_t ud;
1485 cjni_callback_info_t *cbi;
1487 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1488 if (cbi == NULL)
1489 return (-1);
1491 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1493 memset (&ud, 0, sizeof (ud));
1494 ud.data = (void *) cbi;
1495 ud.free_func = cjni_callback_info_destroy;
1497 plugin_register_log (cbi->name, cjni_log, &ud);
1499 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1501 return (0);
1502 } /* }}} jint cjni_api_register_log */
1504 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1505 jobject this, jobject o_name, jobject o_notification)
1506 {
1507 user_data_t ud;
1508 cjni_callback_info_t *cbi;
1510 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1511 CB_TYPE_NOTIFICATION);
1512 if (cbi == NULL)
1513 return (-1);
1515 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1517 memset (&ud, 0, sizeof (ud));
1518 ud.data = (void *) cbi;
1519 ud.free_func = cjni_callback_info_destroy;
1521 plugin_register_notification (cbi->name, cjni_notification, &ud);
1523 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1525 return (0);
1526 } /* }}} jint cjni_api_register_notification */
1528 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1529 jobject this, jobject o_name, jobject o_match, int type)
1530 {
1531 int status;
1532 const char *c_name;
1534 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1535 if (c_name == NULL)
1536 {
1537 ERROR ("java plugin: cjni_api_register_match_target: "
1538 "GetStringUTFChars failed.");
1539 return (-1);
1540 }
1542 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1543 if (status != 0)
1544 {
1545 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1546 return (-1);
1547 }
1549 if (type == CB_TYPE_MATCH)
1550 {
1551 match_proc_t m_proc;
1553 memset (&m_proc, 0, sizeof (m_proc));
1554 m_proc.create = cjni_match_target_create;
1555 m_proc.destroy = cjni_match_target_destroy;
1556 m_proc.match = (void *) cjni_match_target_invoke;
1558 status = fc_register_match (c_name, m_proc);
1559 }
1560 else if (type == CB_TYPE_TARGET)
1561 {
1562 target_proc_t t_proc;
1564 memset (&t_proc, 0, sizeof (t_proc));
1565 t_proc.create = cjni_match_target_create;
1566 t_proc.destroy = cjni_match_target_destroy;
1567 t_proc.invoke = cjni_match_target_invoke;
1569 status = fc_register_target (c_name, t_proc);
1570 }
1571 else
1572 {
1573 ERROR ("java plugin: cjni_api_register_match_target: "
1574 "Don't know whether to create a match or a target.");
1575 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1576 return (-1);
1577 }
1579 if (status != 0)
1580 {
1581 ERROR ("java plugin: cjni_api_register_match_target: "
1582 "%s failed.",
1583 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1584 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1585 return (-1);
1586 }
1588 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1590 return (0);
1591 } /* }}} jint cjni_api_register_match_target */
1593 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1594 jobject this, jobject o_name, jobject o_match)
1595 {
1596 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1597 CB_TYPE_MATCH));
1598 } /* }}} jint cjni_api_register_match */
1600 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1601 jobject this, jobject o_name, jobject o_target)
1602 {
1603 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1604 CB_TYPE_TARGET));
1605 } /* }}} jint cjni_api_register_target */
1607 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1608 jobject this, jint severity, jobject o_message)
1609 {
1610 const char *c_str;
1612 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1613 if (c_str == NULL)
1614 {
1615 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1616 return;
1617 }
1619 if (severity < LOG_ERR)
1620 severity = LOG_ERR;
1621 if (severity > LOG_DEBUG)
1622 severity = LOG_DEBUG;
1624 plugin_log (severity, "%s", c_str);
1626 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1627 } /* }}} void cjni_api_log */
1629 /* List of ``native'' functions, i. e. C-functions that can be called from
1630 * Java. */
1631 static JNINativeMethod jni_api_functions[] = /* {{{ */
1632 {
1633 { "dispatchValues",
1634 "(Lorg/collectd/api/ValueList;)I",
1635 cjni_api_dispatch_values },
1637 { "dispatchNotification",
1638 "(Lorg/collectd/api/Notification;)I",
1639 cjni_api_dispatch_notification },
1641 { "getDS",
1642 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1643 cjni_api_get_ds },
1645 { "registerConfig",
1646 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1647 cjni_api_register_config },
1649 { "registerInit",
1650 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1651 cjni_api_register_init },
1653 { "registerRead",
1654 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1655 cjni_api_register_read },
1657 { "registerWrite",
1658 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1659 cjni_api_register_write },
1661 { "registerFlush",
1662 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1663 cjni_api_register_flush },
1665 { "registerShutdown",
1666 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1667 cjni_api_register_shutdown },
1669 { "registerLog",
1670 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1671 cjni_api_register_log },
1673 { "registerNotification",
1674 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1675 cjni_api_register_notification },
1677 { "registerMatch",
1678 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1679 cjni_api_register_match },
1681 { "registerTarget",
1682 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1683 cjni_api_register_target },
1685 { "log",
1686 "(ILjava/lang/String;)V",
1687 cjni_api_log },
1688 };
1689 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1690 / sizeof (jni_api_functions[0]);
1691 /* }}} */
1693 /*
1694 * Functions
1695 */
1696 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1697 * all registration functions. */
1698 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1699 jobject o_name, jobject o_callback, int type)
1700 {
1701 const char *c_name;
1702 cjni_callback_info_t *cbi;
1703 const char *method_name;
1704 const char *method_signature;
1706 switch (type)
1707 {
1708 case CB_TYPE_CONFIG:
1709 method_name = "config";
1710 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1711 break;
1713 case CB_TYPE_INIT:
1714 method_name = "init";
1715 method_signature = "()I";
1716 break;
1718 case CB_TYPE_READ:
1719 method_name = "read";
1720 method_signature = "()I";
1721 break;
1723 case CB_TYPE_WRITE:
1724 method_name = "write";
1725 method_signature = "(Lorg/collectd/api/ValueList;)I";
1726 break;
1728 case CB_TYPE_FLUSH:
1729 method_name = "flush";
1730 method_signature = "(ILjava/lang/String;)I";
1731 break;
1733 case CB_TYPE_SHUTDOWN:
1734 method_name = "shutdown";
1735 method_signature = "()I";
1736 break;
1738 case CB_TYPE_LOG:
1739 method_name = "log";
1740 method_signature = "(ILjava/lang/String;)V";
1741 break;
1743 case CB_TYPE_NOTIFICATION:
1744 method_name = "notification";
1745 method_signature = "(Lorg/collectd/api/Notification;)I";
1746 break;
1748 case CB_TYPE_MATCH:
1749 method_name = "createMatch";
1750 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1751 "Lorg/collectd/api/CollectdMatchInterface;";
1752 break;
1754 case CB_TYPE_TARGET:
1755 method_name = "createTarget";
1756 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1757 "Lorg/collectd/api/CollectdTargetInterface;";
1758 break;
1760 default:
1761 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1762 type);
1763 return (NULL);
1764 }
1766 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1767 if (c_name == NULL)
1768 {
1769 ERROR ("java plugin: cjni_callback_info_create: "
1770 "GetStringUTFChars failed.");
1771 return (NULL);
1772 }
1774 cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1775 if (cbi == NULL)
1776 {
1777 ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1778 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1779 return (NULL);
1780 }
1781 memset (cbi, 0, sizeof (*cbi));
1782 cbi->type = type;
1784 cbi->name = strdup (c_name);
1785 if (cbi->name == NULL)
1786 {
1787 pthread_mutex_unlock (&java_callbacks_lock);
1788 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1789 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1790 return (NULL);
1791 }
1793 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1795 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1796 if (cbi->object == NULL)
1797 {
1798 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1799 free (cbi);
1800 return (NULL);
1801 }
1803 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1804 if (cbi->class == NULL)
1805 {
1806 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1807 free (cbi);
1808 return (NULL);
1809 }
1811 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1812 method_name, method_signature);
1813 if (cbi->method == NULL)
1814 {
1815 ERROR ("java plugin: cjni_callback_info_create: "
1816 "Cannot find the `%s' method with signature `%s'.",
1817 method_name, method_signature);
1818 free (cbi);
1819 return (NULL);
1820 }
1822 return (cbi);
1823 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1825 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1826 * to the global `java_callbacks' variable. This is used for `config', `init',
1827 * and `shutdown' callbacks. */
1828 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1829 jobject o_name, jobject o_callback, int type)
1830 {
1831 cjni_callback_info_t *cbi;
1832 cjni_callback_info_t *tmp;
1833 #if COLLECT_DEBUG
1834 const char *type_str;
1835 #endif
1837 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1838 if (cbi == NULL)
1839 return (-1);
1841 #if COLLECT_DEBUG
1842 switch (type)
1843 {
1844 case CB_TYPE_CONFIG:
1845 type_str = "config";
1846 break;
1848 case CB_TYPE_INIT:
1849 type_str = "init";
1850 break;
1852 case CB_TYPE_SHUTDOWN:
1853 type_str = "shutdown";
1854 break;
1856 case CB_TYPE_MATCH:
1857 type_str = "match";
1858 break;
1860 case CB_TYPE_TARGET:
1861 type_str = "target";
1862 break;
1864 default:
1865 type_str = "<unknown>";
1866 }
1867 DEBUG ("java plugin: Registering new %s callback: %s",
1868 type_str, cbi->name);
1869 #endif
1871 pthread_mutex_lock (&java_callbacks_lock);
1873 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1874 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1875 if (tmp == NULL)
1876 {
1877 pthread_mutex_unlock (&java_callbacks_lock);
1878 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1880 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1881 free (cbi);
1883 return (-1);
1884 }
1885 java_callbacks = tmp;
1886 java_callbacks[java_callbacks_num] = *cbi;
1887 java_callbacks_num++;
1889 pthread_mutex_unlock (&java_callbacks_lock);
1891 free (cbi);
1892 return (0);
1893 } /* }}} int cjni_callback_register */
1895 /* Callback for `pthread_key_create'. It frees the data contained in
1896 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1897 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1898 {
1899 cjni_jvm_env_t *cjni_env;
1901 if (args == NULL)
1902 return;
1904 cjni_env = (cjni_jvm_env_t *) args;
1906 if (cjni_env->reference_counter > 0)
1907 {
1908 ERROR ("java plugin: cjni_jvm_env_destroy: "
1909 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1910 }
1912 if (cjni_env->jvm_env != NULL)
1913 {
1914 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1915 (void *) cjni_env->jvm_env);
1916 }
1918 /* The pointer is allocated in `cjni_thread_attach' */
1919 free (cjni_env);
1920 } /* }}} void cjni_jvm_env_destroy */
1922 /* Register ``native'' functions with the JVM. Native functions are C-functions
1923 * that can be called by Java code. */
1924 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1925 {
1926 jclass api_class_ptr;
1927 int status;
1929 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1930 if (api_class_ptr == NULL)
1931 {
1932 ERROR ("cjni_init_native: Cannot find API class `org/collectd/api/Collectd'.");
1933 return (-1);
1934 }
1936 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1937 jni_api_functions, (jint) jni_api_functions_num);
1938 if (status != 0)
1939 {
1940 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1941 return (-1);
1942 }
1944 return (0);
1945 } /* }}} int cjni_init_native */
1947 /* Create the JVM. This is called when the first thread tries to access the JVM
1948 * via cjni_thread_attach. */
1949 static int cjni_create_jvm (void) /* {{{ */
1950 {
1951 JNIEnv *jvm_env;
1952 JavaVMInitArgs vm_args;
1953 JavaVMOption vm_options[jvm_argc];
1955 int status;
1956 size_t i;
1958 if (jvm != NULL)
1959 return (0);
1961 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1962 if (status != 0)
1963 {
1964 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1965 "with status %i.", status);
1966 return (-1);
1967 }
1969 jvm_env = NULL;
1971 memset (&vm_args, 0, sizeof (vm_args));
1972 vm_args.version = JNI_VERSION_1_2;
1973 vm_args.options = vm_options;
1974 vm_args.nOptions = (jint) jvm_argc;
1976 for (i = 0; i < jvm_argc; i++)
1977 {
1978 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
1979 i, jvm_argv[i]);
1980 vm_args.options[i].optionString = jvm_argv[i];
1981 }
1983 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
1984 if (status != 0)
1985 {
1986 ERROR ("java plugin: cjni_create_jvm: "
1987 "JNI_CreateJavaVM failed with status %i.",
1988 status);
1989 return (-1);
1990 }
1991 assert (jvm != NULL);
1992 assert (jvm_env != NULL);
1994 /* Call RegisterNatives */
1995 status = cjni_init_native (jvm_env);
1996 if (status != 0)
1997 {
1998 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
1999 return (-1);
2000 }
2002 DEBUG ("java plugin: The JVM has been created.");
2003 return (0);
2004 } /* }}} int cjni_create_jvm */
2006 /* Increase the reference counter to the JVM for this thread. If it was zero,
2007 * attach the JVM first. */
2008 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2009 {
2010 cjni_jvm_env_t *cjni_env;
2011 JNIEnv *jvm_env;
2013 /* If we're the first thread to access the JVM, we'll have to create it
2014 * first.. */
2015 if (jvm == NULL)
2016 {
2017 int status;
2019 status = cjni_create_jvm ();
2020 if (status != 0)
2021 {
2022 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2023 return (NULL);
2024 }
2025 }
2026 assert (jvm != NULL);
2028 cjni_env = pthread_getspecific (jvm_env_key);
2029 if (cjni_env == NULL)
2030 {
2031 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2032 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
2033 if (cjni_env == NULL)
2034 {
2035 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
2036 return (NULL);
2037 }
2038 memset (cjni_env, 0, sizeof (*cjni_env));
2039 cjni_env->reference_counter = 0;
2040 cjni_env->jvm_env = NULL;
2042 pthread_setspecific (jvm_env_key, cjni_env);
2043 }
2045 if (cjni_env->reference_counter > 0)
2046 {
2047 cjni_env->reference_counter++;
2048 jvm_env = cjni_env->jvm_env;
2049 }
2050 else
2051 {
2052 int status;
2053 JavaVMAttachArgs args;
2055 assert (cjni_env->jvm_env == NULL);
2057 memset (&args, 0, sizeof (args));
2058 args.version = JNI_VERSION_1_2;
2060 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2061 if (status != 0)
2062 {
2063 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2064 "with status %i.", status);
2065 return (NULL);
2066 }
2068 cjni_env->reference_counter = 1;
2069 cjni_env->jvm_env = jvm_env;
2070 }
2072 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2073 cjni_env->reference_counter);
2074 assert (jvm_env != NULL);
2075 return (jvm_env);
2076 } /* }}} JNIEnv *cjni_thread_attach */
2078 /* Decrease the reference counter of this thread. If it reaches zero, detach
2079 * from the JVM. */
2080 static int cjni_thread_detach (void) /* {{{ */
2081 {
2082 cjni_jvm_env_t *cjni_env;
2083 int status;
2085 cjni_env = pthread_getspecific (jvm_env_key);
2086 if (cjni_env == NULL)
2087 {
2088 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2089 return (-1);
2090 }
2092 assert (cjni_env->reference_counter > 0);
2093 assert (cjni_env->jvm_env != NULL);
2095 cjni_env->reference_counter--;
2096 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2097 cjni_env->reference_counter);
2099 if (cjni_env->reference_counter > 0)
2100 return (0);
2102 status = (*jvm)->DetachCurrentThread (jvm);
2103 if (status != 0)
2104 {
2105 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2106 "with status %i.", status);
2107 }
2109 cjni_env->reference_counter = 0;
2110 cjni_env->jvm_env = NULL;
2112 return (0);
2113 } /* }}} JNIEnv *cjni_thread_attach */
2115 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2116 {
2117 char **tmp;
2119 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2120 {
2121 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2122 return (-1);
2123 }
2125 if (jvm != NULL)
2126 {
2127 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2128 "`LoadPlugin' options! The JVM is already started and I have to "
2129 "ignore this argument: %s",
2130 ci->values[0].value.string);
2131 return (-1);
2132 }
2134 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2135 if (tmp == NULL)
2136 {
2137 ERROR ("java plugin: realloc failed.");
2138 return (-1);
2139 }
2140 jvm_argv = tmp;
2142 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2143 if (jvm_argv[jvm_argc] == NULL)
2144 {
2145 ERROR ("java plugin: strdup failed.");
2146 return (-1);
2147 }
2148 jvm_argc++;
2150 return (0);
2151 } /* }}} int cjni_config_add_jvm_arg */
2153 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2154 {
2155 JNIEnv *jvm_env;
2156 java_plugin_class_t *class;
2157 jmethodID constructor_id;
2158 jobject tmp_object;
2160 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2161 {
2162 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2163 return (-1);
2164 }
2166 jvm_env = cjni_thread_attach ();
2167 if (jvm_env == NULL)
2168 return (-1);
2170 class = (java_plugin_class_t *) realloc (java_classes_list,
2171 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2172 if (class == NULL)
2173 {
2174 ERROR ("java plugin: realloc failed.");
2175 cjni_thread_detach ();
2176 return (-1);
2177 }
2178 java_classes_list = class;
2179 class = java_classes_list + java_classes_list_len;
2181 memset (class, 0, sizeof (*class));
2182 class->name = strdup (ci->values[0].value.string);
2183 if (class->name == NULL)
2184 {
2185 ERROR ("java plugin: strdup failed.");
2186 cjni_thread_detach ();
2187 return (-1);
2188 }
2189 class->class = NULL;
2190 class->object = NULL;
2192 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2193 thorough the Java community, but (Sun's) `FindClass' and friends need
2194 slashes. */
2195 size_t i;
2196 for (i = 0; class->name[i] != 0; i++)
2197 if (class->name[i] == '.')
2198 class->name[i] = '/';
2199 }
2201 DEBUG ("java plugin: Loading class %s", class->name);
2203 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2204 if (class->class == NULL)
2205 {
2206 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2207 class->name);
2208 cjni_thread_detach ();
2209 free (class->name);
2210 return (-1);
2211 }
2213 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2214 "<init>", "()V");
2215 if (constructor_id == NULL)
2216 {
2217 ERROR ("java plugin: cjni_config_load_plugin: "
2218 "Could not find the constructor for `%s'.",
2219 class->name);
2220 cjni_thread_detach ();
2221 free (class->name);
2222 return (-1);
2223 }
2225 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2226 constructor_id);
2227 if (tmp_object != NULL)
2228 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2229 else
2230 class->object = NULL;
2231 if (class->object == NULL)
2232 {
2233 ERROR ("java plugin: cjni_config_load_plugin: "
2234 "Could create a new `%s' object.",
2235 class->name);
2236 cjni_thread_detach ();
2237 free (class->name);
2238 return (-1);
2239 }
2241 cjni_thread_detach ();
2243 java_classes_list_len++;
2245 return (0);
2246 } /* }}} int cjni_config_load_plugin */
2248 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2249 {
2250 JNIEnv *jvm_env;
2251 cjni_callback_info_t *cbi;
2252 jobject o_ocitem;
2253 const char *name;
2254 int status;
2255 size_t i;
2257 jclass class;
2258 jmethodID method;
2260 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2261 {
2262 WARNING ("java plugin: `Plugin' blocks "
2263 "need exactly one string argument.");
2264 return (-1);
2265 }
2267 name = ci->values[0].value.string;
2269 cbi = NULL;
2270 for (i = 0; i < java_callbacks_num; i++)
2271 {
2272 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2273 continue;
2275 if (strcmp (name, java_callbacks[i].name) != 0)
2276 continue;
2278 cbi = java_callbacks + i;
2279 break;
2280 }
2282 if (cbi == NULL)
2283 {
2284 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2285 "configuration callback has been registered. Please make sure, the "
2286 "`LoadPlugin' lines precede the `Plugin' blocks.",
2287 name);
2288 return (0);
2289 }
2291 DEBUG ("java plugin: Configuring %s", name);
2293 jvm_env = cjni_thread_attach ();
2294 if (jvm_env == NULL)
2295 return (-1);
2297 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2298 if (o_ocitem == NULL)
2299 {
2300 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2301 cjni_thread_detach ();
2302 return (-1);
2303 }
2305 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2306 method = (*jvm_env)->GetMethodID (jvm_env, class,
2307 "config", "(Lorg/collectd/api/OConfigItem;)I");
2309 status = (*jvm_env)->CallIntMethod (jvm_env,
2310 cbi->object, method, o_ocitem);
2312 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2313 cjni_thread_detach ();
2314 return (0);
2315 } /* }}} int cjni_config_plugin_block */
2317 static int cjni_config (oconfig_item_t *ci) /* {{{ */
2318 {
2319 int success;
2320 int errors;
2321 int status;
2322 int i;
2324 success = 0;
2325 errors = 0;
2327 for (i = 0; i < ci->children_num; i++)
2328 {
2329 oconfig_item_t *child = ci->children + i;
2331 if (strcasecmp ("JVMArg", child->key) == 0)
2332 {
2333 status = cjni_config_add_jvm_arg (child);
2334 if (status == 0)
2335 success++;
2336 else
2337 errors++;
2338 }
2339 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2340 {
2341 status = cjni_config_load_plugin (child);
2342 if (status == 0)
2343 success++;
2344 else
2345 errors++;
2346 }
2347 else if (strcasecmp ("Plugin", child->key) == 0)
2348 {
2349 status = cjni_config_plugin_block (child);
2350 if (status == 0)
2351 success++;
2352 else
2353 errors++;
2354 }
2355 else
2356 {
2357 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2358 errors++;
2359 }
2360 }
2362 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2363 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2365 if ((success == 0) && (errors > 0))
2366 {
2367 ERROR ("java plugin: All statements failed.");
2368 return (-1);
2369 }
2371 return (0);
2372 } /* }}} int cjni_config */
2374 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2375 * and `cjni_write'. In particular, delete the global reference to the Java
2376 * object. */
2377 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2378 {
2379 JNIEnv *jvm_env;
2380 cjni_callback_info_t *cbi;
2382 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2384 cbi = (cjni_callback_info_t *) arg;
2386 /* This condition can occurr when shutting down. */
2387 if (jvm == NULL)
2388 {
2389 sfree (cbi);
2390 return;
2391 }
2393 if (arg == NULL)
2394 return;
2396 jvm_env = cjni_thread_attach ();
2397 if (jvm_env == NULL)
2398 {
2399 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2400 return;
2401 }
2403 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2405 cbi->method = NULL;
2406 cbi->object = NULL;
2407 cbi->class = NULL;
2408 free (cbi);
2410 cjni_thread_detach ();
2411 } /* }}} void cjni_callback_info_destroy */
2413 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2414 static int cjni_read (user_data_t *ud) /* {{{ */
2415 {
2416 JNIEnv *jvm_env;
2417 cjni_callback_info_t *cbi;
2418 int status;
2419 int ret_status;
2421 if (jvm == NULL)
2422 {
2423 ERROR ("java plugin: cjni_read: jvm == NULL");
2424 return (-1);
2425 }
2427 if ((ud == NULL) || (ud->data == NULL))
2428 {
2429 ERROR ("java plugin: cjni_read: Invalid user data.");
2430 return (-1);
2431 }
2433 jvm_env = cjni_thread_attach ();
2434 if (jvm_env == NULL)
2435 return (-1);
2437 cbi = (cjni_callback_info_t *) ud->data;
2439 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2440 cbi->method);
2442 status = cjni_thread_detach ();
2443 if (status != 0)
2444 {
2445 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2446 return (-1);
2447 }
2449 return (ret_status);
2450 } /* }}} int cjni_read */
2452 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2453 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2454 user_data_t *ud)
2455 {
2456 JNIEnv *jvm_env;
2457 cjni_callback_info_t *cbi;
2458 jobject vl_java;
2459 int status;
2460 int ret_status;
2462 if (jvm == NULL)
2463 {
2464 ERROR ("java plugin: cjni_write: jvm == NULL");
2465 return (-1);
2466 }
2468 if ((ud == NULL) || (ud->data == NULL))
2469 {
2470 ERROR ("java plugin: cjni_write: Invalid user data.");
2471 return (-1);
2472 }
2474 jvm_env = cjni_thread_attach ();
2475 if (jvm_env == NULL)
2476 return (-1);
2478 cbi = (cjni_callback_info_t *) ud->data;
2480 vl_java = ctoj_value_list (jvm_env, ds, vl);
2481 if (vl_java == NULL)
2482 {
2483 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2484 return (-1);
2485 }
2487 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2488 cbi->object, cbi->method, vl_java);
2490 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2492 status = cjni_thread_detach ();
2493 if (status != 0)
2494 {
2495 ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2496 return (-1);
2497 }
2499 return (ret_status);
2500 } /* }}} int cjni_write */
2502 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2503 static int cjni_flush (int timeout, const char *identifier, /* {{{ */
2504 user_data_t *ud)
2505 {
2506 JNIEnv *jvm_env;
2507 cjni_callback_info_t *cbi;
2508 jobject o_identifier;
2509 int status;
2510 int ret_status;
2512 if (jvm == NULL)
2513 {
2514 ERROR ("java plugin: cjni_flush: jvm == NULL");
2515 return (-1);
2516 }
2518 if ((ud == NULL) || (ud->data == NULL))
2519 {
2520 ERROR ("java plugin: cjni_flush: Invalid user data.");
2521 return (-1);
2522 }
2524 jvm_env = cjni_thread_attach ();
2525 if (jvm_env == NULL)
2526 return (-1);
2528 cbi = (cjni_callback_info_t *) ud->data;
2530 o_identifier = NULL;
2531 if (identifier != NULL)
2532 {
2533 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2534 if (o_identifier == NULL)
2535 {
2536 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2537 return (-1);
2538 }
2539 }
2541 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2542 cbi->object, cbi->method, (jint) timeout, o_identifier);
2544 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2546 status = cjni_thread_detach ();
2547 if (status != 0)
2548 {
2549 ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2550 return (-1);
2551 }
2553 return (ret_status);
2554 } /* }}} int cjni_flush */
2556 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2557 static void cjni_log (int severity, const char *message, /* {{{ */
2558 user_data_t *ud)
2559 {
2560 JNIEnv *jvm_env;
2561 cjni_callback_info_t *cbi;
2562 jobject o_message;
2564 if (jvm == NULL)
2565 return;
2567 if ((ud == NULL) || (ud->data == NULL))
2568 return;
2570 jvm_env = cjni_thread_attach ();
2571 if (jvm_env == NULL)
2572 return;
2574 cbi = (cjni_callback_info_t *) ud->data;
2576 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2577 if (o_message == NULL)
2578 return;
2580 (*jvm_env)->CallVoidMethod (jvm_env,
2581 cbi->object, cbi->method, (jint) severity, o_message);
2583 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2585 cjni_thread_detach ();
2586 } /* }}} void cjni_log */
2588 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2589 * pointer. */
2590 static int cjni_notification (const notification_t *n, /* {{{ */
2591 user_data_t *ud)
2592 {
2593 JNIEnv *jvm_env;
2594 cjni_callback_info_t *cbi;
2595 jobject o_notification;
2596 int status;
2597 int ret_status;
2599 if (jvm == NULL)
2600 {
2601 ERROR ("java plugin: cjni_read: jvm == NULL");
2602 return (-1);
2603 }
2605 if ((ud == NULL) || (ud->data == NULL))
2606 {
2607 ERROR ("java plugin: cjni_read: Invalid user data.");
2608 return (-1);
2609 }
2611 jvm_env = cjni_thread_attach ();
2612 if (jvm_env == NULL)
2613 return (-1);
2615 cbi = (cjni_callback_info_t *) ud->data;
2617 o_notification = ctoj_notification (jvm_env, n);
2618 if (o_notification == NULL)
2619 {
2620 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2621 return (-1);
2622 }
2624 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2625 cbi->object, cbi->method, o_notification);
2627 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2629 status = cjni_thread_detach ();
2630 if (status != 0)
2631 {
2632 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2633 return (-1);
2634 }
2636 return (ret_status);
2637 } /* }}} int cjni_notification */
2639 /* Callbacks for matches implemented in Java */
2640 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2641 void **user_data)
2642 {
2643 JNIEnv *jvm_env;
2644 cjni_callback_info_t *cbi_ret;
2645 cjni_callback_info_t *cbi_factory;
2646 const char *name;
2647 jobject o_ci;
2648 jobject o_tmp;
2649 int type;
2650 size_t i;
2652 cbi_ret = NULL;
2653 o_ci = NULL;
2654 jvm_env = NULL;
2656 #define BAIL_OUT(status) \
2657 if (cbi_ret != NULL) { \
2658 free (cbi_ret->name); \
2659 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2660 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2661 } \
2662 free (cbi_ret); \
2663 if (jvm_env != NULL) { \
2664 if (o_ci != NULL) \
2665 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2666 cjni_thread_detach (); \
2667 } \
2668 return (status)
2670 if (jvm == NULL)
2671 {
2672 ERROR ("java plugin: cjni_read: jvm == NULL");
2673 BAIL_OUT (-1);
2674 }
2676 jvm_env = cjni_thread_attach ();
2677 if (jvm_env == NULL)
2678 {
2679 BAIL_OUT (-1);
2680 }
2682 /* Find out whether to create a match or a target. */
2683 if (strcasecmp ("Match", ci->key) == 0)
2684 type = CB_TYPE_MATCH;
2685 else if (strcasecmp ("Target", ci->key) == 0)
2686 type = CB_TYPE_TARGET;
2687 else
2688 {
2689 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2690 "to create a match or a target.");
2691 BAIL_OUT (-1);
2692 }
2694 /* This is the name of the match we should create. */
2695 name = ci->values[0].value.string;
2697 /* Lets see if we have a matching factory here.. */
2698 cbi_factory = NULL;
2699 for (i = 0; i < java_callbacks_num; i++)
2700 {
2701 if (java_callbacks[i].type != type)
2702 continue;
2704 if (strcmp (name, java_callbacks[i].name) != 0)
2705 continue;
2707 cbi_factory = java_callbacks + i;
2708 break;
2709 }
2711 /* Nope, no factory for that name.. */
2712 if (cbi_factory == NULL)
2713 {
2714 ERROR ("java plugin: cjni_match_target_create: "
2715 "No such match factory registered: %s",
2716 name);
2717 BAIL_OUT (-1);
2718 }
2720 /* We convert `ci' to its Java equivalent.. */
2721 o_ci = ctoj_oconfig_item (jvm_env, ci);
2722 if (o_ci == NULL)
2723 {
2724 ERROR ("java plugin: cjni_match_target_create: "
2725 "ctoj_oconfig_item failed.");
2726 BAIL_OUT (-1);
2727 }
2729 /* Allocate a new callback info structure. This is going to be our user_data
2730 * pointer. */
2731 cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
2732 if (cbi_ret == NULL)
2733 {
2734 ERROR ("java plugin: cjni_match_target_create: malloc failed.");
2735 BAIL_OUT (-1);
2736 }
2737 memset (cbi_ret, 0, sizeof (*cbi_ret));
2738 cbi_ret->object = NULL;
2739 cbi_ret->type = type;
2741 /* Lets fill the callback info structure.. First, the name: */
2742 cbi_ret->name = strdup (name);
2743 if (cbi_ret->name == NULL)
2744 {
2745 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2746 BAIL_OUT (-1);
2747 }
2749 /* Then call the factory method so it creates a new object for us. */
2750 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2751 cbi_factory->object, cbi_factory->method, o_ci);
2752 if (o_tmp == NULL)
2753 {
2754 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2755 BAIL_OUT (-1);
2756 }
2758 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2759 if (o_tmp == NULL)
2760 {
2761 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2762 BAIL_OUT (-1);
2763 }
2765 /* This is the class of the match. It is possibly different from the class of
2766 * the match-factory! */
2767 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2768 if (cbi_ret->class == NULL)
2769 {
2770 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2771 BAIL_OUT (-1);
2772 }
2774 /* Lookup the `int match (DataSet, ValueList)' method. */
2775 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2776 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2777 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2778 if (cbi_ret->method == NULL)
2779 {
2780 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2781 BAIL_OUT (-1);
2782 }
2784 /* Return the newly created match via the user_data pointer. */
2785 *user_data = (void *) cbi_ret;
2787 cjni_thread_detach ();
2789 DEBUG ("java plugin: cjni_match_target_create: "
2790 "Successfully created a `%s' %s.",
2791 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2793 /* Success! */
2794 return (0);
2795 #undef BAIL_OUT
2796 } /* }}} int cjni_match_target_create */
2798 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2799 {
2800 cjni_callback_info_destroy (*user_data);
2801 *user_data = NULL;
2803 return (0);
2804 } /* }}} int cjni_match_target_destroy */
2806 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2807 value_list_t *vl, notification_meta_t **meta, void **user_data)
2808 {
2809 JNIEnv *jvm_env;
2810 cjni_callback_info_t *cbi;
2811 jobject o_vl;
2812 jobject o_ds;
2813 int ret_status;
2814 int status;
2816 if (jvm == NULL)
2817 {
2818 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2819 return (-1);
2820 }
2822 jvm_env = cjni_thread_attach ();
2823 if (jvm_env == NULL)
2824 return (-1);
2826 cbi = (cjni_callback_info_t *) *user_data;
2828 o_vl = ctoj_value_list (jvm_env, ds, vl);
2829 if (o_vl == NULL)
2830 {
2831 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2832 cjni_thread_detach ();
2833 return (-1);
2834 }
2836 o_ds = ctoj_data_set (jvm_env, ds);
2837 if (o_ds == NULL)
2838 {
2839 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2840 cjni_thread_detach ();
2841 return (-1);
2842 }
2844 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2845 o_ds, o_vl);
2847 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2849 /* If we're executing a target, copy the `ValueList' back to our
2850 * `value_list_t'. */
2851 if (cbi->type == CB_TYPE_TARGET)
2852 {
2853 value_list_t new_vl;
2855 memset (&new_vl, 0, sizeof (new_vl));
2856 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2857 if (status != 0)
2858 {
2859 ERROR ("java plugin: cjni_match_target_invoke: "
2860 "jtoc_value_list failed.");
2861 }
2862 else /* if (status == 0) */
2863 {
2864 /* plugin_dispatch_values assures that this is dynamically allocated
2865 * memory. */
2866 sfree (vl->values);
2868 /* This will replace the vl->values pointer to a new, dynamically
2869 * allocated piece of memory. */
2870 memcpy (vl, &new_vl, sizeof (*vl));
2871 }
2872 } /* if (cbi->type == CB_TYPE_TARGET) */
2874 status = cjni_thread_detach ();
2875 if (status != 0)
2876 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2878 return (ret_status);
2879 } /* }}} int cjni_match_target_invoke */
2881 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2882 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2883 {
2884 int status;
2885 size_t i;
2887 for (i = 0; i < java_callbacks_num; i++)
2888 {
2889 if (java_callbacks[i].type != CB_TYPE_INIT)
2890 continue;
2892 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2894 status = (*jvm_env)->CallIntMethod (jvm_env,
2895 java_callbacks[i].object, java_callbacks[i].method);
2896 if (status != 0)
2897 {
2898 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2899 "Removing read function.",
2900 java_callbacks[i].name, status);
2901 plugin_unregister_read (java_callbacks[i].name);
2902 }
2903 }
2905 return (0);
2906 } /* }}} int cjni_init_plugins */
2908 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2909 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2910 {
2911 int status;
2912 size_t i;
2914 for (i = 0; i < java_callbacks_num; i++)
2915 {
2916 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2917 continue;
2919 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2921 status = (*jvm_env)->CallIntMethod (jvm_env,
2922 java_callbacks[i].object, java_callbacks[i].method);
2923 if (status != 0)
2924 {
2925 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2926 java_callbacks[i].name, status);
2927 }
2928 }
2930 return (0);
2931 } /* }}} int cjni_shutdown_plugins */
2934 static int cjni_shutdown (void) /* {{{ */
2935 {
2936 JNIEnv *jvm_env;
2937 JavaVMAttachArgs args;
2938 int status;
2939 size_t i;
2941 if (jvm == NULL)
2942 return (0);
2944 jvm_env = NULL;
2945 memset (&args, 0, sizeof (args));
2946 args.version = JNI_VERSION_1_2;
2948 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
2949 if (status != 0)
2950 {
2951 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2952 status);
2953 return (-1);
2954 }
2956 /* Execute all the shutdown functions registered by plugins. */
2957 cjni_shutdown_plugins (jvm_env);
2959 /* Release all the global references to callback functions */
2960 for (i = 0; i < java_callbacks_num; i++)
2961 {
2962 if (java_callbacks[i].object != NULL)
2963 {
2964 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
2965 java_callbacks[i].object = NULL;
2966 }
2967 sfree (java_callbacks[i].name);
2968 }
2969 java_callbacks_num = 0;
2970 sfree (java_callbacks);
2972 /* Release all the global references to directly loaded classes. */
2973 for (i = 0; i < java_classes_list_len; i++)
2974 {
2975 if (java_classes_list[i].object != NULL)
2976 {
2977 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
2978 java_classes_list[i].object = NULL;
2979 }
2980 sfree (java_classes_list[i].name);
2981 }
2982 java_classes_list_len = 0;
2983 sfree (java_classes_list);
2985 /* Destroy the JVM */
2986 DEBUG ("java plugin: Destroying the JVM.");
2987 (*jvm)->DestroyJavaVM (jvm);
2988 jvm = NULL;
2989 jvm_env = NULL;
2991 pthread_key_delete (jvm_env_key);
2993 /* Free the JVM argument list */
2994 for (i = 0; i < jvm_argc; i++)
2995 sfree (jvm_argv[i]);
2996 jvm_argc = 0;
2997 sfree (jvm_argv);
2999 return (0);
3000 } /* }}} int cjni_shutdown */
3002 /* Initialization: Create a JVM, load all configured classes and call their
3003 * `config' and `init' callback methods. */
3004 static int cjni_init (void) /* {{{ */
3005 {
3006 JNIEnv *jvm_env;
3008 if (jvm == NULL)
3009 {
3010 ERROR ("java plugin: cjni_init: jvm == NULL");
3011 return (-1);
3012 }
3014 jvm_env = cjni_thread_attach ();
3015 if (jvm_env == NULL)
3016 return (-1);
3018 cjni_init_plugins (jvm_env);
3020 cjni_thread_detach ();
3021 return (0);
3022 } /* }}} int cjni_init */
3024 void module_register (void)
3025 {
3026 plugin_register_complex_config ("java", cjni_config);
3027 plugin_register_init ("java", cjni_init);
3028 plugin_register_shutdown ("java", cjni_shutdown);
3029 } /* void module_register (void) */
3031 /* vim: set sw=2 sts=2 et fdm=marker : */