# HG changeset patch # User Jaroslav Tulach # Date 1355585128 -3600 # Node ID d41cfd77842dca04e3936048bf1be547a78e968d # Parent 3884815c0629831abb5b744174d3045529639272 Basic sketch of an HTTP server for launching bck2brwsr applications diff -r 3884815c0629 -r d41cfd77842d launcher/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/pom.xml Sat Dec 15 16:25:28 2012 +0100 @@ -0,0 +1,36 @@ + + + 4.0.0 + + org.apidesign + bck2brwsr + 0.3-SNAPSHOT + + org.apidesign.bck2brwsr + launcher + 0.3-SNAPSHOT + Bck2Brwsr Launcher + http://maven.apache.org + + UTF-8 + + + + junit + junit + 3.8.1 + test + + + org.glassfish.grizzly + grizzly-http-server + 2.2.19 + + + ${project.groupId} + vm4brwsr + ${project.version} + + + diff -r 3884815c0629 -r d41cfd77842d launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Sat Dec 15 16:25:28 2012 +0100 @@ -0,0 +1,124 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.launcher; + +import java.awt.Desktop; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Writer; +import java.net.URI; +import java.net.URL; +import java.util.Enumeration; +import org.apidesign.vm4brwsr.Bck2Brwsr; +import org.glassfish.grizzly.PortRange; +import org.glassfish.grizzly.http.server.HttpHandler; +import org.glassfish.grizzly.http.server.HttpServer; +import org.glassfish.grizzly.http.server.NetworkListener; +import org.glassfish.grizzly.http.server.Request; +import org.glassfish.grizzly.http.server.Response; +import org.glassfish.grizzly.http.server.ServerConfiguration; + +/** + * Lightweight server to launch Bck2Brwsr applications in real browser. + */ +public class Bck2BrwsrLauncher { + public static void main( String[] args ) throws Exception { + HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535)); + final ClassLoader loader = Bck2BrwsrLauncher.class.getClassLoader(); + + final ServerConfiguration conf = server.getServerConfiguration(); + conf.addHttpHandler(new HttpHandler() { + @Override + public void service(Request request, Response response) throws Exception { + response.setContentType("text/html"); + OutputStream os = response.getOutputStream(); + InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml"); + for (;;) { + int ch = is.read(); + if (ch == -1) { + break; + } + os.write(ch); + } + } + }, "/console"); + conf.addHttpHandler(new HttpHandler() { + @Override + public void service(Request request, Response response) throws Exception { + response.setCharacterEncoding("UTF-8"); + response.setContentType("text/javascript"); + Bck2Brwsr.generate(response.getWriter(), loader); + } + }, "/bck2brwsr.js"); + conf.addHttpHandler(new HttpHandler() { + @Override + public void service(Request request, Response response) throws Exception { + String res = request.getHttpHandlerPath(); + if (res.startsWith("/")) { + res = res.substring(1); + } + Enumeration en = loader.getResources(res); + URL u = null; + while (en.hasMoreElements()) { + u = en.nextElement(); + } + if (u == null) { + response.setError(); + response.setDetailMessage("Can't find resource " + res); + } + response.setContentType("text/javascript"); + InputStream is = u.openStream(); + Writer w = response.getWriter(); + w.append("["); + for (int i = 0;; i++) { + int b = is.read(); + if (b == -1) { + break; + } + if (i > 0) { + w.append(", "); + } + if (i % 20 == 0) { + w.write("\n"); + } + if (b > 127) { + b = b - 256; + } + w.append(Integer.toString(b)); + } + w.append("\n]"); + } + }, "/classes/"); + + server.start(); + NetworkListener listener = server.getListeners().iterator().next(); + int port = listener.getPort(); + + URI uri = new URI("http://localhost:" + port + "/console"); + try { + Desktop.getDesktop().browse(uri); + } catch (UnsupportedOperationException ex) { + String[] cmd = { + "xdg-open", uri.toString() + }; + Runtime.getRuntime().exec(cmd).waitFor(); + } + + System.in.read(); + } +} diff -r 3884815c0629 -r d41cfd77842d launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Sat Dec 15 16:25:28 2012 +0100 @@ -0,0 +1,65 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.launcher; + +import java.lang.reflect.Method; +import org.apidesign.bck2brwsr.core.JavaScriptBody; + +/** + * + * @author Jaroslav Tulach + */ +public class Console { + public static String welcome() { + final String msg = "Hello from Bck2Brwsr!"; + alert(msg); + return msg; + } + + @JavaScriptBody(args = "msg", body = "alert(msg);") + private static native void alert(String msg); + + @JavaScriptBody(args = {"id", "attr"}, body = + "return window.document.getElementById(id)[attr].toString();") + private static native Object getAttr(String id, String attr); + + @JavaScriptBody(args = {"id", "attr", "value"}, body = + "window.document.getElementById(id)[attr] = value;") + private static native void setAttr(String id, String attr, Object value); + + public static void execute() throws Exception { + String clazz = (String) getAttr("clazz", "value"); + String method = (String) getAttr("method", "value"); + + Method found = null; + Class c = Class.forName(clazz); + for (Method m : c.getMethods()) { + if (m.getName().equals(method)) { + found = m; + } + } + Object res; + if (found != null) { + res = found.invoke(null); + } else { + res = "Can't find method " + method + " in " + clazz; + } + + setAttr("result", "value", res); + } +} diff -r 3884815c0629 -r d41cfd77842d launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/console.xhtml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/console.xhtml Sat Dec 15 16:25:28 2012 +0100 @@ -0,0 +1,54 @@ + + + + + + Bck2Brwsr Launcher + + + + +

Bck2Browser Console Launcher

+ + Class Name: + +
+ Method Name: + + +
+ + + +
+ + + diff -r 3884815c0629 -r d41cfd77842d pom.xml --- a/pom.xml Sat Dec 15 08:17:45 2012 +0100 +++ b/pom.xml Sat Dec 15 16:25:28 2012 +0100 @@ -14,6 +14,7 @@ javaquery javap benchmarks + launcher @@ -109,3 +110,4 @@ COPYING +