launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Feb 2015 11:12:53 +0100
changeset 1787 ea12a3bb4b33
parent 1746 001275142bf7
child 1914 88b00e03fc8f
permissions -rw-r--r--
Using year range 2012-2015 in copyright header
     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.File;
    21 import java.io.IOException;
    22 import java.io.InputStreamReader;
    23 import java.io.Reader;
    24 import java.net.MalformedURLException;
    25 import java.net.URISyntaxException;
    26 import java.net.URL;
    27 import java.util.Arrays;
    28 import java.util.HashSet;
    29 import java.util.List;
    30 import java.util.Set;
    31 import java.util.logging.Level;
    32 
    33 /**
    34  * Lightweight server to launch Bck2Brwsr applications and tests.
    35  * Supports execution in native browser as well as Java's internal 
    36  * execution engine.
    37  */
    38 final class Bck2BrwsrLauncher extends BaseHTTPLauncher {
    39     private Set<String> testClasses = new HashSet<String>();
    40     
    41     public Bck2BrwsrLauncher(String cmd) {
    42         super(cmd);
    43     }
    44     
    45     @Override
    46     String harnessResource() {
    47         return "org/apidesign/bck2brwsr/launcher/harness.xhtml";
    48     }
    49 
    50     @Override
    51     Object compileJar(URL jar, URL precompiled) throws IOException {
    52         if (precompiled != null) {
    53             LOG.log(Level.INFO, "Found precompiled JAR version of {0} at {1}. Using.", new Object[]{jar, precompiled});
    54             return precompiled.openStream();
    55         }
    56         File f;
    57         try {
    58             f = new File(jar.toURI());
    59         } catch (URISyntaxException ex) {
    60             throw new IOException(ex);
    61         }
    62         LOG.log(Level.INFO, "No precompiled version for {0} found. Compiling.", jar);
    63         return CompileCP.compileJAR(f, testClasses);
    64     }
    65 
    66     @Override
    67     public InvocationContext createInvocation(Class<?> clazz, String method) {
    68         testClasses.add(clazz.getName().replace('.', '/'));
    69         return super.createInvocation(clazz, method);
    70     }
    71 
    72     @Override String compileFromClassPath(URL f, Res loader) throws IOException {
    73         return CompileCP.compileFromClassPath(f, loader);
    74     }
    75     
    76     @Override
    77     void generateBck2BrwsrJS(StringBuilder sb, final Res loader) throws IOException {
    78         String b2b = System.getProperty("bck2brwsr.js");
    79         if (b2b != null) {
    80             LOG.log(Level.INFO, "Serving bck2brwsr.js from {0}", b2b);
    81             URL bu;
    82             try {
    83                 bu = new URL(b2b);
    84             } catch (MalformedURLException ex) {
    85                 File f = new File(b2b);
    86                 if (f.exists()) {
    87                     bu = f.toURI().toURL();
    88                 } else {
    89                     throw ex;
    90                 }
    91             }
    92             try (Reader r = new InputStreamReader(bu.openStream())) {
    93                 char[] arr = new char[4096];
    94                 for (;;) {
    95                    int len = r.read(arr);
    96                    if (len == -1) {
    97                        break;
    98                    }
    99                    sb.append(arr, 0, len);
   100                 }
   101             }
   102         } else {
   103             LOG.log(Level.INFO, "Generating bck2brwsr.js from scratch", b2b);
   104             CompileCP.compileVM(sb, loader);
   105         }
   106         sb.append(
   107               "(function WrapperVM(global) {\n"
   108             + "  var cache = {};\n"
   109             + "  var empty = {};\n"
   110             + "  function ldCls(res, skip) {\n"
   111             + "    var c = cache[res];\n"
   112             + "    if (c) {\n"
   113             + "      if (c[skip] === empty) return null;\n"
   114             + "      if (c[skip]) return c[skip];\n"
   115             + "    } else {\n"
   116             + "      cache[res] = c = new Array();\n"
   117             + "    }\n"
   118             + "    var request = new XMLHttpRequest();\n"
   119             + "    request.open('GET', '/classes/' + res + '?skip=' + skip, false);\n"
   120             + "    request.send();\n"
   121             + "    if (request.status !== 200) {\n"
   122             + "      c[skip] = null;\n"
   123             + "      return null;\n"
   124             + "    }\n"
   125             + "    var arr = eval(request.responseText);\n"
   126             + "    if (arr === null) c[skip] = empty;\n"
   127             + "    else c[skip] = arr;\n"
   128             + "    return arr;\n"
   129             + "  }\n"
   130             + "  var prevvm = global.bck2brwsr;\n"
   131             + "  global.bck2brwsr = function() {\n"
   132             + "    var args = Array.prototype.slice.apply(arguments);\n"
   133             + "    args.unshift(ldCls);\n"
   134             + "    return prevvm.apply(null, args);\n"
   135             + "  };\n"
   136             + "  global.bck2brwsr.register = prevvm.register;\n"
   137             + "})(this);\n"
   138         );
   139         LOG.log(Level.INFO, "Serving bck2brwsr.js", b2b);
   140     }
   141 
   142 }