1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Tue Dec 18 13:10:56 2012 +0100
1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Thu Dec 20 11:03:34 2012 +0100
1.3 @@ -20,6 +20,7 @@
1.4 import java.awt.Desktop;
1.5 import java.io.IOException;
1.6 import java.io.InputStream;
1.7 +import java.io.InterruptedIOException;
1.8 import java.io.OutputStream;
1.9 import java.io.Writer;
1.10 import java.net.URI;
1.11 @@ -32,6 +33,12 @@
1.12 import java.util.Set;
1.13 import java.util.concurrent.CountDownLatch;
1.14 import java.util.concurrent.TimeUnit;
1.15 +import java.util.logging.Level;
1.16 +import java.util.logging.Logger;
1.17 +import javax.script.Invocable;
1.18 +import javax.script.ScriptEngine;
1.19 +import javax.script.ScriptEngineManager;
1.20 +import javax.script.ScriptException;
1.21 import static org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher.copyStream;
1.22 import org.apidesign.vm4brwsr.Bck2Brwsr;
1.23 import org.glassfish.grizzly.PortRange;
1.24 @@ -49,6 +56,7 @@
1.25 private Set<ClassLoader> loaders = new LinkedHashSet<>();
1.26 private List<MethodInvocation> methods = new ArrayList<>();
1.27 private long timeOut;
1.28 + private String sen;
1.29
1.30
1.31 public MethodInvocation addMethod(Class<?> clazz, String method) {
1.32 @@ -62,6 +70,10 @@
1.33 timeOut = ms;
1.34 }
1.35
1.36 + public void setScriptEngineName(String sen) {
1.37 + this.sen = sen;
1.38 + }
1.39 +
1.40 public static void main( String[] args ) throws Exception {
1.41 Bck2BrwsrLauncher l = new Bck2BrwsrLauncher();
1.42
1.43 @@ -78,7 +90,59 @@
1.44 }
1.45
1.46
1.47 - public void execute() throws URISyntaxException, IOException, InterruptedException {
1.48 + public void execute() throws IOException {
1.49 + try {
1.50 + if (sen != null) {
1.51 + executeRhino();
1.52 + } else {
1.53 + executeInBrowser();
1.54 + }
1.55 + } catch (InterruptedException ex) {
1.56 + final InterruptedIOException iio = new InterruptedIOException(ex.getMessage());
1.57 + iio.initCause(ex);
1.58 + throw iio;
1.59 + } catch (Exception ex) {
1.60 + if (ex instanceof IOException) {
1.61 + throw (IOException)ex;
1.62 + }
1.63 + if (ex instanceof RuntimeException) {
1.64 + throw (RuntimeException)ex;
1.65 + }
1.66 + throw new IOException(ex);
1.67 + }
1.68 + }
1.69 +
1.70 + private void executeRhino() throws IOException, ScriptException, NoSuchMethodException {
1.71 + StringBuilder sb = new StringBuilder();
1.72 + Bck2Brwsr.generate(sb, new Res());
1.73 +
1.74 + ScriptEngineManager sem = new ScriptEngineManager();
1.75 + ScriptEngine mach = sem.getEngineByExtension(sen);
1.76 +
1.77 + sb.append(
1.78 + "\nvar vm = bck2brwsr(org.apidesign.bck2brwsr.vmtest.VMTest.read);"
1.79 + + "\nfunction initVM() { return vm; };"
1.80 + + "\n");
1.81 +
1.82 + Object res = mach.eval(sb.toString());
1.83 + if (!(mach instanceof Invocable)) {
1.84 + throw new IOException("It is invocable object: " + res);
1.85 + }
1.86 + Invocable code = (Invocable) mach;
1.87 +
1.88 + Object vm = code.invokeFunction("initVM");
1.89 + Object console = code.invokeMethod(vm, "loadClass", Console.class.getName());
1.90 +
1.91 + final MethodInvocation[] cases = this.methods.toArray(new MethodInvocation[0]);
1.92 + for (MethodInvocation mi : cases) {
1.93 + mi.result = code.invokeMethod(console,
1.94 + "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
1.95 + mi.className, mi.methodName
1.96 + ).toString();
1.97 + }
1.98 + }
1.99 +
1.100 + private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
1.101 final CountDownLatch wait = new CountDownLatch(1);
1.102 final MethodInvocation[] cases = this.methods.toArray(new MethodInvocation[0]);
1.103
1.104 @@ -158,7 +222,7 @@
1.105 }
1.106 }
1.107 }
1.108 -
1.109 +
1.110 private class Res implements Bck2Brwsr.Resources {
1.111 @Override
1.112 public InputStream get(String resource) throws IOException {