File: java-destination-proxy.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (372 lines) | stat: -rw-r--r-- 12,005 bytes parent folder | download | duplicates (3)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Copyright (c) 2010-2014 Balabit
 * Copyright (c) 2010-2014 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-destination-proxy.h"
#include "java-logmsg-proxy.h"
#include "java-class-loader.h"
#include "messages.h"
#include "logthrdest/logthrdestdrv.h"
#include <string.h>


typedef struct _JavaDestinationImpl
{
  jobject dest_object;
  jmethodID mi_constructor;
  jmethodID mi_init;
  jmethodID mi_deinit;
  jmethodID mi_send;
  jmethodID mi_send_msg;
  jmethodID mi_open;
  jmethodID mi_close;
  jmethodID mi_is_opened;
  jmethodID flush;
  jmethodID mi_get_name_by_uniq_options;
} JavaDestinationImpl;

struct _JavaDestinationProxy
{
  JavaVMSingleton *java_machine;
  jclass loaded_class;
  JavaDestinationImpl dest_impl;
  LogTemplate *template;
  gint32 *seq_num;
  GString *formatted_message;
  JavaLogMessageProxy *msg_builder;
  gchar *name_by_uniq_options;
};

static gboolean
__load_destination_object(JavaDestinationProxy *self, const gchar *class_name, const gchar *class_path, gpointer handle)
{
  JNIEnv *java_env = java_machine_get_env(self->java_machine);
  self->loaded_class = java_machine_load_class(self->java_machine, class_name, class_path);
  if (!self->loaded_class)
    {
      msg_error("Can't find class",
                evt_tag_str("class_name", class_name));
      return FALSE;
    }

  self->dest_impl.mi_constructor = CALL_JAVA_FUNCTION(java_env, GetMethodID, self->loaded_class, "<init>", "(J)V");
  if (!self->dest_impl.mi_constructor)
    {
      msg_error("Can't find default constructor for class",
                evt_tag_str("class_name", class_name));
      return FALSE;
    }

  self->dest_impl.mi_init = CALL_JAVA_FUNCTION(java_env, GetMethodID, self->loaded_class, "initProxy", "()Z");
  if (!self->dest_impl.mi_init)
    {
      msg_error("Can't find method in class",
                evt_tag_str("class_name", class_name),
                evt_tag_str("method", "boolean initProxy(SyslogNg)"));
      return FALSE;
    }

  self->dest_impl.mi_deinit = CALL_JAVA_FUNCTION(java_env, GetMethodID, self->loaded_class, "deinitProxy", "()V");
  if (!self->dest_impl.mi_deinit)
    {
      msg_error("Can't find method in class",
                evt_tag_str("class_name", class_name),
                evt_tag_str("method", "void deinitProxy()"));
      return FALSE;
    }

  self->dest_impl.mi_send = CALL_JAVA_FUNCTION(java_env, GetMethodID, self->loaded_class, "sendProxy",
                                               "(Ljava/lang/String;)I");
  self->dest_impl.mi_send_msg = CALL_JAVA_FUNCTION(java_env, GetMethodID, self->loaded_class, "sendProxy",
                                                   "(Lorg/syslog_ng/LogMessage;)I");

  if (!self->dest_impl.mi_send_msg && !self->dest_impl.mi_send)
    {
      msg_error("Can't find any queue method in class",
                evt_tag_str("class_name", class_name),
                evt_tag_str("method", "int sendProxy(String) or int sendProxy(LogMessage)"));
    }

  self->dest_impl.flush = CALL_JAVA_FUNCTION(java_env, GetMethodID, self->loaded_class,
                                             "flushProxy", "()I");
  if (!self->dest_impl.flush)
    {
      msg_error("Can't find method in class",
                evt_tag_str("class_name", class_name),
                evt_tag_str("method", "int flushProxy()"));
    }

  self->dest_impl.mi_open = CALL_JAVA_FUNCTION(java_env, GetMethodID, self->loaded_class, "openProxy", "()Z");
  if (!self->dest_impl.mi_open)
    {
      msg_error("Can't find method in class",
                evt_tag_str("class_name", class_name),
                evt_tag_str("method", "boolean openProxy()"));
    }

  self->dest_impl.mi_close = CALL_JAVA_FUNCTION(java_env, GetMethodID, self->loaded_class, "closeProxy", "()V");
  if (!self->dest_impl.mi_close)
    {
      msg_error("Can't find method in class",
                evt_tag_str("class_name", class_name),
                evt_tag_str("method", "void closeProxy()"));
    }

  self->dest_impl.mi_is_opened = CALL_JAVA_FUNCTION(java_env, GetMethodID, self->loaded_class, "isOpenedProxy", "()Z");
  if (!self->dest_impl.mi_is_opened)
    {
      msg_error("Can't find method in class",
                evt_tag_str("class_name", class_name),
                evt_tag_str("method", "boolean isOpenedProxy()"));
    }

  self->dest_impl.dest_object = CALL_JAVA_FUNCTION(java_env, NewObject, self->loaded_class,
                                                   self->dest_impl.mi_constructor, handle);
  if (!self->dest_impl.dest_object)
    {
      jthrowable exc = CALL_JAVA_FUNCTION_VOID(java_env, ExceptionOccurred);
      if (exc)
        {
          CALL_JAVA_FUNCTION_VOID(java_env, ExceptionDescribe);
          CALL_JAVA_FUNCTION_VOID(java_env, ExceptionClear);
        }
      msg_error("Can't create object",
                evt_tag_str("class_name", class_name));
      return FALSE;
    }

  self->dest_impl.mi_get_name_by_uniq_options = CALL_JAVA_FUNCTION(java_env,
                                                GetMethodID,
                                                self->loaded_class,
                                                "getNameByUniqOptionsProxy",
                                                "()Ljava/lang/String;"
                                                                  );
  if (!self->dest_impl.mi_get_name_by_uniq_options)
    {
      msg_error("Can't get name by unique options",
                evt_tag_str("name", class_name));
      return FALSE;
    }
  return TRUE;
}


void
java_destination_proxy_free(JavaDestinationProxy *self)
{
  JNIEnv *env = java_machine_get_env(self->java_machine);
  if (self->dest_impl.dest_object)
    {
      CALL_JAVA_FUNCTION(env, DeleteLocalRef, self->dest_impl.dest_object);
    }

  if (self->loaded_class)
    {
      CALL_JAVA_FUNCTION(env, DeleteLocalRef, self->loaded_class);
    }

  if (self->msg_builder)
    {
      java_log_message_proxy_free(self->msg_builder);
    }
  java_machine_unref(self->java_machine);
  g_string_free(self->formatted_message, TRUE);
  g_free(self->name_by_uniq_options);
  log_template_unref(self->template);
  g_free(self);
}

JavaDestinationProxy *
java_destination_proxy_new(const gchar *class_name, const gchar *class_path, gpointer handle, LogTemplate *template,
                           gint32 *seq_num, const gchar *jvm_options)
{
  JavaDestinationProxy *self = g_new0(JavaDestinationProxy, 1);
  self->java_machine = java_machine_ref();
  self->formatted_message = g_string_sized_new(1024);
  self->template = log_template_ref(template);
  self->seq_num = seq_num;

  if (!java_machine_start(self->java_machine, jvm_options))
    goto error;

  if (!__load_destination_object(self, class_name, class_path, handle))
    {
      goto error;
    }

  self->msg_builder = java_log_message_proxy_new();
  if (!self->msg_builder)
    {
      goto error;
    }

  return self;
error:
  java_destination_proxy_free(self);
  return NULL;
}

static gint
__queue_native_message(JavaDestinationProxy *self, JNIEnv *env, LogMessage *msg)
{
  jobject jmsg = java_log_message_proxy_create_java_object(self->msg_builder, msg);
  jint res = CALL_JAVA_FUNCTION(env,
                                CallIntMethod,
                                self->dest_impl.dest_object,
                                self->dest_impl.mi_send_msg,
                                jmsg);

  CALL_JAVA_FUNCTION(env, DeleteLocalRef, jmsg);
  return res;
}

static gint
__queue_formatted_message(JavaDestinationProxy *self, JNIEnv *env, LogMessage *msg)
{
  LogTemplateEvalOptions options = {NULL, LTZ_SEND, *self->seq_num, NULL, LM_VT_STRING};
  log_template_format(self->template, msg, &options, self->formatted_message);
  jstring message = CALL_JAVA_FUNCTION(env, NewStringUTF, self->formatted_message->str);
  jint res = CALL_JAVA_FUNCTION(env, CallIntMethod, self->dest_impl.dest_object, self->dest_impl.mi_send,
                                message);
  CALL_JAVA_FUNCTION(env, DeleteLocalRef, message);
  return res;
}

gint
java_destination_proxy_send(JavaDestinationProxy *self, LogMessage *msg)
{
  JNIEnv *env = java_machine_get_env(self->java_machine);
  if (self->dest_impl.mi_send_msg != 0)
    {
      return __queue_native_message(self, env, msg);
    }
  else
    {
      return __queue_formatted_message(self, env, msg);
    }
}

gchar *
java_destination_proxy_get_name_by_uniq_options(JavaDestinationProxy *self)
{
  return self->name_by_uniq_options;
}

static gchar *
__java_str_dup(JNIEnv *env, jstring java_string)
{
  gchar *result = NULL;
  gchar *c_string = NULL;

  c_string = (gchar *) CALL_JAVA_FUNCTION(env, GetStringUTFChars, java_string, NULL);
  if (strlen(c_string) == 0)
    goto exit;

  result = strdup(c_string);
exit:
  CALL_JAVA_FUNCTION(env, ReleaseStringUTFChars, java_string, c_string);
  return result;
}

static gchar *
__get_name_by_uniq_options(JavaDestinationProxy *self)
{
  JNIEnv *env = java_machine_get_env(self->java_machine);
  jstring java_string;

  java_string = (jstring) CALL_JAVA_FUNCTION(env, CallObjectMethod, self->dest_impl.dest_object,
                                             self->dest_impl.mi_get_name_by_uniq_options);
  if (!java_string)
    {
      msg_error("Can't get name by unique options");
      return NULL;
    }

  return __java_str_dup(env, java_string);
}

gboolean
java_destination_proxy_init(JavaDestinationProxy *self)
{
  jboolean result;
  JNIEnv *env = java_machine_get_env(self->java_machine);

  result = CALL_JAVA_FUNCTION(env, CallBooleanMethod, self->dest_impl.dest_object, self->dest_impl.mi_init);

  if (!!(result))
    {
      self->name_by_uniq_options = __get_name_by_uniq_options(self);
      if (!self->name_by_uniq_options)
        {
          msg_error("Name by uniq options is empty");
          result = FALSE;
        }
    }

  return !!(result);
}

void
java_destination_proxy_deinit(JavaDestinationProxy *self)
{
  JNIEnv *env = java_machine_get_env(self->java_machine);

  CALL_JAVA_FUNCTION(env, CallVoidMethod, self->dest_impl.dest_object, self->dest_impl.mi_deinit);
}

gint
java_destination_proxy_flush(JavaDestinationProxy *self)
{
  JNIEnv *env = java_machine_get_env(self->java_machine);
  return CALL_JAVA_FUNCTION(env, CallIntMethod, self->dest_impl.dest_object, self->dest_impl.flush);
}

gboolean
java_destination_proxy_open(JavaDestinationProxy *self)
{
  JNIEnv *env = java_machine_get_env(self->java_machine);

  jboolean res = CALL_JAVA_FUNCTION(env, CallBooleanMethod, self->dest_impl.dest_object, self->dest_impl.mi_open);

  return !!(res);
}

gboolean
java_destination_proxy_is_opened(JavaDestinationProxy *self)
{
  JNIEnv *env = java_machine_get_env(self->java_machine);

  jboolean res = CALL_JAVA_FUNCTION(env, CallBooleanMethod, self->dest_impl.dest_object, self->dest_impl.mi_is_opened);

  return !!(res);
}

void
java_destination_proxy_close(JavaDestinationProxy *self)
{
  JNIEnv *env = java_machine_get_env(self->java_machine);

  CALL_JAVA_FUNCTION(env, CallBooleanMethod, self->dest_impl.dest_object, self->dest_impl.mi_close);

}