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