summaryrefslogtreecommitdiffstats
path: root/rubbos/app/tomcat-connectors-1.2.32-src/native/common/jk_jni_worker.c
blob: 048f4435985d7161ebfea12b0225aed3c984beda (plain)
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/***************************************************************************
 * Description: In process JNI worker                                      *
 * Author:      Gal Shachor <shachor@il.ibm.com>                           *
 * Based on:                                                               *
 * Version:     $Revision: 750428 $                                           *
 ***************************************************************************/

#if !defined(WIN32) && !defined(NETWARE) && !defined(AS400)
#include <dlfcn.h>
#endif

#include <jni.h>

#include "jk_pool.h"
#include "jk_jni_worker.h"
#include "jk_util.h"

#if !defined(JNI_VERSION_1_6)
#if defined LINUX && defined APACHE2_SIGHACK
#include <pthread.h>
#include <signal.h>
#include <bits/signum.h>
#endif

#ifdef NETWARE
#ifdef __NOVELL_LIBC__
#include <dlfcn.h>
#else
#include <nwthread.h>
#include <nwadv.h>
#endif
#endif

#ifndef JNI_VERSION_1_1
#define JNI_VERSION_1_1 0x00010001
#endif

/* probably on an older system that doesn't support RTLD_NOW or RTLD_LAZY.
 * The below define is a lie since we are really doing RTLD_LAZY since the
 * system doesn't support RTLD_NOW.
 */
#ifndef RTLD_NOW
#define RTLD_NOW 1
#endif

#ifndef RTLD_GLOBAL
#define RTLD_GLOBAL 0
#endif

#define null_check(e) if ((e) == 0) return JK_FALSE

jint(JNICALL * jni_get_default_java_vm_init_args) (void *) = NULL;
jint(JNICALL * jni_create_java_vm) (JavaVM **, JNIEnv **, void *) = NULL;
#ifdef AS400
jint(JNICALL * jni_get_created_java_vms) (JavaVM **, long, long *) = NULL;
#else
jint(JNICALL * jni_get_created_java_vms) (JavaVM **, int, int *) = NULL;
#endif

#define TC33_JAVA_BRIDGE_CLASS_NAME ("org/apache/tomcat/modules/server/JNIEndpoint")
#define TC32_JAVA_BRIDGE_CLASS_NAME ("org/apache/tomcat/service/JNIEndpoint")

static jk_worker_t *the_singleton_jni_worker = NULL;

struct jni_worker
{

    int was_verified;
    int was_initialized;

    jk_pool_t p;
    jk_pool_atom_t buf[TINY_POOL_SIZE];

    /*
     * JVM Object pointer.
     */
    JavaVM *jvm;

    /*
     * [V] JNIEnv used for boostraping from validate -> init w/o an attach
     */
    JNIEnv *tmp_env;

    /*
     * Web Server to Java bridge, instance and class.
     */
    jobject jk_java_bridge_object;
    jclass jk_java_bridge_class;

    /*
     * Java methods ids, to jump into the JVM
     */
    jmethodID jk_startup_method;
    jmethodID jk_service_method;
    jmethodID jk_shutdown_method;

    /*
     * Command line for tomcat startup
     */
    char *tomcat_cmd_line;

    /*
     * Bridge Type, Tomcat 32/33/40/41/5
     */
    unsigned bridge_type;

    /*
     * Classpath
     */
    char *tomcat_classpath;

    /*
     * Full path to the jni javai/jvm dll
     */
    char *jvm_dll_path;

    /*
     * Initial Java heap size
     */
    unsigned tomcat_ms;

    /*
     * Max Java heap size
     */
    unsigned tomcat_mx;

    /*
     * Java system properties
     */
    char **sysprops;

#ifdef JNI_VERSION_1_2
    /*
     * Java 2 initialization options (-X... , -verbose etc.)
     */
    char **java2opts;

    /*
     * Java 2 lax/strict option checking (bool)
     */
    int java2lax;
#endif

    /*
     * stdout and stderr file names for Java
     */
    char *stdout_name;
    char *stderr_name;

    char *name;
    jk_worker_t worker;
};
typedef struct jni_worker jni_worker_t;

struct jni_endpoint
{
    int attached;
    JNIEnv *env;
    jni_worker_t *worker;

    jk_endpoint_t endpoint;
};
typedef struct jni_endpoint jni_endpoint_t;


static int load_jvm_dll(jni_worker_t * p, jk_logger_t *l);

static int open_jvm(jni_worker_t * p, JNIEnv ** env, jk_logger_t *l);

static int open_jvm1(jni_worker_t * p, JNIEnv ** env, jk_logger_t *l);

#ifdef JNI_VERSION_1_2
static int detect_jvm_version(jk_logger_t *l);

static int open_jvm2(jni_worker_t * p, JNIEnv ** env, jk_logger_t *l);
#endif


static int get_bridge_object(jni_worker_t * p, JNIEnv * env, jk_logger_t *l);

static int get_method_ids(jni_worker_t * p, JNIEnv * env, jk_logger_t *l);

static JNIEnv *attach_to_jvm(jni_worker_t * p, jk_logger_t *l);

static void detach_from_jvm(jni_worker_t * p, jk_logger_t *l);


/*
   Duplicate string and convert it to ASCII on EBDIC based systems
   Needed for at least AS/400 (before V5R4 ?) and BS2000 but what about other EBDIC systems ?
*/
static void *strdup_ascii(jk_pool_t *p, char *s)
{
    char *rc;
    rc = jk_pool_strdup(p, s);

#if defined(AS400) || defined(_OSD_POSIX)
    jk_xlate_to_ascii(rc, strlen(rc));
#endif

    return rc;
}

#if defined LINUX && defined APACHE2_SIGHACK
static void linux_signal_hack()
{
    sigset_t newM;
    sigset_t old;

    sigemptyset(&newM);
    pthread_sigmask(SIG_SETMASK, &newM, &old);

    sigdelset(&old, SIGUSR1);
    sigdelset(&old, SIGUSR2);
    sigdelset(&old, SIGUNUSED);
    sigdelset(&old, SIGRTMIN);
    sigdelset(&old, SIGRTMIN + 1);
    sigdelset(&old, SIGRTMIN + 2);
    pthread_sigmask(SIG_SETMASK, &old, NULL);
}

static void print_signals(sigset_t * sset)
{
    int sig;
    for (sig = 1; sig < 20; sig++) {
        if (sigismember(sset, sig)) {
            printf(" %d", sig);
        }
    }
    printf("\n");
}
#endif

/*
 * Return values of service() method for jni worker:
 * return value  is_error              reason
 * JK_FALSE      JK_HTTP_SERVER_ERROR  Invalid parameters (null values)
 *                                     Error during attach to the JNI backend
 *                                     Error during JNI call
 * JK_TRUE       JK_HTTP_OK            All other cases
 */
static int JK_METHOD service(jk_endpoint_t *e,
                             jk_ws_service_t *s,
                             jk_logger_t *l, int *is_error)
{
    jni_endpoint_t *p;
    jint rc;

    JK_TRACE_ENTER(l);

    if (!e || !e->endpoint_private || !s || !is_error) {
        JK_LOG_NULL_PARAMS(l);
        if (is_error)
            *is_error = JK_HTTP_SERVER_ERROR;
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    p = e->endpoint_private;

    /* Set returned error to OK */
    *is_error = JK_HTTP_OK;

    if (!p->attached) {
        /* Try to attach */
        if (!(p->env = attach_to_jvm(p->worker, l))) {
            jk_log(l, JK_LOG_EMERG, "Attach failed");
            *is_error = JK_HTTP_SERVER_ERROR;
            JK_TRACE_EXIT(l);
            return JK_FALSE;
        }
        p->attached = JK_TRUE;
    }

    /* we are attached now */

    /*
     * When we call the JVM we cannot know what happens
     * So we can not recover !!!
     */
    jk_log(l, JK_LOG_DEBUG, "In service, calling Tomcat...");

    rc = (*(p->env))->CallIntMethod(p->env,
                                    p->worker->jk_java_bridge_object,
                                    p->worker->jk_service_method,
                                    /* [V] For some reason gcc likes this pointer -> int -> jlong conversion, */
                                    /*     but not the direct pointer -> jlong conversion. I hope it's okay.  */
#ifdef AS400
                                    s, l
#else
                                    (jlong) (int)s, (jlong) (int)l
#endif
        );

    /* [V] Righ now JNIEndpoint::service() only returns 1 or 0 */
    if (rc) {
        jk_log(l, JK_LOG_DEBUG, "Tomcat returned OK, done");
        JK_TRACE_EXIT(l);
        return JK_TRUE;
    }
    else {
        jk_log(l, JK_LOG_ERROR, "Tomcat FAILED!");
        *is_error = JK_HTTP_SERVER_ERROR;
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }
}

static int JK_METHOD done(jk_endpoint_t **e, jk_logger_t *l)
{
    jni_endpoint_t *p;

    JK_TRACE_ENTER(l);
    if (!e || !*e || !(*e)->endpoint_private) {
        JK_LOG_NULL_PARAMS(l);
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    p = (*e)->endpoint_private;

    if (p->attached) {
        detach_from_jvm(p->worker, l);
    }

    free(p);
    *e = NULL;
    JK_TRACE_EXIT(l);
    return JK_TRUE;
}

static int JK_METHOD validate(jk_worker_t *pThis,
                              jk_map_t *props,
                              jk_worker_env_t *we, jk_logger_t *l)
{
    jni_worker_t *p;
    int mem_config = 0;
    int btype = 0;
    const char *str_config = NULL;
    JNIEnv *env;

    JK_TRACE_ENTER(l);

    if (!pThis || !pThis->worker_private) {
        JK_LOG_NULL_PARAMS(l);
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    p = pThis->worker_private;

    if (p->was_verified) {
        jk_log(l, JK_LOG_DEBUG, "been here before, done");
        JK_TRACE_EXIT(l);
        return JK_TRUE;
    }

    if (jk_get_worker_mx(props, p->name, (unsigned int *)&mem_config)) {
        p->tomcat_mx = mem_config;
    }

    if (jk_get_worker_ms(props, p->name, (unsigned int *)&mem_config)) {
        p->tomcat_ms = mem_config;
    }

    if (jk_get_worker_classpath(props, p->name, &str_config)) {
        p->tomcat_classpath = jk_pool_strdup(&p->p, str_config);
    }

    if (!p->tomcat_classpath) {
        jk_log(l, JK_LOG_EMERG, "no classpath");
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    if (jk_get_worker_bridge_type(props, p->name, (unsigned int *)&btype)) {
        p->bridge_type = btype;
    }

    if (jk_get_worker_jvm_path(props, p->name, &str_config)) {
        p->jvm_dll_path = jk_pool_strdup(&p->p, str_config);
    }

    if (!p->jvm_dll_path || !jk_file_exists(p->jvm_dll_path)) {
        jk_log(l, JK_LOG_EMERG, "no jvm_dll_path");
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    if (jk_get_worker_cmd_line(props, p->name, &str_config)) {
        p->tomcat_cmd_line = jk_pool_strdup(&p->p, str_config);
    }

    if (jk_get_worker_stdout(props, p->name, &str_config)) {
        p->stdout_name = jk_pool_strdup(&p->p, str_config);
    }

    if (jk_get_worker_stderr(props, p->name, &str_config)) {
        p->stderr_name = jk_pool_strdup(&p->p, str_config);
    }

    if (jk_get_worker_sysprops(props, p->name, &str_config)) {
        p->sysprops = jk_parse_sysprops(&p->p, str_config);
    }

#ifdef JNI_VERSION_1_2
    if (jk_get_worker_str_prop(props, p->name, "java2opts", &str_config)) {
        /* jk_log(l, JK_LOG_DEBUG, "Got opts: %s", str_config); */
        p->java2opts = jk_parse_sysprops(&p->p, str_config);
    }
    if (jk_get_worker_int_prop(props, p->name, "java2lax", &mem_config)) {
        p->java2lax = mem_config ? JK_TRUE : JK_FALSE;
    }
#endif

    if (jk_get_worker_libpath(props, p->name, &str_config)) {
        jk_append_libpath(&p->p, str_config);
    }

    if (!load_jvm_dll(p, l)) {
        jk_log(l, JK_LOG_EMERG, "can't load jvm dll");
        /* [V] no detach needed here */
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    if (!open_jvm(p, &env, l)) {
        jk_log(l, JK_LOG_EMERG, "can't open jvm");
        /* [V] no detach needed here */
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    if (!get_bridge_object(p, env, l)) {
        jk_log(l, JK_LOG_EMERG, "can't get bridge object");
        /* [V] the detach here may segfault on 1.1 JVM... */
        detach_from_jvm(p, l);
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    if (!get_method_ids(p, env, l)) {
        jk_log(l, JK_LOG_EMERG, "can't get method ids");
        /* [V] the detach here may segfault on 1.1 JVM... */
        detach_from_jvm(p, l);
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    p->was_verified = JK_TRUE;
    p->tmp_env = env;

    JK_TRACE_EXIT(l);
    return JK_TRUE;
}

static int JK_METHOD init(jk_worker_t *pThis,
                          jk_map_t *props,
                          jk_worker_env_t *we, jk_logger_t *l)
{
    jni_worker_t *p;
    JNIEnv *env;

    JK_TRACE_ENTER(l);

    if (!pThis || !pThis->worker_private) {
        JK_LOG_NULL_PARAMS(l);
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    p = pThis->worker_private;

    if (p->was_initialized) {
        jk_log(l, JK_LOG_DEBUG, "done (been here!)");
        JK_TRACE_EXIT(l);
        return JK_TRUE;
    }

    if (!p->jvm ||
        !p->jk_java_bridge_object ||
        !p->jk_service_method ||
        !p->jk_startup_method || !p->jk_shutdown_method) {
        jk_log(l, JK_LOG_EMERG, "worker not set completely");
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    /* [V] init is called from the same thread that called validate */
    /* there is no need to attach to the JVM, just get the env */

    /* if(env = attach_to_jvm(p,l)) { */
    if ((env = p->tmp_env)) {
        jstring cmd_line = (jstring) NULL;
        jstring stdout_name = (jstring) NULL;
        jstring stderr_name = (jstring) NULL;
        jint rc = 0;

        /* AS400/BS2000 need EBCDIC to ASCII conversion for JNI */

        if (p->tomcat_cmd_line) {
            cmd_line =
                (*env)->NewStringUTF(env,
                                     strdup_ascii(&p->p, p->tomcat_cmd_line));
        }
        if (p->stdout_name) {
            stdout_name =
                (*env)->NewStringUTF(env,
                                     strdup_ascii(&p->p, p->stdout_name));
        }
        if (p->stderr_name) {
            stderr_name =
                (*env)->NewStringUTF(env,
                                     strdup_ascii(&p->p, p->stderr_name));
        }

        jk_log(l, JK_LOG_DEBUG,
               "calling Tomcat to intialize itself...");
        rc = (*env)->CallIntMethod(env, p->jk_java_bridge_object,
                                   p->jk_startup_method, cmd_line,
                                   stdout_name, stderr_name);

        detach_from_jvm(p, l);

        if (rc) {
            p->was_initialized = JK_TRUE;
            jk_log(l, JK_LOG_DEBUG, "Tomcat initialized OK, done");
            JK_TRACE_EXIT(l);
            return JK_TRUE;
        }
        else {
            jk_log(l, JK_LOG_EMERG, "could not initialize Tomcat");
            JK_TRACE_EXIT(l);
            return JK_FALSE;
        }
    }
    else {
        jk_log(l, JK_LOG_ERROR,
               "In init, FIXME: init didn't gen env from validate!");
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }
}

static int JK_METHOD get_endpoint(jk_worker_t *pThis,
                                  jk_endpoint_t **pend, jk_logger_t *l)
{
    /* [V] This slow, needs replacement */
    jni_endpoint_t *p;

    JK_TRACE_ENTER(l);

    if (!pThis || !pThis->worker_private || !pend) {
        JK_LOG_NULL_PARAMS(l);
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    p = (jni_endpoint_t *) calloc(1, sizeof(jni_endpoint_t));
    if (p) {
        p->attached = JK_FALSE;
        p->env = NULL;
        p->worker = pThis->worker_private;
        p->endpoint.endpoint_private = p;
        p->endpoint.service = service;
        p->endpoint.done = done;
        *pend = &p->endpoint;

        JK_TRACE_EXIT(l);
        return JK_TRUE;
    }
    else {
        jk_log(l, JK_LOG_ERROR,
               "could not allocate endpoint");
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }
}

static int JK_METHOD destroy(jk_worker_t **pThis, jk_logger_t *l)
{
    jni_worker_t *p;
    JNIEnv *env;

    JK_TRACE_ENTER(l);

    if (!pThis || !*pThis || !(*pThis)->worker_private) {
        JK_LOG_NULL_PARAMS(l);
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    p = (*pThis)->worker_private;

    if (!p->jvm) {
        jk_log(l, JK_LOG_DEBUG, "JVM not intantiated");
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    if (!p->jk_java_bridge_object || !p->jk_shutdown_method) {
        jk_log(l, JK_LOG_DEBUG, "Tomcat not intantiated");
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    if ((env = attach_to_jvm(p, l))) {
        jk_log(l, JK_LOG_DEBUG, "shutting down Tomcat...");
        (*env)->CallVoidMethod(env,
                               p->jk_java_bridge_object,
                               p->jk_shutdown_method);
        detach_from_jvm(p, l);
    }

    jk_close_pool(&p->p);
    free(p);

    jk_log(l, JK_LOG_DEBUG, "destroyed");

    JK_TRACE_EXIT(l);
    return JK_TRUE;
}

int JK_METHOD jni_worker_factory(jk_worker_t **w,
                                 const char *name, jk_logger_t *l)
{
    jni_worker_t *private_data;

    JK_TRACE_ENTER(l);

    jk_log(l, JK_LOG_WARNING,
           "Worker '%s' is of type jni, which is deprecated "
           "and will be removed in a future release.",
           name ? name : "(null)");

    if (!name || !w) {
        JK_LOG_NULL_PARAMS(l);
        JK_TRACE_EXIT(l);
        return 0;
    }

    if (the_singleton_jni_worker) {
        jk_log(l, JK_LOG_DEBUG,
               "instance already created");
        *w = the_singleton_jni_worker;
        JK_TRACE_EXIT(l);
        return JK_JNI_WORKER_TYPE;
    }

    private_data = (jni_worker_t *) malloc(sizeof(jni_worker_t));

    if (!private_data) {
        jk_log(l, JK_LOG_ERROR,
               "memory allocation error");
        JK_TRACE_EXIT(l);
        return 0;
    }

    jk_open_pool(&private_data->p,
                 private_data->buf, sizeof(jk_pool_atom_t) * TINY_POOL_SIZE);

    private_data->name = jk_pool_strdup(&private_data->p, name);

    if (!private_data->name) {
        jk_log(l, JK_LOG_ERROR,
               "memory allocation error");
        jk_close_pool(&private_data->p);
        free(private_data);
        JK_TRACE_EXIT(l);
        return 0;
    }

    private_data->was_verified = JK_FALSE;
    private_data->was_initialized = JK_FALSE;
    private_data->jvm = NULL;
    private_data->tmp_env = NULL;
    private_data->jk_java_bridge_object = (jobject) NULL;
    private_data->jk_java_bridge_class = (jclass) NULL;
    private_data->jk_startup_method = (jmethodID) NULL;
    private_data->jk_service_method = (jmethodID) NULL;
    private_data->jk_shutdown_method = (jmethodID) NULL;
    private_data->tomcat_cmd_line = NULL;
    private_data->tomcat_classpath = NULL;
    private_data->bridge_type = TC33_BRIDGE_TYPE;
    private_data->jvm_dll_path = NULL;
    private_data->tomcat_ms = 0;
    private_data->tomcat_mx = 0;
    private_data->sysprops = NULL;
#ifdef JNI_VERSION_1_2
    private_data->java2opts = NULL;
    private_data->java2lax = JK_TRUE;
#endif
    private_data->stdout_name = NULL;
    private_data->stderr_name = NULL;

    private_data->worker.worker_private = private_data;
    private_data->worker.validate = validate;
    private_data->worker.init = init;
    private_data->worker.get_endpoint = get_endpoint;
    private_data->worker.destroy = destroy;

    *w = &private_data->worker;
    the_singleton_jni_worker = &private_data->worker;

    JK_TRACE_EXIT(l);
    return JK_JNI_WORKER_TYPE;
}

static int load_jvm_dll(jni_worker_t * p, jk_logger_t *l)
{
#ifdef WIN32
    HINSTANCE hInst = LoadLibrary(p->jvm_dll_path);
    if (hInst) {
        (FARPROC) jni_create_java_vm =
            GetProcAddress(hInst, "JNI_CreateJavaVM");

        (FARPROC) jni_get_created_java_vms =
            GetProcAddress(hInst, "JNI_GetCreatedJavaVMs");

        (FARPROC) jni_get_default_java_vm_init_args =
            GetProcAddress(hInst, "JNI_GetDefaultJavaVMInitArgs");

        jk_log(l, JK_LOG_DEBUG, "Loaded all JNI procs");

        if (jni_create_java_vm && jni_get_default_java_vm_init_args
            && jni_get_created_java_vms) {
            return JK_TRUE;
        }

        FreeLibrary(hInst);
    }
#elif defined(NETWARE) && !defined(__NOVELL_LIBC__)
    int javaNlmHandle = FindNLMHandle("JVM");
    if (0 == javaNlmHandle) {
        /* if we didn't get a handle, try to load java and retry getting the */
        /* handle */
        spawnlp(P_NOWAIT, "JVM.NLM", NULL);
        ThreadSwitchWithDelay();
        javaNlmHandle = FindNLMHandle("JVM");
        if (0 == javaNlmHandle)
            printf("Error loading Java.");

    }
    if (0 != javaNlmHandle) {
        jni_create_java_vm = ImportSymbol(GetNLMHandle(), "JNI_CreateJavaVM");
        jni_get_created_java_vms =
            ImportSymbol(GetNLMHandle(), "JNI_GetCreatedJavaVMs");
        jni_get_default_java_vm_init_args =
            ImportSymbol(GetNLMHandle(), "JNI_GetDefaultJavaVMInitArgs");
    }
    if (jni_create_java_vm && jni_get_default_java_vm_init_args
        && jni_get_created_java_vms) {
        return JK_TRUE;
    }
#elif defined(AS400)
    jk_log(l, JK_LOG_DEBUG,
           "Direct reference to JNI entry points (no SRVPGM)");
    jni_create_java_vm = &JNI_CreateJavaVM;
    jni_get_default_java_vm_init_args = &JNI_GetDefaultJavaVMInitArgs;
    jni_get_created_java_vms = &JNI_GetCreatedJavaVMs;
#else
    void *handle;
    jk_log(l, JK_LOG_DEBUG, "loading JVM %s", p->jvm_dll_path);

    handle = dlopen(p->jvm_dll_path, RTLD_NOW | RTLD_GLOBAL);

    if (!handle) {
        jk_log(l, JK_LOG_EMERG,
               "Can't load native library %s : %s", p->jvm_dll_path,
               dlerror());
    }
    else {
        jni_create_java_vm = dlsym(handle, "JNI_CreateJavaVM");
        jni_get_default_java_vm_init_args =
            dlsym(handle, "JNI_GetDefaultJavaVMInitArgs");
        jni_get_created_java_vms = dlsym(handle, "JNI_GetCreatedJavaVMs");

        if (jni_create_java_vm && jni_get_default_java_vm_init_args &&
            jni_get_created_java_vms) {
            jk_log(l, JK_LOG_DEBUG,
                   "In load_jvm_dll, symbols resolved, done");
            return JK_TRUE;
        }
        jk_log(l, JK_LOG_EMERG,
               "Can't resolve JNI_CreateJavaVM or JNI_GetDefaultJavaVMInitArgs");
        dlclose(handle);
    }
#endif
    return JK_FALSE;
}

static int open_jvm(jni_worker_t * p, JNIEnv ** env, jk_logger_t *l)
{
#ifdef JNI_VERSION_1_2
    int jvm_version = detect_jvm_version(l);

    switch (jvm_version) {
    case JNI_VERSION_1_1:
        return open_jvm1(p, env, l);
    case JNI_VERSION_1_2:
        return open_jvm2(p, env, l);
    default:
        return JK_FALSE;
    }
#else
    /* [V] Make sure this is _really_ visible */
#warning -------------------------------------------------------
#warning NO JAVA 2 HEADERS! SUPPORT FOR JAVA 2 FEATURES DISABLED
#warning -------------------------------------------------------
    return open_jvm1(p, env, l);
#endif
}

static int open_jvm1(jni_worker_t * p, JNIEnv ** env, jk_logger_t *l)
{
    JDK1_1InitArgs vm_args;
    JNIEnv *penv;
    int err;
    *env = NULL;

    JK_TRACE_ENTER(l);

    vm_args.version = JNI_VERSION_1_1;

    if (0 != jni_get_default_java_vm_init_args(&vm_args)) {
        jk_log(l, JK_LOG_EMERG, "can't get default vm init args");
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }
    jk_log(l, JK_LOG_DEBUG, "got default jvm args");

    if (vm_args.classpath) {
        size_t len = strlen(vm_args.classpath) +
            strlen(p->tomcat_classpath) + 3;
        char *tmp = jk_pool_alloc(&p->p, len);
        if (tmp) {
            sprintf(tmp, "%s%c%s",
                    strdup_ascii(&p->p, p->tomcat_classpath),
                    PATH_SEPERATOR, vm_args.classpath);
            p->tomcat_classpath = tmp;
        }
        else {
            jk_log(l, JK_LOG_EMERG,
                   "allocation error for classpath");
            JK_TRACE_EXIT(l);
            return JK_FALSE;
        }
    }
    vm_args.classpath = p->tomcat_classpath;

    if (p->tomcat_mx) {
        vm_args.maxHeapSize = p->tomcat_mx;
    }

    if (p->tomcat_ms) {
        vm_args.minHeapSize = p->tomcat_ms;
    }

    if (p->sysprops) {
        /* No EBCDIC to ASCII conversion here for AS/400 - later */
        vm_args.properties = p->sysprops;
    }

    jk_log(l, JK_LOG_DEBUG, "In open_jvm1, about to create JVM...");
    if ((err = jni_create_java_vm(&(p->jvm), &penv, &vm_args)) != 0) {
        jk_log(l, JK_LOG_EMERG,
               "could not create JVM, code: %d ", err);
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }
    jk_log(l, JK_LOG_DEBUG, "JVM created, done");

    *env = penv;

    JK_TRACE_EXIT(l);
    return JK_TRUE;
}

#ifdef JNI_VERSION_1_2
static int detect_jvm_version(jk_logger_t *l)
{
    JNIEnv *env = NULL;
    JDK1_1InitArgs vm_args;

    JK_TRACE_ENTER(l);

    /* [V] Idea: ask for 1.2. If the JVM is 1.1 it will return 1.1 instead  */
    /*     Note: asking for 1.1 won't work, 'cause 1.2 JVMs will return 1.1 */
    vm_args.version = JNI_VERSION_1_2;

    if (0 != jni_get_default_java_vm_init_args(&vm_args)) {
        jk_log(l, JK_LOG_EMERG, "can't get default vm init args");
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }
    jk_log(l, JK_LOG_DEBUG,
           "found version: %X, done", vm_args.version);

    JK_TRACE_EXIT(l);
    return vm_args.version;
}

static char *build_opt_str(jk_pool_t *p,
                           char *opt_name, char *opt_value, jk_logger_t *l)
{
    size_t len = strlen(opt_name) + strlen(opt_value) + 2;

    /* [V] IMHO, these should not be deallocated as long as the JVM runs */
    char *tmp = jk_pool_alloc(p, len);

    if (tmp) {
        sprintf(tmp, "%s%s", opt_name, opt_value);
        return tmp;
    }
    else {
        jk_log(l, JK_LOG_EMERG,
               "allocation error for %s", opt_name);
        return NULL;
    }
}

static char *build_opt_int(jk_pool_t *p,
                           char *opt_name, int opt_value, jk_logger_t *l)
{
    /* [V] this should suffice even for 64-bit int */
    size_t len = strlen(opt_name) + 20 + 2;
    /* [V] IMHO, these should not be deallocated as long as the JVM runs */
    char *tmp = jk_pool_alloc(p, len);

    if (tmp) {
        sprintf(tmp, "%s%d", opt_name, opt_value);
        return tmp;
    }
    else {
        jk_log(l, JK_LOG_EMERG,
               "allocation error for %s", opt_name);
        return NULL;
    }
}

static int open_jvm2(jni_worker_t * p, JNIEnv ** env, jk_logger_t *l)
{
    JavaVMInitArgs vm_args;
    JNIEnv *penv = NULL;
    JavaVMOption options[100];
    int optn = 0, err;
    char *tmp;

    *env = NULL;

    JK_TRACE_ENTER(l);

    vm_args.version = JNI_VERSION_1_2;
    vm_args.options = options;

/* AS/400 need EBCDIC to ASCII conversion to parameters passed to JNI */
/* No conversion for ASCII based systems (what about BS2000 ?) */

    if (p->tomcat_classpath) {
        jk_log(l, JK_LOG_DEBUG, "setting classpath to %s",
               p->tomcat_classpath);
        tmp =
            build_opt_str(&p->p, "-Djava.class.path=", p->tomcat_classpath,
                          l);
        null_check(tmp);
        options[optn++].optionString = strdup_ascii(&p->p, tmp);
    }

    if (p->tomcat_mx) {
        jk_log(l, JK_LOG_DEBUG, "setting max heap to %d",
               p->tomcat_mx);
        tmp = build_opt_int(&p->p, "-Xmx", p->tomcat_mx, l);
        null_check(tmp);
        options[optn++].optionString = strdup_ascii(&p->p, tmp);
    }

    if (p->tomcat_ms) {
        jk_log(l, JK_LOG_DEBUG, "setting start heap to %d",
               p->tomcat_ms);
        tmp = build_opt_int(&p->p, "-Xms", p->tomcat_ms, l);
        null_check(tmp);
        options[optn++].optionString = strdup_ascii(&p->p, tmp);
    }

    if (p->sysprops) {
        int i = 0;
        while (p->sysprops[i]) {
            jk_log(l, JK_LOG_DEBUG, "setting %s",
                   p->sysprops[i]);
            tmp = build_opt_str(&p->p, "-D", p->sysprops[i], l);
            null_check(tmp);
            options[optn++].optionString = strdup_ascii(&p->p, tmp);
            i++;
        }
    }

    if (p->java2opts) {
        int i = 0;

        while (p->java2opts[i]) {
            jk_log(l, JK_LOG_DEBUG, "using option: %s",
                   p->java2opts[i]);
            /* Pass it "as is" */
            options[optn++].optionString =
                strdup_ascii(&p->p, p->java2opts[i++]);
        }
    }

    vm_args.nOptions = optn;

    if (p->java2lax) {
        jk_log(l, JK_LOG_DEBUG,
               "the JVM will ignore unknown options");
        vm_args.ignoreUnrecognized = JNI_TRUE;
    }
    else {
        jk_log(l, JK_LOG_DEBUG,
               "the JVM will FAIL if it finds unknown options");
        vm_args.ignoreUnrecognized = JNI_FALSE;
    }

    jk_log(l, JK_LOG_DEBUG, "about to create JVM...");

    err = jni_create_java_vm(&(p->jvm), &penv, &vm_args);

    if (JNI_EEXIST == err) {
#ifdef AS400
        long vmCount;
#else
        int vmCount;
#endif
        jk_log(l, JK_LOG_DEBUG, "JVM alread instantiated."
               "Trying to attach instead.");

        jni_get_created_java_vms(&(p->jvm), 1, &vmCount);
        if (NULL != p->jvm)
            penv = attach_to_jvm(p, l);

        if (NULL != penv)
            err = 0;
    }

    if (err != 0) {
        jk_log(l, JK_LOG_EMERG, "Fail-> could not create JVM, code: %d ",
               err);
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    *env = penv;
    jk_log(l, JK_LOG_DEBUG, "JVM created");

    JK_TRACE_EXIT(l);
    return JK_TRUE;
}
#endif

static int get_bridge_object(jni_worker_t * p, JNIEnv * env, jk_logger_t *l)
{
    char *btype;
    char *ctype;

    jmethodID constructor_method_id;

    JK_TRACE_ENTER(l);

    switch (p->bridge_type) {
    case TC32_BRIDGE_TYPE:
        btype = TC32_JAVA_BRIDGE_CLASS_NAME;
        break;

    case TC33_BRIDGE_TYPE:
        btype = TC33_JAVA_BRIDGE_CLASS_NAME;
        break;

    case TC40_BRIDGE_TYPE:
    case TC41_BRIDGE_TYPE:
    case TC50_BRIDGE_TYPE:
        jk_log(l, JK_LOG_EMERG, "Bridge type %d not supported",
               p->bridge_type);
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

/* AS400/BS2000 need conversion from EBCDIC to ASCII before passing to JNI */
/* for others, strdup_ascii is just jk_pool_strdup */

    ctype = strdup_ascii(&p->p, btype);

    p->jk_java_bridge_class = (*env)->FindClass(env, ctype);

    if (!p->jk_java_bridge_class) {
        jk_log(l, JK_LOG_EMERG, "Can't find class %s", btype);
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }
    jk_log(l, JK_LOG_DEBUG,
           "In get_bridge_object, loaded %s bridge class", btype);

    constructor_method_id = (*env)->GetMethodID(env, p->jk_java_bridge_class, strdup_ascii(&p->p, "<init>"),    /* method name */
                                                strdup_ascii(&p->p, "()V"));    /* method sign */

    if (!constructor_method_id) {
        p->jk_java_bridge_class = (jclass) NULL;
        jk_log(l, JK_LOG_EMERG, "Can't find constructor");
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    p->jk_java_bridge_object = (*env)->NewObject(env,
                                                 p->jk_java_bridge_class,
                                                 constructor_method_id);
    if (!p->jk_java_bridge_object) {
        p->jk_java_bridge_class = (jclass) NULL;
        jk_log(l, JK_LOG_EMERG, "Can't create new bridge object");
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    p->jk_java_bridge_object =
        (jobject) (*env)->NewGlobalRef(env, p->jk_java_bridge_object);
    if (!p->jk_java_bridge_object) {
        jk_log(l, JK_LOG_EMERG, "Can't create global ref to bridge object");
        p->jk_java_bridge_class = (jclass) NULL;
        p->jk_java_bridge_object = (jobject) NULL;
        JK_TRACE_EXIT(l);
        return JK_FALSE;
    }

    JK_TRACE_EXIT(l);
    return JK_TRUE;
}

static int get_method_ids(jni_worker_t * p, JNIEnv * env, jk_logger_t *l)
{

/* AS400/BS2000 need conversion from EBCDIC to ASCII before passing to JNI */

    p->jk_startup_method = (*env)->GetMethodID(env,
                                               p->jk_java_bridge_class,
                                               strdup_ascii(&p->p, "startup"),
                                               strdup_ascii(&p->p,
                                                            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I"));

    if (!p->jk_startup_method) {
        jk_log(l, JK_LOG_EMERG, "Can't find startup()");
        return JK_FALSE;
    }

    p->jk_service_method = (*env)->GetMethodID(env,
                                               p->jk_java_bridge_class,
                                               strdup_ascii(&p->p, "service"),
                                               strdup_ascii(&p->p, "(JJ)I"));

    if (!p->jk_service_method) {
        jk_log(l, JK_LOG_EMERG, "Can't find service()");
        return JK_FALSE;
    }

    p->jk_shutdown_method = (*env)->GetMethodID(env,
                                                p->jk_java_bridge_class,
                                                strdup_ascii(&p->p,
                                                             "shutdown"),
                                                strdup_ascii(&p->p, "()V"));

    if (!p->jk_shutdown_method) {
        jk_log(l, JK_LOG_EMERG, "Can't find shutdown()");
        return JK_FALSE;
    }

    return JK_TRUE;
}

static JNIEnv *attach_to_jvm(jni_worker_t * p, jk_logger_t *l)
{
    JNIEnv *rc = NULL;
    /* [V] This message is important. If there are signal mask issues,    *
     *     the JVM usually hangs when a new thread tries to attach to it  */
    JK_TRACE_ENTER(l);

#if defined LINUX && defined APACHE2_SIGHACK
    linux_signal_hack();
#endif

    if (0 == (*(p->jvm))->AttachCurrentThread(p->jvm,
#ifdef JNI_VERSION_1_2
                                              (void **)
#endif
                                              &rc, NULL)) {
        jk_log(l, JK_LOG_DEBUG, "In attach_to_jvm, attached ok");
        JK_TRACE_EXIT(l);
        return rc;
    }
    jk_log(l, JK_LOG_ERROR,
           "In attach_to_jvm, cannot attach thread to JVM.");
    JK_TRACE_EXIT(l);
    return NULL;
}

/*
static JNIEnv *attach_to_jvm(jni_worker_t *p)
{
    JNIEnv *rc = NULL;

#ifdef LINUX
    linux_signal_hack();
#endif

    if(0 == (*(p->jvm))->AttachCurrentThread(p->jvm,
#ifdef JNI_VERSION_1_2
           (void **)
#endif
                                             &rc,
                                             NULL)) {
        return rc;
    }

    return NULL;
}
*/
static void detach_from_jvm(jni_worker_t * p, jk_logger_t *l)
{
    JK_TRACE_ENTER(l);
    if (!p->jvm || !(*(p->jvm))) {
        jk_log(l, JK_LOG_ERROR,
               "cannot detach from NULL JVM.");
    }

    if (0 == (*(p->jvm))->DetachCurrentThread(p->jvm)) {
        jk_log(l, JK_LOG_DEBUG, "detached ok");
    }
    else {
        jk_log(l, JK_LOG_ERROR,
               "cannot detach from JVM.");
    }
    JK_TRACE_EXIT(l);
}
#else
int JK_METHOD jni_worker_factory(jk_worker_t **w,
                                 const char *name, jk_logger_t *l)
{
    jk_log(l, JK_LOG_WARNING,
           "Worker '%s' is of type jni, which is deprecated "
           "and will be removed in a future release.",
           name ? name : "(null)");
    if (w)
        *w = NULL;
    return 0;
}
#endif /* JNI_VERSION_1_6 */