launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 May 2015 22:47:26 +0200
changeset 1834 d4433058bf0c
parent 1805 18ae4fbcfb87
child 1850 23babc7d5bb8
permissions -rw-r--r--
Removing spaces at the end of lines and providing support for http.header substitution
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.ByteArrayInputStream;
    21 import java.io.ByteArrayOutputStream;
    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.JarURLConnection;
    32 import java.net.URI;
    33 import java.net.URISyntaxException;
    34 import java.net.URL;
    35 import java.net.URLConnection;
    36 import java.util.ArrayList;
    37 import java.util.Arrays;
    38 import java.util.Enumeration;
    39 import java.util.HashSet;
    40 import java.util.LinkedHashSet;
    41 import java.util.List;
    42 import java.util.Map;
    43 import java.util.Properties;
    44 import java.util.Set;
    45 import java.util.concurrent.BlockingQueue;
    46 import java.util.concurrent.Callable;
    47 import java.util.concurrent.CountDownLatch;
    48 import java.util.concurrent.LinkedBlockingQueue;
    49 import java.util.concurrent.TimeUnit;
    50 import java.util.jar.Attributes;
    51 import java.util.jar.Manifest;
    52 import java.util.logging.ConsoleHandler;
    53 import java.util.logging.Level;
    54 import java.util.logging.Logger;
    55 import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
    56 import org.glassfish.grizzly.PortRange;
    57 import org.glassfish.grizzly.http.server.HttpHandler;
    58 import org.glassfish.grizzly.http.server.HttpServer;
    59 import org.glassfish.grizzly.http.server.NetworkListener;
    60 import org.glassfish.grizzly.http.server.Request;
    61 import org.glassfish.grizzly.http.server.Response;
    62 import org.glassfish.grizzly.http.server.ServerConfiguration;
    63 import org.glassfish.grizzly.http.server.StaticHttpHandler;
    64 import org.glassfish.grizzly.http.util.Header;
    65 import org.glassfish.grizzly.http.util.HttpStatus;
    66 import org.glassfish.grizzly.websockets.WebSocket;
    67 import org.glassfish.grizzly.websockets.WebSocketAddOn;
    68 import org.glassfish.grizzly.websockets.WebSocketApplication;
    69 import org.glassfish.grizzly.websockets.WebSocketEngine;
    70 
    71 /**
    72  * Lightweight server to launch Bck2Brwsr applications and tests.
    73  * Supports execution in native browser as well as Java's internal
    74  * execution engine.
    75  */
    76 abstract class BaseHTTPLauncher extends Launcher implements Closeable, Callable<HttpServer> {
    77     static final Logger LOG = Logger.getLogger(BaseHTTPLauncher.class.getName());
    78     private static final InvocationContext END = new InvocationContext(null, null, null);
    79     private final Set<ClassLoader> loaders = new LinkedHashSet<ClassLoader>();
    80     private final BlockingQueue<InvocationContext> methods = new LinkedBlockingQueue<InvocationContext>();
    81     private long timeOut;
    82     private final Res resources = new Res();
    83     private final String cmd;
    84     private Object[] brwsr;
    85     private HttpServer server;
    86     private CountDownLatch wait;
    87 
    88     public BaseHTTPLauncher(String cmd) {
    89         this.cmd = cmd;
    90         addClassLoader(BaseHTTPLauncher.class.getClassLoader());
    91         setTimeout(180000);
    92     }
    93 
    94     @Override
    95     InvocationContext runMethod(InvocationContext c) throws IOException {
    96         loaders.add(c.clazz.getClassLoader());
    97         methods.add(c);
    98         try {
    99             c.await(timeOut);
   100         } catch (InterruptedException ex) {
   101             throw new IOException(ex);
   102         }
   103         return c;
   104     }
   105 
   106     public void setTimeout(long ms) {
   107         timeOut = ms;
   108     }
   109 
   110     public void addClassLoader(ClassLoader url) {
   111         this.loaders.add(url);
   112     }
   113 
   114     ClassLoader[] loaders() {
   115         return loaders.toArray(new ClassLoader[loaders.size()]);
   116     }
   117 
   118     public void showURL(String startpage) throws IOException {
   119         if (!startpage.startsWith("/")) {
   120             startpage = "/" + startpage;
   121         }
   122         HttpServer s = initServer(".", true, "");
   123         int last = startpage.lastIndexOf('/');
   124         String prefix = startpage.substring(0, last);
   125         String simpleName = startpage.substring(last);
   126         s.getServerConfiguration().addHttpHandler(new SubTree(resources, prefix), "/");
   127         server = s;
   128         try {
   129             launchServerAndBrwsr(s, simpleName);
   130         } catch (Exception ex) {
   131             throw new IOException(ex);
   132         }
   133     }
   134 
   135     void showDirectory(File dir, String startpage, boolean addClasses) throws IOException {
   136         if (!startpage.startsWith("/")) {
   137             startpage = "/" + startpage;
   138         }
   139         String prefix = null;
   140         if (!new File(dir, "bck2brwsr.js").exists()) {
   141             int last = startpage.lastIndexOf('/');
   142             if (last >= 0) {
   143                 prefix = startpage.substring(0, last);
   144             }
   145         }
   146         HttpServer s = initServer(dir.getPath(), addClasses, prefix);
   147         try {
   148             launchServerAndBrwsr(s, startpage);
   149         } catch (Exception ex) {
   150             throw new IOException(ex);
   151         }
   152     }
   153 
   154     @Override
   155     public void initialize() throws IOException {
   156         try {
   157             executeInBrowser();
   158         } catch (InterruptedException ex) {
   159             final InterruptedIOException iio = new InterruptedIOException(ex.getMessage());
   160             iio.initCause(ex);
   161             throw iio;
   162         } catch (Exception ex) {
   163             if (ex instanceof IOException) {
   164                 throw (IOException)ex;
   165             }
   166             if (ex instanceof RuntimeException) {
   167                 throw (RuntimeException)ex;
   168             }
   169             throw new IOException(ex);
   170         }
   171     }
   172 
   173     private HttpServer initServer(String path, boolean addClasses, String vmPrefix) throws IOException {
   174         HttpServer s = HttpServer.createSimpleServer(null, new PortRange(8080, 65535));
   175         /*
   176         ThreadPoolConfig fewThreads = ThreadPoolConfig.defaultConfig().copy().
   177             setPoolName("Fx/Bck2 Brwsr").
   178             setCorePoolSize(3).
   179             setMaxPoolSize(5);
   180         ThreadPoolConfig oneKernel = ThreadPoolConfig.defaultConfig().copy().
   181             setPoolName("Kernel Fx/Bck2").
   182             setCorePoolSize(3).
   183             setMaxPoolSize(3);
   184         for (NetworkListener nl : s.getListeners()) {
   185             nl.getTransport().setWorkerThreadPoolConfig(fewThreads);
   186             nl.getTransport().setKernelThreadPoolConfig(oneKernel);
   187         }
   188 */
   189         final ServerConfiguration conf = s.getServerConfiguration();
   190         VMAndPages vm = new VMAndPages();
   191         conf.addHttpHandler(vm, "/");
   192         if (vmPrefix != null) {
   193             vm.registerVM(vmPrefix + "/bck2brwsr.js");
   194         }
   195         if (path != null) {
   196             vm.addDocRoot(path);
   197         }
   198         if (addClasses) {
   199             conf.addHttpHandler(new Classes(resources), "/classes/");
   200         }
   201         final WebSocketAddOn addon = new WebSocketAddOn();
   202         for (NetworkListener listener : s.getListeners()) {
   203             listener.registerAddOn(addon);
   204         }
   205         Logger l = Logger.getLogger("org.glassfish.grizzly.http.server.HttpHandler");
   206         l.setLevel(Level.FINE);
   207         l.setUseParentHandlers(false);
   208         ConsoleHandler ch = new ConsoleHandler();
   209         ch.setLevel(Level.ALL);
   210         l.addHandler(ch);
   211         return s;
   212     }
   213 
   214     private static int resourcesCount;
   215     private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
   216         wait = new CountDownLatch(1);
   217         server = initServer(".", true, "");
   218         final ServerConfiguration conf = server.getServerConfiguration();
   219 
   220         class DynamicResourceHandler extends HttpHandler {
   221             private final InvocationContext ic;
   222             DynamicResourceHandler delegate;
   223             public DynamicResourceHandler(InvocationContext ic) {
   224                 this.ic = ic;
   225                 for (Resource r : ic.resources) {
   226                     conf.addHttpHandler(this, r.httpPath);
   227                 }
   228             }
   229 
   230             public void close(DynamicResourceHandler del) {
   231                 conf.removeHttpHandler(this);
   232                 delegate = del;
   233             }
   234 
   235             @Override
   236             public void service(Request request, Response response) throws Exception {
   237                 if (delegate != null) {
   238                     delegate.service(request, response);
   239                     return;
   240                 }
   241 
   242                 if ("/dynamic".equals(request.getRequestURI())) {
   243                     boolean webSocket = false;
   244                     String mimeType = request.getParameter("mimeType");
   245                     List<String> params = new ArrayList<String>();
   246                     for (int i = 0; ; i++) {
   247                         String p = request.getParameter("param" + i);
   248                         if (p == null) {
   249                             break;
   250                         }
   251                         params.add(p);
   252                         if ("protocol:ws".equals(p)) {
   253                             webSocket = true;
   254                             continue;
   255                         }                    }
   256                     final String cnt = request.getParameter("content");
   257                     String mangle = cnt.replace("%20", " ").replace("%0A", "\n");
   258                     ByteArrayInputStream is = new ByteArrayInputStream(mangle.getBytes("UTF-8"));
   259                     URI url;
   260                     final Resource res = new Resource(is, mimeType, "/dynamic/res" + ++resourcesCount, params.toArray(new String[params.size()]));
   261                     if (webSocket) {
   262                         url = registerWebSocket(res);
   263                     } else {
   264                         url = registerResource(res);
   265                     }
   266                     response.setHeader(Header.CacheControl, "no-cache");
   267                     response.setHeader(Header.Pragma, "no-cache");
   268                     response.getWriter().write(url.toString());
   269                     response.getWriter().write("\n");
   270                     return;
   271                 }
   272 
   273                 for (Resource r : ic.resources) {
   274                     if (r.httpPath.equals(request.getRequestURI())) {
   275                         LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
   276                         response.setContentType(r.httpType);
   277                         response.setHeader(Header.CacheControl, "no-cache");
   278                         response.setHeader(Header.Pragma, "no-cache");
   279                         r.httpContent.reset();
   280                         String[] params = null;
   281                         if (r.parameters.length != 0) {
   282                             params = new String[r.parameters.length];
   283                             for (int i = 0; i < r.parameters.length; i++) {
   284                                 params[i] = request.getParameter(r.parameters[i]);
   285                                 if (params[i] == null) {
   286                                     if ("http.method".equals(r.parameters[i])) {
   287                                         params[i] = request.getMethod().toString();
   288                                     } else if ("http.requestBody".equals(r.parameters[i])) {
   289                                         Reader rdr = request.getReader();
   290                                         StringBuilder sb = new StringBuilder();
   291                                         for (;;) {
   292                                             int ch = rdr.read();
   293                                             if (ch == -1) {
   294                                                 break;
   295                                             }
   296                                             sb.append((char)ch);
   297                                         }
   298                                         params[i] = sb.toString();
   299                                     } else if (r.parameters[i].startsWith("http.header.")) {
   300                                         params[i] = request.getHeader(r.parameters[i].substring(12));
   301                                     }
   302                                 }
   303                                 if (params[i] == null) {
   304                                     params[i] = "null";
   305                                 }
   306                             }
   307                         }
   308 
   309                         copyStream(r.httpContent, response.getOutputStream(), null, params);
   310                     }
   311                 }
   312             }
   313 
   314             private URI registerWebSocket(Resource r) {
   315                 WebSocketEngine.getEngine().register("", r.httpPath, new WS(r));
   316                 return pageURL("ws", server, r.httpPath);
   317             }
   318 
   319             private URI registerResource(Resource r) {
   320                 if (!ic.resources.contains(r)) {
   321                     ic.resources.add(r);
   322                     conf.addHttpHandler(this, r.httpPath);
   323                 }
   324                 return pageURL("http", server, r.httpPath);
   325             }
   326         }
   327 
   328         conf.addHttpHandler(new Page(resources, harnessResource()), "/execute");
   329 
   330         conf.addHttpHandler(new HttpHandler() {
   331             int cnt;
   332             List<InvocationContext> cases = new ArrayList<InvocationContext>();
   333             DynamicResourceHandler prev;
   334             @Override
   335             public void service(Request request, Response response) throws Exception {
   336                 String id = request.getParameter("request");
   337                 String value = request.getParameter("result");
   338                 if (value != null && value.indexOf((char)0xC5) != -1) {
   339                     value = toUTF8(value);
   340                 }
   341 
   342 
   343                 InvocationContext mi = null;
   344                 int caseNmbr = -1;
   345 
   346                 if (id != null && value != null) {
   347                     LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value});
   348                     value = decodeURL(value);
   349                     int indx = Integer.parseInt(id);
   350                     cases.get(indx).result(value, null);
   351                     if (++indx < cases.size()) {
   352                         mi = cases.get(indx);
   353                         LOG.log(Level.INFO, "Re-executing case {0}", indx);
   354                         caseNmbr = indx;
   355                     }
   356                 } else {
   357                     if (!cases.isEmpty()) {
   358                         LOG.info("Re-executing test cases");
   359                         mi = cases.get(0);
   360                         caseNmbr = 0;
   361                     }
   362                 }
   363 
   364                 if (mi == null) {
   365                     mi = methods.take();
   366                     caseNmbr = cnt++;
   367                 }
   368                 if (mi == END) {
   369                     response.getWriter().write("");
   370                     wait.countDown();
   371                     cnt = 0;
   372                     LOG.log(Level.INFO, "End of data reached. Exiting.");
   373                     return;
   374                 }
   375                 final DynamicResourceHandler newRH = new DynamicResourceHandler(mi);
   376                 if (prev != null) {
   377                     prev.close(newRH);
   378                 }
   379                 prev = newRH;
   380                 conf.addHttpHandler(prev, "/dynamic");
   381 
   382                 cases.add(mi);
   383                 final String cn = mi.clazz.getName();
   384                 final String mn = mi.methodName;
   385                 LOG.log(Level.INFO, "Request for {0} case. Sending {1}.{2}", new Object[]{caseNmbr, cn, mn});
   386                 response.getWriter().write("{"
   387                     + "className: '" + cn + "', "
   388                     + "methodName: '" + mn + "', "
   389                     + "request: " + caseNmbr
   390                 );
   391                 if (mi.html != null) {
   392                     response.getWriter().write(", html: '");
   393                     response.getWriter().write(encodeJSON(mi.html));
   394                     response.getWriter().write("'");
   395                 }
   396                 response.getWriter().write("}");
   397             }
   398         }, "/data");
   399 
   400         this.brwsr = launchServerAndBrwsr(server, "/execute");
   401     }
   402 
   403     private static String encodeJSON(String in) {
   404         StringBuilder sb = new StringBuilder();
   405         for (int i = 0; i < in.length(); i++) {
   406             char ch = in.charAt(i);
   407             if (ch < 32 || ch == '\'' || ch == '"') {
   408                 sb.append("\\u");
   409                 String hs = "0000" + Integer.toHexString(ch);
   410                 hs = hs.substring(hs.length() - 4);
   411                 sb.append(hs);
   412             } else {
   413                 sb.append(ch);
   414             }
   415         }
   416         return sb.toString();
   417     }
   418 
   419     @Override
   420     public void shutdown() throws IOException {
   421         methods.offer(END);
   422         for (;;) {
   423             int prev = methods.size();
   424             try {
   425                 if (wait != null && wait.await(timeOut, TimeUnit.MILLISECONDS)) {
   426                     break;
   427                 }
   428             } catch (InterruptedException ex) {
   429                 throw new IOException(ex);
   430             }
   431             if (prev == methods.size()) {
   432                 LOG.log(
   433                     Level.WARNING,
   434                     "Timeout and no test has been executed meanwhile (at {0}). Giving up.",
   435                     methods.size()
   436                 );
   437                 break;
   438             }
   439             LOG.log(Level.INFO,
   440                 "Timeout, but tests got from {0} to {1}. Trying again.",
   441                 new Object[]{prev, methods.size()}
   442             );
   443         }
   444         stopServerAndBrwsr(server, brwsr);
   445     }
   446 
   447     static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
   448         for (;;) {
   449             int ch = is.read();
   450             if (ch == -1) {
   451                 break;
   452             }
   453             if (ch == '$' && params.length > 0) {
   454                 int cnt = is.read() - '0';
   455                 if (baseURL != null && cnt == 'U' - '0') {
   456                     os.write(baseURL.getBytes("UTF-8"));
   457                 } else {
   458                     if (cnt >= 0 && cnt < params.length) {
   459                         os.write(params[cnt].getBytes("UTF-8"));
   460                     } else {
   461                         os.write('$');
   462                         os.write(cnt + '0');
   463                     }
   464                 }
   465             } else {
   466                 os.write(ch);
   467             }
   468         }
   469     }
   470 
   471     private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
   472         server.start();
   473         URI uri = pageURL("http", server, page);
   474         return showBrwsr(uri);
   475     }
   476     private static String toUTF8(String value) throws UnsupportedEncodingException {
   477         byte[] arr = new byte[value.length()];
   478         for (int i = 0; i < arr.length; i++) {
   479             arr[i] = (byte)value.charAt(i);
   480         }
   481         return new String(arr, "UTF-8");
   482     }
   483 
   484     private static String decodeURL(String s) {
   485         for (;;) {
   486             int pos = s.indexOf('%');
   487             if (pos == -1) {
   488                 return s;
   489             }
   490             int i = Integer.parseInt(s.substring(pos + 1, pos + 2), 16);
   491             s = s.substring(0, pos) + (char)i + s.substring(pos + 2);
   492         }
   493     }
   494 
   495     private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException {
   496         if (brwsr == null) {
   497             return;
   498         }
   499         Process process = (Process)brwsr[0];
   500 
   501         server.stop();
   502         InputStream stdout = process.getInputStream();
   503         InputStream stderr = process.getErrorStream();
   504         drain("StdOut", stdout);
   505         drain("StdErr", stderr);
   506         process.destroy();
   507         int res;
   508         try {
   509             res = process.waitFor();
   510         } catch (InterruptedException ex) {
   511             throw new IOException(ex);
   512         }
   513         LOG.log(Level.INFO, "Exit code: {0}", res);
   514 
   515         deleteTree((File)brwsr[1]);
   516     }
   517 
   518     private static void drain(String name, InputStream is) throws IOException {
   519         int av = is.available();
   520         if (av > 0) {
   521             StringBuilder sb = new StringBuilder();
   522             sb.append("v== ").append(name).append(" ==v\n");
   523             while (av-- > 0) {
   524                 sb.append((char)is.read());
   525             }
   526             sb.append("\n^== ").append(name).append(" ==^");
   527             LOG.log(Level.INFO, sb.toString());
   528         }
   529     }
   530 
   531     private void deleteTree(File file) {
   532         if (file == null) {
   533             return;
   534         }
   535         File[] arr = file.listFiles();
   536         if (arr != null) {
   537             for (File s : arr) {
   538                 deleteTree(s);
   539             }
   540         }
   541         file.delete();
   542     }
   543 
   544     @Override
   545     public HttpServer call() throws Exception {
   546         return server;
   547     }
   548 
   549     @Override
   550     public void close() throws IOException {
   551         shutdown();
   552     }
   553 
   554     protected Object[] showBrwsr(URI uri) throws IOException {
   555         LOG.log(Level.INFO, "Showing {0}", uri);
   556         if (cmd == null) {
   557             try {
   558                 LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[] {
   559                     System.getProperty("java.vm.name"),
   560                     System.getProperty("java.vm.vendor"),
   561                     System.getProperty("java.vm.version"),
   562                 });
   563                 java.awt.Desktop.getDesktop().browse(uri);
   564                 LOG.log(Level.INFO, "Desktop.browse successfully finished");
   565                 return null;
   566             } catch (UnsupportedOperationException ex) {
   567                 LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
   568                 LOG.log(Level.FINE, null, ex);
   569             } catch (IOException ex) {
   570                 LOG.log(Level.INFO, "Desktop.browse failed: {0}", ex.getMessage());
   571                 LOG.log(Level.FINE, null, ex);
   572             }
   573         }
   574         {
   575             String cmdName = cmd == null ? "xdg-open" : cmd;
   576             String[] cmdArr = {
   577                 cmdName, uri.toString()
   578             };
   579             LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
   580             final Process process = Runtime.getRuntime().exec(cmdArr);
   581             return new Object[] { process, null };
   582         }
   583     }
   584 
   585     abstract void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException;
   586     abstract String harnessResource();
   587     Object compileJar(URL jar, URL precompiled) throws IOException {
   588         return null;
   589     }
   590     String compileFromClassPath(URL f, Res loader) throws IOException {
   591         return null;
   592     }
   593 
   594     private static URI pageURL(String protocol, HttpServer server, final String page) {
   595         NetworkListener listener = server.getListeners().iterator().next();
   596         int port = listener.getPort();
   597         try {
   598             return new URI(protocol + "://localhost:" + port + page);
   599         } catch (URISyntaxException ex) {
   600             throw new IllegalStateException(ex);
   601         }
   602     }
   603 
   604     final class Res {
   605         private final Set<URL> ignore = new HashSet<URL>();
   606 
   607         Object compileJar(URL jarURL) throws IOException {
   608             List<String[]> libraries = new ArrayList<String[]>();
   609             for (ClassLoader loader : loaders) {
   610                 Enumeration<URL> en = loader.getResources("META-INF/MANIFEST.MF");
   611                 while (en.hasMoreElements()) {
   612                     URL e = en.nextElement();
   613                     Manifest mf = new Manifest(e.openStream());
   614                     for (Map.Entry<String, Attributes> entrySet : mf.getEntries().entrySet()) {
   615                         String key = entrySet.getKey();
   616                         Attributes attr = entrySet.getValue();
   617 
   618                         final String a = attr.getValue("Bck2BrwsrArtifactId");
   619                         final String g = attr.getValue("Bck2BrwsrGroupId");
   620                         final String v = attr.getValue("Bck2BrwsrVersion");
   621                         final String d = attr.getValue("Bck2BrwsrDebug");
   622 
   623                         if (g != null && a != null && v != null && "true".equals(d)) {
   624                             libraries.add(new String[] {
   625                                 a, g, v, key
   626                             });
   627                         }
   628                     }
   629                 }
   630             }
   631             URL precompiled = null;
   632             for (ClassLoader loader : loaders) {
   633                 for (String[] lib : libraries) {
   634                     final String res = "META-INF/maven/" + lib[1] + "/" + lib[0] + "/pom.properties";
   635                     URL props = loader.getResource(res);
   636                     if (props != null) {
   637                         URLConnection c = props.openConnection();
   638                         Properties load = new Properties();
   639                         final InputStream is = c.getInputStream();
   640                         load.load(is);
   641                         is.close();
   642                         if (lib[2].equals(load.getProperty("version"))) {
   643                             if (c instanceof JarURLConnection) {
   644                                 final URL definedInURL = ((JarURLConnection)c).getJarFileURL();
   645                                 if (definedInURL.equals(jarURL)) {
   646                                     precompiled = loader.getResource(lib[3]);
   647                                 }
   648                             }
   649                         }
   650                     }
   651                 }
   652             }
   653             Object ret = BaseHTTPLauncher.this.compileJar(jarURL, precompiled);
   654             ignore.add(jarURL);
   655             return ret;
   656         }
   657         String compileFromClassPath(URL f) throws IOException {
   658             return BaseHTTPLauncher.this.compileFromClassPath(f, this);
   659         }
   660         public URL get(String resource, int skip) throws IOException {
   661             if (!resource.endsWith(".class")) {
   662                 return getResource(resource, skip);
   663             }
   664             URL u = null;
   665             for (ClassLoader l : loaders) {
   666                 Enumeration<URL> en = l.getResources(resource);
   667                 while (en.hasMoreElements()) {
   668                     u = en.nextElement();
   669                     if (u.toExternalForm().matches("^.*emul.*rt\\.jar.*$")) {
   670                         return u;
   671                     }
   672                 }
   673             }
   674             if (u != null) {
   675                 if (u.toExternalForm().contains("rt.jar")) {
   676                     LOG.log(Level.WARNING, "No fallback to bootclasspath for {0}", u);
   677                     return null;
   678                 }
   679                 return u;
   680             }
   681             throw new IOException("Can't find " + resource);
   682         }
   683         private URL getResource(String resource, int skip) throws IOException {
   684             for (ClassLoader l : loaders) {
   685                 Enumeration<URL> en = l.getResources(resource);
   686                 while (en.hasMoreElements()) {
   687                     final URL now = en.nextElement();
   688                     if (now.toExternalForm().contains("sisu-inject-bean")) {
   689                         // certainly we don't want this resource, as that
   690                         // module is not compiled with target 1.6, currently
   691                         continue;
   692                     }
   693                     if (now.getProtocol().equals("jar")) {
   694                         JarURLConnection juc = (JarURLConnection) now.openConnection();
   695                         if (now.getFile().endsWith(".class") && ignore.contains(juc.getJarFileURL())) {
   696                             continue;
   697                         }
   698                     }
   699                     if (--skip < 0) {
   700                         return now;
   701                     }
   702                 }
   703             }
   704             throw new IOException("Not found (anymore of) " + resource);
   705         }
   706     }
   707 
   708     private static class Page extends HttpHandler {
   709         final String resource;
   710         private final String[] args;
   711         private final Res res;
   712 
   713         public Page(Res res, String resource, String... args) {
   714             this.res = res;
   715             this.resource = resource;
   716             this.args = args.length == 0 ? new String[] { "$0" } : args;
   717         }
   718 
   719         @Override
   720         public void service(Request request, Response response) throws Exception {
   721             String r = computePage(request);
   722             if (r.startsWith("/")) {
   723                 r = r.substring(1);
   724             }
   725             String[] replace = {};
   726             if (r.endsWith(".html")) {
   727                 response.setContentType("text/html");
   728                 LOG.info("Content type text/html");
   729                 replace = args;
   730             }
   731             if (r.endsWith(".xhtml")) {
   732                 response.setContentType("application/xhtml+xml");
   733                 LOG.info("Content type application/xhtml+xml");
   734                 replace = args;
   735             }
   736             OutputStream os = response.getOutputStream();
   737             try {
   738                 InputStream is = res.get(r, 0).openStream();
   739                 copyStream(is, os, request.getRequestURL().toString(), replace);
   740             } catch (IOException ex) {
   741                 response.setDetailMessage(ex.getLocalizedMessage());
   742                 response.setError();
   743                 response.setStatus(404);
   744             }
   745         }
   746 
   747         protected String computePage(Request request) {
   748             String r = resource;
   749             if (r == null) {
   750                 r = request.getHttpHandlerPath();
   751             }
   752             return r;
   753         }
   754     }
   755 
   756     private static class SubTree extends Page {
   757 
   758         public SubTree(Res res, String resource, String... args) {
   759             super(res, resource, args);
   760         }
   761 
   762         @Override
   763         protected String computePage(Request request) {
   764             return resource + request.getHttpHandlerPath();
   765         }
   766 
   767 
   768     }
   769 
   770     private class VMAndPages extends StaticHttpHandler {
   771         private String vmResource;
   772 
   773         public VMAndPages() {
   774             super((String[]) null);
   775         }
   776 
   777         @Override
   778         public void service(Request request, Response response) throws Exception {
   779             if (request.getRequestURI().equals(vmResource)) {
   780                 response.setCharacterEncoding("UTF-8");
   781                 response.setContentType("text/javascript");
   782                 StringBuilder sb = new StringBuilder();
   783                 generateBck2BrwsrJS(sb, BaseHTTPLauncher.this.resources);
   784                 response.getWriter().write(sb.toString());
   785             } else {
   786                 super.service(request, response);
   787             }
   788         }
   789 
   790         private void registerVM(String vmResource) {
   791             this.vmResource = vmResource;
   792         }
   793     }
   794 
   795     private static class Classes extends HttpHandler {
   796         private final Res loader;
   797 
   798         public Classes(Res loader) {
   799             this.loader = loader;
   800         }
   801 
   802         @Override
   803         public void service(Request request, Response response) throws Exception {
   804             String res = request.getHttpHandlerPath();
   805             if (res.startsWith("/")) {
   806                 res = res.substring(1);
   807             }
   808             String skip = request.getParameter("skip");
   809             int skipCnt = skip == null ? 0 : Integer.parseInt(skip);
   810             URL url = loader.get(res, skipCnt);
   811             if (url != null && !res.equals("META-INF/MANIFEST.MF")) try {
   812                 response.setCharacterEncoding("UTF-8");
   813                 if (url.getProtocol().equals("jar")) {
   814                     JarURLConnection juc = (JarURLConnection) url.openConnection();
   815                     Object s = null;
   816                     try {
   817                         s = loader.compileJar(juc.getJarFileURL());
   818                     } catch (IOException iOException) {
   819                         throw new IOException("Can't compile " + url.toExternalForm(), iOException);
   820                     }
   821                     if (s instanceof String) {
   822                         Writer w = response.getWriter();
   823                         w.append((String)s);
   824                         w.close();
   825                         return;
   826                     }
   827                     if (s instanceof InputStream) {
   828                         copyStream((InputStream) s, response.getOutputStream(), null);
   829                         return;
   830                     }
   831                 }
   832                 if (url.getProtocol().equals("file")) {
   833                     final String filePart = url.getFile();
   834                     if (filePart.endsWith(res)) {
   835                         url = new URL(
   836                             url.getProtocol(),
   837                             url.getHost(),
   838                             url.getPort(),
   839                             filePart.substring(0, filePart.length() - res.length())
   840                         );
   841                     }
   842                     String s = loader.compileFromClassPath(url);
   843                     if (s != null) {
   844                         Writer w = response.getWriter();
   845                         w.append(s);
   846                         w.close();
   847                         return;
   848                     }
   849                 }
   850             } catch (IOException ex) {
   851                 LOG.log(Level.SEVERE, "Cannot handle " + res, ex);
   852             }
   853             InputStream is = null;
   854             try {
   855                 if (url == null) {
   856                     throw new IOException("Resource not found");
   857                 }
   858                 is = url.openStream();
   859                 response.setContentType("text/javascript");
   860                 Writer w = response.getWriter();
   861                 w.append("([");
   862                 for (int i = 0;; i++) {
   863                     int b = is.read();
   864                     if (b == -1) {
   865                         break;
   866                     }
   867                     if (i > 0) {
   868                         w.append(", ");
   869                     }
   870                     if (i % 20 == 0) {
   871                         w.write("\n");
   872                     }
   873                     if (b > 127) {
   874                         b = b - 256;
   875                     }
   876                     w.append(Integer.toString(b));
   877                 }
   878                 w.append("\n])");
   879             } catch (IOException ex) {
   880                 response.setStatus(HttpStatus.NOT_FOUND_404);
   881                 response.setError();
   882                 response.setDetailMessage(ex.getMessage());
   883             } finally {
   884                 if (is != null) {
   885                     is.close();
   886                 }
   887             }
   888         }
   889 
   890     }
   891     private static class WS extends WebSocketApplication {
   892 
   893         private final Resource r;
   894 
   895         private WS(Resource r) {
   896             this.r = r;
   897         }
   898 
   899         @Override
   900         public void onMessage(WebSocket socket, String text) {
   901             try {
   902                 r.httpContent.reset();
   903                 ByteArrayOutputStream out = new ByteArrayOutputStream();
   904                 copyStream(r.httpContent, out, null, text);
   905                 String s = new String(out.toByteArray(), "UTF-8");
   906                 socket.send(s);
   907             } catch (IOException ex) {
   908                 LOG.log(Level.WARNING, null, ex);
   909             }
   910         }
   911 
   912     }}