nxt_java.c (1488:6976d36be926) nxt_java.c (1489:4a3ec07f4b19)
1
2/*
3 * Copyright (C) NGINX, Inc.
4 */
5
6
7#include <jni.h>
8

--- 12 unchanged lines hidden (view full) ---

21#include "java/nxt_jni_Context.h"
22#include "java/nxt_jni_Request.h"
23#include "java/nxt_jni_Response.h"
24#include "java/nxt_jni_InputStream.h"
25#include "java/nxt_jni_OutputStream.h"
26#include "java/nxt_jni_URLClassLoader.h"
27
28#include "nxt_jars.h"
1
2/*
3 * Copyright (C) NGINX, Inc.
4 */
5
6
7#include <jni.h>
8

--- 12 unchanged lines hidden (view full) ---

21#include "java/nxt_jni_Context.h"
22#include "java/nxt_jni_Request.h"
23#include "java/nxt_jni_Response.h"
24#include "java/nxt_jni_InputStream.h"
25#include "java/nxt_jni_OutputStream.h"
26#include "java/nxt_jni_URLClassLoader.h"
27
28#include "nxt_jars.h"
29#include "nxt_java_mounts.h"
29
30static nxt_int_t nxt_java_setup(nxt_task_t *task, nxt_process_t *process,
31 nxt_common_app_conf_t *conf);
32static nxt_int_t nxt_java_start(nxt_task_t *task,
33 nxt_process_data_t *data);
34static void nxt_java_request_handler(nxt_unit_request_info_t *req);
35static void nxt_java_websocket_handler(nxt_unit_websocket_frame_t *ws);
36static void nxt_java_close_handler(nxt_unit_request_info_t *req);

--- 8 unchanged lines hidden (view full) ---

45#define NXT_STRING(x) _NXT_STRING(x)
46#define _NXT_STRING(x) #x
47
48NXT_EXPORT nxt_app_module_t nxt_app_module = {
49 sizeof(compat),
50 compat,
51 nxt_string("java"),
52 NXT_STRING(NXT_JAVA_VERSION),
30
31static nxt_int_t nxt_java_setup(nxt_task_t *task, nxt_process_t *process,
32 nxt_common_app_conf_t *conf);
33static nxt_int_t nxt_java_start(nxt_task_t *task,
34 nxt_process_data_t *data);
35static void nxt_java_request_handler(nxt_unit_request_info_t *req);
36static void nxt_java_websocket_handler(nxt_unit_websocket_frame_t *ws);
37static void nxt_java_close_handler(nxt_unit_request_info_t *req);

--- 8 unchanged lines hidden (view full) ---

46#define NXT_STRING(x) _NXT_STRING(x)
47#define _NXT_STRING(x) #x
48
49NXT_EXPORT nxt_app_module_t nxt_app_module = {
50 sizeof(compat),
51 compat,
52 nxt_string("java"),
53 NXT_STRING(NXT_JAVA_VERSION),
54 nxt_java_mounts,
55 nxt_nitems(nxt_java_mounts),
53 nxt_java_setup,
54 nxt_java_start,
55};
56
57typedef struct {
58 JNIEnv *env;
59 jobject ctx;
60} nxt_java_data_t;
61
62
63static nxt_int_t
64nxt_java_setup(nxt_task_t *task, nxt_process_t *process,
65 nxt_common_app_conf_t *conf)
66{
56 nxt_java_setup,
57 nxt_java_start,
58};
59
60typedef struct {
61 JNIEnv *env;
62 jobject ctx;
63} nxt_java_data_t;
64
65
66static nxt_int_t
67nxt_java_setup(nxt_task_t *task, nxt_process_t *process,
68 nxt_common_app_conf_t *conf)
69{
70 char *path, *relpath, *p, *rootfs;
71 size_t jars_dir_len, rootfs_len;
67 const char *unit_jars;
68
72 const char *unit_jars;
73
74 rootfs = (char *) process->isolation.rootfs;
75 rootfs_len = 0;
76
69 unit_jars = conf->u.java.unit_jars;
70 if (unit_jars == NULL) {
77 unit_jars = conf->u.java.unit_jars;
78 if (unit_jars == NULL) {
71 unit_jars = NXT_JARS;
79 if (rootfs != NULL) {
80 unit_jars = "/";
81 } else {
82 unit_jars = NXT_JARS;
83 }
72 }
73
84 }
85
74 nxt_java_modules = realpath(unit_jars, NULL);
75 if (nxt_java_modules == NULL) {
76 nxt_alert(task, "realpath(%s) failed: %E", unit_jars, nxt_errno);
86 relpath = strdup(unit_jars);
87 if (nxt_slow_path(relpath == NULL)) {
77 return NXT_ERROR;
78 }
79
88 return NXT_ERROR;
89 }
90
91 if (rootfs != NULL) {
92 jars_dir_len = strlen(unit_jars);
93 rootfs_len = strlen(rootfs);
94
95 path = nxt_malloc(jars_dir_len + rootfs_len + 1);
96 if (nxt_slow_path(path == NULL)) {
97 free(relpath);
98 return NXT_ERROR;
99 }
100
101 p = nxt_cpymem(path, process->isolation.rootfs, rootfs_len);
102 p = nxt_cpymem(p, relpath, jars_dir_len);
103 *p = '\0';
104
105 free(relpath);
106
107 } else {
108 path = relpath;
109 }
110
111 nxt_java_modules = realpath(path, NULL);
112 if (nxt_java_modules == NULL) {
113 nxt_alert(task, "realpath(\"%s\") failed %E", path, nxt_errno);
114 goto free;
115 }
116
117 if (rootfs != NULL && strlen(path) > rootfs_len) {
118 nxt_java_modules = path + rootfs_len;
119 }
120
121 nxt_debug(task, "JAVA MODULES: %s", nxt_java_modules);
122
80 return NXT_OK;
123 return NXT_OK;
124
125free:
126
127 nxt_free(path);
128
129 return NXT_ERROR;
81}
82
83
84static char **
85nxt_java_module_jars(const char *jars[], int jar_count)
86{
87 char **res, *jurl;
130}
131
132
133static char **
134nxt_java_module_jars(const char *jars[], int jar_count)
135{
136 char **res, *jurl;
137 uint8_t pathsep;
88 nxt_int_t modules_len, jlen, i;
89 const char **jar;
90
91 res = nxt_malloc(jar_count * sizeof(char*));
92 if (res == NULL) {
93 return NULL;
94 }
95
96 modules_len = nxt_strlen(nxt_java_modules);
97
138 nxt_int_t modules_len, jlen, i;
139 const char **jar;
140
141 res = nxt_malloc(jar_count * sizeof(char*));
142 if (res == NULL) {
143 return NULL;
144 }
145
146 modules_len = nxt_strlen(nxt_java_modules);
147
148 pathsep = nxt_java_modules[modules_len - 1] == '/';
149
98 for (i = 0, jar = jars; *jar != NULL; jar++) {
150 for (i = 0, jar = jars; *jar != NULL; jar++) {
99 jlen = nxt_length("file:") + modules_len + nxt_length("/")
100 + nxt_strlen(*jar) + 1;
151 jlen = nxt_length("file:") + modules_len
152 + (!pathsep ? nxt_length("/") : 0)
153 + nxt_strlen(*jar) + 1;
154
101 jurl = nxt_malloc(jlen);
102 if (jurl == NULL) {
103 return NULL;
104 }
105
106 res[i++] = jurl;
107
108 jurl = nxt_cpymem(jurl, "file:", nxt_length("file:"));
109 jurl = nxt_cpymem(jurl, nxt_java_modules, modules_len);
155 jurl = nxt_malloc(jlen);
156 if (jurl == NULL) {
157 return NULL;
158 }
159
160 res[i++] = jurl;
161
162 jurl = nxt_cpymem(jurl, "file:", nxt_length("file:"));
163 jurl = nxt_cpymem(jurl, nxt_java_modules, modules_len);
110 *jurl++ = '/';
164
165 if (!pathsep) {
166 *jurl++ = '/';
167 }
168
111 jurl = nxt_cpymem(jurl, *jar, nxt_strlen(*jar));
112 *jurl++ = '\0';
113 }
114
115 return res;
116}
117
118

--- 418 unchanged lines hidden ---
169 jurl = nxt_cpymem(jurl, *jar, nxt_strlen(*jar));
170 *jurl++ = '\0';
171 }
172
173 return res;
174}
175
176

--- 418 unchanged lines hidden ---