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 package webserver;
29
30 import java.io.
File;
31 import java.lang.reflect.
InvocationTargetException;
32 import java.lang.reflect.
Method;
33 import java.net.
MalformedURLException;
34 import java.net.
URL;
35 import java.net.
URLClassLoader;
36 import java.util.
ArrayList;
37 import java.util.logging.
Level;
38 import java.util.logging.
Logger;
39 import webserver.nsapi.
Module;
40 import webserver.nsapi.
PBlock;
41 import webserver.nsapi.
Request;
42 import webserver.nsapi.
SAF;
43 import webserver.nsapi.
Session;
44
45 class ServerPlugin {
46 private ArrayList<
ServerFunction> registeredFunctions =
new ArrayList<>(
32);
47
48 public ServerPlugin() {
49
50 }
51
52 public native void addMethod(
String name,
int safIndex);
53
54 static {
55 System.loadLibrary(
"nsapi_jni");
56 }
57
58 public int loadModule(
String jar,
String classes) {
59 System.out.println(
"load-jmodule jar=\""+jar+
"\" classes=\""+classes+
"\"");
60
61 File jarFile =
new File(jar);
62 if(!jarFile.exists()) {
63 System.err.println(
"jar file does not exist");
64 return -
1;
65 }
66 try {
67 URL[] urls =
new URL[]{jarFile.toURI().toURL()};
68 URLClassLoader cl =
new URLClassLoader(urls,
this.getClass().getClassLoader());
69
70 for(
String className : classes.split(
",")) {
71 Class moduleClass =
Class.forName(className, true, cl);
72 loadClass(moduleClass);
73 }
74 }
catch (
MalformedURLException e) {
75 System.err.println(
"ServerPlugin.loadModule: "+e);
76 return -
1;
77 }
catch (
ClassNotFoundException e) {
78 System.err.println(
"Java Module Class not found: "+e);
79 return -
1;
80 }
catch (
InstantiationException e) {
81 System.err.println(
"ServerPlugin.loadModule: "+e);
82 return -
1;
83 }
catch (
IllegalAccessException e) {
84 System.err.println(
"ServerPlugin.loadModule: "+e);
85 return -
1;
86 }
87
88 System.out.println(
"Java Module loaded");
89 return 0;
90 }
91
92 public void loadClass(
Class cl)
throws InstantiationException,
IllegalAccessException {
93 Module module = (
Module)cl.getAnnotation(
Module.
class);
94 if(module == null) {
95 System.err.println(
"Missing Module annotation for class: "+cl.getCanonicalName());
96 return;
97 }
98
99 Object moduleInstance = cl.newInstance();
100 for(
Method m : cl.getMethods()) {
101 if(isSAF(m)) {
102 SAF saf = m.getAnnotation(
SAF.
class);
103 if(saf.init()) {
104
105 }
106 if(saf.name().length() >
0) {
107 ServerFunction f =
new ServerFunction();
108 f.module = moduleInstance;
109 f.method = m;
110
111 String safName = module.name() +
"." + saf.name();
112 registeredFunctions.add(f);
113 int index = registeredFunctions.lastIndexOf(f);
114 addMethod(safName, index);
115 }
116 }
117 }
118 }
119
120 private boolean isSAF(
Method m) {
121 SAF saf = m.getAnnotation(
SAF.
class);
122 if(saf != null) {
123
124 if(m.getReturnType() !=
int.
class) {
125 System.err.println(
"wrong return type for SAF: "+m.getName());
126 return false;
127 }
128 Class paramTypes[] = m.getParameterTypes();
129 if(paramTypes.length !=
3) {
130 System.err.println(
"wrong number of parameters for SAF: "+m.getName());
131 return false;
132 }
133 if(paramTypes[
0] !=
PBlock.
class) {
134 System.err.println(
"wrong method signature for SAF: "+m.getName());
135 return false;
136 }
137 if(paramTypes[
1] !=
Session.
class) {
138 System.err.println(
"wrong method signature for SAF: "+m.getName());
139 return false;
140 }
141 if(paramTypes[
2] !=
Request.
class) {
142 System.err.println(
"wrong method signature for SAF: "+m.getName());
143 return false;
144 }
145 return true;
146 }
147 return false;
148 }
149
150 public int exec(
int safIndex) {
151 System.out.println(
"(Java) exec: "+safIndex);
152 ServerFunction saf = registeredFunctions.get(safIndex);
153 if(saf == null) {
154 return -
1;
155 }
156 Integer ret =
new Integer(-
1);
157 try {
158 ret = (
Integer)saf.method.invoke(saf.module, null, null, null);
159 }
catch (
IllegalAccessException e) {
160 e.printStackTrace();
161 }
catch (
IllegalArgumentException e) {
162 e.printStackTrace();
163 }
catch (
InvocationTargetException e) {
164 e.printStackTrace();
165 }
166 return ret.intValue();
167 }
168 }
169