launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 12 Oct 2013 09:05:08 +0200
changeset 1371 fd2d4ca28bd3
parent 1336 804f6f982f4e
child 1375 a6c71e376889
permissions -rw-r--r--
If j2js task is used, read the generated bck2brwsr.js in rather than compiling new one during execution
     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.URL;
    27 import java.util.logging.Level;
    28 import org.apidesign.vm4brwsr.Bck2Brwsr;
    29 
    30 /**
    31  * Lightweight server to launch Bck2Brwsr applications and tests.
    32  * Supports execution in native browser as well as Java's internal 
    33  * execution engine.
    34  */
    35 final class Bck2BrwsrLauncher extends BaseHTTPLauncher {
    36     
    37     public Bck2BrwsrLauncher(String cmd) {
    38         super(cmd);
    39     }
    40     
    41     @Override
    42     String harnessResource() {
    43         return "org/apidesign/bck2brwsr/launcher/harness.xhtml";
    44     }
    45     
    46     @Override
    47     void generateBck2BrwsrJS(StringBuilder sb, final Res loader) throws IOException {
    48         class R implements Bck2Brwsr.Resources {
    49             @Override
    50             public InputStream get(String resource) throws IOException {
    51                 return loader.get(resource);
    52             }
    53         }
    54         String b2b = System.getProperty("bck2brwsr.js");
    55         if (b2b != null) {
    56             LOG.log(Level.INFO, "Serving bck2brwsr.js from {0}", b2b);
    57             try (Reader r = new InputStreamReader(new URL(b2b).openStream())) {
    58                 char[] arr = new char[4096];
    59                 for (;;) {
    60                    int len = r.read(arr);
    61                    if (len == -1) {
    62                        break;
    63                    }
    64                    sb.append(arr, 0, len);
    65                 }
    66             }
    67         } else {
    68             LOG.log(Level.INFO, "Generating bck2brwsr.js from scratch", b2b);
    69             Bck2Brwsr.generate(sb, new R());
    70         }
    71         sb.append(
    72               "(function WrapperVM(global) {"
    73             + "  function ldCls(res) {\n"
    74             + "    var request = new XMLHttpRequest();\n"
    75             + "    request.open('GET', '/classes/' + res, false);\n"
    76             + "    request.send();\n"
    77             + "    if (request.status !== 200) return null;\n"
    78             + "    var arr = eval('(' + request.responseText + ')');\n"
    79             + "    return arr;\n"
    80             + "  }\n"
    81             + "  var prevvm = global.bck2brwsr;\n"
    82             + "  global.bck2brwsr = function() {\n"
    83             + "    var args = Array.prototype.slice.apply(arguments);\n"
    84             + "    args.unshift(ldCls);\n"
    85             + "    return prevvm.apply(null, args);\n"
    86             + "  };\n"
    87             + "})(this);\n"
    88         );
    89         LOG.log(Level.INFO, "Serving bck2brwsr.js", b2b);
    90     }
    91 
    92 }