launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Dec 2012 11:58:48 +0100
branchlauncher
changeset 342 60f9aad12731
parent 332 6949044415df
child 348 4e9d576780ca
permissions -rw-r--r--
Using URL.getContent to communicate with the launcher and execute two testing methods
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.launcher;
    19 
    20 import java.awt.Desktop;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import java.io.OutputStream;
    24 import java.io.Writer;
    25 import java.net.URI;
    26 import java.net.URL;
    27 import java.util.Enumeration;
    28 import java.util.concurrent.CountDownLatch;
    29 import static org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher.copyStream;
    30 import org.apidesign.vm4brwsr.Bck2Brwsr;
    31 import org.glassfish.grizzly.PortRange;
    32 import org.glassfish.grizzly.http.server.HttpHandler;
    33 import org.glassfish.grizzly.http.server.HttpServer;
    34 import org.glassfish.grizzly.http.server.NetworkListener;
    35 import org.glassfish.grizzly.http.server.Request;
    36 import org.glassfish.grizzly.http.server.Response;
    37 import org.glassfish.grizzly.http.server.ServerConfiguration;
    38 
    39 /**
    40  * Lightweight server to launch Bck2Brwsr applications in real browser.
    41  */
    42 public class Bck2BrwsrLauncher {
    43     public static void main( String[] args ) throws Exception {
    44         final Case[] cases = { 
    45             new Case("org.apidesign.bck2brwsr.launcher.Console", "welcome"),
    46             new Case("org.apidesign.bck2brwsr.launcher.Console", "multiply")
    47         };
    48         final CountDownLatch wait = new CountDownLatch(1);
    49         
    50         HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
    51         final ClassLoader loader = Bck2BrwsrLauncher.class.getClassLoader();
    52         
    53         final ServerConfiguration conf = server.getServerConfiguration();
    54         conf.addHttpHandler(new Page("console.xhtml", 
    55             "org.apidesign.bck2brwsr.launcher.Console", "welcome", "false"
    56         ), "/console");
    57         conf.addHttpHandler(new VM(loader), "/bck2brwsr.js");
    58         conf.addHttpHandler(new VMInit(), "/vm.js");
    59         conf.addHttpHandler(new Classes(loader), "/classes/");
    60         conf.addHttpHandler(new HttpHandler() {
    61             @Override
    62             public void service(Request request, Response response) throws Exception {
    63                 String clazz = request.getParameter("class");
    64                 String method = request.getParameter("method");
    65                 if (clazz == null || method == null) {
    66                     response.setError();
    67                     response.setDetailMessage("Need two parameters: class and method name!");
    68                     return;
    69                 }
    70                 response.setContentType("text/html");
    71                 OutputStream os = response.getOutputStream();
    72                 InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml");
    73                 copyStream(is, os, clazz, method, "true");
    74             }
    75         }, "/");
    76         conf.addHttpHandler(new HttpHandler() {
    77             int cnt;
    78             @Override
    79             public void service(Request request, Response response) throws Exception {
    80                 String id = request.getParameter("request");
    81                 String value = request.getParameter("result");
    82                 if (id != null && value != null) {
    83                     value = value.replace("%20", " ");
    84                     cases[Integer.parseInt(id)].result = value;
    85                 }
    86                 
    87                 if (cnt >= cases.length) {
    88                     response.getWriter().write("");
    89                     wait.countDown();
    90                     cnt = 0;
    91                     return;
    92                 }
    93                 
    94                 response.getWriter().write("{"
    95                     + "className: '" + cases[cnt].className + "', "
    96                     + "methodName: '" + cases[cnt].methodName + "', "
    97                     + "request: " + cnt
    98                     + "}");
    99                 cnt++;
   100             }
   101         }, "/data");
   102         conf.addHttpHandler(new Page("harness.xhtml"), "/execute");
   103         
   104         server.start();
   105         NetworkListener listener = server.getListeners().iterator().next();
   106         int port = listener.getPort();
   107         
   108         URI uri = new URI("http://localhost:" + port + "/execute");
   109         try {
   110             Desktop.getDesktop().browse(uri);
   111         } catch (UnsupportedOperationException ex) {
   112             String[] cmd = { 
   113                 "xdg-open", uri.toString()
   114             };
   115             Runtime.getRuntime().exec(cmd).waitFor();
   116         }
   117         
   118         wait.await();
   119         
   120         for (Case c : cases) {
   121             System.err.println(c.className + "." + c.methodName + " = " + c.result);
   122         }
   123     }
   124     
   125     static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
   126         for (;;) {
   127             int ch = is.read();
   128             if (ch == -1) {
   129                 break;
   130             }
   131             if (ch == '$') {
   132                 int cnt = is.read() - '0';
   133                 if (cnt == 'U' - '0') {
   134                     os.write(baseURL.getBytes());
   135                 }
   136                 if (cnt < params.length) {
   137                     os.write(params[cnt].getBytes());
   138                 }
   139             } else {
   140                 os.write(ch);
   141             }
   142         }
   143     }
   144 
   145     private static class Page extends HttpHandler {
   146         private final String resource;
   147         private final String[] args;
   148         
   149         public Page(String resource, String... args) {
   150             this.resource = resource;
   151             this.args = args;
   152         }
   153 
   154         @Override
   155         public void service(Request request, Response response) throws Exception {
   156             response.setContentType("text/html");
   157             OutputStream os = response.getOutputStream();
   158             InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream(resource);
   159             copyStream(is, os, request.getRequestURL().toString(), args);
   160         }
   161     }
   162 
   163     private static class VM extends HttpHandler {
   164         private final ClassLoader loader;
   165 
   166         public VM(ClassLoader loader) {
   167             this.loader = loader;
   168         }
   169 
   170         @Override
   171         public void service(Request request, Response response) throws Exception {
   172             response.setCharacterEncoding("UTF-8");
   173             response.setContentType("text/javascript");
   174             Bck2Brwsr.generate(response.getWriter(), loader);
   175         }
   176     }
   177     private static class VMInit extends HttpHandler {
   178         public VMInit() {
   179         }
   180 
   181         @Override
   182         public void service(Request request, Response response) throws Exception {
   183             response.setCharacterEncoding("UTF-8");
   184             response.setContentType("text/javascript");
   185             response.getWriter().append(
   186                 "function ldCls(res) {\n"
   187                 + "  var request = new XMLHttpRequest();\n"
   188                 + "  request.open('GET', 'classes/' + res, false);\n"
   189                 + "  request.send();\n"
   190                 + "  var arr = eval('(' + request.responseText + ')');\n"
   191                 + "  return arr;\n"
   192                 + "}\n"
   193                 + "var vm = new bck2brwsr(ldCls);\n");
   194         }
   195     }
   196 
   197     private static class Classes extends HttpHandler {
   198         private final ClassLoader loader;
   199 
   200         public Classes(ClassLoader loader) {
   201             this.loader = loader;
   202         }
   203 
   204         @Override
   205         public void service(Request request, Response response) throws Exception {
   206             String res = request.getHttpHandlerPath();
   207             if (res.startsWith("/")) {
   208                 res = res.substring(1);
   209             }
   210             Enumeration<URL> en = loader.getResources(res);
   211             URL u = null;
   212             while (en.hasMoreElements()) {
   213                 u = en.nextElement();
   214             }
   215             if (u == null) {
   216                 response.setError();
   217                 response.setDetailMessage("Can't find resource " + res);
   218             }
   219             response.setContentType("text/javascript");
   220             InputStream is = u.openStream();
   221             Writer w = response.getWriter();
   222             w.append("[");
   223             for (int i = 0;; i++) {
   224                 int b = is.read();
   225                 if (b == -1) {
   226                     break;
   227                 }
   228                 if (i > 0) {
   229                     w.append(", ");
   230                 }
   231                 if (i % 20 == 0) {
   232                     w.write("\n");
   233                 }
   234                 if (b > 127) {
   235                     b = b - 256;
   236                 }
   237                 w.append(Integer.toString(b));
   238             }
   239             w.append("\n]");
   240         }
   241     }
   242     
   243     private static final class Case {
   244         final String className;
   245         final String methodName;
   246         String result;
   247 
   248         public Case(String className, String methodName) {
   249             this.className = className;
   250             this.methodName = methodName;
   251         }
   252     }
   253 }