# HG changeset patch # User Olaf Wintermann # Date 1373274654 -7200 # Node ID afd57ce39ec92b4225d99420062aada3c59f02a4 # Parent 28433f06d5ee0aaeac61c56eb64213734e8b2bf6 added initial java plugin code diff -r 28433f06d5ee -r afd57ce39ec9 make/solaris.mk --- a/make/solaris.mk Mon Jul 01 18:05:13 2013 +0200 +++ b/make/solaris.mk Mon Jul 08 11:10:54 2013 +0200 @@ -31,6 +31,8 @@ LDFLAGS += -lsendfile -lposix4 -lpthread -ldl -lm -lxerces-c -lldap +PLUGINS = java + # platform dependend source files PLATFORM_DAEMONOBJ = event_solaris.o diff -r 28433f06d5ee -r afd57ce39ec9 src/server/Makefile --- a/src/server/Makefile Mon Jul 01 18:05:13 2013 +0200 +++ b/src/server/Makefile Mon Jul 08 11:10:54 2013 +0200 @@ -35,7 +35,7 @@ MAIN_TARGET = $(BUILD_ROOT)/work/bin/webservd -all: preparation $(MAIN_TARGET) +all: preparation $(MAIN_TARGET) $(PLUGINS) include ucx/objs.mk include util/objs.mk @@ -55,7 +55,7 @@ MAINOBJS = $(UCXOBJS) $(UTILOBJS) $(SAFOBJS) $(DAVOBJS) $(DAEMONOBJS) $(CONFOBJS) $(ADMINOBJS) -OBJ_DIRS = daemon safs ucx util webdav config admin +OBJ_DIRS = daemon safs ucx util webdav config admin plugins MK_OBJ_DIRS = $(OBJ_DIRS:%=$(OBJ_DIR)server/%) MK_OBJ_DIRS += $(BUILD_ROOT)/work/bin @@ -74,6 +74,8 @@ $(CC) -o $@ -c $(CFLAGS) $< +$(PLUGINS): $(MAIN_TARGET) + cd plugins/$@/; $(MAKE) all diff -r 28433f06d5ee -r afd57ce39ec9 src/server/daemon/func.c --- a/src/server/daemon/func.c Mon Jul 01 18:05:13 2013 +0200 +++ b/src/server/daemon/func.c Mon Jul 08 11:10:54 2013 +0200 @@ -42,9 +42,16 @@ } void add_function(FuncStruct *func) { - printf("add_function %s\n", func->name); struct FuncStruct *f = malloc(sizeof(FuncStruct)); *f = *func; + char *name = strdup(func->name); + for(int i=0;name[i]!='\0';i++) { + if(name[i] == '_') { + name[i] = '-'; + } + } + f->name = name; + printf("add_function %s\n", f->name); ucx_map_cstr_put(function_map, (char*)f->name, f); } @@ -57,6 +64,11 @@ } FuncStruct* get_function(char *name) { + for(int i=0;name[i]!='\0';i++) { + if(name[i] == '_') { + name[i] = '-'; + } + } return ucx_map_cstr_get(function_map, name); } @@ -82,3 +94,16 @@ return func->func(pb, sn, rq); } + + + +// public API + +struct FuncStruct func_insert(char *name, FuncPtr fn) { + struct FuncStruct fc; + ZERO(&fc, sizeof(FuncStruct)); + fc.func = fn; + fc.name = strdup(name); + add_function(&fc); + return *get_function(name); +} diff -r 28433f06d5ee -r afd57ce39ec9 src/server/daemon/func.h --- a/src/server/daemon/func.h Mon Jul 01 18:05:13 2013 +0200 +++ b/src/server/daemon/func.h Mon Jul 08 11:10:54 2013 +0200 @@ -35,6 +35,10 @@ extern "C" { #endif +#define SAF_EXEC(f,pb,sn,rq) (f)->func_exec ? \ + (f)->func_exec(f, pb, sn, rq) : \ + (f)->func(pb, sn, rq) + void func_init(); void add_function(struct FuncStruct *func); diff -r 28433f06d5ee -r afd57ce39ec9 src/server/daemon/httprequest.c --- a/src/server/daemon/httprequest.c Mon Jul 01 18:05:13 2013 +0200 +++ b/src/server/daemon/httprequest.c Mon Jul 08 11:10:54 2013 +0200 @@ -40,6 +40,7 @@ #include "config.h" #include "vserver.h" #include "httplistener.h" +#include "func.h" #include "error.h" void http_request_init(HTTPRequest *req) { @@ -771,7 +772,7 @@ return nsapi_exec_tp(d, sn, rq, sn->defaultpool); } - return d->func->func(d->param, (Session*)sn, (Request*)rq); + return SAF_EXEC(d->func, d->param, (Session*)sn, (Request*)rq); } int nsapi_exec_tp( @@ -826,7 +827,8 @@ struct _tpd_data *data = d; data->sn->currentpool = data->threadpool; - int r = data->directive->func->func( + int r = SAF_EXEC( + data->directive->func, data->directive->param, (Session*)data->sn, (Request*)data->rq); diff -r 28433f06d5ee -r afd57ce39ec9 src/server/daemon/request.c --- a/src/server/daemon/request.c Mon Jul 01 18:05:13 2013 +0200 +++ b/src/server/daemon/request.c Mon Jul 08 11:10:54 2013 +0200 @@ -95,6 +95,8 @@ nrq->context.objset_index = -1; nrq->context.dtable_index = 0; + + nrq->jvm_context = NULL; // new diff -r 28433f06d5ee -r afd57ce39ec9 src/server/daemon/request.h --- a/src/server/daemon/request.h Mon Jul 01 18:05:13 2013 +0200 +++ b/src/server/daemon/request.h Mon Jul 08 11:10:54 2013 +0200 @@ -43,6 +43,7 @@ RequestPhase phase; VirtualServer *vs; NSAPIContext context; + void *jvm_context; }; /* macros for short context access */ diff -r 28433f06d5ee -r afd57ce39ec9 src/server/daemon/ws-fn.c --- a/src/server/daemon/ws-fn.c Mon Jul 01 18:05:13 2013 +0200 +++ b/src/server/daemon/ws-fn.c Mon Jul 08 11:10:54 2013 +0200 @@ -41,27 +41,27 @@ #include "../admin/admin.h" struct FuncStruct webserver_funcs[] = { - { "init-test", init_test, NULL, 0 }, - { "load-modules", load_modules, NULL, 0}, - { "test-nametrans", test_nametrans, NULL, 0 }, - { "assign-name", assign_name, NULL, 0}, - { "type-by-extension", object_type_by_extension, NULL, 0}, - { "send-file", send_file, NULL, 0}, - { "common-index", service_index, NULL, 0}, - { "service-hello", service_hello, NULL, 0}, - { "send-options", send_options, NULL, 0}, - { "webdav-init", webdav_init, NULL, 0}, - { "webdav-setcollection", webdav_setcollection, NULL, 0}, - { "webdav-service", webdav_service, NULL, 0}, - { "admin-init", admin_init, NULL, 0}, - { "admin-service", admin_service, NULL, 0}, - { "auth-basic", auth_basic, NULL, 0 }, - { "auth-db", auth_db, NULL, 0 }, - { "require-auth", require_auth, NULL, 0}, - { "require-access", require_access, NULL, 0}, - { "append-acl", append_acl, NULL, 0}, - { "check-acl", check_acl, NULL, 0}, - { "print-message", print_message, NULL, 0}, - { "common-log", common_log, NULL, 0}, - {NULL, NULL, NULL, 0} + { "init-test", init_test, NULL, NULL, 0 }, + { "load-modules", load_modules, NULL, NULL, 0}, + { "test-nametrans", test_nametrans, NULL, NULL, 0 }, + { "assign-name", assign_name, NULL, NULL, 0}, + { "type-by-extension", object_type_by_extension, NULL, NULL, 0}, + { "send-file", send_file, NULL, NULL, 0}, + { "common-index", service_index, NULL, NULL, 0}, + { "service-hello", service_hello, NULL, NULL, 0}, + { "send-options", send_options, NULL, NULL, 0}, + { "webdav-init", webdav_init, NULL, NULL, 0}, + { "webdav-setcollection", webdav_setcollection, NULL, NULL, 0}, + { "webdav-service", webdav_service, NULL, NULL, 0}, + { "admin-init", admin_init, NULL, NULL, 0}, + { "admin-service", admin_service, NULL, NULL, 0}, + { "auth-basic", auth_basic, NULL, NULL, 0 }, + { "auth-db", auth_db, NULL, NULL, 0 }, + { "require-auth", require_auth, NULL, NULL, 0}, + { "require-access", require_access, NULL, NULL, 0}, + { "append-acl", append_acl, NULL, NULL, 0}, + { "check-acl", check_acl, NULL, NULL, 0}, + { "print-message", print_message, NULL, NULL, 0}, + { "common-log", common_log, NULL, NULL, 0}, + {NULL, NULL, NULL, NULL, 0} }; diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/Makefile Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,71 @@ +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 2013 Olaf Wintermann. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +BUILD_ROOT = ../../../.. +include $(BUILD_ROOT)/config.mk + +# LDFLAGS += -lpthread -ldl -lnsl -lm -lxerces-c -lldap +CFLAGS += -I/usr/java/include -I/usr/java/include/solaris -I../../ -Kpic +LDFLAGS = -shared -Kpic + +OBJ_DIR = $(BUILD_ROOT)/build/ + +NSAPI_PLUGIN_LIB = $(BUILD_ROOT)/work/lib/libjava_nsapi.so +NSAPI_JNI_LIB = $(BUILD_ROOT)/work/lib/libnsapi_jni.so +JAVA_WSRT = $(BUILD_ROOT)/work/lib/wsrt.jar + +all: preparation $(NSAPI_PLUGIN_LIB) $(NSAPI_JNI_LIB) $(JAVA_WSRT) + +include objs.mk +include javaobjs.mk + +OBJ_DIRS = plugins/java +MK_OBJ_DIRS = $(OBJ_DIRS:%=$(OBJ_DIR)server/%) +MK_OBJ_DIRS += $(BUILD_ROOT)/work/lib + +preparation: $(MK_OBJ_DIRS) + +$(MK_OBJ_DIRS): + mkdir -p $@ + +$(NSAPI_PLUGIN_LIB): $(JVM_PLUGIN_OBJS) + $(CC) -o $(NSAPI_PLUGIN_LIB) $(JVM_PLUGIN_OBJS) $(LDFLAGS) + +$(NSAPI_JNI_LIB): $(JNI_OBJS) + $(CC) -o $(NSAPI_JNI_LIB) $(JNI_OBJS) $(LDFLAGS) + +$(JAVA_WSRT): $(JAVASRC) + ant compile jar + + +../../../../build/server/plugins/java/%.o: %.c + $(CC) -o $@ -c $(CFLAGS) $< + + + + diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/build.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/build.xml Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/javaobjs.mk --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/javaobjs.mk Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,33 @@ +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 2013 Olaf Wintermann. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +JAVASRC = javasrc/webserver/ServerPlugin.java +JAVASRC += javasrc/webserver/nsapi/Module.java +JAVASRC += javasrc/webserver/nsapi/Request.java +JAVASRC += javasrc/webserver/nsapi/SAF.java +JAVASRC += javasrc/webserver/nsapi/Session.java diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/javasrc/webserver/ServerFunction.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/javasrc/webserver/ServerFunction.java Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,40 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package webserver; + +import java.lang.reflect.Method; +import java.util.Random; + +/** + * + * @author olaf + */ +public class ServerFunction { + Object module; + Method method; +} diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/javasrc/webserver/ServerPlugin.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/javasrc/webserver/ServerPlugin.java Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,168 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package webserver; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; +import webserver.nsapi.Module; +import webserver.nsapi.PBlock; +import webserver.nsapi.Request; +import webserver.nsapi.SAF; +import webserver.nsapi.Session; + +class ServerPlugin { + private ArrayList registeredFunctions = new ArrayList<>(32); + + public ServerPlugin() { + + } + + public native void addMethod(String name, int safIndex); + + static { + System.loadLibrary("nsapi_jni"); + } + + public int loadModule(String jar, String classes) { + System.out.println("load-jmodule jar=\""+jar+"\" classes=\""+classes+"\""); + + File jarFile = new File(jar); + if(!jarFile.exists()) { + System.err.println("jar file does not exist"); + return -1; + } + try { + URL[] urls = new URL[]{jarFile.toURI().toURL()}; + URLClassLoader cl = new URLClassLoader(urls, this.getClass().getClassLoader()); + + for(String className : classes.split(",")) { + Class moduleClass = Class.forName(className, true, cl); + loadClass(moduleClass); + } + } catch (MalformedURLException e) { + System.err.println("ServerPlugin.loadModule: "+e); + return -1; + } catch (ClassNotFoundException e) { + System.err.println("Java Module Class not found: "+e); + return -1; + } catch (InstantiationException e) { + System.err.println("ServerPlugin.loadModule: "+e); + return -1; + } catch (IllegalAccessException e) { + System.err.println("ServerPlugin.loadModule: "+e); + return -1; + } + + System.out.println("Java Module loaded"); + return 0; + } + + public void loadClass(Class cl) throws InstantiationException, IllegalAccessException { + Module module = (Module)cl.getAnnotation(Module.class); + if(module == null) { + System.err.println("Missing Module annotation for class: "+cl.getCanonicalName()); + return; + } + + Object moduleInstance = cl.newInstance(); + for(Method m : cl.getMethods()) { + if(isSAF(m)) { + SAF saf = m.getAnnotation(SAF.class); + if(saf.init()) { + + } + if(saf.name().length() > 0) { + ServerFunction f = new ServerFunction(); + f.module = moduleInstance; + f.method = m; + + String safName = module.name() + "." + saf.name(); + registeredFunctions.add(f); + int index = registeredFunctions.lastIndexOf(f); + addMethod(safName, index); + } + } + } + } + + private boolean isSAF(Method m) { + SAF saf = m.getAnnotation(SAF.class); + if(saf != null) { + // check method signature + if(m.getReturnType() != int.class) { + System.err.println("wrong return type for SAF: "+m.getName()); + return false; + } + Class paramTypes[] = m.getParameterTypes(); + if(paramTypes.length != 3) { + System.err.println("wrong number of parameters for SAF: "+m.getName()); + return false; + } + if(paramTypes[0] != PBlock.class) { + System.err.println("wrong method signature for SAF: "+m.getName()); + return false; + } + if(paramTypes[1] != Session.class) { + System.err.println("wrong method signature for SAF: "+m.getName()); + return false; + } + if(paramTypes[2] != Request.class) { + System.err.println("wrong method signature for SAF: "+m.getName()); + return false; + } + return true; + } + return false; + } + + public int exec(int safIndex) { + System.out.println("(Java) exec: "+safIndex); + ServerFunction saf = registeredFunctions.get(safIndex); + if(saf == null) { + return -1; + } + Integer ret = new Integer(-1); + try { + ret = (Integer)saf.method.invoke(saf.module, null, null, null); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return ret.intValue(); + } +} diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/javasrc/webserver/nsapi/Module.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/javasrc/webserver/nsapi/Module.java Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,44 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package webserver.nsapi; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * + * @author Olaf Wintermann + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Module { + String name(); +} diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/javasrc/webserver/nsapi/PBlock.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/javasrc/webserver/nsapi/PBlock.java Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,38 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package webserver.nsapi; + +import java.util.HashMap; + +/** + * + * @author Olaf Wintermann + */ +public class PBlock extends HashMap { + +} diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/javasrc/webserver/nsapi/Request.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/javasrc/webserver/nsapi/Request.java Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,36 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package webserver.nsapi; + +/** + * + * @author Olaf Wintermann + */ +public class Request { + +} diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/javasrc/webserver/nsapi/SAF.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/javasrc/webserver/nsapi/SAF.java Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,44 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package webserver.nsapi; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author Olaf Wintermann + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface SAF { + String name() default ""; + boolean init() default false; +} diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/javasrc/webserver/nsapi/Session.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/javasrc/webserver/nsapi/Session.java Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,36 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package webserver.nsapi; + +/** + * + * @author Olaf Wintermann + */ +public class Session { + +} diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/jvm.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/jvm.c Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,206 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include + +#include "jvm.h" +#include "daemon/request.h" + +#ifdef __i386 +#define LIBJVM "/usr/java/jre/lib/i386/server/libjvm.so" +#else + +#endif + +static JNIEnv *env = NULL; +static JavaVM *jvm = NULL; + +static jobject plugin_instance = NULL; +static jmethodID load_jmodules_method = NULL; +static jmethodID exec_method = NULL; + +int jvm_init(pblock *pb, Session *sn, Request *rq) { + // load lib + void *lib = dlopen(LIBJVM, RTLD_GLOBAL | RTLD_NOW); + if(!lib) { + log_ereport(LOG_FAILURE, "Cannot load libjvm.so"); + return REQ_ABORTED; + } + + // get symbol + void *sym = dlsym(lib, "JNI_CreateJavaVM"); + if(sym == NULL) { + log_ereport( + LOG_FAILURE, + "Cannot get symbol JNI_CreateJavaVM from lib"); + return REQ_ABORTED; + } + + // create JVM + JavaVMInitArgs vm_args; + JavaVMOption options[2]; + options[0].optionString = "-Djava.class.path=lib/wsrt.jar"; + options[1].optionString = "-Djava.library.path=lib/"; + vm_args.version = JNI_VERSION_1_6; + vm_args.options = options; + vm_args.nOptions = 2; + vm_args.ignoreUnrecognized = JNI_TRUE; + + CreateJVMFunc create_jvm = (CreateJVMFunc)sym; + if(create_jvm(&jvm, (void**)&env, &vm_args)) { + dlclose(lib); + log_ereport(LOG_FAILURE, "Cannot create JVM"); + } + + // load classes and methods + jclass cls = (*env)->FindClass(env, "webserver/ServerPlugin"); + if(!cls) { + log_ereport( + LOG_FAILURE, + "jvm-init: missing or broken wsrt.jar"); + log_ereport( + LOG_FAILURE, + "jvm-init: missing class webserver.ServerPlugin"); + return REQ_ABORTED; + } + + jmethodID constr = (*env)->GetMethodID(env, cls, "", "()V"); + if(!constr) { + log_ereport( + LOG_FAILURE, + "jvm-init: missing or broken wsrt.jar"); + log_ereport( + LOG_FAILURE, + "jvm-init: missing constructor"); + return REQ_ABORTED; + } + plugin_instance = (*env)->NewObject(env, cls, constr); + if(!plugin_instance) { + log_ereport( + LOG_FAILURE, + "jvm-init: cannot create ServerPlugin instance"); + return REQ_ABORTED; + } + + // methods + load_jmodules_method = (*env)->GetMethodID( + env, + cls, + "loadModule", + "(Ljava/lang/String;Ljava/lang/String;)I"); + if(!load_jmodules_method) { + log_ereport(LOG_FAILURE, "jvm-init: missing or broken wsrt.jar"); + log_ereport(LOG_FAILURE, "jvm-init: missing method (loadModule)"); + return REQ_ABORTED; + } + + exec_method = (*env)->GetMethodID( + env, + cls, + "exec", + "(I)I"); + if(!exec_method) { + log_ereport(LOG_FAILURE, "jvm-init: missing or broken wsrt.jar"); + log_ereport(LOG_FAILURE, "jvm-init: missing method (exec)"); + return REQ_ABORTED; + } + + + return REQ_PROCEED; +} + +int load_jmodules(pblock *pb, Session *sn, Request *rq) { + char *jar = pblock_findval("jar", pb); + char *classes = pblock_findval("classes", pb); + + if(!jar || !classes) { + log_ereport( + LOG_MISCONFIG, + "load-jmodule: missing parameters (jar, classes)"); + return REQ_ABORTED; + } + + // check jvm + if(!plugin_instance) { + log_ereport( + LOG_FAILURE, + "load-jmodules: java plugin not properly loaded"); + return REQ_ABORTED; + } + if(!load_jmodules_method) { + log_ereport( + LOG_FAILURE, + "load-jmodules: java plugin not properly loaded"); + return REQ_ABORTED; + } + + // load java module + jstring jar_param = (*env)->NewStringUTF(env, jar); + jstring classes_param = (*env)->NewStringUTF(env, classes); + + jint ret = (*env)->CallIntMethod( + env, + plugin_instance, + load_jmodules_method, + jar_param, + classes_param); + + // free stuff + (*env)->DeleteLocalRef(env, jar_param); + (*env)->DeleteLocalRef(env, classes_param); + + return ret; +} + +int jexec(pblock *pb, Session *sn, Request *rq) { + return REQ_NOACTION; +} + +int jvm_method_exec(FuncStruct *func, pblock *pb, Session *sn, Request *rq) { + printf("jvm_method_exec: %d\n", func); + NSAPIRequest *nrq = (NSAPIRequest*)rq; + + JNIEnv *e = NULL; + int r = (*jvm)->AttachCurrentThread(jvm, (void**)&e, NULL); + + jint index = (jint)func->exec_data; + printf("(C) exec: %d\n", index); + + jint ret = (*env)->CallIntMethod( + env, + plugin_instance, + exec_method, + index); + + return ret; +} + diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/jvm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/jvm.h Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,51 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef JVM_H +#define JVM_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef jint (*CreateJVMFunc)(JavaVM **, void **, void *); + +int jvm_method_exec(FuncStruct *func, pblock *pb, Session *sn, Request *rq); + + + + +#ifdef __cplusplus +} +#endif + +#endif /* JVM_H */ + diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/objs.mk --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/objs.mk Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,42 @@ +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 2013 Olaf Wintermann. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +JVM_PLUGIN_SRC_DIR = server/plugins/java/ + +JVM_PLUGIN_OBJPRE = $(OBJ_DIR)$(JVM_PLUGIN_SRC_DIR) + +JVM_PLUGIN_OBJ = jvm.o + +JVM_PLUGIN_OBJS = $(JVM_PLUGIN_OBJ:%=$(JVM_PLUGIN_OBJPRE)%) +JVM_PLUGIN_SOURCE = $(JVM_PLUGIN_OBJ:%.o=/plugins/java/%.c) + + +JNI_OBJ = wsjni.o + +JNI_OBJS = $(JNI_OBJ:%=$(JVM_PLUGIN_OBJPRE)%) +JNI_SOURCE = $(JNI_OBJ:%.o=/plugins/java/%.c) diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/wsjni.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/wsjni.c Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,55 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include + +#include "wsjni.h" +#include "jvm.h" + + +JNIEXPORT void JNICALL Java_webserver_ModuleLoader_addMethod + (JNIEnv *e, jobject loader, jstring funcName, jint safindex) +{ + const char *name = (*e)->GetStringUTFChars(e, funcName , NULL); + printf("addMethod: Name: {%s} index: %d\n", name, safindex); + + FuncStruct func; + func.name = strdup(name); + func.func = NULL; + func.func_exec = jvm_method_exec; + func.exec_data = (void*)safindex; + printf("exec_data: %d\n", func.exec_data); + add_function(&func); + + (*e)->ReleaseStringUTFChars(e, funcName, name); +} + diff -r 28433f06d5ee -r afd57ce39ec9 src/server/plugins/java/wsjni.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/java/wsjni.h Mon Jul 08 11:10:54 2013 +0200 @@ -0,0 +1,53 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef JNI_H +#define JNI_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: webserver_ModuleLoader + * Method: addMethod + * Signature: (Ljava/lang/String;I)V + */ +JNIEXPORT void JNICALL Java_webserver_ModuleLoader_addMethod + (JNIEnv *, jobject, jstring, jint); + + +#ifdef __cplusplus +} +#endif + +#endif /* JNI_H */ + diff -r 28433f06d5ee -r afd57ce39ec9 src/server/public/nsapi.h --- a/src/server/public/nsapi.h Mon Jul 01 18:05:13 2013 +0200 +++ b/src/server/public/nsapi.h Mon Jul 08 11:10:54 2013 +0200 @@ -683,17 +683,24 @@ PRNetAddr *pr_local_addr; }; + +typedef struct FuncStruct FuncStruct; + /* * FuncPtr is a pointer to an NSAPI SAF function */ #ifdef XP_UNIX typedef int Func(pblock *, Session *, Request *); +// new func executor +typedef int FuncExec(FuncStruct *, pblock *, Session *, Request *); #else /* XP_WIN32 */ typedef int _cdecl Func(pblock *, Session *, Request *); +typedef int _cdecl uncExec(FuncStruct *, pblock *, Session *, Request *); #endif /* XP_WIN32 */ -typedef Func *FuncPtr; +typedef Func *FuncPtr; +typedef FuncExec *FuncExecPtr; /* * FuncStruct is a structure used in the static declaration of the @@ -701,12 +708,12 @@ * startup. */ -typedef struct FuncStruct FuncStruct; - struct FuncStruct { - const char * name; - FuncPtr func; - struct FuncStruct *next; + const char *name; + FuncPtr func; + //struct FuncStruct *next; + FuncExecPtr func_exec; + void *exec_data; unsigned flags; unsigned poolID; unsigned pool_resolved; @@ -1352,8 +1359,9 @@ // func util functions FuncStruct* func_resolve(pblock *pb, Session *sn, Request *rq); -int func_exec (pblock *pb, Session *sn, Request *rq); - +int func_exec(pblock *pb, Session *sn, Request *rq); +struct FuncStruct func_insert(char *name, FuncPtr fn); +#define func_insert func_insert void protocol_status(Session *sn, Request *rq, int n, const char *m); diff -r 28433f06d5ee -r afd57ce39ec9 src/server/safs/init.c --- a/src/server/safs/init.c Mon Jul 01 18:05:13 2013 +0200 +++ b/src/server/safs/init.c Mon Jul 08 11:10:54 2013 +0200 @@ -73,6 +73,9 @@ // load function symbols int b = 0; for(int i=0;;i++) { + if(funcs[i] == '-') { + funcs[i] = '_'; + } if(funcs[i] == ',' || funcs[i] == 0) { if(funcs[i] == 0) { b = 1; @@ -80,10 +83,11 @@ funcs[i] = 0; - // we have a function name + // we have a function name void *sym = dlsym(lib, funcs); if(sym == NULL) { fprintf(stderr, "Cannot load symbol %s\n", funcs); + dlclose(lib); return REQ_ABORTED; } struct FuncStruct fc; @@ -99,6 +103,6 @@ i = 0; } } - + return REQ_PROCEED; }