-
Notifications
You must be signed in to change notification settings - Fork 1
/
Entrypoint.java
259 lines (216 loc) · 8.74 KB
/
Entrypoint.java
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
package example;
import com.aliyun.fc.runtime.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Entrypoint implements StreamRequestHandler, FunctionInitializer, HttpRequestHandler {
private ClassLoader nasLibClassloader;
private static final Map<String, Object> handlerObjectCache = new ConcurrentHashMap<>();
{
List<URL> classpathExt = Stream.of("/mnt/auto/lib", "/code")
.map(p -> new File(p))
.flatMap(f -> Stream.concat(Stream.of(f), listJar(f)))
.map(f -> {
try {
return f.toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
})
.collect(Collectors.toList());
nasLibClassloader = new ChildFirstURLClassLoader(classpathExt.toArray(new URL[0]), Thread.currentThread().getContextClassLoader());
}
private Stream<File> listJar(File dir) {
File[] jarFiles = dir.listFiles((_dir, name) ->
name.endsWith(".jar") || name.endsWith(".JAR") || name.endsWith(".zip") || name.endsWith(".ZIP"));
if (jarFiles == null) {
return Stream.empty();
} else {
return Stream.of(jarFiles);
}
}
private Class<?> loadClass(String className, Class<?> superClass) {
try {
Class customerClass = Class.forName(className, true, nasLibClassloader);
if (PojoRequestHandler.class.isAssignableFrom(customerClass)) {
throw new RuntimeException("interface com.aliyun.fc.runtime.PojoRequestHandler is not support, please use other com.aliyun.fc.runtime interfaces");
}
if (!superClass.isAssignableFrom(customerClass)) {
throw new RuntimeException(String.format("Handler class '%s' must implement one of the com.aliyun.fc.runtime interfaces", className));
}
return customerClass;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
private Object newObject(Class<?> clasz) {
try {
return clasz.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private Object getHandlerObject(String className, Class<?> superClass) {
Object handler = handlerObjectCache.get(className);
if (handler == null) {
handler = newObject(loadClass(className, superClass));
handlerObjectCache.put(className, handler);
}
return handler;
}
private String[] splitHandlerName(String handlerName) {
String[] splittedNames = handlerName.split("::");
if (splittedNames.length != 2) {
throw new RuntimeException(String.format("Handler '%s' must contain one and only one \'::\'", handlerName));
}
return splittedNames;
}
private void invokeMethod(String envKey,Class<?> superClass, Class<?>[] parameterTypes, Object... args) {
String handlerName = System.getenv(envKey);
if (handlerName == null) {
throw new RuntimeException(String.format("ENV: '%s' is not set", envKey));
}
String[] splittedNames = splitHandlerName(handlerName);
String className = splittedNames[0];
String methodName = splittedNames[1];
Class<?> customClass = loadClass(className, FunctionInitializer.class);
try {
Method initialize = customClass.getDeclaredMethod(methodName, parameterTypes);
initialize.invoke(getHandlerObject(className, superClass), args);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private Class<?>[] asClassList(Class<?>... classes) {
return classes;
}
public void initialize(Context context) throws IOException {
Thread.currentThread().setContextClassLoader(nasLibClassloader);
invokeMethod("FUN_INITIALIZER", FunctionInitializer.class, asClassList(Context.class), context);
}
@Override
public void handleRequest(
InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
Thread.currentThread().setContextClassLoader(nasLibClassloader);
invokeMethod("FUN_HANDLER", StreamRequestHandler.class, asClassList(InputStream.class, OutputStream.class, Context.class), inputStream, outputStream, context);
}
@Override
public void handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Context context) throws IOException, ServletException {
Thread.currentThread().setContextClassLoader(nasLibClassloader);
invokeMethod("FUN_HANDLER", HttpRequestHandler.class, asClassList(HttpServletRequest.class, HttpServletResponse.class, Context.class), httpServletRequest, httpServletResponse, context);
}
}
class ChildFirstURLClassLoader extends URLClassLoader {
private ClassLoader system;
public ChildFirstURLClassLoader(URL[] classpath, ClassLoader parent) {
super(classpath, parent);
system = getSystemClassLoader();
}
@Override
protected synchronized Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
if (system != null) {
try {
// checking system: jvm classes, endorsed, cmd classpath, etc.
c = system.loadClass(name);
} catch (ClassNotFoundException ignored) {
}
}
if (c == null) {
try {
// checking local
c = findClass(name);
} catch (ClassNotFoundException e) {
// checking parent
// This call to loadClass may eventually call findClass again, in case the parent doesn't find anything.
c = super.loadClass(name, resolve);
}
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
@Override
public URL getResource(String name) {
URL url = null;
if (system != null) {
url = system.getResource(name);
}
if (url == null) {
url = findResource(name);
if (url == null) {
// This call to getResource may eventually call findResource again, in case the parent doesn't find anything.
url = super.getResource(name);
}
}
return url;
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {
/**
* Similar to super, but local resources are enumerated before parent resources
*/
Enumeration<URL> systemUrls = null;
if (system != null) {
systemUrls = system.getResources(name);
}
Enumeration<URL> localUrls = findResources(name);
Enumeration<URL> parentUrls = null;
if (getParent() != null) {
parentUrls = getParent().getResources(name);
}
final List<URL> urls = new ArrayList<URL>();
if (systemUrls != null) {
while (systemUrls.hasMoreElements()) {
urls.add(systemUrls.nextElement());
}
}
if (localUrls != null) {
while (localUrls.hasMoreElements()) {
urls.add(localUrls.nextElement());
}
}
if (parentUrls != null) {
while (parentUrls.hasMoreElements()) {
urls.add(parentUrls.nextElement());
}
}
return new Enumeration<URL>() {
Iterator<URL> iter = urls.iterator();
public boolean hasMoreElements() {
return iter.hasNext();
}
public URL nextElement() {
return iter.next();
}
};
}
@Override
public InputStream getResourceAsStream(String name) {
URL url = getResource(name);
try {
return url != null ? url.openStream() : null;
} catch (IOException e) {
}
return null;
}
}