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 the API class \"org.collectd.api"
1935 ".Collectd\". Please set the correct class path "
1936 "using 'JVMArg \"-Djava.class.path=...\"'.");
1937 return (-1);
1938 }
1940 status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1941 jni_api_functions, (jint) jni_api_functions_num);
1942 if (status != 0)
1943 {
1944 ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1945 return (-1);
1946 }
1948 return (0);
1949 } /* }}} int cjni_init_native */
1951 /* Create the JVM. This is called when the first thread tries to access the JVM
1952 * via cjni_thread_attach. */
1953 static int cjni_create_jvm (void) /* {{{ */
1954 {
1955 JNIEnv *jvm_env;
1956 JavaVMInitArgs vm_args;
1957 JavaVMOption vm_options[jvm_argc];
1959 int status;
1960 size_t i;
1962 if (jvm != NULL)
1963 return (0);
1965 status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1966 if (status != 0)
1967 {
1968 ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1969 "with status %i.", status);
1970 return (-1);
1971 }
1973 jvm_env = NULL;
1975 memset (&vm_args, 0, sizeof (vm_args));
1976 vm_args.version = JNI_VERSION_1_2;
1977 vm_args.options = vm_options;
1978 vm_args.nOptions = (jint) jvm_argc;
1980 for (i = 0; i < jvm_argc; i++)
1981 {
1982 DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
1983 i, jvm_argv[i]);
1984 vm_args.options[i].optionString = jvm_argv[i];
1985 }
1987 status = JNI_CreateJavaVM (&jvm, (void *) &jvm_env, (void *) &vm_args);
1988 if (status != 0)
1989 {
1990 ERROR ("java plugin: cjni_create_jvm: "
1991 "JNI_CreateJavaVM failed with status %i.",
1992 status);
1993 return (-1);
1994 }
1995 assert (jvm != NULL);
1996 assert (jvm_env != NULL);
1998 /* Call RegisterNatives */
1999 status = cjni_init_native (jvm_env);
2000 if (status != 0)
2001 {
2002 ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
2003 return (-1);
2004 }
2006 DEBUG ("java plugin: The JVM has been created.");
2007 return (0);
2008 } /* }}} int cjni_create_jvm */
2010 /* Increase the reference counter to the JVM for this thread. If it was zero,
2011 * attach the JVM first. */
2012 static JNIEnv *cjni_thread_attach (void) /* {{{ */
2013 {
2014 cjni_jvm_env_t *cjni_env;
2015 JNIEnv *jvm_env;
2017 /* If we're the first thread to access the JVM, we'll have to create it
2018 * first.. */
2019 if (jvm == NULL)
2020 {
2021 int status;
2023 status = cjni_create_jvm ();
2024 if (status != 0)
2025 {
2026 ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2027 return (NULL);
2028 }
2029 }
2030 assert (jvm != NULL);
2032 cjni_env = pthread_getspecific (jvm_env_key);
2033 if (cjni_env == NULL)
2034 {
2035 /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2036 cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
2037 if (cjni_env == NULL)
2038 {
2039 ERROR ("java plugin: cjni_thread_attach: malloc failed.");
2040 return (NULL);
2041 }
2042 memset (cjni_env, 0, sizeof (*cjni_env));
2043 cjni_env->reference_counter = 0;
2044 cjni_env->jvm_env = NULL;
2046 pthread_setspecific (jvm_env_key, cjni_env);
2047 }
2049 if (cjni_env->reference_counter > 0)
2050 {
2051 cjni_env->reference_counter++;
2052 jvm_env = cjni_env->jvm_env;
2053 }
2054 else
2055 {
2056 int status;
2057 JavaVMAttachArgs args;
2059 assert (cjni_env->jvm_env == NULL);
2061 memset (&args, 0, sizeof (args));
2062 args.version = JNI_VERSION_1_2;
2064 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2065 if (status != 0)
2066 {
2067 ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2068 "with status %i.", status);
2069 return (NULL);
2070 }
2072 cjni_env->reference_counter = 1;
2073 cjni_env->jvm_env = jvm_env;
2074 }
2076 DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2077 cjni_env->reference_counter);
2078 assert (jvm_env != NULL);
2079 return (jvm_env);
2080 } /* }}} JNIEnv *cjni_thread_attach */
2082 /* Decrease the reference counter of this thread. If it reaches zero, detach
2083 * from the JVM. */
2084 static int cjni_thread_detach (void) /* {{{ */
2085 {
2086 cjni_jvm_env_t *cjni_env;
2087 int status;
2089 cjni_env = pthread_getspecific (jvm_env_key);
2090 if (cjni_env == NULL)
2091 {
2092 ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2093 return (-1);
2094 }
2096 assert (cjni_env->reference_counter > 0);
2097 assert (cjni_env->jvm_env != NULL);
2099 cjni_env->reference_counter--;
2100 DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2101 cjni_env->reference_counter);
2103 if (cjni_env->reference_counter > 0)
2104 return (0);
2106 status = (*jvm)->DetachCurrentThread (jvm);
2107 if (status != 0)
2108 {
2109 ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2110 "with status %i.", status);
2111 }
2113 cjni_env->reference_counter = 0;
2114 cjni_env->jvm_env = NULL;
2116 return (0);
2117 } /* }}} JNIEnv *cjni_thread_attach */
2119 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2120 {
2121 char **tmp;
2123 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2124 {
2125 WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2126 return (-1);
2127 }
2129 if (jvm != NULL)
2130 {
2131 ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2132 "`LoadPlugin' options! The JVM is already started and I have to "
2133 "ignore this argument: %s",
2134 ci->values[0].value.string);
2135 return (-1);
2136 }
2138 tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2139 if (tmp == NULL)
2140 {
2141 ERROR ("java plugin: realloc failed.");
2142 return (-1);
2143 }
2144 jvm_argv = tmp;
2146 jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2147 if (jvm_argv[jvm_argc] == NULL)
2148 {
2149 ERROR ("java plugin: strdup failed.");
2150 return (-1);
2151 }
2152 jvm_argc++;
2154 return (0);
2155 } /* }}} int cjni_config_add_jvm_arg */
2157 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2158 {
2159 JNIEnv *jvm_env;
2160 java_plugin_class_t *class;
2161 jmethodID constructor_id;
2162 jobject tmp_object;
2164 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2165 {
2166 WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2167 return (-1);
2168 }
2170 jvm_env = cjni_thread_attach ();
2171 if (jvm_env == NULL)
2172 return (-1);
2174 class = (java_plugin_class_t *) realloc (java_classes_list,
2175 (java_classes_list_len + 1) * sizeof (*java_classes_list));
2176 if (class == NULL)
2177 {
2178 ERROR ("java plugin: realloc failed.");
2179 cjni_thread_detach ();
2180 return (-1);
2181 }
2182 java_classes_list = class;
2183 class = java_classes_list + java_classes_list_len;
2185 memset (class, 0, sizeof (*class));
2186 class->name = strdup (ci->values[0].value.string);
2187 if (class->name == NULL)
2188 {
2189 ERROR ("java plugin: strdup failed.");
2190 cjni_thread_detach ();
2191 return (-1);
2192 }
2193 class->class = NULL;
2194 class->object = NULL;
2196 { /* Replace all dots ('.') with slashes ('/'). Dots are usually used
2197 thorough the Java community, but (Sun's) `FindClass' and friends need
2198 slashes. */
2199 size_t i;
2200 for (i = 0; class->name[i] != 0; i++)
2201 if (class->name[i] == '.')
2202 class->name[i] = '/';
2203 }
2205 DEBUG ("java plugin: Loading class %s", class->name);
2207 class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2208 if (class->class == NULL)
2209 {
2210 ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2211 class->name);
2212 cjni_thread_detach ();
2213 free (class->name);
2214 return (-1);
2215 }
2217 constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2218 "<init>", "()V");
2219 if (constructor_id == NULL)
2220 {
2221 ERROR ("java plugin: cjni_config_load_plugin: "
2222 "Could not find the constructor for `%s'.",
2223 class->name);
2224 cjni_thread_detach ();
2225 free (class->name);
2226 return (-1);
2227 }
2229 tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2230 constructor_id);
2231 if (tmp_object != NULL)
2232 class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2233 else
2234 class->object = NULL;
2235 if (class->object == NULL)
2236 {
2237 ERROR ("java plugin: cjni_config_load_plugin: "
2238 "Could create a new `%s' object.",
2239 class->name);
2240 cjni_thread_detach ();
2241 free (class->name);
2242 return (-1);
2243 }
2245 cjni_thread_detach ();
2247 java_classes_list_len++;
2249 return (0);
2250 } /* }}} int cjni_config_load_plugin */
2252 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2253 {
2254 JNIEnv *jvm_env;
2255 cjni_callback_info_t *cbi;
2256 jobject o_ocitem;
2257 const char *name;
2258 int status;
2259 size_t i;
2261 jclass class;
2262 jmethodID method;
2264 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2265 {
2266 WARNING ("java plugin: `Plugin' blocks "
2267 "need exactly one string argument.");
2268 return (-1);
2269 }
2271 name = ci->values[0].value.string;
2273 cbi = NULL;
2274 for (i = 0; i < java_callbacks_num; i++)
2275 {
2276 if (java_callbacks[i].type != CB_TYPE_CONFIG)
2277 continue;
2279 if (strcmp (name, java_callbacks[i].name) != 0)
2280 continue;
2282 cbi = java_callbacks + i;
2283 break;
2284 }
2286 if (cbi == NULL)
2287 {
2288 NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2289 "configuration callback has been registered. Please make sure, the "
2290 "`LoadPlugin' lines precede the `Plugin' blocks.",
2291 name);
2292 return (0);
2293 }
2295 DEBUG ("java plugin: Configuring %s", name);
2297 jvm_env = cjni_thread_attach ();
2298 if (jvm_env == NULL)
2299 return (-1);
2301 o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2302 if (o_ocitem == NULL)
2303 {
2304 ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2305 cjni_thread_detach ();
2306 return (-1);
2307 }
2309 class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2310 method = (*jvm_env)->GetMethodID (jvm_env, class,
2311 "config", "(Lorg/collectd/api/OConfigItem;)I");
2313 status = (*jvm_env)->CallIntMethod (jvm_env,
2314 cbi->object, method, o_ocitem);
2316 (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2317 cjni_thread_detach ();
2318 return (0);
2319 } /* }}} int cjni_config_plugin_block */
2321 static int cjni_config_perform (oconfig_item_t *ci) /* {{{ */
2322 {
2323 int success;
2324 int errors;
2325 int status;
2326 int i;
2328 success = 0;
2329 errors = 0;
2331 for (i = 0; i < ci->children_num; i++)
2332 {
2333 oconfig_item_t *child = ci->children + i;
2335 if (strcasecmp ("JVMArg", child->key) == 0)
2336 {
2337 status = cjni_config_add_jvm_arg (child);
2338 if (status == 0)
2339 success++;
2340 else
2341 errors++;
2342 }
2343 else if (strcasecmp ("LoadPlugin", child->key) == 0)
2344 {
2345 status = cjni_config_load_plugin (child);
2346 if (status == 0)
2347 success++;
2348 else
2349 errors++;
2350 }
2351 else if (strcasecmp ("Plugin", child->key) == 0)
2352 {
2353 status = cjni_config_plugin_block (child);
2354 if (status == 0)
2355 success++;
2356 else
2357 errors++;
2358 }
2359 else
2360 {
2361 WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2362 errors++;
2363 }
2364 }
2366 DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2367 DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2369 if ((success == 0) && (errors > 0))
2370 {
2371 ERROR ("java plugin: All statements failed.");
2372 return (-1);
2373 }
2375 return (0);
2376 } /* }}} int cjni_config_perform */
2378 /* Copy the children of `ci' to the global `config_block' variable. */
2379 static int cjni_config_callback (oconfig_item_t *ci) /* {{{ */
2380 {
2381 oconfig_item_t *ci_copy;
2382 oconfig_item_t *tmp;
2384 assert (ci != NULL);
2385 if (ci->children_num == 0)
2386 return (0); /* nothing to do */
2388 ci_copy = oconfig_clone (ci);
2389 if (ci_copy == NULL)
2390 {
2391 ERROR ("java plugin: oconfig_clone failed.");
2392 return (-1);
2393 }
2395 if (config_block == NULL)
2396 {
2397 config_block = ci_copy;
2398 return (0);
2399 }
2401 tmp = realloc (config_block->children,
2402 (config_block->children_num + ci_copy->children_num) * sizeof (*tmp));
2403 if (tmp == NULL)
2404 {
2405 ERROR ("java plugin: realloc failed.");
2406 oconfig_free (ci_copy);
2407 return (-1);
2408 }
2409 config_block->children = tmp;
2411 /* Copy the pointers */
2412 memcpy (config_block->children + config_block->children_num,
2413 ci_copy->children,
2414 ci_copy->children_num * sizeof (*ci_copy->children));
2415 config_block->children_num += ci_copy->children_num;
2417 /* Delete the pointers from the copy, so `oconfig_free' can't free them. */
2418 memset (ci_copy->children, 0,
2419 ci_copy->children_num * sizeof (*ci_copy->children));
2420 ci_copy->children_num = 0;
2422 oconfig_free (ci_copy);
2424 return (0);
2425 } /* }}} int cjni_config_callback */
2427 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2428 * and `cjni_write'. In particular, delete the global reference to the Java
2429 * object. */
2430 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2431 {
2432 JNIEnv *jvm_env;
2433 cjni_callback_info_t *cbi;
2435 DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2437 cbi = (cjni_callback_info_t *) arg;
2439 /* This condition can occurr when shutting down. */
2440 if (jvm == NULL)
2441 {
2442 sfree (cbi);
2443 return;
2444 }
2446 if (arg == NULL)
2447 return;
2449 jvm_env = cjni_thread_attach ();
2450 if (jvm_env == NULL)
2451 {
2452 ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2453 return;
2454 }
2456 (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2458 cbi->method = NULL;
2459 cbi->object = NULL;
2460 cbi->class = NULL;
2461 free (cbi);
2463 cjni_thread_detach ();
2464 } /* }}} void cjni_callback_info_destroy */
2466 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2467 static int cjni_read (user_data_t *ud) /* {{{ */
2468 {
2469 JNIEnv *jvm_env;
2470 cjni_callback_info_t *cbi;
2471 int status;
2472 int ret_status;
2474 if (jvm == NULL)
2475 {
2476 ERROR ("java plugin: cjni_read: jvm == NULL");
2477 return (-1);
2478 }
2480 if ((ud == NULL) || (ud->data == NULL))
2481 {
2482 ERROR ("java plugin: cjni_read: Invalid user data.");
2483 return (-1);
2484 }
2486 jvm_env = cjni_thread_attach ();
2487 if (jvm_env == NULL)
2488 return (-1);
2490 cbi = (cjni_callback_info_t *) ud->data;
2492 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2493 cbi->method);
2495 status = cjni_thread_detach ();
2496 if (status != 0)
2497 {
2498 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2499 return (-1);
2500 }
2502 return (ret_status);
2503 } /* }}} int cjni_read */
2505 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2506 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2507 user_data_t *ud)
2508 {
2509 JNIEnv *jvm_env;
2510 cjni_callback_info_t *cbi;
2511 jobject vl_java;
2512 int status;
2513 int ret_status;
2515 if (jvm == NULL)
2516 {
2517 ERROR ("java plugin: cjni_write: jvm == NULL");
2518 return (-1);
2519 }
2521 if ((ud == NULL) || (ud->data == NULL))
2522 {
2523 ERROR ("java plugin: cjni_write: Invalid user data.");
2524 return (-1);
2525 }
2527 jvm_env = cjni_thread_attach ();
2528 if (jvm_env == NULL)
2529 return (-1);
2531 cbi = (cjni_callback_info_t *) ud->data;
2533 vl_java = ctoj_value_list (jvm_env, ds, vl);
2534 if (vl_java == NULL)
2535 {
2536 ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2537 return (-1);
2538 }
2540 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2541 cbi->object, cbi->method, vl_java);
2543 (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2545 status = cjni_thread_detach ();
2546 if (status != 0)
2547 {
2548 ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2549 return (-1);
2550 }
2552 return (ret_status);
2553 } /* }}} int cjni_write */
2555 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2556 static int cjni_flush (int timeout, const char *identifier, /* {{{ */
2557 user_data_t *ud)
2558 {
2559 JNIEnv *jvm_env;
2560 cjni_callback_info_t *cbi;
2561 jobject o_identifier;
2562 int status;
2563 int ret_status;
2565 if (jvm == NULL)
2566 {
2567 ERROR ("java plugin: cjni_flush: jvm == NULL");
2568 return (-1);
2569 }
2571 if ((ud == NULL) || (ud->data == NULL))
2572 {
2573 ERROR ("java plugin: cjni_flush: Invalid user data.");
2574 return (-1);
2575 }
2577 jvm_env = cjni_thread_attach ();
2578 if (jvm_env == NULL)
2579 return (-1);
2581 cbi = (cjni_callback_info_t *) ud->data;
2583 o_identifier = NULL;
2584 if (identifier != NULL)
2585 {
2586 o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2587 if (o_identifier == NULL)
2588 {
2589 ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2590 return (-1);
2591 }
2592 }
2594 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2595 cbi->object, cbi->method, (jint) timeout, o_identifier);
2597 (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2599 status = cjni_thread_detach ();
2600 if (status != 0)
2601 {
2602 ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2603 return (-1);
2604 }
2606 return (ret_status);
2607 } /* }}} int cjni_flush */
2609 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2610 static void cjni_log (int severity, const char *message, /* {{{ */
2611 user_data_t *ud)
2612 {
2613 JNIEnv *jvm_env;
2614 cjni_callback_info_t *cbi;
2615 jobject o_message;
2617 if (jvm == NULL)
2618 return;
2620 if ((ud == NULL) || (ud->data == NULL))
2621 return;
2623 jvm_env = cjni_thread_attach ();
2624 if (jvm_env == NULL)
2625 return;
2627 cbi = (cjni_callback_info_t *) ud->data;
2629 o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2630 if (o_message == NULL)
2631 return;
2633 (*jvm_env)->CallVoidMethod (jvm_env,
2634 cbi->object, cbi->method, (jint) severity, o_message);
2636 (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2638 cjni_thread_detach ();
2639 } /* }}} void cjni_log */
2641 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2642 * pointer. */
2643 static int cjni_notification (const notification_t *n, /* {{{ */
2644 user_data_t *ud)
2645 {
2646 JNIEnv *jvm_env;
2647 cjni_callback_info_t *cbi;
2648 jobject o_notification;
2649 int status;
2650 int ret_status;
2652 if (jvm == NULL)
2653 {
2654 ERROR ("java plugin: cjni_read: jvm == NULL");
2655 return (-1);
2656 }
2658 if ((ud == NULL) || (ud->data == NULL))
2659 {
2660 ERROR ("java plugin: cjni_read: Invalid user data.");
2661 return (-1);
2662 }
2664 jvm_env = cjni_thread_attach ();
2665 if (jvm_env == NULL)
2666 return (-1);
2668 cbi = (cjni_callback_info_t *) ud->data;
2670 o_notification = ctoj_notification (jvm_env, n);
2671 if (o_notification == NULL)
2672 {
2673 ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2674 return (-1);
2675 }
2677 ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2678 cbi->object, cbi->method, o_notification);
2680 (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2682 status = cjni_thread_detach ();
2683 if (status != 0)
2684 {
2685 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2686 return (-1);
2687 }
2689 return (ret_status);
2690 } /* }}} int cjni_notification */
2692 /* Callbacks for matches implemented in Java */
2693 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2694 void **user_data)
2695 {
2696 JNIEnv *jvm_env;
2697 cjni_callback_info_t *cbi_ret;
2698 cjni_callback_info_t *cbi_factory;
2699 const char *name;
2700 jobject o_ci;
2701 jobject o_tmp;
2702 int type;
2703 size_t i;
2705 cbi_ret = NULL;
2706 o_ci = NULL;
2707 jvm_env = NULL;
2709 #define BAIL_OUT(status) \
2710 if (cbi_ret != NULL) { \
2711 free (cbi_ret->name); \
2712 if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2713 (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2714 } \
2715 free (cbi_ret); \
2716 if (jvm_env != NULL) { \
2717 if (o_ci != NULL) \
2718 (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2719 cjni_thread_detach (); \
2720 } \
2721 return (status)
2723 if (jvm == NULL)
2724 {
2725 ERROR ("java plugin: cjni_read: jvm == NULL");
2726 BAIL_OUT (-1);
2727 }
2729 jvm_env = cjni_thread_attach ();
2730 if (jvm_env == NULL)
2731 {
2732 BAIL_OUT (-1);
2733 }
2735 /* Find out whether to create a match or a target. */
2736 if (strcasecmp ("Match", ci->key) == 0)
2737 type = CB_TYPE_MATCH;
2738 else if (strcasecmp ("Target", ci->key) == 0)
2739 type = CB_TYPE_TARGET;
2740 else
2741 {
2742 ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2743 "to create a match or a target.");
2744 BAIL_OUT (-1);
2745 }
2747 /* This is the name of the match we should create. */
2748 name = ci->values[0].value.string;
2750 /* Lets see if we have a matching factory here.. */
2751 cbi_factory = NULL;
2752 for (i = 0; i < java_callbacks_num; i++)
2753 {
2754 if (java_callbacks[i].type != type)
2755 continue;
2757 if (strcmp (name, java_callbacks[i].name) != 0)
2758 continue;
2760 cbi_factory = java_callbacks + i;
2761 break;
2762 }
2764 /* Nope, no factory for that name.. */
2765 if (cbi_factory == NULL)
2766 {
2767 ERROR ("java plugin: cjni_match_target_create: "
2768 "No such match factory registered: %s",
2769 name);
2770 BAIL_OUT (-1);
2771 }
2773 /* We convert `ci' to its Java equivalent.. */
2774 o_ci = ctoj_oconfig_item (jvm_env, ci);
2775 if (o_ci == NULL)
2776 {
2777 ERROR ("java plugin: cjni_match_target_create: "
2778 "ctoj_oconfig_item failed.");
2779 BAIL_OUT (-1);
2780 }
2782 /* Allocate a new callback info structure. This is going to be our user_data
2783 * pointer. */
2784 cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
2785 if (cbi_ret == NULL)
2786 {
2787 ERROR ("java plugin: cjni_match_target_create: malloc failed.");
2788 BAIL_OUT (-1);
2789 }
2790 memset (cbi_ret, 0, sizeof (*cbi_ret));
2791 cbi_ret->object = NULL;
2792 cbi_ret->type = type;
2794 /* Lets fill the callback info structure.. First, the name: */
2795 cbi_ret->name = strdup (name);
2796 if (cbi_ret->name == NULL)
2797 {
2798 ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2799 BAIL_OUT (-1);
2800 }
2802 /* Then call the factory method so it creates a new object for us. */
2803 o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2804 cbi_factory->object, cbi_factory->method, o_ci);
2805 if (o_tmp == NULL)
2806 {
2807 ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2808 BAIL_OUT (-1);
2809 }
2811 cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2812 if (o_tmp == NULL)
2813 {
2814 ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2815 BAIL_OUT (-1);
2816 }
2818 /* This is the class of the match. It is possibly different from the class of
2819 * the match-factory! */
2820 cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2821 if (cbi_ret->class == NULL)
2822 {
2823 ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2824 BAIL_OUT (-1);
2825 }
2827 /* Lookup the `int match (DataSet, ValueList)' method. */
2828 cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2829 /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2830 "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2831 if (cbi_ret->method == NULL)
2832 {
2833 ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2834 BAIL_OUT (-1);
2835 }
2837 /* Return the newly created match via the user_data pointer. */
2838 *user_data = (void *) cbi_ret;
2840 cjni_thread_detach ();
2842 DEBUG ("java plugin: cjni_match_target_create: "
2843 "Successfully created a `%s' %s.",
2844 cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2846 /* Success! */
2847 return (0);
2848 #undef BAIL_OUT
2849 } /* }}} int cjni_match_target_create */
2851 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2852 {
2853 cjni_callback_info_destroy (*user_data);
2854 *user_data = NULL;
2856 return (0);
2857 } /* }}} int cjni_match_target_destroy */
2859 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2860 value_list_t *vl, notification_meta_t **meta, void **user_data)
2861 {
2862 JNIEnv *jvm_env;
2863 cjni_callback_info_t *cbi;
2864 jobject o_vl;
2865 jobject o_ds;
2866 int ret_status;
2867 int status;
2869 if (jvm == NULL)
2870 {
2871 ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2872 return (-1);
2873 }
2875 jvm_env = cjni_thread_attach ();
2876 if (jvm_env == NULL)
2877 return (-1);
2879 cbi = (cjni_callback_info_t *) *user_data;
2881 o_vl = ctoj_value_list (jvm_env, ds, vl);
2882 if (o_vl == NULL)
2883 {
2884 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2885 cjni_thread_detach ();
2886 return (-1);
2887 }
2889 o_ds = ctoj_data_set (jvm_env, ds);
2890 if (o_ds == NULL)
2891 {
2892 ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2893 cjni_thread_detach ();
2894 return (-1);
2895 }
2897 ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2898 o_ds, o_vl);
2900 DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2902 /* If we're executing a target, copy the `ValueList' back to our
2903 * `value_list_t'. */
2904 if (cbi->type == CB_TYPE_TARGET)
2905 {
2906 value_list_t new_vl;
2908 memset (&new_vl, 0, sizeof (new_vl));
2909 status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2910 if (status != 0)
2911 {
2912 ERROR ("java plugin: cjni_match_target_invoke: "
2913 "jtoc_value_list failed.");
2914 }
2915 else /* if (status == 0) */
2916 {
2917 /* plugin_dispatch_values assures that this is dynamically allocated
2918 * memory. */
2919 sfree (vl->values);
2921 /* This will replace the vl->values pointer to a new, dynamically
2922 * allocated piece of memory. */
2923 memcpy (vl, &new_vl, sizeof (*vl));
2924 }
2925 } /* if (cbi->type == CB_TYPE_TARGET) */
2927 status = cjni_thread_detach ();
2928 if (status != 0)
2929 ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2931 return (ret_status);
2932 } /* }}} int cjni_match_target_invoke */
2934 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2935 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2936 {
2937 int status;
2938 size_t i;
2940 for (i = 0; i < java_callbacks_num; i++)
2941 {
2942 if (java_callbacks[i].type != CB_TYPE_INIT)
2943 continue;
2945 DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2947 status = (*jvm_env)->CallIntMethod (jvm_env,
2948 java_callbacks[i].object, java_callbacks[i].method);
2949 if (status != 0)
2950 {
2951 ERROR ("java plugin: Initializing `%s' failed with status %i. "
2952 "Removing read function.",
2953 java_callbacks[i].name, status);
2954 plugin_unregister_read (java_callbacks[i].name);
2955 }
2956 }
2958 return (0);
2959 } /* }}} int cjni_init_plugins */
2961 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2962 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2963 {
2964 int status;
2965 size_t i;
2967 for (i = 0; i < java_callbacks_num; i++)
2968 {
2969 if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2970 continue;
2972 DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2974 status = (*jvm_env)->CallIntMethod (jvm_env,
2975 java_callbacks[i].object, java_callbacks[i].method);
2976 if (status != 0)
2977 {
2978 ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2979 java_callbacks[i].name, status);
2980 }
2981 }
2983 return (0);
2984 } /* }}} int cjni_shutdown_plugins */
2987 static int cjni_shutdown (void) /* {{{ */
2988 {
2989 JNIEnv *jvm_env;
2990 JavaVMAttachArgs args;
2991 int status;
2992 size_t i;
2994 if (jvm == NULL)
2995 return (0);
2997 jvm_env = NULL;
2998 memset (&args, 0, sizeof (args));
2999 args.version = JNI_VERSION_1_2;
3001 status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, &args);
3002 if (status != 0)
3003 {
3004 ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
3005 status);
3006 return (-1);
3007 }
3009 /* Execute all the shutdown functions registered by plugins. */
3010 cjni_shutdown_plugins (jvm_env);
3012 /* Release all the global references to callback functions */
3013 for (i = 0; i < java_callbacks_num; i++)
3014 {
3015 if (java_callbacks[i].object != NULL)
3016 {
3017 (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
3018 java_callbacks[i].object = NULL;
3019 }
3020 sfree (java_callbacks[i].name);
3021 }
3022 java_callbacks_num = 0;
3023 sfree (java_callbacks);
3025 /* Release all the global references to directly loaded classes. */
3026 for (i = 0; i < java_classes_list_len; i++)
3027 {
3028 if (java_classes_list[i].object != NULL)
3029 {
3030 (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
3031 java_classes_list[i].object = NULL;
3032 }
3033 sfree (java_classes_list[i].name);
3034 }
3035 java_classes_list_len = 0;
3036 sfree (java_classes_list);
3038 /* Destroy the JVM */
3039 DEBUG ("java plugin: Destroying the JVM.");
3040 (*jvm)->DestroyJavaVM (jvm);
3041 jvm = NULL;
3042 jvm_env = NULL;
3044 pthread_key_delete (jvm_env_key);
3046 /* Free the JVM argument list */
3047 for (i = 0; i < jvm_argc; i++)
3048 sfree (jvm_argv[i]);
3049 jvm_argc = 0;
3050 sfree (jvm_argv);
3052 return (0);
3053 } /* }}} int cjni_shutdown */
3055 /* Initialization: Create a JVM, load all configured classes and call their
3056 * `config' and `init' callback methods. */
3057 static int cjni_init (void) /* {{{ */
3058 {
3059 JNIEnv *jvm_env;
3061 if ((config_block == NULL) && (jvm == NULL))
3062 {
3063 ERROR ("java plugin: cjni_init: No configuration block for "
3064 "the java plugin was found.");
3065 return (-1);
3066 }
3068 if (config_block != NULL)
3069 {
3070 int status;
3072 status = cjni_config_perform (config_block);
3073 oconfig_free (config_block);
3074 config_block = NULL;
3075 }
3077 if (jvm == NULL)
3078 {
3079 ERROR ("java plugin: cjni_init: jvm == NULL");
3080 return (-1);
3081 }
3083 jvm_env = cjni_thread_attach ();
3084 if (jvm_env == NULL)
3085 return (-1);
3087 cjni_init_plugins (jvm_env);
3089 cjni_thread_detach ();
3090 return (0);
3091 } /* }}} int cjni_init */
3093 void module_register (void)
3094 {
3095 plugin_register_complex_config ("java", cjni_config_callback);
3096 plugin_register_init ("java", cjni_init);
3097 plugin_register_shutdown ("java", cjni_shutdown);
3098 } /* void module_register (void) */
3100 /* vim: set sw=2 sts=2 et fdm=marker : */