-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
64 lines (55 loc) · 2.45 KB
/
Main.java
File metadata and controls
64 lines (55 loc) · 2.45 KB
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
package com.leacox.sandbox.runtime.simple;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Map;
/**
* The main method and bootstrapper for the sandboxed runtime environment.
*
* @author John Leacox
*/
public class Main {
public static void main(String[] args) throws Exception {
// Add jars to this loader that should be shared between the runtime and the user runnables.
ClassLoader commonLoader = new URLClassLoader(new URL[] {}, null);
// For a real runtime it's probably best to create an assembly with a lib directory to load
// all of the required jars from.
ClassLoader runnerLoader = new URLClassLoader(new URL[] {
new File(
"simple-runtime/target/simple-runtime-1.0-SNAPSHOT.jar").toURI().toURL(),
new File(
"runtime-stubs-definitions/target/runtime-stubs-definitions-1.0-SNAPSHOT.jar")
.toURI().toURL()
}, commonLoader);
try {
Thread.currentThread().setContextClassLoader(runnerLoader);
Class<?> runtimeClass = runnerLoader.loadClass("com.leacox.sandbox.runtime.simple.SimpleRuntime");
Object runtimeInstance = runtimeClass.newInstance();
String startMethodName = "start";
Method startMethod = runtimeClass.getMethod(startMethodName, ClassLoader.class);
startMethod.invoke(runtimeInstance, commonLoader);
Map<String, String> runners = new HashMap<>();
runners.put(
"com.leacox.sandbox.runtime.simple.AllowedRunner",
"simple-runtime-examples/target/simple-runtime-examples-1.0-SNAPSHOT.jar");
runners.put(
"com.leacox.sandbox.runtime.simple.DeniedRunner",
"simple-runtime-examples/target/simple-runtime-examples-1.0-SNAPSHOT.jar");
// These jars attempt to avoid being killed.
//runners.put(
// "com.leacox.sandbox.runtime.simple.NeverEndingRunner",
// "simple-runtime-examples/target/simple-runtime-examples-1.0-SNAPSHOT.jar");
//runners.put(
// "com.leacox.sandbox.runtime.simple.IterativeNeverEndingRunner",
// "simple-runtime-examples/target/simple-runtime-examples-1.0-SNAPSHOT.jar");
String runMethodName = "run";
Method runMethod = runtimeClass.getMethod(runMethodName, Map.class);
runMethod.invoke(runtimeInstance, runners);
} catch (Exception e) {
System.out.println(e);
throw new RuntimeException(e);
}
}
}