java plugin: Expose `plugin_dispatch_notification' to Java plugins.
[collectd.git] / src / java.c
index d4acdab..890df17 100644 (file)
@@ -60,11 +60,14 @@ struct java_plugin_config_s /* {{{ */
 typedef struct java_plugin_config_s java_plugin_config_t;
 /* }}} */
 
-#define CB_TYPE_CONFIG   1
-#define CB_TYPE_INIT     2
-#define CB_TYPE_READ     3
-#define CB_TYPE_WRITE    4
-#define CB_TYPE_SHUTDOWN 5
+#define CB_TYPE_CONFIG       1
+#define CB_TYPE_INIT         2
+#define CB_TYPE_READ         3
+#define CB_TYPE_WRITE        4
+#define CB_TYPE_FLUSH        5
+#define CB_TYPE_SHUTDOWN     6
+#define CB_TYPE_LOG          7
+#define CB_TYPE_NOTIFICATION 8
 struct cjni_callback_info_s /* {{{ */
 {
   char     *name;
@@ -113,6 +116,9 @@ static int cjni_callback_register (JNIEnv *jvm_env, jobject o_name,
 static int cjni_read (user_data_t *user_data);
 static int cjni_write (const data_set_t *ds, const value_list_t *vl,
     user_data_t *ud);
+static int cjni_flush (int timeout, const char *identifier, user_data_t *ud);
+static void cjni_log (int severity, const char *message, user_data_t *ud);
+static int cjni_notification (const notification_t *n, user_data_t *ud);
 
 /* 
  * C to Java conversion functions
@@ -784,7 +790,7 @@ static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
   status = ctoj_string (jvm_env, str, \
       c_valuelist, o_valuelist, method_name); \
   if (status != 0) { \
-    ERROR ("java plugin: ctoj_value_list: jtoc_string (%s) failed.", \
+    ERROR ("java plugin: ctoj_value_list: ctoj_string (%s) failed.", \
         method_name); \
     (*jvm_env)->DeleteLocalRef (jvm_env, o_valuelist); \
     return (NULL); \
@@ -832,14 +838,97 @@ static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */
   }
 
   return (o_valuelist);
-} /* }}} int ctoj_value_list */
+} /* }}} jobject ctoj_value_list */
+
+/* Convert a notification_t to a org.collectd.api.Notification */
+static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */
+    const notification_t *n)
+{
+  jclass c_notification;
+  jmethodID m_constructor;
+  jobject o_notification;
+  int status;
+
+  /* First, create a new Notification instance..
+   * Look up the class.. */
+  c_notification = (*jvm_env)->FindClass (jvm_env,
+      "org.collectd.api.Notification");
+  if (c_notification == NULL)
+  {
+    ERROR ("java plugin: ctoj_notification: "
+        "FindClass (org.collectd.api.Notification) failed.");
+    return (NULL);
+  }
+
+  /* Lookup the `Notification ()' constructor. */
+  m_constructor = (*jvm_env)->GetMethodID (jvm_env, c_notification,
+      "<init>", "()V");
+  if (m_constructor == NULL)
+  {
+    ERROR ("java plugin: ctoj_notification: Cannot find the "
+        "`Notification ()' constructor.");
+    return (NULL);
+  }
+
+  /* Create a new instance. */
+  o_notification = (*jvm_env)->NewObject (jvm_env, c_notification,
+      m_constructor);
+  if (o_notification == NULL)
+  {
+    ERROR ("java plugin: ctoj_notification: Creating a new Notification "
+        "instance failed.");
+    return (NULL);
+  }
+
+  /* Set the strings.. */
+#define SET_STRING(str,method_name) do { \
+  status = ctoj_string (jvm_env, str, \
+      c_notification, o_notification, method_name); \
+  if (status != 0) { \
+    ERROR ("java plugin: ctoj_notification: ctoj_string (%s) failed.", \
+        method_name); \
+    (*jvm_env)->DeleteLocalRef (jvm_env, o_notification); \
+    return (NULL); \
+  } } while (0)
+
+  SET_STRING (n->host,            "setHost");
+  SET_STRING (n->plugin,          "setPlugin");
+  SET_STRING (n->plugin_instance, "setPluginInstance");
+  SET_STRING (n->type,            "setType");
+  SET_STRING (n->type_instance,   "setTypeInstance");
+  SET_STRING (n->message,         "setMessage");
+
+#undef SET_STRING
+
+  /* Set the `time' member. Java stores time in milliseconds. */
+  status = ctoj_long (jvm_env, ((jlong) n->time) * ((jlong) 1000),
+      c_notification, o_notification, "setTime");
+  if (status != 0)
+  {
+    ERROR ("java plugin: ctoj_notification: ctoj_long (setTime) failed.");
+    (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
+    return (NULL);
+  }
+
+  /* Set the `interval' member.. */
+  status = ctoj_int (jvm_env, (jint) n->severity,
+      c_notification, o_notification, "setSeverity");
+  if (status != 0)
+  {
+    ERROR ("java plugin: ctoj_notification: ctoj_int (setSeverity) failed.");
+    (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
+    return (NULL);
+  }
+
+  return (o_notification);
+} /* }}} jobject ctoj_notification */
 
 /*
  * Java to C conversion functions
  */
 /* Call a `String <method> ()' method. */
 static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
-    char *buffer, size_t buffer_size,
+    char *buffer, size_t buffer_size, int empty_okay,
     jclass class_ptr, jobject object_ptr, const char *method_name)
 {
   jmethodID method_id;
@@ -856,12 +945,17 @@ static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
   }
 
   string_obj = (*jvm_env)->CallObjectMethod (jvm_env, object_ptr, method_id);
-  if (string_obj == NULL)
+  if ((string_obj == NULL) && (empty_okay == 0))
   {
     ERROR ("java plugin: jtoc_string: CallObjectMethod (%s) failed.",
         method_name);
     return (-1);
   }
+  else if ((string_obj == NULL) && (empty_okay != 0))
+  {
+    memset (buffer, 0, buffer_size);
+    return (0);
+  }
 
   c_str = (*jvm_env)->GetStringUTFChars (jvm_env, string_obj, 0);
   if (c_str == NULL)
@@ -879,6 +973,27 @@ static int jtoc_string (JNIEnv *jvm_env, /* {{{ */
   return (0);
 } /* }}} int jtoc_string */
 
+/* Call an `int <method> ()' method. */
+static int jtoc_int (JNIEnv *jvm_env, /* {{{ */
+    jint *ret_value,
+    jclass class_ptr, jobject object_ptr, const char *method_name)
+{
+  jmethodID method_id;
+
+  method_id = (*jvm_env)->GetMethodID (jvm_env, class_ptr,
+      method_name, "()I");
+  if (method_id == NULL)
+  {
+    ERROR ("java plugin: jtoc_int: Cannot find method `int %s ()'.",
+        method_name);
+    return (-1);
+  }
+
+  *ret_value = (*jvm_env)->CallIntMethod (jvm_env, object_ptr, method_id);
+
+  return (0);
+} /* }}} int jtoc_int */
+
 /* Call a `long <method> ()' method. */
 static int jtoc_long (JNIEnv *jvm_env, /* {{{ */
     jlong *ret_value,
@@ -911,7 +1026,7 @@ static int jtoc_double (JNIEnv *jvm_env, /* {{{ */
       method_name, "()D");
   if (method_id == NULL)
   {
-    ERROR ("java plugin: jtoc_string: Cannot find method `double %s ()'.",
+    ERROR ("java plugin: jtoc_double: Cannot find method `double %s ()'.",
         method_name);
     return (-1);
   }
@@ -1082,8 +1197,9 @@ static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
     return (-1);
   }
 
-#define SET_STRING(buffer,method) do { \
-  status = jtoc_string (jvm_env, buffer, sizeof (buffer), \
+  /* eo == empty okay */
+#define SET_STRING(buffer,method, eo) do { \
+  status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
       class_ptr, object_ptr, method); \
   if (status != 0) { \
     ERROR ("java plugin: jtoc_value_list: jtoc_string (%s) failed.", \
@@ -1091,7 +1207,7 @@ static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
     return (-1); \
   } } while (0)
 
-  SET_STRING(vl->type, "getType");
+  SET_STRING(vl->type, "getType", /* empty = */ 0);
 
   ds = plugin_get_ds (vl->type);
   if (ds == NULL)
@@ -1102,10 +1218,10 @@ static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
     return (-1);
   }
 
-  SET_STRING(vl->host, "getHost");
-  SET_STRING(vl->plugin, "getPlugin");
-  SET_STRING(vl->plugin_instance, "getPluginInstance");
-  SET_STRING(vl->type_instance, "getTypeInstance");
+  SET_STRING(vl->host,            "getHost",           /* empty = */ 0);
+  SET_STRING(vl->plugin,          "getPlugin",         /* empty = */ 0);
+  SET_STRING(vl->plugin_instance, "getPluginInstance", /* empty = */ 1);
+  SET_STRING(vl->type_instance,   "getTypeInstance",   /* empty = */ 1);
 
 #undef SET_STRING
 
@@ -1137,6 +1253,61 @@ static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */
   return (0);
 } /* }}} int jtoc_value_list */
 
+/* Convert a org.collectd.api.Notification to a notification_t. */
+static int jtoc_notification (JNIEnv *jvm_env, notification_t *n, /* {{{ */
+    jobject object_ptr)
+{
+  jclass class_ptr;
+  int status;
+  jlong tmp_long;
+  jint tmp_int;
+
+  class_ptr = (*jvm_env)->GetObjectClass (jvm_env, object_ptr);
+  if (class_ptr == NULL)
+  {
+    ERROR ("java plugin: jtoc_notification: GetObjectClass failed.");
+    return (-1);
+  }
+
+  /* eo == empty okay */
+#define SET_STRING(buffer,method, eo) do { \
+  status = jtoc_string (jvm_env, buffer, sizeof (buffer), eo, \
+      class_ptr, object_ptr, method); \
+  if (status != 0) { \
+    ERROR ("java plugin: jtoc_notification: jtoc_string (%s) failed.", \
+        method); \
+    return (-1); \
+  } } while (0)
+
+  SET_STRING (n->host,            "getHost",           /* empty = */ 1);
+  SET_STRING (n->plugin,          "getPlugin",         /* empty = */ 1);
+  SET_STRING (n->plugin_instance, "getPluginInstance", /* empty = */ 1);
+  SET_STRING (n->type,            "getType",           /* empty = */ 1);
+  SET_STRING (n->type_instance,   "getTypeInstance",   /* empty = */ 1);
+  SET_STRING (n->message,         "getMessage",        /* empty = */ 0);
+
+#undef SET_STRING
+
+  status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getTime");
+  if (status != 0)
+  {
+    ERROR ("java plugin: jtoc_notification: jtoc_long (getTime) failed.");
+    return (-1);
+  }
+  /* Java measures time in milliseconds. */
+  n->time = (time_t) (tmp_long / ((jlong) 1000));
+
+  status = jtoc_int (jvm_env, &tmp_int,
+      class_ptr, object_ptr, "getSeverity");
+  if (status != 0)
+  {
+    ERROR ("java plugin: jtoc_notification: jtoc_int (getSeverity) failed.");
+    return (-1);
+  }
+  n->severity = (int) tmp_int;
+
+  return (0);
+} /* }}} int jtoc_notification */
 /* 
  * Functions accessible from Java
  */
@@ -1162,6 +1333,27 @@ static jint JNICALL cjni_api_dispatch_values (JNIEnv *jvm_env, /* {{{ */
   return (status);
 } /* }}} jint cjni_api_dispatch_values */
 
+static jint JNICALL cjni_api_dispatch_notification (JNIEnv *jvm_env, /* {{{ */
+    jobject this, jobject o_notification)
+{
+  notification_t n;
+  int status;
+
+  memset (&n, 0, sizeof (n));
+  n.meta = NULL;
+
+  status = jtoc_notification (jvm_env, &n, o_notification);
+  if (status != 0)
+  {
+    ERROR ("java plugin: cjni_api_dispatch_notification: jtoc_notification failed.");
+    return (-1);
+  }
+
+  status = plugin_dispatch_notification (&n);
+
+  return (status);
+} /* }}} jint cjni_api_dispatch_notification */
+
 static jobject JNICALL cjni_api_get_ds (JNIEnv *jvm_env, /* {{{ */
     jobject this, jobject o_string_type)
 {
@@ -1247,6 +1439,29 @@ static jint JNICALL cjni_api_register_write (JNIEnv *jvm_env, /* {{{ */
   return (0);
 } /* }}} jint cjni_api_register_write */
 
+static jint JNICALL cjni_api_register_flush (JNIEnv *jvm_env, /* {{{ */
+    jobject this, jobject o_name, jobject o_flush)
+{
+  user_data_t ud;
+  cjni_callback_info_t *cbi;
+
+  cbi = cjni_callback_info_create (jvm_env, o_name, o_flush, CB_TYPE_FLUSH);
+  if (cbi == NULL)
+    return (-1);
+
+  DEBUG ("java plugin: Registering new flush callback: %s", cbi->name);
+
+  memset (&ud, 0, sizeof (ud));
+  ud.data = (void *) cbi;
+  ud.free_func = cjni_callback_info_destroy;
+
+  plugin_register_flush (cbi->name, cjni_flush, &ud);
+
+  (*jvm_env)->DeleteLocalRef (jvm_env, o_flush);
+
+  return (0);
+} /* }}} jint cjni_api_register_flush */
+
 static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
     jobject this, jobject o_name, jobject o_shutdown)
 {
@@ -1254,6 +1469,53 @@ static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */
         CB_TYPE_SHUTDOWN));
 } /* }}} jint cjni_api_register_shutdown */
 
+static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */
+    jobject this, jobject o_name, jobject o_log)
+{
+  user_data_t ud;
+  cjni_callback_info_t *cbi;
+
+  cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG);
+  if (cbi == NULL)
+    return (-1);
+
+  DEBUG ("java plugin: Registering new log callback: %s", cbi->name);
+
+  memset (&ud, 0, sizeof (ud));
+  ud.data = (void *) cbi;
+  ud.free_func = cjni_callback_info_destroy;
+
+  plugin_register_log (cbi->name, cjni_log, &ud);
+
+  (*jvm_env)->DeleteLocalRef (jvm_env, o_log);
+
+  return (0);
+} /* }}} jint cjni_api_register_log */
+
+static jint JNICALL cjni_api_register_notification (JNIEnv *jvm_env, /* {{{ */
+    jobject this, jobject o_name, jobject o_notification)
+{
+  user_data_t ud;
+  cjni_callback_info_t *cbi;
+
+  cbi = cjni_callback_info_create (jvm_env, o_name, o_notification,
+      CB_TYPE_NOTIFICATION);
+  if (cbi == NULL)
+    return (-1);
+
+  DEBUG ("java plugin: Registering new notification callback: %s", cbi->name);
+
+  memset (&ud, 0, sizeof (ud));
+  ud.data = (void *) cbi;
+  ud.free_func = cjni_callback_info_destroy;
+
+  plugin_register_notification (cbi->name, cjni_notification, &ud);
+
+  (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
+
+  return (0);
+} /* }}} jint cjni_api_register_notification */
+
 static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */
     jobject this, jint severity, jobject o_message)
 {
@@ -1284,6 +1546,10 @@ static JNINativeMethod jni_api_functions[] = /* {{{ */
     "(Lorg/collectd/api/ValueList;)I",
     cjni_api_dispatch_values },
 
+  { "dispatchNotification",
+    "(Lorg/collectd/api/Notification;)I",
+    cjni_api_dispatch_notification },
+
   { "getDS",
     "(Ljava/lang/String;)Lorg/collectd/api/DataSet;",
     cjni_api_get_ds },
@@ -1304,10 +1570,22 @@ static JNINativeMethod jni_api_functions[] = /* {{{ */
     "(Ljava/lang/String;Lorg/collectd/api/CollectdWriteInterface;)I",
     cjni_api_register_write },
 
+  { "registerFlush",
+    "(Ljava/lang/String;Lorg/collectd/api/CollectdFlushInterface;)I",
+    cjni_api_register_flush },
+
   { "registerShutdown",
     "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I",
     cjni_api_register_shutdown },
 
+  { "registerLog",
+    "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I",
+    cjni_api_register_log },
+
+  { "registerNotification",
+    "(Ljava/lang/String;Lorg/collectd/api/CollectdNotificationInterface;)I",
+    cjni_api_register_notification },
+
   { "log",
     "(ILjava/lang/String;)V",
     cjni_api_log },
@@ -1351,11 +1629,26 @@ static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{
       method_signature = "(Lorg/collectd/api/ValueList;)I";
       break;
 
+    case CB_TYPE_FLUSH:
+      method_name = "flush";
+      method_signature = "(ILjava/lang/String;)I";
+      break;
+
     case CB_TYPE_SHUTDOWN:
       method_name = "shutdown";
       method_signature = "()I";
       break;
 
+    case CB_TYPE_LOG:
+      method_name = "log";
+      method_signature = "(ILjava/lang/String;)V";
+      break;
+
+    case CB_TYPE_NOTIFICATION:
+      method_name = "notification";
+      method_signature = "(LLorg/collectd/api/Notification;)I";
+      break;
+
     default:
       ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x",
           type);
@@ -1798,6 +2091,15 @@ static void cjni_callback_info_destroy (void *arg) /* {{{ */
 
   DEBUG ("java plugin: cjni_callback_info_destroy (arg = %p);", arg);
 
+  cbi = (cjni_callback_info_t *) arg;
+
+  /* This condition can occurr when shutting down. */
+  if (jvm == NULL)
+  {
+    sfree (cbi);
+    return;
+  }
+
   if (arg == NULL)
     return;
 
@@ -1808,8 +2110,6 @@ static void cjni_callback_info_destroy (void *arg) /* {{{ */
     return;
   }
 
-  cbi = (cjni_callback_info_t *) arg;
-
   (*jvm_env)->DeleteGlobalRef (jvm_env, cbi->object);
 
   cbi->method = NULL;
@@ -1826,6 +2126,7 @@ static int cjni_read (user_data_t *ud) /* {{{ */
   JNIEnv *jvm_env;
   cjni_callback_info_t *cbi;
   int status;
+  int ret_status;
 
   if (jvm == NULL)
   {
@@ -1845,7 +2146,7 @@ static int cjni_read (user_data_t *ud) /* {{{ */
 
   cbi = (cjni_callback_info_t *) ud->data;
 
-  status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
+  ret_status = (*jvm_env)->CallIntMethod (jvm_env, cbi->object,
       cbi->method);
 
   status = cjni_thread_detach ();
@@ -1855,7 +2156,7 @@ static int cjni_read (user_data_t *ud) /* {{{ */
     return (-1);
   }
 
-  return (status);
+  return (ret_status);
 } /* }}} int cjni_read */
 
 /* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */
@@ -1866,6 +2167,7 @@ static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
   cjni_callback_info_t *cbi;
   jobject vl_java;
   int status;
+  int ret_status;
 
   if (jvm == NULL)
   {
@@ -1892,7 +2194,7 @@ static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
     return (-1);
   }
 
-  status = (*jvm_env)->CallIntMethod (jvm_env,
+  ret_status = (*jvm_env)->CallIntMethod (jvm_env,
       cbi->object, cbi->method, vl_java);
 
   (*jvm_env)->DeleteLocalRef (jvm_env, vl_java);
@@ -1904,9 +2206,146 @@ static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
     return (-1);
   }
 
-  return (status);
+  return (ret_status);
 } /* }}} int cjni_write */
 
+/* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */
+static int cjni_flush (int timeout, const char *identifier, /* {{{ */
+    user_data_t *ud)
+{
+  JNIEnv *jvm_env;
+  cjni_callback_info_t *cbi;
+  jobject o_identifier;
+  int status;
+  int ret_status;
+
+  if (jvm == NULL)
+  {
+    ERROR ("java plugin: cjni_flush: jvm == NULL");
+    return (-1);
+  }
+
+  if ((ud == NULL) || (ud->data == NULL))
+  {
+    ERROR ("java plugin: cjni_flush: Invalid user data.");
+    return (-1);
+  }
+
+  jvm_env = cjni_thread_attach ();
+  if (jvm_env == NULL)
+    return (-1);
+
+  cbi = (cjni_callback_info_t *) ud->data;
+
+  o_identifier = NULL;
+  if (identifier != NULL)
+  {
+    o_identifier = (*jvm_env)->NewStringUTF (jvm_env, identifier);
+    if (o_identifier == NULL)
+    {
+      ERROR ("java plugin: cjni_flush: NewStringUTF failed.");
+      return (-1);
+    }
+  }
+
+  ret_status = (*jvm_env)->CallIntMethod (jvm_env,
+      cbi->object, cbi->method, (jint) timeout, o_identifier);
+
+  (*jvm_env)->DeleteLocalRef (jvm_env, o_identifier);
+
+  status = cjni_thread_detach ();
+  if (status != 0)
+  {
+    ERROR ("java plugin: cjni_flush: cjni_thread_detach failed.");
+    return (-1);
+  }
+
+  return (ret_status);
+} /* }}} int cjni_flush */
+
+/* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */
+static void cjni_log (int severity, const char *message, /* {{{ */
+    user_data_t *ud)
+{
+  JNIEnv *jvm_env;
+  cjni_callback_info_t *cbi;
+  jobject o_message;
+
+  if (jvm == NULL)
+    return;
+
+  if ((ud == NULL) || (ud->data == NULL))
+    return;
+
+  jvm_env = cjni_thread_attach ();
+  if (jvm_env == NULL)
+    return;
+
+  cbi = (cjni_callback_info_t *) ud->data;
+
+  o_message = (*jvm_env)->NewStringUTF (jvm_env, message);
+  if (o_message == NULL)
+    return;
+
+  (*jvm_env)->CallVoidMethod (jvm_env,
+      cbi->object, cbi->method, (jint) severity, o_message);
+
+  (*jvm_env)->DeleteLocalRef (jvm_env, o_message);
+
+  cjni_thread_detach ();
+} /* }}} void cjni_log */
+
+/* Call the CB_TYPE_NOTIFICATION callback pointed to by the `user_data_t'
+ * pointer. */
+static int cjni_notification (const notification_t *n, /* {{{ */
+    user_data_t *ud)
+{
+  JNIEnv *jvm_env;
+  cjni_callback_info_t *cbi;
+  jobject o_notification;
+  int status;
+  int ret_status;
+
+  if (jvm == NULL)
+  {
+    ERROR ("java plugin: cjni_read: jvm == NULL");
+    return (-1);
+  }
+
+  if ((ud == NULL) || (ud->data == NULL))
+  {
+    ERROR ("java plugin: cjni_read: Invalid user data.");
+    return (-1);
+  }
+
+  jvm_env = cjni_thread_attach ();
+  if (jvm_env == NULL)
+    return (-1);
+
+  cbi = (cjni_callback_info_t *) ud->data;
+
+  o_notification = ctoj_notification (jvm_env, n);
+  if (o_notification == NULL)
+  {
+    ERROR ("java plugin: cjni_notification: ctoj_notification failed.");
+    return (-1);
+  }
+
+  ret_status = (*jvm_env)->CallIntMethod (jvm_env,
+      cbi->object, cbi->method, o_notification);
+
+  (*jvm_env)->DeleteLocalRef (jvm_env, o_notification);
+
+  status = cjni_thread_detach ();
+  if (status != 0)
+  {
+    ERROR ("java plugin: cjni_read: cjni_thread_detach failed.");
+    return (-1);
+  }
+
+  return (ret_status);
+} /* }}} int cjni_notification */
+
 /* Iterate over `java_classes_list' and create one object of each class. This
  * will trigger the object's constructors, to the objects can register callback
  * methods. */
@@ -2110,6 +2549,7 @@ static int cjni_shutdown (void) /* {{{ */
   sfree (java_classes_list);
 
   /* Destroy the JVM */
+  DEBUG ("java plugin: Destroying the JVM.");
   (*jvm)->DestroyJavaVM (jvm);
   jvm = NULL;
   jvm_env = NULL;
@@ -2141,10 +2581,10 @@ static int cjni_init_native (JNIEnv *jvm_env) /* {{{ */
   jclass api_class_ptr;
   int status;
 
-  api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.CollectdAPI");
+  api_class_ptr = (*jvm_env)->FindClass (jvm_env, "org.collectd.api.Collectd");
   if (api_class_ptr == NULL)
   {
-    ERROR ("cjni_init_native: Cannot find API class `org.collectd.api.CollectdAPI'.");
+    ERROR ("cjni_init_native: Cannot find API class `org.collectd.api.Collectd'.");
     return (-1);
   }