1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Thu Jan 31 16:13:04 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,253 +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;
1.22 -
1.23 -import java.io.IOException;
1.24 -import java.io.InputStream;
1.25 -import java.lang.reflect.InvocationTargetException;
1.26 -import java.lang.reflect.Method;
1.27 -import java.lang.reflect.Modifier;
1.28 -import java.net.URL;
1.29 -import java.util.Enumeration;
1.30 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.31 -
1.32 -/**
1.33 - *
1.34 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.35 - */
1.36 -public class Console {
1.37 - static {
1.38 - turnAssetionStatusOn();
1.39 - }
1.40 -
1.41 - @JavaScriptBody(args = {"id", "attr"}, body =
1.42 - "return window.document.getElementById(id)[attr].toString();")
1.43 - private static native Object getAttr(String id, String attr);
1.44 -
1.45 - @JavaScriptBody(args = {"id", "attr", "value"}, body =
1.46 - "window.document.getElementById(id)[attr] = value;")
1.47 - private static native void setAttr(String id, String attr, Object value);
1.48 -
1.49 - @JavaScriptBody(args = {}, body = "return; window.close();")
1.50 - private static native void closeWindow();
1.51 -
1.52 - private static void log(String newText) {
1.53 - String id = "bck2brwsr.result";
1.54 - String attr = "value";
1.55 - setAttr(id, attr, getAttr(id, attr) + "\n" + newText);
1.56 - setAttr(id, "scrollTop", getAttr(id, "scrollHeight"));
1.57 - }
1.58 -
1.59 - public static void execute() throws Exception {
1.60 - String clazz = (String) getAttr("clazz", "value");
1.61 - String method = (String) getAttr("method", "value");
1.62 - Object res = invokeMethod(clazz, method);
1.63 - setAttr("bck2brwsr.result", "value", res);
1.64 - }
1.65 -
1.66 - @JavaScriptBody(args = { "url", "callback", "arr" }, body = ""
1.67 - + "var request = new XMLHttpRequest();\n"
1.68 - + "request.open('GET', url, true);\n"
1.69 - + "request.onreadystatechange = function() {\n"
1.70 - + " if (this.readyState!==4) return;\n"
1.71 - + " arr[0] = this.responseText;\n"
1.72 - + " callback.run__V();\n"
1.73 - + "};"
1.74 - + "request.send();"
1.75 - )
1.76 - private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
1.77 -
1.78 - public static void harness(String url) throws IOException {
1.79 - log("Connecting to " + url);
1.80 - Request r = new Request(url);
1.81 - }
1.82 -
1.83 - private static class Request implements Runnable {
1.84 - private final String[] arr = { null };
1.85 - private final String url;
1.86 -
1.87 - private Request(String url) throws IOException {
1.88 - this.url = url;
1.89 - loadText(url, this, arr);
1.90 - }
1.91 -
1.92 - @Override
1.93 - public void run() {
1.94 - try {
1.95 - String data = arr[0];
1.96 - log("\nGot \"" + data + "\"");
1.97 -
1.98 - if (data == null) {
1.99 - log("Some error exiting");
1.100 - closeWindow();
1.101 - return;
1.102 - }
1.103 -
1.104 - if (data.isEmpty()) {
1.105 - log("No data, exiting");
1.106 - closeWindow();
1.107 - return;
1.108 - }
1.109 -
1.110 - Case c = Case.parseData(data);
1.111 - if (c.getHtmlFragment() != null) {
1.112 - setAttr("bck2brwsr.fragment", "innerHTML", c.getHtmlFragment());
1.113 - }
1.114 - log("Invoking " + c.getClassName() + '.' + c.getMethodName() + " as request: " + c.getRequestId());
1.115 -
1.116 - Object result = invokeMethod(c.getClassName(), c.getMethodName());
1.117 -
1.118 - setAttr("bck2brwsr.fragment", "innerHTML", "");
1.119 - log("Result: " + result);
1.120 -
1.121 - result = encodeURL("" + result);
1.122 -
1.123 - log("Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + result);
1.124 - String u = url + "?request=" + c.getRequestId() + "&result=" + result;
1.125 -
1.126 - loadText(u, this, arr);
1.127 -
1.128 - } catch (Exception ex) {
1.129 - log(ex.getMessage());
1.130 - }
1.131 - }
1.132 - }
1.133 -
1.134 - private static String encodeURL(String r) {
1.135 - StringBuilder sb = new StringBuilder();
1.136 - for (int i = 0; i < r.length(); i++) {
1.137 - int ch = r.charAt(i);
1.138 - if (ch < 32 || ch == '%' || ch == '+') {
1.139 - sb.append("%").append(("0" + Integer.toHexString(ch)).substring(0, 2));
1.140 - } else {
1.141 - if (ch == 32) {
1.142 - sb.append("+");
1.143 - } else {
1.144 - sb.append((char)ch);
1.145 - }
1.146 - }
1.147 - }
1.148 - return sb.toString();
1.149 - }
1.150 -
1.151 - static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
1.152 - final Object r = invokeMethod(clazz, method);
1.153 - return r == null ? "null" : r.toString().toString();
1.154 - }
1.155 -
1.156 - /** Helper method that inspects the classpath and loads given resource
1.157 - * (usually a class file). Used while running tests in Rhino.
1.158 - *
1.159 - * @param name resource name to find
1.160 - * @return the array of bytes in the given resource
1.161 - * @throws IOException I/O in case something goes wrong
1.162 - */
1.163 - public static byte[] read(String name) throws IOException {
1.164 - URL u = null;
1.165 - Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
1.166 - while (en.hasMoreElements()) {
1.167 - u = en.nextElement();
1.168 - }
1.169 - if (u == null) {
1.170 - throw new IOException("Can't find " + name);
1.171 - }
1.172 - try (InputStream is = u.openStream()) {
1.173 - byte[] arr;
1.174 - arr = new byte[is.available()];
1.175 - int offset = 0;
1.176 - while (offset < arr.length) {
1.177 - int len = is.read(arr, offset, arr.length - offset);
1.178 - if (len == -1) {
1.179 - throw new IOException("Can't read " + name);
1.180 - }
1.181 - offset += len;
1.182 - }
1.183 - return arr;
1.184 - }
1.185 - }
1.186 -
1.187 - private static Object invokeMethod(String clazz, String method)
1.188 - throws ClassNotFoundException, InvocationTargetException,
1.189 - SecurityException, IllegalAccessException, IllegalArgumentException,
1.190 - InstantiationException {
1.191 - Method found = null;
1.192 - Class<?> c = Class.forName(clazz);
1.193 - for (Method m : c.getMethods()) {
1.194 - if (m.getName().equals(method)) {
1.195 - found = m;
1.196 - }
1.197 - }
1.198 - Object res;
1.199 - if (found != null) {
1.200 - try {
1.201 - if ((found.getModifiers() & Modifier.STATIC) != 0) {
1.202 - res = found.invoke(null);
1.203 - } else {
1.204 - res = found.invoke(c.newInstance());
1.205 - }
1.206 - } catch (Throwable ex) {
1.207 - res = ex.getClass().getName() + ":" + ex.getMessage();
1.208 - }
1.209 - } else {
1.210 - res = "Can't find method " + method + " in " + clazz;
1.211 - }
1.212 - return res;
1.213 - }
1.214 -
1.215 - @JavaScriptBody(args = {}, body = "vm.desiredAssertionStatus = true;")
1.216 - private static void turnAssetionStatusOn() {
1.217 - }
1.218 -
1.219 - private static final class Case {
1.220 - private final Object data;
1.221 -
1.222 - private Case(Object data) {
1.223 - this.data = data;
1.224 - }
1.225 -
1.226 - public static Case parseData(String s) {
1.227 - return new Case(toJSON(s));
1.228 - }
1.229 -
1.230 - public String getMethodName() {
1.231 - return value("methodName", data);
1.232 - }
1.233 -
1.234 - public String getClassName() {
1.235 - return value("className", data);
1.236 - }
1.237 -
1.238 - public String getRequestId() {
1.239 - return value("request", data);
1.240 - }
1.241 -
1.242 - public String getHtmlFragment() {
1.243 - return value("html", data);
1.244 - }
1.245 -
1.246 - @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
1.247 - private static native Object toJSON(String s);
1.248 -
1.249 - @JavaScriptBody(args = {"p", "d"}, body =
1.250 - "var v = d[p];\n"
1.251 - + "if (typeof v === 'undefined') return null;\n"
1.252 - + "return v.toString();"
1.253 - )
1.254 - private static native String value(String p, Object d);
1.255 - }
1.256 -}