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