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
|
/*
* Copyright (C) NGINX, Inc.
*/
#include <nxt_auto_config.h>
#include <nxt_unit.h>
#include <jni.h>
#include "nxt_jni.h"
#include "nxt_jni_Context.h"
#include "nxt_jni_URLClassLoader.h"
static jclass nxt_java_Context_class;
static jmethodID nxt_java_Context_start;
static jmethodID nxt_java_Context_service;
static jmethodID nxt_java_Context_stop;
static void JNICALL nxt_java_Context_log(JNIEnv *env, jclass cls,
jlong ctx_ptr, jstring msg, jint msg_len);
static void JNICALL nxt_java_Context_trace(JNIEnv *env, jclass cls,
jlong ctx_ptr, jstring msg, jint msg_len);
int
nxt_java_initContext(JNIEnv *env, jobject cl)
{
int res;
jclass cls;
cls = nxt_java_loadClass(env, cl, "nginx.unit.Context");
if (cls == NULL) {
nxt_unit_warn(NULL, "nginx.unit.Context not found");
return NXT_UNIT_ERROR;
}
nxt_java_Context_class = (*env)->NewGlobalRef(env, cls);
(*env)->DeleteLocalRef(env, cls);
cls = nxt_java_Context_class;
nxt_java_Context_start = (*env)->GetStaticMethodID(env, cls, "start",
"(Ljava/lang/String;[Ljava/net/URL;)Lnginx/unit/Context;");
if (nxt_java_Context_start == NULL) {
nxt_unit_warn(NULL, "nginx.unit.Context.start() not found");
goto failed;
}
nxt_java_Context_service = (*env)->GetMethodID(env, cls, "service",
"(Lnginx/unit/Request;Lnginx/unit/Response;)V");
if (nxt_java_Context_service == NULL) {
nxt_unit_warn(NULL, "nginx.unit.Context.service() not found");
goto failed;
}
nxt_java_Context_stop = (*env)->GetMethodID(env, cls, "stop", "()V");
if (nxt_java_Context_stop == NULL) {
nxt_unit_warn(NULL, "nginx.unit.Context.stop() not found");
goto failed;
}
JNINativeMethod context_methods[] = {
{ (char *) "log",
(char *) "(JLjava/lang/String;I)V",
nxt_java_Context_log },
{ (char *) "trace",
(char *) "(JLjava/lang/String;I)V",
nxt_java_Context_trace },
};
res = (*env)->RegisterNatives(env, nxt_java_Context_class,
context_methods,
sizeof(context_methods)
/ sizeof(context_methods[0]));
nxt_unit_debug(NULL, "registered Context methods: %d", res);
if (res != 0) {
nxt_unit_warn(NULL, "registering natives for Context failed");
goto failed;
}
return NXT_UNIT_OK;
failed:
(*env)->DeleteGlobalRef(env, cls);
return NXT_UNIT_ERROR;
}
jobject
nxt_java_startContext(JNIEnv *env, const char *webapp, jobject classpaths)
{
jstring webapp_str;
webapp_str = (*env)->NewStringUTF(env, webapp);
if (webapp_str == NULL) {
return NULL;
}
return (*env)->CallStaticObjectMethod(env, nxt_java_Context_class,
nxt_java_Context_start, webapp_str,
classpaths);
}
void
nxt_java_service(JNIEnv *env, jobject ctx, jobject jreq, jobject jresp)
{
(*env)->CallVoidMethod(env, ctx, nxt_java_Context_service, jreq, jresp);
}
void
nxt_java_stopContext(JNIEnv *env, jobject ctx)
{
(*env)->CallVoidMethod(env, ctx, nxt_java_Context_stop);
}
static void JNICALL
nxt_java_Context_log(JNIEnv *env, jclass cls, jlong ctx_ptr, jstring msg,
jint msg_len)
{
const char *msg_str;
nxt_unit_ctx_t *ctx;
ctx = nxt_jlong2ptr(ctx_ptr);
msg_str = (*env)->GetStringUTFChars(env, msg, NULL);
if (msg_str == NULL) {
return;
}
nxt_unit_log(ctx, NXT_UNIT_LOG_INFO, "%.*s", msg_len, msg_str);
(*env)->ReleaseStringUTFChars(env, msg, msg_str);
}
static void JNICALL
nxt_java_Context_trace(JNIEnv *env, jclass cls, jlong ctx_ptr, jstring msg,
jint msg_len)
{
#if (NXT_DEBUG)
const char *msg_str;
nxt_unit_ctx_t *ctx;
ctx = nxt_jlong2ptr(ctx_ptr);
msg_str = (*env)->GetStringUTFChars(env, msg, NULL);
if (msg_str == NULL) {
return;
}
nxt_unit_debug(ctx, "%.*s", msg_len, msg_str);
(*env)->ReleaseStringUTFChars(env, msg, msg_str);
#endif
}
|