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