Closing the window when data are processed.
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 @JavaScriptBody(args = {}, body = "window.close();")
49 private static native void closeWindow();
51 private static void log(String newText) {
53 String attr = "value";
54 setAttr(id, attr, getAttr(id, attr) + "\n" + newText);
55 setAttr(id, "scrollTop", getAttr(id, "scrollHeight"));
58 public static void execute() throws Exception {
59 String clazz = (String) getAttr("clazz", "value");
60 String method = (String) getAttr("method", "value");
61 Object res = invokeMethod(clazz, method);
62 setAttr("result", "value", res);
65 public static void harness(String url) {
66 log("Connecting to " + url);
70 String data = (String) u.getContent(new Class[] { String.class });
71 log("\nGot \"" + data + "\"");
73 log("No data, exiting");
78 Case c = Case.parseData(data);
79 log("Invoking " + c.getClassName() + '.' + c.getMethodName() + " as request: " + c.getRequestId());
81 Object result = invokeMethod(c.getClassName(), c.getMethodName());
83 log("Result: " + result);
84 log("Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + result);
85 u = new URL(url + "?request=" + c.getRequestId() + "&result=" + result);
89 } catch (Exception ex) {
94 static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException {
95 final Object r = invokeMethod(clazz, method);
96 return r == null ? "null" : r.toString().toString();
99 /** Helper method that inspects the classpath and loads given resource
100 * (usually a class file). Used while running tests in Rhino.
102 * @param name resource name to find
103 * @return the array of bytes in the given resource
104 * @throws IOException I/O in case something goes wrong
106 public static byte[] read(String name) throws IOException {
108 Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
109 while (en.hasMoreElements()) {
110 u = en.nextElement();
113 throw new IOException("Can't find " + name);
115 try (InputStream is = u.openStream()) {
117 arr = new byte[is.available()];
119 while (offset < arr.length) {
120 int len = is.read(arr, offset, arr.length - offset);
122 throw new IOException("Can't read " + name);
130 private static Object invokeMethod(String clazz, String method)
131 throws ClassNotFoundException, InvocationTargetException,
132 SecurityException, IllegalAccessException, IllegalArgumentException {
134 Class<?> c = Class.forName(clazz);
135 for (Method m : c.getMethods()) {
136 if (m.getName().equals(method)) {
142 res = found.invoke(null);
144 res = "Can't find method " + method + " in " + clazz;
149 private static final class Case {
150 private final Object data;
152 private Case(Object data) {
156 public static Case parseData(String s) {
157 return new Case(toJSON(s));
160 public String getMethodName() {
161 return value("methodName", data);
164 public String getClassName() {
165 return value("className", data);
168 public String getRequestId() {
169 return value("request", data);
172 @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
173 private static native Object toJSON(String s);
175 @JavaScriptBody(args = {"p", "d"}, body = "return d[p].toString();")
176 private static native String value(String p, Object d);