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