1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/*
* Copyright (c) 2010-2015 Balabit
* Copyright (c) 2010-2015 Viktor Juhasz <viktor.juhasz@balabit.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "java-logmsg-proxy.h"
#include "java_machine.h"
#include "messages.h"
#include "str-utils.h"
#include "logmsg/logmsg.h"
#define LOG_MESSAGE "org.syslog_ng.LogMessage"
struct _JavaLogMessageProxy
{
JavaVMSingleton *java_machine;
jclass loaded_class;
jmethodID mi_constructor;
};
JNIEXPORT void JNICALL
Java_org_syslog_1ng_LogMessage_unref(JNIEnv *env, jobject obj, jlong handle)
{
LogMessage *msg = (LogMessage *)handle;
log_msg_unref(msg);
}
JNIEXPORT jstring JNICALL
Java_org_syslog_1ng_LogMessage_getValue(JNIEnv *env, jobject obj, jlong handle, jstring name)
{
LogMessage *msg = (LogMessage *)handle;
const gchar *value;
gssize value_len;
const char *name_str = (*env)->GetStringUTFChars(env, name, NULL);
if (name_str == NULL)
{
return NULL;
}
value = log_msg_get_value_by_name(msg, name_str, &value_len);
(*env)->ReleaseStringUTFChars(env, name, name_str);
if (value)
{
APPEND_ZERO(value, value, value_len);
return (*env)->NewStringUTF(env, value);
}
else
{
return NULL;
}
}
static gboolean
__load_object(JavaLogMessageProxy *self)
{
JNIEnv *java_env = java_machine_get_env(self->java_machine);
self->loaded_class = java_machine_load_class(self->java_machine, LOG_MESSAGE, NULL);
if (!self->loaded_class)
{
msg_error("Can't find class",
evt_tag_str("class_name", LOG_MESSAGE));
return FALSE;
}
self->mi_constructor = CALL_JAVA_FUNCTION(java_env, GetMethodID, self->loaded_class, "<init>", "(J)V");
if (!self->mi_constructor)
{
msg_error("Can't find default constructor for class",
evt_tag_str("class_name", LOG_MESSAGE));
return FALSE;
}
return TRUE;
}
jobject
java_log_message_proxy_create_java_object(JavaLogMessageProxy *self, LogMessage *msg)
{
JNIEnv *java_env = java_machine_get_env(self->java_machine);
jobject jmsg = CALL_JAVA_FUNCTION(java_env, NewObject, self->loaded_class, self->mi_constructor, log_msg_ref(msg));
if (!jmsg)
{
msg_error("Can't create object",
evt_tag_str("class_name", LOG_MESSAGE));
}
return jmsg;
}
void
java_log_message_proxy_free(JavaLogMessageProxy *self)
{
JNIEnv *env = java_machine_get_env(self->java_machine);
if (self->loaded_class)
{
CALL_JAVA_FUNCTION(env, DeleteLocalRef, self->loaded_class);
}
java_machine_unref(self->java_machine);
g_free(self);
}
JavaLogMessageProxy *
java_log_message_proxy_new(void)
{
JavaLogMessageProxy *self = g_new0(JavaLogMessageProxy, 1);
self->java_machine = java_machine_ref();
if (!__load_object(self))
{
goto error;
}
return self;
error:
java_log_message_proxy_free(self);
return NULL;
}
|