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