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