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