summaryrefslogtreecommitdiffhomepage
path: root/auto/modules/java
blob: 27030da1a00c8dec5036acb1c7ab0bdca1a53621 (plain) (blame)
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

# Copyright (C) NGINX, Inc.


shift

NXT_JAVA_HOME=${JAVA_HOME-}
NXT_JAR_REPO=http://central.maven.org/maven2/
NXT_JAR_LOCAL_REPO=$HOME/.m2/repository/

for nxt_option; do

    case "$nxt_option" in
        -*=*) value=`echo "$nxt_option" | sed -e 's/[-_a-zA-Z0-9]*=//'`     ;;
           *) value="" ;;
    esac

    case "$nxt_option" in

        --module=*)     NXT_JAVA_MODULE="$value"                              ;;
        --home=*)       NXT_JAVA_HOME="$value"                                ;;
        --lib-path=*)   NXT_JAVA_LIB_PATH="$value"                            ;;
        --repo=*)       NXT_JAR_REPO="$value"                                 ;;
        --local-repo=*) NXT_JAR_LOCAL_REPO="$value"                           ;;
        --jars=*)       NXT_JARS="$value"                                     ;;

        --help)
            cat << END

    --module=NAME         set unit Java module name
    --home=DIR            set Java home directory
    --lib-path=DIRECTORY  set directory path to libjvm.so library
    --repo=URL            set Maven remote repository URL
                          default: "$NXT_JAR_REPO"
    --local-repo=DIR      set local repository directory
                          default: "$NXT_JAR_LOCAL_REPO"
    --jars=DIR            set jars install/search directory

END
            exit 0
        ;;

        *)
            echo
            echo $0: error: invalid Java option \"$nxt_option\"
            echo
            exit 1
        ;;
    esac

done


if [ ! -f $NXT_AUTOCONF_DATA ]; then
   echo
   echo Please run common $0 before configuring module \"$nxt_module\".
   echo
   exit 1
fi

. $NXT_AUTOCONF_DATA


NXT_JARS=${NXT_JARS=$NXT_MODULES}
NXT_JAVA_MODULE=${NXT_JAVA_MODULE=java}
NXT_JAVA_LIB_PATH=${NXT_JAVA_LIB_PATH=}

$echo "configuring Java module"
$echo "configuring Java module ..." >> $NXT_AUTOCONF_ERR

if [ -n "${NXT_JAVA_HOME}" ]; then
    $echo "using java.home ${NXT_JAVA_HOME}"
    $echo "using java.home ${NXT_JAVA_HOME}" >> $NXT_AUTOCONF_ERR

    if [ ! -d "${NXT_JAVA_HOME}" ]; then
        $echo
        $echo $0: error: Java home directory not found.
        $echo
        exit 1
    fi

    NXT_JAVA="${NXT_JAVA_HOME}/bin/java"

else
    $echo -n "checking for java executable"
    $echo "checking for java executable ..." >> $NXT_AUTOCONF_ERR

    NXT_JAVA=`which java || :`
    if [ -z "$NXT_JAVA" -o ! -x "$NXT_JAVA" ]; then
        $echo
        $echo $0: error: java executable not found.
        $echo
        exit 1
    fi

    $echo " found $NXT_JAVA"
    $echo "found $NXT_JAVA" >> $NXT_AUTOCONF_ERR

    "$NXT_JAVA" -version

    $echo -n "checking java.home"
    $echo "checking java.home ..." >> $NXT_AUTOCONF_ERR

    NXT_JAVA_HOME=`$NXT_JAVA -XshowSettings 2>&1 | grep -F -e java.home | sed -e 's/^.*= //'`
    if [ -z "$NXT_JAVA_HOME" ]; then
        $echo
        $echo $0: error: java.home not found.
        $echo
        exit 1
    fi

    $echo " $NXT_JAVA_HOME"
    $echo "got java.home $NXT_JAVA_HOME" >> $NXT_AUTOCONF_ERR

    if [ ! -x "${NXT_JAVA_HOME}/bin/javac" ]; then
        NXT_JAVA_HOME_=${NXT_JAVA_HOME%/jre}
        if [ -x "${NXT_JAVA_HOME_}/bin/javac" ]; then
            $echo "adjust java.home $NXT_JAVA_HOME_"
            $echo "adjust java.home $NXT_JAVA_HOME_" >> $NXT_AUTOCONF_ERR

            NXT_JAVA_HOME="$NXT_JAVA_HOME_"
        fi
    fi
fi

NXT_JAVAC="${NXT_JAVA_HOME}/bin/javac"

if [ ! -x "$NXT_JAVAC" ]; then
    $echo
    $echo $0: error: javac not found.
    $echo
    exit 1
fi

NXT_JAVA_INCLUDE="-I${NXT_JAVA_HOME}/include"

case "$NXT_SYSTEM" in
    Linux)
        NXT_JAVA_INCLUDE="${NXT_JAVA_INCLUDE} -I${NXT_JAVA_HOME}/include/linux"
        ;;
    Darwin)
        NXT_JAVA_INCLUDE="${NXT_JAVA_INCLUDE} -I${NXT_JAVA_HOME}/include/darwin"
        ;;
    FreeBSD)
        NXT_JAVA_INCLUDE="${NXT_JAVA_INCLUDE} -I${NXT_JAVA_HOME}/include/freebsd"
        ;;
esac

if [ -z "$NXT_JAVA_LIB_PATH" ]; then
    $echo -n "checking library path"
    $echo "checking library path ..." >> $NXT_AUTOCONF_ERR

    if [ ! -x "$NXT_JAVA" ]; then
        $echo
        $echo $0: error: java executable not found.
        $echo
        exit 1
    fi

    NXT_JAVA_LIB_PATH=`$NXT_JAVA -XshowSettings 2>&1 | grep -F -e sun.boot.library.path | sed -e 's/^.*= //'`

    if [ -z "$NXT_JAVA_LIB_PATH" ]; then
        $echo
        $echo $0: error: library path not found.
        $echo
        exit 1
    fi

    NXT_JAVA_LIB_PATH="${NXT_JAVA_LIB_PATH}/server"

    $echo " $NXT_JAVA_LIB_PATH"
    $echo "got library path $NXT_JAVA_LIB_PATH" >> $NXT_AUTOCONF_ERR
fi

NXT_JAVA_LDFLAGS="-L${NXT_JAVA_LIB_PATH} -Wl,-rpath ${NXT_JAVA_LIB_PATH} -ljvm"


nxt_found=no

nxt_feature="JNI"
nxt_feature_name=""
nxt_feature_run=no
nxt_feature_incs="${NXT_JAVA_INCLUDE}"
nxt_feature_libs="${NXT_JAVA_LDFLAGS}"
nxt_feature_test="
    #include <jni.h>

    int main() {
        JNI_CreateJavaVM(NULL, NULL, NULL);
        return 0;
    }"

. auto/feature


if [ $nxt_found = no ]; then
    $echo
    $echo $0: error: no JNI found.
    $echo
    exit 1;
fi

NXT_JAVA_VERSION=`$NXT_JAVAC -version 2>&1`
NXT_JAVA_VERSION=${NXT_JAVA_VERSION#javac }
NXT_JAVA_INCLUDE="$NXT_JAVA_INCLUDE -I$NXT_BUILD_DIR/$NXT_JAVA_MODULE -DNXT_JAVA_VERSION=$NXT_JAVA_VERSION"

if grep ^$NXT_JAVA_MODULE: $NXT_MAKEFILE 2>&1 > /dev/null; then
    $echo
    $echo $0: error: duplicate \"$NXT_JAVA_MODULE\" module configured.
    $echo
    exit 1;
fi

. ./version

NXT_UNIT_JAR=nginx-unit-jsc-${NXT_JAVA_MODULE}-$NXT_VERSION.jar

NXT_JAVA_BUILD_CP=$NXT_BUILD_DIR/$NXT_JAVA_MODULE
NXT_JAVA_INSTALL_JARS=
NXT_JAVA_UNINSTALL_JARS=

NXT_JAVA_JARS=$NXT_BUILD_DIR/$NXT_JAVA_MODULE/nxt_jars.h
mkdir -p $NXT_BUILD_DIR/$NXT_JAVA_MODULE

cat << END > $NXT_JAVA_JARS
#ifndef _NXT_JAVA_JARS_INCLUDED_
#define _NXT_JAVA_JARS_INCLUDED_

#define NXT_JARS "$NXT_JARS"

static const char *nxt_java_system_jars[] = {
END

NXT_TOMCAT_VERSION=9.0.13

NXT_JAR_VERSION=$NXT_TOMCAT_VERSION

NXT_JAR_NAME=tomcat-servlet-api
NXT_JAR_NAMESPACE=org/apache/tomcat/
. auto/modules/java_get_jar

NXT_JAR_NAME=tomcat-el-api
. auto/modules/java_get_jar

NXT_JAR_NAME=tomcat-jsp-api
. auto/modules/java_get_jar

NXT_JAR_NAME=tomcat-jasper
. auto/modules/java_get_jar

NXT_JAR_NAME=tomcat-jasper-el
. auto/modules/java_get_jar

NXT_JAR_NAME=tomcat-juli
. auto/modules/java_get_jar

NXT_JAR_NAME=tomcat-api
. auto/modules/java_get_jar

NXT_JAR_NAME=tomcat-util-scan
. auto/modules/java_get_jar

NXT_JAR_NAME=tomcat-util
. auto/modules/java_get_jar

NXT_JAR_NAME=ecj
NXT_JAR_VERSION=3.13.102
NXT_JAR_NAMESPACE=org/eclipse/jdt/
. auto/modules/java_get_jar

cat << END >> $NXT_JAVA_JARS
    NULL
};

static const char *nxt_java_unit_jars[] = {
    "$NXT_UNIT_JAR",
END

NXT_JAR_VERSION=9.4.12.v20180830
NXT_JAR_NAMESPACE=org/eclipse/jetty/

NXT_JAR_NAME=jetty-util
. auto/modules/java_get_jar

NXT_JAR_NAME=jetty-server
. auto/modules/java_get_jar

NXT_JAR_NAME=jetty-http
. auto/modules/java_get_jar

NXT_JAR_NAME=classgraph
NXT_JAR_VERSION=4.4.11
NXT_JAR_NAMESPACE=io/github/classgraph/
. auto/modules/java_get_jar

cat << END >> $NXT_JAVA_JARS
    NULL
};

#endif /* _NXT_JAVA_JARS_INCLUDED_ */
END

$echo " + Java module: ${NXT_JAVA_MODULE}.unit.so"

. auto/cc/deps

$echo >> $NXT_MAKEFILE

NXT_JAVA_MODULE_SRCS=" \
    src/nxt_java.c \
    src/java/nxt_jni.c \
    src/java/nxt_jni_Context.c \
    src/java/nxt_jni_HeaderNamesEnumeration.c \
    src/java/nxt_jni_HeadersEnumeration.c \
    src/java/nxt_jni_InputStream.c \
    src/java/nxt_jni_OutputStream.c \
    src/java/nxt_jni_Request.c \
    src/java/nxt_jni_Response.c \
    src/java/nxt_jni_Thread.c \
    src/java/nxt_jni_URLClassLoader.c \
"

# The Java module object files.

nxt_objs=$NXT_BUILD_DIR/src/nxt_unit.o

for nxt_src in $NXT_JAVA_MODULE_SRCS; do

    nxt_obj=${nxt_src%.c}-$NXT_JAVA_MODULE.o
    nxt_dep=${nxt_src%.c}-$NXT_JAVA_MODULE.dep
    nxt_dep_flags=`nxt_gen_dep_flags`
    nxt_dep_post=`nxt_gen_dep_post`
    nxt_objs="$nxt_objs $NXT_BUILD_DIR/$nxt_obj"

    cat << END >> $NXT_MAKEFILE

$NXT_BUILD_DIR/$nxt_obj: $nxt_src
	mkdir -p $NXT_BUILD_DIR/src/java
	\$(CC) -c \$(CFLAGS) \$(NXT_INCS) $NXT_JAVA_INCLUDE \\
	$nxt_dep_flags \\
	-o $NXT_BUILD_DIR/$nxt_obj $nxt_src
	$nxt_dep_post

-include $NXT_BUILD_DIR/$nxt_dep

END

done

NXT_JAVA_SRCS=" \
    src/java/nginx/unit/Context.java \
    src/java/nginx/unit/DynamicDispatcherRequest.java \
    src/java/nginx/unit/DynamicPathRequest.java \
    src/java/nginx/unit/ForwardRequestWrapper.java \
    src/java/nginx/unit/HeaderNamesEnumeration.java \
    src/java/nginx/unit/HeadersEnumeration.java \
    src/java/nginx/unit/IncludeRequestWrapper.java \
    src/java/nginx/unit/IncludeResponseWrapper.java \
    src/java/nginx/unit/InitParams.java \
    src/java/nginx/unit/InputStream.java \
    src/java/nginx/unit/JspPropertyGroup.java \
    src/java/nginx/unit/OutputStream.java \
    src/java/nginx/unit/Request.java \
    src/java/nginx/unit/RequestAttrProxy.java \
    src/java/nginx/unit/Response.java \
    src/java/nginx/unit/Session.java \
    src/java/nginx/unit/SessionAttrProxy.java \
    src/java/nginx/unit/Taglib.java \
    src/java/nginx/unit/UnitSessionCookieConfig.java \
"

cat << END >> $NXT_MAKEFILE

.PHONY: ${NXT_JAVA_MODULE}
.PHONY: ${NXT_JAVA_MODULE}-install
.PHONY: ${NXT_JAVA_MODULE}-uninstall

all: ${NXT_JAVA_MODULE}

${NXT_JAVA_MODULE}: $NXT_BUILD_DIR/${NXT_JAVA_MODULE}.unit.so \
    $NXT_BUILD_DIR/$NXT_UNIT_JAR

$NXT_BUILD_DIR/${NXT_JAVA_MODULE}.unit.so: $nxt_objs
	\$(NXT_MODULE_LINK) -o $NXT_BUILD_DIR/${NXT_JAVA_MODULE}.unit.so \\
	$nxt_objs $NXT_JAVA_LDFLAGS $NXT_LD_OPT


install: ${NXT_JAVA_MODULE}-install

${NXT_JAVA_MODULE}-install: $NXT_BUILD_DIR/${NXT_JAVA_MODULE}.unit.so \\
    $NXT_BUILD_DIR/$NXT_UNIT_JAR java-shared-install
	install -d \$(DESTDIR)$NXT_MODULES
	install -p $NXT_BUILD_DIR/${NXT_JAVA_MODULE}.unit.so \\
		\$(DESTDIR)$NXT_MODULES/
	install -d \$(DESTDIR)$NXT_JARS
	install -p -m 0644 $NXT_BUILD_DIR/$NXT_UNIT_JAR \$(DESTDIR)$NXT_JARS/


uninstall: ${NXT_JAVA_MODULE}-uninstall

${NXT_JAVA_MODULE}-uninstall: java-shared-uninstall
	rm -f \$(DESTDIR)$NXT_MODULES/${NXT_JAVA_MODULE}.unit.so
	@rmdir -p \$(DESTDIR)$NXT_MODULES 2>/dev/null || true
	rm -f \$(DESTDIR)$NXT_JARS/$NXT_UNIT_JAR
	@rmdir -p \$(DESTDIR)$NXT_JARS 2>/dev/null || true

END

if ! grep ^$NXT_BUILD_DIR/$NXT_UNIT_JAR: $NXT_MAKEFILE 2>&1 > /dev/null; then

    cat << END >> $NXT_MAKEFILE

.INTERMEDIATE: $NXT_BUILD_DIR/$NXT_JAVA_MODULE/.classes

NXT_JAVA_SRCS = $NXT_JAVA_SRCS

$NXT_BUILD_DIR/$NXT_JAVA_MODULE/.classes: \$(NXT_JAVA_SRCS)
	rm -rf $NXT_BUILD_DIR/$NXT_JAVA_MODULE/nginx
	$NXT_JAVAC -d $NXT_BUILD_DIR/$NXT_JAVA_MODULE -cp $NXT_JAVA_BUILD_CP \\
		\$(NXT_JAVA_SRCS)

$NXT_BUILD_DIR/$NXT_JAVA_MODULE/META-INF/LICENSE: LICENSE
	mkdir -p $NXT_BUILD_DIR/$NXT_JAVA_MODULE/META-INF
	cp -p LICENSE \$@

$NXT_BUILD_DIR/$NXT_JAVA_MODULE/META-INF/NOTICE: NOTICE
	mkdir -p $NXT_BUILD_DIR/$NXT_JAVA_MODULE/META-INF
	cp -p NOTICE \$@


$NXT_BUILD_DIR/$NXT_UNIT_JAR: $NXT_BUILD_DIR/$NXT_JAVA_MODULE/.classes \\
    $NXT_BUILD_DIR/$NXT_JAVA_MODULE/META-INF/LICENSE \\
    $NXT_BUILD_DIR/$NXT_JAVA_MODULE/META-INF/NOTICE
	$NXT_JAVA_HOME/bin/jar c -C $NXT_BUILD_DIR/$NXT_JAVA_MODULE META-INF \\
		-C $NXT_BUILD_DIR/$NXT_JAVA_MODULE nginx/unit > \$@

END

fi

if ! grep ^java-shared-install: $NXT_MAKEFILE 2>&1 > /dev/null; then

    cat << END >> $NXT_MAKEFILE

.PHONY: java-shared-install
.PHONY: java-shared-uninstall

java-shared-install: $NXT_JAVA_INSTALL_JARS
	install -d \$(DESTDIR)$NXT_JARS
	install -p -m 0644 $NXT_JAVA_INSTALL_JARS \$(DESTDIR)$NXT_JARS/

java-shared-uninstall:
	rm -f $NXT_JAVA_UNINSTALL_JARS
	@rmdir -p \$(DESTDIR)$NXT_JARS 2>/dev/null || true

END

fi