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 else
288 return (NULL);
289 } /* }}} jobject ctoj_value_to_number */
291 /* Convert a data_source_t to a org/collectd/api/DataSource */
292 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
293 const data_source_t *dsrc)
294 {
295 jclass c_datasource;
296 jmethodID m_datasource_constructor;
297 jobject o_datasource;
298 int status;
300 /* Look up the DataSource class */
301 c_datasource = (*jvm_env)->FindClass (jvm_env,
302 "org/collectd/api/DataSource");
303 if (c_datasource == NULL)
304 {
305 ERROR ("java plugin: ctoj_data_source: "
306 "FindClass (org/collectd/api/DataSource) failed.");
307 return (NULL);
308 }
310 /* Lookup the `ValueList ()' constructor. */
311 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
312 "<init>", "()V");
313 if (m_datasource_constructor == NULL)
314 {
315 ERROR ("java plugin: ctoj_data_source: Cannot find the "
316 "`DataSource ()' constructor.");
317 return (NULL);
318 }
320 /* Create a new instance. */
321 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
322 m_datasource_constructor);
323 if (o_datasource == NULL)
324 {
325 ERROR ("java plugin: ctoj_data_source: "
326 "Creating a new DataSource instance failed.");
327 return (NULL);
328 }
330 /* Set name via `void setName (String name)' */
331 status = ctoj_string (jvm_env, dsrc->name,
332 c_datasource, o_datasource, "setName");
333 if (status != 0)
334 {
335 ERROR ("java plugin: ctoj_data_source: "
336 "ctoj_string (setName) failed.");
337 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
338 return (NULL);
339 }
341 /* Set type via `void setType (int type)' */
342 status = ctoj_int (jvm_env, dsrc->type,
343 c_datasource, o_datasource, "setType");
344 if (status != 0)
345 {
346 ERROR ("java plugin: ctoj_data_source: "
347 "ctoj_int (setType) failed.");
348 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
349 return (NULL);
350 }
352 /* Set min via `void setMin (double min)' */
353 status = ctoj_double (jvm_env, dsrc->min,
354 c_datasource, o_datasource, "setMin");
355 if (status != 0)
356 {
357 ERROR ("java plugin: ctoj_data_source: "
358 "ctoj_double (setMin) failed.");
359 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
360 return (NULL);
361 }
363 /* Set max via `void setMax (double max)' */
364 status = ctoj_double (jvm_env, dsrc->max,
365 c_datasource, o_datasource, "setMax");
366 if (status != 0)
367 {
368 ERROR ("java plugin: ctoj_data_source: "
369 "ctoj_double (setMax) failed.");
370 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
371 return (NULL);
372 }
374 return (o_datasource);
375 } /* }}} jobject ctoj_data_source */
377 /* Convert a oconfig_value_t to a org/collectd/api/OConfigValue */
378 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
379 oconfig_value_t ocvalue)
380 {
381 jclass c_ocvalue;
382 jmethodID m_ocvalue_constructor;
383 jobject o_argument;
384 jobject o_ocvalue;
386 m_ocvalue_constructor = NULL;
387 o_argument = NULL;
389 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
390 "org/collectd/api/OConfigValue");
391 if (c_ocvalue == NULL)
392 {
393 ERROR ("java plugin: ctoj_oconfig_value: "
394 "FindClass (org/collectd/api/OConfigValue) failed.");
395 return (NULL);
396 }
398 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
399 {
400 jboolean tmp_boolean;
402 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
404 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
405 "<init>", "(Z)V");
406 if (m_ocvalue_constructor == NULL)
407 {
408 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
409 "`OConfigValue (boolean)' constructor.");
410 return (NULL);
411 }
413 return ((*jvm_env)->NewObject (jvm_env,
414 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
415 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
416 else if (ocvalue.type == OCONFIG_TYPE_STRING)
417 {
418 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
419 "<init>", "(Ljava/lang/String;)V");
420 if (m_ocvalue_constructor == NULL)
421 {
422 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
423 "`OConfigValue (String)' constructor.");
424 return (NULL);
425 }
427 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
428 if (o_argument == NULL)
429 {
430 ERROR ("java plugin: ctoj_oconfig_value: "
431 "Creating a String object failed.");
432 return (NULL);
433 }
434 }
435 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
436 {
437 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
438 "<init>", "(Ljava/lang/Number;)V");
439 if (m_ocvalue_constructor == NULL)
440 {
441 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
442 "`OConfigValue (Number)' constructor.");
443 return (NULL);
444 }
446 o_argument = ctoj_jdouble_to_number (jvm_env,
447 (jdouble) ocvalue.value.number);
448 if (o_argument == NULL)
449 {
450 ERROR ("java plugin: ctoj_oconfig_value: "
451 "Creating a Number object failed.");
452 return (NULL);
453 }
454 }
455 else
456 {
457 return (NULL);
458 }
460 assert (m_ocvalue_constructor != NULL);
461 assert (o_argument != NULL);
463 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
464 c_ocvalue, m_ocvalue_constructor, o_argument);
465 if (o_ocvalue == NULL)
466 {
467 ERROR ("java plugin: ctoj_oconfig_value: "
468 "Creating an OConfigValue object failed.");
469 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
470 return (NULL);
471 }
473 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
474 return (o_ocvalue);
475 } /* }}} jobject ctoj_oconfig_value */
477 /* Convert a oconfig_item_t to a org/collectd/api/OConfigItem */
478 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
479 const oconfig_item_t *ci)
480 {
481 jclass c_ocitem;
482 jmethodID m_ocitem_constructor;
483 jmethodID m_addvalue;
484 jmethodID m_addchild;
485 jobject o_key;
486 jobject o_ocitem;
487 int i;
489 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/OConfigItem");
490 if (c_ocitem == NULL)
491 {
492 ERROR ("java plugin: ctoj_oconfig_item: "
493 "FindClass (org/collectd/api/OConfigItem) failed.");
494 return (NULL);
495 }
497 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
498 * {{{ */
499 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
500 "<init>", "(Ljava/lang/String;)V");
501 if (m_ocitem_constructor == NULL)
502 {
503 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
504 "`OConfigItem (String)' constructor.");
505 return (NULL);
506 }
508 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
509 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
510 if (m_addvalue == NULL)
511 {
512 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
513 "`addValue (OConfigValue)' method.");
514 return (NULL);
515 }
517 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
518 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
519 if (m_addchild == NULL)
520 {
521 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
522 "`addChild (OConfigItem)' method.");
523 return (NULL);
524 }
525 /* }}} */
527 /* Create a String object with the key.
528 * Needed for calling the constructor. */
529 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
530 if (o_key == NULL)
531 {
532 ERROR ("java plugin: ctoj_oconfig_item: "
533 "Creating String object failed.");
534 return (NULL);
535 }
537 /* Create an OConfigItem object */
538 o_ocitem = (*jvm_env)->NewObject (jvm_env,
539 c_ocitem, m_ocitem_constructor, o_key);
540 if (o_ocitem == NULL)
541 {
542 ERROR ("java plugin: ctoj_oconfig_item: "
543 "Creating an OConfigItem object failed.");
544 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
545 return (NULL);
546 }
548 /* We don't need the String object any longer.. */
549 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
551 /* Call OConfigItem.addValue for each value */
552 for (i = 0; i < ci->values_num; i++) /* {{{ */
553 {
554 jobject o_value;
556 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
557 if (o_value == NULL)
558 {
559 ERROR ("java plugin: ctoj_oconfig_item: "
560 "Creating an OConfigValue object failed.");
561 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
562 return (NULL);
563 }
565 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
566 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
567 } /* }}} for (i = 0; i < ci->values_num; i++) */
569 /* Call OConfigItem.addChild for each child */
570 for (i = 0; i < ci->children_num; i++) /* {{{ */
571 {
572 jobject o_child;
574 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
575 if (o_child == NULL)
576 {
577 ERROR ("java plugin: ctoj_oconfig_item: "
578 "Creating an OConfigItem object failed.");
579 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
580 return (NULL);
581 }
583 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addchild, o_child);
584 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
585 } /* }}} for (i = 0; i < ci->children_num; i++) */
587 return (o_ocitem);
588 } /* }}} jobject ctoj_oconfig_item */
590 /* Convert a data_set_t to a org/collectd/api/DataSet */
591 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
592 {
593 jclass c_dataset;
594 jmethodID m_constructor;
595 jmethodID m_add;
596 jobject o_type;
597 jobject o_dataset;
598 int i;
600 /* Look up the org/collectd/api/DataSet class */
601 c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet");
602 if (c_dataset == NULL)
603 {
604 ERROR ("java plugin: ctoj_data_set: Looking up the "
605 "org/collectd/api/DataSet class failed.");
606 return (NULL);
607 }
609 /* Search for the `DataSet (String type)' constructor. */
610 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
611 c_dataset, "<init>", "(Ljava/lang/String;)V");
612 if (m_constructor == NULL)
613 {
614 ERROR ("java plugin: ctoj_data_set: Looking up the "
615 "`DataSet (String)' constructor failed.");
616 return (NULL);
617 }
619 /* Search for the `void addDataSource (DataSource)' method. */
620 m_add = (*jvm_env)->GetMethodID (jvm_env,
621 c_dataset, "addDataSource", "(Lorg/collectd/api/DataSource;)V");
622 if (m_add == NULL)
623 {
624 ERROR ("java plugin: ctoj_data_set: Looking up the "
625 "`addDataSource (DataSource)' method failed.");
626 return (NULL);
627 }
629 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
630 if (o_type == NULL)
631 {
632 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
633 return (NULL);
634 }
636 o_dataset = (*jvm_env)->NewObject (jvm_env,
637 c_dataset, m_constructor, o_type);
638 if (o_dataset == NULL)
639 {
640 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
641 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
642 return (NULL);
643 }
645 /* Decrease reference counter on the java.lang.String object. */
646 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
648 for (i = 0; i < ds->ds_num; i++)
649 {
650 jobject o_datasource;
652 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
653 if (o_datasource == NULL)
654 {
655 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
656 ds->type, ds->ds[i].name);
657 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
658 return (NULL);
659 }
661 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
663 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
664 } /* for (i = 0; i < ds->ds_num; i++) */
666 return (o_dataset);
667 } /* }}} jobject ctoj_data_set */
669 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
670 value_t value, int ds_type,
671 jclass class_ptr, jobject object_ptr)
672 {
673 jmethodID m_addvalue;
674 jobject o_number;
676 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
677 "addValue", "(Ljava/lang/Number;)V");
678 if (m_addvalue == NULL)
679 {
680 ERROR ("java plugin: ctoj_value_list_add_value: "
681 "Cannot find method `void addValue (Number)'.");
682 return (-1);
683 }
685 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
686 if (o_number == NULL)
687 {
688 ERROR ("java plugin: ctoj_value_list_add_value: "
689 "ctoj_value_to_number failed.");
690 return (-1);
691 }
693 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
695 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
697 return (0);
698 } /* }}} int ctoj_value_list_add_value */
700 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
701 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
702 {
703 jmethodID m_setdataset;
704 jobject o_dataset;
706 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
707 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
708 "setDataSet", "(Lorg/collectd/api/DataSet;)V");
709 if (m_setdataset == NULL)
710 {
711 ERROR ("java plugin: ctoj_value_list_add_data_set: "
712 "Cannot find the `void setDataSet (DataSet)' method.");
713 return (-1);
714 }
716 /* Create a DataSet object. */
717 o_dataset = ctoj_data_set (jvm_env, ds);
718 if (o_dataset == NULL)
719 {
720 ERROR ("java plugin: ctoj_value_list_add_data_set: "
721 "ctoj_data_set (%s) failed.", ds->type);
722 return (-1);
723 }
725 /* Actually call the method. */
726 (*jvm_env)->CallVoidMethod (jvm_env,
727 o_valuelist, m_setdataset, o_dataset);
729 /* Decrease reference counter on the List<DataSource> object. */
730 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
732 return (0);
733 } /* }}} int ctoj_value_list_add_data_set */
735 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
736 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
737 const data_set_t *ds, const value_list_t *vl)
738 {
739 jclass c_valuelist;
740 jmethodID m_valuelist_constructor;
741 jobject o_valuelist;
742 int status;
743 int i;
745 /* First, create a new ValueList instance..
746 * Look up the class.. */
747 c_valuelist = (*jvm_env)->FindClass (jvm_env,
748 "org/collectd/api/ValueList");
749 if (c_valuelist == NULL)
750 {
751 ERROR ("java plugin: ctoj_value_list: "
752 "FindClass (org/collectd/api/ValueList) failed.");
753 return (NULL);
754 }
756 /* Lookup the `ValueList ()' constructor. */
757 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
758 "<init>", "()V");
759 if (m_valuelist_constructor == NULL)
760 {
761 ERROR ("java plugin: ctoj_value_list: Cannot find the "
762 "`ValueList ()' constructor.");
763 return (NULL);
764 }
766 /* Create a new instance. */
767 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
768 m_valuelist_constructor);
769 if (o_valuelist == NULL)
770 {
771 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
772 "failed.");
773 return (NULL);
774 }
776 status = ctoj_value_list_add_data_set (jvm_env,
777 c_valuelist, o_valuelist, ds);
778 if (status != 0)
779 {
780 ERROR ("java plugin: ctoj_value_list: "
781 "ctoj_value_list_add_data_set failed.");
782 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
783 return (NULL);
784 }
786 /* Set the strings.. */
787 #define SET_STRING(str,method_name) do { \
788 status = ctoj_string (jvm_env, str, \
789 c_valuelist, o_valuelist, method_name); \
790 if (status != 0) { \
791 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
792 method_name); \
793 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
794 return (NULL); \
795 } } while (0)
797 SET_STRING (vl->host, "setHost");
798 SET_STRING (vl->plugin, "setPlugin");
799 SET_STRING (vl->plugin_instance, "setPluginInstance");
800 SET_STRING (vl->type, "setType");
801 SET_STRING (vl->type_instance, "setTypeInstance");
803 #undef SET_STRING
805 /* Set the `time' member. Java stores time in milliseconds. */
806 status = ctoj_long (jvm_env, ((jlong) vl->time) * ((jlong) 1000),
807 c_valuelist, o_valuelist, "setTime");
808 if (status != 0)
809 {
810 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
811 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
812 return (NULL);
813 }
815 /* Set the `interval' member.. */
816 status = ctoj_long (jvm_env, (jlong) vl->interval,
817 c_valuelist, o_valuelist, "setInterval");
818 if (status != 0)
819 {
820 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
821 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
822 return (NULL);
823 }
825 for (i = 0; i < vl->values_len; i++)
826 {
827 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
828 c_valuelist, o_valuelist);
829 if (status != 0)
830 {
831 ERROR ("java plugin: ctoj_value_list: "
832 "ctoj_value_list_add_value failed.");
833 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
834 return (NULL);
835 }
836 }
838 return (o_valuelist);
839 } /* }}} jobject ctoj_value_list */
841 /* Convert a notification_t to a org/collectd/api/Notification */
842 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
843 const notification_t *n)
844 {
845 jclass c_notification;
846 jmethodID m_constructor;
847 jobject o_notification;
848 int status;
850 /* First, create a new Notification instance..
851 * Look up the class.. */
852 c_notification = (*jvm_env)->FindClass (jvm_env,
853 "org/collectd/api/Notification");
854 if (c_notification == NULL)
855 {
856 ERROR ("java plugin: ctoj_notification: "
857 "FindClass (org/collectd/api/Notification) failed.");
858 return (NULL);
859 }
861 /* Lookup the `Notification ()' constructor. */
862 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
863 "<init>", "()V");
864 if (m_constructor == NULL)
865 {
866 ERROR ("java plugin: ctoj_notification: Cannot find the "
867 "`Notification ()' constructor.");
868 return (NULL);
869 }
871 /* Create a new instance. */
872 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
873 m_constructor);
874 if (o_notification == NULL)
875 {
876 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
877 "instance failed.");
878 return (NULL);
879 }
881 /* Set the strings.. */
882 #define SET_STRING(str,method_name) do { \
883 status = ctoj_string (jvm_env, str, \
884 c_notification, o_notification, method_name); \
885 if (status != 0) { \
886 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
887 method_name); \
888 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
889 return (NULL); \
890 } } while (0)
892 SET_STRING (n->host, "setHost");
893 SET_STRING (n->plugin, "setPlugin");
894 SET_STRING (n->plugin_instance, "setPluginInstance");
895 SET_STRING (n->type, "setType");
896 SET_STRING (n->type_instance, "setTypeInstance");
897 SET_STRING (n->message, "setMessage");
899 #undef SET_STRING
901 /* Set the `time' member. Java stores time in milliseconds. */
902 status = ctoj_long (jvm_env, ((jlong) n->time) * ((jlong) 1000),
903 c_notification, o_notification, "setTime");
904 if (status != 0)
905 {
906 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
907 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
908 return (NULL);
909 }
911 /* Set the `interval' member.. */
912 status = ctoj_int (jvm_env, (jint) n->severity,
913 c_notification, o_notification, "setSeverity");
914 if (status != 0)
915 {
916 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
917 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
918 return (NULL);
919 }
921 return (o_notification);
922 } /* }}} jobject ctoj_notification */
924 /*
925 * Java to C conversion functions
926 */
927 /* Call a `String <method> ()' method. */
928 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
929 char *buffer, size_t buffer_size, int empty_okay,
930 jclass class_ptr, jobject object_ptr, const char *method_name)
931 {
932 jmethodID method_id;
933 jobject string_obj;
934 const char *c_str;
936 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
937 method_name, "()Ljava/lang/String;");
938 if (method_id == NULL)
939 {
940 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
941 method_name);
942 return (-1);
943 }
945 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
946 if ((string_obj == NULL) && (empty_okay == 0))
947 {
948 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
949 method_name);
950 return (-1);
951 }
952 else if ((string_obj == NULL) && (empty_okay != 0))
953 {
954 memset (buffer, 0, buffer_size);
955 return (0);
956 }
958 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
959 if (c_str == NULL)
960 {
961 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
962 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
963 return (-1);
964 }
966 sstrncpy (buffer, c_str, buffer_size);
968 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
969 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
971 return (0);
972 } /* }}} int jtoc_string */
974 /* Call an `int <method> ()' method. */
975 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
976 jint *ret_value,
977 jclass class_ptr, jobject object_ptr, const char *method_name)
978 {
979 jmethodID method_id;
981 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
982 method_name, "()I");
983 if (method_id == NULL)
984 {
985 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
986 method_name);
987 return (-1);
988 }
990 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
992 return (0);
993 } /* }}} int jtoc_int */
995 /* Call a `long <method> ()' method. */
996 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
997 jlong *ret_value,
998 jclass class_ptr, jobject object_ptr, const char *method_name)
999 {
1000 jmethodID method_id;
1002 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1003 method_name, "()J");
1004 if (method_id == NULL)
1005 {
1006 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1007 method_name);
1008 return (-1);
1009 }
1011 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1013 return (0);
1014 } /* }}} int jtoc_long */
1016 /* Call a `double <method> ()' method. */
1017 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1018 jdouble *ret_value,
1019 jclass class_ptr, jobject object_ptr, const char *method_name)
1020 {
1021 jmethodID method_id;
1023 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1024 method_name, "()D");
1025 if (method_id == NULL)
1026 {
1027 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1028 method_name);
1029 return (-1);
1030 }
1032 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1034 return (0);
1035 } /* }}} int jtoc_double */
1037 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1038 value_t *ret_value, int ds_type, jobject object_ptr)
1039 {
1040 jclass class_ptr;
1041 int status;
1043 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1045 if (ds_type == DS_TYPE_COUNTER)
1046 {
1047 jlong tmp_long;
1049 status = jtoc_long (jvm_env, &tmp_long,
1050 class_ptr, object_ptr, "longValue");
1051 if (status != 0)
1052 {
1053 ERROR ("java plugin: jtoc_value: "
1054 "jtoc_long failed.");
1055 return (-1);
1056 }
1057 (*ret_value).counter = (counter_t) tmp_long;
1058 }
1059 else
1060 {
1061 jdouble tmp_double;
1063 status = jtoc_double (jvm_env, &tmp_double,
1064 class_ptr, object_ptr, "doubleValue");
1065 if (status != 0)
1066 {
1067 ERROR ("java plugin: jtoc_value: "
1068 "jtoc_double failed.");
1069 return (-1);
1070 }
1071 (*ret_value).gauge = (gauge_t) tmp_double;
1072 }
1074 return (0);
1075 } /* }}} int jtoc_value */
1077 /* Read a List<Number>, convert it to `value_t' and add it to the given
1078 * `value_list_t'. */
1079 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1080 const data_set_t *ds, value_list_t *vl,
1081 jclass class_ptr, jobject object_ptr)
1082 {
1083 jmethodID m_getvalues;
1084 jmethodID m_toarray;
1085 jobject o_list;
1086 jobjectArray o_number_array;
1088 value_t *values;
1089 int values_num;
1090 int i;
1092 values_num = ds->ds_num;
1094 values = NULL;
1095 o_number_array = NULL;
1096 o_list = NULL;
1098 #define BAIL_OUT(status) \
1099 free (values); \
1100 if (o_number_array != NULL) \
1101 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1102 if (o_list != NULL) \
1103 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1104 return (status);
1106 /* Call: List<Number> ValueList.getValues () */
1107 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1108 "getValues", "()Ljava/util/List;");
1109 if (m_getvalues == NULL)
1110 {
1111 ERROR ("java plugin: jtoc_values_array: "
1112 "Cannot find method `List getValues ()'.");
1113 BAIL_OUT (-1);
1114 }
1116 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1117 if (o_list == NULL)
1118 {
1119 ERROR ("java plugin: jtoc_values_array: "
1120 "CallObjectMethod (getValues) failed.");
1121 BAIL_OUT (-1);
1122 }
1124 /* Call: Number[] List.toArray () */
1125 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1126 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1127 "toArray", "()[Ljava/lang/Object;");
1128 if (m_toarray == NULL)
1129 {
1130 ERROR ("java plugin: jtoc_values_array: "
1131 "Cannot find method `Object[] toArray ()'.");
1132 BAIL_OUT (-1);
1133 }
1135 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1136 if (o_number_array == NULL)
1137 {
1138 ERROR ("java plugin: jtoc_values_array: "
1139 "CallObjectMethod (toArray) failed.");
1140 BAIL_OUT (-1);
1141 }
1143 values = (value_t *) calloc (values_num, sizeof (value_t));
1144 if (values == NULL)
1145 {
1146 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1147 BAIL_OUT (-1);
1148 }
1150 for (i = 0; i < values_num; i++)
1151 {
1152 jobject o_number;
1153 int status;
1155 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1156 o_number_array, (jsize) i);
1157 if (o_number == NULL)
1158 {
1159 ERROR ("java plugin: jtoc_values_array: "
1160 "GetObjectArrayElement (%i) failed.", i);
1161 BAIL_OUT (-1);
1162 }
1164 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1165 if (status != 0)
1166 {
1167 ERROR ("java plugin: jtoc_values_array: "
1168 "jtoc_value (%i) failed.", i);
1169 BAIL_OUT (-1);
1170 }
1171 } /* for (i = 0; i < values_num; i++) */
1173 vl->values = values;
1174 vl->values_len = values_num;
1176 #undef BAIL_OUT
1177 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1178 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1179 return (0);
1180 } /* }}} int jtoc_values_array */
1182 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1183 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1184 jobject object_ptr)
1185 {
1186 jclass class_ptr;
1187 int status;
1188 jlong tmp_long;
1189 const data_set_t *ds;
1191 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1192 if (class_ptr == NULL)
1193 {
1194 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1195 return (-1);
1196 }
1198 /* eo == empty okay */
1199 #define SET_STRING(buffer,method, eo) do { \
1200 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1201 class_ptr, object_ptr, method); \
1202 if (status != 0) { \
1203 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1204 method); \
1205 return (-1); \
1206 } } while (0)
1208 SET_STRING(vl->type, "getType", /* empty = */ 0);
1210 ds = plugin_get_ds (vl->type);
1211 if (ds == NULL)
1212 {
1213 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1214 "Please consult the types.db(5) manpage for mor information.",
1215 vl->type);
1216 return (-1);
1217 }
1219 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1220 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1221 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1222 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1224 #undef SET_STRING
1226 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1227 if (status != 0)
1228 {
1229 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1230 return (-1);
1231 }
1232 /* Java measures time in milliseconds. */
1233 vl->time = (time_t) (tmp_long / ((jlong) 1000));
1235 status = jtoc_long (jvm_env, &tmp_long,
1236 class_ptr, object_ptr, "getInterval");
1237 if (status != 0)
1238 {
1239 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1240 return (-1);
1241 }
1242 vl->interval = (int) tmp_long;
1244 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1245 if (status != 0)
1246 {
1247 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1248 return (-1);
1249 }
1251 return (0);
1252 } /* }}} int jtoc_value_list */
1254 /* Convert a org/collectd/api/Notification to a notification_t. */
1255 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1256 jobject object_ptr)
1257 {
1258 jclass class_ptr;
1259 int status;
1260 jlong tmp_long;
1261 jint tmp_int;
1263 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1264 if (class_ptr == NULL)
1265 {
1266 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1267 return (-1);
1268 }
1270 /* eo == empty okay */
1271 #define SET_STRING(buffer,method, eo) do { \
1272 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1273 class_ptr, object_ptr, method); \
1274 if (status != 0) { \
1275 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1276 method); \
1277 return (-1); \
1278 } } while (0)
1280 SET_STRING (n->host, "getHost", /* empty = */ 1);
1281 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1282 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1283 SET_STRING (n->type, "getType", /* empty = */ 1);
1284 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1285 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1287 #undef SET_STRING
1289 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1290 if (status != 0)
1291 {
1292 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1293 return (-1);
1294 }
1295 /* Java measures time in milliseconds. */
1296 n->time = (time_t) (tmp_long / ((jlong) 1000));
1298 status = jtoc_int (jvm_env, &tmp_int,
1299 class_ptr, object_ptr, "getSeverity");
1300 if (status != 0)
1301 {
1302 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1303 return (-1);
1304 }
1305 n->severity = (int) tmp_int;
1307 return (0);
1308 } /* }}} int jtoc_notification */
1309 /*
1310 * Functions accessible from Java
1311 */
1312 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1313 jobject this, jobject java_vl)
1314 {
1315 value_list_t vl = VALUE_LIST_INIT;
1316 int status;
1318 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1320 status = jtoc_value_list (jvm_env, &vl, java_vl);
1321 if (status != 0)
1322 {
1323 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1324 return (-1);
1325 }
1327 status = plugin_dispatch_values (&vl);
1329 sfree (vl.values);
1331 return (status);
1332 } /* }}} jint cjni_api_dispatch_values */
1334 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1335 jobject this, jobject o_notification)
1336 {
1337 notification_t n;
1338 int status;
1340 memset (&n, 0, sizeof (n));
1341 n.meta = NULL;
1343 status = jtoc_notification (jvm_env, &n, o_notification);
1344 if (status != 0)
1345 {
1346 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1347 return (-1);
1348 }
1350 status = plugin_dispatch_notification (&n);
1352 return (status);
1353 } /* }}} jint cjni_api_dispatch_notification */
1355 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1356 jobject this, jobject o_string_type)
1357 {
1358 const char *ds_name;
1359 const data_set_t *ds;
1360 jobject o_dataset;
1362 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1363 if (ds_name == NULL)
1364 {
1365 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1366 return (NULL);
1367 }
1369 ds = plugin_get_ds (ds_name);
1370 DEBUG ("java plugin: cjni_api_get_ds: "
1371 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1373 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1375 if (ds == NULL)
1376 return (NULL);
1378 o_dataset = ctoj_data_set (jvm_env, ds);
1379 return (o_dataset);
1380 } /* }}} jint cjni_api_get_ds */
1382 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1383 jobject this, jobject o_name, jobject o_config)
1384 {
1385 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1386 } /* }}} jint cjni_api_register_config */
1388 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1389 jobject this, jobject o_name, jobject o_config)
1390 {
1391 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1392 } /* }}} jint cjni_api_register_init */
1394 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1395 jobject this, jobject o_name, jobject o_read)
1396 {
1397 user_data_t ud;
1398 cjni_callback_info_t *cbi;
1400 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1401 if (cbi == NULL)
1402 return (-1);
1404 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1406 memset (&ud, 0, sizeof (ud));
1407 ud.data = (void *) cbi;
1408 ud.free_func = cjni_callback_info_destroy;
1410 plugin_register_complex_read (cbi->name, cjni_read,
1411 /* interval = */ NULL, &ud);
1413 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1415 return (0);
1416 } /* }}} jint cjni_api_register_read */
1418 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1419 jobject this, jobject o_name, jobject o_write)
1420 {
1421 user_data_t ud;
1422 cjni_callback_info_t *cbi;
1424 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1425 if (cbi == NULL)
1426 return (-1);
1428 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1430 memset (&ud, 0, sizeof (ud));
1431 ud.data = (void *) cbi;
1432 ud.free_func = cjni_callback_info_destroy;
1434 plugin_register_write (cbi->name, cjni_write, &ud);
1436 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1438 return (0);
1439 } /* }}} jint cjni_api_register_write */
1441 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1442 jobject this, jobject o_name, jobject o_flush)
1443 {
1444 user_data_t ud;
1445 cjni_callback_info_t *cbi;
1447 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1448 if (cbi == NULL)
1449 return (-1);
1451 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1453 memset (&ud, 0, sizeof (ud));
1454 ud.data = (void *) cbi;
1455 ud.free_func = cjni_callback_info_destroy;
1457 plugin_register_flush (cbi->name, cjni_flush, &ud);
1459 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1461 return (0);
1462 } /* }}} jint cjni_api_register_flush */
1464 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1465 jobject this, jobject o_name, jobject o_shutdown)
1466 {
1467 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1468 CB_TYPE_SHUTDOWN));
1469 } /* }}} jint cjni_api_register_shutdown */
1471 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1472 jobject this, jobject o_name, jobject o_log)
1473 {
1474 user_data_t ud;
1475 cjni_callback_info_t *cbi;
1477 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1478 if (cbi == NULL)
1479 return (-1);
1481 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1483 memset (&ud, 0, sizeof (ud));
1484 ud.data = (void *) cbi;
1485 ud.free_func = cjni_callback_info_destroy;
1487 plugin_register_log (cbi->name, cjni_log, &ud);
1489 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1491 return (0);
1492 } /* }}} jint cjni_api_register_log */
1494 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1495 jobject this, jobject o_name, jobject o_notification)
1496 {
1497 user_data_t ud;
1498 cjni_callback_info_t *cbi;
1500 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1501 CB_TYPE_NOTIFICATION);
1502 if (cbi == NULL)
1503 return (-1);
1505 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1507 memset (&ud, 0, sizeof (ud));
1508 ud.data = (void *) cbi;
1509 ud.free_func = cjni_callback_info_destroy;
1511 plugin_register_notification (cbi->name, cjni_notification, &ud);
1513 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1515 return (0);
1516 } /* }}} jint cjni_api_register_notification */
1518 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1519 jobject this, jobject o_name, jobject o_match, int type)
1520 {
1521 int status;
1522 const char *c_name;
1524 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1525 if (c_name == NULL)
1526 {
1527 ERROR ("java plugin: cjni_api_register_match_target: "
1528 "GetStringUTFChars failed.");
1529 return (-1);
1530 }
1532 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1533 if (status != 0)
1534 {
1535 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1536 return (-1);
1537 }
1539 if (type == CB_TYPE_MATCH)
1540 {
1541 match_proc_t m_proc;
1543 memset (&m_proc, 0, sizeof (m_proc));
1544 m_proc.create = cjni_match_target_create;
1545 m_proc.destroy = cjni_match_target_destroy;
1546 m_proc.match = (void *) cjni_match_target_invoke;
1548 status = fc_register_match (c_name, m_proc);
1549 }
1550 else if (type == CB_TYPE_TARGET)
1551 {
1552 target_proc_t t_proc;
1554 memset (&t_proc, 0, sizeof (t_proc));
1555 t_proc.create = cjni_match_target_create;
1556 t_proc.destroy = cjni_match_target_destroy;
1557 t_proc.invoke = cjni_match_target_invoke;
1559 status = fc_register_target (c_name, t_proc);
1560 }
1561 else
1562 {
1563 ERROR ("java plugin: cjni_api_register_match_target: "
1564 "Don't know whether to create a match or a target.");
1565 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1566 return (-1);
1567 }
1569 if (status != 0)
1570 {
1571 ERROR ("java plugin: cjni_api_register_match_target: "
1572 "%s failed.",
1573 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1574 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1575 return (-1);
1576 }
1578 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1580 return (0);
1581 } /* }}} jint cjni_api_register_match_target */
1583 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1584 jobject this, jobject o_name, jobject o_match)
1585 {
1586 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1587 CB_TYPE_MATCH));
1588 } /* }}} jint cjni_api_register_match */
1590 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1591 jobject this, jobject o_name, jobject o_target)
1592 {
1593 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1594 CB_TYPE_TARGET));
1595 } /* }}} jint cjni_api_register_target */
1597 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1598 jobject this, jint severity, jobject o_message)
1599 {
1600 const char *c_str;
1602 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1603 if (c_str == NULL)
1604 {
1605 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1606 return;
1607 }
1609 if (severity < LOG_ERR)
1610 severity = LOG_ERR;
1611 if (severity > LOG_DEBUG)
1612 severity = LOG_DEBUG;
1614 plugin_log (severity, "%s", c_str);
1616 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1617 } /* }}} void cjni_api_log */
1619 /* List of ``native'' functions, i. e. C-functions that can be called from
1620 * Java. */
1621 static JNINativeMethod jni_api_functions[] = /* {{{ */
1622 {
1623 { "dispatchValues",
1624 "(Lorg/collectd/api/ValueList;)I",
1625 cjni_api_dispatch_values },
1627 { "dispatchNotification",
1628 "(Lorg/collectd/api/Notification;)I",
1629 cjni_api_dispatch_notification },
1631 { "getDS",
1632 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1633 cjni_api_get_ds },
1635 { "registerConfig",
1636 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1637 cjni_api_register_config },
1639 { "registerInit",
1640 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1641 cjni_api_register_init },
1643 { "registerRead",
1644 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1645 cjni_api_register_read },
1647 { "registerWrite",
1648 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1649 cjni_api_register_write },
1651 { "registerFlush",
1652 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1653 cjni_api_register_flush },
1655 { "registerShutdown",
1656 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1657 cjni_api_register_shutdown },
1659 { "registerLog",
1660 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1661 cjni_api_register_log },
1663 { "registerNotification",
1664 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1665 cjni_api_register_notification },
1667 { "registerMatch",
1668 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1669 cjni_api_register_match },
1671 { "registerTarget",
1672 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1673 cjni_api_register_target },
1675 { "log",
1676 "(ILjava/lang/String;)V",
1677 cjni_api_log },
1678 };
1679 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1680 / sizeof (jni_api_functions[0]);
1681 /* }}} */
1683 /*
1684 * Functions
1685 */
1686 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1687 * all registration functions. */
1688 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1689 jobject o_name, jobject o_callback, int type)
1690 {
1691 const char *c_name;
1692 cjni_callback_info_t *cbi;
1693 const char *method_name;
1694 const char *method_signature;
1696 switch (type)
1697 {
1698 case CB_TYPE_CONFIG:
1699 method_name = "config";
1700 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1701 break;
1703 case CB_TYPE_INIT:
1704 method_name = "init";
1705 method_signature = "()I";
1706 break;
1708 case CB_TYPE_READ:
1709 method_name = "read";
1710 method_signature = "()I";
1711 break;
1713 case CB_TYPE_WRITE:
1714 method_name = "write";
1715 method_signature = "(Lorg/collectd/api/ValueList;)I";
1716 break;
1718 case CB_TYPE_FLUSH:
1719 method_name = "flush";
1720 method_signature = "(ILjava/lang/String;)I";
1721 break;
1723 case CB_TYPE_SHUTDOWN:
1724 method_name = "shutdown";
1725 method_signature = "()I";
1726 break;
1728 case CB_TYPE_LOG:
1729 method_name = "log";
1730 method_signature = "(ILjava/lang/String;)V";
1731 break;
1733 case CB_TYPE_NOTIFICATION:
1734 method_name = "notification";
1735 method_signature = "(Lorg/collectd/api/Notification;)I";
1736 break;
1738 case CB_TYPE_MATCH:
1739 method_name = "createMatch";
1740 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1741 "Lorg/collectd/api/CollectdMatchInterface;";
1742 break;
1744 case CB_TYPE_TARGET:
1745 method_name = "createTarget";
1746 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1747 "Lorg/collectd/api/CollectdTargetInterface;";
1748 break;
1750 default:
1751 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1752 type);
1753 return (NULL);
1754 }
1756 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1757 if (c_name == NULL)
1758 {
1759 ERROR ("java plugin: cjni_callback_info_create: "
1760 "GetStringUTFChars failed.");
1761 return (NULL);
1762 }
1764 cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1765 if (cbi == NULL)
1766 {
1767 ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1768 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1769 return (NULL);
1770 }
1771 memset (cbi, 0, sizeof (*cbi));
1772 cbi->type = type;
1774 cbi->name = strdup (c_name);
1775 if (cbi->name == NULL)
1776 {
1777 pthread_mutex_unlock (&java_callbacks_lock);
1778 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1779 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1780 return (NULL);
1781 }
1783 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1785 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1786 if (cbi->object == NULL)
1787 {
1788 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1789 free (cbi);
1790 return (NULL);
1791 }
1793 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1794 if (cbi->class == NULL)
1795 {
1796 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1797 free (cbi);
1798 return (NULL);
1799 }
1801 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1802 method_name, method_signature);
1803 if (cbi->method == NULL)
1804 {
1805 ERROR ("java plugin: cjni_callback_info_create: "
1806 "Cannot find the `%s' method with signature `%s'.",
1807 method_name, method_signature);
1808 free (cbi);
1809 return (NULL);
1810 }
1812 return (cbi);
1813 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1815 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1816 * to the global `java_callbacks' variable. This is used for `config', `init',
1817 * and `shutdown' callbacks. */
1818 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1819 jobject o_name, jobject o_callback, int type)
1820 {
1821 cjni_callback_info_t *cbi;
1822 cjni_callback_info_t *tmp;
1823 #if COLLECT_DEBUG
1824 const char *type_str;
1825 #endif
1827 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1828 if (cbi == NULL)
1829 return (-1);
1831 #if COLLECT_DEBUG
1832 switch (type)
1833 {
1834 case CB_TYPE_CONFIG:
1835 type_str = "config";
1836 break;
1838 case CB_TYPE_INIT:
1839 type_str = "init";
1840 break;
1842 case CB_TYPE_SHUTDOWN:
1843 type_str = "shutdown";
1844 break;
1846 case CB_TYPE_MATCH:
1847 type_str = "match";
1848 break;
1850 case CB_TYPE_TARGET:
1851 type_str = "target";
1852 break;
1854 default:
1855 type_str = "<unknown>";
1856 }
1857 DEBUG ("java plugin: Registering new %s callback: %s",
1858 type_str, cbi->name);
1859 #endif
1861 pthread_mutex_lock (&java_callbacks_lock);
1863 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1864 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1865 if (tmp == NULL)
1866 {
1867 pthread_mutex_unlock (&java_callbacks_lock);
1868 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1870 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1871 free (cbi);
1873 return (-1);
1874 }
1875 java_callbacks = tmp;
1876 java_callbacks[java_callbacks_num] = *cbi;
1877 java_callbacks_num++;
1879 pthread_mutex_unlock (&java_callbacks_lock);
1881 free (cbi);
1882 return (0);
1883 } /* }}} int cjni_callback_register */
1885 /* Callback for `pthread_key_create'. It frees the data contained in
1886 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1887 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1888 {
1889 cjni_jvm_env_t *cjni_env;
1891 if (args == NULL)
1892 return;
1894 cjni_env = (cjni_jvm_env_t *) args;
1896 if (cjni_env->reference_counter > 0)
1897 {
1898 ERROR ("java plugin: cjni_jvm_env_destroy: "
1899 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1900 }
1902 if (cjni_env->jvm_env != NULL)
1903 {
1904 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1905 (void *) cjni_env->jvm_env);
1906 }
1908 /* The pointer is allocated in `cjni_thread_attach' */
1909 free (cjni_env);
1910 } /* }}} void cjni_jvm_env_destroy */
1912 /* Register ``native'' functions with the JVM. Native functions are C-functions
1913 * that can be called by Java code. */
1914 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1915 {
1916 jclass api_class_ptr;
1917 int status;
1919 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1920 if (api_class_ptr == NULL)
1921 {
1922 ERROR ("cjni_init_native: Cannot find API class `org/collectd/api/Collectd'.");
1923 return (-1);
1924 }
1926 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1927 jni_api_functions, (jint) jni_api_functions_num);
1928 if (status != 0)
1929 {
1930 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1931 return (-1);
1932 }
1934 return (0);
1935 } /* }}} int cjni_init_native */
1937 /* Create the JVM. This is called when the first thread tries to access the JVM
1938 * via cjni_thread_attach. */
1939 static int cjni_create_jvm (void) /* {{{ */
1940 {
1941 JNIEnv *jvm_env;
1942 JavaVMInitArgs vm_args;
1943 JavaVMOption vm_options[jvm_argc];
1945 int status;
1946 size_t i;
1948 if (jvm != NULL)
1949 return (0);
1951 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1952 if (status != 0)
1953 {
1954 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1955 "with status %i.", status);
1956 return (-1);
1957 }
1959 jvm_env = NULL;
1961 memset (&vm_args, 0, sizeof (vm_args));
1962 vm_args.version = JNI_VERSION_1_2;
1963 vm_args.options = vm_options;
1964 vm_args.nOptions = (jint) jvm_argc;
1966 for (i = 0; i < jvm_argc; i++)
1967 {
1968 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
1969 i, jvm_argv[i]);
1970 vm_args.options[i].optionString = jvm_argv[i];
1971 }
1973 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
1974 if (status != 0)
1975 {
1976 ERROR ("java plugin: cjni_create_jvm: "
1977 "JNI_CreateJavaVM failed with status %i.",
1978 status);
1979 return (-1);
1980 }
1981 assert (jvm != NULL);
1982 assert (jvm_env != NULL);
1984 /* Call RegisterNatives */
1985 status = cjni_init_native (jvm_env);
1986 if (status != 0)
1987 {
1988 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
1989 return (-1);
1990 }
1992 DEBUG ("java plugin: The JVM has been created.");
1993 return (0);
1994 } /* }}} int cjni_create_jvm */
1996 /* Increase the reference counter to the JVM for this thread. If it was zero,
1997 * attach the JVM first. */
1998 static JNIEnv *cjni_thread_attach (void) /* {{{ */
1999 {
2000 cjni_jvm_env_t *cjni_env;
2001 JNIEnv *jvm_env;
2003 /* If we're the first thread to access the JVM, we'll have to create it
2004 * first.. */
2005 if (jvm == NULL)
2006 {
2007 int status;
2009 status = cjni_create_jvm ();
2010 if (status != 0)
2011 {
2012 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2013 return (NULL);
2014 }
2015 }
2016 assert (jvm != NULL);
2018 cjni_env = pthread_getspecific (jvm_env_key);
2019 if (cjni_env == NULL)
2020 {
2021 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2022 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
2023 if (cjni_env == NULL)
2024 {
2025 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
2026 return (NULL);
2027 }
2028 memset (cjni_env, 0, sizeof (*cjni_env));
2029 cjni_env->reference_counter = 0;
2030 cjni_env->jvm_env = NULL;
2032 pthread_setspecific (jvm_env_key, cjni_env);
2033 }
2035 if (cjni_env->reference_counter > 0)
2036 {
2037 cjni_env->reference_counter++;
2038 jvm_env = cjni_env->jvm_env;
2039 }
2040 else
2041 {
2042 int status;
2043 JavaVMAttachArgs args;
2045 assert (cjni_env->jvm_env == NULL);
2047 memset (&args, 0, sizeof (args));
2048 args.version = JNI_VERSION_1_2;
2050 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2051 if (status != 0)
2052 {
2053 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2054 "with status %i.", status);
2055 return (NULL);
2056 }
2058 cjni_env->reference_counter = 1;
2059 cjni_env->jvm_env = jvm_env;
2060 }
2062 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2063 cjni_env->reference_counter);
2064 assert (jvm_env != NULL);
2065 return (jvm_env);
2066 } /* }}} JNIEnv *cjni_thread_attach */
2068 /* Decrease the reference counter of this thread. If it reaches zero, detach
2069 * from the JVM. */
2070 static int cjni_thread_detach (void) /* {{{ */
2071 {
2072 cjni_jvm_env_t *cjni_env;
2073 int status;
2075 cjni_env = pthread_getspecific (jvm_env_key);
2076 if (cjni_env == NULL)
2077 {
2078 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2079 return (-1);
2080 }
2082 assert (cjni_env->reference_counter > 0);
2083 assert (cjni_env->jvm_env != NULL);
2085 cjni_env->reference_counter--;
2086 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2087 cjni_env->reference_counter);
2089 if (cjni_env->reference_counter > 0)
2090 return (0);
2092 status = (*jvm)->DetachCurrentThread (jvm);
2093 if (status != 0)
2094 {
2095 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2096 "with status %i.", status);
2097 }
2099 cjni_env->reference_counter = 0;
2100 cjni_env->jvm_env = NULL;
2102 return (0);
2103 } /* }}} JNIEnv *cjni_thread_attach */
2105 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2106 {
2107 char **tmp;
2109 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2110 {
2111 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2112 return (-1);
2113 }
2115 if (jvm != NULL)
2116 {
2117 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2118 "`LoadPlugin' options! The JVM is already started and I have to "
2119 "ignore this argument: %s",
2120 ci->values[0].value.string);
2121 return (-1);
2122 }
2124 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2125 if (tmp == NULL)
2126 {
2127 ERROR ("java plugin: realloc failed.");
2128 return (-1);
2129 }
2130 jvm_argv = tmp;
2132 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2133 if (jvm_argv[jvm_argc] == NULL)
2134 {
2135 ERROR ("java plugin: strdup failed.");
2136 return (-1);
2137 }
2138 jvm_argc++;
2140 return (0);
2141 } /* }}} int cjni_config_add_jvm_arg */
2143 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2144 {
2145 JNIEnv *jvm_env;
2146 java_plugin_class_t *class;
2147 jmethodID constructor_id;
2148 jobject tmp_object;
2150 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2151 {
2152 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2153 return (-1);
2154 }
2156 jvm_env = cjni_thread_attach ();
2157 if (jvm_env == NULL)
2158 return (-1);
2160 class = (java_plugin_class_t *) realloc (java_classes_list,
2161 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2162 if (class == NULL)
2163 {
2164 ERROR ("java plugin: realloc failed.");
2165 cjni_thread_detach ();
2166 return (-1);
2167 }
2168 java_classes_list = class;
2169 class = java_classes_list + java_classes_list_len;
2171 memset (class, 0, sizeof (*class));
2172 class->name = strdup (ci->values[0].value.string);
2173 if (class->name == NULL)
2174 {
2175 ERROR ("java plugin: strdup failed.");
2176 cjni_thread_detach ();
2177 return (-1);
2178 }
2179 class->class = NULL;
2180 class->object = NULL;
2182 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2183 thorough the Java community, but (Sun's) `FindClass' and friends need
2184 slashes. */
2185 size_t i;
2186 for (i = 0; class->name[i] != 0; i++)
2187 if (class->name[i] == '.')
2188 class->name[i] = '/';
2189 }
2191 DEBUG ("java plugin: Loading class %s", class->name);
2193 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2194 if (class->class == NULL)
2195 {
2196 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2197 class->name);
2198 cjni_thread_detach ();
2199 free (class->name);
2200 return (-1);
2201 }
2203 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2204 "<init>", "()V");
2205 if (constructor_id == NULL)
2206 {
2207 ERROR ("java plugin: cjni_config_load_plugin: "
2208 "Could not find the constructor for `%s'.",
2209 class->name);
2210 cjni_thread_detach ();
2211 free (class->name);
2212 return (-1);
2213 }
2215 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2216 constructor_id);
2217 if (tmp_object != NULL)
2218 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2219 else
2220 class->object = NULL;
2221 if (class->object == NULL)
2222 {
2223 ERROR ("java plugin: cjni_config_load_plugin: "
2224 "Could create a new `%s' object.",
2225 class->name);
2226 cjni_thread_detach ();
2227 free (class->name);
2228 return (-1);
2229 }
2231 cjni_thread_detach ();
2233 java_classes_list_len++;
2235 return (0);
2236 } /* }}} int cjni_config_load_plugin */
2238 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2239 {
2240 JNIEnv *jvm_env;
2241 cjni_callback_info_t *cbi;
2242 jobject o_ocitem;
2243 const char *name;
2244 int status;
2245 size_t i;
2247 jclass class;
2248 jmethodID method;
2250 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2251 {
2252 WARNING ("java plugin: `Plugin' blocks "
2253 "need exactly one string argument.");
2254 return (-1);
2255 }
2257 name = ci->values[0].value.string;
2259 cbi = NULL;
2260 for (i = 0; i < java_callbacks_num; i++)
2261 {
2262 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2263 continue;
2265 if (strcmp (name, java_callbacks[i].name) != 0)
2266 continue;
2268 cbi = java_callbacks + i;
2269 break;
2270 }
2272 if (cbi == NULL)
2273 {
2274 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2275 "configuration callback has been registered. Please make sure, the "
2276 "`LoadPlugin' lines precede the `Plugin' blocks.",
2277 name);
2278 return (0);
2279 }
2281 DEBUG ("java plugin: Configuring %s", name);
2283 jvm_env = cjni_thread_attach ();
2284 if (jvm_env == NULL)
2285 return (-1);
2287 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2288 if (o_ocitem == NULL)
2289 {
2290 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2291 cjni_thread_detach ();
2292 return (-1);
2293 }
2295 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2296 method = (*jvm_env)->GetMethodID (jvm_env, class,
2297 "config", "(Lorg/collectd/api/OConfigItem;)I");
2299 status = (*jvm_env)->CallIntMethod (jvm_env,
2300 cbi->object, method, o_ocitem);
2302 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2303 cjni_thread_detach ();
2304 return (0);
2305 } /* }}} int cjni_config_plugin_block */
2307 static int cjni_config (oconfig_item_t *ci) /* {{{ */
2308 {
2309 int success;
2310 int errors;
2311 int status;
2312 int i;
2314 success = 0;
2315 errors = 0;
2317 for (i = 0; i < ci->children_num; i++)
2318 {
2319 oconfig_item_t *child = ci->children + i;
2321 if (strcasecmp ("JVMArg", child->key) == 0)
2322 {
2323 status = cjni_config_add_jvm_arg (child);
2324 if (status == 0)
2325 success++;
2326 else
2327 errors++;
2328 }
2329 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2330 {
2331 status = cjni_config_load_plugin (child);
2332 if (status == 0)
2333 success++;
2334 else
2335 errors++;
2336 }
2337 else if (strcasecmp ("Plugin", child->key) == 0)
2338 {
2339 status = cjni_config_plugin_block (child);
2340 if (status == 0)
2341 success++;
2342 else
2343 errors++;
2344 }
2345 else
2346 {
2347 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2348 errors++;
2349 }
2350 }
2352 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2353 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2355 if ((success == 0) && (errors > 0))
2356 {
2357 ERROR ("java plugin: All statements failed.");
2358 return (-1);
2359 }
2361 return (0);
2362 } /* }}} int cjni_config */
2364 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2365 * and `cjni_write'. In particular, delete the global reference to the Java
2366 * object. */
2367 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2368 {
2369 JNIEnv *jvm_env;
2370 cjni_callback_info_t *cbi;
2372 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2374 cbi = (cjni_callback_info_t *) arg;
2376 /* This condition can occurr when shutting down. */
2377 if (jvm == NULL)
2378 {
2379 sfree (cbi);
2380 return;
2381 }
2383 if (arg == NULL)
2384 return;
2386 jvm_env = cjni_thread_attach ();
2387 if (jvm_env == NULL)
2388 {
2389 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2390 return;
2391 }
2393 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2395 cbi->method = NULL;
2396 cbi->object = NULL;
2397 cbi->class = NULL;
2398 free (cbi);
2400 cjni_thread_detach ();
2401 } /* }}} void cjni_callback_info_destroy */
2403 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2404 static int cjni_read (user_data_t *ud) /* {{{ */
2405 {
2406 JNIEnv *jvm_env;
2407 cjni_callback_info_t *cbi;
2408 int status;
2409 int ret_status;
2411 if (jvm == NULL)
2412 {
2413 ERROR ("java plugin: cjni_read: jvm == NULL");
2414 return (-1);
2415 }
2417 if ((ud == NULL) || (ud->data == NULL))
2418 {
2419 ERROR ("java plugin: cjni_read: Invalid user data.");
2420 return (-1);
2421 }
2423 jvm_env = cjni_thread_attach ();
2424 if (jvm_env == NULL)
2425 return (-1);
2427 cbi = (cjni_callback_info_t *) ud->data;
2429 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2430 cbi->method);
2432 status = cjni_thread_detach ();
2433 if (status != 0)
2434 {
2435 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2436 return (-1);
2437 }
2439 return (ret_status);
2440 } /* }}} int cjni_read */
2442 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2443 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2444 user_data_t *ud)
2445 {
2446 JNIEnv *jvm_env;
2447 cjni_callback_info_t *cbi;
2448 jobject vl_java;
2449 int status;
2450 int ret_status;
2452 if (jvm == NULL)
2453 {
2454 ERROR ("java plugin: cjni_write: jvm == NULL");
2455 return (-1);
2456 }
2458 if ((ud == NULL) || (ud->data == NULL))
2459 {
2460 ERROR ("java plugin: cjni_write: Invalid user data.");
2461 return (-1);
2462 }
2464 jvm_env = cjni_thread_attach ();
2465 if (jvm_env == NULL)
2466 return (-1);
2468 cbi = (cjni_callback_info_t *) ud->data;
2470 vl_java = ctoj_value_list (jvm_env, ds, vl);
2471 if (vl_java == NULL)
2472 {
2473 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2474 return (-1);
2475 }
2477 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2478 cbi->object, cbi->method, vl_java);
2480 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2482 status = cjni_thread_detach ();
2483 if (status != 0)
2484 {
2485 ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2486 return (-1);
2487 }
2489 return (ret_status);
2490 } /* }}} int cjni_write */
2492 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2493 static int cjni_flush (int timeout, const char *identifier, /* {{{ */
2494 user_data_t *ud)
2495 {
2496 JNIEnv *jvm_env;
2497 cjni_callback_info_t *cbi;
2498 jobject o_identifier;
2499 int status;
2500 int ret_status;
2502 if (jvm == NULL)
2503 {
2504 ERROR ("java plugin: cjni_flush: jvm == NULL");
2505 return (-1);
2506 }
2508 if ((ud == NULL) || (ud->data == NULL))
2509 {
2510 ERROR ("java plugin: cjni_flush: Invalid user data.");
2511 return (-1);
2512 }
2514 jvm_env = cjni_thread_attach ();
2515 if (jvm_env == NULL)
2516 return (-1);
2518 cbi = (cjni_callback_info_t *) ud->data;
2520 o_identifier = NULL;
2521 if (identifier != NULL)
2522 {
2523 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2524 if (o_identifier == NULL)
2525 {
2526 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2527 return (-1);
2528 }
2529 }
2531 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2532 cbi->object, cbi->method, (jint) timeout, o_identifier);
2534 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2536 status = cjni_thread_detach ();
2537 if (status != 0)
2538 {
2539 ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2540 return (-1);
2541 }
2543 return (ret_status);
2544 } /* }}} int cjni_flush */
2546 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2547 static void cjni_log (int severity, const char *message, /* {{{ */
2548 user_data_t *ud)
2549 {
2550 JNIEnv *jvm_env;
2551 cjni_callback_info_t *cbi;
2552 jobject o_message;
2554 if (jvm == NULL)
2555 return;
2557 if ((ud == NULL) || (ud->data == NULL))
2558 return;
2560 jvm_env = cjni_thread_attach ();
2561 if (jvm_env == NULL)
2562 return;
2564 cbi = (cjni_callback_info_t *) ud->data;
2566 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2567 if (o_message == NULL)
2568 return;
2570 (*jvm_env)->CallVoidMethod (jvm_env,
2571 cbi->object, cbi->method, (jint) severity, o_message);
2573 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2575 cjni_thread_detach ();
2576 } /* }}} void cjni_log */
2578 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2579 * pointer. */
2580 static int cjni_notification (const notification_t *n, /* {{{ */
2581 user_data_t *ud)
2582 {
2583 JNIEnv *jvm_env;
2584 cjni_callback_info_t *cbi;
2585 jobject o_notification;
2586 int status;
2587 int ret_status;
2589 if (jvm == NULL)
2590 {
2591 ERROR ("java plugin: cjni_read: jvm == NULL");
2592 return (-1);
2593 }
2595 if ((ud == NULL) || (ud->data == NULL))
2596 {
2597 ERROR ("java plugin: cjni_read: Invalid user data.");
2598 return (-1);
2599 }
2601 jvm_env = cjni_thread_attach ();
2602 if (jvm_env == NULL)
2603 return (-1);
2605 cbi = (cjni_callback_info_t *) ud->data;
2607 o_notification = ctoj_notification (jvm_env, n);
2608 if (o_notification == NULL)
2609 {
2610 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2611 return (-1);
2612 }
2614 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2615 cbi->object, cbi->method, o_notification);
2617 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2619 status = cjni_thread_detach ();
2620 if (status != 0)
2621 {
2622 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2623 return (-1);
2624 }
2626 return (ret_status);
2627 } /* }}} int cjni_notification */
2629 /* Callbacks for matches implemented in Java */
2630 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2631 void **user_data)
2632 {
2633 JNIEnv *jvm_env;
2634 cjni_callback_info_t *cbi_ret;
2635 cjni_callback_info_t *cbi_factory;
2636 const char *name;
2637 jobject o_ci;
2638 jobject o_tmp;
2639 int type;
2640 size_t i;
2642 cbi_ret = NULL;
2643 o_ci = NULL;
2644 jvm_env = NULL;
2646 #define BAIL_OUT(status) \
2647 if (cbi_ret != NULL) { \
2648 free (cbi_ret->name); \
2649 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2650 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2651 } \
2652 free (cbi_ret); \
2653 if (jvm_env != NULL) { \
2654 if (o_ci != NULL) \
2655 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2656 cjni_thread_detach (); \
2657 } \
2658 return (status)
2660 if (jvm == NULL)
2661 {
2662 ERROR ("java plugin: cjni_read: jvm == NULL");
2663 BAIL_OUT (-1);
2664 }
2666 jvm_env = cjni_thread_attach ();
2667 if (jvm_env == NULL)
2668 {
2669 BAIL_OUT (-1);
2670 }
2672 /* Find out whether to create a match or a target. */
2673 if (strcasecmp ("Match", ci->key) == 0)
2674 type = CB_TYPE_MATCH;
2675 else if (strcasecmp ("Target", ci->key) == 0)
2676 type = CB_TYPE_TARGET;
2677 else
2678 {
2679 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2680 "to create a match or a target.");
2681 BAIL_OUT (-1);
2682 }
2684 /* This is the name of the match we should create. */
2685 name = ci->values[0].value.string;
2687 /* Lets see if we have a matching factory here.. */
2688 cbi_factory = NULL;
2689 for (i = 0; i < java_callbacks_num; i++)
2690 {
2691 if (java_callbacks[i].type != type)
2692 continue;
2694 if (strcmp (name, java_callbacks[i].name) != 0)
2695 continue;
2697 cbi_factory = java_callbacks + i;
2698 break;
2699 }
2701 /* Nope, no factory for that name.. */
2702 if (cbi_factory == NULL)
2703 {
2704 ERROR ("java plugin: cjni_match_target_create: "
2705 "No such match factory registered: %s",
2706 name);
2707 BAIL_OUT (-1);
2708 }
2710 /* We convert `ci' to its Java equivalent.. */
2711 o_ci = ctoj_oconfig_item (jvm_env, ci);
2712 if (o_ci == NULL)
2713 {
2714 ERROR ("java plugin: cjni_match_target_create: "
2715 "ctoj_oconfig_item failed.");
2716 BAIL_OUT (-1);
2717 }
2719 /* Allocate a new callback info structure. This is going to be our user_data
2720 * pointer. */
2721 cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
2722 if (cbi_ret == NULL)
2723 {
2724 ERROR ("java plugin: cjni_match_target_create: malloc failed.");
2725 BAIL_OUT (-1);
2726 }
2727 memset (cbi_ret, 0, sizeof (*cbi_ret));
2728 cbi_ret->object = NULL;
2729 cbi_ret->type = type;
2731 /* Lets fill the callback info structure.. First, the name: */
2732 cbi_ret->name = strdup (name);
2733 if (cbi_ret->name == NULL)
2734 {
2735 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2736 BAIL_OUT (-1);
2737 }
2739 /* Then call the factory method so it creates a new object for us. */
2740 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2741 cbi_factory->object, cbi_factory->method, o_ci);
2742 if (o_tmp == NULL)
2743 {
2744 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2745 BAIL_OUT (-1);
2746 }
2748 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2749 if (o_tmp == NULL)
2750 {
2751 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2752 BAIL_OUT (-1);
2753 }
2755 /* This is the class of the match. It is possibly different from the class of
2756 * the match-factory! */
2757 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2758 if (cbi_ret->class == NULL)
2759 {
2760 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2761 BAIL_OUT (-1);
2762 }
2764 /* Lookup the `int match (DataSet, ValueList)' method. */
2765 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2766 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2767 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2768 if (cbi_ret->method == NULL)
2769 {
2770 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2771 BAIL_OUT (-1);
2772 }
2774 /* Return the newly created match via the user_data pointer. */
2775 *user_data = (void *) cbi_ret;
2777 cjni_thread_detach ();
2779 DEBUG ("java plugin: cjni_match_target_create: "
2780 "Successfully created a `%s' %s.",
2781 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2783 /* Success! */
2784 return (0);
2785 #undef BAIL_OUT
2786 } /* }}} int cjni_match_target_create */
2788 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2789 {
2790 cjni_callback_info_destroy (*user_data);
2791 *user_data = NULL;
2793 return (0);
2794 } /* }}} int cjni_match_target_destroy */
2796 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2797 value_list_t *vl, notification_meta_t **meta, void **user_data)
2798 {
2799 JNIEnv *jvm_env;
2800 cjni_callback_info_t *cbi;
2801 jobject o_vl;
2802 jobject o_ds;
2803 int ret_status;
2804 int status;
2806 if (jvm == NULL)
2807 {
2808 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2809 return (-1);
2810 }
2812 jvm_env = cjni_thread_attach ();
2813 if (jvm_env == NULL)
2814 return (-1);
2816 cbi = (cjni_callback_info_t *) *user_data;
2818 o_vl = ctoj_value_list (jvm_env, ds, vl);
2819 if (o_vl == NULL)
2820 {
2821 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2822 cjni_thread_detach ();
2823 return (-1);
2824 }
2826 o_ds = ctoj_data_set (jvm_env, ds);
2827 if (o_ds == NULL)
2828 {
2829 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2830 cjni_thread_detach ();
2831 return (-1);
2832 }
2834 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2835 o_ds, o_vl);
2837 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2839 /* If we're executing a target, copy the `ValueList' back to our
2840 * `value_list_t'. */
2841 if (cbi->type == CB_TYPE_TARGET)
2842 {
2843 value_list_t new_vl;
2845 memset (&new_vl, 0, sizeof (new_vl));
2846 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2847 if (status != 0)
2848 {
2849 ERROR ("java plugin: cjni_match_target_invoke: "
2850 "jtoc_value_list failed.");
2851 }
2852 else /* if (status == 0) */
2853 {
2854 /* plugin_dispatch_values assures that this is dynamically allocated
2855 * memory. */
2856 sfree (vl->values);
2858 /* This will replace the vl->values pointer to a new, dynamically
2859 * allocated piece of memory. */
2860 memcpy (vl, &new_vl, sizeof (*vl));
2861 }
2862 } /* if (cbi->type == CB_TYPE_TARGET) */
2864 status = cjni_thread_detach ();
2865 if (status != 0)
2866 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2868 return (ret_status);
2869 } /* }}} int cjni_match_target_invoke */
2871 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2872 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2873 {
2874 int status;
2875 size_t i;
2877 for (i = 0; i < java_callbacks_num; i++)
2878 {
2879 if (java_callbacks[i].type != CB_TYPE_INIT)
2880 continue;
2882 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2884 status = (*jvm_env)->CallIntMethod (jvm_env,
2885 java_callbacks[i].object, java_callbacks[i].method);
2886 if (status != 0)
2887 {
2888 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2889 "Removing read function.",
2890 java_callbacks[i].name, status);
2891 plugin_unregister_read (java_callbacks[i].name);
2892 }
2893 }
2895 return (0);
2896 } /* }}} int cjni_init_plugins */
2898 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2899 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2900 {
2901 int status;
2902 size_t i;
2904 for (i = 0; i < java_callbacks_num; i++)
2905 {
2906 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2907 continue;
2909 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2911 status = (*jvm_env)->CallIntMethod (jvm_env,
2912 java_callbacks[i].object, java_callbacks[i].method);
2913 if (status != 0)
2914 {
2915 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2916 java_callbacks[i].name, status);
2917 }
2918 }
2920 return (0);
2921 } /* }}} int cjni_shutdown_plugins */
2924 static int cjni_shutdown (void) /* {{{ */
2925 {
2926 JNIEnv *jvm_env;
2927 JavaVMAttachArgs args;
2928 int status;
2929 size_t i;
2931 if (jvm == NULL)
2932 return (0);
2934 jvm_env = NULL;
2935 memset (&args, 0, sizeof (args));
2936 args.version = JNI_VERSION_1_2;
2938 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
2939 if (status != 0)
2940 {
2941 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2942 status);
2943 return (-1);
2944 }
2946 /* Execute all the shutdown functions registered by plugins. */
2947 cjni_shutdown_plugins (jvm_env);
2949 /* Release all the global references to callback functions */
2950 for (i = 0; i < java_callbacks_num; i++)
2951 {
2952 if (java_callbacks[i].object != NULL)
2953 {
2954 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
2955 java_callbacks[i].object = NULL;
2956 }
2957 sfree (java_callbacks[i].name);
2958 }
2959 java_callbacks_num = 0;
2960 sfree (java_callbacks);
2962 /* Release all the global references to directly loaded classes. */
2963 for (i = 0; i < java_classes_list_len; i++)
2964 {
2965 if (java_classes_list[i].object != NULL)
2966 {
2967 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
2968 java_classes_list[i].object = NULL;
2969 }
2970 sfree (java_classes_list[i].name);
2971 }
2972 java_classes_list_len = 0;
2973 sfree (java_classes_list);
2975 /* Destroy the JVM */
2976 DEBUG ("java plugin: Destroying the JVM.");
2977 (*jvm)->DestroyJavaVM (jvm);
2978 jvm = NULL;
2979 jvm_env = NULL;
2981 pthread_key_delete (jvm_env_key);
2983 /* Free the JVM argument list */
2984 for (i = 0; i < jvm_argc; i++)
2985 sfree (jvm_argv[i]);
2986 jvm_argc = 0;
2987 sfree (jvm_argv);
2989 return (0);
2990 } /* }}} int cjni_shutdown */
2992 /* Initialization: Create a JVM, load all configured classes and call their
2993 * `config' and `init' callback methods. */
2994 static int cjni_init (void) /* {{{ */
2995 {
2996 JNIEnv *jvm_env;
2998 if (jvm == NULL)
2999 {
3000 ERROR ("java plugin: cjni_init: jvm == NULL");
3001 return (-1);
3002 }
3004 jvm_env = cjni_thread_attach ();
3005 if (jvm_env == NULL)
3006 return (-1);
3008 cjni_init_plugins (jvm_env);
3010 cjni_thread_detach ();
3011 return (0);
3012 } /* }}} int cjni_init */
3014 void module_register (void)
3015 {
3016 plugin_register_complex_config ("java", cjni_config);
3017 plugin_register_init ("java", cjni_init);
3018 plugin_register_shutdown ("java", cjni_shutdown);
3019 } /* void module_register (void) */
3021 /* vim: set sw=2 sts=2 et fdm=marker : */