diff -r 6949044415df -r 60f9aad12731 launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Sun Dec 16 20:11:18 2012 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Mon Dec 17 11:58:48 2012 +0100 @@ -17,7 +17,6 @@ */ package org.apidesign.bck2brwsr.launcher; -import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; @@ -29,13 +28,11 @@ */ public class Console { public static String welcome() { - final String msg = "Hello from Bck2Brwsr!"; - alert(msg); - return msg; + return "HellofromBck2Brwsr"; } - - @JavaScriptBody(args = "msg", body = "alert(msg);") - private static native void alert(String msg); + public static String multiply() { + return String.valueOf(Integer.MAX_VALUE / 2 + Integer.MAX_VALUE); + } @JavaScriptBody(args = {"id", "attr"}, body = "return window.document.getElementById(id)[attr].toString();") @@ -44,6 +41,10 @@ @JavaScriptBody(args = {"id", "attr", "value"}, body = "window.document.getElementById(id)[attr] = value;") private static native void setAttr(String id, String attr, Object value); + + private static void addAttr(String id, String attr, String newText) { + setAttr(id, attr, getAttr(id, attr) + "\n" + newText); + } public static void execute() throws Exception { String clazz = (String) getAttr("clazz", "value"); @@ -52,13 +53,41 @@ setAttr("result", "value", res); } - public static void harness() { + public static void harness(String url) { + setAttr("result", "value", "Connecting to " + url); try { - URL u = new URL("/execute/data"); - String data = (String) u.getContent(new Class[] { String.class }); - setAttr("result", "value", data); + URL u = new URL(url); + for (;;) { + String data = (String) u.getContent(new Class[] { String.class }); + addAttr("result", "value", data); + if (data.isEmpty()) { + addAttr("result", "value", "No data, exiting"); + break; + } + + Case c = Case.parseData(data); + addAttr("result", "value", "className: " + c.getClassName()); + addAttr("result", "value", "methodName: " + c.getMethodName()); + addAttr("result", "value", "request: " + c.getRequestId()); + + Object result = invokeMethod(c.getClassName(), c.getMethodName()); + + addAttr("result", "value", "result: " + result); + + String toSend; + if (result == null) { + toSend = "null"; + } else { + toSend = result.toString(); + } + + addAttr("result", "value", "Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + toSend); + u = new URL(url + "?request=" + c.getRequestId() + "&result=" + toSend); + } + + } catch (Exception ex) { - setAttr("result", "value", ex.getMessage()); + addAttr("result", "value", ex.getMessage()); } } @@ -80,4 +109,34 @@ } return res; } + + private static final class Case { + private final Object data; + + private Case(Object data) { + this.data = data; + } + + public static Case parseData(String s) { + return new Case(toJSON(s)); + } + + public String getMethodName() { + return value("methodName", data); + } + + public String getClassName() { + return value("className", data); + } + + public String getRequestId() { + return value("request", data); + } + + @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');") + private static native Object toJSON(String s); + + @JavaScriptBody(args = {"p", "d"}, body = "return d[p].toString();") + private static native String value(String p, Object d); + } }