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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
/*
* Copyright (C) NGINX, Inc.
*/
#include <jni.h>
#include <nxt_main.h>
#include <nxt_runtime.h>
#include <nxt_router.h>
#include <nxt_unit.h>
#include <nxt_unit_field.h>
#include <nxt_unit_request.h>
#include <nxt_unit_response.h>
#include <java/nxt_jni.h>
#include "java/nxt_jni_Thread.h"
#include "java/nxt_jni_Context.h"
#include "java/nxt_jni_Request.h"
#include "java/nxt_jni_Response.h"
#include "java/nxt_jni_InputStream.h"
#include "java/nxt_jni_OutputStream.h"
#include "java/nxt_jni_URLClassLoader.h"
#include "nxt_jars.h"
static nxt_int_t nxt_java_pre_init(nxt_task_t *task,
nxt_common_app_conf_t *conf);
static nxt_int_t nxt_java_init(nxt_task_t *task, nxt_common_app_conf_t *conf);
static void nxt_java_request_handler(nxt_unit_request_info_t *req);
static uint32_t compat[] = {
NXT_VERNUM, NXT_DEBUG,
};
char *nxt_java_modules;
#define NXT_STRING(x) _NXT_STRING(x)
#define _NXT_STRING(x) #x
NXT_EXPORT nxt_app_module_t nxt_app_module = {
sizeof(compat),
compat,
nxt_string("java"),
NXT_STRING(NXT_JAVA_VERSION),
nxt_java_pre_init,
nxt_java_init,
};
typedef struct {
JNIEnv *env;
jobject ctx;
} nxt_java_data_t;
static nxt_int_t
nxt_java_pre_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
{
const char *unit_jars;
unit_jars = conf->u.java.unit_jars;
if (unit_jars == NULL) {
unit_jars = NXT_JARS;
}
nxt_java_modules = realpath(unit_jars, NULL);
if (nxt_java_modules == NULL) {
nxt_alert(task, "realpath(%s) failed: %E", unit_jars, nxt_errno);
return NXT_ERROR;
}
return NXT_OK;
}
static char **
nxt_java_module_jars(const char *jars[], int jar_count)
{
char **res, *jurl;
nxt_int_t modules_len, jlen, i;
const char **jar;
res = nxt_malloc(jar_count * sizeof(char*));
if (res == NULL) {
return NULL;
}
modules_len = nxt_strlen(nxt_java_modules);
for (i = 0, jar = jars; *jar != NULL; jar++) {
jlen = nxt_length("file:") + modules_len + nxt_length("/")
+ nxt_strlen(*jar) + 1;
jurl = nxt_malloc(jlen);
if (jurl == NULL) {
return NULL;
}
res[i++] = jurl;
jurl = nxt_cpymem(jurl, "file:", nxt_length("file:"));
jurl = nxt_cpymem(jurl, nxt_java_modules, modules_len);
*jurl++ = '/';
jurl = nxt_cpymem(jurl, *jar, nxt_strlen(*jar));
*jurl++ = '\0';
}
return res;
}
static nxt_int_t
nxt_java_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
{
jint rc;
char *opt, *real_path;
char **classpath_arr, **unit_jars, **system_jars;
JavaVM *jvm;
JNIEnv *env;
jobject cl, classpath;
nxt_str_t str;
nxt_int_t opt_len, real_path_len;
nxt_uint_t i, unit_jars_count, classpath_count, system_jars_count;
JavaVMOption *jvm_opt;
JavaVMInitArgs jvm_args;
nxt_unit_ctx_t *ctx;
nxt_unit_init_t java_init;
nxt_java_data_t data;
nxt_conf_value_t *value;
nxt_java_app_conf_t *c;
//setenv("ASAN_OPTIONS", "handle_segv=0", 1);
jvm_args.version = JNI_VERSION_1_6;
jvm_args.nOptions = 0;
jvm_args.ignoreUnrecognized = 0;
c = &conf->u.java;
if (c->options != NULL) {
jvm_args.nOptions += nxt_conf_array_elements_count(c->options);
}
jvm_opt = nxt_malloc(jvm_args.nOptions * sizeof(JavaVMOption));
if (jvm_opt == NULL) {
nxt_alert(task, "failed to allocate jvm_opt");
return NXT_ERROR;
}
jvm_args.options = jvm_opt;
unit_jars_count = nxt_nitems(nxt_java_unit_jars) - 1;
unit_jars = nxt_java_module_jars(nxt_java_unit_jars, unit_jars_count);
if (unit_jars == NULL) {
nxt_alert(task, "failed to allocate buffer for unit_jars array");
return NXT_ERROR;
}
system_jars_count = nxt_nitems(nxt_java_system_jars) - 1;
system_jars = nxt_java_module_jars(nxt_java_system_jars, system_jars_count);
if (system_jars == NULL) {
nxt_alert(task, "failed to allocate buffer for system_jars array");
return NXT_ERROR;
}
if (c->options != NULL) {
for (i = 0; /* void */ ; i++) {
value = nxt_conf_get_array_element(c->options, i);
if (value == NULL) {
break;
}
nxt_conf_get_string(value, &str);
opt = nxt_malloc(str.length + 1);
if (opt == NULL) {
nxt_alert(task, "failed to allocate jvm_opt");
return NXT_ERROR;
}
memcpy(opt, str.start, str.length);
opt[str.length] = '\0';
jvm_opt[i].optionString = opt;
}
}
if (c->classpath != NULL) {
classpath_count = nxt_conf_array_elements_count(c->classpath);
classpath_arr = nxt_malloc(classpath_count * sizeof(char *));
for (i = 0; /* void */ ; i++) {
value = nxt_conf_get_array_element(c->classpath, i);
if (value == NULL) {
break;
}
nxt_conf_get_string(value, &str);
opt_len = str.length + 1;
char *sc = memchr(str.start, ':', str.length);
if (sc == NULL && str.start[0] == '/') {
opt_len += nxt_length("file:");
}
opt = nxt_malloc(opt_len);
if (opt == NULL) {
nxt_alert(task, "failed to allocate classpath");
return NXT_ERROR;
}
if (sc == NULL && str.start[0] != '/') {
nxt_memcpy(opt, str.start, str.length);
opt[str.length] = '\0';
real_path = realpath(opt, NULL);
if (real_path == NULL) {
nxt_alert(task, "realpath(%s) failed: %E", opt, nxt_errno);
return NXT_ERROR;
}
real_path_len = nxt_strlen(real_path);
free(opt);
opt_len = nxt_length("file:") + real_path_len + 1;
opt = nxt_malloc(opt_len);
if (opt == NULL) {
nxt_alert(task, "failed to allocate classpath");
return NXT_ERROR;
}
} else {
real_path = (char *) str.start; /* I love this cast! */
real_path_len = str.length;
}
classpath_arr[i] = opt;
if (sc == NULL) {
opt = nxt_cpymem(opt, "file:", nxt_length("file:"));
}
opt = nxt_cpymem(opt, real_path, real_path_len);
*opt = '\0';
}
} else {
classpath_count = 0;
classpath_arr = NULL;
}
rc = JNI_CreateJavaVM(&jvm, (void **) &env, &jvm_args);
if (rc != JNI_OK) {
nxt_alert(task, "failed to create Java VM: %d", (int) rc);
return NXT_ERROR;
}
rc = nxt_java_initThread(env);
if (rc != NXT_UNIT_OK) {
nxt_alert(task, "nxt_java_initThread() failed");
goto env_failed;
}
rc = nxt_java_initURLClassLoader(env);
if (rc != NXT_UNIT_OK) {
nxt_alert(task, "nxt_java_initURLClassLoader() failed");
goto env_failed;
}
cl = nxt_java_newURLClassLoader(env, system_jars_count, system_jars);
if (cl == NULL) {
nxt_alert(task, "nxt_java_newURLClassLoader failed");
goto env_failed;
}
nxt_java_setContextClassLoader(env, cl);
cl = nxt_java_newURLClassLoader_parent(env, unit_jars_count, unit_jars, cl);
if (cl == NULL) {
nxt_alert(task, "nxt_java_newURLClassLoader_parent failed");
goto env_failed;
}
nxt_java_setContextClassLoader(env, cl);
rc = nxt_java_initContext(env, cl);
if (rc != NXT_UNIT_OK) {
nxt_alert(task, "nxt_java_initContext() failed");
goto env_failed;
}
rc = nxt_java_initRequest(env, cl);
if (rc != NXT_UNIT_OK) {
nxt_alert(task, "nxt_java_initRequest() failed");
goto env_failed;
}
rc = nxt_java_initResponse(env, cl);
if (rc != NXT_UNIT_OK) {
nxt_alert(task, "nxt_java_initResponse() failed");
goto env_failed;
}
rc = nxt_java_initInputStream(env, cl);
if (rc != NXT_UNIT_OK) {
nxt_alert(task, "nxt_java_initInputStream() failed");
goto env_failed;
}
rc = nxt_java_initOutputStream(env, cl);
if (rc != NXT_UNIT_OK) {
nxt_alert(task, "nxt_java_initOutputStream() failed");
goto env_failed;
}
nxt_java_jni_init(env);
if (rc != NXT_UNIT_OK) {
nxt_alert(task, "nxt_java_jni_init() failed");
goto env_failed;
}
classpath = nxt_java_newURLs(env, classpath_count, classpath_arr);
if (classpath == NULL) {
nxt_alert(task, "nxt_java_newURLs failed");
goto env_failed;
}
data.env = env;
data.ctx = nxt_java_startContext(env, c->webapp, classpath);
if ((*env)->ExceptionCheck(env)) {
nxt_alert(task, "Unhandled exception in application start");
(*env)->ExceptionDescribe(env);
return NXT_ERROR;
}
nxt_unit_default_init(task, &java_init);
java_init.callbacks.request_handler = nxt_java_request_handler;
java_init.request_data_size = sizeof(nxt_java_request_data_t);
java_init.data = &data;
ctx = nxt_unit_init(&java_init);
if (nxt_slow_path(ctx == NULL)) {
nxt_alert(task, "nxt_unit_init() failed");
return NXT_ERROR;
}
rc = nxt_unit_run(ctx);
if (nxt_slow_path(rc != NXT_UNIT_OK)) {
/* TODO report error */
}
nxt_unit_done(ctx);
nxt_java_stopContext(env, data.ctx);
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionDescribe(env);
}
(*jvm)->DestroyJavaVM(jvm);
exit(0);
return NXT_OK;
env_failed:
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionDescribe(env);
}
return NXT_ERROR;
}
static void
nxt_java_request_handler(nxt_unit_request_info_t *req)
{
JNIEnv *env;
jobject jreq, jresp;
nxt_java_data_t *java_data;
nxt_java_request_data_t *data;
java_data = req->unit->data;
env = java_data->env;
data = req->data;
jreq = nxt_java_newRequest(env, java_data->ctx, req);
if (jreq == NULL) {
nxt_unit_req_alert(req, "failed to create Request instance");
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
}
nxt_unit_request_done(req, NXT_UNIT_ERROR);
return;
}
jresp = nxt_java_newResponse(env, req);
if (jresp == NULL) {
nxt_unit_req_alert(req, "failed to create Response instance");
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
}
(*env)->DeleteLocalRef(env, jreq);
nxt_unit_request_done(req, NXT_UNIT_ERROR);
return;
}
data->header_size = 10 * 1024;
data->buf_size = 32 * 1024; /* from Jetty */
data->jreq = jreq;
data->jresp = jresp;
data->buf = NULL;
nxt_unit_request_group_dup_fields(req);
nxt_java_service(env, java_data->ctx, jreq, jresp);
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
}
if (!nxt_unit_response_is_init(req)) {
nxt_unit_response_init(req, 200, 0, 0);
}
if (!nxt_unit_response_is_sent(req)) {
nxt_unit_response_send(req);
}
if (data->buf != NULL) {
nxt_unit_buf_send(data->buf);
data->buf = NULL;
}
(*env)->DeleteLocalRef(env, jresp);
(*env)->DeleteLocalRef(env, jreq);
nxt_unit_request_done(req, NXT_UNIT_OK);
}
|