1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Sat Dec 15 22:46:26 2012 +0100
1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Sun Dec 16 20:11:18 2012 +0100
1.3 @@ -44,8 +44,11 @@
1.4 final ClassLoader loader = Bck2BrwsrLauncher.class.getClassLoader();
1.5
1.6 final ServerConfiguration conf = server.getServerConfiguration();
1.7 - conf.addHttpHandler(new Console("org.apidesign.bck2brwsr.launcher.Console", "welcome", "false"), "/console");
1.8 + conf.addHttpHandler(new Page("console.xhtml",
1.9 + "org.apidesign.bck2brwsr.launcher.Console", "welcome", "false"
1.10 + ), "/console");
1.11 conf.addHttpHandler(new VM(loader), "/bck2brwsr.js");
1.12 + conf.addHttpHandler(new VMInit(), "/vm.js");
1.13 conf.addHttpHandler(new Classes(loader), "/classes/");
1.14 conf.addHttpHandler(new HttpHandler() {
1.15 @Override
1.16 @@ -62,13 +65,25 @@
1.17 InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml");
1.18 copyStream(is, os, clazz, method, "true");
1.19 }
1.20 - }, "/execute");
1.21 + }, "/");
1.22 + conf.addHttpHandler(new HttpHandler() {
1.23 + int cnt;
1.24 + @Override
1.25 + public void service(Request request, Response response) throws Exception {
1.26 + response.getWriter().write("{"
1.27 + + "className: 'org.apidesign.bck2brwsr.launcher.Console',"
1.28 + + "methodName: 'welcome',"
1.29 + + "request: " + cnt
1.30 + + "}");
1.31 + }
1.32 + }, "execute/data");
1.33 + conf.addHttpHandler(new Page("harness.xhtml"), "/execute");
1.34
1.35 server.start();
1.36 NetworkListener listener = server.getListeners().iterator().next();
1.37 int port = listener.getPort();
1.38
1.39 - URI uri = new URI("http://localhost:" + port + "/execute?class=org.apidesign.bck2brwsr.launcher.Console&method=welcome");
1.40 + URI uri = new URI("http://localhost:" + port + "/execute");
1.41 try {
1.42 Desktop.getDesktop().browse(uri);
1.43 } catch (UnsupportedOperationException ex) {
1.44 @@ -98,10 +113,12 @@
1.45 }
1.46 }
1.47
1.48 - private static class Console extends HttpHandler {
1.49 + private static class Page extends HttpHandler {
1.50 + private final String resource;
1.51 private final String[] args;
1.52
1.53 - public Console(String... args) {
1.54 + public Page(String resource, String... args) {
1.55 + this.resource = resource;
1.56 this.args = args;
1.57 }
1.58
1.59 @@ -109,7 +126,7 @@
1.60 public void service(Request request, Response response) throws Exception {
1.61 response.setContentType("text/html");
1.62 OutputStream os = response.getOutputStream();
1.63 - InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml");
1.64 + InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream(resource);
1.65 copyStream(is, os, args);
1.66 }
1.67 }
1.68 @@ -128,6 +145,25 @@
1.69 Bck2Brwsr.generate(response.getWriter(), loader);
1.70 }
1.71 }
1.72 + private static class VMInit extends HttpHandler {
1.73 + public VMInit() {
1.74 + }
1.75 +
1.76 + @Override
1.77 + public void service(Request request, Response response) throws Exception {
1.78 + response.setCharacterEncoding("UTF-8");
1.79 + response.setContentType("text/javascript");
1.80 + response.getWriter().append(
1.81 + "function ldCls(res) {\n"
1.82 + + " var request = new XMLHttpRequest();\n"
1.83 + + " request.open('GET', 'classes/' + res, false);\n"
1.84 + + " request.send();\n"
1.85 + + " var arr = eval('(' + request.responseText + ')');\n"
1.86 + + " return arr;\n"
1.87 + + "}\n"
1.88 + + "var vm = new bck2brwsr(ldCls);\n");
1.89 + }
1.90 + }
1.91
1.92 private static class Classes extends HttpHandler {
1.93 private final ClassLoader loader;
2.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Sat Dec 15 22:46:26 2012 +0100
2.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Sun Dec 16 20:11:18 2012 +0100
2.3 @@ -17,7 +17,10 @@
2.4 */
2.5 package org.apidesign.bck2brwsr.launcher;
2.6
2.7 +import java.io.InputStream;
2.8 +import java.lang.reflect.InvocationTargetException;
2.9 import java.lang.reflect.Method;
2.10 +import java.net.URL;
2.11 import org.apidesign.bck2brwsr.core.JavaScriptBody;
2.12
2.13 /**
2.14 @@ -45,7 +48,23 @@
2.15 public static void execute() throws Exception {
2.16 String clazz = (String) getAttr("clazz", "value");
2.17 String method = (String) getAttr("method", "value");
2.18 + Object res = invokeMethod(clazz, method);
2.19 + setAttr("result", "value", res);
2.20 + }
2.21 +
2.22 + public static void harness() {
2.23 + try {
2.24 + URL u = new URL("/execute/data");
2.25 + String data = (String) u.getContent(new Class[] { String.class });
2.26 + setAttr("result", "value", data);
2.27 + } catch (Exception ex) {
2.28 + setAttr("result", "value", ex.getMessage());
2.29 + }
2.30 + }
2.31
2.32 + private static Object invokeMethod(String clazz, String method)
2.33 + throws ClassNotFoundException, InvocationTargetException,
2.34 + SecurityException, IllegalAccessException, IllegalArgumentException {
2.35 Method found = null;
2.36 Class<?> c = Class.forName(clazz);
2.37 for (Method m : c.getMethods()) {
2.38 @@ -59,7 +78,6 @@
2.39 } else {
2.40 res = "Can't find method " + method + " in " + clazz;
2.41 }
2.42 -
2.43 - setAttr("result", "value", res);
2.44 + return res;
2.45 }
2.46 }
3.1 --- a/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/console.xhtml Sat Dec 15 22:46:26 2012 +0100
3.2 +++ b/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/console.xhtml Sun Dec 16 20:11:18 2012 +0100
3.3 @@ -24,17 +24,9 @@
3.4 <title>Bck2Brwsr Launcher</title>
3.5 </head>
3.6 <body>
3.7 - <script src="bck2brwsr.js"></script>
3.8 - <script type="text/javascript">
3.9 - function ldCls(res) {
3.10 - var request = new XMLHttpRequest();
3.11 - request.open("GET", "classes/" + res, false);
3.12 - request.send();
3.13 - var arr = eval('(' + request.responseText + ')');
3.14 - return arr;
3.15 - }
3.16 - var vm = new bck2brwsr(ldCls);
3.17 - </script>
3.18 + <script src="/bck2brwsr.js"></script>
3.19 + <script src="/vm.js"></script>
3.20 +
3.21 <h1>Bck2Browser Console Launcher</h1>
3.22
3.23 Class Name:
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml Sun Dec 16 20:11:18 2012 +0100
4.3 @@ -0,0 +1,39 @@
4.4 +<?xml version="1.0" encoding="UTF-8"?>
4.5 +<!--
4.6 +
4.7 + Back 2 Browser Bytecode Translator
4.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.9 +
4.10 + This program is free software: you can redistribute it and/or modify
4.11 + it under the terms of the GNU General Public License as published by
4.12 + the Free Software Foundation, version 2 of the License.
4.13 +
4.14 + This program is distributed in the hope that it will be useful,
4.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
4.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.17 + GNU General Public License for more details.
4.18 +
4.19 + You should have received a copy of the GNU General Public License
4.20 + along with this program. Look for COPYING file in the top folder.
4.21 + If not, see http://opensource.org/licenses/GPL-2.0.
4.22 +
4.23 +-->
4.24 +<!DOCTYPE html>
4.25 +<html xmlns="http://www.w3.org/1999/xhtml">
4.26 + <head>
4.27 + <title>Bck2Brwsr Harness</title>
4.28 + </head>
4.29 + <body>
4.30 + <script src="/bck2brwsr.js"></script>
4.31 + <script src="/vm.js"></script>
4.32 +
4.33 + <h1>Bck2Browser Execution Harness</h1>
4.34 +
4.35 + <textarea id="result" rows="10" cols="80" disabled="">
4.36 + </textarea>
4.37 +
4.38 + <script type="text/javascript">
4.39 + vm.loadClass('org.apidesign.bck2brwsr.launcher.Console').harness__V();
4.40 + </script>
4.41 + </body>
4.42 +</html>