2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.launcher;
20 import java.io.IOException;
21 import java.io.InputStream;
23 import java.util.Enumeration;
24 import java.util.LinkedHashSet;
26 import java.util.logging.Logger;
27 import javax.script.Invocable;
28 import javax.script.ScriptEngine;
29 import javax.script.ScriptEngineManager;
30 import javax.script.ScriptException;
31 import org.apidesign.vm4brwsr.Bck2Brwsr;
34 * Tests execution in Java's internal scripting engine.
36 final class JSLauncher extends Launcher {
37 private static final Logger LOG = Logger.getLogger(JSLauncher.class.getName());
38 private Set<ClassLoader> loaders = new LinkedHashSet<>();
39 private final Res resources = new Res();
40 private Invocable code;
41 private StringBuilder codeSeq;
42 private Object console;
46 public MethodInvocation addMethod(Class<?> clazz, String method) {
47 loaders.add(clazz.getClassLoader());
48 MethodInvocation mi = new MethodInvocation(clazz.getName(), method);
50 mi.result(code.invokeMethod(
52 "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
53 mi.className, mi.methodName).toString(), null);
54 } catch (ScriptException | NoSuchMethodException ex) {
60 public void addClassLoader(ClassLoader url) {
61 this.loaders.add(url);
65 public void initialize() throws IOException {
68 } catch (Exception ex) {
69 if (ex instanceof IOException) {
70 throw (IOException)ex;
72 if (ex instanceof RuntimeException) {
73 throw (RuntimeException)ex;
75 throw new IOException(ex);
79 private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
80 StringBuilder sb = new StringBuilder();
81 Bck2Brwsr.generate(sb, new Res());
83 ScriptEngineManager sem = new ScriptEngineManager();
84 ScriptEngine mach = sem.getEngineByExtension("js");
87 "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.Console.read);"
88 + "\nfunction initVM() { return vm; };"
91 Object res = mach.eval(sb.toString());
92 if (!(mach instanceof Invocable)) {
93 throw new IOException("It is invocable object: " + res);
95 code = (Invocable) mach;
98 Object vm = code.invokeFunction("initVM");
99 console = code.invokeMethod(vm, "loadClass", Console.class.getName());
103 public void shutdown() throws IOException {
107 public String toString() {
108 return codeSeq.toString();
111 private class Res implements Bck2Brwsr.Resources {
113 public InputStream get(String resource) throws IOException {
114 for (ClassLoader l : loaders) {
116 Enumeration<URL> en = l.getResources(resource);
117 while (en.hasMoreElements()) {
118 u = en.nextElement();
121 return u.openStream();
124 throw new IOException("Can't find " + resource);