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 {
34 public static String welcome() {
35 return "HellofromBck2Brwsr";
37 public static String multiply() {
38 return String.valueOf(Integer.MAX_VALUE / 2 + Integer.MAX_VALUE);
41 @JavaScriptBody(args = {"id", "attr"}, body =
42 "return window.document.getElementById(id)[attr].toString();")
43 private static native Object getAttr(String id, String attr);
45 @JavaScriptBody(args = {"id", "attr", "value"}, body =
46 "window.document.getElementById(id)[attr] = value;")
47 private static native void setAttr(String id, String attr, Object value);
49 @JavaScriptBody(args = {}, body = "return; window.close();")
50 private static native void closeWindow();
52 private static void log(String newText) {
54 String attr = "value";
55 setAttr(id, attr, getAttr(id, attr) + "\n" + newText);
56 setAttr(id, "scrollTop", getAttr(id, "scrollHeight"));
59 public static void execute() throws Exception {
60 String clazz = (String) getAttr("clazz", "value");
61 String method = (String) getAttr("method", "value");
62 Object res = invokeMethod(clazz, method);
63 setAttr("result", "value", res);
66 public static void harness(String url) {
67 log("Connecting to " + url);
71 String data = (String) u.getContent(new Class[] { String.class });
72 log("\nGot \"" + data + "\"");
74 log("No data, exiting");
79 Case c = Case.parseData(data);
80 log("Invoking " + c.getClassName() + '.' + c.getMethodName() + " as request: " + c.getRequestId());
82 Object result = invokeMethod(c.getClassName(), c.getMethodName());
84 log("Result: " + result);
86 result = encodeURL("" + result);
88 log("Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + result);
89 u = new URL(url + "?request=" + c.getRequestId() + "&result=" + result);
93 } catch (Exception ex) {
98 private static String encodeURL(String r) {
99 StringBuilder sb = new StringBuilder();
100 for (int i = 0; i < r.length(); i++) {
101 int ch = r.charAt(i);
102 if (ch < 32 || ch == '%' || ch == '+') {
103 sb.append("%").append(("0" + Integer.toHexString(ch)).substring(0, 2));
112 return sb.toString();
115 static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
116 final Object r = invokeMethod(clazz, method);
117 return r == null ? "null" : r.toString().toString();
120 /** Helper method that inspects the classpath and loads given resource
121 * (usually a class file). Used while running tests in Rhino.
123 * @param name resource name to find
124 * @return the array of bytes in the given resource
125 * @throws IOException I/O in case something goes wrong
127 public static byte[] read(String name) throws IOException {
129 Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
130 while (en.hasMoreElements()) {
131 u = en.nextElement();
134 throw new IOException("Can't find " + name);
136 try (InputStream is = u.openStream()) {
138 arr = new byte[is.available()];
140 while (offset < arr.length) {
141 int len = is.read(arr, offset, arr.length - offset);
143 throw new IOException("Can't read " + name);
151 private static Object invokeMethod(String clazz, String method)
152 throws ClassNotFoundException, InvocationTargetException,
153 SecurityException, IllegalAccessException, IllegalArgumentException,
154 InstantiationException {
156 Class<?> c = Class.forName(clazz);
157 for (Method m : c.getMethods()) {
158 if (m.getName().equals(method)) {
165 if ((found.getModifiers() & Modifier.STATIC) != 0) {
166 res = found.invoke(null);
168 res = found.invoke(c.newInstance());
170 } catch (Exception ex) {
171 res = ex.getClass().getName() + ":" + ex.getMessage();
174 res = "Can't find method " + method + " in " + clazz;
179 private static final class Case {
180 private final Object data;
182 private Case(Object data) {
186 public static Case parseData(String s) {
187 return new Case(toJSON(s));
190 public String getMethodName() {
191 return value("methodName", data);
194 public String getClassName() {
195 return value("className", data);
198 public String getRequestId() {
199 return value("request", data);
202 @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
203 private static native Object toJSON(String s);
205 @JavaScriptBody(args = {"p", "d"}, body = "return d[p].toString();")
206 private static native String value(String p, Object d);