src/server/plugins/java/jvm.c

Sun, 01 May 2022 10:48:20 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 01 May 2022 10:48:20 +0200
branch
webdav
changeset 319
a9b9344875aa
parent 84
afd57ce39ec9
child 415
d938228c382e
permissions
-rw-r--r--

add WebdavNSList <-> string converting functions

84
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <dlfcn.h>
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <ucx/map.h>
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "jvm.h"
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "daemon/request.h"
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #ifdef __i386
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #define LIBJVM "/usr/java/jre/lib/i386/server/libjvm.so"
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #else
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #endif
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 static JNIEnv *env = NULL;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 static JavaVM *jvm = NULL;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 static jobject plugin_instance = NULL;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 static jmethodID load_jmodules_method = NULL;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 static jmethodID exec_method = NULL;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 int jvm_init(pblock *pb, Session *sn, Request *rq) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 // load lib
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 void *lib = dlopen(LIBJVM, RTLD_GLOBAL | RTLD_NOW);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 if(!lib) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 log_ereport(LOG_FAILURE, "Cannot load libjvm.so");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 return REQ_ABORTED;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 // get symbol
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 void *sym = dlsym(lib, "JNI_CreateJavaVM");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 if(sym == NULL) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 log_ereport(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 LOG_FAILURE,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 "Cannot get symbol JNI_CreateJavaVM from lib");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 return REQ_ABORTED;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 // create JVM
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 JavaVMInitArgs vm_args;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 JavaVMOption options[2];
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 options[0].optionString = "-Djava.class.path=lib/wsrt.jar";
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 options[1].optionString = "-Djava.library.path=lib/";
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 vm_args.version = JNI_VERSION_1_6;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 vm_args.options = options;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 vm_args.nOptions = 2;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 vm_args.ignoreUnrecognized = JNI_TRUE;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 CreateJVMFunc create_jvm = (CreateJVMFunc)sym;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 if(create_jvm(&jvm, (void**)&env, &vm_args)) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 dlclose(lib);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 log_ereport(LOG_FAILURE, "Cannot create JVM");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 // load classes and methods
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 jclass cls = (*env)->FindClass(env, "webserver/ServerPlugin");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 if(!cls) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 log_ereport(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 LOG_FAILURE,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 "jvm-init: missing or broken wsrt.jar");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 log_ereport(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 LOG_FAILURE,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 "jvm-init: missing class webserver.ServerPlugin");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 return REQ_ABORTED;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 jmethodID constr = (*env)->GetMethodID(env, cls, "<init>", "()V");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 if(!constr) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 log_ereport(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 LOG_FAILURE,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 "jvm-init: missing or broken wsrt.jar");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 log_ereport(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 LOG_FAILURE,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 "jvm-init: missing constructor");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 return REQ_ABORTED;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 plugin_instance = (*env)->NewObject(env, cls, constr);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 if(!plugin_instance) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 log_ereport(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 LOG_FAILURE,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 "jvm-init: cannot create ServerPlugin instance");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 return REQ_ABORTED;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 // methods
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 load_jmodules_method = (*env)->GetMethodID(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 env,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 cls,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 "loadModule",
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 "(Ljava/lang/String;Ljava/lang/String;)I");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 if(!load_jmodules_method) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 log_ereport(LOG_FAILURE, "jvm-init: missing or broken wsrt.jar");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 log_ereport(LOG_FAILURE, "jvm-init: missing method (loadModule)");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 return REQ_ABORTED;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 exec_method = (*env)->GetMethodID(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 env,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 cls,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 "exec",
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 "(I)I");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 if(!exec_method) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 log_ereport(LOG_FAILURE, "jvm-init: missing or broken wsrt.jar");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 log_ereport(LOG_FAILURE, "jvm-init: missing method (exec)");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 return REQ_ABORTED;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 return REQ_PROCEED;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 int load_jmodules(pblock *pb, Session *sn, Request *rq) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 char *jar = pblock_findval("jar", pb);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 char *classes = pblock_findval("classes", pb);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 if(!jar || !classes) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 log_ereport(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 LOG_MISCONFIG,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 "load-jmodule: missing parameters (jar, classes)");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 return REQ_ABORTED;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 // check jvm
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 if(!plugin_instance) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 log_ereport(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 LOG_FAILURE,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 "load-jmodules: java plugin not properly loaded");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 return REQ_ABORTED;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 if(!load_jmodules_method) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 log_ereport(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 LOG_FAILURE,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 "load-jmodules: java plugin not properly loaded");
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 return REQ_ABORTED;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 // load java module
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 jstring jar_param = (*env)->NewStringUTF(env, jar);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 jstring classes_param = (*env)->NewStringUTF(env, classes);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 jint ret = (*env)->CallIntMethod(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 env,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 plugin_instance,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 load_jmodules_method,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 jar_param,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 classes_param);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 // free stuff
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 (*env)->DeleteLocalRef(env, jar_param);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 (*env)->DeleteLocalRef(env, classes_param);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 return ret;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 int jexec(pblock *pb, Session *sn, Request *rq) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 return REQ_NOACTION;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 int jvm_method_exec(FuncStruct *func, pblock *pb, Session *sn, Request *rq) {
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 printf("jvm_method_exec: %d\n", func);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 NSAPIRequest *nrq = (NSAPIRequest*)rq;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 JNIEnv *e = NULL;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 int r = (*jvm)->AttachCurrentThread(jvm, (void**)&e, NULL);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 jint index = (jint)func->exec_data;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 printf("(C) exec: %d\n", index);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 jint ret = (*env)->CallIntMethod(
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 env,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 plugin_instance,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 exec_method,
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 index);
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 return ret;
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 }
afd57ce39ec9 added initial java plugin code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206

mercurial