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