1 /**
2 * collectd - src/java.c
3 * Copyright (C) 2009 Florian octo Forster
4 * Copyright (C) 2008 Justo Alonso Achaques
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at collectd.org>
21 * Justo Alonso Achaques <justo.alonso at gmail.com>
22 **/
24 #include "collectd.h"
25 #include "plugin.h"
26 #include "common.h"
27 #include "filter_chain.h"
29 #include <pthread.h>
30 #include <jni.h>
32 #if !defined(JNI_VERSION_1_2)
33 # error "Need JNI 1.2 compatible interface!"
34 #endif
36 /*
37 * Types
38 */
39 struct cjni_jvm_env_s /* {{{ */
40 {
41 JNIEnv *jvm_env;
42 int reference_counter;
43 };
44 typedef struct cjni_jvm_env_s cjni_jvm_env_t;
45 /* }}} */
47 struct java_plugin_class_s /* {{{ */
48 {
49 char *name;
50 jclass class;
51 jobject object;
52 };
53 typedef struct java_plugin_class_s java_plugin_class_t;
54 /* }}} */
56 #define CB_TYPE_CONFIG 1
57 #define CB_TYPE_INIT 2
58 #define CB_TYPE_READ 3
59 #define CB_TYPE_WRITE 4
60 #define CB_TYPE_FLUSH 5
61 #define CB_TYPE_SHUTDOWN 6
62 #define CB_TYPE_LOG 7
63 #define CB_TYPE_NOTIFICATION 8
64 #define CB_TYPE_MATCH 9
65 #define CB_TYPE_TARGET 10
66 struct cjni_callback_info_s /* {{{ */
67 {
68 char *name;
69 int type;
70 jclass class;
71 jobject object;
72 jmethodID method;
73 };
74 typedef struct cjni_callback_info_s cjni_callback_info_t;
75 /* }}} */
77 /*
78 * Global variables
79 */
80 static JavaVM *jvm = NULL;
81 static pthread_key_t jvm_env_key;
83 /* Configuration options for the JVM. */
84 static char **jvm_argv = NULL;
85 static size_t jvm_argc = 0;
87 /* List of class names to load */
88 static java_plugin_class_t *java_classes_list = NULL;
89 static size_t java_classes_list_len;
91 /* List of config, init, and shutdown callbacks. */
92 static cjni_callback_info_t *java_callbacks = NULL;
93 static size_t java_callbacks_num = 0;
94 static pthread_mutex_t java_callbacks_lock = PTHREAD_MUTEX_INITIALIZER;
96 static oconfig_item_t *config_block = NULL;
98 /*
99 * Prototypes
100 *
101 * Mostly functions that are needed by the Java interface (``native'')
102 * functions.
103 */
104 static void cjni_callback_info_destroy (void *arg);
105 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env,
106 jobject o_name, jobject o_callback, int type);
107 static int cjni_callback_register (JNIEnv *jvm_env, jobject o_name,
108 jobject o_callback, int type);
109 static int cjni_read (user_data_t *user_data);
110 static int cjni_write (const data_set_t *ds, const value_list_t *vl,
111 user_data_t *ud);
112 static int cjni_flush (cdtime_t timeout, const char *identifier, user_data_t *ud);
113 static void cjni_log (int severity, const char *message, user_data_t *ud);
114 static int cjni_notification (const notification_t *n, user_data_t *ud);
116 /* Create, destroy, and match/invoke functions, used by both, matches AND
117 * targets. */
118 static int cjni_match_target_create (const oconfig_item_t *ci, void **user_data);
119 static int cjni_match_target_destroy (void **user_data);
120 static int cjni_match_target_invoke (const data_set_t *ds, value_list_t *vl,
121 notification_meta_t **meta, void **user_data);
123 /*
124 * C to Java conversion functions
125 */
126 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
127 const char *string,
128 jclass class_ptr, jobject object_ptr, const char *method_name)
129 {
130 jmethodID m_set;
131 jstring o_string;
133 /* Create a java.lang.String */
134 o_string = (*jvm_env)->NewStringUTF (jvm_env,
135 (string != NULL) ? string : "");
136 if (o_string == NULL)
137 {
138 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
139 return (-1);
140 }
142 /* Search for the `void setFoo (String s)' method. */
143 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
144 method_name, "(Ljava/lang/String;)V");
145 if (m_set == NULL)
146 {
147 ERROR ("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
148 method_name);
149 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
150 return (-1);
151 }
153 /* Call the method. */
154 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, o_string);
156 /* Decrease reference counter on the java.lang.String object. */
157 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
159 return (0);
160 } /* }}} int ctoj_string */
162 static jstring ctoj_output_string (JNIEnv *jvm_env, /* {{{ */
163 const char *string)
164 {
165 jstring o_string;
167 /* Create a java.lang.String */
168 o_string = (*jvm_env)->NewStringUTF (jvm_env,
169 (string != NULL) ? string : "");
170 if (o_string == NULL)
171 {
172 ERROR ("java plugin: ctoj_output_string: NewStringUTF failed.");
173 return NULL;
174 }
176 return (o_string);
177 } /* }}} int ctoj_output_string */
179 static int ctoj_int (JNIEnv *jvm_env, /* {{{ */
180 jint value,
181 jclass class_ptr, jobject object_ptr, const char *method_name)
182 {
183 jmethodID m_set;
185 /* Search for the `void setFoo (int i)' method. */
186 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
187 method_name, "(I)V");
188 if (m_set == NULL)
189 {
190 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
191 method_name);
192 return (-1);
193 }
195 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
197 return (0);
198 } /* }}} int ctoj_int */
200 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
201 jlong value,
202 jclass class_ptr, jobject object_ptr, const char *method_name)
203 {
204 jmethodID m_set;
206 /* Search for the `void setFoo (long l)' method. */
207 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
208 method_name, "(J)V");
209 if (m_set == NULL)
210 {
211 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
212 method_name);
213 return (-1);
214 }
216 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
218 return (0);
219 } /* }}} int ctoj_long */
221 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
222 jdouble value,
223 jclass class_ptr, jobject object_ptr, const char *method_name)
224 {
225 jmethodID m_set;
227 /* Search for the `void setFoo (double d)' method. */
228 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
229 method_name, "(D)V");
230 if (m_set == NULL)
231 {
232 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
233 method_name);
234 return (-1);
235 }
237 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
239 return (0);
240 } /* }}} int ctoj_double */
242 /* Convert a jlong to a java.lang.Number */
243 static jobject ctoj_jlong_to_number (JNIEnv *jvm_env, jlong value) /* {{{ */
244 {
245 jclass c_long;
246 jmethodID m_long_constructor;
248 /* Look up the java.lang.Long class */
249 c_long = (*jvm_env)->FindClass (jvm_env, "java/lang/Long");
250 if (c_long == NULL)
251 {
252 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
253 "java.lang.Long class failed.");
254 return (NULL);
255 }
257 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
258 c_long, "<init>", "(J)V");
259 if (m_long_constructor == NULL)
260 {
261 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
262 "`Long (long)' constructor failed.");
263 return (NULL);
264 }
266 return ((*jvm_env)->NewObject (jvm_env,
267 c_long, m_long_constructor, value));
268 } /* }}} jobject ctoj_jlong_to_number */
270 /* Convert a jdouble to a java.lang.Number */
271 static jobject ctoj_jdouble_to_number (JNIEnv *jvm_env, jdouble value) /* {{{ */
272 {
273 jclass c_double;
274 jmethodID m_double_constructor;
276 /* Look up the java.lang.Long class */
277 c_double = (*jvm_env)->FindClass (jvm_env, "java/lang/Double");
278 if (c_double == NULL)
279 {
280 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
281 "java.lang.Double class failed.");
282 return (NULL);
283 }
285 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
286 c_double, "<init>", "(D)V");
287 if (m_double_constructor == NULL)
288 {
289 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
290 "`Double (double)' constructor failed.");
291 return (NULL);
292 }
294 return ((*jvm_env)->NewObject (jvm_env,
295 c_double, m_double_constructor, value));
296 } /* }}} jobject ctoj_jdouble_to_number */
298 /* Convert a value_t to a java.lang.Number */
299 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
300 value_t value, int ds_type)
301 {
302 if (ds_type == DS_TYPE_COUNTER)
303 return (ctoj_jlong_to_number (jvm_env, (jlong) value.counter));
304 else if (ds_type == DS_TYPE_GAUGE)
305 return (ctoj_jdouble_to_number (jvm_env, (jdouble) value.gauge));
306 if (ds_type == DS_TYPE_DERIVE)
307 return (ctoj_jlong_to_number (jvm_env, (jlong) value.derive));
308 if (ds_type == DS_TYPE_ABSOLUTE)
309 return (ctoj_jlong_to_number (jvm_env, (jlong) value.absolute));
310 else
311 return (NULL);
312 } /* }}} jobject ctoj_value_to_number */
314 /* Convert a data_source_t to a org/collectd/api/DataSource */
315 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
316 const data_source_t *dsrc)
317 {
318 jclass c_datasource;
319 jmethodID m_datasource_constructor;
320 jobject o_datasource;
321 int status;
323 /* Look up the DataSource class */
324 c_datasource = (*jvm_env)->FindClass (jvm_env,
325 "org/collectd/api/DataSource");
326 if (c_datasource == NULL)
327 {
328 ERROR ("java plugin: ctoj_data_source: "
329 "FindClass (org/collectd/api/DataSource) failed.");
330 return (NULL);
331 }
333 /* Lookup the `ValueList ()' constructor. */
334 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
335 "<init>", "()V");
336 if (m_datasource_constructor == NULL)
337 {
338 ERROR ("java plugin: ctoj_data_source: Cannot find the "
339 "`DataSource ()' constructor.");
340 return (NULL);
341 }
343 /* Create a new instance. */
344 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
345 m_datasource_constructor);
346 if (o_datasource == NULL)
347 {
348 ERROR ("java plugin: ctoj_data_source: "
349 "Creating a new DataSource instance failed.");
350 return (NULL);
351 }
353 /* Set name via `void setName (String name)' */
354 status = ctoj_string (jvm_env, dsrc->name,
355 c_datasource, o_datasource, "setName");
356 if (status != 0)
357 {
358 ERROR ("java plugin: ctoj_data_source: "
359 "ctoj_string (setName) failed.");
360 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
361 return (NULL);
362 }
364 /* Set type via `void setType (int type)' */
365 status = ctoj_int (jvm_env, dsrc->type,
366 c_datasource, o_datasource, "setType");
367 if (status != 0)
368 {
369 ERROR ("java plugin: ctoj_data_source: "
370 "ctoj_int (setType) failed.");
371 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
372 return (NULL);
373 }
375 /* Set min via `void setMin (double min)' */
376 status = ctoj_double (jvm_env, dsrc->min,
377 c_datasource, o_datasource, "setMin");
378 if (status != 0)
379 {
380 ERROR ("java plugin: ctoj_data_source: "
381 "ctoj_double (setMin) failed.");
382 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
383 return (NULL);
384 }
386 /* Set max via `void setMax (double max)' */
387 status = ctoj_double (jvm_env, dsrc->max,
388 c_datasource, o_datasource, "setMax");
389 if (status != 0)
390 {
391 ERROR ("java plugin: ctoj_data_source: "
392 "ctoj_double (setMax) failed.");
393 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
394 return (NULL);
395 }
397 return (o_datasource);
398 } /* }}} jobject ctoj_data_source */
400 /* Convert a oconfig_value_t to a org/collectd/api/OConfigValue */
401 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
402 oconfig_value_t ocvalue)
403 {
404 jclass c_ocvalue;
405 jmethodID m_ocvalue_constructor;
406 jobject o_argument;
407 jobject o_ocvalue;
409 m_ocvalue_constructor = NULL;
410 o_argument = NULL;
412 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
413 "org/collectd/api/OConfigValue");
414 if (c_ocvalue == NULL)
415 {
416 ERROR ("java plugin: ctoj_oconfig_value: "
417 "FindClass (org/collectd/api/OConfigValue) failed.");
418 return (NULL);
419 }
421 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
422 {
423 jboolean tmp_boolean;
425 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
427 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
428 "<init>", "(Z)V");
429 if (m_ocvalue_constructor == NULL)
430 {
431 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
432 "`OConfigValue (boolean)' constructor.");
433 return (NULL);
434 }
436 return ((*jvm_env)->NewObject (jvm_env,
437 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
438 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
439 else if (ocvalue.type == OCONFIG_TYPE_STRING)
440 {
441 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
442 "<init>", "(Ljava/lang/String;)V");
443 if (m_ocvalue_constructor == NULL)
444 {
445 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
446 "`OConfigValue (String)' constructor.");
447 return (NULL);
448 }
450 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
451 if (o_argument == NULL)
452 {
453 ERROR ("java plugin: ctoj_oconfig_value: "
454 "Creating a String object failed.");
455 return (NULL);
456 }
457 }
458 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
459 {
460 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
461 "<init>", "(Ljava/lang/Number;)V");
462 if (m_ocvalue_constructor == NULL)
463 {
464 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
465 "`OConfigValue (Number)' constructor.");
466 return (NULL);
467 }
469 o_argument = ctoj_jdouble_to_number (jvm_env,
470 (jdouble) ocvalue.value.number);
471 if (o_argument == NULL)
472 {
473 ERROR ("java plugin: ctoj_oconfig_value: "
474 "Creating a Number object failed.");
475 return (NULL);
476 }
477 }
478 else
479 {
480 return (NULL);
481 }
483 assert (m_ocvalue_constructor != NULL);
484 assert (o_argument != NULL);
486 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
487 c_ocvalue, m_ocvalue_constructor, o_argument);
488 if (o_ocvalue == NULL)
489 {
490 ERROR ("java plugin: ctoj_oconfig_value: "
491 "Creating an OConfigValue object failed.");
492 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
493 return (NULL);
494 }
496 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
497 return (o_ocvalue);
498 } /* }}} jobject ctoj_oconfig_value */
500 /* Convert a oconfig_item_t to a org/collectd/api/OConfigItem */
501 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
502 const oconfig_item_t *ci)
503 {
504 jclass c_ocitem;
505 jmethodID m_ocitem_constructor;
506 jmethodID m_addvalue;
507 jmethodID m_addchild;
508 jobject o_key;
509 jobject o_ocitem;
510 int i;
512 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/OConfigItem");
513 if (c_ocitem == NULL)
514 {
515 ERROR ("java plugin: ctoj_oconfig_item: "
516 "FindClass (org/collectd/api/OConfigItem) failed.");
517 return (NULL);
518 }
520 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
521 * {{{ */
522 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
523 "<init>", "(Ljava/lang/String;)V");
524 if (m_ocitem_constructor == NULL)
525 {
526 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
527 "`OConfigItem (String)' constructor.");
528 return (NULL);
529 }
531 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
532 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
533 if (m_addvalue == NULL)
534 {
535 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
536 "`addValue (OConfigValue)' method.");
537 return (NULL);
538 }
540 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
541 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
542 if (m_addchild == NULL)
543 {
544 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
545 "`addChild (OConfigItem)' method.");
546 return (NULL);
547 }
548 /* }}} */
550 /* Create a String object with the key.
551 * Needed for calling the constructor. */
552 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
553 if (o_key == NULL)
554 {
555 ERROR ("java plugin: ctoj_oconfig_item: "
556 "Creating String object failed.");
557 return (NULL);
558 }
560 /* Create an OConfigItem object */
561 o_ocitem = (*jvm_env)->NewObject (jvm_env,
562 c_ocitem, m_ocitem_constructor, o_key);
563 if (o_ocitem == NULL)
564 {
565 ERROR ("java plugin: ctoj_oconfig_item: "
566 "Creating an OConfigItem object failed.");
567 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
568 return (NULL);
569 }
571 /* We don't need the String object any longer.. */
572 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
574 /* Call OConfigItem.addValue for each value */
575 for (i = 0; i < ci->values_num; i++) /* {{{ */
576 {
577 jobject o_value;
579 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
580 if (o_value == NULL)
581 {
582 ERROR ("java plugin: ctoj_oconfig_item: "
583 "Creating an OConfigValue object failed.");
584 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
585 return (NULL);
586 }
588 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
589 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
590 } /* }}} for (i = 0; i < ci->values_num; i++) */
592 /* Call OConfigItem.addChild for each child */
593 for (i = 0; i < ci->children_num; i++) /* {{{ */
594 {
595 jobject o_child;
597 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
598 if (o_child == NULL)
599 {
600 ERROR ("java plugin: ctoj_oconfig_item: "
601 "Creating an OConfigItem object failed.");
602 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
603 return (NULL);
604 }
606 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addchild, o_child);
607 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
608 } /* }}} for (i = 0; i < ci->children_num; i++) */
610 return (o_ocitem);
611 } /* }}} jobject ctoj_oconfig_item */
613 /* Convert a data_set_t to a org/collectd/api/DataSet */
614 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
615 {
616 jclass c_dataset;
617 jmethodID m_constructor;
618 jmethodID m_add;
619 jobject o_type;
620 jobject o_dataset;
621 int i;
623 /* Look up the org/collectd/api/DataSet class */
624 c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet");
625 if (c_dataset == NULL)
626 {
627 ERROR ("java plugin: ctoj_data_set: Looking up the "
628 "org/collectd/api/DataSet class failed.");
629 return (NULL);
630 }
632 /* Search for the `DataSet (String type)' constructor. */
633 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
634 c_dataset, "<init>", "(Ljava/lang/String;)V");
635 if (m_constructor == NULL)
636 {
637 ERROR ("java plugin: ctoj_data_set: Looking up the "
638 "`DataSet (String)' constructor failed.");
639 return (NULL);
640 }
642 /* Search for the `void addDataSource (DataSource)' method. */
643 m_add = (*jvm_env)->GetMethodID (jvm_env,
644 c_dataset, "addDataSource", "(Lorg/collectd/api/DataSource;)V");
645 if (m_add == NULL)
646 {
647 ERROR ("java plugin: ctoj_data_set: Looking up the "
648 "`addDataSource (DataSource)' method failed.");
649 return (NULL);
650 }
652 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
653 if (o_type == NULL)
654 {
655 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
656 return (NULL);
657 }
659 o_dataset = (*jvm_env)->NewObject (jvm_env,
660 c_dataset, m_constructor, o_type);
661 if (o_dataset == NULL)
662 {
663 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
664 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
665 return (NULL);
666 }
668 /* Decrease reference counter on the java.lang.String object. */
669 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
671 for (i = 0; i < ds->ds_num; i++)
672 {
673 jobject o_datasource;
675 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
676 if (o_datasource == NULL)
677 {
678 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
679 ds->type, ds->ds[i].name);
680 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
681 return (NULL);
682 }
684 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
686 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
687 } /* for (i = 0; i < ds->ds_num; i++) */
689 return (o_dataset);
690 } /* }}} jobject ctoj_data_set */
692 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
693 value_t value, int ds_type,
694 jclass class_ptr, jobject object_ptr)
695 {
696 jmethodID m_addvalue;
697 jobject o_number;
699 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
700 "addValue", "(Ljava/lang/Number;)V");
701 if (m_addvalue == NULL)
702 {
703 ERROR ("java plugin: ctoj_value_list_add_value: "
704 "Cannot find method `void addValue (Number)'.");
705 return (-1);
706 }
708 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
709 if (o_number == NULL)
710 {
711 ERROR ("java plugin: ctoj_value_list_add_value: "
712 "ctoj_value_to_number failed.");
713 return (-1);
714 }
716 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
718 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
720 return (0);
721 } /* }}} int ctoj_value_list_add_value */
723 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
724 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
725 {
726 jmethodID m_setdataset;
727 jobject o_dataset;
729 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
730 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
731 "setDataSet", "(Lorg/collectd/api/DataSet;)V");
732 if (m_setdataset == NULL)
733 {
734 ERROR ("java plugin: ctoj_value_list_add_data_set: "
735 "Cannot find the `void setDataSet (DataSet)' method.");
736 return (-1);
737 }
739 /* Create a DataSet object. */
740 o_dataset = ctoj_data_set (jvm_env, ds);
741 if (o_dataset == NULL)
742 {
743 ERROR ("java plugin: ctoj_value_list_add_data_set: "
744 "ctoj_data_set (%s) failed.", ds->type);
745 return (-1);
746 }
748 /* Actually call the method. */
749 (*jvm_env)->CallVoidMethod (jvm_env,
750 o_valuelist, m_setdataset, o_dataset);
752 /* Decrease reference counter on the List<DataSource> object. */
753 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
755 return (0);
756 } /* }}} int ctoj_value_list_add_data_set */
758 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
759 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
760 const data_set_t *ds, const value_list_t *vl)
761 {
762 jclass c_valuelist;
763 jmethodID m_valuelist_constructor;
764 jobject o_valuelist;
765 int status;
766 int i;
768 /* First, create a new ValueList instance..
769 * Look up the class.. */
770 c_valuelist = (*jvm_env)->FindClass (jvm_env,
771 "org/collectd/api/ValueList");
772 if (c_valuelist == NULL)
773 {
774 ERROR ("java plugin: ctoj_value_list: "
775 "FindClass (org/collectd/api/ValueList) failed.");
776 return (NULL);
777 }
779 /* Lookup the `ValueList ()' constructor. */
780 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
781 "<init>", "()V");
782 if (m_valuelist_constructor == NULL)
783 {
784 ERROR ("java plugin: ctoj_value_list: Cannot find the "
785 "`ValueList ()' constructor.");
786 return (NULL);
787 }
789 /* Create a new instance. */
790 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
791 m_valuelist_constructor);
792 if (o_valuelist == NULL)
793 {
794 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
795 "failed.");
796 return (NULL);
797 }
799 status = ctoj_value_list_add_data_set (jvm_env,
800 c_valuelist, o_valuelist, ds);
801 if (status != 0)
802 {
803 ERROR ("java plugin: ctoj_value_list: "
804 "ctoj_value_list_add_data_set failed.");
805 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
806 return (NULL);
807 }
809 /* Set the strings.. */
810 #define SET_STRING(str,method_name) do { \
811 status = ctoj_string (jvm_env, str, \
812 c_valuelist, o_valuelist, method_name); \
813 if (status != 0) { \
814 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
815 method_name); \
816 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
817 return (NULL); \
818 } } while (0)
820 SET_STRING (vl->host, "setHost");
821 SET_STRING (vl->plugin, "setPlugin");
822 SET_STRING (vl->plugin_instance, "setPluginInstance");
823 SET_STRING (vl->type, "setType");
824 SET_STRING (vl->type_instance, "setTypeInstance");
826 #undef SET_STRING
828 /* Set the `time' member. Java stores time in milliseconds. */
829 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (vl->time),
830 c_valuelist, o_valuelist, "setTime");
831 if (status != 0)
832 {
833 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
834 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
835 return (NULL);
836 }
838 /* Set the `interval' member.. */
839 status = ctoj_long (jvm_env,
840 (jlong) CDTIME_T_TO_MS (vl->interval),
841 c_valuelist, o_valuelist, "setInterval");
842 if (status != 0)
843 {
844 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
845 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
846 return (NULL);
847 }
849 for (i = 0; i < vl->values_len; i++)
850 {
851 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
852 c_valuelist, o_valuelist);
853 if (status != 0)
854 {
855 ERROR ("java plugin: ctoj_value_list: "
856 "ctoj_value_list_add_value failed.");
857 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
858 return (NULL);
859 }
860 }
862 return (o_valuelist);
863 } /* }}} jobject ctoj_value_list */
865 /* Convert a notification_t to a org/collectd/api/Notification */
866 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
867 const notification_t *n)
868 {
869 jclass c_notification;
870 jmethodID m_constructor;
871 jobject o_notification;
872 int status;
874 /* First, create a new Notification instance..
875 * Look up the class.. */
876 c_notification = (*jvm_env)->FindClass (jvm_env,
877 "org/collectd/api/Notification");
878 if (c_notification == NULL)
879 {
880 ERROR ("java plugin: ctoj_notification: "
881 "FindClass (org/collectd/api/Notification) failed.");
882 return (NULL);
883 }
885 /* Lookup the `Notification ()' constructor. */
886 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
887 "<init>", "()V");
888 if (m_constructor == NULL)
889 {
890 ERROR ("java plugin: ctoj_notification: Cannot find the "
891 "`Notification ()' constructor.");
892 return (NULL);
893 }
895 /* Create a new instance. */
896 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
897 m_constructor);
898 if (o_notification == NULL)
899 {
900 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
901 "instance failed.");
902 return (NULL);
903 }
905 /* Set the strings.. */
906 #define SET_STRING(str,method_name) do { \
907 status = ctoj_string (jvm_env, str, \
908 c_notification, o_notification, method_name); \
909 if (status != 0) { \
910 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
911 method_name); \
912 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
913 return (NULL); \
914 } } while (0)
916 SET_STRING (n->host, "setHost");
917 SET_STRING (n->plugin, "setPlugin");
918 SET_STRING (n->plugin_instance, "setPluginInstance");
919 SET_STRING (n->type, "setType");
920 SET_STRING (n->type_instance, "setTypeInstance");
921 SET_STRING (n->message, "setMessage");
923 #undef SET_STRING
925 /* Set the `time' member. Java stores time in milliseconds. */
926 status = ctoj_long (jvm_env, (jlong) CDTIME_T_TO_MS (n->time),
927 c_notification, o_notification, "setTime");
928 if (status != 0)
929 {
930 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
931 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
932 return (NULL);
933 }
935 /* Set the `severity' member.. */
936 status = ctoj_int (jvm_env, (jint) n->severity,
937 c_notification, o_notification, "setSeverity");
938 if (status != 0)
939 {
940 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
941 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
942 return (NULL);
943 }
945 return (o_notification);
946 } /* }}} jobject ctoj_notification */
948 /*
949 * Java to C conversion functions
950 */
951 /* Call a `String <method> ()' method. */
952 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
953 char *buffer, size_t buffer_size, int empty_okay,
954 jclass class_ptr, jobject object_ptr, const char *method_name)
955 {
956 jmethodID method_id;
957 jobject string_obj;
958 const char *c_str;
960 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
961 method_name, "()Ljava/lang/String;");
962 if (method_id == NULL)
963 {
964 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
965 method_name);
966 return (-1);
967 }
969 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
970 if ((string_obj == NULL) && (empty_okay == 0))
971 {
972 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
973 method_name);
974 return (-1);
975 }
976 else if ((string_obj == NULL) && (empty_okay != 0))
977 {
978 memset (buffer, 0, buffer_size);
979 return (0);
980 }
982 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
983 if (c_str == NULL)
984 {
985 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
986 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
987 return (-1);
988 }
990 sstrncpy (buffer, c_str, buffer_size);
992 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
993 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
995 return (0);
996 } /* }}} int jtoc_string */
998 /* Call an `int <method> ()' method. */
999 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
1000 jint *ret_value,
1001 jclass class_ptr, jobject object_ptr, const char *method_name)
1002 {
1003 jmethodID method_id;
1005 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1006 method_name, "()I");
1007 if (method_id == NULL)
1008 {
1009 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
1010 method_name);
1011 return (-1);
1012 }
1014 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
1016 return (0);
1017 } /* }}} int jtoc_int */
1019 /* Call a `long <method> ()' method. */
1020 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
1021 jlong *ret_value,
1022 jclass class_ptr, jobject object_ptr, const char *method_name)
1023 {
1024 jmethodID method_id;
1026 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1027 method_name, "()J");
1028 if (method_id == NULL)
1029 {
1030 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1031 method_name);
1032 return (-1);
1033 }
1035 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1037 return (0);
1038 } /* }}} int jtoc_long */
1040 /* Call a `double <method> ()' method. */
1041 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1042 jdouble *ret_value,
1043 jclass class_ptr, jobject object_ptr, const char *method_name)
1044 {
1045 jmethodID method_id;
1047 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1048 method_name, "()D");
1049 if (method_id == NULL)
1050 {
1051 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1052 method_name);
1053 return (-1);
1054 }
1056 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1058 return (0);
1059 } /* }}} int jtoc_double */
1061 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1062 value_t *ret_value, int ds_type, jobject object_ptr)
1063 {
1064 jclass class_ptr;
1065 int status;
1067 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1069 if (ds_type == DS_TYPE_GAUGE)
1070 {
1071 jdouble tmp_double;
1073 status = jtoc_double (jvm_env, &tmp_double,
1074 class_ptr, object_ptr, "doubleValue");
1075 if (status != 0)
1076 {
1077 ERROR ("java plugin: jtoc_value: "
1078 "jtoc_double failed.");
1079 return (-1);
1080 }
1081 (*ret_value).gauge = (gauge_t) tmp_double;
1082 }
1083 else
1084 {
1085 jlong tmp_long;
1087 status = jtoc_long (jvm_env, &tmp_long,
1088 class_ptr, object_ptr, "longValue");
1089 if (status != 0)
1090 {
1091 ERROR ("java plugin: jtoc_value: "
1092 "jtoc_long failed.");
1093 return (-1);
1094 }
1096 if (ds_type == DS_TYPE_DERIVE)
1097 (*ret_value).derive = (derive_t) tmp_long;
1098 else if (ds_type == DS_TYPE_ABSOLUTE)
1099 (*ret_value).absolute = (absolute_t) tmp_long;
1100 else
1101 (*ret_value).counter = (counter_t) tmp_long;
1102 }
1104 return (0);
1105 } /* }}} int jtoc_value */
1107 /* Read a List<Number>, convert it to `value_t' and add it to the given
1108 * `value_list_t'. */
1109 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1110 const data_set_t *ds, value_list_t *vl,
1111 jclass class_ptr, jobject object_ptr)
1112 {
1113 jmethodID m_getvalues;
1114 jmethodID m_toarray;
1115 jobject o_list;
1116 jobjectArray o_number_array;
1118 value_t *values;
1119 int values_num;
1120 int i;
1122 values_num = ds->ds_num;
1124 values = NULL;
1125 o_number_array = NULL;
1126 o_list = NULL;
1128 #define BAIL_OUT(status) \
1129 free (values); \
1130 if (o_number_array != NULL) \
1131 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1132 if (o_list != NULL) \
1133 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1134 return (status);
1136 /* Call: List<Number> ValueList.getValues () */
1137 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1138 "getValues", "()Ljava/util/List;");
1139 if (m_getvalues == NULL)
1140 {
1141 ERROR ("java plugin: jtoc_values_array: "
1142 "Cannot find method `List getValues ()'.");
1143 BAIL_OUT (-1);
1144 }
1146 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1147 if (o_list == NULL)
1148 {
1149 ERROR ("java plugin: jtoc_values_array: "
1150 "CallObjectMethod (getValues) failed.");
1151 BAIL_OUT (-1);
1152 }
1154 /* Call: Number[] List.toArray () */
1155 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1156 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1157 "toArray", "()[Ljava/lang/Object;");
1158 if (m_toarray == NULL)
1159 {
1160 ERROR ("java plugin: jtoc_values_array: "
1161 "Cannot find method `Object[] toArray ()'.");
1162 BAIL_OUT (-1);
1163 }
1165 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1166 if (o_number_array == NULL)
1167 {
1168 ERROR ("java plugin: jtoc_values_array: "
1169 "CallObjectMethod (toArray) failed.");
1170 BAIL_OUT (-1);
1171 }
1173 values = (value_t *) calloc (values_num, sizeof (value_t));
1174 if (values == NULL)
1175 {
1176 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1177 BAIL_OUT (-1);
1178 }
1180 for (i = 0; i < values_num; i++)
1181 {
1182 jobject o_number;
1183 int status;
1185 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1186 o_number_array, (jsize) i);
1187 if (o_number == NULL)
1188 {
1189 ERROR ("java plugin: jtoc_values_array: "
1190 "GetObjectArrayElement (%i) failed.", i);
1191 BAIL_OUT (-1);
1192 }
1194 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1195 if (status != 0)
1196 {
1197 ERROR ("java plugin: jtoc_values_array: "
1198 "jtoc_value (%i) failed.", i);
1199 BAIL_OUT (-1);
1200 }
1201 } /* for (i = 0; i < values_num; i++) */
1203 vl->values = values;
1204 vl->values_len = values_num;
1206 #undef BAIL_OUT
1207 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1208 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1209 return (0);
1210 } /* }}} int jtoc_values_array */
1212 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1213 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1214 jobject object_ptr)
1215 {
1216 jclass class_ptr;
1217 int status;
1218 jlong tmp_long;
1219 const data_set_t *ds;
1221 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1222 if (class_ptr == NULL)
1223 {
1224 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1225 return (-1);
1226 }
1228 /* eo == empty okay */
1229 #define SET_STRING(buffer,method, eo) do { \
1230 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1231 class_ptr, object_ptr, method); \
1232 if (status != 0) { \
1233 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1234 method); \
1235 return (-1); \
1236 } } while (0)
1238 SET_STRING(vl->type, "getType", /* empty = */ 0);
1240 ds = plugin_get_ds (vl->type);
1241 if (ds == NULL)
1242 {
1243 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1244 "Please consult the types.db(5) manpage for mor information.",
1245 vl->type);
1246 return (-1);
1247 }
1249 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1250 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1251 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1252 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1254 #undef SET_STRING
1256 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1257 if (status != 0)
1258 {
1259 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1260 return (-1);
1261 }
1262 /* Java measures time in milliseconds. */
1263 vl->time = MS_TO_CDTIME_T (tmp_long);
1265 status = jtoc_long (jvm_env, &tmp_long,
1266 class_ptr, object_ptr, "getInterval");
1267 if (status != 0)
1268 {
1269 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1270 return (-1);
1271 }
1272 vl->interval = MS_TO_CDTIME_T (tmp_long);
1274 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1275 if (status != 0)
1276 {
1277 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1278 return (-1);
1279 }
1281 return (0);
1282 } /* }}} int jtoc_value_list */
1284 /* Convert a org/collectd/api/Notification to a notification_t. */
1285 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1286 jobject object_ptr)
1287 {
1288 jclass class_ptr;
1289 int status;
1290 jlong tmp_long;
1291 jint tmp_int;
1293 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1294 if (class_ptr == NULL)
1295 {
1296 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1297 return (-1);
1298 }
1300 /* eo == empty okay */
1301 #define SET_STRING(buffer,method, eo) do { \
1302 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1303 class_ptr, object_ptr, method); \
1304 if (status != 0) { \
1305 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1306 method); \
1307 return (-1); \
1308 } } while (0)
1310 SET_STRING (n->host, "getHost", /* empty = */ 1);
1311 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1312 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1313 SET_STRING (n->type, "getType", /* empty = */ 1);
1314 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1315 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1317 #undef SET_STRING
1319 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1320 if (status != 0)
1321 {
1322 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1323 return (-1);
1324 }
1325 /* Java measures time in milliseconds. */
1326 n->time = MS_TO_CDTIME_T(tmp_long);
1328 status = jtoc_int (jvm_env, &tmp_int,
1329 class_ptr, object_ptr, "getSeverity");
1330 if (status != 0)
1331 {
1332 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1333 return (-1);
1334 }
1335 n->severity = (int) tmp_int;
1337 return (0);
1338 } /* }}} int jtoc_notification */
1339 /*
1340 * Functions accessible from Java
1341 */
1342 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1343 jobject this, jobject java_vl)
1344 {
1345 value_list_t vl = VALUE_LIST_INIT;
1346 int status;
1348 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1350 status = jtoc_value_list (jvm_env, &vl, java_vl);
1351 if (status != 0)
1352 {
1353 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1354 return (-1);
1355 }
1357 status = plugin_dispatch_values (&vl);
1359 sfree (vl.values);
1361 return (status);
1362 } /* }}} jint cjni_api_dispatch_values */
1364 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1365 jobject this, jobject o_notification)
1366 {
1367 notification_t n;
1368 int status;
1370 memset (&n, 0, sizeof (n));
1371 n.meta = NULL;
1373 status = jtoc_notification (jvm_env, &n, o_notification);
1374 if (status != 0)
1375 {
1376 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1377 return (-1);
1378 }
1380 status = plugin_dispatch_notification (&n);
1382 return (status);
1383 } /* }}} jint cjni_api_dispatch_notification */
1385 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1386 jobject this, jobject o_string_type)
1387 {
1388 const char *ds_name;
1389 const data_set_t *ds;
1390 jobject o_dataset;
1392 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1393 if (ds_name == NULL)
1394 {
1395 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1396 return (NULL);
1397 }
1399 ds = plugin_get_ds (ds_name);
1400 DEBUG ("java plugin: cjni_api_get_ds: "
1401 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1403 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1405 if (ds == NULL)
1406 return (NULL);
1408 o_dataset = ctoj_data_set (jvm_env, ds);
1409 return (o_dataset);
1410 } /* }}} jint cjni_api_get_ds */
1412 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1413 jobject this, jobject o_name, jobject o_config)
1414 {
1415 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1416 } /* }}} jint cjni_api_register_config */
1418 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1419 jobject this, jobject o_name, jobject o_config)
1420 {
1421 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1422 } /* }}} jint cjni_api_register_init */
1424 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1425 jobject this, jobject o_name, jobject o_read)
1426 {
1427 user_data_t ud;
1428 cjni_callback_info_t *cbi;
1430 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1431 if (cbi == NULL)
1432 return (-1);
1434 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1436 memset (&ud, 0, sizeof (ud));
1437 ud.data = (void *) cbi;
1438 ud.free_func = cjni_callback_info_destroy;
1440 plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read,
1441 /* interval = */ 0, &ud);
1443 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1445 return (0);
1446 } /* }}} jint cjni_api_register_read */
1448 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1449 jobject this, jobject o_name, jobject o_write)
1450 {
1451 user_data_t ud;
1452 cjni_callback_info_t *cbi;
1454 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1455 if (cbi == NULL)
1456 return (-1);
1458 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1460 memset (&ud, 0, sizeof (ud));
1461 ud.data = (void *) cbi;
1462 ud.free_func = cjni_callback_info_destroy;
1464 plugin_register_write (cbi->name, cjni_write, &ud);
1466 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1468 return (0);
1469 } /* }}} jint cjni_api_register_write */
1471 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1472 jobject this, jobject o_name, jobject o_flush)
1473 {
1474 user_data_t ud;
1475 cjni_callback_info_t *cbi;
1477 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1478 if (cbi == NULL)
1479 return (-1);
1481 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1483 memset (&ud, 0, sizeof (ud));
1484 ud.data = (void *) cbi;
1485 ud.free_func = cjni_callback_info_destroy;
1487 plugin_register_flush (cbi->name, cjni_flush, &ud);
1489 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1491 return (0);
1492 } /* }}} jint cjni_api_register_flush */
1494 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1495 jobject this, jobject o_name, jobject o_shutdown)
1496 {
1497 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1498 CB_TYPE_SHUTDOWN));
1499 } /* }}} jint cjni_api_register_shutdown */
1501 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1502 jobject this, jobject o_name, jobject o_log)
1503 {
1504 user_data_t ud;
1505 cjni_callback_info_t *cbi;
1507 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1508 if (cbi == NULL)
1509 return (-1);
1511 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1513 memset (&ud, 0, sizeof (ud));
1514 ud.data = (void *) cbi;
1515 ud.free_func = cjni_callback_info_destroy;
1517 plugin_register_log (cbi->name, cjni_log, &ud);
1519 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1521 return (0);
1522 } /* }}} jint cjni_api_register_log */
1524 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1525 jobject this, jobject o_name, jobject o_notification)
1526 {
1527 user_data_t ud;
1528 cjni_callback_info_t *cbi;
1530 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1531 CB_TYPE_NOTIFICATION);
1532 if (cbi == NULL)
1533 return (-1);
1535 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1537 memset (&ud, 0, sizeof (ud));
1538 ud.data = (void *) cbi;
1539 ud.free_func = cjni_callback_info_destroy;
1541 plugin_register_notification (cbi->name, cjni_notification, &ud);
1543 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1545 return (0);
1546 } /* }}} jint cjni_api_register_notification */
1548 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1549 jobject this, jobject o_name, jobject o_match, int type)
1550 {
1551 int status;
1552 const char *c_name;
1554 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1555 if (c_name == NULL)
1556 {
1557 ERROR ("java plugin: cjni_api_register_match_target: "
1558 "GetStringUTFChars failed.");
1559 return (-1);
1560 }
1562 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1563 if (status != 0)
1564 {
1565 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1566 return (-1);
1567 }
1569 if (type == CB_TYPE_MATCH)
1570 {
1571 match_proc_t m_proc;
1573 memset (&m_proc, 0, sizeof (m_proc));
1574 m_proc.create = cjni_match_target_create;
1575 m_proc.destroy = cjni_match_target_destroy;
1576 m_proc.match = (void *) cjni_match_target_invoke;
1578 status = fc_register_match (c_name, m_proc);
1579 }
1580 else if (type == CB_TYPE_TARGET)
1581 {
1582 target_proc_t t_proc;
1584 memset (&t_proc, 0, sizeof (t_proc));
1585 t_proc.create = cjni_match_target_create;
1586 t_proc.destroy = cjni_match_target_destroy;
1587 t_proc.invoke = cjni_match_target_invoke;
1589 status = fc_register_target (c_name, t_proc);
1590 }
1591 else
1592 {
1593 ERROR ("java plugin: cjni_api_register_match_target: "
1594 "Don't know whether to create a match or a target.");
1595 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1596 return (-1);
1597 }
1599 if (status != 0)
1600 {
1601 ERROR ("java plugin: cjni_api_register_match_target: "
1602 "%s failed.",
1603 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1604 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1605 return (-1);
1606 }
1608 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1610 return (0);
1611 } /* }}} jint cjni_api_register_match_target */
1613 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1614 jobject this, jobject o_name, jobject o_match)
1615 {
1616 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1617 CB_TYPE_MATCH));
1618 } /* }}} jint cjni_api_register_match */
1620 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1621 jobject this, jobject o_name, jobject o_target)
1622 {
1623 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1624 CB_TYPE_TARGET));
1625 } /* }}} jint cjni_api_register_target */
1627 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1628 jobject this, jint severity, jobject o_message)
1629 {
1630 const char *c_str;
1632 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1633 if (c_str == NULL)
1634 {
1635 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1636 return;
1637 }
1639 if (severity < LOG_ERR)
1640 severity = LOG_ERR;
1641 if (severity > LOG_DEBUG)
1642 severity = LOG_DEBUG;
1644 plugin_log (severity, "%s", c_str);
1646 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1647 } /* }}} void cjni_api_log */
1649 static jstring JNICALL cjni_api_get_hostname (JNIEnv *jvm_env, jobject this)
1650 {
1651 return ctoj_output_string(jvm_env, hostname_g);
1652 }
1654 /* List of ``native'' functions, i. e. C-functions that can be called from
1655 * Java. */
1656 static JNINativeMethod jni_api_functions[] = /* {{{ */
1657 {
1658 { "dispatchValues",
1659 "(Lorg/collectd/api/ValueList;)I",
1660 cjni_api_dispatch_values },
1662 { "dispatchNotification",
1663 "(Lorg/collectd/api/Notification;)I",
1664 cjni_api_dispatch_notification },
1666 { "getDS",
1667 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1668 cjni_api_get_ds },
1670 { "registerConfig",
1671 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1672 cjni_api_register_config },
1674 { "registerInit",
1675 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1676 cjni_api_register_init },
1678 { "registerRead",
1679 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1680 cjni_api_register_read },
1682 { "registerWrite",
1683 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1684 cjni_api_register_write },
1686 { "registerFlush",
1687 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1688 cjni_api_register_flush },
1690 { "registerShutdown",
1691 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1692 cjni_api_register_shutdown },
1694 { "registerLog",
1695 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1696 cjni_api_register_log },
1698 { "registerNotification",
1699 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1700 cjni_api_register_notification },
1702 { "registerMatch",
1703 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1704 cjni_api_register_match },
1706 { "registerTarget",
1707 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1708 cjni_api_register_target },
1710 { "log",
1711 "(ILjava/lang/String;)V",
1712 cjni_api_log },
1714 { "getHostname",
1715 "()Ljava/lang/String;",
1716 cjni_api_get_hostname },
1718 };
1719 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1720 / sizeof (jni_api_functions[0]);
1721 /* }}} */
1723 /*
1724 * Functions
1725 */
1726 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1727 * all registration functions. */
1728 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1729 jobject o_name, jobject o_callback, int type)
1730 {
1731 const char *c_name;
1732 cjni_callback_info_t *cbi;
1733 const char *method_name;
1734 const char *method_signature;
1736 switch (type)
1737 {
1738 case CB_TYPE_CONFIG:
1739 method_name = "config";
1740 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1741 break;
1743 case CB_TYPE_INIT:
1744 method_name = "init";
1745 method_signature = "()I";
1746 break;
1748 case CB_TYPE_READ:
1749 method_name = "read";
1750 method_signature = "()I";
1751 break;
1753 case CB_TYPE_WRITE:
1754 method_name = "write";
1755 method_signature = "(Lorg/collectd/api/ValueList;)I";
1756 break;
1758 case CB_TYPE_FLUSH:
1759 method_name = "flush";
1760 method_signature = "(Ljava/lang/Number;Ljava/lang/String;)I";
1761 break;
1763 case CB_TYPE_SHUTDOWN:
1764 method_name = "shutdown";
1765 method_signature = "()I";
1766 break;
1768 case CB_TYPE_LOG:
1769 method_name = "log";
1770 method_signature = "(ILjava/lang/String;)V";
1771 break;
1773 case CB_TYPE_NOTIFICATION:
1774 method_name = "notification";
1775 method_signature = "(Lorg/collectd/api/Notification;)I";
1776 break;
1778 case CB_TYPE_MATCH:
1779 method_name = "createMatch";
1780 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1781 "Lorg/collectd/api/CollectdMatchInterface;";
1782 break;
1784 case CB_TYPE_TARGET:
1785 method_name = "createTarget";
1786 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1787 "Lorg/collectd/api/CollectdTargetInterface;";
1788 break;
1790 default:
1791 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1792 type);
1793 return (NULL);
1794 }
1796 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1797 if (c_name == NULL)
1798 {
1799 ERROR ("java plugin: cjni_callback_info_create: "
1800 "GetStringUTFChars failed.");
1801 return (NULL);
1802 }
1804 cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1805 if (cbi == NULL)
1806 {
1807 ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1808 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1809 return (NULL);
1810 }
1811 memset (cbi, 0, sizeof (*cbi));
1812 cbi->type = type;
1814 cbi->name = strdup (c_name);
1815 if (cbi->name == NULL)
1816 {
1817 pthread_mutex_unlock (&java_callbacks_lock);
1818 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1819 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1820 return (NULL);
1821 }
1823 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1825 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1826 if (cbi->object == NULL)
1827 {
1828 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1829 free (cbi);
1830 return (NULL);
1831 }
1833 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1834 if (cbi->class == NULL)
1835 {
1836 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1837 free (cbi);
1838 return (NULL);
1839 }
1841 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1842 method_name, method_signature);
1843 if (cbi->method == NULL)
1844 {
1845 ERROR ("java plugin: cjni_callback_info_create: "
1846 "Cannot find the `%s' method with signature `%s'.",
1847 method_name, method_signature);
1848 free (cbi);
1849 return (NULL);
1850 }
1852 return (cbi);
1853 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1855 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1856 * to the global `java_callbacks' variable. This is used for `config', `init',
1857 * and `shutdown' callbacks. */
1858 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1859 jobject o_name, jobject o_callback, int type)
1860 {
1861 cjni_callback_info_t *cbi;
1862 cjni_callback_info_t *tmp;
1863 #if COLLECT_DEBUG
1864 const char *type_str;
1865 #endif
1867 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1868 if (cbi == NULL)
1869 return (-1);
1871 #if COLLECT_DEBUG
1872 switch (type)
1873 {
1874 case CB_TYPE_CONFIG:
1875 type_str = "config";
1876 break;
1878 case CB_TYPE_INIT:
1879 type_str = "init";
1880 break;
1882 case CB_TYPE_SHUTDOWN:
1883 type_str = "shutdown";
1884 break;
1886 case CB_TYPE_MATCH:
1887 type_str = "match";
1888 break;
1890 case CB_TYPE_TARGET:
1891 type_str = "target";
1892 break;
1894 default:
1895 type_str = "<unknown>";
1896 }
1897 DEBUG ("java plugin: Registering new %s callback: %s",
1898 type_str, cbi->name);
1899 #endif
1901 pthread_mutex_lock (&java_callbacks_lock);
1903 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1904 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1905 if (tmp == NULL)
1906 {
1907 pthread_mutex_unlock (&java_callbacks_lock);
1908 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1910 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1911 free (cbi);
1913 return (-1);
1914 }
1915 java_callbacks = tmp;
1916 java_callbacks[java_callbacks_num] = *cbi;
1917 java_callbacks_num++;
1919 pthread_mutex_unlock (&java_callbacks_lock);
1921 free (cbi);
1922 return (0);
1923 } /* }}} int cjni_callback_register */
1925 /* Callback for `pthread_key_create'. It frees the data contained in
1926 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1927 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1928 {
1929 cjni_jvm_env_t *cjni_env;
1931 if (args == NULL)
1932 return;
1934 cjni_env = (cjni_jvm_env_t *) args;
1936 if (cjni_env->reference_counter > 0)
1937 {
1938 ERROR ("java plugin: cjni_jvm_env_destroy: "
1939 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1940 }
1942 if (cjni_env->jvm_env != NULL)
1943 {
1944 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1945 (void *) cjni_env->jvm_env);
1946 }
1948 /* The pointer is allocated in `cjni_thread_attach' */
1949 free (cjni_env);
1950 } /* }}} void cjni_jvm_env_destroy */
1952 /* Register ``native'' functions with the JVM. Native functions are C-functions
1953 * that can be called by Java code. */
1954 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1955 {
1956 jclass api_class_ptr;
1957 int status;
1959 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1960 if (api_class_ptr == NULL)
1961 {
1962 ERROR ("cjni_init_native: Cannot find the API class \"org.collectd.api"
1963 ".Collectd\". Please set the correct class path "
1964 "using 'JVMArg \"-Djava.class.path=...\"'.");
1965 return (-1);
1966 }
1968 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1969 jni_api_functions, (jint) jni_api_functions_num);
1970 if (status != 0)
1971 {
1972 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1973 return (-1);
1974 }
1976 return (0);
1977 } /* }}} int cjni_init_native */
1979 /* Create the JVM. This is called when the first thread tries to access the JVM
1980 * via cjni_thread_attach. */
1981 static int cjni_create_jvm (void) /* {{{ */
1982 {
1983 JNIEnv *jvm_env;
1984 JavaVMInitArgs vm_args;
1985 JavaVMOption vm_options[jvm_argc];
1987 int status;
1988 size_t i;
1990 if (jvm != NULL)
1991 return (0);
1993 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1994 if (status != 0)
1995 {
1996 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1997 "with status %i.", status);
1998 return (-1);
1999 }
2001 jvm_env = NULL;
2003 memset (&vm_args, 0, sizeof (vm_args));
2004 vm_args.version = JNI_VERSION_1_2;
2005 vm_args.options = vm_options;
2006 vm_args.nOptions = (jint) jvm_argc;
2008 for (i = 0; i < jvm_argc; i++)
2009 {
2010 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
2011 i, jvm_argv[i]);
2012 vm_args.options[i].optionString = jvm_argv[i];
2013 }
2015 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
2016 if (status != 0)
2017 {
2018 ERROR ("java plugin: cjni_create_jvm: "
2019 "JNI_CreateJavaVM failed with status %i.",
2020 status);
2021 return (-1);
2022 }
2023 assert (jvm != NULL);
2024 assert (jvm_env != NULL);
2026 /* Call RegisterNatives */
2027 status = cjni_init_native (jvm_env);
2028 if (status != 0)
2029 {
2030 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
2031 return (-1);
2032 }
2034 DEBUG ("java plugin: The JVM has been created.");
2035 return (0);
2036 } /* }}} int cjni_create_jvm */
2038 /* Increase the reference counter to the JVM for this thread. If it was zero,
2039 * attach the JVM first. */
2040 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2041 {
2042 cjni_jvm_env_t *cjni_env;
2043 JNIEnv *jvm_env;
2045 /* If we're the first thread to access the JVM, we'll have to create it
2046 * first.. */
2047 if (jvm == NULL)
2048 {
2049 int status;
2051 status = cjni_create_jvm ();
2052 if (status != 0)
2053 {
2054 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2055 return (NULL);
2056 }
2057 }
2058 assert (jvm != NULL);
2060 cjni_env = pthread_getspecific (jvm_env_key);
2061 if (cjni_env == NULL)
2062 {
2063 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2064 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
2065 if (cjni_env == NULL)
2066 {
2067 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
2068 return (NULL);
2069 }
2070 memset (cjni_env, 0, sizeof (*cjni_env));
2071 cjni_env->reference_counter = 0;
2072 cjni_env->jvm_env = NULL;
2074 pthread_setspecific (jvm_env_key, cjni_env);
2075 }
2077 if (cjni_env->reference_counter > 0)
2078 {
2079 cjni_env->reference_counter++;
2080 jvm_env = cjni_env->jvm_env;
2081 }
2082 else
2083 {
2084 int status;
2085 JavaVMAttachArgs args;
2087 assert (cjni_env->jvm_env == NULL);
2089 memset (&args, 0, sizeof (args));
2090 args.version = JNI_VERSION_1_2;
2092 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2093 if (status != 0)
2094 {
2095 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2096 "with status %i.", status);
2097 return (NULL);
2098 }
2100 cjni_env->reference_counter = 1;
2101 cjni_env->jvm_env = jvm_env;
2102 }
2104 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2105 cjni_env->reference_counter);
2106 assert (jvm_env != NULL);
2107 return (jvm_env);
2108 } /* }}} JNIEnv *cjni_thread_attach */
2110 /* Decrease the reference counter of this thread. If it reaches zero, detach
2111 * from the JVM. */
2112 static int cjni_thread_detach (void) /* {{{ */
2113 {
2114 cjni_jvm_env_t *cjni_env;
2115 int status;
2117 cjni_env = pthread_getspecific (jvm_env_key);
2118 if (cjni_env == NULL)
2119 {
2120 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2121 return (-1);
2122 }
2124 assert (cjni_env->reference_counter > 0);
2125 assert (cjni_env->jvm_env != NULL);
2127 cjni_env->reference_counter--;
2128 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2129 cjni_env->reference_counter);
2131 if (cjni_env->reference_counter > 0)
2132 return (0);
2134 status = (*jvm)->DetachCurrentThread (jvm);
2135 if (status != 0)
2136 {
2137 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2138 "with status %i.", status);
2139 }
2141 cjni_env->reference_counter = 0;
2142 cjni_env->jvm_env = NULL;
2144 return (0);
2145 } /* }}} int cjni_thread_detach */
2147 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2148 {
2149 char **tmp;
2151 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2152 {
2153 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2154 return (-1);
2155 }
2157 if (jvm != NULL)
2158 {
2159 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2160 "`LoadPlugin' options! The JVM is already started and I have to "
2161 "ignore this argument: %s",
2162 ci->values[0].value.string);
2163 return (-1);
2164 }
2166 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2167 if (tmp == NULL)
2168 {
2169 ERROR ("java plugin: realloc failed.");
2170 return (-1);
2171 }
2172 jvm_argv = tmp;
2174 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2175 if (jvm_argv[jvm_argc] == NULL)
2176 {
2177 ERROR ("java plugin: strdup failed.");
2178 return (-1);
2179 }
2180 jvm_argc++;
2182 return (0);
2183 } /* }}} int cjni_config_add_jvm_arg */
2185 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2186 {
2187 JNIEnv *jvm_env;
2188 java_plugin_class_t *class;
2189 jmethodID constructor_id;
2190 jobject tmp_object;
2192 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2193 {
2194 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2195 return (-1);
2196 }
2198 jvm_env = cjni_thread_attach ();
2199 if (jvm_env == NULL)
2200 return (-1);
2202 class = (java_plugin_class_t *) realloc (java_classes_list,
2203 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2204 if (class == NULL)
2205 {
2206 ERROR ("java plugin: realloc failed.");
2207 cjni_thread_detach ();
2208 return (-1);
2209 }
2210 java_classes_list = class;
2211 class = java_classes_list + java_classes_list_len;
2213 memset (class, 0, sizeof (*class));
2214 class->name = strdup (ci->values[0].value.string);
2215 if (class->name == NULL)
2216 {
2217 ERROR ("java plugin: strdup failed.");
2218 cjni_thread_detach ();
2219 return (-1);
2220 }
2221 class->class = NULL;
2222 class->object = NULL;
2224 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2225 thorough the Java community, but (Sun's) `FindClass' and friends need
2226 slashes. */
2227 size_t i;
2228 for (i = 0; class->name[i] != 0; i++)
2229 if (class->name[i] == '.')
2230 class->name[i] = '/';
2231 }
2233 DEBUG ("java plugin: Loading class %s", class->name);
2235 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2236 if (class->class == NULL)
2237 {
2238 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2239 class->name);
2240 cjni_thread_detach ();
2241 free (class->name);
2242 return (-1);
2243 }
2245 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2246 "<init>", "()V");
2247 if (constructor_id == NULL)
2248 {
2249 ERROR ("java plugin: cjni_config_load_plugin: "
2250 "Could not find the constructor for `%s'.",
2251 class->name);
2252 cjni_thread_detach ();
2253 free (class->name);
2254 return (-1);
2255 }
2257 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2258 constructor_id);
2259 if (tmp_object != NULL)
2260 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2261 else
2262 class->object = NULL;
2263 if (class->object == NULL)
2264 {
2265 ERROR ("java plugin: cjni_config_load_plugin: "
2266 "Could create a new `%s' object.",
2267 class->name);
2268 cjni_thread_detach ();
2269 free (class->name);
2270 return (-1);
2271 }
2273 cjni_thread_detach ();
2275 java_classes_list_len++;
2277 return (0);
2278 } /* }}} int cjni_config_load_plugin */
2280 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2281 {
2282 JNIEnv *jvm_env;
2283 cjni_callback_info_t *cbi;
2284 jobject o_ocitem;
2285 const char *name;
2286 size_t i;
2288 jclass class;
2289 jmethodID method;
2291 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2292 {
2293 WARNING ("java plugin: `Plugin' blocks "
2294 "need exactly one string argument.");
2295 return (-1);
2296 }
2298 name = ci->values[0].value.string;
2300 cbi = NULL;
2301 for (i = 0; i < java_callbacks_num; i++)
2302 {
2303 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2304 continue;
2306 if (strcmp (name, java_callbacks[i].name) != 0)
2307 continue;
2309 cbi = java_callbacks + i;
2310 break;
2311 }
2313 if (cbi == NULL)
2314 {
2315 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2316 "configuration callback has been registered. Please make sure, the "
2317 "`LoadPlugin' lines precede the `Plugin' blocks.",
2318 name);
2319 return (0);
2320 }
2322 DEBUG ("java plugin: Configuring %s", name);
2324 jvm_env = cjni_thread_attach ();
2325 if (jvm_env == NULL)
2326 return (-1);
2328 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2329 if (o_ocitem == NULL)
2330 {
2331 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2332 cjni_thread_detach ();
2333 return (-1);
2334 }
2336 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2337 method = (*jvm_env)->GetMethodID (jvm_env, class,
2338 "config", "(Lorg/collectd/api/OConfigItem;)I");
2340 (*jvm_env)->CallIntMethod (jvm_env,
2341 cbi->object, method, o_ocitem);
2343 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2344 cjni_thread_detach ();
2345 return (0);
2346 } /* }}} int cjni_config_plugin_block */
2348 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2349 {
2350 int success;
2351 int errors;
2352 int status;
2353 int i;
2355 success = 0;
2356 errors = 0;
2358 for (i = 0; i < ci->children_num; i++)
2359 {
2360 oconfig_item_t *child = ci->children + i;
2362 if (strcasecmp ("JVMArg", child->key) == 0)
2363 {
2364 status = cjni_config_add_jvm_arg (child);
2365 if (status == 0)
2366 success++;
2367 else
2368 errors++;
2369 }
2370 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2371 {
2372 status = cjni_config_load_plugin (child);
2373 if (status == 0)
2374 success++;
2375 else
2376 errors++;
2377 }
2378 else if (strcasecmp ("Plugin", child->key) == 0)
2379 {
2380 status = cjni_config_plugin_block (child);
2381 if (status == 0)
2382 success++;
2383 else
2384 errors++;
2385 }
2386 else
2387 {
2388 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2389 errors++;
2390 }
2391 }
2393 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2394 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2396 if ((success == 0) && (errors > 0))
2397 {
2398 ERROR ("java plugin: All statements failed.");
2399 return (-1);
2400 }
2402 return (0);
2403 } /* }}} int cjni_config_perform */
2405 /* Copy the children of `ci' to the global `config_block' variable. */
2406 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2407 {
2408 oconfig_item_t *ci_copy;
2409 oconfig_item_t *tmp;
2411 assert (ci != NULL);
2412 if (ci->children_num == 0)
2413 return (0); /* nothing to do */
2415 ci_copy = oconfig_clone (ci);
2416 if (ci_copy == NULL)
2417 {
2418 ERROR ("java plugin: oconfig_clone failed.");
2419 return (-1);
2420 }
2422 if (config_block == NULL)
2423 {
2424 config_block = ci_copy;
2425 return (0);
2426 }
2428 tmp = realloc (config_block->children,
2429 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2430 if (tmp == NULL)
2431 {
2432 ERROR ("java plugin: realloc failed.");
2433 oconfig_free (ci_copy);
2434 return (-1);
2435 }
2436 config_block->children = tmp;
2438 /* Copy the pointers */
2439 memcpy (config_block->children + config_block->children_num,
2440 ci_copy->children,
2441 ci_copy->children_num * sizeof (*ci_copy->children));
2442 config_block->children_num += ci_copy->children_num;
2444 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2445 memset (ci_copy->children, 0,
2446 ci_copy->children_num * sizeof (*ci_copy->children));
2447 ci_copy->children_num = 0;
2449 oconfig_free (ci_copy);
2451 return (0);
2452 } /* }}} int cjni_config_callback */
2454 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2455 * and `cjni_write'. In particular, delete the global reference to the Java
2456 * object. */
2457 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2458 {
2459 JNIEnv *jvm_env;
2460 cjni_callback_info_t *cbi;
2462 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2464 cbi = (cjni_callback_info_t *) arg;
2466 /* This condition can occur when shutting down. */
2467 if (jvm == NULL)
2468 {
2469 sfree (cbi);
2470 return;
2471 }
2473 if (arg == NULL)
2474 return;
2476 jvm_env = cjni_thread_attach ();
2477 if (jvm_env == NULL)
2478 {
2479 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2480 return;
2481 }
2483 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2485 cbi->method = NULL;
2486 cbi->object = NULL;
2487 cbi->class = NULL;
2488 free (cbi);
2490 cjni_thread_detach ();
2491 } /* }}} void cjni_callback_info_destroy */
2493 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2494 static int cjni_read (user_data_t *ud) /* {{{ */
2495 {
2496 JNIEnv *jvm_env;
2497 cjni_callback_info_t *cbi;
2498 int ret_status;
2500 if (jvm == NULL)
2501 {
2502 ERROR ("java plugin: cjni_read: jvm == NULL");
2503 return (-1);
2504 }
2506 if ((ud == NULL) || (ud->data == NULL))
2507 {
2508 ERROR ("java plugin: cjni_read: Invalid user data.");
2509 return (-1);
2510 }
2512 jvm_env = cjni_thread_attach ();
2513 if (jvm_env == NULL)
2514 return (-1);
2516 cbi = (cjni_callback_info_t *) ud->data;
2518 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2519 cbi->method);
2521 cjni_thread_detach ();
2522 return (ret_status);
2523 } /* }}} int cjni_read */
2525 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2526 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2527 user_data_t *ud)
2528 {
2529 JNIEnv *jvm_env;
2530 cjni_callback_info_t *cbi;
2531 jobject vl_java;
2532 int ret_status;
2534 if (jvm == NULL)
2535 {
2536 ERROR ("java plugin: cjni_write: jvm == NULL");
2537 return (-1);
2538 }
2540 if ((ud == NULL) || (ud->data == NULL))
2541 {
2542 ERROR ("java plugin: cjni_write: Invalid user data.");
2543 return (-1);
2544 }
2546 jvm_env = cjni_thread_attach ();
2547 if (jvm_env == NULL)
2548 return (-1);
2550 cbi = (cjni_callback_info_t *) ud->data;
2552 vl_java = ctoj_value_list (jvm_env, ds, vl);
2553 if (vl_java == NULL)
2554 {
2555 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2556 cjni_thread_detach ();
2557 return (-1);
2558 }
2560 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2561 cbi->object, cbi->method, vl_java);
2563 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2565 cjni_thread_detach ();
2566 return (ret_status);
2567 } /* }}} int cjni_write */
2569 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2570 static int cjni_flush (cdtime_t timeout, const char *identifier, /* {{{ */
2571 user_data_t *ud)
2572 {
2573 JNIEnv *jvm_env;
2574 cjni_callback_info_t *cbi;
2575 jobject o_timeout;
2576 jobject o_identifier;
2577 int ret_status;
2579 if (jvm == NULL)
2580 {
2581 ERROR ("java plugin: cjni_flush: jvm == NULL");
2582 return (-1);
2583 }
2585 if ((ud == NULL) || (ud->data == NULL))
2586 {
2587 ERROR ("java plugin: cjni_flush: Invalid user data.");
2588 return (-1);
2589 }
2591 jvm_env = cjni_thread_attach ();
2592 if (jvm_env == NULL)
2593 return (-1);
2595 cbi = (cjni_callback_info_t *) ud->data;
2597 o_timeout = ctoj_jdouble_to_number (jvm_env,
2598 (jdouble) CDTIME_T_TO_DOUBLE (timeout));
2599 if (o_timeout == NULL)
2600 {
2601 ERROR ("java plugin: cjni_flush: Converting double "
2602 "to Number object failed.");
2603 cjni_thread_detach ();
2604 return (-1);
2605 }
2607 o_identifier = NULL;
2608 if (identifier != NULL)
2609 {
2610 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2611 if (o_identifier == NULL)
2612 {
2613 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2614 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2615 cjni_thread_detach ();
2616 return (-1);
2617 }
2618 }
2620 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2621 cbi->object, cbi->method, o_timeout, o_identifier);
2623 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2624 (*jvm_env)->DeleteLocalRef (jvm_env, o_timeout);
2626 cjni_thread_detach ();
2627 return (ret_status);
2628 } /* }}} int cjni_flush */
2630 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2631 static void cjni_log (int severity, const char *message, /* {{{ */
2632 user_data_t *ud)
2633 {
2634 JNIEnv *jvm_env;
2635 cjni_callback_info_t *cbi;
2636 jobject o_message;
2638 if (jvm == NULL)
2639 return;
2641 if ((ud == NULL) || (ud->data == NULL))
2642 return;
2644 jvm_env = cjni_thread_attach ();
2645 if (jvm_env == NULL)
2646 return;
2648 cbi = (cjni_callback_info_t *) ud->data;
2650 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2651 if (o_message == NULL)
2652 {
2653 cjni_thread_detach ();
2654 return;
2655 }
2657 (*jvm_env)->CallVoidMethod (jvm_env,
2658 cbi->object, cbi->method, (jint) severity, o_message);
2660 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2662 cjni_thread_detach ();
2663 } /* }}} void cjni_log */
2665 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2666 * pointer. */
2667 static int cjni_notification (const notification_t *n, /* {{{ */
2668 user_data_t *ud)
2669 {
2670 JNIEnv *jvm_env;
2671 cjni_callback_info_t *cbi;
2672 jobject o_notification;
2673 int ret_status;
2675 if (jvm == NULL)
2676 {
2677 ERROR ("java plugin: cjni_read: jvm == NULL");
2678 return (-1);
2679 }
2681 if ((ud == NULL) || (ud->data == NULL))
2682 {
2683 ERROR ("java plugin: cjni_read: Invalid user data.");
2684 return (-1);
2685 }
2687 jvm_env = cjni_thread_attach ();
2688 if (jvm_env == NULL)
2689 return (-1);
2691 cbi = (cjni_callback_info_t *) ud->data;
2693 o_notification = ctoj_notification (jvm_env, n);
2694 if (o_notification == NULL)
2695 {
2696 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2697 cjni_thread_detach ();
2698 return (-1);
2699 }
2701 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2702 cbi->object, cbi->method, o_notification);
2704 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2706 cjni_thread_detach ();
2707 return (ret_status);
2708 } /* }}} int cjni_notification */
2710 /* Callbacks for matches implemented in Java */
2711 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2712 void **user_data)
2713 {
2714 JNIEnv *jvm_env;
2715 cjni_callback_info_t *cbi_ret;
2716 cjni_callback_info_t *cbi_factory;
2717 const char *name;
2718 jobject o_ci;
2719 jobject o_tmp;
2720 int type;
2721 size_t i;
2723 cbi_ret = NULL;
2724 o_ci = NULL;
2725 jvm_env = NULL;
2727 #define BAIL_OUT(status) \
2728 if (cbi_ret != NULL) { \
2729 free (cbi_ret->name); \
2730 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2731 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2732 } \
2733 free (cbi_ret); \
2734 if (o_ci != NULL) \
2735 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2736 cjni_thread_detach (); \
2737 return (status)
2739 if (jvm == NULL)
2740 {
2741 ERROR ("java plugin: cjni_read: jvm == NULL");
2742 return (-1);
2743 }
2745 jvm_env = cjni_thread_attach ();
2746 if (jvm_env == NULL)
2747 return (-1);
2749 /* Find out whether to create a match or a target. */
2750 if (strcasecmp ("Match", ci->key) == 0)
2751 type = CB_TYPE_MATCH;
2752 else if (strcasecmp ("Target", ci->key) == 0)
2753 type = CB_TYPE_TARGET;
2754 else
2755 {
2756 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2757 "to create a match or a target.");
2758 BAIL_OUT (-1);
2759 }
2761 /* This is the name of the match we should create. */
2762 name = ci->values[0].value.string;
2764 /* Lets see if we have a matching factory here.. */
2765 cbi_factory = NULL;
2766 for (i = 0; i < java_callbacks_num; i++)
2767 {
2768 if (java_callbacks[i].type != type)
2769 continue;
2771 if (strcmp (name, java_callbacks[i].name) != 0)
2772 continue;
2774 cbi_factory = java_callbacks + i;
2775 break;
2776 }
2778 /* Nope, no factory for that name.. */
2779 if (cbi_factory == NULL)
2780 {
2781 ERROR ("java plugin: cjni_match_target_create: "
2782 "No such match factory registered: %s",
2783 name);
2784 BAIL_OUT (-1);
2785 }
2787 /* We convert `ci' to its Java equivalent.. */
2788 o_ci = ctoj_oconfig_item (jvm_env, ci);
2789 if (o_ci == NULL)
2790 {
2791 ERROR ("java plugin: cjni_match_target_create: "
2792 "ctoj_oconfig_item failed.");
2793 BAIL_OUT (-1);
2794 }
2796 /* Allocate a new callback info structure. This is going to be our user_data
2797 * pointer. */
2798 cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
2799 if (cbi_ret == NULL)
2800 {
2801 ERROR ("java plugin: cjni_match_target_create: malloc failed.");
2802 BAIL_OUT (-1);
2803 }
2804 memset (cbi_ret, 0, sizeof (*cbi_ret));
2805 cbi_ret->object = NULL;
2806 cbi_ret->type = type;
2808 /* Lets fill the callback info structure.. First, the name: */
2809 cbi_ret->name = strdup (name);
2810 if (cbi_ret->name == NULL)
2811 {
2812 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2813 BAIL_OUT (-1);
2814 }
2816 /* Then call the factory method so it creates a new object for us. */
2817 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2818 cbi_factory->object, cbi_factory->method, o_ci);
2819 if (o_tmp == NULL)
2820 {
2821 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2822 BAIL_OUT (-1);
2823 }
2825 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2826 if (o_tmp == NULL)
2827 {
2828 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2829 BAIL_OUT (-1);
2830 }
2832 /* This is the class of the match. It is possibly different from the class of
2833 * the match-factory! */
2834 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2835 if (cbi_ret->class == NULL)
2836 {
2837 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2838 BAIL_OUT (-1);
2839 }
2841 /* Lookup the `int match (DataSet, ValueList)' method. */
2842 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2843 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2844 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2845 if (cbi_ret->method == NULL)
2846 {
2847 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2848 BAIL_OUT (-1);
2849 }
2851 /* Return the newly created match via the user_data pointer. */
2852 *user_data = (void *) cbi_ret;
2854 cjni_thread_detach ();
2856 DEBUG ("java plugin: cjni_match_target_create: "
2857 "Successfully created a `%s' %s.",
2858 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2860 /* Success! */
2861 return (0);
2862 #undef BAIL_OUT
2863 } /* }}} int cjni_match_target_create */
2865 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2866 {
2867 cjni_callback_info_destroy (*user_data);
2868 *user_data = NULL;
2870 return (0);
2871 } /* }}} int cjni_match_target_destroy */
2873 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2874 value_list_t *vl, notification_meta_t **meta, void **user_data)
2875 {
2876 JNIEnv *jvm_env;
2877 cjni_callback_info_t *cbi;
2878 jobject o_vl;
2879 jobject o_ds;
2880 int ret_status;
2881 int status;
2883 if (jvm == NULL)
2884 {
2885 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2886 return (-1);
2887 }
2889 jvm_env = cjni_thread_attach ();
2890 if (jvm_env == NULL)
2891 return (-1);
2893 cbi = (cjni_callback_info_t *) *user_data;
2895 o_vl = ctoj_value_list (jvm_env, ds, vl);
2896 if (o_vl == NULL)
2897 {
2898 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2899 cjni_thread_detach ();
2900 return (-1);
2901 }
2903 o_ds = ctoj_data_set (jvm_env, ds);
2904 if (o_ds == NULL)
2905 {
2906 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2907 cjni_thread_detach ();
2908 return (-1);
2909 }
2911 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2912 o_ds, o_vl);
2914 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2916 /* If we're executing a target, copy the `ValueList' back to our
2917 * `value_list_t'. */
2918 if (cbi->type == CB_TYPE_TARGET)
2919 {
2920 value_list_t new_vl;
2922 memset (&new_vl, 0, sizeof (new_vl));
2923 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2924 if (status != 0)
2925 {
2926 ERROR ("java plugin: cjni_match_target_invoke: "
2927 "jtoc_value_list failed.");
2928 }
2929 else /* if (status == 0) */
2930 {
2931 /* plugin_dispatch_values assures that this is dynamically allocated
2932 * memory. */
2933 sfree (vl->values);
2935 /* This will replace the vl->values pointer to a new, dynamically
2936 * allocated piece of memory. */
2937 memcpy (vl, &new_vl, sizeof (*vl));
2938 }
2939 } /* if (cbi->type == CB_TYPE_TARGET) */
2941 cjni_thread_detach ();
2942 return (ret_status);
2943 } /* }}} int cjni_match_target_invoke */
2945 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2946 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2947 {
2948 int status;
2949 size_t i;
2951 for (i = 0; i < java_callbacks_num; i++)
2952 {
2953 if (java_callbacks[i].type != CB_TYPE_INIT)
2954 continue;
2956 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2958 status = (*jvm_env)->CallIntMethod (jvm_env,
2959 java_callbacks[i].object, java_callbacks[i].method);
2960 if (status != 0)
2961 {
2962 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2963 "Removing read function.",
2964 java_callbacks[i].name, status);
2965 plugin_unregister_read (java_callbacks[i].name);
2966 }
2967 }
2969 return (0);
2970 } /* }}} int cjni_init_plugins */
2972 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2973 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2974 {
2975 int status;
2976 size_t i;
2978 for (i = 0; i < java_callbacks_num; i++)
2979 {
2980 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2981 continue;
2983 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2985 status = (*jvm_env)->CallIntMethod (jvm_env,
2986 java_callbacks[i].object, java_callbacks[i].method);
2987 if (status != 0)
2988 {
2989 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2990 java_callbacks[i].name, status);
2991 }
2992 }
2994 return (0);
2995 } /* }}} int cjni_shutdown_plugins */
2998 static int cjni_shutdown (void) /* {{{ */
2999 {
3000 JNIEnv *jvm_env;
3001 JavaVMAttachArgs args;
3002 int status;
3003 size_t i;
3005 if (jvm == NULL)
3006 return (0);
3008 jvm_env = NULL;
3009 memset (&args, 0, sizeof (args));
3010 args.version = JNI_VERSION_1_2;
3012 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
3013 if (status != 0)
3014 {
3015 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
3016 status);
3017 return (-1);
3018 }
3020 /* Execute all the shutdown functions registered by plugins. */
3021 cjni_shutdown_plugins (jvm_env);
3023 /* Release all the global references to callback functions */
3024 for (i = 0; i < java_callbacks_num; i++)
3025 {
3026 if (java_callbacks[i].object != NULL)
3027 {
3028 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3029 java_callbacks[i].object = NULL;
3030 }
3031 sfree (java_callbacks[i].name);
3032 }
3033 java_callbacks_num = 0;
3034 sfree (java_callbacks);
3036 /* Release all the global references to directly loaded classes. */
3037 for (i = 0; i < java_classes_list_len; i++)
3038 {
3039 if (java_classes_list[i].object != NULL)
3040 {
3041 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3042 java_classes_list[i].object = NULL;
3043 }
3044 sfree (java_classes_list[i].name);
3045 }
3046 java_classes_list_len = 0;
3047 sfree (java_classes_list);
3049 /* Destroy the JVM */
3050 DEBUG ("java plugin: Destroying the JVM.");
3051 (*jvm)->DestroyJavaVM (jvm);
3052 jvm = NULL;
3053 jvm_env = NULL;
3055 pthread_key_delete (jvm_env_key);
3057 /* Free the JVM argument list */
3058 for (i = 0; i < jvm_argc; i++)
3059 sfree (jvm_argv[i]);
3060 jvm_argc = 0;
3061 sfree (jvm_argv);
3063 return (0);
3064 } /* }}} int cjni_shutdown */
3066 /* Initialization: Create a JVM, load all configured classes and call their
3067 * `config' and `init' callback methods. */
3068 static int cjni_init (void) /* {{{ */
3069 {
3070 JNIEnv *jvm_env;
3072 if ((config_block == NULL) && (jvm == NULL))
3073 {
3074 ERROR ("java plugin: cjni_init: No configuration block for "
3075 "the java plugin was found.");
3076 return (-1);
3077 }
3079 if (config_block != NULL)
3080 {
3081 cjni_config_perform (config_block);
3082 oconfig_free (config_block);
3083 }
3085 if (jvm == NULL)
3086 {
3087 ERROR ("java plugin: cjni_init: jvm == NULL");
3088 return (-1);
3089 }
3091 jvm_env = cjni_thread_attach ();
3092 if (jvm_env == NULL)
3093 return (-1);
3095 cjni_init_plugins (jvm_env);
3097 cjni_thread_detach ();
3098 return (0);
3099 } /* }}} int cjni_init */
3101 void module_register (void)
3102 {
3103 plugin_register_complex_config ("java", cjni_config_callback);
3104 plugin_register_init ("java", cjni_init);
3105 plugin_register_shutdown ("java", cjni_shutdown);
3106 } /* void module_register (void) */
3108 /* vim: set sw=2 sts=2 et fdm=marker : */