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 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 (int 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 int ctoj_int (JNIEnv *jvm_env, /* {{{ */
163 jint value,
164 jclass class_ptr, jobject object_ptr, const char *method_name)
165 {
166 jmethodID m_set;
168 /* Search for the `void setFoo (int i)' method. */
169 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
170 method_name, "(I)V");
171 if (m_set == NULL)
172 {
173 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
174 method_name);
175 return (-1);
176 }
178 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
180 return (0);
181 } /* }}} int ctoj_int */
183 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
184 jlong value,
185 jclass class_ptr, jobject object_ptr, const char *method_name)
186 {
187 jmethodID m_set;
189 /* Search for the `void setFoo (long l)' method. */
190 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
191 method_name, "(J)V");
192 if (m_set == NULL)
193 {
194 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
195 method_name);
196 return (-1);
197 }
199 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
201 return (0);
202 } /* }}} int ctoj_long */
204 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
205 jdouble value,
206 jclass class_ptr, jobject object_ptr, const char *method_name)
207 {
208 jmethodID m_set;
210 /* Search for the `void setFoo (double d)' method. */
211 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
212 method_name, "(D)V");
213 if (m_set == NULL)
214 {
215 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
216 method_name);
217 return (-1);
218 }
220 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
222 return (0);
223 } /* }}} int ctoj_double */
225 /* Convert a jlong to a java.lang.Number */
226 static jobject ctoj_jlong_to_number (JNIEnv *jvm_env, jlong value) /* {{{ */
227 {
228 jclass c_long;
229 jmethodID m_long_constructor;
231 /* Look up the java.lang.Long class */
232 c_long = (*jvm_env)->FindClass (jvm_env, "java/lang/Long");
233 if (c_long == NULL)
234 {
235 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
236 "java.lang.Long class failed.");
237 return (NULL);
238 }
240 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
241 c_long, "<init>", "(J)V");
242 if (m_long_constructor == NULL)
243 {
244 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
245 "`Long (long)' constructor failed.");
246 return (NULL);
247 }
249 return ((*jvm_env)->NewObject (jvm_env,
250 c_long, m_long_constructor, value));
251 } /* }}} jobject ctoj_jlong_to_number */
253 /* Convert a jdouble to a java.lang.Number */
254 static jobject ctoj_jdouble_to_number (JNIEnv *jvm_env, jdouble value) /* {{{ */
255 {
256 jclass c_double;
257 jmethodID m_double_constructor;
259 /* Look up the java.lang.Long class */
260 c_double = (*jvm_env)->FindClass (jvm_env, "java/lang/Double");
261 if (c_double == NULL)
262 {
263 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
264 "java.lang.Double class failed.");
265 return (NULL);
266 }
268 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
269 c_double, "<init>", "(D)V");
270 if (m_double_constructor == NULL)
271 {
272 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
273 "`Double (double)' constructor failed.");
274 return (NULL);
275 }
277 return ((*jvm_env)->NewObject (jvm_env,
278 c_double, m_double_constructor, value));
279 } /* }}} jobject ctoj_jdouble_to_number */
281 /* Convert a value_t to a java.lang.Number */
282 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
283 value_t value, int ds_type)
284 {
285 if (ds_type == DS_TYPE_COUNTER)
286 return (ctoj_jlong_to_number (jvm_env, (jlong) value.counter));
287 else if (ds_type == DS_TYPE_GAUGE)
288 return (ctoj_jdouble_to_number (jvm_env, (jdouble) value.gauge));
289 else
290 return (NULL);
291 } /* }}} jobject ctoj_value_to_number */
293 /* Convert a data_source_t to a org/collectd/api/DataSource */
294 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
295 const data_source_t *dsrc)
296 {
297 jclass c_datasource;
298 jmethodID m_datasource_constructor;
299 jobject o_datasource;
300 int status;
302 /* Look up the DataSource class */
303 c_datasource = (*jvm_env)->FindClass (jvm_env,
304 "org/collectd/api/DataSource");
305 if (c_datasource == NULL)
306 {
307 ERROR ("java plugin: ctoj_data_source: "
308 "FindClass (org/collectd/api/DataSource) failed.");
309 return (NULL);
310 }
312 /* Lookup the `ValueList ()' constructor. */
313 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
314 "<init>", "()V");
315 if (m_datasource_constructor == NULL)
316 {
317 ERROR ("java plugin: ctoj_data_source: Cannot find the "
318 "`DataSource ()' constructor.");
319 return (NULL);
320 }
322 /* Create a new instance. */
323 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
324 m_datasource_constructor);
325 if (o_datasource == NULL)
326 {
327 ERROR ("java plugin: ctoj_data_source: "
328 "Creating a new DataSource instance failed.");
329 return (NULL);
330 }
332 /* Set name via `void setName (String name)' */
333 status = ctoj_string (jvm_env, dsrc->name,
334 c_datasource, o_datasource, "setName");
335 if (status != 0)
336 {
337 ERROR ("java plugin: ctoj_data_source: "
338 "ctoj_string (setName) failed.");
339 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
340 return (NULL);
341 }
343 /* Set type via `void setType (int type)' */
344 status = ctoj_int (jvm_env, dsrc->type,
345 c_datasource, o_datasource, "setType");
346 if (status != 0)
347 {
348 ERROR ("java plugin: ctoj_data_source: "
349 "ctoj_int (setType) failed.");
350 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
351 return (NULL);
352 }
354 /* Set min via `void setMin (double min)' */
355 status = ctoj_double (jvm_env, dsrc->min,
356 c_datasource, o_datasource, "setMin");
357 if (status != 0)
358 {
359 ERROR ("java plugin: ctoj_data_source: "
360 "ctoj_double (setMin) failed.");
361 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
362 return (NULL);
363 }
365 /* Set max via `void setMax (double max)' */
366 status = ctoj_double (jvm_env, dsrc->max,
367 c_datasource, o_datasource, "setMax");
368 if (status != 0)
369 {
370 ERROR ("java plugin: ctoj_data_source: "
371 "ctoj_double (setMax) failed.");
372 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
373 return (NULL);
374 }
376 return (o_datasource);
377 } /* }}} jobject ctoj_data_source */
379 /* Convert a oconfig_value_t to a org/collectd/api/OConfigValue */
380 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
381 oconfig_value_t ocvalue)
382 {
383 jclass c_ocvalue;
384 jmethodID m_ocvalue_constructor;
385 jobject o_argument;
386 jobject o_ocvalue;
388 m_ocvalue_constructor = NULL;
389 o_argument = NULL;
391 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
392 "org/collectd/api/OConfigValue");
393 if (c_ocvalue == NULL)
394 {
395 ERROR ("java plugin: ctoj_oconfig_value: "
396 "FindClass (org/collectd/api/OConfigValue) failed.");
397 return (NULL);
398 }
400 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
401 {
402 jboolean tmp_boolean;
404 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
406 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
407 "<init>", "(Z)V");
408 if (m_ocvalue_constructor == NULL)
409 {
410 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
411 "`OConfigValue (boolean)' constructor.");
412 return (NULL);
413 }
415 return ((*jvm_env)->NewObject (jvm_env,
416 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
417 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
418 else if (ocvalue.type == OCONFIG_TYPE_STRING)
419 {
420 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
421 "<init>", "(Ljava/lang/String;)V");
422 if (m_ocvalue_constructor == NULL)
423 {
424 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
425 "`OConfigValue (String)' constructor.");
426 return (NULL);
427 }
429 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
430 if (o_argument == NULL)
431 {
432 ERROR ("java plugin: ctoj_oconfig_value: "
433 "Creating a String object failed.");
434 return (NULL);
435 }
436 }
437 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
438 {
439 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
440 "<init>", "(Ljava/lang/Number;)V");
441 if (m_ocvalue_constructor == NULL)
442 {
443 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
444 "`OConfigValue (Number)' constructor.");
445 return (NULL);
446 }
448 o_argument = ctoj_jdouble_to_number (jvm_env,
449 (jdouble) ocvalue.value.number);
450 if (o_argument == NULL)
451 {
452 ERROR ("java plugin: ctoj_oconfig_value: "
453 "Creating a Number object failed.");
454 return (NULL);
455 }
456 }
457 else
458 {
459 return (NULL);
460 }
462 assert (m_ocvalue_constructor != NULL);
463 assert (o_argument != NULL);
465 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
466 c_ocvalue, m_ocvalue_constructor, o_argument);
467 if (o_ocvalue == NULL)
468 {
469 ERROR ("java plugin: ctoj_oconfig_value: "
470 "Creating an OConfigValue object failed.");
471 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
472 return (NULL);
473 }
475 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
476 return (o_ocvalue);
477 } /* }}} jobject ctoj_oconfig_value */
479 /* Convert a oconfig_item_t to a org/collectd/api/OConfigItem */
480 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
481 const oconfig_item_t *ci)
482 {
483 jclass c_ocitem;
484 jmethodID m_ocitem_constructor;
485 jmethodID m_addvalue;
486 jmethodID m_addchild;
487 jobject o_key;
488 jobject o_ocitem;
489 int i;
491 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/OConfigItem");
492 if (c_ocitem == NULL)
493 {
494 ERROR ("java plugin: ctoj_oconfig_item: "
495 "FindClass (org/collectd/api/OConfigItem) failed.");
496 return (NULL);
497 }
499 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
500 * {{{ */
501 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
502 "<init>", "(Ljava/lang/String;)V");
503 if (m_ocitem_constructor == NULL)
504 {
505 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
506 "`OConfigItem (String)' constructor.");
507 return (NULL);
508 }
510 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
511 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
512 if (m_addvalue == NULL)
513 {
514 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
515 "`addValue (OConfigValue)' method.");
516 return (NULL);
517 }
519 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
520 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
521 if (m_addchild == NULL)
522 {
523 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
524 "`addChild (OConfigItem)' method.");
525 return (NULL);
526 }
527 /* }}} */
529 /* Create a String object with the key.
530 * Needed for calling the constructor. */
531 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
532 if (o_key == NULL)
533 {
534 ERROR ("java plugin: ctoj_oconfig_item: "
535 "Creating String object failed.");
536 return (NULL);
537 }
539 /* Create an OConfigItem object */
540 o_ocitem = (*jvm_env)->NewObject (jvm_env,
541 c_ocitem, m_ocitem_constructor, o_key);
542 if (o_ocitem == NULL)
543 {
544 ERROR ("java plugin: ctoj_oconfig_item: "
545 "Creating an OConfigItem object failed.");
546 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
547 return (NULL);
548 }
550 /* We don't need the String object any longer.. */
551 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
553 /* Call OConfigItem.addValue for each value */
554 for (i = 0; i < ci->values_num; i++) /* {{{ */
555 {
556 jobject o_value;
558 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
559 if (o_value == NULL)
560 {
561 ERROR ("java plugin: ctoj_oconfig_item: "
562 "Creating an OConfigValue object failed.");
563 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
564 return (NULL);
565 }
567 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
568 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
569 } /* }}} for (i = 0; i < ci->values_num; i++) */
571 /* Call OConfigItem.addChild for each child */
572 for (i = 0; i < ci->children_num; i++) /* {{{ */
573 {
574 jobject o_child;
576 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
577 if (o_child == NULL)
578 {
579 ERROR ("java plugin: ctoj_oconfig_item: "
580 "Creating an OConfigItem object failed.");
581 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
582 return (NULL);
583 }
585 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addchild, o_child);
586 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
587 } /* }}} for (i = 0; i < ci->children_num; i++) */
589 return (o_ocitem);
590 } /* }}} jobject ctoj_oconfig_item */
592 /* Convert a data_set_t to a org/collectd/api/DataSet */
593 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
594 {
595 jclass c_dataset;
596 jmethodID m_constructor;
597 jmethodID m_add;
598 jobject o_type;
599 jobject o_dataset;
600 int i;
602 /* Look up the org/collectd/api/DataSet class */
603 c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet");
604 if (c_dataset == NULL)
605 {
606 ERROR ("java plugin: ctoj_data_set: Looking up the "
607 "org/collectd/api/DataSet class failed.");
608 return (NULL);
609 }
611 /* Search for the `DataSet (String type)' constructor. */
612 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
613 c_dataset, "<init>", "(Ljava/lang/String;)V");
614 if (m_constructor == NULL)
615 {
616 ERROR ("java plugin: ctoj_data_set: Looking up the "
617 "`DataSet (String)' constructor failed.");
618 return (NULL);
619 }
621 /* Search for the `void addDataSource (DataSource)' method. */
622 m_add = (*jvm_env)->GetMethodID (jvm_env,
623 c_dataset, "addDataSource", "(Lorg/collectd/api/DataSource;)V");
624 if (m_add == NULL)
625 {
626 ERROR ("java plugin: ctoj_data_set: Looking up the "
627 "`addDataSource (DataSource)' method failed.");
628 return (NULL);
629 }
631 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
632 if (o_type == NULL)
633 {
634 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
635 return (NULL);
636 }
638 o_dataset = (*jvm_env)->NewObject (jvm_env,
639 c_dataset, m_constructor, o_type);
640 if (o_dataset == NULL)
641 {
642 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
643 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
644 return (NULL);
645 }
647 /* Decrease reference counter on the java.lang.String object. */
648 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
650 for (i = 0; i < ds->ds_num; i++)
651 {
652 jobject o_datasource;
654 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
655 if (o_datasource == NULL)
656 {
657 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
658 ds->type, ds->ds[i].name);
659 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
660 return (NULL);
661 }
663 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
665 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
666 } /* for (i = 0; i < ds->ds_num; i++) */
668 return (o_dataset);
669 } /* }}} jobject ctoj_data_set */
671 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
672 value_t value, int ds_type,
673 jclass class_ptr, jobject object_ptr)
674 {
675 jmethodID m_addvalue;
676 jobject o_number;
678 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
679 "addValue", "(Ljava/lang/Number;)V");
680 if (m_addvalue == NULL)
681 {
682 ERROR ("java plugin: ctoj_value_list_add_value: "
683 "Cannot find method `void addValue (Number)'.");
684 return (-1);
685 }
687 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
688 if (o_number == NULL)
689 {
690 ERROR ("java plugin: ctoj_value_list_add_value: "
691 "ctoj_value_to_number failed.");
692 return (-1);
693 }
695 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
697 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
699 return (0);
700 } /* }}} int ctoj_value_list_add_value */
702 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
703 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
704 {
705 jmethodID m_setdataset;
706 jobject o_dataset;
708 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
709 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
710 "setDataSet", "(Lorg/collectd/api/DataSet;)V");
711 if (m_setdataset == NULL)
712 {
713 ERROR ("java plugin: ctoj_value_list_add_data_set: "
714 "Cannot find the `void setDataSet (DataSet)' method.");
715 return (-1);
716 }
718 /* Create a DataSet object. */
719 o_dataset = ctoj_data_set (jvm_env, ds);
720 if (o_dataset == NULL)
721 {
722 ERROR ("java plugin: ctoj_value_list_add_data_set: "
723 "ctoj_data_set (%s) failed.", ds->type);
724 return (-1);
725 }
727 /* Actually call the method. */
728 (*jvm_env)->CallVoidMethod (jvm_env,
729 o_valuelist, m_setdataset, o_dataset);
731 /* Decrease reference counter on the List<DataSource> object. */
732 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
734 return (0);
735 } /* }}} int ctoj_value_list_add_data_set */
737 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
738 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
739 const data_set_t *ds, const value_list_t *vl)
740 {
741 jclass c_valuelist;
742 jmethodID m_valuelist_constructor;
743 jobject o_valuelist;
744 int status;
745 int i;
747 /* First, create a new ValueList instance..
748 * Look up the class.. */
749 c_valuelist = (*jvm_env)->FindClass (jvm_env,
750 "org/collectd/api/ValueList");
751 if (c_valuelist == NULL)
752 {
753 ERROR ("java plugin: ctoj_value_list: "
754 "FindClass (org/collectd/api/ValueList) failed.");
755 return (NULL);
756 }
758 /* Lookup the `ValueList ()' constructor. */
759 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
760 "<init>", "()V");
761 if (m_valuelist_constructor == NULL)
762 {
763 ERROR ("java plugin: ctoj_value_list: Cannot find the "
764 "`ValueList ()' constructor.");
765 return (NULL);
766 }
768 /* Create a new instance. */
769 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
770 m_valuelist_constructor);
771 if (o_valuelist == NULL)
772 {
773 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
774 "failed.");
775 return (NULL);
776 }
778 status = ctoj_value_list_add_data_set (jvm_env,
779 c_valuelist, o_valuelist, ds);
780 if (status != 0)
781 {
782 ERROR ("java plugin: ctoj_value_list: "
783 "ctoj_value_list_add_data_set failed.");
784 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
785 return (NULL);
786 }
788 /* Set the strings.. */
789 #define SET_STRING(str,method_name) do { \
790 status = ctoj_string (jvm_env, str, \
791 c_valuelist, o_valuelist, method_name); \
792 if (status != 0) { \
793 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
794 method_name); \
795 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
796 return (NULL); \
797 } } while (0)
799 SET_STRING (vl->host, "setHost");
800 SET_STRING (vl->plugin, "setPlugin");
801 SET_STRING (vl->plugin_instance, "setPluginInstance");
802 SET_STRING (vl->type, "setType");
803 SET_STRING (vl->type_instance, "setTypeInstance");
805 #undef SET_STRING
807 /* Set the `time' member. Java stores time in milliseconds. */
808 status = ctoj_long (jvm_env, ((jlong) vl->time) * ((jlong) 1000),
809 c_valuelist, o_valuelist, "setTime");
810 if (status != 0)
811 {
812 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
813 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
814 return (NULL);
815 }
817 /* Set the `interval' member.. */
818 status = ctoj_long (jvm_env, (jlong) vl->interval,
819 c_valuelist, o_valuelist, "setInterval");
820 if (status != 0)
821 {
822 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
823 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
824 return (NULL);
825 }
827 for (i = 0; i < vl->values_len; i++)
828 {
829 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
830 c_valuelist, o_valuelist);
831 if (status != 0)
832 {
833 ERROR ("java plugin: ctoj_value_list: "
834 "ctoj_value_list_add_value failed.");
835 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
836 return (NULL);
837 }
838 }
840 return (o_valuelist);
841 } /* }}} jobject ctoj_value_list */
843 /* Convert a notification_t to a org/collectd/api/Notification */
844 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
845 const notification_t *n)
846 {
847 jclass c_notification;
848 jmethodID m_constructor;
849 jobject o_notification;
850 int status;
852 /* First, create a new Notification instance..
853 * Look up the class.. */
854 c_notification = (*jvm_env)->FindClass (jvm_env,
855 "org/collectd/api/Notification");
856 if (c_notification == NULL)
857 {
858 ERROR ("java plugin: ctoj_notification: "
859 "FindClass (org/collectd/api/Notification) failed.");
860 return (NULL);
861 }
863 /* Lookup the `Notification ()' constructor. */
864 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
865 "<init>", "()V");
866 if (m_constructor == NULL)
867 {
868 ERROR ("java plugin: ctoj_notification: Cannot find the "
869 "`Notification ()' constructor.");
870 return (NULL);
871 }
873 /* Create a new instance. */
874 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
875 m_constructor);
876 if (o_notification == NULL)
877 {
878 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
879 "instance failed.");
880 return (NULL);
881 }
883 /* Set the strings.. */
884 #define SET_STRING(str,method_name) do { \
885 status = ctoj_string (jvm_env, str, \
886 c_notification, o_notification, method_name); \
887 if (status != 0) { \
888 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
889 method_name); \
890 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
891 return (NULL); \
892 } } while (0)
894 SET_STRING (n->host, "setHost");
895 SET_STRING (n->plugin, "setPlugin");
896 SET_STRING (n->plugin_instance, "setPluginInstance");
897 SET_STRING (n->type, "setType");
898 SET_STRING (n->type_instance, "setTypeInstance");
899 SET_STRING (n->message, "setMessage");
901 #undef SET_STRING
903 /* Set the `time' member. Java stores time in milliseconds. */
904 status = ctoj_long (jvm_env, ((jlong) n->time) * ((jlong) 1000),
905 c_notification, o_notification, "setTime");
906 if (status != 0)
907 {
908 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
909 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
910 return (NULL);
911 }
913 /* Set the `interval' member.. */
914 status = ctoj_int (jvm_env, (jint) n->severity,
915 c_notification, o_notification, "setSeverity");
916 if (status != 0)
917 {
918 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
919 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
920 return (NULL);
921 }
923 return (o_notification);
924 } /* }}} jobject ctoj_notification */
926 /*
927 * Java to C conversion functions
928 */
929 /* Call a `String <method> ()' method. */
930 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
931 char *buffer, size_t buffer_size, int empty_okay,
932 jclass class_ptr, jobject object_ptr, const char *method_name)
933 {
934 jmethodID method_id;
935 jobject string_obj;
936 const char *c_str;
938 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
939 method_name, "()Ljava/lang/String;");
940 if (method_id == NULL)
941 {
942 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
943 method_name);
944 return (-1);
945 }
947 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
948 if ((string_obj == NULL) && (empty_okay == 0))
949 {
950 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
951 method_name);
952 return (-1);
953 }
954 else if ((string_obj == NULL) && (empty_okay != 0))
955 {
956 memset (buffer, 0, buffer_size);
957 return (0);
958 }
960 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
961 if (c_str == NULL)
962 {
963 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
964 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
965 return (-1);
966 }
968 sstrncpy (buffer, c_str, buffer_size);
970 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
971 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
973 return (0);
974 } /* }}} int jtoc_string */
976 /* Call an `int <method> ()' method. */
977 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
978 jint *ret_value,
979 jclass class_ptr, jobject object_ptr, const char *method_name)
980 {
981 jmethodID method_id;
983 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
984 method_name, "()I");
985 if (method_id == NULL)
986 {
987 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
988 method_name);
989 return (-1);
990 }
992 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
994 return (0);
995 } /* }}} int jtoc_int */
997 /* Call a `long <method> ()' method. */
998 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
999 jlong *ret_value,
1000 jclass class_ptr, jobject object_ptr, const char *method_name)
1001 {
1002 jmethodID method_id;
1004 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1005 method_name, "()J");
1006 if (method_id == NULL)
1007 {
1008 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1009 method_name);
1010 return (-1);
1011 }
1013 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1015 return (0);
1016 } /* }}} int jtoc_long */
1018 /* Call a `double <method> ()' method. */
1019 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1020 jdouble *ret_value,
1021 jclass class_ptr, jobject object_ptr, const char *method_name)
1022 {
1023 jmethodID method_id;
1025 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1026 method_name, "()D");
1027 if (method_id == NULL)
1028 {
1029 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1030 method_name);
1031 return (-1);
1032 }
1034 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1036 return (0);
1037 } /* }}} int jtoc_double */
1039 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1040 value_t *ret_value, int ds_type, jobject object_ptr)
1041 {
1042 jclass class_ptr;
1043 int status;
1045 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1047 if (ds_type == DS_TYPE_COUNTER)
1048 {
1049 jlong tmp_long;
1051 status = jtoc_long (jvm_env, &tmp_long,
1052 class_ptr, object_ptr, "longValue");
1053 if (status != 0)
1054 {
1055 ERROR ("java plugin: jtoc_value: "
1056 "jtoc_long failed.");
1057 return (-1);
1058 }
1059 (*ret_value).counter = (counter_t) tmp_long;
1060 }
1061 else
1062 {
1063 jdouble tmp_double;
1065 status = jtoc_double (jvm_env, &tmp_double,
1066 class_ptr, object_ptr, "doubleValue");
1067 if (status != 0)
1068 {
1069 ERROR ("java plugin: jtoc_value: "
1070 "jtoc_double failed.");
1071 return (-1);
1072 }
1073 (*ret_value).gauge = (gauge_t) tmp_double;
1074 }
1076 return (0);
1077 } /* }}} int jtoc_value */
1079 /* Read a List<Number>, convert it to `value_t' and add it to the given
1080 * `value_list_t'. */
1081 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1082 const data_set_t *ds, value_list_t *vl,
1083 jclass class_ptr, jobject object_ptr)
1084 {
1085 jmethodID m_getvalues;
1086 jmethodID m_toarray;
1087 jobject o_list;
1088 jobjectArray o_number_array;
1090 value_t *values;
1091 int values_num;
1092 int i;
1094 values_num = ds->ds_num;
1096 values = NULL;
1097 o_number_array = NULL;
1098 o_list = NULL;
1100 #define BAIL_OUT(status) \
1101 free (values); \
1102 if (o_number_array != NULL) \
1103 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1104 if (o_list != NULL) \
1105 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1106 return (status);
1108 /* Call: List<Number> ValueList.getValues () */
1109 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1110 "getValues", "()Ljava/util/List;");
1111 if (m_getvalues == NULL)
1112 {
1113 ERROR ("java plugin: jtoc_values_array: "
1114 "Cannot find method `List getValues ()'.");
1115 BAIL_OUT (-1);
1116 }
1118 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1119 if (o_list == NULL)
1120 {
1121 ERROR ("java plugin: jtoc_values_array: "
1122 "CallObjectMethod (getValues) failed.");
1123 BAIL_OUT (-1);
1124 }
1126 /* Call: Number[] List.toArray () */
1127 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1128 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1129 "toArray", "()[Ljava/lang/Object;");
1130 if (m_toarray == NULL)
1131 {
1132 ERROR ("java plugin: jtoc_values_array: "
1133 "Cannot find method `Object[] toArray ()'.");
1134 BAIL_OUT (-1);
1135 }
1137 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1138 if (o_number_array == NULL)
1139 {
1140 ERROR ("java plugin: jtoc_values_array: "
1141 "CallObjectMethod (toArray) failed.");
1142 BAIL_OUT (-1);
1143 }
1145 values = (value_t *) calloc (values_num, sizeof (value_t));
1146 if (values == NULL)
1147 {
1148 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1149 BAIL_OUT (-1);
1150 }
1152 for (i = 0; i < values_num; i++)
1153 {
1154 jobject o_number;
1155 int status;
1157 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1158 o_number_array, (jsize) i);
1159 if (o_number == NULL)
1160 {
1161 ERROR ("java plugin: jtoc_values_array: "
1162 "GetObjectArrayElement (%i) failed.", i);
1163 BAIL_OUT (-1);
1164 }
1166 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1167 if (status != 0)
1168 {
1169 ERROR ("java plugin: jtoc_values_array: "
1170 "jtoc_value (%i) failed.", i);
1171 BAIL_OUT (-1);
1172 }
1173 } /* for (i = 0; i < values_num; i++) */
1175 vl->values = values;
1176 vl->values_len = values_num;
1178 #undef BAIL_OUT
1179 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1180 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1181 return (0);
1182 } /* }}} int jtoc_values_array */
1184 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1185 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1186 jobject object_ptr)
1187 {
1188 jclass class_ptr;
1189 int status;
1190 jlong tmp_long;
1191 const data_set_t *ds;
1193 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1194 if (class_ptr == NULL)
1195 {
1196 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1197 return (-1);
1198 }
1200 /* eo == empty okay */
1201 #define SET_STRING(buffer,method, eo) do { \
1202 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1203 class_ptr, object_ptr, method); \
1204 if (status != 0) { \
1205 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1206 method); \
1207 return (-1); \
1208 } } while (0)
1210 SET_STRING(vl->type, "getType", /* empty = */ 0);
1212 ds = plugin_get_ds (vl->type);
1213 if (ds == NULL)
1214 {
1215 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1216 "Please consult the types.db(5) manpage for mor information.",
1217 vl->type);
1218 return (-1);
1219 }
1221 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1222 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1223 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1224 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1226 #undef SET_STRING
1228 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1229 if (status != 0)
1230 {
1231 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1232 return (-1);
1233 }
1234 /* Java measures time in milliseconds. */
1235 vl->time = (time_t) (tmp_long / ((jlong) 1000));
1237 status = jtoc_long (jvm_env, &tmp_long,
1238 class_ptr, object_ptr, "getInterval");
1239 if (status != 0)
1240 {
1241 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1242 return (-1);
1243 }
1244 vl->interval = (int) tmp_long;
1246 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1247 if (status != 0)
1248 {
1249 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1250 return (-1);
1251 }
1253 return (0);
1254 } /* }}} int jtoc_value_list */
1256 /* Convert a org/collectd/api/Notification to a notification_t. */
1257 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1258 jobject object_ptr)
1259 {
1260 jclass class_ptr;
1261 int status;
1262 jlong tmp_long;
1263 jint tmp_int;
1265 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1266 if (class_ptr == NULL)
1267 {
1268 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1269 return (-1);
1270 }
1272 /* eo == empty okay */
1273 #define SET_STRING(buffer,method, eo) do { \
1274 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1275 class_ptr, object_ptr, method); \
1276 if (status != 0) { \
1277 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1278 method); \
1279 return (-1); \
1280 } } while (0)
1282 SET_STRING (n->host, "getHost", /* empty = */ 1);
1283 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1284 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1285 SET_STRING (n->type, "getType", /* empty = */ 1);
1286 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1287 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1289 #undef SET_STRING
1291 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1292 if (status != 0)
1293 {
1294 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1295 return (-1);
1296 }
1297 /* Java measures time in milliseconds. */
1298 n->time = (time_t) (tmp_long / ((jlong) 1000));
1300 status = jtoc_int (jvm_env, &tmp_int,
1301 class_ptr, object_ptr, "getSeverity");
1302 if (status != 0)
1303 {
1304 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1305 return (-1);
1306 }
1307 n->severity = (int) tmp_int;
1309 return (0);
1310 } /* }}} int jtoc_notification */
1311 /*
1312 * Functions accessible from Java
1313 */
1314 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1315 jobject this, jobject java_vl)
1316 {
1317 value_list_t vl = VALUE_LIST_INIT;
1318 int status;
1320 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1322 status = jtoc_value_list (jvm_env, &vl, java_vl);
1323 if (status != 0)
1324 {
1325 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1326 return (-1);
1327 }
1329 status = plugin_dispatch_values (&vl);
1331 sfree (vl.values);
1333 return (status);
1334 } /* }}} jint cjni_api_dispatch_values */
1336 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1337 jobject this, jobject o_notification)
1338 {
1339 notification_t n;
1340 int status;
1342 memset (&n, 0, sizeof (n));
1343 n.meta = NULL;
1345 status = jtoc_notification (jvm_env, &n, o_notification);
1346 if (status != 0)
1347 {
1348 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1349 return (-1);
1350 }
1352 status = plugin_dispatch_notification (&n);
1354 return (status);
1355 } /* }}} jint cjni_api_dispatch_notification */
1357 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1358 jobject this, jobject o_string_type)
1359 {
1360 const char *ds_name;
1361 const data_set_t *ds;
1362 jobject o_dataset;
1364 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1365 if (ds_name == NULL)
1366 {
1367 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1368 return (NULL);
1369 }
1371 ds = plugin_get_ds (ds_name);
1372 DEBUG ("java plugin: cjni_api_get_ds: "
1373 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1375 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1377 if (ds == NULL)
1378 return (NULL);
1380 o_dataset = ctoj_data_set (jvm_env, ds);
1381 return (o_dataset);
1382 } /* }}} jint cjni_api_get_ds */
1384 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1385 jobject this, jobject o_name, jobject o_config)
1386 {
1387 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1388 } /* }}} jint cjni_api_register_config */
1390 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1391 jobject this, jobject o_name, jobject o_config)
1392 {
1393 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1394 } /* }}} jint cjni_api_register_init */
1396 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1397 jobject this, jobject o_name, jobject o_read)
1398 {
1399 user_data_t ud;
1400 cjni_callback_info_t *cbi;
1402 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1403 if (cbi == NULL)
1404 return (-1);
1406 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1408 memset (&ud, 0, sizeof (ud));
1409 ud.data = (void *) cbi;
1410 ud.free_func = cjni_callback_info_destroy;
1412 plugin_register_complex_read (cbi->name, cjni_read,
1413 /* interval = */ NULL, &ud);
1415 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1417 return (0);
1418 } /* }}} jint cjni_api_register_read */
1420 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1421 jobject this, jobject o_name, jobject o_write)
1422 {
1423 user_data_t ud;
1424 cjni_callback_info_t *cbi;
1426 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1427 if (cbi == NULL)
1428 return (-1);
1430 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1432 memset (&ud, 0, sizeof (ud));
1433 ud.data = (void *) cbi;
1434 ud.free_func = cjni_callback_info_destroy;
1436 plugin_register_write (cbi->name, cjni_write, &ud);
1438 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1440 return (0);
1441 } /* }}} jint cjni_api_register_write */
1443 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1444 jobject this, jobject o_name, jobject o_flush)
1445 {
1446 user_data_t ud;
1447 cjni_callback_info_t *cbi;
1449 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1450 if (cbi == NULL)
1451 return (-1);
1453 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1455 memset (&ud, 0, sizeof (ud));
1456 ud.data = (void *) cbi;
1457 ud.free_func = cjni_callback_info_destroy;
1459 plugin_register_flush (cbi->name, cjni_flush, &ud);
1461 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1463 return (0);
1464 } /* }}} jint cjni_api_register_flush */
1466 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1467 jobject this, jobject o_name, jobject o_shutdown)
1468 {
1469 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1470 CB_TYPE_SHUTDOWN));
1471 } /* }}} jint cjni_api_register_shutdown */
1473 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1474 jobject this, jobject o_name, jobject o_log)
1475 {
1476 user_data_t ud;
1477 cjni_callback_info_t *cbi;
1479 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1480 if (cbi == NULL)
1481 return (-1);
1483 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1485 memset (&ud, 0, sizeof (ud));
1486 ud.data = (void *) cbi;
1487 ud.free_func = cjni_callback_info_destroy;
1489 plugin_register_log (cbi->name, cjni_log, &ud);
1491 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1493 return (0);
1494 } /* }}} jint cjni_api_register_log */
1496 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1497 jobject this, jobject o_name, jobject o_notification)
1498 {
1499 user_data_t ud;
1500 cjni_callback_info_t *cbi;
1502 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1503 CB_TYPE_NOTIFICATION);
1504 if (cbi == NULL)
1505 return (-1);
1507 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1509 memset (&ud, 0, sizeof (ud));
1510 ud.data = (void *) cbi;
1511 ud.free_func = cjni_callback_info_destroy;
1513 plugin_register_notification (cbi->name, cjni_notification, &ud);
1515 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1517 return (0);
1518 } /* }}} jint cjni_api_register_notification */
1520 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1521 jobject this, jobject o_name, jobject o_match, int type)
1522 {
1523 int status;
1524 const char *c_name;
1526 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1527 if (c_name == NULL)
1528 {
1529 ERROR ("java plugin: cjni_api_register_match_target: "
1530 "GetStringUTFChars failed.");
1531 return (-1);
1532 }
1534 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1535 if (status != 0)
1536 {
1537 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1538 return (-1);
1539 }
1541 if (type == CB_TYPE_MATCH)
1542 {
1543 match_proc_t m_proc;
1545 memset (&m_proc, 0, sizeof (m_proc));
1546 m_proc.create = cjni_match_target_create;
1547 m_proc.destroy = cjni_match_target_destroy;
1548 m_proc.match = (void *) cjni_match_target_invoke;
1550 status = fc_register_match (c_name, m_proc);
1551 }
1552 else if (type == CB_TYPE_TARGET)
1553 {
1554 target_proc_t t_proc;
1556 memset (&t_proc, 0, sizeof (t_proc));
1557 t_proc.create = cjni_match_target_create;
1558 t_proc.destroy = cjni_match_target_destroy;
1559 t_proc.invoke = cjni_match_target_invoke;
1561 status = fc_register_target (c_name, t_proc);
1562 }
1563 else
1564 {
1565 ERROR ("java plugin: cjni_api_register_match_target: "
1566 "Don't know whether to create a match or a target.");
1567 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1568 return (-1);
1569 }
1571 if (status != 0)
1572 {
1573 ERROR ("java plugin: cjni_api_register_match_target: "
1574 "%s failed.",
1575 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1576 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1577 return (-1);
1578 }
1580 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1582 return (0);
1583 } /* }}} jint cjni_api_register_match_target */
1585 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1586 jobject this, jobject o_name, jobject o_match)
1587 {
1588 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1589 CB_TYPE_MATCH));
1590 } /* }}} jint cjni_api_register_match */
1592 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1593 jobject this, jobject o_name, jobject o_target)
1594 {
1595 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1596 CB_TYPE_TARGET));
1597 } /* }}} jint cjni_api_register_target */
1599 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1600 jobject this, jint severity, jobject o_message)
1601 {
1602 const char *c_str;
1604 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1605 if (c_str == NULL)
1606 {
1607 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1608 return;
1609 }
1611 if (severity < LOG_ERR)
1612 severity = LOG_ERR;
1613 if (severity > LOG_DEBUG)
1614 severity = LOG_DEBUG;
1616 plugin_log (severity, "%s", c_str);
1618 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1619 } /* }}} void cjni_api_log */
1621 /* List of ``native'' functions, i. e. C-functions that can be called from
1622 * Java. */
1623 static JNINativeMethod jni_api_functions[] = /* {{{ */
1624 {
1625 { "dispatchValues",
1626 "(Lorg/collectd/api/ValueList;)I",
1627 cjni_api_dispatch_values },
1629 { "dispatchNotification",
1630 "(Lorg/collectd/api/Notification;)I",
1631 cjni_api_dispatch_notification },
1633 { "getDS",
1634 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1635 cjni_api_get_ds },
1637 { "registerConfig",
1638 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1639 cjni_api_register_config },
1641 { "registerInit",
1642 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1643 cjni_api_register_init },
1645 { "registerRead",
1646 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1647 cjni_api_register_read },
1649 { "registerWrite",
1650 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1651 cjni_api_register_write },
1653 { "registerFlush",
1654 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1655 cjni_api_register_flush },
1657 { "registerShutdown",
1658 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1659 cjni_api_register_shutdown },
1661 { "registerLog",
1662 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1663 cjni_api_register_log },
1665 { "registerNotification",
1666 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1667 cjni_api_register_notification },
1669 { "registerMatch",
1670 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1671 cjni_api_register_match },
1673 { "registerTarget",
1674 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1675 cjni_api_register_target },
1677 { "log",
1678 "(ILjava/lang/String;)V",
1679 cjni_api_log },
1680 };
1681 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1682 / sizeof (jni_api_functions[0]);
1683 /* }}} */
1685 /*
1686 * Functions
1687 */
1688 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1689 * all registration functions. */
1690 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1691 jobject o_name, jobject o_callback, int type)
1692 {
1693 const char *c_name;
1694 cjni_callback_info_t *cbi;
1695 const char *method_name;
1696 const char *method_signature;
1698 switch (type)
1699 {
1700 case CB_TYPE_CONFIG:
1701 method_name = "config";
1702 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1703 break;
1705 case CB_TYPE_INIT:
1706 method_name = "init";
1707 method_signature = "()I";
1708 break;
1710 case CB_TYPE_READ:
1711 method_name = "read";
1712 method_signature = "()I";
1713 break;
1715 case CB_TYPE_WRITE:
1716 method_name = "write";
1717 method_signature = "(Lorg/collectd/api/ValueList;)I";
1718 break;
1720 case CB_TYPE_FLUSH:
1721 method_name = "flush";
1722 method_signature = "(ILjava/lang/String;)I";
1723 break;
1725 case CB_TYPE_SHUTDOWN:
1726 method_name = "shutdown";
1727 method_signature = "()I";
1728 break;
1730 case CB_TYPE_LOG:
1731 method_name = "log";
1732 method_signature = "(ILjava/lang/String;)V";
1733 break;
1735 case CB_TYPE_NOTIFICATION:
1736 method_name = "notification";
1737 method_signature = "(Lorg/collectd/api/Notification;)I";
1738 break;
1740 case CB_TYPE_MATCH:
1741 method_name = "createMatch";
1742 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1743 "Lorg/collectd/api/CollectdMatchInterface;";
1744 break;
1746 case CB_TYPE_TARGET:
1747 method_name = "createTarget";
1748 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1749 "Lorg/collectd/api/CollectdTargetInterface;";
1750 break;
1752 default:
1753 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1754 type);
1755 return (NULL);
1756 }
1758 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1759 if (c_name == NULL)
1760 {
1761 ERROR ("java plugin: cjni_callback_info_create: "
1762 "GetStringUTFChars failed.");
1763 return (NULL);
1764 }
1766 cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1767 if (cbi == NULL)
1768 {
1769 ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1770 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1771 return (NULL);
1772 }
1773 memset (cbi, 0, sizeof (*cbi));
1774 cbi->type = type;
1776 cbi->name = strdup (c_name);
1777 if (cbi->name == NULL)
1778 {
1779 pthread_mutex_unlock (&java_callbacks_lock);
1780 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1781 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1782 return (NULL);
1783 }
1785 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1787 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1788 if (cbi->object == NULL)
1789 {
1790 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1791 free (cbi);
1792 return (NULL);
1793 }
1795 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1796 if (cbi->class == NULL)
1797 {
1798 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1799 free (cbi);
1800 return (NULL);
1801 }
1803 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1804 method_name, method_signature);
1805 if (cbi->method == NULL)
1806 {
1807 ERROR ("java plugin: cjni_callback_info_create: "
1808 "Cannot find the `%s' method with signature `%s'.",
1809 method_name, method_signature);
1810 free (cbi);
1811 return (NULL);
1812 }
1814 return (cbi);
1815 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1817 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1818 * to the global `java_callbacks' variable. This is used for `config', `init',
1819 * and `shutdown' callbacks. */
1820 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1821 jobject o_name, jobject o_callback, int type)
1822 {
1823 cjni_callback_info_t *cbi;
1824 cjni_callback_info_t *tmp;
1825 #if COLLECT_DEBUG
1826 const char *type_str;
1827 #endif
1829 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1830 if (cbi == NULL)
1831 return (-1);
1833 #if COLLECT_DEBUG
1834 switch (type)
1835 {
1836 case CB_TYPE_CONFIG:
1837 type_str = "config";
1838 break;
1840 case CB_TYPE_INIT:
1841 type_str = "init";
1842 break;
1844 case CB_TYPE_SHUTDOWN:
1845 type_str = "shutdown";
1846 break;
1848 case CB_TYPE_MATCH:
1849 type_str = "match";
1850 break;
1852 case CB_TYPE_TARGET:
1853 type_str = "target";
1854 break;
1856 default:
1857 type_str = "<unknown>";
1858 }
1859 DEBUG ("java plugin: Registering new %s callback: %s",
1860 type_str, cbi->name);
1861 #endif
1863 pthread_mutex_lock (&java_callbacks_lock);
1865 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1866 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1867 if (tmp == NULL)
1868 {
1869 pthread_mutex_unlock (&java_callbacks_lock);
1870 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1872 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1873 free (cbi);
1875 return (-1);
1876 }
1877 java_callbacks = tmp;
1878 java_callbacks[java_callbacks_num] = *cbi;
1879 java_callbacks_num++;
1881 pthread_mutex_unlock (&java_callbacks_lock);
1883 free (cbi);
1884 return (0);
1885 } /* }}} int cjni_callback_register */
1887 /* Callback for `pthread_key_create'. It frees the data contained in
1888 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1889 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1890 {
1891 cjni_jvm_env_t *cjni_env;
1893 if (args == NULL)
1894 return;
1896 cjni_env = (cjni_jvm_env_t *) args;
1898 if (cjni_env->reference_counter > 0)
1899 {
1900 ERROR ("java plugin: cjni_jvm_env_destroy: "
1901 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1902 }
1904 if (cjni_env->jvm_env != NULL)
1905 {
1906 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1907 (void *) cjni_env->jvm_env);
1908 }
1910 /* The pointer is allocated in `cjni_thread_attach' */
1911 free (cjni_env);
1912 } /* }}} void cjni_jvm_env_destroy */
1914 /* Register ``native'' functions with the JVM. Native functions are C-functions
1915 * that can be called by Java code. */
1916 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1917 {
1918 jclass api_class_ptr;
1919 int status;
1921 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1922 if (api_class_ptr == NULL)
1923 {
1924 ERROR ("cjni_init_native: Cannot find API class `org/collectd/api/Collectd'.");
1925 return (-1);
1926 }
1928 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1929 jni_api_functions, (jint) jni_api_functions_num);
1930 if (status != 0)
1931 {
1932 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1933 return (-1);
1934 }
1936 return (0);
1937 } /* }}} int cjni_init_native */
1939 /* Create the JVM. This is called when the first thread tries to access the JVM
1940 * via cjni_thread_attach. */
1941 static int cjni_create_jvm (void) /* {{{ */
1942 {
1943 JNIEnv *jvm_env;
1944 JavaVMInitArgs vm_args;
1945 JavaVMOption vm_options[jvm_argc];
1947 int status;
1948 size_t i;
1950 if (jvm != NULL)
1951 return (0);
1953 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1954 if (status != 0)
1955 {
1956 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1957 "with status %i.", status);
1958 return (-1);
1959 }
1961 jvm_env = NULL;
1963 memset (&vm_args, 0, sizeof (vm_args));
1964 vm_args.version = JNI_VERSION_1_2;
1965 vm_args.options = vm_options;
1966 vm_args.nOptions = (jint) jvm_argc;
1968 for (i = 0; i < jvm_argc; i++)
1969 {
1970 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
1971 i, jvm_argv[i]);
1972 vm_args.options[i].optionString = jvm_argv[i];
1973 }
1975 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
1976 if (status != 0)
1977 {
1978 ERROR ("java plugin: cjni_create_jvm: "
1979 "JNI_CreateJavaVM failed with status %i.",
1980 status);
1981 return (-1);
1982 }
1983 assert (jvm != NULL);
1984 assert (jvm_env != NULL);
1986 /* Call RegisterNatives */
1987 status = cjni_init_native (jvm_env);
1988 if (status != 0)
1989 {
1990 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
1991 return (-1);
1992 }
1994 DEBUG ("java plugin: The JVM has been created.");
1995 return (0);
1996 } /* }}} int cjni_create_jvm */
1998 /* Increase the reference counter to the JVM for this thread. If it was zero,
1999 * attach the JVM first. */
2000 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2001 {
2002 cjni_jvm_env_t *cjni_env;
2003 JNIEnv *jvm_env;
2005 /* If we're the first thread to access the JVM, we'll have to create it
2006 * first.. */
2007 if (jvm == NULL)
2008 {
2009 int status;
2011 status = cjni_create_jvm ();
2012 if (status != 0)
2013 {
2014 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2015 return (NULL);
2016 }
2017 }
2018 assert (jvm != NULL);
2020 cjni_env = pthread_getspecific (jvm_env_key);
2021 if (cjni_env == NULL)
2022 {
2023 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2024 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
2025 if (cjni_env == NULL)
2026 {
2027 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
2028 return (NULL);
2029 }
2030 memset (cjni_env, 0, sizeof (*cjni_env));
2031 cjni_env->reference_counter = 0;
2032 cjni_env->jvm_env = NULL;
2034 pthread_setspecific (jvm_env_key, cjni_env);
2035 }
2037 if (cjni_env->reference_counter > 0)
2038 {
2039 cjni_env->reference_counter++;
2040 jvm_env = cjni_env->jvm_env;
2041 }
2042 else
2043 {
2044 int status;
2045 JavaVMAttachArgs args;
2047 assert (cjni_env->jvm_env == NULL);
2049 memset (&args, 0, sizeof (args));
2050 args.version = JNI_VERSION_1_2;
2052 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2053 if (status != 0)
2054 {
2055 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2056 "with status %i.", status);
2057 return (NULL);
2058 }
2060 cjni_env->reference_counter = 1;
2061 cjni_env->jvm_env = jvm_env;
2062 }
2064 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2065 cjni_env->reference_counter);
2066 assert (jvm_env != NULL);
2067 return (jvm_env);
2068 } /* }}} JNIEnv *cjni_thread_attach */
2070 /* Decrease the reference counter of this thread. If it reaches zero, detach
2071 * from the JVM. */
2072 static int cjni_thread_detach (void) /* {{{ */
2073 {
2074 cjni_jvm_env_t *cjni_env;
2075 int status;
2077 cjni_env = pthread_getspecific (jvm_env_key);
2078 if (cjni_env == NULL)
2079 {
2080 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2081 return (-1);
2082 }
2084 assert (cjni_env->reference_counter > 0);
2085 assert (cjni_env->jvm_env != NULL);
2087 cjni_env->reference_counter--;
2088 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2089 cjni_env->reference_counter);
2091 if (cjni_env->reference_counter > 0)
2092 return (0);
2094 status = (*jvm)->DetachCurrentThread (jvm);
2095 if (status != 0)
2096 {
2097 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2098 "with status %i.", status);
2099 }
2101 cjni_env->reference_counter = 0;
2102 cjni_env->jvm_env = NULL;
2104 return (0);
2105 } /* }}} JNIEnv *cjni_thread_attach */
2107 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2108 {
2109 char **tmp;
2111 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2112 {
2113 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2114 return (-1);
2115 }
2117 if (jvm != NULL)
2118 {
2119 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2120 "`LoadPlugin' options! The JVM is already started and I have to "
2121 "ignore this argument: %s",
2122 ci->values[0].value.string);
2123 return (-1);
2124 }
2126 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2127 if (tmp == NULL)
2128 {
2129 ERROR ("java plugin: realloc failed.");
2130 return (-1);
2131 }
2132 jvm_argv = tmp;
2134 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2135 if (jvm_argv[jvm_argc] == NULL)
2136 {
2137 ERROR ("java plugin: strdup failed.");
2138 return (-1);
2139 }
2140 jvm_argc++;
2142 return (0);
2143 } /* }}} int cjni_config_add_jvm_arg */
2145 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2146 {
2147 JNIEnv *jvm_env;
2148 java_plugin_class_t *class;
2149 jmethodID constructor_id;
2150 jobject tmp_object;
2152 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2153 {
2154 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2155 return (-1);
2156 }
2158 jvm_env = cjni_thread_attach ();
2159 if (jvm_env == NULL)
2160 return (-1);
2162 class = (java_plugin_class_t *) realloc (java_classes_list,
2163 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2164 if (class == NULL)
2165 {
2166 ERROR ("java plugin: realloc failed.");
2167 cjni_thread_detach ();
2168 return (-1);
2169 }
2170 java_classes_list = class;
2171 class = java_classes_list + java_classes_list_len;
2173 memset (class, 0, sizeof (*class));
2174 class->name = strdup (ci->values[0].value.string);
2175 if (class->name == NULL)
2176 {
2177 ERROR ("java plugin: strdup failed.");
2178 cjni_thread_detach ();
2179 return (-1);
2180 }
2181 class->class = NULL;
2182 class->object = NULL;
2184 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2185 thorough the Java community, but (Sun's) `FindClass' and friends need
2186 slashes. */
2187 size_t i;
2188 for (i = 0; class->name[i] != 0; i++)
2189 if (class->name[i] == '.')
2190 class->name[i] = '/';
2191 }
2193 DEBUG ("java plugin: Loading class %s", class->name);
2195 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2196 if (class->class == NULL)
2197 {
2198 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2199 class->name);
2200 cjni_thread_detach ();
2201 free (class->name);
2202 return (-1);
2203 }
2205 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2206 "<init>", "()V");
2207 if (constructor_id == NULL)
2208 {
2209 ERROR ("java plugin: cjni_config_load_plugin: "
2210 "Could not find the constructor for `%s'.",
2211 class->name);
2212 cjni_thread_detach ();
2213 free (class->name);
2214 return (-1);
2215 }
2217 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2218 constructor_id);
2219 if (tmp_object != NULL)
2220 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2221 else
2222 class->object = NULL;
2223 if (class->object == NULL)
2224 {
2225 ERROR ("java plugin: cjni_config_load_plugin: "
2226 "Could create a new `%s' object.",
2227 class->name);
2228 cjni_thread_detach ();
2229 free (class->name);
2230 return (-1);
2231 }
2233 cjni_thread_detach ();
2235 java_classes_list_len++;
2237 return (0);
2238 } /* }}} int cjni_config_load_plugin */
2240 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2241 {
2242 JNIEnv *jvm_env;
2243 cjni_callback_info_t *cbi;
2244 jobject o_ocitem;
2245 const char *name;
2246 int status;
2247 size_t i;
2249 jclass class;
2250 jmethodID method;
2252 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2253 {
2254 WARNING ("java plugin: `Plugin' blocks "
2255 "need exactly one string argument.");
2256 return (-1);
2257 }
2259 name = ci->values[0].value.string;
2261 cbi = NULL;
2262 for (i = 0; i < java_callbacks_num; i++)
2263 {
2264 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2265 continue;
2267 if (strcmp (name, java_callbacks[i].name) != 0)
2268 continue;
2270 cbi = java_callbacks + i;
2271 break;
2272 }
2274 if (cbi == NULL)
2275 {
2276 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2277 "configuration callback has been registered. Please make sure, the "
2278 "`LoadPlugin' lines precede the `Plugin' blocks.",
2279 name);
2280 return (0);
2281 }
2283 DEBUG ("java plugin: Configuring %s", name);
2285 jvm_env = cjni_thread_attach ();
2286 if (jvm_env == NULL)
2287 return (-1);
2289 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2290 if (o_ocitem == NULL)
2291 {
2292 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2293 cjni_thread_detach ();
2294 return (-1);
2295 }
2297 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2298 method = (*jvm_env)->GetMethodID (jvm_env, class,
2299 "config", "(Lorg/collectd/api/OConfigItem;)I");
2301 status = (*jvm_env)->CallIntMethod (jvm_env,
2302 cbi->object, method, o_ocitem);
2304 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2305 cjni_thread_detach ();
2306 return (0);
2307 } /* }}} int cjni_config_plugin_block */
2309 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2310 {
2311 int success;
2312 int errors;
2313 int status;
2314 int i;
2316 success = 0;
2317 errors = 0;
2319 for (i = 0; i < ci->children_num; i++)
2320 {
2321 oconfig_item_t *child = ci->children + i;
2323 if (strcasecmp ("JVMArg", child->key) == 0)
2324 {
2325 status = cjni_config_add_jvm_arg (child);
2326 if (status == 0)
2327 success++;
2328 else
2329 errors++;
2330 }
2331 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2332 {
2333 status = cjni_config_load_plugin (child);
2334 if (status == 0)
2335 success++;
2336 else
2337 errors++;
2338 }
2339 else if (strcasecmp ("Plugin", child->key) == 0)
2340 {
2341 status = cjni_config_plugin_block (child);
2342 if (status == 0)
2343 success++;
2344 else
2345 errors++;
2346 }
2347 else
2348 {
2349 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2350 errors++;
2351 }
2352 }
2354 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2355 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2357 if ((success == 0) && (errors > 0))
2358 {
2359 ERROR ("java plugin: All statements failed.");
2360 return (-1);
2361 }
2363 return (0);
2364 } /* }}} int cjni_config_perform */
2366 /* Copy the children of `ci' to the global `config_block' variable. */
2367 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2368 {
2369 oconfig_item_t *ci_copy;
2370 oconfig_item_t *tmp;
2372 assert (ci != NULL);
2373 if (ci->children_num == 0)
2374 return (0); /* nothing to do */
2376 ci_copy = oconfig_clone (ci);
2377 if (ci_copy == NULL)
2378 {
2379 ERROR ("java plugin: oconfig_clone failed.");
2380 return (-1);
2381 }
2383 if (config_block == NULL)
2384 {
2385 config_block = ci_copy;
2386 return (0);
2387 }
2389 tmp = realloc (config_block->children,
2390 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2391 if (tmp == NULL)
2392 {
2393 ERROR ("java plugin: realloc failed.");
2394 oconfig_free (ci_copy);
2395 return (-1);
2396 }
2397 config_block->children = tmp;
2399 /* Copy the pointers */
2400 memcpy (config_block->children + config_block->children_num,
2401 ci_copy->children,
2402 ci_copy->children_num * sizeof (*ci_copy->children));
2403 config_block->children_num += ci_copy->children_num;
2405 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2406 memset (ci_copy->children, 0,
2407 ci_copy->children_num * sizeof (*ci_copy->children));
2408 ci_copy->children_num = 0;
2410 oconfig_free (ci_copy);
2412 return (0);
2413 } /* }}} int cjni_config_callback */
2415 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2416 * and `cjni_write'. In particular, delete the global reference to the Java
2417 * object. */
2418 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2419 {
2420 JNIEnv *jvm_env;
2421 cjni_callback_info_t *cbi;
2423 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2425 cbi = (cjni_callback_info_t *) arg;
2427 /* This condition can occurr when shutting down. */
2428 if (jvm == NULL)
2429 {
2430 sfree (cbi);
2431 return;
2432 }
2434 if (arg == NULL)
2435 return;
2437 jvm_env = cjni_thread_attach ();
2438 if (jvm_env == NULL)
2439 {
2440 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2441 return;
2442 }
2444 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2446 cbi->method = NULL;
2447 cbi->object = NULL;
2448 cbi->class = NULL;
2449 free (cbi);
2451 cjni_thread_detach ();
2452 } /* }}} void cjni_callback_info_destroy */
2454 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2455 static int cjni_read (user_data_t *ud) /* {{{ */
2456 {
2457 JNIEnv *jvm_env;
2458 cjni_callback_info_t *cbi;
2459 int status;
2460 int ret_status;
2462 if (jvm == NULL)
2463 {
2464 ERROR ("java plugin: cjni_read: jvm == NULL");
2465 return (-1);
2466 }
2468 if ((ud == NULL) || (ud->data == NULL))
2469 {
2470 ERROR ("java plugin: cjni_read: 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 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2481 cbi->method);
2483 status = cjni_thread_detach ();
2484 if (status != 0)
2485 {
2486 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2487 return (-1);
2488 }
2490 return (ret_status);
2491 } /* }}} int cjni_read */
2493 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2494 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2495 user_data_t *ud)
2496 {
2497 JNIEnv *jvm_env;
2498 cjni_callback_info_t *cbi;
2499 jobject vl_java;
2500 int status;
2501 int ret_status;
2503 if (jvm == NULL)
2504 {
2505 ERROR ("java plugin: cjni_write: jvm == NULL");
2506 return (-1);
2507 }
2509 if ((ud == NULL) || (ud->data == NULL))
2510 {
2511 ERROR ("java plugin: cjni_write: Invalid user data.");
2512 return (-1);
2513 }
2515 jvm_env = cjni_thread_attach ();
2516 if (jvm_env == NULL)
2517 return (-1);
2519 cbi = (cjni_callback_info_t *) ud->data;
2521 vl_java = ctoj_value_list (jvm_env, ds, vl);
2522 if (vl_java == NULL)
2523 {
2524 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2525 return (-1);
2526 }
2528 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2529 cbi->object, cbi->method, vl_java);
2531 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2533 status = cjni_thread_detach ();
2534 if (status != 0)
2535 {
2536 ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2537 return (-1);
2538 }
2540 return (ret_status);
2541 } /* }}} int cjni_write */
2543 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2544 static int cjni_flush (int timeout, const char *identifier, /* {{{ */
2545 user_data_t *ud)
2546 {
2547 JNIEnv *jvm_env;
2548 cjni_callback_info_t *cbi;
2549 jobject o_identifier;
2550 int status;
2551 int ret_status;
2553 if (jvm == NULL)
2554 {
2555 ERROR ("java plugin: cjni_flush: jvm == NULL");
2556 return (-1);
2557 }
2559 if ((ud == NULL) || (ud->data == NULL))
2560 {
2561 ERROR ("java plugin: cjni_flush: Invalid user data.");
2562 return (-1);
2563 }
2565 jvm_env = cjni_thread_attach ();
2566 if (jvm_env == NULL)
2567 return (-1);
2569 cbi = (cjni_callback_info_t *) ud->data;
2571 o_identifier = NULL;
2572 if (identifier != NULL)
2573 {
2574 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2575 if (o_identifier == NULL)
2576 {
2577 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2578 return (-1);
2579 }
2580 }
2582 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2583 cbi->object, cbi->method, (jint) timeout, o_identifier);
2585 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2587 status = cjni_thread_detach ();
2588 if (status != 0)
2589 {
2590 ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2591 return (-1);
2592 }
2594 return (ret_status);
2595 } /* }}} int cjni_flush */
2597 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2598 static void cjni_log (int severity, const char *message, /* {{{ */
2599 user_data_t *ud)
2600 {
2601 JNIEnv *jvm_env;
2602 cjni_callback_info_t *cbi;
2603 jobject o_message;
2605 if (jvm == NULL)
2606 return;
2608 if ((ud == NULL) || (ud->data == NULL))
2609 return;
2611 jvm_env = cjni_thread_attach ();
2612 if (jvm_env == NULL)
2613 return;
2615 cbi = (cjni_callback_info_t *) ud->data;
2617 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2618 if (o_message == NULL)
2619 return;
2621 (*jvm_env)->CallVoidMethod (jvm_env,
2622 cbi->object, cbi->method, (jint) severity, o_message);
2624 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2626 cjni_thread_detach ();
2627 } /* }}} void cjni_log */
2629 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2630 * pointer. */
2631 static int cjni_notification (const notification_t *n, /* {{{ */
2632 user_data_t *ud)
2633 {
2634 JNIEnv *jvm_env;
2635 cjni_callback_info_t *cbi;
2636 jobject o_notification;
2637 int status;
2638 int ret_status;
2640 if (jvm == NULL)
2641 {
2642 ERROR ("java plugin: cjni_read: jvm == NULL");
2643 return (-1);
2644 }
2646 if ((ud == NULL) || (ud->data == NULL))
2647 {
2648 ERROR ("java plugin: cjni_read: Invalid user data.");
2649 return (-1);
2650 }
2652 jvm_env = cjni_thread_attach ();
2653 if (jvm_env == NULL)
2654 return (-1);
2656 cbi = (cjni_callback_info_t *) ud->data;
2658 o_notification = ctoj_notification (jvm_env, n);
2659 if (o_notification == NULL)
2660 {
2661 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2662 return (-1);
2663 }
2665 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2666 cbi->object, cbi->method, o_notification);
2668 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2670 status = cjni_thread_detach ();
2671 if (status != 0)
2672 {
2673 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2674 return (-1);
2675 }
2677 return (ret_status);
2678 } /* }}} int cjni_notification */
2680 /* Callbacks for matches implemented in Java */
2681 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2682 void **user_data)
2683 {
2684 JNIEnv *jvm_env;
2685 cjni_callback_info_t *cbi_ret;
2686 cjni_callback_info_t *cbi_factory;
2687 const char *name;
2688 jobject o_ci;
2689 jobject o_tmp;
2690 int type;
2691 size_t i;
2693 cbi_ret = NULL;
2694 o_ci = NULL;
2695 jvm_env = NULL;
2697 #define BAIL_OUT(status) \
2698 if (cbi_ret != NULL) { \
2699 free (cbi_ret->name); \
2700 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2701 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2702 } \
2703 free (cbi_ret); \
2704 if (jvm_env != NULL) { \
2705 if (o_ci != NULL) \
2706 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2707 cjni_thread_detach (); \
2708 } \
2709 return (status)
2711 if (jvm == NULL)
2712 {
2713 ERROR ("java plugin: cjni_read: jvm == NULL");
2714 BAIL_OUT (-1);
2715 }
2717 jvm_env = cjni_thread_attach ();
2718 if (jvm_env == NULL)
2719 {
2720 BAIL_OUT (-1);
2721 }
2723 /* Find out whether to create a match or a target. */
2724 if (strcasecmp ("Match", ci->key) == 0)
2725 type = CB_TYPE_MATCH;
2726 else if (strcasecmp ("Target", ci->key) == 0)
2727 type = CB_TYPE_TARGET;
2728 else
2729 {
2730 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2731 "to create a match or a target.");
2732 BAIL_OUT (-1);
2733 }
2735 /* This is the name of the match we should create. */
2736 name = ci->values[0].value.string;
2738 /* Lets see if we have a matching factory here.. */
2739 cbi_factory = NULL;
2740 for (i = 0; i < java_callbacks_num; i++)
2741 {
2742 if (java_callbacks[i].type != type)
2743 continue;
2745 if (strcmp (name, java_callbacks[i].name) != 0)
2746 continue;
2748 cbi_factory = java_callbacks + i;
2749 break;
2750 }
2752 /* Nope, no factory for that name.. */
2753 if (cbi_factory == NULL)
2754 {
2755 ERROR ("java plugin: cjni_match_target_create: "
2756 "No such match factory registered: %s",
2757 name);
2758 BAIL_OUT (-1);
2759 }
2761 /* We convert `ci' to its Java equivalent.. */
2762 o_ci = ctoj_oconfig_item (jvm_env, ci);
2763 if (o_ci == NULL)
2764 {
2765 ERROR ("java plugin: cjni_match_target_create: "
2766 "ctoj_oconfig_item failed.");
2767 BAIL_OUT (-1);
2768 }
2770 /* Allocate a new callback info structure. This is going to be our user_data
2771 * pointer. */
2772 cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
2773 if (cbi_ret == NULL)
2774 {
2775 ERROR ("java plugin: cjni_match_target_create: malloc failed.");
2776 BAIL_OUT (-1);
2777 }
2778 memset (cbi_ret, 0, sizeof (*cbi_ret));
2779 cbi_ret->object = NULL;
2780 cbi_ret->type = type;
2782 /* Lets fill the callback info structure.. First, the name: */
2783 cbi_ret->name = strdup (name);
2784 if (cbi_ret->name == NULL)
2785 {
2786 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2787 BAIL_OUT (-1);
2788 }
2790 /* Then call the factory method so it creates a new object for us. */
2791 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2792 cbi_factory->object, cbi_factory->method, o_ci);
2793 if (o_tmp == NULL)
2794 {
2795 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2796 BAIL_OUT (-1);
2797 }
2799 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2800 if (o_tmp == NULL)
2801 {
2802 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2803 BAIL_OUT (-1);
2804 }
2806 /* This is the class of the match. It is possibly different from the class of
2807 * the match-factory! */
2808 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2809 if (cbi_ret->class == NULL)
2810 {
2811 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2812 BAIL_OUT (-1);
2813 }
2815 /* Lookup the `int match (DataSet, ValueList)' method. */
2816 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2817 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2818 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2819 if (cbi_ret->method == NULL)
2820 {
2821 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2822 BAIL_OUT (-1);
2823 }
2825 /* Return the newly created match via the user_data pointer. */
2826 *user_data = (void *) cbi_ret;
2828 cjni_thread_detach ();
2830 DEBUG ("java plugin: cjni_match_target_create: "
2831 "Successfully created a `%s' %s.",
2832 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2834 /* Success! */
2835 return (0);
2836 #undef BAIL_OUT
2837 } /* }}} int cjni_match_target_create */
2839 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2840 {
2841 cjni_callback_info_destroy (*user_data);
2842 *user_data = NULL;
2844 return (0);
2845 } /* }}} int cjni_match_target_destroy */
2847 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2848 value_list_t *vl, notification_meta_t **meta, void **user_data)
2849 {
2850 JNIEnv *jvm_env;
2851 cjni_callback_info_t *cbi;
2852 jobject o_vl;
2853 jobject o_ds;
2854 int ret_status;
2855 int status;
2857 if (jvm == NULL)
2858 {
2859 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2860 return (-1);
2861 }
2863 jvm_env = cjni_thread_attach ();
2864 if (jvm_env == NULL)
2865 return (-1);
2867 cbi = (cjni_callback_info_t *) *user_data;
2869 o_vl = ctoj_value_list (jvm_env, ds, vl);
2870 if (o_vl == NULL)
2871 {
2872 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2873 cjni_thread_detach ();
2874 return (-1);
2875 }
2877 o_ds = ctoj_data_set (jvm_env, ds);
2878 if (o_ds == NULL)
2879 {
2880 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2881 cjni_thread_detach ();
2882 return (-1);
2883 }
2885 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2886 o_ds, o_vl);
2888 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2890 /* If we're executing a target, copy the `ValueList' back to our
2891 * `value_list_t'. */
2892 if (cbi->type == CB_TYPE_TARGET)
2893 {
2894 value_list_t new_vl;
2896 memset (&new_vl, 0, sizeof (new_vl));
2897 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2898 if (status != 0)
2899 {
2900 ERROR ("java plugin: cjni_match_target_invoke: "
2901 "jtoc_value_list failed.");
2902 }
2903 else /* if (status == 0) */
2904 {
2905 /* plugin_dispatch_values assures that this is dynamically allocated
2906 * memory. */
2907 sfree (vl->values);
2909 /* This will replace the vl->values pointer to a new, dynamically
2910 * allocated piece of memory. */
2911 memcpy (vl, &new_vl, sizeof (*vl));
2912 }
2913 } /* if (cbi->type == CB_TYPE_TARGET) */
2915 status = cjni_thread_detach ();
2916 if (status != 0)
2917 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2919 return (ret_status);
2920 } /* }}} int cjni_match_target_invoke */
2922 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2923 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2924 {
2925 int status;
2926 size_t i;
2928 for (i = 0; i < java_callbacks_num; i++)
2929 {
2930 if (java_callbacks[i].type != CB_TYPE_INIT)
2931 continue;
2933 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2935 status = (*jvm_env)->CallIntMethod (jvm_env,
2936 java_callbacks[i].object, java_callbacks[i].method);
2937 if (status != 0)
2938 {
2939 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2940 "Removing read function.",
2941 java_callbacks[i].name, status);
2942 plugin_unregister_read (java_callbacks[i].name);
2943 }
2944 }
2946 return (0);
2947 } /* }}} int cjni_init_plugins */
2949 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2950 static int cjni_shutdown_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_SHUTDOWN)
2958 continue;
2960 DEBUG ("java plugin: Shutting down %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: Shutting down `%s' failed with status %i. ",
2967 java_callbacks[i].name, status);
2968 }
2969 }
2971 return (0);
2972 } /* }}} int cjni_shutdown_plugins */
2975 static int cjni_shutdown (void) /* {{{ */
2976 {
2977 JNIEnv *jvm_env;
2978 JavaVMAttachArgs args;
2979 int status;
2980 size_t i;
2982 if (jvm == NULL)
2983 return (0);
2985 jvm_env = NULL;
2986 memset (&args, 0, sizeof (args));
2987 args.version = JNI_VERSION_1_2;
2989 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
2990 if (status != 0)
2991 {
2992 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2993 status);
2994 return (-1);
2995 }
2997 /* Execute all the shutdown functions registered by plugins. */
2998 cjni_shutdown_plugins (jvm_env);
3000 /* Release all the global references to callback functions */
3001 for (i = 0; i < java_callbacks_num; i++)
3002 {
3003 if (java_callbacks[i].object != NULL)
3004 {
3005 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3006 java_callbacks[i].object = NULL;
3007 }
3008 sfree (java_callbacks[i].name);
3009 }
3010 java_callbacks_num = 0;
3011 sfree (java_callbacks);
3013 /* Release all the global references to directly loaded classes. */
3014 for (i = 0; i < java_classes_list_len; i++)
3015 {
3016 if (java_classes_list[i].object != NULL)
3017 {
3018 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3019 java_classes_list[i].object = NULL;
3020 }
3021 sfree (java_classes_list[i].name);
3022 }
3023 java_classes_list_len = 0;
3024 sfree (java_classes_list);
3026 /* Destroy the JVM */
3027 DEBUG ("java plugin: Destroying the JVM.");
3028 (*jvm)->DestroyJavaVM (jvm);
3029 jvm = NULL;
3030 jvm_env = NULL;
3032 pthread_key_delete (jvm_env_key);
3034 /* Free the JVM argument list */
3035 for (i = 0; i < jvm_argc; i++)
3036 sfree (jvm_argv[i]);
3037 jvm_argc = 0;
3038 sfree (jvm_argv);
3040 return (0);
3041 } /* }}} int cjni_shutdown */
3043 /* Initialization: Create a JVM, load all configured classes and call their
3044 * `config' and `init' callback methods. */
3045 static int cjni_init (void) /* {{{ */
3046 {
3047 JNIEnv *jvm_env;
3049 if ((config_block == NULL) && (jvm == NULL))
3050 {
3051 ERROR ("java plugin: cjni_init: No configuration block for "
3052 "the java plugin was found.");
3053 return (-1);
3054 }
3056 if (config_block != NULL)
3057 {
3058 int status;
3060 status = cjni_config_perform (config_block);
3061 oconfig_free (config_block);
3062 config_block = NULL;
3063 }
3065 if (jvm == NULL)
3066 {
3067 ERROR ("java plugin: cjni_init: jvm == NULL");
3068 return (-1);
3069 }
3071 jvm_env = cjni_thread_attach ();
3072 if (jvm_env == NULL)
3073 return (-1);
3075 cjni_init_plugins (jvm_env);
3077 cjni_thread_detach ();
3078 return (0);
3079 } /* }}} int cjni_init */
3081 void module_register (void)
3082 {
3083 plugin_register_complex_config ("java", cjni_config_callback);
3084 plugin_register_init ("java", cjni_init);
3085 plugin_register_shutdown ("java", cjni_shutdown);
3086 } /* void module_register (void) */
3088 /* vim: set sw=2 sts=2 et fdm=marker : */