1 /**
2 * collectd - src/java.c
3 * Copyright (C) 2009 Florian octo Forster
4 * Copyright (C) 2008 Justo Alonso Achaques
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 * Justo Alonso Achaques <justo.alonso at gmail.com>
22 **/
24 #include "collectd.h"
25 #include "plugin.h"
26 #include "common.h"
27 #include "filter_chain.h"
29 #include <pthread.h>
30 #include <jni.h>
32 #if !defined(JNI_VERSION_1_2)
33 # error "Need JNI 1.2 compatible interface!"
34 #endif
36 /*
37 * Types
38 */
39 struct cjni_jvm_env_s /* {{{ */
40 {
41 JNIEnv *jvm_env;
42 int reference_counter;
43 };
44 typedef struct cjni_jvm_env_s cjni_jvm_env_t;
45 /* }}} */
47 struct java_plugin_class_s /* {{{ */
48 {
49 char *name;
50 jclass class;
51 jobject object;
52 };
53 typedef struct java_plugin_class_s java_plugin_class_t;
54 /* }}} */
56 #define CB_TYPE_CONFIG 1
57 #define CB_TYPE_INIT 2
58 #define CB_TYPE_READ 3
59 #define CB_TYPE_WRITE 4
60 #define CB_TYPE_FLUSH 5
61 #define CB_TYPE_SHUTDOWN 6
62 #define CB_TYPE_LOG 7
63 #define CB_TYPE_NOTIFICATION 8
64 #define CB_TYPE_MATCH 9
65 #define CB_TYPE_TARGET 10
66 struct cjni_callback_info_s /* {{{ */
67 {
68 char *name;
69 int type;
70 jclass class;
71 jobject object;
72 jmethodID method;
73 };
74 typedef struct cjni_callback_info_s cjni_callback_info_t;
75 /* }}} */
77 /*
78 * Global variables
79 */
80 static JavaVM *jvm = NULL;
81 static pthread_key_t jvm_env_key;
83 /* Configuration options for the JVM. */
84 static char **jvm_argv = NULL;
85 static size_t jvm_argc = 0;
87 /* List of class names to load */
88 static java_plugin_class_t *java_classes_list = NULL;
89 static size_t java_classes_list_len;
91 /* List of config, init, and shutdown callbacks. */
92 static cjni_callback_info_t *java_callbacks = NULL;
93 static size_t java_callbacks_num = 0;
94 static pthread_mutex_t java_callbacks_lock = PTHREAD_MUTEX_INITIALIZER;
96 static oconfig_item_t *config_block = NULL;
98 /*
99 * Prototypes
100 *
101 * Mostly functions that are needed by the Java interface (``native'')
102 * functions.
103 */
104 static void cjni_callback_info_destroy (void *arg);
105 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env,
106 jobject o_name, jobject o_callback, int type);
107 static int cjni_callback_register (JNIEnv *jvm_env, jobject o_name,
108 jobject o_callback, int type);
109 static int cjni_read (user_data_t *user_data);
110 static int cjni_write (const data_set_t *ds, const value_list_t *vl,
111 user_data_t *ud);
112 static int cjni_flush (int timeout, const char *identifier, user_data_t *ud);
113 static void cjni_log (int severity, const char *message, user_data_t *ud);
114 static int cjni_notification (const notification_t *n, user_data_t *ud);
116 /* Create, destroy, and match/invoke functions, used by both, matches AND
117 * targets. */
118 static int cjni_match_target_create (const oconfig_item_t *ci, void **user_data);
119 static int cjni_match_target_destroy (void **user_data);
120 static int cjni_match_target_invoke (const data_set_t *ds, value_list_t *vl,
121 notification_meta_t **meta, void **user_data);
123 /*
124 * C to Java conversion functions
125 */
126 static int ctoj_string (JNIEnv *jvm_env, /* {{{ */
127 const char *string,
128 jclass class_ptr, jobject object_ptr, const char *method_name)
129 {
130 jmethodID m_set;
131 jstring o_string;
133 /* Create a java.lang.String */
134 o_string = (*jvm_env)->NewStringUTF (jvm_env,
135 (string != NULL) ? string : "");
136 if (o_string == NULL)
137 {
138 ERROR ("java plugin: ctoj_string: NewStringUTF failed.");
139 return (-1);
140 }
142 /* Search for the `void setFoo (String s)' method. */
143 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
144 method_name, "(Ljava/lang/String;)V");
145 if (m_set == NULL)
146 {
147 ERROR ("java plugin: ctoj_string: Cannot find method `void %s (String)'.",
148 method_name);
149 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
150 return (-1);
151 }
153 /* Call the method. */
154 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, o_string);
156 /* Decrease reference counter on the java.lang.String object. */
157 (*jvm_env)->DeleteLocalRef (jvm_env, o_string);
159 return (0);
160 } /* }}} int ctoj_string */
162 static int ctoj_int (JNIEnv *jvm_env, /* {{{ */
163 jint value,
164 jclass class_ptr, jobject object_ptr, const char *method_name)
165 {
166 jmethodID m_set;
168 /* Search for the `void setFoo (int i)' method. */
169 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
170 method_name, "(I)V");
171 if (m_set == NULL)
172 {
173 ERROR ("java plugin: ctoj_int: Cannot find method `void %s (int)'.",
174 method_name);
175 return (-1);
176 }
178 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
180 return (0);
181 } /* }}} int ctoj_int */
183 static int ctoj_long (JNIEnv *jvm_env, /* {{{ */
184 jlong value,
185 jclass class_ptr, jobject object_ptr, const char *method_name)
186 {
187 jmethodID m_set;
189 /* Search for the `void setFoo (long l)' method. */
190 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
191 method_name, "(J)V");
192 if (m_set == NULL)
193 {
194 ERROR ("java plugin: ctoj_long: Cannot find method `void %s (long)'.",
195 method_name);
196 return (-1);
197 }
199 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
201 return (0);
202 } /* }}} int ctoj_long */
204 static int ctoj_double (JNIEnv *jvm_env, /* {{{ */
205 jdouble value,
206 jclass class_ptr, jobject object_ptr, const char *method_name)
207 {
208 jmethodID m_set;
210 /* Search for the `void setFoo (double d)' method. */
211 m_set = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
212 method_name, "(D)V");
213 if (m_set == NULL)
214 {
215 ERROR ("java plugin: ctoj_double: Cannot find method `void %s (double)'.",
216 method_name);
217 return (-1);
218 }
220 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_set, value);
222 return (0);
223 } /* }}} int ctoj_double */
225 /* Convert a jlong to a java.lang.Number */
226 static jobject ctoj_jlong_to_number (JNIEnv *jvm_env, jlong value) /* {{{ */
227 {
228 jclass c_long;
229 jmethodID m_long_constructor;
231 /* Look up the java.lang.Long class */
232 c_long = (*jvm_env)->FindClass (jvm_env, "java/lang/Long");
233 if (c_long == NULL)
234 {
235 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
236 "java.lang.Long class failed.");
237 return (NULL);
238 }
240 m_long_constructor = (*jvm_env)->GetMethodID (jvm_env,
241 c_long, "<init>", "(J)V");
242 if (m_long_constructor == NULL)
243 {
244 ERROR ("java plugin: ctoj_jlong_to_number: Looking up the "
245 "`Long (long)' constructor failed.");
246 return (NULL);
247 }
249 return ((*jvm_env)->NewObject (jvm_env,
250 c_long, m_long_constructor, value));
251 } /* }}} jobject ctoj_jlong_to_number */
253 /* Convert a jdouble to a java.lang.Number */
254 static jobject ctoj_jdouble_to_number (JNIEnv *jvm_env, jdouble value) /* {{{ */
255 {
256 jclass c_double;
257 jmethodID m_double_constructor;
259 /* Look up the java.lang.Long class */
260 c_double = (*jvm_env)->FindClass (jvm_env, "java/lang/Double");
261 if (c_double == NULL)
262 {
263 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
264 "java.lang.Double class failed.");
265 return (NULL);
266 }
268 m_double_constructor = (*jvm_env)->GetMethodID (jvm_env,
269 c_double, "<init>", "(D)V");
270 if (m_double_constructor == NULL)
271 {
272 ERROR ("java plugin: ctoj_jdouble_to_number: Looking up the "
273 "`Double (double)' constructor failed.");
274 return (NULL);
275 }
277 return ((*jvm_env)->NewObject (jvm_env,
278 c_double, m_double_constructor, value));
279 } /* }}} jobject ctoj_jdouble_to_number */
281 /* Convert a value_t to a java.lang.Number */
282 static jobject ctoj_value_to_number (JNIEnv *jvm_env, /* {{{ */
283 value_t value, int ds_type)
284 {
285 if (ds_type == DS_TYPE_COUNTER)
286 return (ctoj_jlong_to_number (jvm_env, (jlong) value.counter));
287 else if (ds_type == DS_TYPE_GAUGE)
288 return (ctoj_jdouble_to_number (jvm_env, (jdouble) value.gauge));
289 if (ds_type == DS_TYPE_DERIVE)
290 return (ctoj_jlong_to_number (jvm_env, (jlong) value.derive));
291 if (ds_type == DS_TYPE_ABSOLUTE)
292 return (ctoj_jlong_to_number (jvm_env, (jlong) value.absolute));
293 else
294 return (NULL);
295 } /* }}} jobject ctoj_value_to_number */
297 /* Convert a data_source_t to a org/collectd/api/DataSource */
298 static jobject ctoj_data_source (JNIEnv *jvm_env, /* {{{ */
299 const data_source_t *dsrc)
300 {
301 jclass c_datasource;
302 jmethodID m_datasource_constructor;
303 jobject o_datasource;
304 int status;
306 /* Look up the DataSource class */
307 c_datasource = (*jvm_env)->FindClass (jvm_env,
308 "org/collectd/api/DataSource");
309 if (c_datasource == NULL)
310 {
311 ERROR ("java plugin: ctoj_data_source: "
312 "FindClass (org/collectd/api/DataSource) failed.");
313 return (NULL);
314 }
316 /* Lookup the `ValueList ()' constructor. */
317 m_datasource_constructor = (*jvm_env)->GetMethodID (jvm_env, c_datasource,
318 "<init>", "()V");
319 if (m_datasource_constructor == NULL)
320 {
321 ERROR ("java plugin: ctoj_data_source: Cannot find the "
322 "`DataSource ()' constructor.");
323 return (NULL);
324 }
326 /* Create a new instance. */
327 o_datasource = (*jvm_env)->NewObject (jvm_env, c_datasource,
328 m_datasource_constructor);
329 if (o_datasource == NULL)
330 {
331 ERROR ("java plugin: ctoj_data_source: "
332 "Creating a new DataSource instance failed.");
333 return (NULL);
334 }
336 /* Set name via `void setName (String name)' */
337 status = ctoj_string (jvm_env, dsrc->name,
338 c_datasource, o_datasource, "setName");
339 if (status != 0)
340 {
341 ERROR ("java plugin: ctoj_data_source: "
342 "ctoj_string (setName) failed.");
343 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
344 return (NULL);
345 }
347 /* Set type via `void setType (int type)' */
348 status = ctoj_int (jvm_env, dsrc->type,
349 c_datasource, o_datasource, "setType");
350 if (status != 0)
351 {
352 ERROR ("java plugin: ctoj_data_source: "
353 "ctoj_int (setType) failed.");
354 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
355 return (NULL);
356 }
358 /* Set min via `void setMin (double min)' */
359 status = ctoj_double (jvm_env, dsrc->min,
360 c_datasource, o_datasource, "setMin");
361 if (status != 0)
362 {
363 ERROR ("java plugin: ctoj_data_source: "
364 "ctoj_double (setMin) failed.");
365 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
366 return (NULL);
367 }
369 /* Set max via `void setMax (double max)' */
370 status = ctoj_double (jvm_env, dsrc->max,
371 c_datasource, o_datasource, "setMax");
372 if (status != 0)
373 {
374 ERROR ("java plugin: ctoj_data_source: "
375 "ctoj_double (setMax) failed.");
376 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
377 return (NULL);
378 }
380 return (o_datasource);
381 } /* }}} jobject ctoj_data_source */
383 /* Convert a oconfig_value_t to a org/collectd/api/OConfigValue */
384 static jobject ctoj_oconfig_value (JNIEnv *jvm_env, /* {{{ */
385 oconfig_value_t ocvalue)
386 {
387 jclass c_ocvalue;
388 jmethodID m_ocvalue_constructor;
389 jobject o_argument;
390 jobject o_ocvalue;
392 m_ocvalue_constructor = NULL;
393 o_argument = NULL;
395 c_ocvalue = (*jvm_env)->FindClass (jvm_env,
396 "org/collectd/api/OConfigValue");
397 if (c_ocvalue == NULL)
398 {
399 ERROR ("java plugin: ctoj_oconfig_value: "
400 "FindClass (org/collectd/api/OConfigValue) failed.");
401 return (NULL);
402 }
404 if (ocvalue.type == OCONFIG_TYPE_BOOLEAN)
405 {
406 jboolean tmp_boolean;
408 tmp_boolean = (ocvalue.value.boolean == 0) ? JNI_FALSE : JNI_TRUE;
410 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
411 "<init>", "(Z)V");
412 if (m_ocvalue_constructor == NULL)
413 {
414 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
415 "`OConfigValue (boolean)' constructor.");
416 return (NULL);
417 }
419 return ((*jvm_env)->NewObject (jvm_env,
420 c_ocvalue, m_ocvalue_constructor, tmp_boolean));
421 } /* if (ocvalue.type == OCONFIG_TYPE_BOOLEAN) */
422 else if (ocvalue.type == OCONFIG_TYPE_STRING)
423 {
424 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
425 "<init>", "(Ljava/lang/String;)V");
426 if (m_ocvalue_constructor == NULL)
427 {
428 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
429 "`OConfigValue (String)' constructor.");
430 return (NULL);
431 }
433 o_argument = (*jvm_env)->NewStringUTF (jvm_env, ocvalue.value.string);
434 if (o_argument == NULL)
435 {
436 ERROR ("java plugin: ctoj_oconfig_value: "
437 "Creating a String object failed.");
438 return (NULL);
439 }
440 }
441 else if (ocvalue.type == OCONFIG_TYPE_NUMBER)
442 {
443 m_ocvalue_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocvalue,
444 "<init>", "(Ljava/lang/Number;)V");
445 if (m_ocvalue_constructor == NULL)
446 {
447 ERROR ("java plugin: ctoj_oconfig_value: Cannot find the "
448 "`OConfigValue (Number)' constructor.");
449 return (NULL);
450 }
452 o_argument = ctoj_jdouble_to_number (jvm_env,
453 (jdouble) ocvalue.value.number);
454 if (o_argument == NULL)
455 {
456 ERROR ("java plugin: ctoj_oconfig_value: "
457 "Creating a Number object failed.");
458 return (NULL);
459 }
460 }
461 else
462 {
463 return (NULL);
464 }
466 assert (m_ocvalue_constructor != NULL);
467 assert (o_argument != NULL);
469 o_ocvalue = (*jvm_env)->NewObject (jvm_env,
470 c_ocvalue, m_ocvalue_constructor, o_argument);
471 if (o_ocvalue == NULL)
472 {
473 ERROR ("java plugin: ctoj_oconfig_value: "
474 "Creating an OConfigValue object failed.");
475 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
476 return (NULL);
477 }
479 (*jvm_env)->DeleteLocalRef (jvm_env, o_argument);
480 return (o_ocvalue);
481 } /* }}} jobject ctoj_oconfig_value */
483 /* Convert a oconfig_item_t to a org/collectd/api/OConfigItem */
484 static jobject ctoj_oconfig_item (JNIEnv *jvm_env, /* {{{ */
485 const oconfig_item_t *ci)
486 {
487 jclass c_ocitem;
488 jmethodID m_ocitem_constructor;
489 jmethodID m_addvalue;
490 jmethodID m_addchild;
491 jobject o_key;
492 jobject o_ocitem;
493 int i;
495 c_ocitem = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/OConfigItem");
496 if (c_ocitem == NULL)
497 {
498 ERROR ("java plugin: ctoj_oconfig_item: "
499 "FindClass (org/collectd/api/OConfigItem) failed.");
500 return (NULL);
501 }
503 /* Get the required methods: m_ocitem_constructor, m_addvalue, and m_addchild
504 * {{{ */
505 m_ocitem_constructor = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
506 "<init>", "(Ljava/lang/String;)V");
507 if (m_ocitem_constructor == NULL)
508 {
509 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
510 "`OConfigItem (String)' constructor.");
511 return (NULL);
512 }
514 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
515 "addValue", "(Lorg/collectd/api/OConfigValue;)V");
516 if (m_addvalue == NULL)
517 {
518 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
519 "`addValue (OConfigValue)' method.");
520 return (NULL);
521 }
523 m_addchild = (*jvm_env)->GetMethodID (jvm_env, c_ocitem,
524 "addChild", "(Lorg/collectd/api/OConfigItem;)V");
525 if (m_addchild == NULL)
526 {
527 ERROR ("java plugin: ctoj_oconfig_item: Cannot find the "
528 "`addChild (OConfigItem)' method.");
529 return (NULL);
530 }
531 /* }}} */
533 /* Create a String object with the key.
534 * Needed for calling the constructor. */
535 o_key = (*jvm_env)->NewStringUTF (jvm_env, ci->key);
536 if (o_key == NULL)
537 {
538 ERROR ("java plugin: ctoj_oconfig_item: "
539 "Creating String object failed.");
540 return (NULL);
541 }
543 /* Create an OConfigItem object */
544 o_ocitem = (*jvm_env)->NewObject (jvm_env,
545 c_ocitem, m_ocitem_constructor, o_key);
546 if (o_ocitem == NULL)
547 {
548 ERROR ("java plugin: ctoj_oconfig_item: "
549 "Creating an OConfigItem object failed.");
550 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
551 return (NULL);
552 }
554 /* We don't need the String object any longer.. */
555 (*jvm_env)->DeleteLocalRef (jvm_env, o_key);
557 /* Call OConfigItem.addValue for each value */
558 for (i = 0; i < ci->values_num; i++) /* {{{ */
559 {
560 jobject o_value;
562 o_value = ctoj_oconfig_value (jvm_env, ci->values[i]);
563 if (o_value == NULL)
564 {
565 ERROR ("java plugin: ctoj_oconfig_item: "
566 "Creating an OConfigValue object failed.");
567 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
568 return (NULL);
569 }
571 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addvalue, o_value);
572 (*jvm_env)->DeleteLocalRef (jvm_env, o_value);
573 } /* }}} for (i = 0; i < ci->values_num; i++) */
575 /* Call OConfigItem.addChild for each child */
576 for (i = 0; i < ci->children_num; i++) /* {{{ */
577 {
578 jobject o_child;
580 o_child = ctoj_oconfig_item (jvm_env, ci->children + i);
581 if (o_child == NULL)
582 {
583 ERROR ("java plugin: ctoj_oconfig_item: "
584 "Creating an OConfigItem object failed.");
585 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
586 return (NULL);
587 }
589 (*jvm_env)->CallVoidMethod (jvm_env, o_ocitem, m_addchild, o_child);
590 (*jvm_env)->DeleteLocalRef (jvm_env, o_child);
591 } /* }}} for (i = 0; i < ci->children_num; i++) */
593 return (o_ocitem);
594 } /* }}} jobject ctoj_oconfig_item */
596 /* Convert a data_set_t to a org/collectd/api/DataSet */
597 static jobject ctoj_data_set (JNIEnv *jvm_env, const data_set_t *ds) /* {{{ */
598 {
599 jclass c_dataset;
600 jmethodID m_constructor;
601 jmethodID m_add;
602 jobject o_type;
603 jobject o_dataset;
604 int i;
606 /* Look up the org/collectd/api/DataSet class */
607 c_dataset = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/DataSet");
608 if (c_dataset == NULL)
609 {
610 ERROR ("java plugin: ctoj_data_set: Looking up the "
611 "org/collectd/api/DataSet class failed.");
612 return (NULL);
613 }
615 /* Search for the `DataSet (String type)' constructor. */
616 m_constructor = (*jvm_env)->GetMethodID (jvm_env,
617 c_dataset, "<init>", "(Ljava/lang/String;)V");
618 if (m_constructor == NULL)
619 {
620 ERROR ("java plugin: ctoj_data_set: Looking up the "
621 "`DataSet (String)' constructor failed.");
622 return (NULL);
623 }
625 /* Search for the `void addDataSource (DataSource)' method. */
626 m_add = (*jvm_env)->GetMethodID (jvm_env,
627 c_dataset, "addDataSource", "(Lorg/collectd/api/DataSource;)V");
628 if (m_add == NULL)
629 {
630 ERROR ("java plugin: ctoj_data_set: Looking up the "
631 "`addDataSource (DataSource)' method failed.");
632 return (NULL);
633 }
635 o_type = (*jvm_env)->NewStringUTF (jvm_env, ds->type);
636 if (o_type == NULL)
637 {
638 ERROR ("java plugin: ctoj_data_set: Creating a String object failed.");
639 return (NULL);
640 }
642 o_dataset = (*jvm_env)->NewObject (jvm_env,
643 c_dataset, m_constructor, o_type);
644 if (o_dataset == NULL)
645 {
646 ERROR ("java plugin: ctoj_data_set: Creating a DataSet object failed.");
647 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
648 return (NULL);
649 }
651 /* Decrease reference counter on the java.lang.String object. */
652 (*jvm_env)->DeleteLocalRef (jvm_env, o_type);
654 for (i = 0; i < ds->ds_num; i++)
655 {
656 jobject o_datasource;
658 o_datasource = ctoj_data_source (jvm_env, ds->ds + i);
659 if (o_datasource == NULL)
660 {
661 ERROR ("java plugin: ctoj_data_set: ctoj_data_source (%s.%s) failed",
662 ds->type, ds->ds[i].name);
663 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
664 return (NULL);
665 }
667 (*jvm_env)->CallVoidMethod (jvm_env, o_dataset, m_add, o_datasource);
669 (*jvm_env)->DeleteLocalRef (jvm_env, o_datasource);
670 } /* for (i = 0; i < ds->ds_num; i++) */
672 return (o_dataset);
673 } /* }}} jobject ctoj_data_set */
675 static int ctoj_value_list_add_value (JNIEnv *jvm_env, /* {{{ */
676 value_t value, int ds_type,
677 jclass class_ptr, jobject object_ptr)
678 {
679 jmethodID m_addvalue;
680 jobject o_number;
682 m_addvalue = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
683 "addValue", "(Ljava/lang/Number;)V");
684 if (m_addvalue == NULL)
685 {
686 ERROR ("java plugin: ctoj_value_list_add_value: "
687 "Cannot find method `void addValue (Number)'.");
688 return (-1);
689 }
691 o_number = ctoj_value_to_number (jvm_env, value, ds_type);
692 if (o_number == NULL)
693 {
694 ERROR ("java plugin: ctoj_value_list_add_value: "
695 "ctoj_value_to_number failed.");
696 return (-1);
697 }
699 (*jvm_env)->CallVoidMethod (jvm_env, object_ptr, m_addvalue, o_number);
701 (*jvm_env)->DeleteLocalRef (jvm_env, o_number);
703 return (0);
704 } /* }}} int ctoj_value_list_add_value */
706 static int ctoj_value_list_add_data_set (JNIEnv *jvm_env, /* {{{ */
707 jclass c_valuelist, jobject o_valuelist, const data_set_t *ds)
708 {
709 jmethodID m_setdataset;
710 jobject o_dataset;
712 /* Look for the `void setDataSource (List<DataSource> ds)' method. */
713 m_setdataset = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
714 "setDataSet", "(Lorg/collectd/api/DataSet;)V");
715 if (m_setdataset == NULL)
716 {
717 ERROR ("java plugin: ctoj_value_list_add_data_set: "
718 "Cannot find the `void setDataSet (DataSet)' method.");
719 return (-1);
720 }
722 /* Create a DataSet object. */
723 o_dataset = ctoj_data_set (jvm_env, ds);
724 if (o_dataset == NULL)
725 {
726 ERROR ("java plugin: ctoj_value_list_add_data_set: "
727 "ctoj_data_set (%s) failed.", ds->type);
728 return (-1);
729 }
731 /* Actually call the method. */
732 (*jvm_env)->CallVoidMethod (jvm_env,
733 o_valuelist, m_setdataset, o_dataset);
735 /* Decrease reference counter on the List<DataSource> object. */
736 (*jvm_env)->DeleteLocalRef (jvm_env, o_dataset);
738 return (0);
739 } /* }}} int ctoj_value_list_add_data_set */
741 /* Convert a value_list_t (and data_set_t) to a org/collectd/api/ValueList */
742 static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
743 const data_set_t *ds, const value_list_t *vl)
744 {
745 jclass c_valuelist;
746 jmethodID m_valuelist_constructor;
747 jobject o_valuelist;
748 int status;
749 int i;
751 /* First, create a new ValueList instance..
752 * Look up the class.. */
753 c_valuelist = (*jvm_env)->FindClass (jvm_env,
754 "org/collectd/api/ValueList");
755 if (c_valuelist == NULL)
756 {
757 ERROR ("java plugin: ctoj_value_list: "
758 "FindClass (org/collectd/api/ValueList) failed.");
759 return (NULL);
760 }
762 /* Lookup the `ValueList ()' constructor. */
763 m_valuelist_constructor = (*jvm_env)->GetMethodID (jvm_env, c_valuelist,
764 "<init>", "()V");
765 if (m_valuelist_constructor == NULL)
766 {
767 ERROR ("java plugin: ctoj_value_list: Cannot find the "
768 "`ValueList ()' constructor.");
769 return (NULL);
770 }
772 /* Create a new instance. */
773 o_valuelist = (*jvm_env)->NewObject (jvm_env, c_valuelist,
774 m_valuelist_constructor);
775 if (o_valuelist == NULL)
776 {
777 ERROR ("java plugin: ctoj_value_list: Creating a new ValueList instance "
778 "failed.");
779 return (NULL);
780 }
782 status = ctoj_value_list_add_data_set (jvm_env,
783 c_valuelist, o_valuelist, ds);
784 if (status != 0)
785 {
786 ERROR ("java plugin: ctoj_value_list: "
787 "ctoj_value_list_add_data_set failed.");
788 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
789 return (NULL);
790 }
792 /* Set the strings.. */
793 #define SET_STRING(str,method_name) do { \
794 status = ctoj_string (jvm_env, str, \
795 c_valuelist, o_valuelist, method_name); \
796 if (status != 0) { \
797 ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
798 method_name); \
799 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
800 return (NULL); \
801 } } while (0)
803 SET_STRING (vl->host, "setHost");
804 SET_STRING (vl->plugin, "setPlugin");
805 SET_STRING (vl->plugin_instance, "setPluginInstance");
806 SET_STRING (vl->type, "setType");
807 SET_STRING (vl->type_instance, "setTypeInstance");
809 #undef SET_STRING
811 /* Set the `time' member. Java stores time in milliseconds. */
812 status = ctoj_long (jvm_env, ((jlong) vl->time) * ((jlong) 1000),
813 c_valuelist, o_valuelist, "setTime");
814 if (status != 0)
815 {
816 ERROR ("java plugin: ctoj_value_list: ctoj_long (setTime) failed.");
817 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
818 return (NULL);
819 }
821 /* Set the `interval' member.. */
822 status = ctoj_long (jvm_env, (jlong) vl->interval,
823 c_valuelist, o_valuelist, "setInterval");
824 if (status != 0)
825 {
826 ERROR ("java plugin: ctoj_value_list: ctoj_long (setInterval) failed.");
827 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
828 return (NULL);
829 }
831 for (i = 0; i < vl->values_len; i++)
832 {
833 status = ctoj_value_list_add_value (jvm_env, vl->values[i], ds->ds[i].type,
834 c_valuelist, o_valuelist);
835 if (status != 0)
836 {
837 ERROR ("java plugin: ctoj_value_list: "
838 "ctoj_value_list_add_value failed.");
839 (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist);
840 return (NULL);
841 }
842 }
844 return (o_valuelist);
845 } /* }}} jobject ctoj_value_list */
847 /* Convert a notification_t to a org/collectd/api/Notification */
848 static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
849 const notification_t *n)
850 {
851 jclass c_notification;
852 jmethodID m_constructor;
853 jobject o_notification;
854 int status;
856 /* First, create a new Notification instance..
857 * Look up the class.. */
858 c_notification = (*jvm_env)->FindClass (jvm_env,
859 "org/collectd/api/Notification");
860 if (c_notification == NULL)
861 {
862 ERROR ("java plugin: ctoj_notification: "
863 "FindClass (org/collectd/api/Notification) failed.");
864 return (NULL);
865 }
867 /* Lookup the `Notification ()' constructor. */
868 m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
869 "<init>", "()V");
870 if (m_constructor == NULL)
871 {
872 ERROR ("java plugin: ctoj_notification: Cannot find the "
873 "`Notification ()' constructor.");
874 return (NULL);
875 }
877 /* Create a new instance. */
878 o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
879 m_constructor);
880 if (o_notification == NULL)
881 {
882 ERROR ("java plugin: ctoj_notification: Creating a new Notification "
883 "instance failed.");
884 return (NULL);
885 }
887 /* Set the strings.. */
888 #define SET_STRING(str,method_name) do { \
889 status = ctoj_string (jvm_env, str, \
890 c_notification, o_notification, method_name); \
891 if (status != 0) { \
892 ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
893 method_name); \
894 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
895 return (NULL); \
896 } } while (0)
898 SET_STRING (n->host, "setHost");
899 SET_STRING (n->plugin, "setPlugin");
900 SET_STRING (n->plugin_instance, "setPluginInstance");
901 SET_STRING (n->type, "setType");
902 SET_STRING (n->type_instance, "setTypeInstance");
903 SET_STRING (n->message, "setMessage");
905 #undef SET_STRING
907 /* Set the `time' member. Java stores time in milliseconds. */
908 status = ctoj_long (jvm_env, ((jlong) n->time) * ((jlong) 1000),
909 c_notification, o_notification, "setTime");
910 if (status != 0)
911 {
912 ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
913 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
914 return (NULL);
915 }
917 /* Set the `interval' member.. */
918 status = ctoj_int (jvm_env, (jint) n->severity,
919 c_notification, o_notification, "setSeverity");
920 if (status != 0)
921 {
922 ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
923 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
924 return (NULL);
925 }
927 return (o_notification);
928 } /* }}} jobject ctoj_notification */
930 /*
931 * Java to C conversion functions
932 */
933 /* Call a `String <method> ()' method. */
934 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
935 char *buffer, size_t buffer_size, int empty_okay,
936 jclass class_ptr, jobject object_ptr, const char *method_name)
937 {
938 jmethodID method_id;
939 jobject string_obj;
940 const char *c_str;
942 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
943 method_name, "()Ljava/lang/String;");
944 if (method_id == NULL)
945 {
946 ERROR ("java plugin: jtoc_string: Cannot find method `String %s ()'.",
947 method_name);
948 return (-1);
949 }
951 string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
952 if ((string_obj == NULL) && (empty_okay == 0))
953 {
954 ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
955 method_name);
956 return (-1);
957 }
958 else if ((string_obj == NULL) && (empty_okay != 0))
959 {
960 memset (buffer, 0, buffer_size);
961 return (0);
962 }
964 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
965 if (c_str == NULL)
966 {
967 ERROR ("java plugin: jtoc_string: GetStringUTFChars failed.");
968 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
969 return (-1);
970 }
972 sstrncpy (buffer, c_str, buffer_size);
974 (*jvm_env)->ReleaseStringUTFChars (jvm_env, string_obj, c_str);
975 (*jvm_env)->DeleteLocalRef (jvm_env, string_obj);
977 return (0);
978 } /* }}} int jtoc_string */
980 /* Call an `int <method> ()' method. */
981 static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
982 jint *ret_value,
983 jclass class_ptr, jobject object_ptr, const char *method_name)
984 {
985 jmethodID method_id;
987 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
988 method_name, "()I");
989 if (method_id == NULL)
990 {
991 ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
992 method_name);
993 return (-1);
994 }
996 *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
998 return (0);
999 } /* }}} int jtoc_int */
1001 /* Call a `long <method> ()' method. */
1002 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
1003 jlong *ret_value,
1004 jclass class_ptr, jobject object_ptr, const char *method_name)
1005 {
1006 jmethodID method_id;
1008 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1009 method_name, "()J");
1010 if (method_id == NULL)
1011 {
1012 ERROR ("java plugin: jtoc_long: Cannot find method `long %s ()'.",
1013 method_name);
1014 return (-1);
1015 }
1017 *ret_value = (*jvm_env)->CallLongMethod (jvm_env, object_ptr, method_id);
1019 return (0);
1020 } /* }}} int jtoc_long */
1022 /* Call a `double <method> ()' method. */
1023 static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
1024 jdouble *ret_value,
1025 jclass class_ptr, jobject object_ptr, const char *method_name)
1026 {
1027 jmethodID method_id;
1029 method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1030 method_name, "()D");
1031 if (method_id == NULL)
1032 {
1033 ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
1034 method_name);
1035 return (-1);
1036 }
1038 *ret_value = (*jvm_env)->CallDoubleMethod (jvm_env, object_ptr, method_id);
1040 return (0);
1041 } /* }}} int jtoc_double */
1043 static int jtoc_value (JNIEnv *jvm_env, /* {{{ */
1044 value_t *ret_value, int ds_type, jobject object_ptr)
1045 {
1046 jclass class_ptr;
1047 int status;
1049 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1051 if (ds_type == DS_TYPE_GAUGE)
1052 {
1053 jdouble tmp_double;
1055 status = jtoc_double (jvm_env, &tmp_double,
1056 class_ptr, object_ptr, "doubleValue");
1057 if (status != 0)
1058 {
1059 ERROR ("java plugin: jtoc_value: "
1060 "jtoc_double failed.");
1061 return (-1);
1062 }
1063 (*ret_value).gauge = (gauge_t) tmp_double;
1064 }
1065 else
1066 {
1067 jlong tmp_long;
1069 status = jtoc_long (jvm_env, &tmp_long,
1070 class_ptr, object_ptr, "longValue");
1071 if (status != 0)
1072 {
1073 ERROR ("java plugin: jtoc_value: "
1074 "jtoc_long failed.");
1075 return (-1);
1076 }
1078 if (ds_type == DS_TYPE_DERIVE)
1079 (*ret_value).derive = (derive_t) tmp_long;
1080 else if (ds_type == DS_TYPE_ABSOLUTE)
1081 (*ret_value).absolute = (absolute_t) tmp_long;
1082 else
1083 (*ret_value).counter = (counter_t) tmp_long;
1084 }
1086 return (0);
1087 } /* }}} int jtoc_value */
1089 /* Read a List<Number>, convert it to `value_t' and add it to the given
1090 * `value_list_t'. */
1091 static int jtoc_values_array (JNIEnv *jvm_env, /* {{{ */
1092 const data_set_t *ds, value_list_t *vl,
1093 jclass class_ptr, jobject object_ptr)
1094 {
1095 jmethodID m_getvalues;
1096 jmethodID m_toarray;
1097 jobject o_list;
1098 jobjectArray o_number_array;
1100 value_t *values;
1101 int values_num;
1102 int i;
1104 values_num = ds->ds_num;
1106 values = NULL;
1107 o_number_array = NULL;
1108 o_list = NULL;
1110 #define BAIL_OUT(status) \
1111 free (values); \
1112 if (o_number_array != NULL) \
1113 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array); \
1114 if (o_list != NULL) \
1115 (*jvm_env)->DeleteLocalRef (jvm_env, o_list); \
1116 return (status);
1118 /* Call: List<Number> ValueList.getValues () */
1119 m_getvalues = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
1120 "getValues", "()Ljava/util/List;");
1121 if (m_getvalues == NULL)
1122 {
1123 ERROR ("java plugin: jtoc_values_array: "
1124 "Cannot find method `List getValues ()'.");
1125 BAIL_OUT (-1);
1126 }
1128 o_list = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, m_getvalues);
1129 if (o_list == NULL)
1130 {
1131 ERROR ("java plugin: jtoc_values_array: "
1132 "CallObjectMethod (getValues) failed.");
1133 BAIL_OUT (-1);
1134 }
1136 /* Call: Number[] List.toArray () */
1137 m_toarray = (*jvm_env)->GetMethodID (jvm_env,
1138 (*jvm_env)->GetObjectClass (jvm_env, o_list),
1139 "toArray", "()[Ljava/lang/Object;");
1140 if (m_toarray == NULL)
1141 {
1142 ERROR ("java plugin: jtoc_values_array: "
1143 "Cannot find method `Object[] toArray ()'.");
1144 BAIL_OUT (-1);
1145 }
1147 o_number_array = (*jvm_env)->CallObjectMethod (jvm_env, o_list, m_toarray);
1148 if (o_number_array == NULL)
1149 {
1150 ERROR ("java plugin: jtoc_values_array: "
1151 "CallObjectMethod (toArray) failed.");
1152 BAIL_OUT (-1);
1153 }
1155 values = (value_t *) calloc (values_num, sizeof (value_t));
1156 if (values == NULL)
1157 {
1158 ERROR ("java plugin: jtoc_values_array: calloc failed.");
1159 BAIL_OUT (-1);
1160 }
1162 for (i = 0; i < values_num; i++)
1163 {
1164 jobject o_number;
1165 int status;
1167 o_number = (*jvm_env)->GetObjectArrayElement (jvm_env,
1168 o_number_array, (jsize) i);
1169 if (o_number == NULL)
1170 {
1171 ERROR ("java plugin: jtoc_values_array: "
1172 "GetObjectArrayElement (%i) failed.", i);
1173 BAIL_OUT (-1);
1174 }
1176 status = jtoc_value (jvm_env, values + i, ds->ds[i].type, o_number);
1177 if (status != 0)
1178 {
1179 ERROR ("java plugin: jtoc_values_array: "
1180 "jtoc_value (%i) failed.", i);
1181 BAIL_OUT (-1);
1182 }
1183 } /* for (i = 0; i < values_num; i++) */
1185 vl->values = values;
1186 vl->values_len = values_num;
1188 #undef BAIL_OUT
1189 (*jvm_env)->DeleteLocalRef (jvm_env, o_number_array);
1190 (*jvm_env)->DeleteLocalRef (jvm_env, o_list);
1191 return (0);
1192 } /* }}} int jtoc_values_array */
1194 /* Convert a org/collectd/api/ValueList to a value_list_t. */
1195 static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
1196 jobject object_ptr)
1197 {
1198 jclass class_ptr;
1199 int status;
1200 jlong tmp_long;
1201 const data_set_t *ds;
1203 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1204 if (class_ptr == NULL)
1205 {
1206 ERROR ("java plugin: jtoc_value_list: GetObjectClass failed.");
1207 return (-1);
1208 }
1210 /* eo == empty okay */
1211 #define SET_STRING(buffer,method, eo) do { \
1212 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1213 class_ptr, object_ptr, method); \
1214 if (status != 0) { \
1215 ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
1216 method); \
1217 return (-1); \
1218 } } while (0)
1220 SET_STRING(vl->type, "getType", /* empty = */ 0);
1222 ds = plugin_get_ds (vl->type);
1223 if (ds == NULL)
1224 {
1225 ERROR ("java plugin: jtoc_value_list: Data-set `%s' is not defined. "
1226 "Please consult the types.db(5) manpage for mor information.",
1227 vl->type);
1228 return (-1);
1229 }
1231 SET_STRING(vl->host, "getHost", /* empty = */ 0);
1232 SET_STRING(vl->plugin, "getPlugin", /* empty = */ 0);
1233 SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
1234 SET_STRING(vl->type_instance, "getTypeInstance", /* empty = */ 1);
1236 #undef SET_STRING
1238 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1239 if (status != 0)
1240 {
1241 ERROR ("java plugin: jtoc_value_list: jtoc_long (getTime) failed.");
1242 return (-1);
1243 }
1244 /* Java measures time in milliseconds. */
1245 vl->time = (time_t) (tmp_long / ((jlong) 1000));
1247 status = jtoc_long (jvm_env, &tmp_long,
1248 class_ptr, object_ptr, "getInterval");
1249 if (status != 0)
1250 {
1251 ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed.");
1252 return (-1);
1253 }
1254 vl->interval = (int) tmp_long;
1256 status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr);
1257 if (status != 0)
1258 {
1259 ERROR ("java plugin: jtoc_value_list: jtoc_values_array failed.");
1260 return (-1);
1261 }
1263 return (0);
1264 } /* }}} int jtoc_value_list */
1266 /* Convert a org/collectd/api/Notification to a notification_t. */
1267 static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
1268 jobject object_ptr)
1269 {
1270 jclass class_ptr;
1271 int status;
1272 jlong tmp_long;
1273 jint tmp_int;
1275 class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
1276 if (class_ptr == NULL)
1277 {
1278 ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
1279 return (-1);
1280 }
1282 /* eo == empty okay */
1283 #define SET_STRING(buffer,method, eo) do { \
1284 status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
1285 class_ptr, object_ptr, method); \
1286 if (status != 0) { \
1287 ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
1288 method); \
1289 return (-1); \
1290 } } while (0)
1292 SET_STRING (n->host, "getHost", /* empty = */ 1);
1293 SET_STRING (n->plugin, "getPlugin", /* empty = */ 1);
1294 SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
1295 SET_STRING (n->type, "getType", /* empty = */ 1);
1296 SET_STRING (n->type_instance, "getTypeInstance", /* empty = */ 1);
1297 SET_STRING (n->message, "getMessage", /* empty = */ 0);
1299 #undef SET_STRING
1301 status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
1302 if (status != 0)
1303 {
1304 ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
1305 return (-1);
1306 }
1307 /* Java measures time in milliseconds. */
1308 n->time = (time_t) (tmp_long / ((jlong) 1000));
1310 status = jtoc_int (jvm_env, &tmp_int,
1311 class_ptr, object_ptr, "getSeverity");
1312 if (status != 0)
1313 {
1314 ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
1315 return (-1);
1316 }
1317 n->severity = (int) tmp_int;
1319 return (0);
1320 } /* }}} int jtoc_notification */
1321 /*
1322 * Functions accessible from Java
1323 */
1324 static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
1325 jobject this, jobject java_vl)
1326 {
1327 value_list_t vl = VALUE_LIST_INIT;
1328 int status;
1330 DEBUG ("cjni_api_dispatch_values: java_vl = %p;", (void *) java_vl);
1332 status = jtoc_value_list (jvm_env, &vl, java_vl);
1333 if (status != 0)
1334 {
1335 ERROR ("java plugin: cjni_api_dispatch_values: jtoc_value_list failed.");
1336 return (-1);
1337 }
1339 status = plugin_dispatch_values (&vl);
1341 sfree (vl.values);
1343 return (status);
1344 } /* }}} jint cjni_api_dispatch_values */
1346 static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
1347 jobject this, jobject o_notification)
1348 {
1349 notification_t n;
1350 int status;
1352 memset (&n, 0, sizeof (n));
1353 n.meta = NULL;
1355 status = jtoc_notification (jvm_env, &n, o_notification);
1356 if (status != 0)
1357 {
1358 ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
1359 return (-1);
1360 }
1362 status = plugin_dispatch_notification (&n);
1364 return (status);
1365 } /* }}} jint cjni_api_dispatch_notification */
1367 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
1368 jobject this, jobject o_string_type)
1369 {
1370 const char *ds_name;
1371 const data_set_t *ds;
1372 jobject o_dataset;
1374 ds_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_string_type, 0);
1375 if (ds_name == NULL)
1376 {
1377 ERROR ("java plugin: cjni_api_get_ds: GetStringUTFChars failed.");
1378 return (NULL);
1379 }
1381 ds = plugin_get_ds (ds_name);
1382 DEBUG ("java plugin: cjni_api_get_ds: "
1383 "plugin_get_ds (%s) = %p;", ds_name, (void *) ds);
1385 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_string_type, ds_name);
1387 if (ds == NULL)
1388 return (NULL);
1390 o_dataset = ctoj_data_set (jvm_env, ds);
1391 return (o_dataset);
1392 } /* }}} jint cjni_api_get_ds */
1394 static jint JNICALL cjni_api_register_config (JNIEnv *jvm_env, /* {{{ */
1395 jobject this, jobject o_name, jobject o_config)
1396 {
1397 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_CONFIG));
1398 } /* }}} jint cjni_api_register_config */
1400 static jint JNICALL cjni_api_register_init (JNIEnv *jvm_env, /* {{{ */
1401 jobject this, jobject o_name, jobject o_config)
1402 {
1403 return (cjni_callback_register (jvm_env, o_name, o_config, CB_TYPE_INIT));
1404 } /* }}} jint cjni_api_register_init */
1406 static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
1407 jobject this, jobject o_name, jobject o_read)
1408 {
1409 user_data_t ud;
1410 cjni_callback_info_t *cbi;
1412 cbi = cjni_callback_info_create (jvm_env, o_name, o_read, CB_TYPE_READ);
1413 if (cbi == NULL)
1414 return (-1);
1416 DEBUG ("java plugin: Registering new read callback: %s", cbi->name);
1418 memset (&ud, 0, sizeof (ud));
1419 ud.data = (void *) cbi;
1420 ud.free_func = cjni_callback_info_destroy;
1422 plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read,
1423 /* interval = */ NULL, &ud);
1425 (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1427 return (0);
1428 } /* }}} jint cjni_api_register_read */
1430 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1431 jobject this, jobject o_name, jobject o_write)
1432 {
1433 user_data_t ud;
1434 cjni_callback_info_t *cbi;
1436 cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1437 if (cbi == NULL)
1438 return (-1);
1440 DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1442 memset (&ud, 0, sizeof (ud));
1443 ud.data = (void *) cbi;
1444 ud.free_func = cjni_callback_info_destroy;
1446 plugin_register_write (cbi->name, cjni_write, &ud);
1448 (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1450 return (0);
1451 } /* }}} jint cjni_api_register_write */
1453 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1454 jobject this, jobject o_name, jobject o_flush)
1455 {
1456 user_data_t ud;
1457 cjni_callback_info_t *cbi;
1459 cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1460 if (cbi == NULL)
1461 return (-1);
1463 DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1465 memset (&ud, 0, sizeof (ud));
1466 ud.data = (void *) cbi;
1467 ud.free_func = cjni_callback_info_destroy;
1469 plugin_register_flush (cbi->name, cjni_flush, &ud);
1471 (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1473 return (0);
1474 } /* }}} jint cjni_api_register_flush */
1476 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1477 jobject this, jobject o_name, jobject o_shutdown)
1478 {
1479 return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1480 CB_TYPE_SHUTDOWN));
1481 } /* }}} jint cjni_api_register_shutdown */
1483 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1484 jobject this, jobject o_name, jobject o_log)
1485 {
1486 user_data_t ud;
1487 cjni_callback_info_t *cbi;
1489 cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1490 if (cbi == NULL)
1491 return (-1);
1493 DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1495 memset (&ud, 0, sizeof (ud));
1496 ud.data = (void *) cbi;
1497 ud.free_func = cjni_callback_info_destroy;
1499 plugin_register_log (cbi->name, cjni_log, &ud);
1501 (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1503 return (0);
1504 } /* }}} jint cjni_api_register_log */
1506 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1507 jobject this, jobject o_name, jobject o_notification)
1508 {
1509 user_data_t ud;
1510 cjni_callback_info_t *cbi;
1512 cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1513 CB_TYPE_NOTIFICATION);
1514 if (cbi == NULL)
1515 return (-1);
1517 DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1519 memset (&ud, 0, sizeof (ud));
1520 ud.data = (void *) cbi;
1521 ud.free_func = cjni_callback_info_destroy;
1523 plugin_register_notification (cbi->name, cjni_notification, &ud);
1525 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1527 return (0);
1528 } /* }}} jint cjni_api_register_notification */
1530 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1531 jobject this, jobject o_name, jobject o_match, int type)
1532 {
1533 int status;
1534 const char *c_name;
1536 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1537 if (c_name == NULL)
1538 {
1539 ERROR ("java plugin: cjni_api_register_match_target: "
1540 "GetStringUTFChars failed.");
1541 return (-1);
1542 }
1544 status = cjni_callback_register (jvm_env, o_name, o_match, type);
1545 if (status != 0)
1546 {
1547 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1548 return (-1);
1549 }
1551 if (type == CB_TYPE_MATCH)
1552 {
1553 match_proc_t m_proc;
1555 memset (&m_proc, 0, sizeof (m_proc));
1556 m_proc.create = cjni_match_target_create;
1557 m_proc.destroy = cjni_match_target_destroy;
1558 m_proc.match = (void *) cjni_match_target_invoke;
1560 status = fc_register_match (c_name, m_proc);
1561 }
1562 else if (type == CB_TYPE_TARGET)
1563 {
1564 target_proc_t t_proc;
1566 memset (&t_proc, 0, sizeof (t_proc));
1567 t_proc.create = cjni_match_target_create;
1568 t_proc.destroy = cjni_match_target_destroy;
1569 t_proc.invoke = cjni_match_target_invoke;
1571 status = fc_register_target (c_name, t_proc);
1572 }
1573 else
1574 {
1575 ERROR ("java plugin: cjni_api_register_match_target: "
1576 "Don't know whether to create a match or a target.");
1577 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1578 return (-1);
1579 }
1581 if (status != 0)
1582 {
1583 ERROR ("java plugin: cjni_api_register_match_target: "
1584 "%s failed.",
1585 (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1586 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1587 return (-1);
1588 }
1590 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1592 return (0);
1593 } /* }}} jint cjni_api_register_match_target */
1595 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1596 jobject this, jobject o_name, jobject o_match)
1597 {
1598 return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1599 CB_TYPE_MATCH));
1600 } /* }}} jint cjni_api_register_match */
1602 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1603 jobject this, jobject o_name, jobject o_target)
1604 {
1605 return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1606 CB_TYPE_TARGET));
1607 } /* }}} jint cjni_api_register_target */
1609 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1610 jobject this, jint severity, jobject o_message)
1611 {
1612 const char *c_str;
1614 c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1615 if (c_str == NULL)
1616 {
1617 ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1618 return;
1619 }
1621 if (severity < LOG_ERR)
1622 severity = LOG_ERR;
1623 if (severity > LOG_DEBUG)
1624 severity = LOG_DEBUG;
1626 plugin_log (severity, "%s", c_str);
1628 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1629 } /* }}} void cjni_api_log */
1631 /* List of ``native'' functions, i. e. C-functions that can be called from
1632 * Java. */
1633 static JNINativeMethod jni_api_functions[] = /* {{{ */
1634 {
1635 { "dispatchValues",
1636 "(Lorg/collectd/api/ValueList;)I",
1637 cjni_api_dispatch_values },
1639 { "dispatchNotification",
1640 "(Lorg/collectd/api/Notification;)I",
1641 cjni_api_dispatch_notification },
1643 { "getDS",
1644 "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1645 cjni_api_get_ds },
1647 { "registerConfig",
1648 "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1649 cjni_api_register_config },
1651 { "registerInit",
1652 "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1653 cjni_api_register_init },
1655 { "registerRead",
1656 "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1657 cjni_api_register_read },
1659 { "registerWrite",
1660 "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1661 cjni_api_register_write },
1663 { "registerFlush",
1664 "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1665 cjni_api_register_flush },
1667 { "registerShutdown",
1668 "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1669 cjni_api_register_shutdown },
1671 { "registerLog",
1672 "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1673 cjni_api_register_log },
1675 { "registerNotification",
1676 "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1677 cjni_api_register_notification },
1679 { "registerMatch",
1680 "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1681 cjni_api_register_match },
1683 { "registerTarget",
1684 "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1685 cjni_api_register_target },
1687 { "log",
1688 "(ILjava/lang/String;)V",
1689 cjni_api_log },
1690 };
1691 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1692 / sizeof (jni_api_functions[0]);
1693 /* }}} */
1695 /*
1696 * Functions
1697 */
1698 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1699 * all registration functions. */
1700 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1701 jobject o_name, jobject o_callback, int type)
1702 {
1703 const char *c_name;
1704 cjni_callback_info_t *cbi;
1705 const char *method_name;
1706 const char *method_signature;
1708 switch (type)
1709 {
1710 case CB_TYPE_CONFIG:
1711 method_name = "config";
1712 method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1713 break;
1715 case CB_TYPE_INIT:
1716 method_name = "init";
1717 method_signature = "()I";
1718 break;
1720 case CB_TYPE_READ:
1721 method_name = "read";
1722 method_signature = "()I";
1723 break;
1725 case CB_TYPE_WRITE:
1726 method_name = "write";
1727 method_signature = "(Lorg/collectd/api/ValueList;)I";
1728 break;
1730 case CB_TYPE_FLUSH:
1731 method_name = "flush";
1732 method_signature = "(ILjava/lang/String;)I";
1733 break;
1735 case CB_TYPE_SHUTDOWN:
1736 method_name = "shutdown";
1737 method_signature = "()I";
1738 break;
1740 case CB_TYPE_LOG:
1741 method_name = "log";
1742 method_signature = "(ILjava/lang/String;)V";
1743 break;
1745 case CB_TYPE_NOTIFICATION:
1746 method_name = "notification";
1747 method_signature = "(Lorg/collectd/api/Notification;)I";
1748 break;
1750 case CB_TYPE_MATCH:
1751 method_name = "createMatch";
1752 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1753 "Lorg/collectd/api/CollectdMatchInterface;";
1754 break;
1756 case CB_TYPE_TARGET:
1757 method_name = "createTarget";
1758 method_signature = "(Lorg/collectd/api/OConfigItem;)"
1759 "Lorg/collectd/api/CollectdTargetInterface;";
1760 break;
1762 default:
1763 ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1764 type);
1765 return (NULL);
1766 }
1768 c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1769 if (c_name == NULL)
1770 {
1771 ERROR ("java plugin: cjni_callback_info_create: "
1772 "GetStringUTFChars failed.");
1773 return (NULL);
1774 }
1776 cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1777 if (cbi == NULL)
1778 {
1779 ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1780 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1781 return (NULL);
1782 }
1783 memset (cbi, 0, sizeof (*cbi));
1784 cbi->type = type;
1786 cbi->name = strdup (c_name);
1787 if (cbi->name == NULL)
1788 {
1789 pthread_mutex_unlock (&java_callbacks_lock);
1790 ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1791 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1792 return (NULL);
1793 }
1795 (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1797 cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1798 if (cbi->object == NULL)
1799 {
1800 ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1801 free (cbi);
1802 return (NULL);
1803 }
1805 cbi->class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1806 if (cbi->class == NULL)
1807 {
1808 ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1809 free (cbi);
1810 return (NULL);
1811 }
1813 cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1814 method_name, method_signature);
1815 if (cbi->method == NULL)
1816 {
1817 ERROR ("java plugin: cjni_callback_info_create: "
1818 "Cannot find the `%s' method with signature `%s'.",
1819 method_name, method_signature);
1820 free (cbi);
1821 return (NULL);
1822 }
1824 return (cbi);
1825 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1827 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1828 * to the global `java_callbacks' variable. This is used for `config', `init',
1829 * and `shutdown' callbacks. */
1830 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1831 jobject o_name, jobject o_callback, int type)
1832 {
1833 cjni_callback_info_t *cbi;
1834 cjni_callback_info_t *tmp;
1835 #if COLLECT_DEBUG
1836 const char *type_str;
1837 #endif
1839 cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1840 if (cbi == NULL)
1841 return (-1);
1843 #if COLLECT_DEBUG
1844 switch (type)
1845 {
1846 case CB_TYPE_CONFIG:
1847 type_str = "config";
1848 break;
1850 case CB_TYPE_INIT:
1851 type_str = "init";
1852 break;
1854 case CB_TYPE_SHUTDOWN:
1855 type_str = "shutdown";
1856 break;
1858 case CB_TYPE_MATCH:
1859 type_str = "match";
1860 break;
1862 case CB_TYPE_TARGET:
1863 type_str = "target";
1864 break;
1866 default:
1867 type_str = "<unknown>";
1868 }
1869 DEBUG ("java plugin: Registering new %s callback: %s",
1870 type_str, cbi->name);
1871 #endif
1873 pthread_mutex_lock (&java_callbacks_lock);
1875 tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1876 (java_callbacks_num + 1) * sizeof (*java_callbacks));
1877 if (tmp == NULL)
1878 {
1879 pthread_mutex_unlock (&java_callbacks_lock);
1880 ERROR ("java plugin: cjni_callback_register: realloc failed.");
1882 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1883 free (cbi);
1885 return (-1);
1886 }
1887 java_callbacks = tmp;
1888 java_callbacks[java_callbacks_num] = *cbi;
1889 java_callbacks_num++;
1891 pthread_mutex_unlock (&java_callbacks_lock);
1893 free (cbi);
1894 return (0);
1895 } /* }}} int cjni_callback_register */
1897 /* Callback for `pthread_key_create'. It frees the data contained in
1898 * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1899 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1900 {
1901 cjni_jvm_env_t *cjni_env;
1903 if (args == NULL)
1904 return;
1906 cjni_env = (cjni_jvm_env_t *) args;
1908 if (cjni_env->reference_counter > 0)
1909 {
1910 ERROR ("java plugin: cjni_jvm_env_destroy: "
1911 "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1912 }
1914 if (cjni_env->jvm_env != NULL)
1915 {
1916 ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1917 (void *) cjni_env->jvm_env);
1918 }
1920 /* The pointer is allocated in `cjni_thread_attach' */
1921 free (cjni_env);
1922 } /* }}} void cjni_jvm_env_destroy */
1924 /* Register ``native'' functions with the JVM. Native functions are C-functions
1925 * that can be called by Java code. */
1926 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1927 {
1928 jclass api_class_ptr;
1929 int status;
1931 api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1932 if (api_class_ptr == NULL)
1933 {
1934 ERROR ("cjni_init_native: Cannot find API class `org/collectd/api/Collectd'.");
1935 return (-1);
1936 }
1938 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1939 jni_api_functions, (jint) jni_api_functions_num);
1940 if (status != 0)
1941 {
1942 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1943 return (-1);
1944 }
1946 return (0);
1947 } /* }}} int cjni_init_native */
1949 /* Create the JVM. This is called when the first thread tries to access the JVM
1950 * via cjni_thread_attach. */
1951 static int cjni_create_jvm (void) /* {{{ */
1952 {
1953 JNIEnv *jvm_env;
1954 JavaVMInitArgs vm_args;
1955 JavaVMOption vm_options[jvm_argc];
1957 int status;
1958 size_t i;
1960 if (jvm != NULL)
1961 return (0);
1963 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1964 if (status != 0)
1965 {
1966 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1967 "with status %i.", status);
1968 return (-1);
1969 }
1971 jvm_env = NULL;
1973 memset (&vm_args, 0, sizeof (vm_args));
1974 vm_args.version = JNI_VERSION_1_2;
1975 vm_args.options = vm_options;
1976 vm_args.nOptions = (jint) jvm_argc;
1978 for (i = 0; i < jvm_argc; i++)
1979 {
1980 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
1981 i, jvm_argv[i]);
1982 vm_args.options[i].optionString = jvm_argv[i];
1983 }
1985 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
1986 if (status != 0)
1987 {
1988 ERROR ("java plugin: cjni_create_jvm: "
1989 "JNI_CreateJavaVM failed with status %i.",
1990 status);
1991 return (-1);
1992 }
1993 assert (jvm != NULL);
1994 assert (jvm_env != NULL);
1996 /* Call RegisterNatives */
1997 status = cjni_init_native (jvm_env);
1998 if (status != 0)
1999 {
2000 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
2001 return (-1);
2002 }
2004 DEBUG ("java plugin: The JVM has been created.");
2005 return (0);
2006 } /* }}} int cjni_create_jvm */
2008 /* Increase the reference counter to the JVM for this thread. If it was zero,
2009 * attach the JVM first. */
2010 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2011 {
2012 cjni_jvm_env_t *cjni_env;
2013 JNIEnv *jvm_env;
2015 /* If we're the first thread to access the JVM, we'll have to create it
2016 * first.. */
2017 if (jvm == NULL)
2018 {
2019 int status;
2021 status = cjni_create_jvm ();
2022 if (status != 0)
2023 {
2024 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2025 return (NULL);
2026 }
2027 }
2028 assert (jvm != NULL);
2030 cjni_env = pthread_getspecific (jvm_env_key);
2031 if (cjni_env == NULL)
2032 {
2033 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2034 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
2035 if (cjni_env == NULL)
2036 {
2037 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
2038 return (NULL);
2039 }
2040 memset (cjni_env, 0, sizeof (*cjni_env));
2041 cjni_env->reference_counter = 0;
2042 cjni_env->jvm_env = NULL;
2044 pthread_setspecific (jvm_env_key, cjni_env);
2045 }
2047 if (cjni_env->reference_counter > 0)
2048 {
2049 cjni_env->reference_counter++;
2050 jvm_env = cjni_env->jvm_env;
2051 }
2052 else
2053 {
2054 int status;
2055 JavaVMAttachArgs args;
2057 assert (cjni_env->jvm_env == NULL);
2059 memset (&args, 0, sizeof (args));
2060 args.version = JNI_VERSION_1_2;
2062 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2063 if (status != 0)
2064 {
2065 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2066 "with status %i.", status);
2067 return (NULL);
2068 }
2070 cjni_env->reference_counter = 1;
2071 cjni_env->jvm_env = jvm_env;
2072 }
2074 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2075 cjni_env->reference_counter);
2076 assert (jvm_env != NULL);
2077 return (jvm_env);
2078 } /* }}} JNIEnv *cjni_thread_attach */
2080 /* Decrease the reference counter of this thread. If it reaches zero, detach
2081 * from the JVM. */
2082 static int cjni_thread_detach (void) /* {{{ */
2083 {
2084 cjni_jvm_env_t *cjni_env;
2085 int status;
2087 cjni_env = pthread_getspecific (jvm_env_key);
2088 if (cjni_env == NULL)
2089 {
2090 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2091 return (-1);
2092 }
2094 assert (cjni_env->reference_counter > 0);
2095 assert (cjni_env->jvm_env != NULL);
2097 cjni_env->reference_counter--;
2098 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2099 cjni_env->reference_counter);
2101 if (cjni_env->reference_counter > 0)
2102 return (0);
2104 status = (*jvm)->DetachCurrentThread (jvm);
2105 if (status != 0)
2106 {
2107 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2108 "with status %i.", status);
2109 }
2111 cjni_env->reference_counter = 0;
2112 cjni_env->jvm_env = NULL;
2114 return (0);
2115 } /* }}} JNIEnv *cjni_thread_attach */
2117 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2118 {
2119 char **tmp;
2121 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2122 {
2123 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2124 return (-1);
2125 }
2127 if (jvm != NULL)
2128 {
2129 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2130 "`LoadPlugin' options! The JVM is already started and I have to "
2131 "ignore this argument: %s",
2132 ci->values[0].value.string);
2133 return (-1);
2134 }
2136 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2137 if (tmp == NULL)
2138 {
2139 ERROR ("java plugin: realloc failed.");
2140 return (-1);
2141 }
2142 jvm_argv = tmp;
2144 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2145 if (jvm_argv[jvm_argc] == NULL)
2146 {
2147 ERROR ("java plugin: strdup failed.");
2148 return (-1);
2149 }
2150 jvm_argc++;
2152 return (0);
2153 } /* }}} int cjni_config_add_jvm_arg */
2155 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2156 {
2157 JNIEnv *jvm_env;
2158 java_plugin_class_t *class;
2159 jmethodID constructor_id;
2160 jobject tmp_object;
2162 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2163 {
2164 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2165 return (-1);
2166 }
2168 jvm_env = cjni_thread_attach ();
2169 if (jvm_env == NULL)
2170 return (-1);
2172 class = (java_plugin_class_t *) realloc (java_classes_list,
2173 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2174 if (class == NULL)
2175 {
2176 ERROR ("java plugin: realloc failed.");
2177 cjni_thread_detach ();
2178 return (-1);
2179 }
2180 java_classes_list = class;
2181 class = java_classes_list + java_classes_list_len;
2183 memset (class, 0, sizeof (*class));
2184 class->name = strdup (ci->values[0].value.string);
2185 if (class->name == NULL)
2186 {
2187 ERROR ("java plugin: strdup failed.");
2188 cjni_thread_detach ();
2189 return (-1);
2190 }
2191 class->class = NULL;
2192 class->object = NULL;
2194 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2195 thorough the Java community, but (Sun's) `FindClass' and friends need
2196 slashes. */
2197 size_t i;
2198 for (i = 0; class->name[i] != 0; i++)
2199 if (class->name[i] == '.')
2200 class->name[i] = '/';
2201 }
2203 DEBUG ("java plugin: Loading class %s", class->name);
2205 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2206 if (class->class == NULL)
2207 {
2208 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2209 class->name);
2210 cjni_thread_detach ();
2211 free (class->name);
2212 return (-1);
2213 }
2215 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2216 "<init>", "()V");
2217 if (constructor_id == NULL)
2218 {
2219 ERROR ("java plugin: cjni_config_load_plugin: "
2220 "Could not find the constructor for `%s'.",
2221 class->name);
2222 cjni_thread_detach ();
2223 free (class->name);
2224 return (-1);
2225 }
2227 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2228 constructor_id);
2229 if (tmp_object != NULL)
2230 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2231 else
2232 class->object = NULL;
2233 if (class->object == NULL)
2234 {
2235 ERROR ("java plugin: cjni_config_load_plugin: "
2236 "Could create a new `%s' object.",
2237 class->name);
2238 cjni_thread_detach ();
2239 free (class->name);
2240 return (-1);
2241 }
2243 cjni_thread_detach ();
2245 java_classes_list_len++;
2247 return (0);
2248 } /* }}} int cjni_config_load_plugin */
2250 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2251 {
2252 JNIEnv *jvm_env;
2253 cjni_callback_info_t *cbi;
2254 jobject o_ocitem;
2255 const char *name;
2256 int status;
2257 size_t i;
2259 jclass class;
2260 jmethodID method;
2262 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2263 {
2264 WARNING ("java plugin: `Plugin' blocks "
2265 "need exactly one string argument.");
2266 return (-1);
2267 }
2269 name = ci->values[0].value.string;
2271 cbi = NULL;
2272 for (i = 0; i < java_callbacks_num; i++)
2273 {
2274 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2275 continue;
2277 if (strcmp (name, java_callbacks[i].name) != 0)
2278 continue;
2280 cbi = java_callbacks + i;
2281 break;
2282 }
2284 if (cbi == NULL)
2285 {
2286 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2287 "configuration callback has been registered. Please make sure, the "
2288 "`LoadPlugin' lines precede the `Plugin' blocks.",
2289 name);
2290 return (0);
2291 }
2293 DEBUG ("java plugin: Configuring %s", name);
2295 jvm_env = cjni_thread_attach ();
2296 if (jvm_env == NULL)
2297 return (-1);
2299 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2300 if (o_ocitem == NULL)
2301 {
2302 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2303 cjni_thread_detach ();
2304 return (-1);
2305 }
2307 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2308 method = (*jvm_env)->GetMethodID (jvm_env, class,
2309 "config", "(Lorg/collectd/api/OConfigItem;)I");
2311 status = (*jvm_env)->CallIntMethod (jvm_env,
2312 cbi->object, method, o_ocitem);
2314 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2315 cjni_thread_detach ();
2316 return (0);
2317 } /* }}} int cjni_config_plugin_block */
2319 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2320 {
2321 int success;
2322 int errors;
2323 int status;
2324 int i;
2326 success = 0;
2327 errors = 0;
2329 for (i = 0; i < ci->children_num; i++)
2330 {
2331 oconfig_item_t *child = ci->children + i;
2333 if (strcasecmp ("JVMArg", child->key) == 0)
2334 {
2335 status = cjni_config_add_jvm_arg (child);
2336 if (status == 0)
2337 success++;
2338 else
2339 errors++;
2340 }
2341 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2342 {
2343 status = cjni_config_load_plugin (child);
2344 if (status == 0)
2345 success++;
2346 else
2347 errors++;
2348 }
2349 else if (strcasecmp ("Plugin", child->key) == 0)
2350 {
2351 status = cjni_config_plugin_block (child);
2352 if (status == 0)
2353 success++;
2354 else
2355 errors++;
2356 }
2357 else
2358 {
2359 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2360 errors++;
2361 }
2362 }
2364 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2365 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2367 if ((success == 0) && (errors > 0))
2368 {
2369 ERROR ("java plugin: All statements failed.");
2370 return (-1);
2371 }
2373 return (0);
2374 } /* }}} int cjni_config_perform */
2376 /* Copy the children of `ci' to the global `config_block' variable. */
2377 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2378 {
2379 oconfig_item_t *ci_copy;
2380 oconfig_item_t *tmp;
2382 assert (ci != NULL);
2383 if (ci->children_num == 0)
2384 return (0); /* nothing to do */
2386 ci_copy = oconfig_clone (ci);
2387 if (ci_copy == NULL)
2388 {
2389 ERROR ("java plugin: oconfig_clone failed.");
2390 return (-1);
2391 }
2393 if (config_block == NULL)
2394 {
2395 config_block = ci_copy;
2396 return (0);
2397 }
2399 tmp = realloc (config_block->children,
2400 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2401 if (tmp == NULL)
2402 {
2403 ERROR ("java plugin: realloc failed.");
2404 oconfig_free (ci_copy);
2405 return (-1);
2406 }
2407 config_block->children = tmp;
2409 /* Copy the pointers */
2410 memcpy (config_block->children + config_block->children_num,
2411 ci_copy->children,
2412 ci_copy->children_num * sizeof (*ci_copy->children));
2413 config_block->children_num += ci_copy->children_num;
2415 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2416 memset (ci_copy->children, 0,
2417 ci_copy->children_num * sizeof (*ci_copy->children));
2418 ci_copy->children_num = 0;
2420 oconfig_free (ci_copy);
2422 return (0);
2423 } /* }}} int cjni_config_callback */
2425 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2426 * and `cjni_write'. In particular, delete the global reference to the Java
2427 * object. */
2428 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2429 {
2430 JNIEnv *jvm_env;
2431 cjni_callback_info_t *cbi;
2433 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2435 cbi = (cjni_callback_info_t *) arg;
2437 /* This condition can occurr when shutting down. */
2438 if (jvm == NULL)
2439 {
2440 sfree (cbi);
2441 return;
2442 }
2444 if (arg == NULL)
2445 return;
2447 jvm_env = cjni_thread_attach ();
2448 if (jvm_env == NULL)
2449 {
2450 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2451 return;
2452 }
2454 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2456 cbi->method = NULL;
2457 cbi->object = NULL;
2458 cbi->class = NULL;
2459 free (cbi);
2461 cjni_thread_detach ();
2462 } /* }}} void cjni_callback_info_destroy */
2464 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2465 static int cjni_read (user_data_t *ud) /* {{{ */
2466 {
2467 JNIEnv *jvm_env;
2468 cjni_callback_info_t *cbi;
2469 int status;
2470 int ret_status;
2472 if (jvm == NULL)
2473 {
2474 ERROR ("java plugin: cjni_read: jvm == NULL");
2475 return (-1);
2476 }
2478 if ((ud == NULL) || (ud->data == NULL))
2479 {
2480 ERROR ("java plugin: cjni_read: Invalid user data.");
2481 return (-1);
2482 }
2484 jvm_env = cjni_thread_attach ();
2485 if (jvm_env == NULL)
2486 return (-1);
2488 cbi = (cjni_callback_info_t *) ud->data;
2490 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2491 cbi->method);
2493 status = cjni_thread_detach ();
2494 if (status != 0)
2495 {
2496 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2497 return (-1);
2498 }
2500 return (ret_status);
2501 } /* }}} int cjni_read */
2503 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2504 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2505 user_data_t *ud)
2506 {
2507 JNIEnv *jvm_env;
2508 cjni_callback_info_t *cbi;
2509 jobject vl_java;
2510 int status;
2511 int ret_status;
2513 if (jvm == NULL)
2514 {
2515 ERROR ("java plugin: cjni_write: jvm == NULL");
2516 return (-1);
2517 }
2519 if ((ud == NULL) || (ud->data == NULL))
2520 {
2521 ERROR ("java plugin: cjni_write: Invalid user data.");
2522 return (-1);
2523 }
2525 jvm_env = cjni_thread_attach ();
2526 if (jvm_env == NULL)
2527 return (-1);
2529 cbi = (cjni_callback_info_t *) ud->data;
2531 vl_java = ctoj_value_list (jvm_env, ds, vl);
2532 if (vl_java == NULL)
2533 {
2534 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2535 return (-1);
2536 }
2538 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2539 cbi->object, cbi->method, vl_java);
2541 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2543 status = cjni_thread_detach ();
2544 if (status != 0)
2545 {
2546 ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2547 return (-1);
2548 }
2550 return (ret_status);
2551 } /* }}} int cjni_write */
2553 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2554 static int cjni_flush (int timeout, const char *identifier, /* {{{ */
2555 user_data_t *ud)
2556 {
2557 JNIEnv *jvm_env;
2558 cjni_callback_info_t *cbi;
2559 jobject o_identifier;
2560 int status;
2561 int ret_status;
2563 if (jvm == NULL)
2564 {
2565 ERROR ("java plugin: cjni_flush: jvm == NULL");
2566 return (-1);
2567 }
2569 if ((ud == NULL) || (ud->data == NULL))
2570 {
2571 ERROR ("java plugin: cjni_flush: Invalid user data.");
2572 return (-1);
2573 }
2575 jvm_env = cjni_thread_attach ();
2576 if (jvm_env == NULL)
2577 return (-1);
2579 cbi = (cjni_callback_info_t *) ud->data;
2581 o_identifier = NULL;
2582 if (identifier != NULL)
2583 {
2584 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2585 if (o_identifier == NULL)
2586 {
2587 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2588 return (-1);
2589 }
2590 }
2592 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2593 cbi->object, cbi->method, (jint) timeout, o_identifier);
2595 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2597 status = cjni_thread_detach ();
2598 if (status != 0)
2599 {
2600 ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2601 return (-1);
2602 }
2604 return (ret_status);
2605 } /* }}} int cjni_flush */
2607 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2608 static void cjni_log (int severity, const char *message, /* {{{ */
2609 user_data_t *ud)
2610 {
2611 JNIEnv *jvm_env;
2612 cjni_callback_info_t *cbi;
2613 jobject o_message;
2615 if (jvm == NULL)
2616 return;
2618 if ((ud == NULL) || (ud->data == NULL))
2619 return;
2621 jvm_env = cjni_thread_attach ();
2622 if (jvm_env == NULL)
2623 return;
2625 cbi = (cjni_callback_info_t *) ud->data;
2627 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2628 if (o_message == NULL)
2629 return;
2631 (*jvm_env)->CallVoidMethod (jvm_env,
2632 cbi->object, cbi->method, (jint) severity, o_message);
2634 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2636 cjni_thread_detach ();
2637 } /* }}} void cjni_log */
2639 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2640 * pointer. */
2641 static int cjni_notification (const notification_t *n, /* {{{ */
2642 user_data_t *ud)
2643 {
2644 JNIEnv *jvm_env;
2645 cjni_callback_info_t *cbi;
2646 jobject o_notification;
2647 int status;
2648 int ret_status;
2650 if (jvm == NULL)
2651 {
2652 ERROR ("java plugin: cjni_read: jvm == NULL");
2653 return (-1);
2654 }
2656 if ((ud == NULL) || (ud->data == NULL))
2657 {
2658 ERROR ("java plugin: cjni_read: Invalid user data.");
2659 return (-1);
2660 }
2662 jvm_env = cjni_thread_attach ();
2663 if (jvm_env == NULL)
2664 return (-1);
2666 cbi = (cjni_callback_info_t *) ud->data;
2668 o_notification = ctoj_notification (jvm_env, n);
2669 if (o_notification == NULL)
2670 {
2671 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2672 return (-1);
2673 }
2675 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2676 cbi->object, cbi->method, o_notification);
2678 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2680 status = cjni_thread_detach ();
2681 if (status != 0)
2682 {
2683 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2684 return (-1);
2685 }
2687 return (ret_status);
2688 } /* }}} int cjni_notification */
2690 /* Callbacks for matches implemented in Java */
2691 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2692 void **user_data)
2693 {
2694 JNIEnv *jvm_env;
2695 cjni_callback_info_t *cbi_ret;
2696 cjni_callback_info_t *cbi_factory;
2697 const char *name;
2698 jobject o_ci;
2699 jobject o_tmp;
2700 int type;
2701 size_t i;
2703 cbi_ret = NULL;
2704 o_ci = NULL;
2705 jvm_env = NULL;
2707 #define BAIL_OUT(status) \
2708 if (cbi_ret != NULL) { \
2709 free (cbi_ret->name); \
2710 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2711 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2712 } \
2713 free (cbi_ret); \
2714 if (jvm_env != NULL) { \
2715 if (o_ci != NULL) \
2716 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2717 cjni_thread_detach (); \
2718 } \
2719 return (status)
2721 if (jvm == NULL)
2722 {
2723 ERROR ("java plugin: cjni_read: jvm == NULL");
2724 BAIL_OUT (-1);
2725 }
2727 jvm_env = cjni_thread_attach ();
2728 if (jvm_env == NULL)
2729 {
2730 BAIL_OUT (-1);
2731 }
2733 /* Find out whether to create a match or a target. */
2734 if (strcasecmp ("Match", ci->key) == 0)
2735 type = CB_TYPE_MATCH;
2736 else if (strcasecmp ("Target", ci->key) == 0)
2737 type = CB_TYPE_TARGET;
2738 else
2739 {
2740 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2741 "to create a match or a target.");
2742 BAIL_OUT (-1);
2743 }
2745 /* This is the name of the match we should create. */
2746 name = ci->values[0].value.string;
2748 /* Lets see if we have a matching factory here.. */
2749 cbi_factory = NULL;
2750 for (i = 0; i < java_callbacks_num; i++)
2751 {
2752 if (java_callbacks[i].type != type)
2753 continue;
2755 if (strcmp (name, java_callbacks[i].name) != 0)
2756 continue;
2758 cbi_factory = java_callbacks + i;
2759 break;
2760 }
2762 /* Nope, no factory for that name.. */
2763 if (cbi_factory == NULL)
2764 {
2765 ERROR ("java plugin: cjni_match_target_create: "
2766 "No such match factory registered: %s",
2767 name);
2768 BAIL_OUT (-1);
2769 }
2771 /* We convert `ci' to its Java equivalent.. */
2772 o_ci = ctoj_oconfig_item (jvm_env, ci);
2773 if (o_ci == NULL)
2774 {
2775 ERROR ("java plugin: cjni_match_target_create: "
2776 "ctoj_oconfig_item failed.");
2777 BAIL_OUT (-1);
2778 }
2780 /* Allocate a new callback info structure. This is going to be our user_data
2781 * pointer. */
2782 cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
2783 if (cbi_ret == NULL)
2784 {
2785 ERROR ("java plugin: cjni_match_target_create: malloc failed.");
2786 BAIL_OUT (-1);
2787 }
2788 memset (cbi_ret, 0, sizeof (*cbi_ret));
2789 cbi_ret->object = NULL;
2790 cbi_ret->type = type;
2792 /* Lets fill the callback info structure.. First, the name: */
2793 cbi_ret->name = strdup (name);
2794 if (cbi_ret->name == NULL)
2795 {
2796 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2797 BAIL_OUT (-1);
2798 }
2800 /* Then call the factory method so it creates a new object for us. */
2801 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2802 cbi_factory->object, cbi_factory->method, o_ci);
2803 if (o_tmp == NULL)
2804 {
2805 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2806 BAIL_OUT (-1);
2807 }
2809 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2810 if (o_tmp == NULL)
2811 {
2812 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2813 BAIL_OUT (-1);
2814 }
2816 /* This is the class of the match. It is possibly different from the class of
2817 * the match-factory! */
2818 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2819 if (cbi_ret->class == NULL)
2820 {
2821 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2822 BAIL_OUT (-1);
2823 }
2825 /* Lookup the `int match (DataSet, ValueList)' method. */
2826 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2827 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2828 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2829 if (cbi_ret->method == NULL)
2830 {
2831 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2832 BAIL_OUT (-1);
2833 }
2835 /* Return the newly created match via the user_data pointer. */
2836 *user_data = (void *) cbi_ret;
2838 cjni_thread_detach ();
2840 DEBUG ("java plugin: cjni_match_target_create: "
2841 "Successfully created a `%s' %s.",
2842 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2844 /* Success! */
2845 return (0);
2846 #undef BAIL_OUT
2847 } /* }}} int cjni_match_target_create */
2849 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2850 {
2851 cjni_callback_info_destroy (*user_data);
2852 *user_data = NULL;
2854 return (0);
2855 } /* }}} int cjni_match_target_destroy */
2857 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2858 value_list_t *vl, notification_meta_t **meta, void **user_data)
2859 {
2860 JNIEnv *jvm_env;
2861 cjni_callback_info_t *cbi;
2862 jobject o_vl;
2863 jobject o_ds;
2864 int ret_status;
2865 int status;
2867 if (jvm == NULL)
2868 {
2869 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2870 return (-1);
2871 }
2873 jvm_env = cjni_thread_attach ();
2874 if (jvm_env == NULL)
2875 return (-1);
2877 cbi = (cjni_callback_info_t *) *user_data;
2879 o_vl = ctoj_value_list (jvm_env, ds, vl);
2880 if (o_vl == NULL)
2881 {
2882 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2883 cjni_thread_detach ();
2884 return (-1);
2885 }
2887 o_ds = ctoj_data_set (jvm_env, ds);
2888 if (o_ds == NULL)
2889 {
2890 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2891 cjni_thread_detach ();
2892 return (-1);
2893 }
2895 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2896 o_ds, o_vl);
2898 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2900 /* If we're executing a target, copy the `ValueList' back to our
2901 * `value_list_t'. */
2902 if (cbi->type == CB_TYPE_TARGET)
2903 {
2904 value_list_t new_vl;
2906 memset (&new_vl, 0, sizeof (new_vl));
2907 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2908 if (status != 0)
2909 {
2910 ERROR ("java plugin: cjni_match_target_invoke: "
2911 "jtoc_value_list failed.");
2912 }
2913 else /* if (status == 0) */
2914 {
2915 /* plugin_dispatch_values assures that this is dynamically allocated
2916 * memory. */
2917 sfree (vl->values);
2919 /* This will replace the vl->values pointer to a new, dynamically
2920 * allocated piece of memory. */
2921 memcpy (vl, &new_vl, sizeof (*vl));
2922 }
2923 } /* if (cbi->type == CB_TYPE_TARGET) */
2925 status = cjni_thread_detach ();
2926 if (status != 0)
2927 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2929 return (ret_status);
2930 } /* }}} int cjni_match_target_invoke */
2932 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2933 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2934 {
2935 int status;
2936 size_t i;
2938 for (i = 0; i < java_callbacks_num; i++)
2939 {
2940 if (java_callbacks[i].type != CB_TYPE_INIT)
2941 continue;
2943 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2945 status = (*jvm_env)->CallIntMethod (jvm_env,
2946 java_callbacks[i].object, java_callbacks[i].method);
2947 if (status != 0)
2948 {
2949 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2950 "Removing read function.",
2951 java_callbacks[i].name, status);
2952 plugin_unregister_read (java_callbacks[i].name);
2953 }
2954 }
2956 return (0);
2957 } /* }}} int cjni_init_plugins */
2959 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2960 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2961 {
2962 int status;
2963 size_t i;
2965 for (i = 0; i < java_callbacks_num; i++)
2966 {
2967 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2968 continue;
2970 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2972 status = (*jvm_env)->CallIntMethod (jvm_env,
2973 java_callbacks[i].object, java_callbacks[i].method);
2974 if (status != 0)
2975 {
2976 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2977 java_callbacks[i].name, status);
2978 }
2979 }
2981 return (0);
2982 } /* }}} int cjni_shutdown_plugins */
2985 static int cjni_shutdown (void) /* {{{ */
2986 {
2987 JNIEnv *jvm_env;
2988 JavaVMAttachArgs args;
2989 int status;
2990 size_t i;
2992 if (jvm == NULL)
2993 return (0);
2995 jvm_env = NULL;
2996 memset (&args, 0, sizeof (args));
2997 args.version = JNI_VERSION_1_2;
2999 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
3000 if (status != 0)
3001 {
3002 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
3003 status);
3004 return (-1);
3005 }
3007 /* Execute all the shutdown functions registered by plugins. */
3008 cjni_shutdown_plugins (jvm_env);
3010 /* Release all the global references to callback functions */
3011 for (i = 0; i < java_callbacks_num; i++)
3012 {
3013 if (java_callbacks[i].object != NULL)
3014 {
3015 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3016 java_callbacks[i].object = NULL;
3017 }
3018 sfree (java_callbacks[i].name);
3019 }
3020 java_callbacks_num = 0;
3021 sfree (java_callbacks);
3023 /* Release all the global references to directly loaded classes. */
3024 for (i = 0; i < java_classes_list_len; i++)
3025 {
3026 if (java_classes_list[i].object != NULL)
3027 {
3028 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3029 java_classes_list[i].object = NULL;
3030 }
3031 sfree (java_classes_list[i].name);
3032 }
3033 java_classes_list_len = 0;
3034 sfree (java_classes_list);
3036 /* Destroy the JVM */
3037 DEBUG ("java plugin: Destroying the JVM.");
3038 (*jvm)->DestroyJavaVM (jvm);
3039 jvm = NULL;
3040 jvm_env = NULL;
3042 pthread_key_delete (jvm_env_key);
3044 /* Free the JVM argument list */
3045 for (i = 0; i < jvm_argc; i++)
3046 sfree (jvm_argv[i]);
3047 jvm_argc = 0;
3048 sfree (jvm_argv);
3050 return (0);
3051 } /* }}} int cjni_shutdown */
3053 /* Initialization: Create a JVM, load all configured classes and call their
3054 * `config' and `init' callback methods. */
3055 static int cjni_init (void) /* {{{ */
3056 {
3057 JNIEnv *jvm_env;
3059 if ((config_block == NULL) && (jvm == NULL))
3060 {
3061 ERROR ("java plugin: cjni_init: No configuration block for "
3062 "the java plugin was found.");
3063 return (-1);
3064 }
3066 if (config_block != NULL)
3067 {
3068 int status;
3070 status = cjni_config_perform (config_block);
3071 oconfig_free (config_block);
3072 config_block = NULL;
3073 }
3075 if (jvm == NULL)
3076 {
3077 ERROR ("java plugin: cjni_init: jvm == NULL");
3078 return (-1);
3079 }
3081 jvm_env = cjni_thread_attach ();
3082 if (jvm_env == NULL)
3083 return (-1);
3085 cjni_init_plugins (jvm_env);
3087 cjni_thread_detach ();
3088 return (0);
3089 } /* }}} int cjni_init */
3091 void module_register (void)
3092 {
3093 plugin_register_complex_config ("java", cjni_config_callback);
3094 plugin_register_init ("java", cjni_init);
3095 plugin_register_shutdown ("java", cjni_shutdown);
3096 } /* void module_register (void) */
3098 /* vim: set sw=2 sts=2 et fdm=marker : */