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