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
|
/*
* Copyright (C) NGINX, Inc.
*/
#include <nxt_auto_config.h>
#include <jni.h>
#include <nxt_unit.h>
#include <nxt_unit_field.h>
#include "nxt_jni.h"
static jclass nxt_java_NoSuchElementException_class;
static jclass nxt_java_IOException_class;
static jclass nxt_java_IllegalStateException_class;
static jclass nxt_java_File_class;
static jmethodID nxt_java_File_ctor;
static inline char nxt_java_lowcase(char c);
int
nxt_java_jni_init(JNIEnv *env)
{
jclass cls;
cls = (*env)->FindClass(env, "java/util/NoSuchElementException");
if (cls == NULL) {
return NXT_UNIT_ERROR;
}
nxt_java_NoSuchElementException_class = (*env)->NewGlobalRef(env, cls);
(*env)->DeleteLocalRef(env, cls);
cls = (*env)->FindClass(env, "java/io/IOException");
if (cls == NULL) {
(*env)->DeleteGlobalRef(env, nxt_java_NoSuchElementException_class);
return NXT_UNIT_ERROR;
}
nxt_java_IOException_class = (*env)->NewGlobalRef(env, cls);
(*env)->DeleteLocalRef(env, cls);
cls = (*env)->FindClass(env, "java/lang/IllegalStateException");
if (cls == NULL) {
(*env)->DeleteGlobalRef(env, nxt_java_NoSuchElementException_class);
(*env)->DeleteGlobalRef(env, nxt_java_IOException_class);
return NXT_UNIT_ERROR;
}
nxt_java_IllegalStateException_class = (*env)->NewGlobalRef(env, cls);
(*env)->DeleteLocalRef(env, cls);
cls = (*env)->FindClass(env, "java/io/File");
if (cls == NULL) {
(*env)->DeleteGlobalRef(env, nxt_java_NoSuchElementException_class);
(*env)->DeleteGlobalRef(env, nxt_java_IOException_class);
(*env)->DeleteGlobalRef(env, nxt_java_IllegalStateException_class);
return NXT_UNIT_ERROR;
}
nxt_java_File_class = (*env)->NewGlobalRef(env, cls);
(*env)->DeleteLocalRef(env, cls);
nxt_java_File_ctor = (*env)->GetMethodID(env, nxt_java_File_class, "<init>",
"(Ljava/lang/String;)V");
if (nxt_java_File_ctor == NULL) {
(*env)->DeleteGlobalRef(env, nxt_java_NoSuchElementException_class);
(*env)->DeleteGlobalRef(env, nxt_java_IOException_class);
(*env)->DeleteGlobalRef(env, nxt_java_IllegalStateException_class);
(*env)->DeleteGlobalRef(env, nxt_java_File_class);
return NXT_UNIT_ERROR;
}
return NXT_UNIT_OK;
}
void
nxt_java_throw_NoSuchElementException(JNIEnv *env, const char *msg)
{
(*env)->ThrowNew(env, nxt_java_NoSuchElementException_class, msg);
}
void
nxt_java_throw_IOException(JNIEnv *env, const char *msg)
{
(*env)->ThrowNew(env, nxt_java_IOException_class, msg);
}
void
nxt_java_throw_IllegalStateException(JNIEnv *env, const char *msg)
{
(*env)->ThrowNew(env, nxt_java_IllegalStateException_class, msg);
}
nxt_unit_field_t *
nxt_java_findHeader(nxt_unit_field_t *f, nxt_unit_field_t *end,
const char *name, uint8_t name_len)
{
const char *field_name;
for (/* void */ ; f < end; f++) {
if (f->skip != 0 || f->name_length != name_len) {
continue;
}
field_name = nxt_unit_sptr_get(&f->name);
if (nxt_java_strcaseeq(name, field_name, name_len)) {
return f;
}
}
return NULL;
}
int
nxt_java_strcaseeq(const char *str1, const char *str2, int len)
{
char c1, c2;
const char *end1;
end1 = str1 + len;
while (str1 < end1) {
c1 = nxt_java_lowcase(*str1++);
c2 = nxt_java_lowcase(*str2++);
if (c1 != c2) {
return 0;
}
}
return 1;
}
static inline char
nxt_java_lowcase(char c)
{
return (c >= 'A' && c <= 'Z') ? c | 0x20 : c;
}
jstring
nxt_java_newString(JNIEnv *env, char *str, uint32_t len)
{
char tmp;
jstring res;
tmp = str[len];
if (tmp != '\0') {
str[len] = '\0';
}
res = (*env)->NewStringUTF(env, str);
if (tmp != '\0') {
str[len] = tmp;
}
return res;
}
|