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;
45 @Override MethodInvocation addMethod(Class<?> clazz, String method, String html) {
46 loaders.add(clazz.getClassLoader());
47 MethodInvocation mi = new MethodInvocation(clazz.getName(), method, html);
49 mi.result(code.invokeMethod(
51 "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
52 mi.className, mi.methodName).toString(), null);
53 } catch (ScriptException | NoSuchMethodException ex) {
59 public void addClassLoader(ClassLoader url) {
60 this.loaders.add(url);
64 public void initialize() throws IOException {
67 } catch (Exception ex) {
68 if (ex instanceof IOException) {
69 throw (IOException)ex;
71 if (ex instanceof RuntimeException) {
72 throw (RuntimeException)ex;
74 throw new IOException(ex);
78 private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
79 StringBuilder sb = new StringBuilder();
80 Bck2Brwsr.generate(sb, new Res());
82 ScriptEngineManager sem = new ScriptEngineManager();
83 ScriptEngine mach = sem.getEngineByExtension("js");
86 "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.Console.read);"
87 + "\nfunction initVM() { return vm; };"
90 Object res = mach.eval(sb.toString());
91 if (!(mach instanceof Invocable)) {
92 throw new IOException("It is invocable object: " + res);
94 code = (Invocable) mach;
97 Object vm = code.invokeFunction("initVM");
98 console = code.invokeMethod(vm, "loadClass", Console.class.getName());
102 public void shutdown() throws IOException {
106 public String toString() {
107 return codeSeq.toString();
110 private class Res implements Bck2Brwsr.Resources {
112 public InputStream get(String resource) throws IOException {
113 for (ClassLoader l : loaders) {
115 Enumeration<URL> en = l.getResources(resource);
116 while (en.hasMoreElements()) {
117 u = en.nextElement();
120 return u.openStream();
123 throw new IOException("Can't find " + resource);