JavaScript and HTTP launchers separated launcher
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 23 Dec 2012 17:02:34 +0100
branchlauncher
changeset 370ed48023d1d85
parent 369 723d854272ae
child 371 bafc670aa10d
JavaScript and HTTP launchers separated
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/MethodInvocation.java
vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java
vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Sun Dec 23 09:17:26 2012 +0100
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Sun Dec 23 17:02:34 2012 +0100
     1.3 @@ -17,7 +17,6 @@
     1.4   */
     1.5  package org.apidesign.bck2brwsr.launcher;
     1.6  
     1.7 -import java.awt.Desktop;
     1.8  import java.io.File;
     1.9  import java.io.IOException;
    1.10  import java.io.InputStream;
    1.11 @@ -27,7 +26,6 @@
    1.12  import java.net.URI;
    1.13  import java.net.URISyntaxException;
    1.14  import java.net.URL;
    1.15 -import java.nio.file.Files;
    1.16  import java.util.ArrayList;
    1.17  import java.util.Arrays;
    1.18  import java.util.Enumeration;
    1.19 @@ -38,11 +36,6 @@
    1.20  import java.util.concurrent.TimeUnit;
    1.21  import java.util.logging.Level;
    1.22  import java.util.logging.Logger;
    1.23 -import javax.script.Invocable;
    1.24 -import javax.script.ScriptEngine;
    1.25 -import javax.script.ScriptEngineManager;
    1.26 -import javax.script.ScriptException;
    1.27 -import static org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher.copyStream;
    1.28  import org.apidesign.vm4brwsr.Bck2Brwsr;
    1.29  import org.glassfish.grizzly.PortRange;
    1.30  import org.glassfish.grizzly.http.server.HttpHandler;
    1.31 @@ -62,7 +55,6 @@
    1.32      private Set<ClassLoader> loaders = new LinkedHashSet<>();
    1.33      private List<MethodInvocation> methods = new ArrayList<>();
    1.34      private long timeOut;
    1.35 -    private String sen;
    1.36      private String showURL;
    1.37      private final Res resources = new Res();
    1.38      
    1.39 @@ -78,10 +70,6 @@
    1.40          timeOut = ms;
    1.41      }
    1.42      
    1.43 -    public void setScriptEngineName(String sen) {
    1.44 -        this.sen = sen;
    1.45 -    }
    1.46 -
    1.47      public void setStartPage(String startpage) {
    1.48          if (!startpage.startsWith("/")) {
    1.49              startpage = "/" + startpage;
    1.50 @@ -104,9 +92,7 @@
    1.51  
    1.52      public void execute() throws IOException {
    1.53          try {
    1.54 -            if (sen != null) {
    1.55 -                executeRhino();
    1.56 -            } else if (showURL != null) {
    1.57 +            if (showURL != null) {
    1.58                  HttpServer server = initServer();
    1.59                  server.getServerConfiguration().addHttpHandler(new Page(resources, null), "/");
    1.60                  launchServerAndBrwsr(server, showURL);
    1.61 @@ -128,36 +114,6 @@
    1.62          }
    1.63      }
    1.64      
    1.65 -    private void executeRhino() throws IOException, ScriptException, NoSuchMethodException {
    1.66 -        StringBuilder sb = new StringBuilder();
    1.67 -        Bck2Brwsr.generate(sb, new Res());
    1.68 -
    1.69 -        ScriptEngineManager sem = new ScriptEngineManager();
    1.70 -        ScriptEngine mach = sem.getEngineByExtension(sen);
    1.71 -
    1.72 -        sb.append(
    1.73 -              "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.Console.read);"
    1.74 -            + "\nfunction initVM() { return vm; };"
    1.75 -            + "\n");
    1.76 -
    1.77 -        Object res = mach.eval(sb.toString());
    1.78 -        if (!(mach instanceof Invocable)) {
    1.79 -            throw new IOException("It is invocable object: " + res);
    1.80 -        }
    1.81 -        Invocable code = (Invocable) mach;
    1.82 -        
    1.83 -        Object vm = code.invokeFunction("initVM");
    1.84 -        Object console = code.invokeMethod(vm, "loadClass", Console.class.getName());
    1.85 -
    1.86 -        final MethodInvocation[] cases = this.methods.toArray(new MethodInvocation[0]);
    1.87 -        for (MethodInvocation mi : cases) {
    1.88 -            mi.result = code.invokeMethod(console, 
    1.89 -                "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2", 
    1.90 -                mi.className, mi.methodName
    1.91 -            ).toString();
    1.92 -        }
    1.93 -    }
    1.94 -    
    1.95      private HttpServer initServer() {
    1.96          HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
    1.97  
    1.98 @@ -456,20 +412,4 @@
    1.99              }
   1.100          }
   1.101      }
   1.102 -    
   1.103 -    public static final class MethodInvocation {
   1.104 -        final String className;
   1.105 -        final String methodName;
   1.106 -        String result;
   1.107 -
   1.108 -        MethodInvocation(String className, String methodName) {
   1.109 -            this.className = className;
   1.110 -            this.methodName = methodName;
   1.111 -        }
   1.112 -
   1.113 -        @Override
   1.114 -        public String toString() {
   1.115 -            return result;
   1.116 -        }
   1.117 -    }
   1.118  }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java	Sun Dec 23 17:02:34 2012 +0100
     2.3 @@ -0,0 +1,116 @@
     2.4 +/**
     2.5 + * Back 2 Browser Bytecode Translator
     2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program. Look for COPYING file in the top folder.
    2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    2.20 + */
    2.21 +package org.apidesign.bck2brwsr.launcher;
    2.22 +
    2.23 +import java.io.IOException;
    2.24 +import java.io.InputStream;
    2.25 +import java.net.URL;
    2.26 +import java.util.Enumeration;
    2.27 +import java.util.LinkedHashSet;
    2.28 +import java.util.Set;
    2.29 +import java.util.logging.Logger;
    2.30 +import javax.script.Invocable;
    2.31 +import javax.script.ScriptEngine;
    2.32 +import javax.script.ScriptEngineManager;
    2.33 +import javax.script.ScriptException;
    2.34 +import org.apidesign.vm4brwsr.Bck2Brwsr;
    2.35 +
    2.36 +/**
    2.37 + * Tests execution in Java's internal scripting engine.
    2.38 + */
    2.39 +public final class JSLauncher {
    2.40 +    private static final Logger LOG = Logger.getLogger(JSLauncher.class.getName());
    2.41 +    private Set<ClassLoader> loaders = new LinkedHashSet<>();
    2.42 +    private final Res resources = new Res();
    2.43 +    private Invocable code;
    2.44 +    private Object console;
    2.45 +    
    2.46 +    
    2.47 +    public MethodInvocation addMethod(Class<?> clazz, String method) {
    2.48 +        loaders.add(clazz.getClassLoader());
    2.49 +        MethodInvocation mi = new MethodInvocation(clazz.getName(), method);
    2.50 +        try {
    2.51 +            mi.result = code.invokeMethod(
    2.52 +                console,
    2.53 +                "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
    2.54 +                mi.className, mi.methodName).toString();
    2.55 +        } catch (ScriptException scriptException) {
    2.56 +            mi.exception = scriptException;
    2.57 +        } catch (NoSuchMethodException noSuchMethodException) {
    2.58 +            mi.exception = noSuchMethodException;
    2.59 +        }
    2.60 +        return mi;
    2.61 +    }
    2.62 +    
    2.63 +    public void addClassLoader(ClassLoader url) {
    2.64 +        this.loaders.add(url);
    2.65 +    }
    2.66 +
    2.67 +    public void initialize() throws IOException {
    2.68 +        try {
    2.69 +            initRhino();
    2.70 +        } catch (Exception ex) {
    2.71 +            if (ex instanceof IOException) {
    2.72 +                throw (IOException)ex;
    2.73 +            }
    2.74 +            if (ex instanceof RuntimeException) {
    2.75 +                throw (RuntimeException)ex;
    2.76 +            }
    2.77 +            throw new IOException(ex);
    2.78 +        }
    2.79 +    }
    2.80 +    
    2.81 +    private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
    2.82 +        StringBuilder sb = new StringBuilder();
    2.83 +        Bck2Brwsr.generate(sb, new Res());
    2.84 +
    2.85 +        ScriptEngineManager sem = new ScriptEngineManager();
    2.86 +        ScriptEngine mach = sem.getEngineByExtension("js");
    2.87 +
    2.88 +        sb.append(
    2.89 +              "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.Console.read);"
    2.90 +            + "\nfunction initVM() { return vm; };"
    2.91 +            + "\n");
    2.92 +
    2.93 +        Object res = mach.eval(sb.toString());
    2.94 +        if (!(mach instanceof Invocable)) {
    2.95 +            throw new IOException("It is invocable object: " + res);
    2.96 +        }
    2.97 +        code = (Invocable) mach;
    2.98 +        
    2.99 +        Object vm = code.invokeFunction("initVM");
   2.100 +        console = code.invokeMethod(vm, "loadClass", Console.class.getName());
   2.101 +    }
   2.102 +    
   2.103 +    private class Res implements Bck2Brwsr.Resources {
   2.104 +        @Override
   2.105 +        public InputStream get(String resource) throws IOException {
   2.106 +            for (ClassLoader l : loaders) {
   2.107 +                URL u = null;
   2.108 +                Enumeration<URL> en = l.getResources(resource);
   2.109 +                while (en.hasMoreElements()) {
   2.110 +                    u = en.nextElement();
   2.111 +                }
   2.112 +                if (u != null) {
   2.113 +                    return u.openStream();
   2.114 +                }
   2.115 +            }
   2.116 +            throw new IOException("Can't find " + resource);
   2.117 +        }
   2.118 +    }
   2.119 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/MethodInvocation.java	Sun Dec 23 17:02:34 2012 +0100
     3.3 @@ -0,0 +1,43 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.bck2brwsr.launcher;
    3.22 +
    3.23 +/**
    3.24 + *
    3.25 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.26 + */
    3.27 +public final class MethodInvocation {
    3.28 +    final String className;
    3.29 +    final String methodName;
    3.30 +    String result;
    3.31 +    Exception exception;
    3.32 +
    3.33 +    MethodInvocation(String className, String methodName) {
    3.34 +        this.className = className;
    3.35 +        this.methodName = methodName;
    3.36 +    }
    3.37 +
    3.38 +    @Override
    3.39 +    public String toString() {
    3.40 +        if (exception != null) {
    3.41 +            return exception.toString();
    3.42 +        }
    3.43 +        return result;
    3.44 +    }
    3.45 +    
    3.46 +}
     4.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java	Sun Dec 23 09:17:26 2012 +0100
     4.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java	Sun Dec 23 17:02:34 2012 +0100
     4.3 @@ -17,7 +17,10 @@
     4.4   */
     4.5  package org.apidesign.bck2brwsr.vmtest;
     4.6  
     4.7 +import java.io.IOException;
     4.8  import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
     4.9 +import org.apidesign.bck2brwsr.launcher.JSLauncher;
    4.10 +import org.apidesign.bck2brwsr.launcher.MethodInvocation;
    4.11  
    4.12  /**
    4.13   *
    4.14 @@ -25,7 +28,7 @@
    4.15   */
    4.16  final class Launcher {
    4.17      private final String sen;
    4.18 -    private Bck2BrwsrLauncher launcher;
    4.19 +    private Object launcher;
    4.20      
    4.21      Launcher() {
    4.22          this(null);
    4.23 @@ -34,27 +37,36 @@
    4.24          this.sen = sen;
    4.25      }
    4.26  
    4.27 -    synchronized Bck2BrwsrLauncher clear() {
    4.28 -        Bck2BrwsrLauncher l = launcher;
    4.29 +    synchronized Object clear() {
    4.30 +        Object l = launcher;
    4.31          launcher = null;
    4.32          return l;
    4.33      }
    4.34  
    4.35 -    synchronized Bck2BrwsrLauncher.MethodInvocation addMethod(Class<?> clazz, String name) {
    4.36 +    synchronized MethodInvocation addMethod(Class<?> clazz, String name) throws IOException {
    4.37          if (launcher == null) {
    4.38 -            launcher = new Bck2BrwsrLauncher();
    4.39 -            launcher.setTimeout(180000);
    4.40              if (sen != null) {
    4.41 -                launcher.setScriptEngineName(sen);
    4.42 +                JSLauncher js = new JSLauncher();
    4.43 +                js.addClassLoader(clazz.getClassLoader());
    4.44 +                js.initialize();
    4.45 +                launcher = js;
    4.46 +            } else {
    4.47 +                Bck2BrwsrLauncher l = new Bck2BrwsrLauncher();
    4.48 +                l.setTimeout(180000);
    4.49 +                launcher = l;
    4.50              }
    4.51          }
    4.52 -        return launcher.addMethod(clazz, name);
    4.53 +        if (launcher instanceof JSLauncher) {
    4.54 +            return ((JSLauncher)launcher).addMethod(clazz, name);
    4.55 +        } else {
    4.56 +            return ((Bck2BrwsrLauncher)launcher).addMethod(clazz, name);
    4.57 +        }
    4.58      }
    4.59  
    4.60      void exec() throws Exception {
    4.61 -        Bck2BrwsrLauncher l = clear();
    4.62 -        if (l != null) {
    4.63 -            l.execute();
    4.64 +        Object l = clear();
    4.65 +        if (l instanceof Bck2BrwsrLauncher) {
    4.66 +            ((Bck2BrwsrLauncher)l).execute();
    4.67          }
    4.68      }
    4.69      
     5.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java	Sun Dec 23 09:17:26 2012 +0100
     5.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java	Sun Dec 23 17:02:34 2012 +0100
     5.3 @@ -26,7 +26,7 @@
     5.4  import java.util.logging.Level;
     5.5  import java.util.logging.Logger;
     5.6  import javax.script.Invocable;
     5.7 -import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
     5.8 +import org.apidesign.bck2brwsr.launcher.MethodInvocation;
     5.9  import org.testng.Assert;
    5.10  import org.testng.ITest;
    5.11  import org.testng.annotations.Factory;
    5.12 @@ -138,11 +138,11 @@
    5.13  
    5.14          @Test(groups = "run") public void executeCode() throws Throwable {
    5.15              if (type == 1) {
    5.16 -                Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst;
    5.17 +                MethodInvocation c = (MethodInvocation) inst;
    5.18                  JS.exec();
    5.19                  value = c.toString();
    5.20              } else if (type == 2) {
    5.21 -                Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst;
    5.22 +                MethodInvocation c = (MethodInvocation) inst;
    5.23                  BROWSER.exec();
    5.24                  value = c.toString();
    5.25              } else {