launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 26 May 2014 16:20:51 +0200
branchclosure
changeset 1598 ec62383beb7d
parent 1531 ce3b83777055
child 1599 36746c46716a
permissions -rw-r--r--
Shorten the name to register an extension and use configuration object with a generated magic to ensure basic level of consistency.
     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.File;
    21 import java.io.FileReader;
    22 import java.io.IOException;
    23 import java.io.InputStream;
    24 import java.io.InputStreamReader;
    25 import java.io.Reader;
    26 import java.net.MalformedURLException;
    27 import java.net.URL;
    28 import java.util.HashSet;
    29 import java.util.Set;
    30 import java.util.jar.JarFile;
    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     String compileJar(JarFile jar) throws IOException {
    52         return CompileCP.compileJAR(jar, testClasses);
    53     }
    54 
    55     @Override
    56     public InvocationContext createInvocation(Class<?> clazz, String method) {
    57         testClasses.add(clazz.getName().replace('.', '/'));
    58         return super.createInvocation(clazz, method);
    59     }
    60 
    61     @Override String compileFromClassPath(URL f, Res loader) throws IOException {
    62         return CompileCP.compileFromClassPath(f, loader);
    63     }
    64     
    65     @Override
    66     void generateBck2BrwsrJS(StringBuilder sb, final Res loader) throws IOException {
    67         String b2b = System.getProperty("bck2brwsr.js");
    68         if (b2b != null) {
    69             LOG.log(Level.INFO, "Serving bck2brwsr.js from {0}", b2b);
    70             URL bu;
    71             try {
    72                 bu = new URL(b2b);
    73             } catch (MalformedURLException ex) {
    74                 File f = new File(b2b);
    75                 if (f.exists()) {
    76                     bu = f.toURI().toURL();
    77                 } else {
    78                     throw ex;
    79                 }
    80             }
    81             try (Reader r = new InputStreamReader(bu.openStream())) {
    82                 char[] arr = new char[4096];
    83                 for (;;) {
    84                    int len = r.read(arr);
    85                    if (len == -1) {
    86                        break;
    87                    }
    88                    sb.append(arr, 0, len);
    89                 }
    90             }
    91         } else {
    92             LOG.log(Level.INFO, "Generating bck2brwsr.js from scratch", b2b);
    93             CompileCP.compileVM(sb, loader);
    94         }
    95         sb.append(
    96               "(function WrapperVM(global) {\n"
    97             + "  var cache = {};\n"
    98             + "  var empty = {};\n"
    99             + "  function ldCls(res, skip) {\n"
   100             + "    var c = cache[res];\n"
   101             + "    if (c) {\n"
   102             + "      if (c[skip] === empty) return null;\n"
   103             + "      if (c[skip]) return c[skip];\n"
   104             + "    } else {\n"
   105             + "      cache[res] = c = new Array();\n"
   106             + "    }\n"
   107             + "    var request = new XMLHttpRequest();\n"
   108             + "    request.open('GET', '/classes/' + res + '?skip=' + skip, false);\n"
   109             + "    request.send();\n"
   110             + "    if (request.status !== 200) {\n"
   111             + "      c[skip] = null;\n"
   112             + "      return null;\n"
   113             + "    }\n"
   114             + "    var arr = eval(request.responseText);\n"
   115             + "    if (arr === null) c[skip] = empty;\n"
   116             + "    else c[skip] = arr;\n"
   117             + "    return arr;\n"
   118             + "  }\n"
   119             + "  var prevvm = global.bck2brwsr;\n"
   120             + "  global.bck2brwsr = function() {\n"
   121             + "    var args = Array.prototype.slice.apply(arguments);\n"
   122             + "    args.unshift(ldCls);\n"
   123             + "    return prevvm.apply(null, args);\n"
   124             + "  };\n"
   125             + "  global.bck2brwsr.register = prevvm.register;\n"
   126             + "})(this);\n"
   127         );
   128         LOG.log(Level.INFO, "Serving bck2brwsr.js", b2b);
   129     }
   130 
   131 }