1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Sun Dec 16 20:11:18 2012 +0100
1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Mon Dec 17 11:58:48 2012 +0100
1.3 @@ -17,7 +17,6 @@
1.4 */
1.5 package org.apidesign.bck2brwsr.launcher;
1.6
1.7 -import java.io.InputStream;
1.8 import java.lang.reflect.InvocationTargetException;
1.9 import java.lang.reflect.Method;
1.10 import java.net.URL;
1.11 @@ -29,13 +28,11 @@
1.12 */
1.13 public class Console {
1.14 public static String welcome() {
1.15 - final String msg = "Hello from Bck2Brwsr!";
1.16 - alert(msg);
1.17 - return msg;
1.18 + return "HellofromBck2Brwsr";
1.19 }
1.20 -
1.21 - @JavaScriptBody(args = "msg", body = "alert(msg);")
1.22 - private static native void alert(String msg);
1.23 + public static String multiply() {
1.24 + return String.valueOf(Integer.MAX_VALUE / 2 + Integer.MAX_VALUE);
1.25 + }
1.26
1.27 @JavaScriptBody(args = {"id", "attr"}, body =
1.28 "return window.document.getElementById(id)[attr].toString();")
1.29 @@ -44,6 +41,10 @@
1.30 @JavaScriptBody(args = {"id", "attr", "value"}, body =
1.31 "window.document.getElementById(id)[attr] = value;")
1.32 private static native void setAttr(String id, String attr, Object value);
1.33 +
1.34 + private static void addAttr(String id, String attr, String newText) {
1.35 + setAttr(id, attr, getAttr(id, attr) + "\n" + newText);
1.36 + }
1.37
1.38 public static void execute() throws Exception {
1.39 String clazz = (String) getAttr("clazz", "value");
1.40 @@ -52,13 +53,41 @@
1.41 setAttr("result", "value", res);
1.42 }
1.43
1.44 - public static void harness() {
1.45 + public static void harness(String url) {
1.46 + setAttr("result", "value", "Connecting to " + url);
1.47 try {
1.48 - URL u = new URL("/execute/data");
1.49 - String data = (String) u.getContent(new Class[] { String.class });
1.50 - setAttr("result", "value", data);
1.51 + URL u = new URL(url);
1.52 + for (;;) {
1.53 + String data = (String) u.getContent(new Class[] { String.class });
1.54 + addAttr("result", "value", data);
1.55 + if (data.isEmpty()) {
1.56 + addAttr("result", "value", "No data, exiting");
1.57 + break;
1.58 + }
1.59 +
1.60 + Case c = Case.parseData(data);
1.61 + addAttr("result", "value", "className: " + c.getClassName());
1.62 + addAttr("result", "value", "methodName: " + c.getMethodName());
1.63 + addAttr("result", "value", "request: " + c.getRequestId());
1.64 +
1.65 + Object result = invokeMethod(c.getClassName(), c.getMethodName());
1.66 +
1.67 + addAttr("result", "value", "result: " + result);
1.68 +
1.69 + String toSend;
1.70 + if (result == null) {
1.71 + toSend = "null";
1.72 + } else {
1.73 + toSend = result.toString();
1.74 + }
1.75 +
1.76 + addAttr("result", "value", "Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + toSend);
1.77 + u = new URL(url + "?request=" + c.getRequestId() + "&result=" + toSend);
1.78 + }
1.79 +
1.80 +
1.81 } catch (Exception ex) {
1.82 - setAttr("result", "value", ex.getMessage());
1.83 + addAttr("result", "value", ex.getMessage());
1.84 }
1.85 }
1.86
1.87 @@ -80,4 +109,34 @@
1.88 }
1.89 return res;
1.90 }
1.91 +
1.92 + private static final class Case {
1.93 + private final Object data;
1.94 +
1.95 + private Case(Object data) {
1.96 + this.data = data;
1.97 + }
1.98 +
1.99 + public static Case parseData(String s) {
1.100 + return new Case(toJSON(s));
1.101 + }
1.102 +
1.103 + public String getMethodName() {
1.104 + return value("methodName", data);
1.105 + }
1.106 +
1.107 + public String getClassName() {
1.108 + return value("className", data);
1.109 + }
1.110 +
1.111 + public String getRequestId() {
1.112 + return value("request", data);
1.113 + }
1.114 +
1.115 + @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
1.116 + private static native Object toJSON(String s);
1.117 +
1.118 + @JavaScriptBody(args = {"p", "d"}, body = "return d[p].toString();")
1.119 + private static native String value(String p, Object d);
1.120 + }
1.121 }