File: servlet_plugin.c

package info (click to toggle)
uwsgi 2.0.29-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,684 kB
  • sloc: ansic: 87,027; python: 7,001; cpp: 1,131; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (90 lines) | stat: -rw-r--r-- 2,577 bytes parent folder | download | duplicates (8)
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
#include <jvm.h>

/*

	Servlet 2.5 JVM handler

*/

#define UWSGI_JVM_REQUEST_HANDLER_SERVLET	2

extern struct uwsgi_jvm ujvm;

struct uwsgi_servlet {
} uservlet;

static struct uwsgi_option uwsgi_servlet_options[] = {
        {0, 0, 0, 0},
};

// the request handler
static int uwsgi_servlet_request(struct wsgi_request *wsgi_req) {
	return UWSGI_OK;
}

static int uwsgi_servlet_setup() {
	uwsgi_log("loading servlet environment...\n");

	jclass servlet = uwsgi_jvm_class("org/apache/jasper/servlet/JspServlet");
	uwsgi_log("jclass = %p\n", servlet);

	jmethodID mid = uwsgi_jvm_get_method_id(servlet, "<init>", "()V");
        if (uwsgi_jvm_exception() || !mid) exit(1);
        jobject instance = (*ujvm_env)->NewObject(ujvm_env, servlet, mid);
	if (uwsgi_jvm_exception() || !instance) exit(1);

	uwsgi_log("done\n");

	jclass uwsgi_servlet_config = uwsgi_jvm_class("uWSGIServletConfig");
	mid = uwsgi_jvm_get_method_id(uwsgi_servlet_config, "<init>", "()V");
	jobject config = (*ujvm_env)->NewObject(ujvm_env, uwsgi_servlet_config, mid);

	mid = uwsgi_jvm_get_method_id(servlet, "init", "(Ljavax/servlet/ServletConfig;)V");
	uwsgi_jvm_call_object(instance, mid, config );

	uwsgi_log("SERVLET initialized\n");

	jclass uwsgi_request = uwsgi_jvm_class("uWSGIServletRequest");
	jclass uwsgi_response = uwsgi_jvm_class("uWSGIServletResponse");

	uwsgi_log("%p %p\n", uwsgi_request, uwsgi_response);

	mid = uwsgi_jvm_get_method_id(uwsgi_request , "<init>", "()V");
	if (uwsgi_jvm_exception() || !mid) exit(1);
	jobject request = (*ujvm_env)->NewObject(ujvm_env, uwsgi_request, mid);

	mid = uwsgi_jvm_get_method_id(uwsgi_response , "<init>", "()V");
	if (uwsgi_jvm_exception() || !mid) exit(1);
	jobject response = (*ujvm_env)->NewObject(ujvm_env, uwsgi_response, mid);

	uwsgi_log("%p %p\n", request, response);

	mid = uwsgi_jvm_get_method_id(servlet, "service", "(Ljavax/servlet/ServletRequest;Ljavax/servlet/ServletResponse;)V");
	if (uwsgi_jvm_exception() || !mid) exit(1);

	uwsgi_jvm_call_object(instance, mid, request, response);

	uwsgi_log("done\n");

	mid = uwsgi_jvm_get_method_id(uwsgi_response, "flushBuffer", "()V");
	if (uwsgi_jvm_exception() || !mid) exit(1);
	uwsgi_jvm_call_object(response, mid);

	uwsgi_log("servlet loaded\n");
	return 0;
}

static int uwsgi_servlet_init() {
	
	if (uwsgi_jvm_register_request_handler(UWSGI_JVM_REQUEST_HANDLER_SERVLET, uwsgi_servlet_setup, uwsgi_servlet_request)) {
		exit(1);
	}

	return 0;
}

struct uwsgi_plugin servlet_plugin = {
	.name = "servlet",
	.options = uwsgi_servlet_options,
	.init = uwsgi_servlet_init,
};