# HG changeset patch # User Jaroslav Tulach # Date 1355685078 -3600 # Node ID 6949044415df81de34179efd7344ee29dcc05180 # Parent 7328b7ba2fa4f37856a56a5dbb3374c37878bdd0 First steps towards execution harness diff -r 7328b7ba2fa4 -r 6949044415df launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Sat Dec 15 22:46:26 2012 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Sun Dec 16 20:11:18 2012 +0100 @@ -44,8 +44,11 @@ final ClassLoader loader = Bck2BrwsrLauncher.class.getClassLoader(); final ServerConfiguration conf = server.getServerConfiguration(); - conf.addHttpHandler(new Console("org.apidesign.bck2brwsr.launcher.Console", "welcome", "false"), "/console"); + conf.addHttpHandler(new Page("console.xhtml", + "org.apidesign.bck2brwsr.launcher.Console", "welcome", "false" + ), "/console"); conf.addHttpHandler(new VM(loader), "/bck2brwsr.js"); + conf.addHttpHandler(new VMInit(), "/vm.js"); conf.addHttpHandler(new Classes(loader), "/classes/"); conf.addHttpHandler(new HttpHandler() { @Override @@ -62,13 +65,25 @@ InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml"); copyStream(is, os, clazz, method, "true"); } - }, "/execute"); + }, "/"); + conf.addHttpHandler(new HttpHandler() { + int cnt; + @Override + public void service(Request request, Response response) throws Exception { + response.getWriter().write("{" + + "className: 'org.apidesign.bck2brwsr.launcher.Console'," + + "methodName: 'welcome'," + + "request: " + cnt + + "}"); + } + }, "execute/data"); + conf.addHttpHandler(new Page("harness.xhtml"), "/execute"); server.start(); NetworkListener listener = server.getListeners().iterator().next(); int port = listener.getPort(); - URI uri = new URI("http://localhost:" + port + "/execute?class=org.apidesign.bck2brwsr.launcher.Console&method=welcome"); + URI uri = new URI("http://localhost:" + port + "/execute"); try { Desktop.getDesktop().browse(uri); } catch (UnsupportedOperationException ex) { @@ -98,10 +113,12 @@ } } - private static class Console extends HttpHandler { + private static class Page extends HttpHandler { + private final String resource; private final String[] args; - public Console(String... args) { + public Page(String resource, String... args) { + this.resource = resource; this.args = args; } @@ -109,7 +126,7 @@ public void service(Request request, Response response) throws Exception { response.setContentType("text/html"); OutputStream os = response.getOutputStream(); - InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml"); + InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream(resource); copyStream(is, os, args); } } @@ -128,6 +145,25 @@ Bck2Brwsr.generate(response.getWriter(), loader); } } + private static class VMInit extends HttpHandler { + public VMInit() { + } + + @Override + public void service(Request request, Response response) throws Exception { + response.setCharacterEncoding("UTF-8"); + response.setContentType("text/javascript"); + response.getWriter().append( + "function ldCls(res) {\n" + + " var request = new XMLHttpRequest();\n" + + " request.open('GET', 'classes/' + res, false);\n" + + " request.send();\n" + + " var arr = eval('(' + request.responseText + ')');\n" + + " return arr;\n" + + "}\n" + + "var vm = new bck2brwsr(ldCls);\n"); + } + } private static class Classes extends HttpHandler { private final ClassLoader loader; diff -r 7328b7ba2fa4 -r 6949044415df launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Sat Dec 15 22:46:26 2012 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Sun Dec 16 20:11:18 2012 +0100 @@ -17,7 +17,10 @@ */ package org.apidesign.bck2brwsr.launcher; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.URL; import org.apidesign.bck2brwsr.core.JavaScriptBody; /** @@ -45,7 +48,23 @@ public static void execute() throws Exception { String clazz = (String) getAttr("clazz", "value"); String method = (String) getAttr("method", "value"); + Object res = invokeMethod(clazz, method); + setAttr("result", "value", res); + } + + public static void harness() { + try { + URL u = new URL("/execute/data"); + String data = (String) u.getContent(new Class[] { String.class }); + setAttr("result", "value", data); + } catch (Exception ex) { + setAttr("result", "value", ex.getMessage()); + } + } + private static Object invokeMethod(String clazz, String method) + throws ClassNotFoundException, InvocationTargetException, + SecurityException, IllegalAccessException, IllegalArgumentException { Method found = null; Class c = Class.forName(clazz); for (Method m : c.getMethods()) { @@ -59,7 +78,6 @@ } else { res = "Can't find method " + method + " in " + clazz; } - - setAttr("result", "value", res); + return res; } } diff -r 7328b7ba2fa4 -r 6949044415df launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/console.xhtml --- a/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/console.xhtml Sat Dec 15 22:46:26 2012 +0100 +++ b/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/console.xhtml Sun Dec 16 20:11:18 2012 +0100 @@ -24,17 +24,9 @@ Bck2Brwsr Launcher - - + + +

Bck2Browser Console Launcher

Class Name: diff -r 7328b7ba2fa4 -r 6949044415df launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml Sun Dec 16 20:11:18 2012 +0100 @@ -0,0 +1,39 @@ + + + + + + Bck2Brwsr Harness + + + + + +

Bck2Browser Execution Harness

+ + + + + +