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;
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;
27 import java.util.HashSet;
29 import java.util.logging.Level;
32 * Lightweight server to launch Bck2Brwsr applications and tests.
33 * Supports execution in native browser as well as Java's internal
36 final class Bck2BrwsrLauncher extends BaseHTTPLauncher {
37 private Set<String> testClasses = new HashSet<String>();
39 public Bck2BrwsrLauncher(String cmd) {
44 String harnessResource() {
45 return "org/apidesign/bck2brwsr/launcher/harness.xhtml";
49 String compileJar(URL jar) throws IOException {
52 f = new File(jar.toURI());
53 } catch (URISyntaxException ex) {
54 throw new IOException(ex);
56 return CompileCP.compileJAR(f, testClasses);
60 public InvocationContext createInvocation(Class<?> clazz, String method) {
61 testClasses.add(clazz.getName().replace('.', '/'));
62 return super.createInvocation(clazz, method);
65 @Override String compileFromClassPath(URL f, Res loader) throws IOException {
66 return CompileCP.compileFromClassPath(f, loader);
70 void generateBck2BrwsrJS(StringBuilder sb, final Res loader) throws IOException {
71 String b2b = System.getProperty("bck2brwsr.js");
73 LOG.log(Level.INFO, "Serving bck2brwsr.js from {0}", b2b);
77 } catch (MalformedURLException ex) {
78 File f = new File(b2b);
80 bu = f.toURI().toURL();
85 try (Reader r = new InputStreamReader(bu.openStream())) {
86 char[] arr = new char[4096];
88 int len = r.read(arr);
92 sb.append(arr, 0, len);
96 LOG.log(Level.INFO, "Generating bck2brwsr.js from scratch", b2b);
97 CompileCP.compileVM(sb, loader);
100 "(function WrapperVM(global) {\n"
101 + " var cache = {};\n"
102 + " var empty = {};\n"
103 + " function ldCls(res, skip) {\n"
104 + " var c = cache[res];\n"
106 + " if (c[skip] === empty) return null;\n"
107 + " if (c[skip]) return c[skip];\n"
109 + " cache[res] = c = new Array();\n"
111 + " var request = new XMLHttpRequest();\n"
112 + " request.open('GET', '/classes/' + res + '?skip=' + skip, false);\n"
113 + " request.send();\n"
114 + " if (request.status !== 200) {\n"
115 + " c[skip] = null;\n"
118 + " var arr = eval(request.responseText);\n"
119 + " if (arr === null) c[skip] = empty;\n"
120 + " else c[skip] = arr;\n"
123 + " var prevvm = global.bck2brwsr;\n"
124 + " global.bck2brwsr = function() {\n"
125 + " var args = Array.prototype.slice.apply(arguments);\n"
126 + " args.unshift(ldCls);\n"
127 + " return prevvm.apply(null, args);\n"
129 + " global.bck2brwsr.register = prevvm.register;\n"
132 LOG.log(Level.INFO, "Serving bck2brwsr.js", b2b);