java plugin: Fix a typo in the oconfig_item conversion function.
[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_addchild, 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,
1411       /* interval = */ NULL, &ud);
1412
1413   (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
1414
1415   return (0);
1416 } /* }}} jint cjni_api_register_read */
1417
1418 static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
1419     jobject this, jobject o_name, jobject o_write)
1420 {
1421   user_data_t ud;
1422   cjni_callback_info_t *cbi;
1423
1424   cbi = cjni_callback_info_create (jvm_env, o_name, o_write, CB_TYPE_WRITE);
1425   if (cbi == NULL)
1426     return (-1);
1427
1428   DEBUG ("java plugin: Registering new write callback: %s", cbi->name);
1429
1430   memset (&ud, 0, sizeof (ud));
1431   ud.data = (void *) cbi;
1432   ud.free_func = cjni_callback_info_destroy;
1433
1434   plugin_register_write (cbi->name, cjni_write, &ud);
1435
1436   (*jvm_env)->DeleteLocalRef (jvm_env, o_write);
1437
1438   return (0);
1439 } /* }}} jint cjni_api_register_write */
1440
1441 static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
1442     jobject this, jobject o_name, jobject o_flush)
1443 {
1444   user_data_t ud;
1445   cjni_callback_info_t *cbi;
1446
1447   cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
1448   if (cbi == NULL)
1449     return (-1);
1450
1451   DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
1452
1453   memset (&ud, 0, sizeof (ud));
1454   ud.data = (void *) cbi;
1455   ud.free_func = cjni_callback_info_destroy;
1456
1457   plugin_register_flush (cbi->name, cjni_flush, &ud);
1458
1459   (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
1460
1461   return (0);
1462 } /* }}} jint cjni_api_register_flush */
1463
1464 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
1465     jobject this, jobject o_name, jobject o_shutdown)
1466 {
1467   return (cjni_callback_register (jvm_env, o_name, o_shutdown,
1468         CB_TYPE_SHUTDOWN));
1469 } /* }}} jint cjni_api_register_shutdown */
1470
1471 static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
1472     jobject this, jobject o_name, jobject o_log)
1473 {
1474   user_data_t ud;
1475   cjni_callback_info_t *cbi;
1476
1477   cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
1478   if (cbi == NULL)
1479     return (-1);
1480
1481   DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
1482
1483   memset (&ud, 0, sizeof (ud));
1484   ud.data = (void *) cbi;
1485   ud.free_func = cjni_callback_info_destroy;
1486
1487   plugin_register_log (cbi->name, cjni_log, &ud);
1488
1489   (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
1490
1491   return (0);
1492 } /* }}} jint cjni_api_register_log */
1493
1494 static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
1495     jobject this, jobject o_name, jobject o_notification)
1496 {
1497   user_data_t ud;
1498   cjni_callback_info_t *cbi;
1499
1500   cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
1501       CB_TYPE_NOTIFICATION);
1502   if (cbi == NULL)
1503     return (-1);
1504
1505   DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
1506
1507   memset (&ud, 0, sizeof (ud));
1508   ud.data = (void *) cbi;
1509   ud.free_func = cjni_callback_info_destroy;
1510
1511   plugin_register_notification (cbi->name, cjni_notification, &ud);
1512
1513   (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
1514
1515   return (0);
1516 } /* }}} jint cjni_api_register_notification */
1517
1518 static jint JNICALL cjni_api_register_match_target (JNIEnv *jvm_env, /* {{{ */
1519     jobject this, jobject o_name, jobject o_match, int type)
1520 {
1521   int status;
1522   const char *c_name;
1523
1524   c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1525   if (c_name == NULL)
1526   {
1527     ERROR ("java plugin: cjni_api_register_match_target: "
1528         "GetStringUTFChars failed.");
1529     return (-1);
1530   }
1531
1532   status = cjni_callback_register (jvm_env, o_name, o_match, type);
1533   if (status != 0)
1534   {
1535     (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1536     return (-1);
1537   }
1538
1539   if (type == CB_TYPE_MATCH)
1540   {
1541     match_proc_t m_proc;
1542
1543     memset (&m_proc, 0, sizeof (m_proc));
1544     m_proc.create  = cjni_match_target_create;
1545     m_proc.destroy = cjni_match_target_destroy;
1546     m_proc.match   = (void *) cjni_match_target_invoke;
1547
1548     status = fc_register_match (c_name, m_proc);
1549   }
1550   else if (type == CB_TYPE_TARGET)
1551   {
1552     target_proc_t t_proc;
1553
1554     memset (&t_proc, 0, sizeof (t_proc));
1555     t_proc.create  = cjni_match_target_create;
1556     t_proc.destroy = cjni_match_target_destroy;
1557     t_proc.invoke  = cjni_match_target_invoke;
1558
1559     status = fc_register_target (c_name, t_proc);
1560   }
1561   else
1562   {
1563     ERROR ("java plugin: cjni_api_register_match_target: "
1564         "Don't know whether to create a match or a target.");
1565     (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1566     return (-1);
1567   }
1568
1569   if (status != 0)
1570   {
1571     ERROR ("java plugin: cjni_api_register_match_target: "
1572         "%s failed.",
1573         (type == CB_TYPE_MATCH) ? "fc_register_match" : "fc_register_target");
1574     (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1575     return (-1);
1576   }
1577
1578   (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1579
1580   return (0);
1581 } /* }}} jint cjni_api_register_match_target */
1582
1583 static jint JNICALL cjni_api_register_match (JNIEnv *jvm_env, /* {{{ */
1584     jobject this, jobject o_name, jobject o_match)
1585 {
1586   return (cjni_api_register_match_target (jvm_env, this, o_name, o_match,
1587         CB_TYPE_MATCH));
1588 } /* }}} jint cjni_api_register_match */
1589
1590 static jint JNICALL cjni_api_register_target (JNIEnv *jvm_env, /* {{{ */
1591     jobject this, jobject o_name, jobject o_target)
1592 {
1593   return (cjni_api_register_match_target (jvm_env, this, o_name, o_target,
1594         CB_TYPE_TARGET));
1595 } /* }}} jint cjni_api_register_target */
1596
1597 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
1598     jobject this, jint severity, jobject o_message)
1599 {
1600   const char *c_str;
1601
1602   c_str = (*jvm_env)->GetStringUTFChars (jvm_env, o_message, 0);
1603   if (c_str == NULL)
1604   {
1605     ERROR ("java plugin: cjni_api_log: GetStringUTFChars failed.");
1606     return;
1607   }
1608
1609   if (severity < LOG_ERR)
1610     severity = LOG_ERR;
1611   if (severity > LOG_DEBUG)
1612     severity = LOG_DEBUG;
1613
1614   plugin_log (severity, "%s", c_str);
1615
1616   (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_message, c_str);
1617 } /* }}} void cjni_api_log */
1618
1619 /* List of ``native'' functions, i. e. C-functions that can be called from
1620  * Java. */
1621 static JNINativeMethod jni_api_functions[] = /* {{{ */
1622 {
1623   { "dispatchValues",
1624     "(Lorg/collectd/api/ValueList;)I",
1625     cjni_api_dispatch_values },
1626
1627   { "dispatchNotification",
1628     "(Lorg/collectd/api/Notification;)I",
1629     cjni_api_dispatch_notification },
1630
1631   { "getDS",
1632     "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
1633     cjni_api_get_ds },
1634
1635   { "registerConfig",
1636     "(Ljava/lang/String;Lorg/collectd/api/CollectdConfigInterface;)I",
1637     cjni_api_register_config },
1638
1639   { "registerInit",
1640     "(Ljava/lang/String;Lorg/collectd/api/CollectdInitInterface;)I",
1641     cjni_api_register_init },
1642
1643   { "registerRead",
1644     "(Ljava/lang/String;Lorg/collectd/api/CollectdReadInterface;)I",
1645     cjni_api_register_read },
1646
1647   { "registerWrite",
1648     "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
1649     cjni_api_register_write },
1650
1651   { "registerFlush",
1652     "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
1653     cjni_api_register_flush },
1654
1655   { "registerShutdown",
1656     "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
1657     cjni_api_register_shutdown },
1658
1659   { "registerLog",
1660     "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
1661     cjni_api_register_log },
1662
1663   { "registerNotification",
1664     "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
1665     cjni_api_register_notification },
1666
1667   { "registerMatch",
1668     "(Ljava/lang/String;Lorg/collectd/api/CollectdMatchFactoryInterface;)I",
1669     cjni_api_register_match },
1670
1671   { "registerTarget",
1672     "(Ljava/lang/String;Lorg/collectd/api/CollectdTargetFactoryInterface;)I",
1673     cjni_api_register_target },
1674
1675   { "log",
1676     "(ILjava/lang/String;)V",
1677     cjni_api_log },
1678 };
1679 static size_t jni_api_functions_num = sizeof (jni_api_functions)
1680   / sizeof (jni_api_functions[0]);
1681 /* }}} */
1682
1683 /*
1684  * Functions
1685  */
1686 /* Allocate a `cjni_callback_info_t' given the type and objects necessary for
1687  * all registration functions. */
1688 static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ */
1689     jobject o_name, jobject o_callback, int type)
1690 {
1691   const char *c_name;
1692   cjni_callback_info_t *cbi;
1693   const char *method_name;
1694   const char *method_signature;
1695
1696   switch (type)
1697   {
1698     case CB_TYPE_CONFIG:
1699       method_name = "config";
1700       method_signature = "(Lorg/collectd/api/OConfigItem;)I";
1701       break;
1702
1703     case CB_TYPE_INIT:
1704       method_name = "init";
1705       method_signature = "()I";
1706       break;
1707
1708     case CB_TYPE_READ:
1709       method_name = "read";
1710       method_signature = "()I";
1711       break;
1712
1713     case CB_TYPE_WRITE:
1714       method_name = "write";
1715       method_signature = "(Lorg/collectd/api/ValueList;)I";
1716       break;
1717
1718     case CB_TYPE_FLUSH:
1719       method_name = "flush";
1720       method_signature = "(ILjava/lang/String;)I";
1721       break;
1722
1723     case CB_TYPE_SHUTDOWN:
1724       method_name = "shutdown";
1725       method_signature = "()I";
1726       break;
1727
1728     case CB_TYPE_LOG:
1729       method_name = "log";
1730       method_signature = "(ILjava/lang/String;)V";
1731       break;
1732
1733     case CB_TYPE_NOTIFICATION:
1734       method_name = "notification";
1735       method_signature = "(Lorg/collectd/api/Notification;)I";
1736       break;
1737
1738     case CB_TYPE_MATCH:
1739       method_name = "createMatch";
1740       method_signature = "(Lorg/collectd/api/OConfigItem;)"
1741         "Lorg/collectd/api/CollectdMatchInterface;";
1742       break;
1743
1744     case CB_TYPE_TARGET:
1745       method_name = "createTarget";
1746       method_signature = "(Lorg/collectd/api/OConfigItem;)"
1747         "Lorg/collectd/api/CollectdTargetInterface;";
1748       break;
1749
1750     default:
1751       ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
1752           type);
1753       return (NULL);
1754   }
1755
1756   c_name = (*jvm_env)->GetStringUTFChars (jvm_env, o_name, 0);
1757   if (c_name == NULL)
1758   {
1759     ERROR ("java plugin: cjni_callback_info_create: "
1760         "GetStringUTFChars failed.");
1761     return (NULL);
1762   }
1763
1764   cbi = (cjni_callback_info_t *) malloc (sizeof (*cbi));
1765   if (cbi == NULL)
1766   {
1767     ERROR ("java plugin: cjni_callback_info_create: malloc failed.");
1768     (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1769     return (NULL);
1770   }
1771   memset (cbi, 0, sizeof (*cbi));
1772   cbi->type = type;
1773
1774   cbi->name = strdup (c_name);
1775   if (cbi->name == NULL)
1776   {
1777     pthread_mutex_unlock (&java_callbacks_lock);
1778     ERROR ("java plugin: cjni_callback_info_create: strdup failed.");
1779     (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1780     return (NULL);
1781   }
1782
1783   (*jvm_env)->ReleaseStringUTFChars (jvm_env, o_name, c_name);
1784
1785   cbi->object = (*jvm_env)->NewGlobalRef (jvm_env, o_callback);
1786   if (cbi->object == NULL)
1787   {
1788     ERROR ("java plugin: cjni_callback_info_create: NewGlobalRef failed.");
1789     free (cbi);
1790     return (NULL);
1791   }
1792
1793   cbi->class  = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
1794   if (cbi->class == NULL)
1795   {
1796     ERROR ("java plugin: cjni_callback_info_create: GetObjectClass failed.");
1797     free (cbi);
1798     return (NULL);
1799   }
1800
1801   cbi->method = (*jvm_env)->GetMethodID (jvm_env, cbi->class,
1802       method_name, method_signature);
1803   if (cbi->method == NULL)
1804   {
1805     ERROR ("java plugin: cjni_callback_info_create: "
1806         "Cannot find the `%s' method with signature `%s'.",
1807         method_name, method_signature);
1808     free (cbi);
1809     return (NULL);
1810   }
1811
1812   return (cbi);
1813 } /* }}} cjni_callback_info_t cjni_callback_info_create */
1814
1815 /* Allocate a `cjni_callback_info_t' via `cjni_callback_info_create' and add it
1816  * to the global `java_callbacks' variable. This is used for `config', `init',
1817  * and `shutdown' callbacks. */
1818 static int cjni_callback_register (JNIEnv *jvm_env, /* {{{ */
1819     jobject o_name, jobject o_callback, int type)
1820 {
1821   cjni_callback_info_t *cbi;
1822   cjni_callback_info_t *tmp;
1823 #if COLLECT_DEBUG
1824   const char *type_str;
1825 #endif
1826
1827   cbi = cjni_callback_info_create (jvm_env, o_name, o_callback, type);
1828   if (cbi == NULL)
1829     return (-1);
1830
1831 #if COLLECT_DEBUG
1832   switch (type)
1833   {
1834     case CB_TYPE_CONFIG:
1835       type_str = "config";
1836       break;
1837
1838     case CB_TYPE_INIT:
1839       type_str = "init";
1840       break;
1841
1842     case CB_TYPE_SHUTDOWN:
1843       type_str = "shutdown";
1844       break;
1845
1846     case CB_TYPE_MATCH:
1847       type_str = "match";
1848       break;
1849
1850     case CB_TYPE_TARGET:
1851       type_str = "target";
1852       break;
1853
1854     default:
1855       type_str = "<unknown>";
1856   }
1857   DEBUG ("java plugin: Registering new %s callback: %s",
1858       type_str, cbi->name);
1859 #endif
1860
1861   pthread_mutex_lock (&java_callbacks_lock);
1862
1863   tmp = (cjni_callback_info_t *) realloc (java_callbacks,
1864       (java_callbacks_num + 1) * sizeof (*java_callbacks));
1865   if (tmp == NULL)
1866   {
1867     pthread_mutex_unlock (&java_callbacks_lock);
1868     ERROR ("java plugin: cjni_callback_register: realloc failed.");
1869
1870     (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
1871     free (cbi);
1872
1873     return (-1);
1874   }
1875   java_callbacks = tmp;
1876   java_callbacks[java_callbacks_num] = *cbi;
1877   java_callbacks_num++;
1878
1879   pthread_mutex_unlock (&java_callbacks_lock);
1880
1881   free (cbi);
1882   return (0);
1883 } /* }}} int cjni_callback_register */
1884
1885 /* Callback for `pthread_key_create'. It frees the data contained in
1886  * `jvm_env_key' and prints a warning if the reference counter is not zero. */
1887 static void cjni_jvm_env_destroy (void *args) /* {{{ */
1888 {
1889   cjni_jvm_env_t *cjni_env;
1890
1891   if (args == NULL)
1892     return;
1893
1894   cjni_env = (cjni_jvm_env_t *) args;
1895
1896   if (cjni_env->reference_counter > 0)
1897   {
1898     ERROR ("java plugin: cjni_jvm_env_destroy: "
1899         "cjni_env->reference_counter = %i;", cjni_env->reference_counter);
1900   }
1901
1902   if (cjni_env->jvm_env != NULL)
1903   {
1904     ERROR ("java plugin: cjni_jvm_env_destroy: cjni_env->jvm_env = %p;",
1905         (void *) cjni_env->jvm_env);
1906   }
1907
1908   /* The pointer is allocated in `cjni_thread_attach' */
1909   free (cjni_env);
1910 } /* }}} void cjni_jvm_env_destroy */
1911
1912 /* Register ``native'' functions with the JVM. Native functions are C-functions
1913  * that can be called by Java code. */
1914 static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
1915 {
1916   jclass api_class_ptr;
1917   int status;
1918
1919   api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org/collectd/api/Collectd");
1920   if (api_class_ptr == NULL)
1921   {
1922     ERROR ("cjni_init_native: Cannot find API class `org/collectd/api/Collectd'.");
1923     return (-1);
1924   }
1925
1926   status = (*jvm_env)->RegisterNatives (jvm_env, api_class_ptr,
1927       jni_api_functions, (jint) jni_api_functions_num);
1928   if (status != 0)
1929   {
1930     ERROR ("cjni_init_native: RegisterNatives failed with status %i.", status);
1931     return (-1);
1932   }
1933
1934   return (0);
1935 } /* }}} int cjni_init_native */
1936
1937 /* Create the JVM. This is called when the first thread tries to access the JVM
1938  * via cjni_thread_attach. */
1939 static int cjni_create_jvm (void) /* {{{ */
1940 {
1941   JNIEnv *jvm_env;
1942   JavaVMInitArgs vm_args;
1943   JavaVMOption vm_options[jvm_argc];
1944
1945   int status;
1946   size_t i;
1947
1948   if (jvm != NULL)
1949     return (0);
1950
1951   status = pthread_key_create (&jvm_env_key, cjni_jvm_env_destroy);
1952   if (status != 0)
1953   {
1954     ERROR ("java plugin: cjni_create_jvm: pthread_key_create failed "
1955         "with status %i.", status);
1956     return (-1);
1957   }
1958
1959   jvm_env = NULL;
1960
1961   memset (&vm_args, 0, sizeof (vm_args));
1962   vm_args.version = JNI_VERSION_1_2;
1963   vm_args.options = vm_options;
1964   vm_args.nOptions = (jint) jvm_argc;
1965
1966   for (i = 0; i < jvm_argc; i++)
1967   {
1968     DEBUG ("java plugin: cjni_create_jvm: jvm_argv[%zu] = %s",
1969         i, jvm_argv[i]);
1970     vm_args.options[i].optionString = jvm_argv[i];
1971   }
1972
1973   status = JNI_CreateJavaVM (&jvm, (void **) &jvm_env, (void **) &vm_args);
1974   if (status != 0)
1975   {
1976     ERROR ("java plugin: cjni_create_jvm: "
1977         "JNI_CreateJavaVM failed with status %i.",
1978         status);
1979     return (-1);
1980   }
1981   assert (jvm != NULL);
1982   assert (jvm_env != NULL);
1983
1984   /* Call RegisterNatives */
1985   status = cjni_init_native (jvm_env);
1986   if (status != 0)
1987   {
1988     ERROR ("java plugin: cjni_create_jvm: cjni_init_native failed.");
1989     return (-1);
1990   }
1991
1992   DEBUG ("java plugin: The JVM has been created.");
1993   return (0);
1994 } /* }}} int cjni_create_jvm */
1995
1996 /* Increase the reference counter to the JVM for this thread. If it was zero,
1997  * attach the JVM first. */
1998 static JNIEnv *cjni_thread_attach (void) /* {{{ */
1999 {
2000   cjni_jvm_env_t *cjni_env;
2001   JNIEnv *jvm_env;
2002
2003   /* If we're the first thread to access the JVM, we'll have to create it
2004    * first.. */
2005   if (jvm == NULL)
2006   {
2007     int status;
2008
2009     status = cjni_create_jvm ();
2010     if (status != 0)
2011     {
2012       ERROR ("java plugin: cjni_thread_attach: cjni_create_jvm failed.");
2013       return (NULL);
2014     }
2015   }
2016   assert (jvm != NULL);
2017
2018   cjni_env = pthread_getspecific (jvm_env_key);
2019   if (cjni_env == NULL)
2020   {
2021     /* This pointer is free'd in `cjni_jvm_env_destroy'. */
2022     cjni_env = (cjni_jvm_env_t *) malloc (sizeof (*cjni_env));
2023     if (cjni_env == NULL)
2024     {
2025       ERROR ("java plugin: cjni_thread_attach: malloc failed.");
2026       return (NULL);
2027     }
2028     memset (cjni_env, 0, sizeof (*cjni_env));
2029     cjni_env->reference_counter = 0;
2030     cjni_env->jvm_env = NULL;
2031
2032     pthread_setspecific (jvm_env_key, cjni_env);
2033   }
2034
2035   if (cjni_env->reference_counter > 0)
2036   {
2037     cjni_env->reference_counter++;
2038     jvm_env = cjni_env->jvm_env;
2039   }
2040   else
2041   {
2042     int status;
2043     JavaVMAttachArgs args;
2044
2045     assert (cjni_env->jvm_env == NULL);
2046
2047     memset (&args, 0, sizeof (args));
2048     args.version = JNI_VERSION_1_2;
2049
2050     status = (*jvm)->AttachCurrentThread (jvm, (void *) &jvm_env, (void *) &args);
2051     if (status != 0)
2052     {
2053       ERROR ("java plugin: cjni_thread_attach: AttachCurrentThread failed "
2054           "with status %i.", status);
2055       return (NULL);
2056     }
2057
2058     cjni_env->reference_counter = 1;
2059     cjni_env->jvm_env = jvm_env;
2060   }
2061
2062   DEBUG ("java plugin: cjni_thread_attach: cjni_env->reference_counter = %i",
2063       cjni_env->reference_counter);
2064   assert (jvm_env != NULL);
2065   return (jvm_env);
2066 } /* }}} JNIEnv *cjni_thread_attach */
2067
2068 /* Decrease the reference counter of this thread. If it reaches zero, detach
2069  * from the JVM. */
2070 static int cjni_thread_detach (void) /* {{{ */
2071 {
2072   cjni_jvm_env_t *cjni_env;
2073   int status;
2074
2075   cjni_env = pthread_getspecific (jvm_env_key);
2076   if (cjni_env == NULL)
2077   {
2078     ERROR ("java plugin: cjni_thread_detach: pthread_getspecific failed.");
2079     return (-1);
2080   }
2081
2082   assert (cjni_env->reference_counter > 0);
2083   assert (cjni_env->jvm_env != NULL);
2084
2085   cjni_env->reference_counter--;
2086   DEBUG ("java plugin: cjni_thread_detach: cjni_env->reference_counter = %i",
2087       cjni_env->reference_counter);
2088
2089   if (cjni_env->reference_counter > 0)
2090     return (0);
2091
2092   status = (*jvm)->DetachCurrentThread (jvm);
2093   if (status != 0)
2094   {
2095     ERROR ("java plugin: cjni_thread_detach: DetachCurrentThread failed "
2096         "with status %i.", status);
2097   }
2098
2099   cjni_env->reference_counter = 0;
2100   cjni_env->jvm_env = NULL;
2101
2102   return (0);
2103 } /* }}} JNIEnv *cjni_thread_attach */
2104
2105 static int cjni_config_add_jvm_arg (oconfig_item_t *ci) /* {{{ */
2106 {
2107   char **tmp;
2108
2109   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2110   {
2111     WARNING ("java plugin: `JVMArg' needs exactly one string argument.");
2112     return (-1);
2113   }
2114
2115   if (jvm != NULL)
2116   {
2117     ERROR ("java plugin: All `JVMArg' options MUST appear before all "
2118         "`LoadPlugin' options! The JVM is already started and I have to "
2119         "ignore this argument: %s",
2120         ci->values[0].value.string);
2121     return (-1);
2122   }
2123
2124   tmp = (char **) realloc (jvm_argv, sizeof (char *) * (jvm_argc + 1));
2125   if (tmp == NULL)
2126   {
2127     ERROR ("java plugin: realloc failed.");
2128     return (-1);
2129   }
2130   jvm_argv = tmp;
2131
2132   jvm_argv[jvm_argc] = strdup (ci->values[0].value.string);
2133   if (jvm_argv[jvm_argc] == NULL)
2134   {
2135     ERROR ("java plugin: strdup failed.");
2136     return (-1);
2137   }
2138   jvm_argc++;
2139
2140   return (0);
2141 } /* }}} int cjni_config_add_jvm_arg */
2142
2143 static int cjni_config_load_plugin (oconfig_item_t *ci) /* {{{ */
2144 {
2145   JNIEnv *jvm_env;
2146   java_plugin_class_t *class;
2147   jmethodID constructor_id;
2148   jobject tmp_object;
2149
2150   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2151   {
2152     WARNING ("java plugin: `LoadPlugin' needs exactly one string argument.");
2153     return (-1);
2154   }
2155
2156   jvm_env = cjni_thread_attach ();
2157   if (jvm_env == NULL)
2158     return (-1);
2159
2160   class = (java_plugin_class_t *) realloc (java_classes_list,
2161       (java_classes_list_len + 1) * sizeof (*java_classes_list));
2162   if (class == NULL)
2163   {
2164     ERROR ("java plugin: realloc failed.");
2165     cjni_thread_detach ();
2166     return (-1);
2167   }
2168   java_classes_list = class;
2169   class = java_classes_list + java_classes_list_len;
2170
2171   memset (class, 0, sizeof (*class));
2172   class->name = strdup (ci->values[0].value.string);
2173   if (class->name == NULL)
2174   {
2175     ERROR ("java plugin: strdup failed.");
2176     cjni_thread_detach ();
2177     return (-1);
2178   }
2179   class->class = NULL;
2180   class->object = NULL;
2181
2182   DEBUG ("java plugin: Loading class %s", class->name);
2183
2184   class->class = (*jvm_env)->FindClass (jvm_env, class->name);
2185   if (class->class == NULL)
2186   {
2187     ERROR ("java plugin: cjni_config_load_plugin: FindClass (%s) failed.",
2188         class->name);
2189     cjni_thread_detach ();
2190     free (class->name);
2191     return (-1);
2192   }
2193
2194   constructor_id = (*jvm_env)->GetMethodID (jvm_env, class->class,
2195       "<init>", "()V");
2196   if (constructor_id == NULL)
2197   {
2198     ERROR ("java plugin: cjni_config_load_plugin: "
2199         "Could not find the constructor for `%s'.",
2200         class->name);
2201     cjni_thread_detach ();
2202     free (class->name);
2203     return (-1);
2204   }
2205
2206   tmp_object = (*jvm_env)->NewObject (jvm_env, class->class,
2207       constructor_id);
2208   if (tmp_object != NULL)
2209     class->object = (*jvm_env)->NewGlobalRef (jvm_env, tmp_object);
2210   else
2211     class->object = NULL;
2212   if (class->object == NULL)
2213   {
2214     ERROR ("java plugin: cjni_config_load_plugin: "
2215         "Could create a new `%s' object.",
2216         class->name);
2217     cjni_thread_detach ();
2218     free (class->name);
2219     return (-1);
2220   }
2221
2222   cjni_thread_detach ();
2223
2224   java_classes_list_len++;
2225
2226   return (0);
2227 } /* }}} int cjni_config_load_plugin */
2228
2229 static int cjni_config_plugin_block (oconfig_item_t *ci) /* {{{ */
2230 {
2231   JNIEnv *jvm_env;
2232   cjni_callback_info_t *cbi;
2233   jobject o_ocitem;
2234   const char *name;
2235   int status;
2236   size_t i;
2237
2238   jclass class;
2239   jmethodID method;
2240
2241   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
2242   {
2243     WARNING ("java plugin: `Plugin' blocks "
2244         "need exactly one string argument.");
2245     return (-1);
2246   }
2247
2248   name = ci->values[0].value.string;
2249
2250   cbi = NULL;
2251   for (i = 0; i < java_callbacks_num; i++)
2252   {
2253     if (java_callbacks[i].type != CB_TYPE_CONFIG)
2254       continue;
2255
2256     if (strcmp (name, java_callbacks[i].name) != 0)
2257       continue;
2258
2259     cbi = java_callbacks + i;
2260     break;
2261   }
2262
2263   if (cbi == NULL)
2264   {
2265     NOTICE ("java plugin: Configuration block for `%s' found, but no such "
2266         "configuration callback has been registered. Please make sure, the "
2267         "`LoadPlugin' lines precede the `Plugin' blocks.",
2268         name);
2269     return (0);
2270   }
2271
2272   DEBUG ("java plugin: Configuring %s", name);
2273
2274   jvm_env = cjni_thread_attach ();
2275   if (jvm_env == NULL)
2276     return (-1);
2277
2278   o_ocitem = ctoj_oconfig_item (jvm_env, ci);
2279   if (o_ocitem == NULL)
2280   {
2281     ERROR ("java plugin: cjni_config_plugin_block: ctoj_oconfig_item failed.");
2282     cjni_thread_detach ();
2283     return (-1);
2284   }
2285
2286   class = (*jvm_env)->GetObjectClass (jvm_env, cbi->object);
2287   method = (*jvm_env)->GetMethodID (jvm_env, class,
2288       "config", "(Lorg/collectd/api/OConfigItem;)I");
2289
2290   status = (*jvm_env)->CallIntMethod (jvm_env,
2291       cbi->object, method, o_ocitem);
2292
2293   (*jvm_env)->DeleteLocalRef (jvm_env, o_ocitem);
2294   cjni_thread_detach ();
2295   return (0);
2296 } /* }}} int cjni_config_plugin_block */
2297
2298 static int cjni_config (oconfig_item_t *ci) /* {{{ */
2299 {
2300   int success;
2301   int errors;
2302   int status;
2303   int i;
2304
2305   success = 0;
2306   errors = 0;
2307
2308   for (i = 0; i < ci->children_num; i++)
2309   {
2310     oconfig_item_t *child = ci->children + i;
2311
2312     if (strcasecmp ("JVMArg", child->key) == 0)
2313     {
2314       status = cjni_config_add_jvm_arg (child);
2315       if (status == 0)
2316         success++;
2317       else
2318         errors++;
2319     }
2320     else if (strcasecmp ("LoadPlugin", child->key) == 0)
2321     {
2322       status = cjni_config_load_plugin (child);
2323       if (status == 0)
2324         success++;
2325       else
2326         errors++;
2327     }
2328     else if (strcasecmp ("Plugin", child->key) == 0)
2329     {
2330       status = cjni_config_plugin_block (child);
2331       if (status == 0)
2332         success++;
2333       else
2334         errors++;
2335     }
2336     else
2337     {
2338       WARNING ("java plugin: Option `%s' not allowed here.", child->key);
2339       errors++;
2340     }
2341   }
2342
2343   DEBUG ("java plugin: jvm_argc = %zu;", jvm_argc);
2344   DEBUG ("java plugin: java_classes_list_len = %zu;", java_classes_list_len);
2345
2346   if ((success == 0) && (errors > 0))
2347   {
2348     ERROR ("java plugin: All statements failed.");
2349     return (-1);
2350   }
2351
2352   return (0);
2353 } /* }}} int cjni_config */
2354
2355 /* Free the data contained in the `user_data_t' pointer passed to `cjni_read'
2356  * and `cjni_write'. In particular, delete the global reference to the Java
2357  * object. */
2358 static void cjni_callback_info_destroy (void *arg) /* {{{ */
2359 {
2360   JNIEnv *jvm_env;
2361   cjni_callback_info_t *cbi;
2362
2363   DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
2364
2365   cbi = (cjni_callback_info_t *) arg;
2366
2367   /* This condition can occurr when shutting down. */
2368   if (jvm == NULL)
2369   {
2370     sfree (cbi);
2371     return;
2372   }
2373
2374   if (arg == NULL)
2375     return;
2376
2377   jvm_env = cjni_thread_attach ();
2378   if (jvm_env == NULL)
2379   {
2380     ERROR ("java plugin: cjni_callback_info_destroy: cjni_thread_attach failed.");
2381     return;
2382   }
2383
2384   (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
2385
2386   cbi->method = NULL;
2387   cbi->object = NULL;
2388   cbi->class  = NULL;
2389   free (cbi);
2390
2391   cjni_thread_detach ();
2392 } /* }}} void cjni_callback_info_destroy */
2393
2394 /* Call the CB_TYPE_READ callback pointed to by the `user_data_t' pointer. */
2395 static int cjni_read (user_data_t *ud) /* {{{ */
2396 {
2397   JNIEnv *jvm_env;
2398   cjni_callback_info_t *cbi;
2399   int status;
2400   int ret_status;
2401
2402   if (jvm == NULL)
2403   {
2404     ERROR ("java plugin: cjni_read: jvm == NULL");
2405     return (-1);
2406   }
2407
2408   if ((ud == NULL) || (ud->data == NULL))
2409   {
2410     ERROR ("java plugin: cjni_read: Invalid user data.");
2411     return (-1);
2412   }
2413
2414   jvm_env = cjni_thread_attach ();
2415   if (jvm_env == NULL)
2416     return (-1);
2417
2418   cbi = (cjni_callback_info_t *) ud->data;
2419
2420   ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
2421       cbi->method);
2422
2423   status = cjni_thread_detach ();
2424   if (status != 0)
2425   {
2426     ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2427     return (-1);
2428   }
2429
2430   return (ret_status);
2431 } /* }}} int cjni_read */
2432
2433 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
2434 static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
2435     user_data_t *ud)
2436 {
2437   JNIEnv *jvm_env;
2438   cjni_callback_info_t *cbi;
2439   jobject vl_java;
2440   int status;
2441   int ret_status;
2442
2443   if (jvm == NULL)
2444   {
2445     ERROR ("java plugin: cjni_write: jvm == NULL");
2446     return (-1);
2447   }
2448
2449   if ((ud == NULL) || (ud->data == NULL))
2450   {
2451     ERROR ("java plugin: cjni_write: Invalid user data.");
2452     return (-1);
2453   }
2454
2455   jvm_env = cjni_thread_attach ();
2456   if (jvm_env == NULL)
2457     return (-1);
2458
2459   cbi = (cjni_callback_info_t *) ud->data;
2460
2461   vl_java = ctoj_value_list (jvm_env, ds, vl);
2462   if (vl_java == NULL)
2463   {
2464     ERROR ("java plugin: cjni_write: ctoj_value_list failed.");
2465     return (-1);
2466   }
2467
2468   ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2469       cbi->object, cbi->method, vl_java);
2470
2471   (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
2472
2473   status = cjni_thread_detach ();
2474   if (status != 0)
2475   {
2476     ERROR ("java plugin: cjni_write: cjni_thread_detach failed.");
2477     return (-1);
2478   }
2479
2480   return (ret_status);
2481 } /* }}} int cjni_write */
2482
2483 /* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
2484 static int cjni_flush (int timeout, const char *identifier, /* {{{ */
2485     user_data_t *ud)
2486 {
2487   JNIEnv *jvm_env;
2488   cjni_callback_info_t *cbi;
2489   jobject o_identifier;
2490   int status;
2491   int ret_status;
2492
2493   if (jvm == NULL)
2494   {
2495     ERROR ("java plugin: cjni_flush: jvm == NULL");
2496     return (-1);
2497   }
2498
2499   if ((ud == NULL) || (ud->data == NULL))
2500   {
2501     ERROR ("java plugin: cjni_flush: Invalid user data.");
2502     return (-1);
2503   }
2504
2505   jvm_env = cjni_thread_attach ();
2506   if (jvm_env == NULL)
2507     return (-1);
2508
2509   cbi = (cjni_callback_info_t *) ud->data;
2510
2511   o_identifier = NULL;
2512   if (identifier != NULL)
2513   {
2514     o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
2515     if (o_identifier == NULL)
2516     {
2517       ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
2518       return (-1);
2519     }
2520   }
2521
2522   ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2523       cbi->object, cbi->method, (jint) timeout, o_identifier);
2524
2525   (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
2526
2527   status = cjni_thread_detach ();
2528   if (status != 0)
2529   {
2530     ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
2531     return (-1);
2532   }
2533
2534   return (ret_status);
2535 } /* }}} int cjni_flush */
2536
2537 /* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
2538 static void cjni_log (int severity, const char *message, /* {{{ */
2539     user_data_t *ud)
2540 {
2541   JNIEnv *jvm_env;
2542   cjni_callback_info_t *cbi;
2543   jobject o_message;
2544
2545   if (jvm == NULL)
2546     return;
2547
2548   if ((ud == NULL) || (ud->data == NULL))
2549     return;
2550
2551   jvm_env = cjni_thread_attach ();
2552   if (jvm_env == NULL)
2553     return;
2554
2555   cbi = (cjni_callback_info_t *) ud->data;
2556
2557   o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
2558   if (o_message == NULL)
2559     return;
2560
2561   (*jvm_env)->CallVoidMethod (jvm_env,
2562       cbi->object, cbi->method, (jint) severity, o_message);
2563
2564   (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
2565
2566   cjni_thread_detach ();
2567 } /* }}} void cjni_log */
2568
2569 /* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
2570  * pointer. */
2571 static int cjni_notification (const notification_t *n, /* {{{ */
2572     user_data_t *ud)
2573 {
2574   JNIEnv *jvm_env;
2575   cjni_callback_info_t *cbi;
2576   jobject o_notification;
2577   int status;
2578   int ret_status;
2579
2580   if (jvm == NULL)
2581   {
2582     ERROR ("java plugin: cjni_read: jvm == NULL");
2583     return (-1);
2584   }
2585
2586   if ((ud == NULL) || (ud->data == NULL))
2587   {
2588     ERROR ("java plugin: cjni_read: Invalid user data.");
2589     return (-1);
2590   }
2591
2592   jvm_env = cjni_thread_attach ();
2593   if (jvm_env == NULL)
2594     return (-1);
2595
2596   cbi = (cjni_callback_info_t *) ud->data;
2597
2598   o_notification = ctoj_notification (jvm_env, n);
2599   if (o_notification == NULL)
2600   {
2601     ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
2602     return (-1);
2603   }
2604
2605   ret_status = (*jvm_env)->CallIntMethod (jvm_env,
2606       cbi->object, cbi->method, o_notification);
2607
2608   (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
2609
2610   status = cjni_thread_detach ();
2611   if (status != 0)
2612   {
2613     ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2614     return (-1);
2615   }
2616
2617   return (ret_status);
2618 } /* }}} int cjni_notification */
2619
2620 /* Callbacks for matches implemented in Java */
2621 static int cjni_match_target_create (const oconfig_item_t *ci, /* {{{ */
2622     void **user_data)
2623 {
2624   JNIEnv *jvm_env;
2625   cjni_callback_info_t *cbi_ret;
2626   cjni_callback_info_t *cbi_factory;
2627   const char *name;
2628   jobject o_ci;
2629   jobject o_tmp;
2630   int type;
2631   size_t i;
2632
2633   cbi_ret = NULL;
2634   o_ci = NULL;
2635   jvm_env = NULL;
2636
2637 #define BAIL_OUT(status) \
2638   if (cbi_ret != NULL) { \
2639     free (cbi_ret->name); \
2640     if ((jvm_env != NULL) && (cbi_ret->object != NULL)) \
2641       (*jvm_env)->DeleteLocalRef (jvm_env, cbi_ret->object); \
2642   } \
2643   free (cbi_ret); \
2644   if (jvm_env != NULL) { \
2645     if (o_ci != NULL) \
2646       (*jvm_env)->DeleteLocalRef (jvm_env, o_ci); \
2647     cjni_thread_detach (); \
2648   } \
2649   return (status)
2650
2651   if (jvm == NULL)
2652   {
2653     ERROR ("java plugin: cjni_read: jvm == NULL");
2654     BAIL_OUT (-1);
2655   }
2656
2657   jvm_env = cjni_thread_attach ();
2658   if (jvm_env == NULL)
2659   {
2660     BAIL_OUT (-1);
2661   }
2662
2663   /* Find out whether to create a match or a target. */
2664   if (strcasecmp ("Match", ci->key) == 0)
2665     type = CB_TYPE_MATCH;
2666   else if (strcasecmp ("Target", ci->key) == 0)
2667     type = CB_TYPE_TARGET;
2668   else
2669   {
2670     ERROR ("java plugin: cjni_match_target_create: Can't figure out whether "
2671         "to create a match or a target.");
2672     BAIL_OUT (-1);
2673   }
2674
2675   /* This is the name of the match we should create. */
2676   name = ci->values[0].value.string;
2677
2678   /* Lets see if we have a matching factory here.. */
2679   cbi_factory = NULL;
2680   for (i = 0; i < java_callbacks_num; i++)
2681   {
2682     if (java_callbacks[i].type != type)
2683       continue;
2684
2685     if (strcmp (name, java_callbacks[i].name) != 0)
2686       continue;
2687
2688     cbi_factory = java_callbacks + i;
2689     break;
2690   }
2691
2692   /* Nope, no factory for that name.. */
2693   if (cbi_factory == NULL)
2694   {
2695     ERROR ("java plugin: cjni_match_target_create: "
2696         "No such match factory registered: %s",
2697         name);
2698     BAIL_OUT (-1);
2699   }
2700
2701   /* We convert `ci' to its Java equivalent.. */
2702   o_ci = ctoj_oconfig_item (jvm_env, ci);
2703   if (o_ci == NULL)
2704   {
2705     ERROR ("java plugin: cjni_match_target_create: "
2706         "ctoj_oconfig_item failed.");
2707     BAIL_OUT (-1);
2708   }
2709
2710   /* Allocate a new callback info structure. This is going to be our user_data
2711    * pointer. */
2712   cbi_ret = (cjni_callback_info_t *) malloc (sizeof (*cbi_ret));
2713   if (cbi_ret == NULL)
2714   {
2715     ERROR ("java plugin: cjni_match_target_create: malloc failed.");
2716     BAIL_OUT (-1);
2717   }
2718   memset (cbi_ret, 0, sizeof (*cbi_ret));
2719   cbi_ret->object = NULL;
2720   cbi_ret->type = type;
2721
2722   /* Lets fill the callback info structure.. First, the name: */
2723   cbi_ret->name = strdup (name);
2724   if (cbi_ret->name == NULL)
2725   {
2726     ERROR ("java plugin: cjni_match_target_create: strdup failed.");
2727     BAIL_OUT (-1);
2728   }
2729
2730   /* Then call the factory method so it creates a new object for us. */
2731   o_tmp = (*jvm_env)->CallObjectMethod (jvm_env,
2732       cbi_factory->object, cbi_factory->method, o_ci);
2733   if (o_tmp == NULL)
2734   {
2735     ERROR ("java plugin: cjni_match_target_create: CallObjectMethod failed.");
2736     BAIL_OUT (-1);
2737   }
2738
2739   cbi_ret->object = (*jvm_env)->NewGlobalRef (jvm_env, o_tmp);
2740   if (o_tmp == NULL)
2741   {
2742     ERROR ("java plugin: cjni_match_target_create: NewGlobalRef failed.");
2743     BAIL_OUT (-1);
2744   }
2745
2746   /* This is the class of the match. It is possibly different from the class of
2747    * the match-factory! */
2748   cbi_ret->class = (*jvm_env)->GetObjectClass (jvm_env, cbi_ret->object);
2749   if (cbi_ret->class == NULL)
2750   {
2751     ERROR ("java plugin: cjni_match_target_create: GetObjectClass failed.");
2752     BAIL_OUT (-1);
2753   }
2754
2755   /* Lookup the `int match (DataSet, ValueList)' method. */
2756   cbi_ret->method = (*jvm_env)->GetMethodID (jvm_env, cbi_ret->class,
2757       /* method name = */ (type == CB_TYPE_MATCH) ? "match" : "invoke",
2758       "(Lorg/collectd/api/DataSet;Lorg/collectd/api/ValueList;)I");
2759   if (cbi_ret->method == NULL)
2760   {
2761     ERROR ("java plugin: cjni_match_target_create: GetMethodID failed.");
2762     BAIL_OUT (-1);
2763   }
2764
2765   /* Return the newly created match via the user_data pointer. */
2766   *user_data = (void *) cbi_ret;
2767
2768   cjni_thread_detach ();
2769
2770   DEBUG ("java plugin: cjni_match_target_create: "
2771       "Successfully created a `%s' %s.",
2772       cbi_ret->name, (type == CB_TYPE_MATCH) ? "match" : "target");
2773
2774   /* Success! */
2775   return (0);
2776 #undef BAIL_OUT
2777 } /* }}} int cjni_match_target_create */
2778
2779 static int cjni_match_target_destroy (void **user_data) /* {{{ */
2780 {
2781   cjni_callback_info_destroy (*user_data);
2782   *user_data = NULL;
2783
2784   return (0);
2785 } /* }}} int cjni_match_target_destroy */
2786
2787 static int cjni_match_target_invoke (const data_set_t *ds, /* {{{ */
2788     value_list_t *vl, notification_meta_t **meta, void **user_data)
2789 {
2790   JNIEnv *jvm_env;
2791   cjni_callback_info_t *cbi;
2792   jobject o_vl;
2793   jobject o_ds;
2794   int ret_status;
2795   int status;
2796
2797   if (jvm == NULL)
2798   {
2799     ERROR ("java plugin: cjni_match_target_invoke: jvm == NULL");
2800     return (-1);
2801   }
2802
2803   jvm_env = cjni_thread_attach ();
2804   if (jvm_env == NULL)
2805     return (-1);
2806
2807   cbi = (cjni_callback_info_t *) *user_data;
2808
2809   o_vl = ctoj_value_list (jvm_env, ds, vl);
2810   if (o_vl == NULL)
2811   {
2812     ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2813     cjni_thread_detach ();
2814     return (-1);
2815   }
2816
2817   o_ds = ctoj_data_set (jvm_env, ds);
2818   if (o_ds == NULL)
2819   {
2820     ERROR ("java plugin: cjni_match_target_invoke: ctoj_value_list failed.");
2821     cjni_thread_detach ();
2822     return (-1);
2823   }
2824
2825   ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object, cbi->method,
2826       o_ds, o_vl);
2827
2828   DEBUG ("java plugin: cjni_match_target_invoke: Method returned %i.", ret_status);
2829
2830   /* If we're executing a target, copy the `ValueList' back to our
2831    * `value_list_t'. */
2832   if (cbi->type == CB_TYPE_TARGET)
2833   {
2834     value_list_t new_vl;
2835
2836     memset (&new_vl, 0, sizeof (new_vl));
2837     status = jtoc_value_list (jvm_env, &new_vl, o_vl);
2838     if (status != 0)
2839     {
2840       ERROR ("java plugin: cjni_match_target_invoke: "
2841           "jtoc_value_list failed.");
2842     }
2843     else /* if (status == 0) */
2844     {
2845       /* plugin_dispatch_values assures that this is dynamically allocated
2846        * memory. */
2847       sfree (vl->values);
2848
2849       /* This will replace the vl->values pointer to a new, dynamically
2850        * allocated piece of memory. */
2851       memcpy (vl, &new_vl, sizeof (*vl));
2852     }
2853   } /* if (cbi->type == CB_TYPE_TARGET) */
2854
2855   status = cjni_thread_detach ();
2856   if (status != 0)
2857     ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
2858
2859   return (ret_status);
2860 } /* }}} int cjni_match_target_invoke */
2861
2862 /* Iterate over `java_callbacks' and call all CB_TYPE_INIT callbacks. */
2863 static int cjni_init_plugins (JNIEnv *jvm_env) /* {{{ */
2864 {
2865   int status;
2866   size_t i;
2867
2868   for (i = 0; i < java_callbacks_num; i++)
2869   {
2870     if (java_callbacks[i].type != CB_TYPE_INIT)
2871       continue;
2872
2873     DEBUG ("java plugin: Initializing %s", java_callbacks[i].name);
2874
2875     status = (*jvm_env)->CallIntMethod (jvm_env,
2876         java_callbacks[i].object, java_callbacks[i].method);
2877     if (status != 0)
2878     {
2879       ERROR ("java plugin: Initializing `%s' failed with status %i. "
2880           "Removing read function.",
2881           java_callbacks[i].name, status);
2882       plugin_unregister_read (java_callbacks[i].name);
2883     }
2884   }
2885
2886   return (0);
2887 } /* }}} int cjni_init_plugins */
2888
2889 /* Iterate over `java_callbacks' and call all CB_TYPE_SHUTDOWN callbacks. */
2890 static int cjni_shutdown_plugins (JNIEnv *jvm_env) /* {{{ */
2891 {
2892   int status;
2893   size_t i;
2894
2895   for (i = 0; i < java_callbacks_num; i++)
2896   {
2897     if (java_callbacks[i].type != CB_TYPE_SHUTDOWN)
2898       continue;
2899
2900     DEBUG ("java plugin: Shutting down %s", java_callbacks[i].name);
2901
2902     status = (*jvm_env)->CallIntMethod (jvm_env,
2903         java_callbacks[i].object, java_callbacks[i].method);
2904     if (status != 0)
2905     {
2906       ERROR ("java plugin: Shutting down `%s' failed with status %i. ",
2907           java_callbacks[i].name, status);
2908     }
2909   }
2910
2911   return (0);
2912 } /* }}} int cjni_shutdown_plugins */
2913
2914
2915 static int cjni_shutdown (void) /* {{{ */
2916 {
2917   JNIEnv *jvm_env;
2918   JavaVMAttachArgs args;
2919   int status;
2920   size_t i;
2921
2922   if (jvm == NULL)
2923     return (0);
2924
2925   jvm_env = NULL;
2926   memset (&args, 0, sizeof (args));
2927   args.version = JNI_VERSION_1_2;
2928
2929   status = (*jvm)->AttachCurrentThread (jvm, (void **) &jvm_env, &args);
2930   if (status != 0)
2931   {
2932     ERROR ("java plugin: cjni_shutdown: AttachCurrentThread failed with status %i.",
2933         status);
2934     return (-1);
2935   }
2936
2937   /* Execute all the shutdown functions registered by plugins. */
2938   cjni_shutdown_plugins (jvm_env);
2939
2940   /* Release all the global references to callback functions */
2941   for (i = 0; i < java_callbacks_num; i++)
2942   {
2943     if (java_callbacks[i].object != NULL)
2944     {
2945       (*jvm_env)->DeleteGlobalRef (jvm_env, java_callbacks[i].object);
2946       java_callbacks[i].object = NULL;
2947     }
2948     sfree (java_callbacks[i].name);
2949   }
2950   java_callbacks_num = 0;
2951   sfree (java_callbacks);
2952
2953   /* Release all the global references to directly loaded classes. */
2954   for (i = 0; i < java_classes_list_len; i++)
2955   {
2956     if (java_classes_list[i].object != NULL)
2957     {
2958       (*jvm_env)->DeleteGlobalRef (jvm_env, java_classes_list[i].object);
2959       java_classes_list[i].object = NULL;
2960     }
2961     sfree (java_classes_list[i].name);
2962   }
2963   java_classes_list_len = 0;
2964   sfree (java_classes_list);
2965
2966   /* Destroy the JVM */
2967   DEBUG ("java plugin: Destroying the JVM.");
2968   (*jvm)->DestroyJavaVM (jvm);
2969   jvm = NULL;
2970   jvm_env = NULL;
2971
2972   pthread_key_delete (jvm_env_key);
2973
2974   /* Free the JVM argument list */
2975   for (i = 0; i < jvm_argc; i++)
2976     sfree (jvm_argv[i]);
2977   jvm_argc = 0;
2978   sfree (jvm_argv);
2979
2980   return (0);
2981 } /* }}} int cjni_shutdown */
2982
2983 /* Initialization: Create a JVM, load all configured classes and call their
2984  * `config' and `init' callback methods. */
2985 static int cjni_init (void) /* {{{ */
2986 {
2987   JNIEnv *jvm_env;
2988
2989   if (jvm == NULL)
2990   {
2991     ERROR ("java plugin: cjni_init: jvm == NULL");
2992     return (-1);
2993   }
2994
2995   jvm_env = cjni_thread_attach ();
2996   if (jvm_env == NULL)
2997     return (-1);
2998
2999   cjni_init_plugins (jvm_env);
3000
3001   cjni_thread_detach ();
3002   return (0);
3003 } /* }}} int cjni_init */
3004
3005 void module_register (void)
3006 {
3007   plugin_register_complex_config ("java", cjni_config);
3008   plugin_register_init ("java", cjni_init);
3009   plugin_register_shutdown ("java", cjni_shutdown);
3010 } /* void module_register (void) */
3011
3012 /* vim: set sw=2 sts=2 et fdm=marker : */