# HG changeset patch # User Jaroslav Tulach # Date 1356278554 -3600 # Node ID ed48023d1d85ad2051c6cf4d97539715c10790e0 # Parent 723d854272aecdfa6913d2357d6bd5f77390241f JavaScript and HTTP launchers separated diff -r 723d854272ae -r ed48023d1d85 launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Sun Dec 23 09:17:26 2012 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Sun Dec 23 17:02:34 2012 +0100 @@ -17,7 +17,6 @@ */ package org.apidesign.bck2brwsr.launcher; -import java.awt.Desktop; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -27,7 +26,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; -import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Enumeration; @@ -38,11 +36,6 @@ import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; -import javax.script.Invocable; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.ScriptException; -import static org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher.copyStream; import org.apidesign.vm4brwsr.Bck2Brwsr; import org.glassfish.grizzly.PortRange; import org.glassfish.grizzly.http.server.HttpHandler; @@ -62,7 +55,6 @@ private Set loaders = new LinkedHashSet<>(); private List methods = new ArrayList<>(); private long timeOut; - private String sen; private String showURL; private final Res resources = new Res(); @@ -78,10 +70,6 @@ timeOut = ms; } - public void setScriptEngineName(String sen) { - this.sen = sen; - } - public void setStartPage(String startpage) { if (!startpage.startsWith("/")) { startpage = "/" + startpage; @@ -104,9 +92,7 @@ public void execute() throws IOException { try { - if (sen != null) { - executeRhino(); - } else if (showURL != null) { + if (showURL != null) { HttpServer server = initServer(); server.getServerConfiguration().addHttpHandler(new Page(resources, null), "/"); launchServerAndBrwsr(server, showURL); @@ -128,36 +114,6 @@ } } - private void executeRhino() throws IOException, ScriptException, NoSuchMethodException { - StringBuilder sb = new StringBuilder(); - Bck2Brwsr.generate(sb, new Res()); - - ScriptEngineManager sem = new ScriptEngineManager(); - ScriptEngine mach = sem.getEngineByExtension(sen); - - sb.append( - "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.Console.read);" - + "\nfunction initVM() { return vm; };" - + "\n"); - - Object res = mach.eval(sb.toString()); - if (!(mach instanceof Invocable)) { - throw new IOException("It is invocable object: " + res); - } - Invocable code = (Invocable) mach; - - Object vm = code.invokeFunction("initVM"); - Object console = code.invokeMethod(vm, "loadClass", Console.class.getName()); - - final MethodInvocation[] cases = this.methods.toArray(new MethodInvocation[0]); - for (MethodInvocation mi : cases) { - mi.result = code.invokeMethod(console, - "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2", - mi.className, mi.methodName - ).toString(); - } - } - private HttpServer initServer() { HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535)); @@ -456,20 +412,4 @@ } } } - - public static final class MethodInvocation { - final String className; - final String methodName; - String result; - - MethodInvocation(String className, String methodName) { - this.className = className; - this.methodName = methodName; - } - - @Override - public String toString() { - return result; - } - } } diff -r 723d854272ae -r ed48023d1d85 launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java Sun Dec 23 17:02:34 2012 +0100 @@ -0,0 +1,116 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.launcher; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Enumeration; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.logging.Logger; +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import org.apidesign.vm4brwsr.Bck2Brwsr; + +/** + * Tests execution in Java's internal scripting engine. + */ +public final class JSLauncher { + private static final Logger LOG = Logger.getLogger(JSLauncher.class.getName()); + private Set loaders = new LinkedHashSet<>(); + private final Res resources = new Res(); + private Invocable code; + private Object console; + + + public MethodInvocation addMethod(Class clazz, String method) { + loaders.add(clazz.getClassLoader()); + MethodInvocation mi = new MethodInvocation(clazz.getName(), method); + try { + mi.result = code.invokeMethod( + console, + "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2", + mi.className, mi.methodName).toString(); + } catch (ScriptException scriptException) { + mi.exception = scriptException; + } catch (NoSuchMethodException noSuchMethodException) { + mi.exception = noSuchMethodException; + } + return mi; + } + + public void addClassLoader(ClassLoader url) { + this.loaders.add(url); + } + + public void initialize() throws IOException { + try { + initRhino(); + } catch (Exception ex) { + if (ex instanceof IOException) { + throw (IOException)ex; + } + if (ex instanceof RuntimeException) { + throw (RuntimeException)ex; + } + throw new IOException(ex); + } + } + + private void initRhino() throws IOException, ScriptException, NoSuchMethodException { + StringBuilder sb = new StringBuilder(); + Bck2Brwsr.generate(sb, new Res()); + + ScriptEngineManager sem = new ScriptEngineManager(); + ScriptEngine mach = sem.getEngineByExtension("js"); + + sb.append( + "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.Console.read);" + + "\nfunction initVM() { return vm; };" + + "\n"); + + Object res = mach.eval(sb.toString()); + if (!(mach instanceof Invocable)) { + throw new IOException("It is invocable object: " + res); + } + code = (Invocable) mach; + + Object vm = code.invokeFunction("initVM"); + console = code.invokeMethod(vm, "loadClass", Console.class.getName()); + } + + private class Res implements Bck2Brwsr.Resources { + @Override + public InputStream get(String resource) throws IOException { + for (ClassLoader l : loaders) { + URL u = null; + Enumeration en = l.getResources(resource); + while (en.hasMoreElements()) { + u = en.nextElement(); + } + if (u != null) { + return u.openStream(); + } + } + throw new IOException("Can't find " + resource); + } + } +} diff -r 723d854272ae -r ed48023d1d85 launcher/src/main/java/org/apidesign/bck2brwsr/launcher/MethodInvocation.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/MethodInvocation.java Sun Dec 23 17:02:34 2012 +0100 @@ -0,0 +1,43 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.launcher; + +/** + * + * @author Jaroslav Tulach + */ +public final class MethodInvocation { + final String className; + final String methodName; + String result; + Exception exception; + + MethodInvocation(String className, String methodName) { + this.className = className; + this.methodName = methodName; + } + + @Override + public String toString() { + if (exception != null) { + return exception.toString(); + } + return result; + } + +} diff -r 723d854272ae -r ed48023d1d85 vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java Sun Dec 23 09:17:26 2012 +0100 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java Sun Dec 23 17:02:34 2012 +0100 @@ -17,7 +17,10 @@ */ package org.apidesign.bck2brwsr.vmtest; +import java.io.IOException; import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher; +import org.apidesign.bck2brwsr.launcher.JSLauncher; +import org.apidesign.bck2brwsr.launcher.MethodInvocation; /** * @@ -25,7 +28,7 @@ */ final class Launcher { private final String sen; - private Bck2BrwsrLauncher launcher; + private Object launcher; Launcher() { this(null); @@ -34,27 +37,36 @@ this.sen = sen; } - synchronized Bck2BrwsrLauncher clear() { - Bck2BrwsrLauncher l = launcher; + synchronized Object clear() { + Object l = launcher; launcher = null; return l; } - synchronized Bck2BrwsrLauncher.MethodInvocation addMethod(Class clazz, String name) { + synchronized MethodInvocation addMethod(Class clazz, String name) throws IOException { if (launcher == null) { - launcher = new Bck2BrwsrLauncher(); - launcher.setTimeout(180000); if (sen != null) { - launcher.setScriptEngineName(sen); + JSLauncher js = new JSLauncher(); + js.addClassLoader(clazz.getClassLoader()); + js.initialize(); + launcher = js; + } else { + Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(); + l.setTimeout(180000); + launcher = l; } } - return launcher.addMethod(clazz, name); + if (launcher instanceof JSLauncher) { + return ((JSLauncher)launcher).addMethod(clazz, name); + } else { + return ((Bck2BrwsrLauncher)launcher).addMethod(clazz, name); + } } void exec() throws Exception { - Bck2BrwsrLauncher l = clear(); - if (l != null) { - l.execute(); + Object l = clear(); + if (l instanceof Bck2BrwsrLauncher) { + ((Bck2BrwsrLauncher)l).execute(); } } diff -r 723d854272ae -r ed48023d1d85 vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Sun Dec 23 09:17:26 2012 +0100 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Sun Dec 23 17:02:34 2012 +0100 @@ -26,7 +26,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.script.Invocable; -import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher; +import org.apidesign.bck2brwsr.launcher.MethodInvocation; import org.testng.Assert; import org.testng.ITest; import org.testng.annotations.Factory; @@ -138,11 +138,11 @@ @Test(groups = "run") public void executeCode() throws Throwable { if (type == 1) { - Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst; + MethodInvocation c = (MethodInvocation) inst; JS.exec(); value = c.toString(); } else if (type == 2) { - Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst; + MethodInvocation c = (MethodInvocation) inst; BROWSER.exec(); value = c.toString(); } else {