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