2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.launcher;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.lang.reflect.Modifier;
26 import java.util.Enumeration;
27 import org.apidesign.bck2brwsr.core.JavaScriptBody;
31 * @author Jaroslav Tulach <jtulach@netbeans.org>
33 public class Console {
35 turnAssetionStatusOn();
38 @JavaScriptBody(args = {"id", "attr"}, body =
39 "return window.document.getElementById(id)[attr].toString();")
40 private static native Object getAttr(String id, String attr);
42 @JavaScriptBody(args = {"id", "attr", "value"}, body =
43 "window.document.getElementById(id)[attr] = value;")
44 private static native void setAttr(String id, String attr, Object value);
46 @JavaScriptBody(args = {}, body = "return; window.close();")
47 private static native void closeWindow();
49 private static void log(String newText) {
50 String id = "bck2brwsr.result";
51 String attr = "value";
52 setAttr(id, attr, getAttr(id, attr) + "\n" + newText);
53 setAttr(id, "scrollTop", getAttr(id, "scrollHeight"));
56 public static void execute() throws Exception {
57 String clazz = (String) getAttr("clazz", "value");
58 String method = (String) getAttr("method", "value");
59 Object res = invokeMethod(clazz, method);
60 setAttr("bck2brwsr.result", "value", res);
63 @JavaScriptBody(args = { "url", "callback", "arr" }, body = ""
64 + "var request = new XMLHttpRequest();\n"
65 + "request.open('GET', url, true);\n"
66 + "request.onreadystatechange = function() {\n"
67 + " if (this.readyState!==4) return;\n"
68 + " arr[0] = this.responseText;\n"
69 + " callback.run__V();\n"
73 private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
75 public static void harness(String url) throws IOException {
76 log("Connecting to " + url);
77 Request r = new Request(url);
80 private static class Request implements Runnable {
81 private final String[] arr = { null };
82 private final String url;
84 private Request(String url) throws IOException {
86 loadText(url, this, arr);
93 log("\nGot \"" + data + "\"");
96 log("Some error exiting");
101 if (data.isEmpty()) {
102 log("No data, exiting");
107 Case c = Case.parseData(data);
108 if (c.getHtmlFragment() != null) {
109 setAttr("bck2brwsr.fragment", "innerHTML", c.getHtmlFragment());
111 log("Invoking " + c.getClassName() + '.' + c.getMethodName() + " as request: " + c.getRequestId());
113 Object result = invokeMethod(c.getClassName(), c.getMethodName());
115 setAttr("bck2brwsr.fragment", "innerHTML", "");
116 log("Result: " + result);
118 result = encodeURL("" + result);
120 log("Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + result);
121 String u = url + "?request=" + c.getRequestId() + "&result=" + result;
123 loadText(u, this, arr);
125 } catch (Exception ex) {
126 log(ex.getMessage());
131 private static String encodeURL(String r) {
132 StringBuilder sb = new StringBuilder();
133 for (int i = 0; i < r.length(); i++) {
134 int ch = r.charAt(i);
135 if (ch < 32 || ch == '%' || ch == '+') {
136 sb.append("%").append(("0" + Integer.toHexString(ch)).substring(0, 2));
145 return sb.toString();
148 static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
149 final Object r = invokeMethod(clazz, method);
150 return r == null ? "null" : r.toString().toString();
153 /** Helper method that inspects the classpath and loads given resource
154 * (usually a class file). Used while running tests in Rhino.
156 * @param name resource name to find
157 * @return the array of bytes in the given resource
158 * @throws IOException I/O in case something goes wrong
160 public static byte[] read(String name) throws IOException {
162 Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
163 while (en.hasMoreElements()) {
164 u = en.nextElement();
167 throw new IOException("Can't find " + name);
169 try (InputStream is = u.openStream()) {
171 arr = new byte[is.available()];
173 while (offset < arr.length) {
174 int len = is.read(arr, offset, arr.length - offset);
176 throw new IOException("Can't read " + name);
184 private static Object invokeMethod(String clazz, String method)
185 throws ClassNotFoundException, InvocationTargetException,
186 SecurityException, IllegalAccessException, IllegalArgumentException,
187 InstantiationException {
189 Class<?> c = Class.forName(clazz);
190 for (Method m : c.getMethods()) {
191 if (m.getName().equals(method)) {
198 if ((found.getModifiers() & Modifier.STATIC) != 0) {
199 res = found.invoke(null);
201 res = found.invoke(c.newInstance());
203 } catch (Throwable ex) {
204 res = ex.getClass().getName() + ":" + ex.getMessage();
207 res = "Can't find method " + method + " in " + clazz;
212 @JavaScriptBody(args = {}, body = "vm.desiredAssertionStatus = true;")
213 private static void turnAssetionStatusOn() {
216 private static final class Case {
217 private final Object data;
219 private Case(Object data) {
223 public static Case parseData(String s) {
224 return new Case(toJSON(s));
227 public String getMethodName() {
228 return value("methodName", data);
231 public String getClassName() {
232 return value("className", data);
235 public String getRequestId() {
236 return value("request", data);
239 public String getHtmlFragment() {
240 return value("html", data);
243 @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
244 private static native Object toJSON(String s);
246 @JavaScriptBody(args = {"p", "d"}, body =
248 + "if (typeof v === 'undefined') return null;\n"
249 + "return v.toString();"
251 private static native String value(String p, Object d);