1.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Fri Apr 26 15:09:59 2013 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,356 +0,0 @@
1.4 -/**
1.5 - * Back 2 Browser Bytecode Translator
1.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, version 2 of the License.
1.11 - *
1.12 - * This program is distributed in the hope that it will be useful,
1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 - * GNU General Public License for more details.
1.16 - *
1.17 - * You should have received a copy of the GNU General Public License
1.18 - * along with this program. Look for COPYING file in the top folder.
1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 - */
1.21 -package org.apidesign.bck2brwsr.launcher.impl;
1.22 -
1.23 -import java.io.IOException;
1.24 -import java.io.InputStream;
1.25 -import java.io.UnsupportedEncodingException;
1.26 -import java.lang.reflect.InvocationTargetException;
1.27 -import java.lang.reflect.Method;
1.28 -import java.lang.reflect.Modifier;
1.29 -import java.net.URL;
1.30 -import java.util.Enumeration;
1.31 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.32 -
1.33 -/**
1.34 - *
1.35 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.36 - */
1.37 -public class Console {
1.38 - private Console() {
1.39 - }
1.40 - static {
1.41 - turnAssetionStatusOn();
1.42 - }
1.43 -
1.44 - @JavaScriptBody(args = {"id", "attr"}, body =
1.45 - "return window.document.getElementById(id)[attr].toString();")
1.46 - private static native Object getAttr(String id, String attr);
1.47 - @JavaScriptBody(args = {"elem", "attr"}, body =
1.48 - "return elem[attr].toString();")
1.49 - private static native Object getAttr(Object elem, String attr);
1.50 -
1.51 - @JavaScriptBody(args = {"id", "attr", "value"}, body =
1.52 - "window.document.getElementById(id)[attr] = value;")
1.53 - private static native void setAttr(String id, String attr, Object value);
1.54 - @JavaScriptBody(args = {"elem", "attr", "value"}, body =
1.55 - "elem[attr] = value;")
1.56 - private static native void setAttr(Object id, String attr, Object value);
1.57 -
1.58 - @JavaScriptBody(args = {}, body = "return; window.close();")
1.59 - private static native void closeWindow();
1.60 -
1.61 - private static Object textArea;
1.62 - private static Object statusArea;
1.63 -
1.64 - private static void log(String newText) {
1.65 - if (textArea == null) {
1.66 - return;
1.67 - }
1.68 - String attr = "value";
1.69 - setAttr(textArea, attr, getAttr(textArea, attr) + "\n" + newText);
1.70 - setAttr(textArea, "scrollTop", getAttr(textArea, "scrollHeight"));
1.71 - }
1.72 -
1.73 - private static void beginTest(Case c) {
1.74 - Object[] arr = new Object[2];
1.75 - beginTest(c.getClassName() + "." + c.getMethodName(), c, arr);
1.76 - textArea = arr[0];
1.77 - statusArea = arr[1];
1.78 - }
1.79 -
1.80 - private static void finishTest(Case c, Object res) {
1.81 - if ("null".equals(res)) {
1.82 - setAttr(statusArea, "innerHTML", "Success");
1.83 - } else {
1.84 - setAttr(statusArea, "innerHTML", "Result " + res);
1.85 - }
1.86 - statusArea = null;
1.87 - textArea = null;
1.88 - }
1.89 -
1.90 - @JavaScriptBody(args = { "test", "c", "arr" }, body =
1.91 - "var ul = window.document.getElementById('bck2brwsr.result');\n"
1.92 - + "var li = window.document.createElement('li');\n"
1.93 - + "var span = window.document.createElement('span');"
1.94 - + "span.innerHTML = test + ' - ';\n"
1.95 - + "var details = window.document.createElement('a');\n"
1.96 - + "details.innerHTML = 'Details';\n"
1.97 - + "details.href = '#';\n"
1.98 - + "var p = window.document.createElement('p');\n"
1.99 - + "var status = window.document.createElement('a');\n"
1.100 - + "status.innerHTML = 'running';"
1.101 - + "details.onclick = function() { li.appendChild(p); li.removeChild(details); status.innerHTML = 'Run Again'; status.href = '#'; };\n"
1.102 - + "status.onclick = function() { c.again__V_3Ljava_lang_Object_2(arr); }\n"
1.103 - + "var pre = window.document.createElement('textarea');\n"
1.104 - + "pre.cols = 100;"
1.105 - + "pre.rows = 10;"
1.106 - + "li.appendChild(span);\n"
1.107 - + "li.appendChild(status);\n"
1.108 - + "var span = window.document.createElement('span');"
1.109 - + "span.innerHTML = ' ';\n"
1.110 - + "li.appendChild(span);\n"
1.111 - + "li.appendChild(details);\n"
1.112 - + "p.appendChild(pre);\n"
1.113 - + "ul.appendChild(li);\n"
1.114 - + "arr[0] = pre;\n"
1.115 - + "arr[1] = status;\n"
1.116 - )
1.117 - private static native void beginTest(String test, Case c, Object[] arr);
1.118 -
1.119 - @JavaScriptBody(args = { "url", "callback", "arr" }, body = ""
1.120 - + "var request = new XMLHttpRequest();\n"
1.121 - + "request.open('GET', url, true);\n"
1.122 - + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n"
1.123 - + "request.onreadystatechange = function() {\n"
1.124 - + " if (this.readyState!==4) return;\n"
1.125 - + " arr[0] = this.responseText;\n"
1.126 - + " callback.run__V();\n"
1.127 - + "};"
1.128 - + "request.send();"
1.129 - )
1.130 - private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
1.131 -
1.132 - public static void harness(String url) throws IOException {
1.133 - log("Connecting to " + url);
1.134 - Request r = new Request(url);
1.135 - }
1.136 -
1.137 - private static class Request implements Runnable {
1.138 - private final String[] arr = { null };
1.139 - private final String url;
1.140 - private Case c;
1.141 - private int retries;
1.142 -
1.143 - private Request(String url) throws IOException {
1.144 - this.url = url;
1.145 - loadText(url, this, arr);
1.146 - }
1.147 - private Request(String url, String u) throws IOException {
1.148 - this.url = url;
1.149 - loadText(u, this, arr);
1.150 - }
1.151 -
1.152 - @Override
1.153 - public void run() {
1.154 - try {
1.155 - if (c == null) {
1.156 - String data = arr[0];
1.157 -
1.158 - if (data == null) {
1.159 - log("Some error exiting");
1.160 - closeWindow();
1.161 - return;
1.162 - }
1.163 -
1.164 - if (data.isEmpty()) {
1.165 - log("No data, exiting");
1.166 - closeWindow();
1.167 - return;
1.168 - }
1.169 -
1.170 - c = Case.parseData(data);
1.171 - beginTest(c);
1.172 - log("Got \"" + data + "\"");
1.173 - } else {
1.174 - log("Processing \"" + arr[0] + "\" for " + retries + " time");
1.175 - }
1.176 - Object result = retries++ >= 10 ? "java.lang.InterruptedException:timeout" : c.runTest();
1.177 - finishTest(c, result);
1.178 -
1.179 - String u = url + "?request=" + c.getRequestId() + "&result=" + result;
1.180 - new Request(url, u);
1.181 - } catch (Exception ex) {
1.182 - if (ex instanceof InterruptedException) {
1.183 - log("Re-scheduling in 100ms");
1.184 - schedule(this, 100);
1.185 - return;
1.186 - }
1.187 - log(ex.getClass().getName() + ":" + ex.getMessage());
1.188 - }
1.189 - }
1.190 - }
1.191 -
1.192 - private static String encodeURL(String r) throws UnsupportedEncodingException {
1.193 - final String SPECIAL = "%$&+,/:;=?@";
1.194 - StringBuilder sb = new StringBuilder();
1.195 - byte[] utf8 = r.getBytes("UTF-8");
1.196 - for (int i = 0; i < utf8.length; i++) {
1.197 - int ch = utf8[i] & 0xff;
1.198 - if (ch < 32 || ch > 127 || SPECIAL.indexOf(ch) >= 0) {
1.199 - final String numbers = "0" + Integer.toHexString(ch);
1.200 - sb.append("%").append(numbers.substring(numbers.length() - 2));
1.201 - } else {
1.202 - if (ch == 32) {
1.203 - sb.append("+");
1.204 - } else {
1.205 - sb.append((char)ch);
1.206 - }
1.207 - }
1.208 - }
1.209 - return sb.toString();
1.210 - }
1.211 -
1.212 - static String invoke(String clazz, String method) throws
1.213 - ClassNotFoundException, InvocationTargetException, IllegalAccessException,
1.214 - InstantiationException, InterruptedException {
1.215 - final Object r = new Case(null).invokeMethod(clazz, method);
1.216 - return r == null ? "null" : r.toString().toString();
1.217 - }
1.218 -
1.219 - /** Helper method that inspects the classpath and loads given resource
1.220 - * (usually a class file). Used while running tests in Rhino.
1.221 - *
1.222 - * @param name resource name to find
1.223 - * @return the array of bytes in the given resource
1.224 - * @throws IOException I/O in case something goes wrong
1.225 - */
1.226 - public static byte[] read(String name) throws IOException {
1.227 - URL u = null;
1.228 - Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
1.229 - while (en.hasMoreElements()) {
1.230 - u = en.nextElement();
1.231 - }
1.232 - if (u == null) {
1.233 - throw new IOException("Can't find " + name);
1.234 - }
1.235 - try (InputStream is = u.openStream()) {
1.236 - byte[] arr;
1.237 - arr = new byte[is.available()];
1.238 - int offset = 0;
1.239 - while (offset < arr.length) {
1.240 - int len = is.read(arr, offset, arr.length - offset);
1.241 - if (len == -1) {
1.242 - throw new IOException("Can't read " + name);
1.243 - }
1.244 - offset += len;
1.245 - }
1.246 - return arr;
1.247 - }
1.248 - }
1.249 -
1.250 - @JavaScriptBody(args = {}, body = "vm.desiredAssertionStatus = true;")
1.251 - private static void turnAssetionStatusOn() {
1.252 - }
1.253 -
1.254 - @JavaScriptBody(args = {"r", "time"}, body =
1.255 - "return window.setTimeout(function() { r.run__V(); }, time);")
1.256 - private static native Object schedule(Runnable r, int time);
1.257 -
1.258 - private static final class Case {
1.259 - private final Object data;
1.260 - private Object inst;
1.261 -
1.262 - private Case(Object data) {
1.263 - this.data = data;
1.264 - }
1.265 -
1.266 - public static Case parseData(String s) {
1.267 - return new Case(toJSON(s));
1.268 - }
1.269 -
1.270 - public String getMethodName() {
1.271 - return value("methodName", data);
1.272 - }
1.273 -
1.274 - public String getClassName() {
1.275 - return value("className", data);
1.276 - }
1.277 -
1.278 - public String getRequestId() {
1.279 - return value("request", data);
1.280 - }
1.281 -
1.282 - public String getHtmlFragment() {
1.283 - return value("html", data);
1.284 - }
1.285 -
1.286 - void again(Object[] arr) {
1.287 - try {
1.288 - textArea = arr[0];
1.289 - statusArea = arr[1];
1.290 - setAttr(textArea, "value", "");
1.291 - runTest();
1.292 - } catch (Exception ex) {
1.293 - log(ex.getClass().getName() + ":" + ex.getMessage());
1.294 - }
1.295 - }
1.296 -
1.297 - private Object runTest() throws IllegalAccessException,
1.298 - IllegalArgumentException, ClassNotFoundException, UnsupportedEncodingException,
1.299 - InvocationTargetException, InstantiationException, InterruptedException {
1.300 - if (this.getHtmlFragment() != null) {
1.301 - setAttr("bck2brwsr.fragment", "innerHTML", this.getHtmlFragment());
1.302 - }
1.303 - log("Invoking " + this.getClassName() + '.' + this.getMethodName() + " as request: " + this.getRequestId());
1.304 - Object result = invokeMethod(this.getClassName(), this.getMethodName());
1.305 - setAttr("bck2brwsr.fragment", "innerHTML", "");
1.306 - log("Result: " + result);
1.307 - result = encodeURL("" + result);
1.308 - log("Sending back: ...?request=" + this.getRequestId() + "&result=" + result);
1.309 - return result;
1.310 - }
1.311 -
1.312 - private Object invokeMethod(String clazz, String method)
1.313 - throws ClassNotFoundException, InvocationTargetException,
1.314 - InterruptedException, IllegalAccessException, IllegalArgumentException,
1.315 - InstantiationException {
1.316 - Method found = null;
1.317 - Class<?> c = Class.forName(clazz);
1.318 - for (Method m : c.getMethods()) {
1.319 - if (m.getName().equals(method)) {
1.320 - found = m;
1.321 - }
1.322 - }
1.323 - Object res;
1.324 - if (found != null) {
1.325 - try {
1.326 - if ((found.getModifiers() & Modifier.STATIC) != 0) {
1.327 - res = found.invoke(null);
1.328 - } else {
1.329 - if (inst == null) {
1.330 - inst = c.newInstance();
1.331 - }
1.332 - res = found.invoke(inst);
1.333 - }
1.334 - } catch (Throwable ex) {
1.335 - if (ex instanceof InvocationTargetException) {
1.336 - ex = ((InvocationTargetException) ex).getTargetException();
1.337 - }
1.338 - if (ex instanceof InterruptedException) {
1.339 - throw (InterruptedException)ex;
1.340 - }
1.341 - res = ex.getClass().getName() + ":" + ex.getMessage();
1.342 - }
1.343 - } else {
1.344 - res = "Can't find method " + method + " in " + clazz;
1.345 - }
1.346 - return res;
1.347 - }
1.348 -
1.349 - @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
1.350 - private static native Object toJSON(String s);
1.351 -
1.352 - @JavaScriptBody(args = {"p", "d"}, body =
1.353 - "var v = d[p];\n"
1.354 - + "if (typeof v === 'undefined') return null;\n"
1.355 - + "return v.toString();"
1.356 - )
1.357 - private static native String value(String p, Object d);
1.358 - }
1.359 -}