launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.launcher;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import org.apidesign.vm4brwsr.Bck2Brwsr;
25 * Lightweight server to launch Bck2Brwsr applications and tests.
26 * Supports execution in native browser as well as Java's internal
29 final class Bck2BrwsrLauncher extends BaseHTTPLauncher {
31 public Bck2BrwsrLauncher(String cmd) {
36 String harnessResource() {
37 return "org/apidesign/bck2brwsr/launcher/harness.xhtml";
41 void generateBck2BrwsrJS(StringBuilder sb, final Res loader) throws IOException {
42 class R implements Bck2Brwsr.Resources {
44 public InputStream get(String resource) throws IOException {
45 return loader.get(resource);
49 Bck2Brwsr.generate(sb, new R());
51 "(function WrapperVM(global) {"
52 + " function ldCls(res) {\n"
53 + " var request = new XMLHttpRequest();\n"
54 + " request.open('GET', '/classes/' + res, false);\n"
55 + " request.send();\n"
56 + " if (request.status !== 200) return null;\n"
57 + " var arr = eval('(' + request.responseText + ')');\n"
60 + " var prevvm = global.bck2brwsr;\n"
61 + " global.bck2brwsr = function() {\n"
62 + " var args = Array.prototype.slice.apply(arguments);\n"
63 + " args.unshift(ldCls);\n"
64 + " return prevvm.apply(null, args);\n"