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();
37 public static String welcome() {
38 return "HellofromBck2Brwsr";
40 public static String multiply() {
41 return String.valueOf(Integer.MAX_VALUE / 2 + Integer.MAX_VALUE);
44 @JavaScriptBody(args = {"id", "attr"}, body =
45 "return window.document.getElementById(id)[attr].toString();")
46 private static native Object getAttr(String id, String attr);
48 @JavaScriptBody(args = {"id", "attr", "value"}, body =
49 "window.document.getElementById(id)[attr] = value;")
50 private static native void setAttr(String id, String attr, Object value);
52 @JavaScriptBody(args = {}, body = "return; window.close();")
53 private static native void closeWindow();
55 private static void log(String newText) {
56 String id = "bck2brwsr.result";
57 String attr = "value";
58 setAttr(id, attr, getAttr(id, attr) + "\n" + newText);
59 setAttr(id, "scrollTop", getAttr(id, "scrollHeight"));
62 public static void execute() throws Exception {
63 String clazz = (String) getAttr("clazz", "value");
64 String method = (String) getAttr("method", "value");
65 Object res = invokeMethod(clazz, method);
66 setAttr("bck2brwsr.result", "value", res);
69 @JavaScriptBody(args = { "url", "callback", "arr" }, body = ""
70 + "var request = new XMLHttpRequest();\n"
71 + "request.open('GET', url, true);\n"
72 + "request.onreadystatechange = function() {\n"
73 + " if (this.readyState!==4) return;\n"
74 + " arr[0] = this.responseText;\n"
75 + " callback.run__V();\n"
79 private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
81 public static void harness(String url) throws IOException {
82 log("Connecting to " + url);
83 Request r = new Request(url);
86 private static class Request implements Runnable {
87 private final String[] arr = { null };
88 private final String url;
90 private Request(String url) throws IOException {
92 loadText(url, this, arr);
99 log("\nGot \"" + data + "\"");
102 log("Some error exiting");
107 if (data.isEmpty()) {
108 log("No data, exiting");
113 Case c = Case.parseData(data);
114 if (c.getHtmlFragment() != null) {
115 setAttr("bck2brwsr.fragment", "innerHTML", c.getHtmlFragment());
117 log("Invoking " + c.getClassName() + '.' + c.getMethodName() + " as request: " + c.getRequestId());
119 Object result = invokeMethod(c.getClassName(), c.getMethodName());
121 setAttr("bck2brwsr.fragment", "innerHTML", "");
122 log("Result: " + result);
124 result = encodeURL("" + result);
126 log("Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + result);
127 String u = url + "?request=" + c.getRequestId() + "&result=" + result;
129 loadText(u, this, arr);
131 } catch (Exception ex) {
132 log(ex.getMessage());
137 private static String encodeURL(String r) {
138 StringBuilder sb = new StringBuilder();
139 for (int i = 0; i < r.length(); i++) {
140 int ch = r.charAt(i);
141 if (ch < 32 || ch == '%' || ch == '+') {
142 sb.append("%").append(("0" + Integer.toHexString(ch)).substring(0, 2));
151 return sb.toString();
154 static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
155 final Object r = invokeMethod(clazz, method);
156 return r == null ? "null" : r.toString().toString();
159 /** Helper method that inspects the classpath and loads given resource
160 * (usually a class file). Used while running tests in Rhino.
162 * @param name resource name to find
163 * @return the array of bytes in the given resource
164 * @throws IOException I/O in case something goes wrong
166 public static byte[] read(String name) throws IOException {
168 Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
169 while (en.hasMoreElements()) {
170 u = en.nextElement();
173 throw new IOException("Can't find " + name);
175 try (InputStream is = u.openStream()) {
177 arr = new byte[is.available()];
179 while (offset < arr.length) {
180 int len = is.read(arr, offset, arr.length - offset);
182 throw new IOException("Can't read " + name);
190 private static Object invokeMethod(String clazz, String method)
191 throws ClassNotFoundException, InvocationTargetException,
192 SecurityException, IllegalAccessException, IllegalArgumentException,
193 InstantiationException {
195 Class<?> c = Class.forName(clazz);
196 for (Method m : c.getMethods()) {
197 if (m.getName().equals(method)) {
204 if ((found.getModifiers() & Modifier.STATIC) != 0) {
205 res = found.invoke(null);
207 res = found.invoke(c.newInstance());
209 } catch (Exception | Error ex) {
210 res = ex.getClass().getName() + ":" + ex.getMessage();
213 res = "Can't find method " + method + " in " + clazz;
218 @JavaScriptBody(args = {}, body = "vm.desiredAssertionStatus = true;")
219 private static void turnAssetionStatusOn() {
222 private static final class Case {
223 private final Object data;
225 private Case(Object data) {
229 public static Case parseData(String s) {
230 return new Case(toJSON(s));
233 public String getMethodName() {
234 return value("methodName", data);
237 public String getClassName() {
238 return value("className", data);
241 public String getRequestId() {
242 return value("request", data);
245 public String getHtmlFragment() {
246 return value("html", data);
249 @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
250 private static native Object toJSON(String s);
252 @JavaScriptBody(args = {"p", "d"}, body =
254 + "if (typeof v === 'undefined') return null;\n"
255 + "return v.toString();"
257 private static native String value(String p, Object d);