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;
25 import java.util.Enumeration;
26 import org.apidesign.bck2brwsr.core.JavaScriptBody;
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 public class Console {
33 public static String welcome() {
34 return "HellofromBck2Brwsr";
36 public static String multiply() {
37 return String.valueOf(Integer.MAX_VALUE / 2 + Integer.MAX_VALUE);
40 @JavaScriptBody(args = {"id", "attr"}, body =
41 "return window.document.getElementById(id)[attr].toString();")
42 private static native Object getAttr(String id, String attr);
44 @JavaScriptBody(args = {"id", "attr", "value"}, body =
45 "window.document.getElementById(id)[attr] = value;")
46 private static native void setAttr(String id, String attr, Object value);
48 private static void log(String newText) {
50 String attr = "value";
51 setAttr(id, attr, getAttr(id, attr) + "\n" + newText);
52 setAttr(id, "scrollTop", getAttr(id, "scrollHeight"));
55 public static void execute() throws Exception {
56 String clazz = (String) getAttr("clazz", "value");
57 String method = (String) getAttr("method", "value");
58 Object res = invokeMethod(clazz, method);
59 setAttr("result", "value", res);
62 public static void harness(String url) {
63 log("Connecting to " + url);
67 String data = (String) u.getContent(new Class[] { String.class });
68 log("\nGot \"" + data + "\"");
70 log("No data, exiting");
74 Case c = Case.parseData(data);
75 log("Invoking " + c.getClassName() + '.' + c.getMethodName() + " as request: " + c.getRequestId());
77 Object result = invokeMethod(c.getClassName(), c.getMethodName());
79 log("Result: " + result);
80 log("Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + result);
81 u = new URL(url + "?request=" + c.getRequestId() + "&result=" + result);
85 } catch (Exception ex) {
90 static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException {
91 final Object r = invokeMethod(clazz, method);
92 return r == null ? "null" : r.toString().toString();
95 /** Helper method that inspects the classpath and loads given resource
96 * (usually a class file). Used while running tests in Rhino.
98 * @param name resource name to find
99 * @return the array of bytes in the given resource
100 * @throws IOException I/O in case something goes wrong
102 public static byte[] read(String name) throws IOException {
104 Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
105 while (en.hasMoreElements()) {
106 u = en.nextElement();
109 throw new IOException("Can't find " + name);
111 try (InputStream is = u.openStream()) {
113 arr = new byte[is.available()];
115 while (offset < arr.length) {
116 int len = is.read(arr, offset, arr.length - offset);
118 throw new IOException("Can't read " + name);
126 private static Object invokeMethod(String clazz, String method)
127 throws ClassNotFoundException, InvocationTargetException,
128 SecurityException, IllegalAccessException, IllegalArgumentException {
130 Class<?> c = Class.forName(clazz);
131 for (Method m : c.getMethods()) {
132 if (m.getName().equals(method)) {
138 res = found.invoke(null);
140 res = "Can't find method " + method + " in " + clazz;
145 private static final class Case {
146 private final Object data;
148 private Case(Object data) {
152 public static Case parseData(String s) {
153 return new Case(toJSON(s));
156 public String getMethodName() {
157 return value("methodName", data);
160 public String getClassName() {
161 return value("className", data);
164 public String getRequestId() {
165 return value("request", data);
168 @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
169 private static native Object toJSON(String s);
171 @JavaScriptBody(args = {"p", "d"}, body = "return d[p].toString();")
172 private static native String value(String p, Object d);