launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 28 Apr 2013 17:42:49 +0200
branchmodel
changeset 1041 f18b7262fe91
parent 1033 launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java@b8773b7b9ecd
child 1043 bd80952bfd11
permissions -rw-r--r--
FX launcher can start tests
     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.io.Closeable;
    21 import java.io.File;
    22 import java.io.IOException;
    23 import java.io.InputStream;
    24 import java.io.InterruptedIOException;
    25 import java.io.OutputStream;
    26 import java.io.UnsupportedEncodingException;
    27 import java.io.Writer;
    28 import java.net.URI;
    29 import java.net.URISyntaxException;
    30 import java.net.URL;
    31 import java.util.ArrayList;
    32 import java.util.Arrays;
    33 import java.util.Enumeration;
    34 import java.util.LinkedHashSet;
    35 import java.util.List;
    36 import java.util.Set;
    37 import java.util.concurrent.BlockingQueue;
    38 import java.util.concurrent.CountDownLatch;
    39 import java.util.concurrent.LinkedBlockingQueue;
    40 import java.util.concurrent.TimeUnit;
    41 import java.util.logging.Level;
    42 import java.util.logging.Logger;
    43 import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
    44 import org.apidesign.vm4brwsr.Bck2Brwsr;
    45 import org.glassfish.grizzly.PortRange;
    46 import org.glassfish.grizzly.http.server.HttpHandler;
    47 import org.glassfish.grizzly.http.server.HttpServer;
    48 import org.glassfish.grizzly.http.server.NetworkListener;
    49 import org.glassfish.grizzly.http.server.Request;
    50 import org.glassfish.grizzly.http.server.Response;
    51 import org.glassfish.grizzly.http.server.ServerConfiguration;
    52 import org.glassfish.grizzly.http.util.HttpStatus;
    53 
    54 /**
    55  * Lightweight server to launch Bck2Brwsr applications and tests.
    56  * Supports execution in native browser as well as Java's internal 
    57  * execution engine.
    58  */
    59 class BaseHTTPLauncher extends Launcher implements Closeable {
    60     private static final Logger LOG = Logger.getLogger(BaseHTTPLauncher.class.getName());
    61     private static final InvocationContext END = new InvocationContext(null, null, null);
    62     private final Set<ClassLoader> loaders = new LinkedHashSet<>();
    63     private final BlockingQueue<InvocationContext> methods = new LinkedBlockingQueue<>();
    64     private long timeOut;
    65     private final Res resources = new Res();
    66     private final String cmd;
    67     private Object[] brwsr;
    68     private HttpServer server;
    69     private CountDownLatch wait;
    70     
    71     public BaseHTTPLauncher(String cmd) {
    72         this.cmd = cmd;
    73         addClassLoader(Bck2Brwsr.class.getClassLoader());
    74         setTimeout(180000);
    75     }
    76     
    77     @Override
    78     InvocationContext runMethod(InvocationContext c) throws IOException {
    79         loaders.add(c.clazz.getClassLoader());
    80         methods.add(c);
    81         try {
    82             c.await(timeOut);
    83         } catch (InterruptedException ex) {
    84             throw new IOException(ex);
    85         }
    86         return c;
    87     }
    88     
    89     public void setTimeout(long ms) {
    90         timeOut = ms;
    91     }
    92     
    93     public void addClassLoader(ClassLoader url) {
    94         this.loaders.add(url);
    95     }
    96     
    97     ClassLoader[] loaders() {
    98         return loaders.toArray(new ClassLoader[loaders.size()]);
    99     }
   100 
   101     public void showURL(String startpage) throws IOException {
   102         if (!startpage.startsWith("/")) {
   103             startpage = "/" + startpage;
   104         }
   105         HttpServer s = initServer(".", true);
   106         int last = startpage.lastIndexOf('/');
   107         String prefix = startpage.substring(0, last);
   108         String simpleName = startpage.substring(last);
   109         s.getServerConfiguration().addHttpHandler(new SubTree(resources, prefix), "/");
   110         try {
   111             launchServerAndBrwsr(s, simpleName);
   112         } catch (URISyntaxException | InterruptedException ex) {
   113             throw new IOException(ex);
   114         }
   115     }
   116 
   117     void showDirectory(File dir, String startpage) throws IOException {
   118         if (!startpage.startsWith("/")) {
   119             startpage = "/" + startpage;
   120         }
   121         HttpServer s = initServer(dir.getPath(), false);
   122         try {
   123             launchServerAndBrwsr(s, startpage);
   124         } catch (URISyntaxException | InterruptedException ex) {
   125             throw new IOException(ex);
   126         }
   127     }
   128 
   129     @Override
   130     public void initialize() throws IOException {
   131         try {
   132             executeInBrowser();
   133         } catch (InterruptedException ex) {
   134             final InterruptedIOException iio = new InterruptedIOException(ex.getMessage());
   135             iio.initCause(ex);
   136             throw iio;
   137         } catch (Exception ex) {
   138             if (ex instanceof IOException) {
   139                 throw (IOException)ex;
   140             }
   141             if (ex instanceof RuntimeException) {
   142                 throw (RuntimeException)ex;
   143             }
   144             throw new IOException(ex);
   145         }
   146     }
   147     
   148     private HttpServer initServer(String path, boolean addClasses) throws IOException {
   149         HttpServer s = HttpServer.createSimpleServer(path, new PortRange(8080, 65535));
   150 
   151         final ServerConfiguration conf = s.getServerConfiguration();
   152         if (addClasses) {
   153             conf.addHttpHandler(new VM(), "/bck2brwsr.js");
   154             conf.addHttpHandler(new Classes(resources), "/classes/");
   155         }
   156         return s;
   157     }
   158     
   159     private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
   160         wait = new CountDownLatch(1);
   161         server = initServer(".", true);
   162         final ServerConfiguration conf = server.getServerConfiguration();
   163         
   164         class DynamicResourceHandler extends HttpHandler {
   165             private final InvocationContext ic;
   166             public DynamicResourceHandler(InvocationContext ic) {
   167                 if (ic == null || ic.resources.isEmpty()) {
   168                     throw new NullPointerException();
   169                 }
   170                 this.ic = ic;
   171                 for (Resource r : ic.resources) {
   172                     conf.addHttpHandler(this, r.httpPath);
   173                 }
   174             }
   175 
   176             public void close() {
   177                 conf.removeHttpHandler(this);
   178             }
   179             
   180             @Override
   181             public void service(Request request, Response response) throws Exception {
   182                 for (Resource r : ic.resources) {
   183                     if (r.httpPath.equals(request.getRequestURI())) {
   184                         LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
   185                         response.setContentType(r.httpType);
   186                         r.httpContent.reset();
   187                         String[] params = null;
   188                         if (r.parameters.length != 0) {
   189                             params = new String[r.parameters.length];
   190                             for (int i = 0; i < r.parameters.length; i++) {
   191                                 params[i] = request.getParameter(r.parameters[i]);
   192                             }
   193                         }
   194                         
   195                         copyStream(r.httpContent, response.getOutputStream(), null, params);
   196                     }
   197                 }
   198             }
   199         }
   200         
   201         conf.addHttpHandler(new Page(resources, 
   202             "org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml"
   203         ), "/execute");
   204         
   205         conf.addHttpHandler(new HttpHandler() {
   206             int cnt;
   207             List<InvocationContext> cases = new ArrayList<>();
   208             DynamicResourceHandler prev;
   209             @Override
   210             public void service(Request request, Response response) throws Exception {
   211                 String id = request.getParameter("request");
   212                 String value = request.getParameter("result");
   213                 if (value != null && value.indexOf((char)0xC5) != -1) {
   214                     value = toUTF8(value);
   215                 }
   216                 
   217                 
   218                 InvocationContext mi = null;
   219                 int caseNmbr = -1;
   220                 
   221                 if (id != null && value != null) {
   222                     LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value});
   223                     value = decodeURL(value);
   224                     int indx = Integer.parseInt(id);
   225                     cases.get(indx).result(value, null);
   226                     if (++indx < cases.size()) {
   227                         mi = cases.get(indx);
   228                         LOG.log(Level.INFO, "Re-executing case {0}", indx);
   229                         caseNmbr = indx;
   230                     }
   231                 } else {
   232                     if (!cases.isEmpty()) {
   233                         LOG.info("Re-executing test cases");
   234                         mi = cases.get(0);
   235                         caseNmbr = 0;
   236                     }
   237                 }
   238                 
   239                 if (prev != null) {
   240                     prev.close();
   241                     prev = null;
   242                 }
   243                 
   244                 if (mi == null) {
   245                     mi = methods.take();
   246                     caseNmbr = cnt++;
   247                 }
   248                 if (mi == END) {
   249                     response.getWriter().write("");
   250                     wait.countDown();
   251                     cnt = 0;
   252                     LOG.log(Level.INFO, "End of data reached. Exiting.");
   253                     return;
   254                 }
   255                 
   256                 if (!mi.resources.isEmpty()) {
   257                     prev = new DynamicResourceHandler(mi);
   258                 }
   259                 
   260                 cases.add(mi);
   261                 final String cn = mi.clazz.getName();
   262                 final String mn = mi.methodName;
   263                 LOG.log(Level.INFO, "Request for {0} case. Sending {1}.{2}", new Object[]{caseNmbr, cn, mn});
   264                 response.getWriter().write("{"
   265                     + "className: '" + cn + "', "
   266                     + "methodName: '" + mn + "', "
   267                     + "request: " + caseNmbr
   268                 );
   269                 if (mi.html != null) {
   270                     response.getWriter().write(", html: '");
   271                     response.getWriter().write(encodeJSON(mi.html));
   272                     response.getWriter().write("'");
   273                 }
   274                 response.getWriter().write("}");
   275             }
   276         }, "/data");
   277 
   278         this.brwsr = launchServerAndBrwsr(server, "/execute");
   279     }
   280     
   281     private static String encodeJSON(String in) {
   282         StringBuilder sb = new StringBuilder();
   283         for (int i = 0; i < in.length(); i++) {
   284             char ch = in.charAt(i);
   285             if (ch < 32 || ch == '\'' || ch == '"') {
   286                 sb.append("\\u");
   287                 String hs = "0000" + Integer.toHexString(ch);
   288                 hs = hs.substring(hs.length() - 4);
   289                 sb.append(hs);
   290             } else {
   291                 sb.append(ch);
   292             }
   293         }
   294         return sb.toString();
   295     }
   296     
   297     @Override
   298     public void shutdown() throws IOException {
   299         methods.offer(END);
   300         for (;;) {
   301             int prev = methods.size();
   302             try {
   303                 if (wait != null && wait.await(timeOut, TimeUnit.MILLISECONDS)) {
   304                     break;
   305                 }
   306             } catch (InterruptedException ex) {
   307                 throw new IOException(ex);
   308             }
   309             if (prev == methods.size()) {
   310                 LOG.log(
   311                     Level.WARNING, 
   312                     "Timeout and no test has been executed meanwhile (at {0}). Giving up.", 
   313                     methods.size()
   314                 );
   315                 break;
   316             }
   317             LOG.log(Level.INFO, 
   318                 "Timeout, but tests got from {0} to {1}. Trying again.", 
   319                 new Object[]{prev, methods.size()}
   320             );
   321         }
   322         stopServerAndBrwsr(server, brwsr);
   323     }
   324     
   325     static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
   326         for (;;) {
   327             int ch = is.read();
   328             if (ch == -1) {
   329                 break;
   330             }
   331             if (ch == '$' && params.length > 0) {
   332                 int cnt = is.read() - '0';
   333                 if (baseURL != null && cnt == 'U' - '0') {
   334                     os.write(baseURL.getBytes("UTF-8"));
   335                 } else {
   336                     if (cnt >= 0 && cnt < params.length) {
   337                         os.write(params[cnt].getBytes("UTF-8"));
   338                     } else {
   339                         os.write('$');
   340                         os.write(cnt + '0');
   341                     }
   342                 }
   343             } else {
   344                 os.write(ch);
   345             }
   346         }
   347     }
   348 
   349     private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
   350         server.start();
   351         NetworkListener listener = server.getListeners().iterator().next();
   352         int port = listener.getPort();
   353         
   354         URI uri = new URI("http://localhost:" + port + page);
   355         return showBrwsr(uri);
   356     }
   357     private static String toUTF8(String value) throws UnsupportedEncodingException {
   358         byte[] arr = new byte[value.length()];
   359         for (int i = 0; i < arr.length; i++) {
   360             arr[i] = (byte)value.charAt(i);
   361         }
   362         return new String(arr, "UTF-8");
   363     }
   364     
   365     private static String decodeURL(String s) {
   366         for (;;) {
   367             int pos = s.indexOf('%');
   368             if (pos == -1) {
   369                 return s;
   370             }
   371             int i = Integer.parseInt(s.substring(pos + 1, pos + 2), 16);
   372             s = s.substring(0, pos) + (char)i + s.substring(pos + 2);
   373         }
   374     }
   375     
   376     private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException {
   377         if (brwsr == null) {
   378             return;
   379         }
   380         Process process = (Process)brwsr[0];
   381         
   382         server.stop();
   383         InputStream stdout = process.getInputStream();
   384         InputStream stderr = process.getErrorStream();
   385         drain("StdOut", stdout);
   386         drain("StdErr", stderr);
   387         process.destroy();
   388         int res;
   389         try {
   390             res = process.waitFor();
   391         } catch (InterruptedException ex) {
   392             throw new IOException(ex);
   393         }
   394         LOG.log(Level.INFO, "Exit code: {0}", res);
   395 
   396         deleteTree((File)brwsr[1]);
   397     }
   398     
   399     private static void drain(String name, InputStream is) throws IOException {
   400         int av = is.available();
   401         if (av > 0) {
   402             StringBuilder sb = new StringBuilder();
   403             sb.append("v== ").append(name).append(" ==v\n");
   404             while (av-- > 0) {
   405                 sb.append((char)is.read());
   406             }
   407             sb.append("\n^== ").append(name).append(" ==^");
   408             LOG.log(Level.INFO, sb.toString());
   409         }
   410     }
   411 
   412     private void deleteTree(File file) {
   413         if (file == null) {
   414             return;
   415         }
   416         File[] arr = file.listFiles();
   417         if (arr != null) {
   418             for (File s : arr) {
   419                 deleteTree(s);
   420             }
   421         }
   422         file.delete();
   423     }
   424 
   425     @Override
   426     public void close() throws IOException {
   427         shutdown();
   428     }
   429 
   430     protected Object[] showBrwsr(URI uri) throws IOException {
   431         LOG.log(Level.INFO, "Showing {0}", uri);
   432         if (cmd == null) {
   433             try {
   434                 LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[] {
   435                     System.getProperty("java.vm.name"),
   436                     System.getProperty("java.vm.vendor"),
   437                     System.getProperty("java.vm.version"),
   438                 });
   439                 java.awt.Desktop.getDesktop().browse(uri);
   440                 LOG.log(Level.INFO, "Desktop.browse successfully finished");
   441                 return null;
   442             } catch (UnsupportedOperationException ex) {
   443                 LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
   444                 LOG.log(Level.FINE, null, ex);
   445             }
   446         }
   447         {
   448             String cmdName = cmd == null ? "xdg-open" : cmd;
   449             String[] cmdArr = { 
   450                 cmdName, uri.toString()
   451             };
   452             LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
   453             final Process process = Runtime.getRuntime().exec(cmdArr);
   454             return new Object[] { process, null };
   455         }
   456     }
   457 
   458     void generateBck2BrwsrJS(StringBuilder sb, Bck2Brwsr.Resources loader) throws IOException {
   459         Bck2Brwsr.generate(sb, loader);
   460         sb.append(
   461             "(function WrapperVM(global) {"
   462             + "  function ldCls(res) {\n"
   463             + "    var request = new XMLHttpRequest();\n"
   464             + "    request.open('GET', '/classes/' + res, false);\n"
   465             + "    request.send();\n"
   466             + "    if (request.status !== 200) return null;\n"
   467             + "    var arr = eval('(' + request.responseText + ')');\n"
   468             + "    return arr;\n"
   469             + "  }\n"
   470             + "  var prevvm = global.bck2brwsr;\n"
   471             + "  global.bck2brwsr = function() {\n"
   472             + "    var args = Array.prototype.slice.apply(arguments);\n"
   473             + "    args.unshift(ldCls);\n"
   474             + "    return prevvm.apply(null, args);\n"
   475             + "  };\n"
   476             + "})(this);\n");
   477     }
   478 
   479     private class Res implements Bck2Brwsr.Resources {
   480         @Override
   481         public InputStream get(String resource) throws IOException {
   482             for (ClassLoader l : loaders) {
   483                 URL u = null;
   484                 Enumeration<URL> en = l.getResources(resource);
   485                 while (en.hasMoreElements()) {
   486                     u = en.nextElement();
   487                 }
   488                 if (u != null) {
   489                     return u.openStream();
   490                 }
   491             }
   492             throw new IOException("Can't find " + resource);
   493         }
   494     }
   495 
   496     private static class Page extends HttpHandler {
   497         final String resource;
   498         private final String[] args;
   499         private final Res res;
   500         
   501         public Page(Res res, String resource, String... args) {
   502             this.res = res;
   503             this.resource = resource;
   504             this.args = args.length == 0 ? new String[] { "$0" } : args;
   505         }
   506 
   507         @Override
   508         public void service(Request request, Response response) throws Exception {
   509             String r = computePage(request);
   510             if (r.startsWith("/")) {
   511                 r = r.substring(1);
   512             }
   513             String[] replace = {};
   514             if (r.endsWith(".html")) {
   515                 response.setContentType("text/html");
   516                 LOG.info("Content type text/html");
   517                 replace = args;
   518             }
   519             if (r.endsWith(".xhtml")) {
   520                 response.setContentType("application/xhtml+xml");
   521                 LOG.info("Content type application/xhtml+xml");
   522                 replace = args;
   523             }
   524             OutputStream os = response.getOutputStream();
   525             try (InputStream is = res.get(r)) {
   526                 copyStream(is, os, request.getRequestURL().toString(), replace);
   527             } catch (IOException ex) {
   528                 response.setDetailMessage(ex.getLocalizedMessage());
   529                 response.setError();
   530                 response.setStatus(404);
   531             }
   532         }
   533 
   534         protected String computePage(Request request) {
   535             String r = resource;
   536             if (r == null) {
   537                 r = request.getHttpHandlerPath();
   538             }
   539             return r;
   540         }
   541     }
   542     
   543     private static class SubTree extends Page {
   544 
   545         public SubTree(Res res, String resource, String... args) {
   546             super(res, resource, args);
   547         }
   548 
   549         @Override
   550         protected String computePage(Request request) {
   551             return resource + request.getHttpHandlerPath();
   552         }
   553         
   554         
   555     }
   556 
   557     private class VM extends HttpHandler {
   558         @Override
   559         public void service(Request request, Response response) throws Exception {
   560             response.setCharacterEncoding("UTF-8");
   561             response.setContentType("text/javascript");
   562             StringBuilder sb = new StringBuilder();
   563             generateBck2BrwsrJS(sb, BaseHTTPLauncher.this.resources);
   564             response.getWriter().write(sb.toString());
   565         }
   566     }
   567 
   568     private static class Classes extends HttpHandler {
   569         private final Res loader;
   570 
   571         public Classes(Res loader) {
   572             this.loader = loader;
   573         }
   574 
   575         @Override
   576         public void service(Request request, Response response) throws Exception {
   577             String res = request.getHttpHandlerPath();
   578             if (res.startsWith("/")) {
   579                 res = res.substring(1);
   580             }
   581             try (InputStream is = loader.get(res)) {
   582                 response.setContentType("text/javascript");
   583                 Writer w = response.getWriter();
   584                 w.append("[");
   585                 for (int i = 0;; i++) {
   586                     int b = is.read();
   587                     if (b == -1) {
   588                         break;
   589                     }
   590                     if (i > 0) {
   591                         w.append(", ");
   592                     }
   593                     if (i % 20 == 0) {
   594                         w.write("\n");
   595                     }
   596                     if (b > 127) {
   597                         b = b - 256;
   598                     }
   599                     w.append(Integer.toString(b));
   600                 }
   601                 w.append("\n]");
   602             } catch (IOException ex) {
   603                 response.setStatus(HttpStatus.NOT_FOUND_404);
   604                 response.setError();
   605                 response.setDetailMessage(ex.getMessage());
   606             }
   607         }
   608     }
   609 }