launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 03 Oct 2013 15:51:55 +0200
changeset 1336 804f6f982f4e
parent 1295 5b3ae17babdf
child 1371 fd2d4ca28bd3
permissions -rw-r--r--
Work on javac branch seems to have some results, so merge it
jaroslav@1033
     1
/**
jaroslav@1033
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1033
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1033
     4
 *
jaroslav@1033
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1033
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1033
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1033
     8
 *
jaroslav@1033
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1033
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1033
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1033
    12
 * GNU General Public License for more details.
jaroslav@1033
    13
 *
jaroslav@1033
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1033
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1033
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1033
    17
 */
jaroslav@1033
    18
package org.apidesign.bck2brwsr.launcher;
jaroslav@1033
    19
jaroslav@1033
    20
import java.io.IOException;
jaroslav@1033
    21
import java.io.InputStream;
jaroslav@1033
    22
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@1033
    23
jaroslav@1033
    24
/**
jaroslav@1033
    25
 * Lightweight server to launch Bck2Brwsr applications and tests.
jaroslav@1033
    26
 * Supports execution in native browser as well as Java's internal 
jaroslav@1033
    27
 * execution engine.
jaroslav@1033
    28
 */
jaroslav@1088
    29
final class Bck2BrwsrLauncher extends BaseHTTPLauncher {
jaroslav@1033
    30
    
jaroslav@1033
    31
    public Bck2BrwsrLauncher(String cmd) {
jaroslav@1088
    32
        super(cmd);
jaroslav@1033
    33
    }
jaroslav@1033
    34
    
jaroslav@1033
    35
    @Override
jaroslav@1088
    36
    String harnessResource() {
jaroslav@1088
    37
        return "org/apidesign/bck2brwsr/launcher/harness.xhtml";
jaroslav@1033
    38
    }
jaroslav@1033
    39
    
jaroslav@1033
    40
    @Override
jaroslav@1088
    41
    void generateBck2BrwsrJS(StringBuilder sb, final Res loader) throws IOException {
jaroslav@1088
    42
        class R implements Bck2Brwsr.Resources {
jaroslav@1088
    43
            @Override
jaroslav@1088
    44
            public InputStream get(String resource) throws IOException {
jaroslav@1088
    45
                return loader.get(resource);
jaroslav@1033
    46
            }
jaroslav@1033
    47
        }
jaroslav@1033
    48
jaroslav@1088
    49
        Bck2Brwsr.generate(sb, new R());
jaroslav@1088
    50
        sb.append(
jaroslav@1088
    51
              "(function WrapperVM(global) {"
jaroslav@1088
    52
            + "  function ldCls(res) {\n"
jaroslav@1088
    53
            + "    var request = new XMLHttpRequest();\n"
jaroslav@1088
    54
            + "    request.open('GET', '/classes/' + res, false);\n"
jaroslav@1088
    55
            + "    request.send();\n"
jtulach@1336
    56
            + "    if (request.status !== 200) return null;\n"
jaroslav@1088
    57
            + "    var arr = eval('(' + request.responseText + ')');\n"
jaroslav@1088
    58
            + "    return arr;\n"
jaroslav@1088
    59
            + "  }\n"
jaroslav@1088
    60
            + "  var prevvm = global.bck2brwsr;\n"
jaroslav@1088
    61
            + "  global.bck2brwsr = function() {\n"
jaroslav@1088
    62
            + "    var args = Array.prototype.slice.apply(arguments);\n"
jaroslav@1088
    63
            + "    args.unshift(ldCls);\n"
jaroslav@1088
    64
            + "    return prevvm.apply(null, args);\n"
jaroslav@1088
    65
            + "  };\n"
jaroslav@1088
    66
            + "})(this);\n"
jaroslav@1088
    67
        );
jaroslav@1033
    68
    }
jaroslav@1033
    69
jaroslav@1033
    70
}