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