jaroslav@323
|
1 |
/**
|
jaroslav@323
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@323
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@323
|
4 |
*
|
jaroslav@323
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@323
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@323
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@323
|
8 |
*
|
jaroslav@323
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@323
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@323
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@323
|
12 |
* GNU General Public License for more details.
|
jaroslav@323
|
13 |
*
|
jaroslav@323
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@323
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@323
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@323
|
17 |
*/
|
jaroslav@323
|
18 |
package org.apidesign.bck2brwsr.launcher;
|
jaroslav@323
|
19 |
|
jaroslav@331
|
20 |
import java.io.IOException;
|
jaroslav@323
|
21 |
import java.io.InputStream;
|
jaroslav@323
|
22 |
import java.net.URL;
|
jaroslav@323
|
23 |
import java.util.Enumeration;
|
jaroslav@348
|
24 |
import java.util.LinkedHashSet;
|
jaroslav@348
|
25 |
import java.util.Set;
|
jaroslav@362
|
26 |
import java.util.logging.Logger;
|
jaroslav@356
|
27 |
import javax.script.Invocable;
|
jaroslav@356
|
28 |
import javax.script.ScriptEngine;
|
jaroslav@356
|
29 |
import javax.script.ScriptEngineManager;
|
jaroslav@356
|
30 |
import javax.script.ScriptException;
|
jaroslav@323
|
31 |
import org.apidesign.vm4brwsr.Bck2Brwsr;
|
jaroslav@323
|
32 |
|
jaroslav@323
|
33 |
/**
|
jaroslav@370
|
34 |
* Tests execution in Java's internal scripting engine.
|
jaroslav@323
|
35 |
*/
|
jaroslav@370
|
36 |
public final class JSLauncher {
|
jaroslav@370
|
37 |
private static final Logger LOG = Logger.getLogger(JSLauncher.class.getName());
|
jaroslav@348
|
38 |
private Set<ClassLoader> loaders = new LinkedHashSet<>();
|
jaroslav@357
|
39 |
private final Res resources = new Res();
|
jaroslav@370
|
40 |
private Invocable code;
|
jaroslav@370
|
41 |
private Object console;
|
jaroslav@348
|
42 |
|
jaroslav@348
|
43 |
|
jaroslav@348
|
44 |
public MethodInvocation addMethod(Class<?> clazz, String method) {
|
jaroslav@348
|
45 |
loaders.add(clazz.getClassLoader());
|
jaroslav@370
|
46 |
MethodInvocation mi = new MethodInvocation(clazz.getName(), method);
|
jaroslav@370
|
47 |
try {
|
jaroslav@370
|
48 |
mi.result = code.invokeMethod(
|
jaroslav@370
|
49 |
console,
|
jaroslav@370
|
50 |
"invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
|
jaroslav@370
|
51 |
mi.className, mi.methodName).toString();
|
jaroslav@370
|
52 |
} catch (ScriptException scriptException) {
|
jaroslav@370
|
53 |
mi.exception = scriptException;
|
jaroslav@370
|
54 |
} catch (NoSuchMethodException noSuchMethodException) {
|
jaroslav@370
|
55 |
mi.exception = noSuchMethodException;
|
jaroslav@370
|
56 |
}
|
jaroslav@370
|
57 |
return mi;
|
jaroslav@348
|
58 |
}
|
jaroslav@348
|
59 |
|
jaroslav@357
|
60 |
public void addClassLoader(ClassLoader url) {
|
jaroslav@357
|
61 |
this.loaders.add(url);
|
jaroslav@357
|
62 |
}
|
jaroslav@348
|
63 |
|
jaroslav@370
|
64 |
public void initialize() throws IOException {
|
jaroslav@356
|
65 |
try {
|
jaroslav@370
|
66 |
initRhino();
|
jaroslav@356
|
67 |
} catch (Exception ex) {
|
jaroslav@356
|
68 |
if (ex instanceof IOException) {
|
jaroslav@356
|
69 |
throw (IOException)ex;
|
jaroslav@356
|
70 |
}
|
jaroslav@356
|
71 |
if (ex instanceof RuntimeException) {
|
jaroslav@356
|
72 |
throw (RuntimeException)ex;
|
jaroslav@356
|
73 |
}
|
jaroslav@356
|
74 |
throw new IOException(ex);
|
jaroslav@356
|
75 |
}
|
jaroslav@356
|
76 |
}
|
jaroslav@356
|
77 |
|
jaroslav@370
|
78 |
private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
|
jaroslav@356
|
79 |
StringBuilder sb = new StringBuilder();
|
jaroslav@356
|
80 |
Bck2Brwsr.generate(sb, new Res());
|
jaroslav@356
|
81 |
|
jaroslav@356
|
82 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@370
|
83 |
ScriptEngine mach = sem.getEngineByExtension("js");
|
jaroslav@356
|
84 |
|
jaroslav@356
|
85 |
sb.append(
|
jaroslav@360
|
86 |
"\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.Console.read);"
|
jaroslav@356
|
87 |
+ "\nfunction initVM() { return vm; };"
|
jaroslav@356
|
88 |
+ "\n");
|
jaroslav@356
|
89 |
|
jaroslav@356
|
90 |
Object res = mach.eval(sb.toString());
|
jaroslav@356
|
91 |
if (!(mach instanceof Invocable)) {
|
jaroslav@356
|
92 |
throw new IOException("It is invocable object: " + res);
|
jaroslav@356
|
93 |
}
|
jaroslav@370
|
94 |
code = (Invocable) mach;
|
jaroslav@356
|
95 |
|
jaroslav@356
|
96 |
Object vm = code.invokeFunction("initVM");
|
jaroslav@370
|
97 |
console = code.invokeMethod(vm, "loadClass", Console.class.getName());
|
jaroslav@356
|
98 |
}
|
jaroslav@356
|
99 |
|
jaroslav@348
|
100 |
private class Res implements Bck2Brwsr.Resources {
|
jaroslav@348
|
101 |
@Override
|
jaroslav@348
|
102 |
public InputStream get(String resource) throws IOException {
|
jaroslav@348
|
103 |
for (ClassLoader l : loaders) {
|
jaroslav@348
|
104 |
URL u = null;
|
jaroslav@348
|
105 |
Enumeration<URL> en = l.getResources(resource);
|
jaroslav@348
|
106 |
while (en.hasMoreElements()) {
|
jaroslav@348
|
107 |
u = en.nextElement();
|
jaroslav@348
|
108 |
}
|
jaroslav@348
|
109 |
if (u != null) {
|
jaroslav@348
|
110 |
return u.openStream();
|
jaroslav@348
|
111 |
}
|
jaroslav@348
|
112 |
}
|
jaroslav@348
|
113 |
throw new IOException("Can't find " + resource);
|
jaroslav@348
|
114 |
}
|
jaroslav@348
|
115 |
}
|
jaroslav@323
|
116 |
}
|