# HG changeset patch # User Jaroslav Tulach # Date 1355741928 -3600 # Node ID 60f9aad1273181dcac6e9365ad4ccc5691e3554e # Parent 4928b51565b21d7e573bf66764dbfd1d63a7d2bf Using URL.getContent to communicate with the launcher and execute two testing methods diff -r 4928b51565b2 -r 60f9aad12731 emul/src/main/java/java/net/URL.java --- a/emul/src/main/java/java/net/URL.java Mon Dec 17 09:57:22 2012 +0100 +++ b/emul/src/main/java/java/net/URL.java Mon Dec 17 11:58:48 2012 +0100 @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.InputStream; +import org.apidesign.bck2brwsr.core.JavaScriptBody; /** * Class URL represents a Uniform Resource @@ -964,9 +965,16 @@ * @see java.net.URLConnection#getContent() */ public final Object getContent() throws java.io.IOException { - throw new IOException(); -// return openConnection().getContent(); + return loadText(toExternalForm()); } + + @JavaScriptBody(args = "url", body = "" + + "var request = new XMLHttpRequest();\n" + + "request.open('GET', url, false);\n" + + "request.send();\n" + + "return request.responseText;\n" + ) + private static native String loadText(String url) throws IOException; /** * Gets the contents of this URL. This method is a shorthand for: @@ -984,8 +992,12 @@ */ public final Object getContent(Class[] classes) throws java.io.IOException { - throw new IOException(); -// return openConnection().getContent(classes); + for (Class c : classes) { + if (c == String.class) { + return getContent(); + } + } + return null; } static URLStreamHandler getURLStreamHandler(String protocol) { diff -r 4928b51565b2 -r 60f9aad12731 launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Mon Dec 17 09:57:22 2012 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Mon Dec 17 11:58:48 2012 +0100 @@ -25,6 +25,7 @@ import java.net.URI; import java.net.URL; import java.util.Enumeration; +import java.util.concurrent.CountDownLatch; import static org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher.copyStream; import org.apidesign.vm4brwsr.Bck2Brwsr; import org.glassfish.grizzly.PortRange; @@ -40,6 +41,12 @@ */ public class Bck2BrwsrLauncher { public static void main( String[] args ) throws Exception { + final Case[] cases = { + new Case("org.apidesign.bck2brwsr.launcher.Console", "welcome"), + new Case("org.apidesign.bck2brwsr.launcher.Console", "multiply") + }; + final CountDownLatch wait = new CountDownLatch(1); + HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535)); final ClassLoader loader = Bck2BrwsrLauncher.class.getClassLoader(); @@ -70,13 +77,28 @@ int cnt; @Override public void service(Request request, Response response) throws Exception { + String id = request.getParameter("request"); + String value = request.getParameter("result"); + if (id != null && value != null) { + value = value.replace("%20", " "); + cases[Integer.parseInt(id)].result = value; + } + + if (cnt >= cases.length) { + response.getWriter().write(""); + wait.countDown(); + cnt = 0; + return; + } + response.getWriter().write("{" - + "className: 'org.apidesign.bck2brwsr.launcher.Console'," - + "methodName: 'welcome'," + + "className: '" + cases[cnt].className + "', " + + "methodName: '" + cases[cnt].methodName + "', " + "request: " + cnt + "}"); + cnt++; } - }, "execute/data"); + }, "/data"); conf.addHttpHandler(new Page("harness.xhtml"), "/execute"); server.start(); @@ -93,10 +115,14 @@ Runtime.getRuntime().exec(cmd).waitFor(); } - System.in.read(); + wait.await(); + + for (Case c : cases) { + System.err.println(c.className + "." + c.methodName + " = " + c.result); + } } - static void copyStream(InputStream is, OutputStream os, String... params) throws IOException { + static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException { for (;;) { int ch = is.read(); if (ch == -1) { @@ -104,6 +130,9 @@ } if (ch == '$') { int cnt = is.read() - '0'; + if (cnt == 'U' - '0') { + os.write(baseURL.getBytes()); + } if (cnt < params.length) { os.write(params[cnt].getBytes()); } @@ -127,7 +156,7 @@ response.setContentType("text/html"); OutputStream os = response.getOutputStream(); InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream(resource); - copyStream(is, os, args); + copyStream(is, os, request.getRequestURL().toString(), args); } } @@ -210,4 +239,15 @@ w.append("\n]"); } } + + private static final class Case { + final String className; + final String methodName; + String result; + + public Case(String className, String methodName) { + this.className = className; + this.methodName = methodName; + } + } } diff -r 4928b51565b2 -r 60f9aad12731 launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Mon Dec 17 09:57:22 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); + } } diff -r 4928b51565b2 -r 60f9aad12731 launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml --- a/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml Mon Dec 17 09:57:22 2012 +0100 +++ b/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml Mon Dec 17 11:58:48 2012 +0100 @@ -33,7 +33,7 @@