jaroslav@323
|
1 |
/**
|
jaroslav@323
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@323
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@323
|
4 |
*
|
jaroslav@323
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@323
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@323
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@323
|
8 |
*
|
jaroslav@323
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@323
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@323
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@323
|
12 |
* GNU General Public License for more details.
|
jaroslav@323
|
13 |
*
|
jaroslav@323
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@323
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@323
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@323
|
17 |
*/
|
jaroslav@323
|
18 |
package org.apidesign.bck2brwsr.launcher;
|
jaroslav@323
|
19 |
|
jaroslav@360
|
20 |
import java.io.IOException;
|
jaroslav@360
|
21 |
import java.io.InputStream;
|
jaroslav@332
|
22 |
import java.lang.reflect.InvocationTargetException;
|
jaroslav@323
|
23 |
import java.lang.reflect.Method;
|
jaroslav@412
|
24 |
import java.lang.reflect.Modifier;
|
jaroslav@332
|
25 |
import java.net.URL;
|
jaroslav@360
|
26 |
import java.util.Enumeration;
|
jaroslav@323
|
27 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@323
|
28 |
|
jaroslav@323
|
29 |
/**
|
jaroslav@323
|
30 |
*
|
jaroslav@323
|
31 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@323
|
32 |
*/
|
jaroslav@323
|
33 |
public class Console {
|
jaroslav@323
|
34 |
public static String welcome() {
|
jaroslav@342
|
35 |
return "HellofromBck2Brwsr";
|
jaroslav@323
|
36 |
}
|
jaroslav@342
|
37 |
public static String multiply() {
|
jaroslav@342
|
38 |
return String.valueOf(Integer.MAX_VALUE / 2 + Integer.MAX_VALUE);
|
jaroslav@342
|
39 |
}
|
jaroslav@323
|
40 |
|
jaroslav@323
|
41 |
@JavaScriptBody(args = {"id", "attr"}, body =
|
jaroslav@323
|
42 |
"return window.document.getElementById(id)[attr].toString();")
|
jaroslav@323
|
43 |
private static native Object getAttr(String id, String attr);
|
jaroslav@323
|
44 |
|
jaroslav@323
|
45 |
@JavaScriptBody(args = {"id", "attr", "value"}, body =
|
jaroslav@323
|
46 |
"window.document.getElementById(id)[attr] = value;")
|
jaroslav@323
|
47 |
private static native void setAttr(String id, String attr, Object value);
|
jaroslav@366
|
48 |
|
jaroslav@381
|
49 |
@JavaScriptBody(args = {}, body = "return; window.close();")
|
jaroslav@366
|
50 |
private static native void closeWindow();
|
jaroslav@342
|
51 |
|
jaroslav@343
|
52 |
private static void log(String newText) {
|
jaroslav@343
|
53 |
String id = "result";
|
jaroslav@343
|
54 |
String attr = "value";
|
jaroslav@342
|
55 |
setAttr(id, attr, getAttr(id, attr) + "\n" + newText);
|
jaroslav@343
|
56 |
setAttr(id, "scrollTop", getAttr(id, "scrollHeight"));
|
jaroslav@342
|
57 |
}
|
jaroslav@323
|
58 |
|
jaroslav@323
|
59 |
public static void execute() throws Exception {
|
jaroslav@323
|
60 |
String clazz = (String) getAttr("clazz", "value");
|
jaroslav@323
|
61 |
String method = (String) getAttr("method", "value");
|
jaroslav@332
|
62 |
Object res = invokeMethod(clazz, method);
|
jaroslav@332
|
63 |
setAttr("result", "value", res);
|
jaroslav@332
|
64 |
}
|
jaroslav@332
|
65 |
|
jaroslav@342
|
66 |
public static void harness(String url) {
|
jaroslav@343
|
67 |
log("Connecting to " + url);
|
jaroslav@332
|
68 |
try {
|
jaroslav@342
|
69 |
URL u = new URL(url);
|
jaroslav@342
|
70 |
for (;;) {
|
jaroslav@342
|
71 |
String data = (String) u.getContent(new Class[] { String.class });
|
jaroslav@344
|
72 |
log("\nGot \"" + data + "\"");
|
jaroslav@342
|
73 |
if (data.isEmpty()) {
|
jaroslav@343
|
74 |
log("No data, exiting");
|
jaroslav@366
|
75 |
closeWindow();
|
jaroslav@342
|
76 |
break;
|
jaroslav@342
|
77 |
}
|
jaroslav@342
|
78 |
|
jaroslav@342
|
79 |
Case c = Case.parseData(data);
|
jaroslav@344
|
80 |
log("Invoking " + c.getClassName() + '.' + c.getMethodName() + " as request: " + c.getRequestId());
|
jaroslav@342
|
81 |
|
jaroslav@342
|
82 |
Object result = invokeMethod(c.getClassName(), c.getMethodName());
|
jaroslav@355
|
83 |
|
jaroslav@344
|
84 |
log("Result: " + result);
|
jaroslav@381
|
85 |
|
jaroslav@381
|
86 |
result = encodeURL("" + result);
|
jaroslav@381
|
87 |
|
jaroslav@355
|
88 |
log("Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + result);
|
jaroslav@355
|
89 |
u = new URL(url + "?request=" + c.getRequestId() + "&result=" + result);
|
jaroslav@342
|
90 |
}
|
jaroslav@342
|
91 |
|
jaroslav@342
|
92 |
|
jaroslav@332
|
93 |
} catch (Exception ex) {
|
jaroslav@343
|
94 |
log(ex.getMessage());
|
jaroslav@332
|
95 |
}
|
jaroslav@332
|
96 |
}
|
jaroslav@356
|
97 |
|
jaroslav@381
|
98 |
private static String encodeURL(String r) {
|
jaroslav@381
|
99 |
StringBuilder sb = new StringBuilder();
|
jaroslav@381
|
100 |
for (int i = 0; i < r.length(); i++) {
|
jaroslav@381
|
101 |
int ch = r.charAt(i);
|
jaroslav@381
|
102 |
if (ch < 32 || ch == '%' || ch == '+') {
|
jaroslav@381
|
103 |
sb.append("%").append(("0" + Integer.toHexString(ch)).substring(0, 2));
|
jaroslav@381
|
104 |
} else {
|
jaroslav@381
|
105 |
if (ch == 32) {
|
jaroslav@381
|
106 |
sb.append("+");
|
jaroslav@381
|
107 |
} else {
|
jaroslav@381
|
108 |
sb.append((char)ch);
|
jaroslav@381
|
109 |
}
|
jaroslav@381
|
110 |
}
|
jaroslav@381
|
111 |
}
|
jaroslav@381
|
112 |
return sb.toString();
|
jaroslav@381
|
113 |
}
|
jaroslav@381
|
114 |
|
jaroslav@412
|
115 |
static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
|
jaroslav@356
|
116 |
final Object r = invokeMethod(clazz, method);
|
jaroslav@356
|
117 |
return r == null ? "null" : r.toString().toString();
|
jaroslav@356
|
118 |
}
|
jaroslav@323
|
119 |
|
jaroslav@360
|
120 |
/** Helper method that inspects the classpath and loads given resource
|
jaroslav@360
|
121 |
* (usually a class file). Used while running tests in Rhino.
|
jaroslav@360
|
122 |
*
|
jaroslav@360
|
123 |
* @param name resource name to find
|
jaroslav@360
|
124 |
* @return the array of bytes in the given resource
|
jaroslav@360
|
125 |
* @throws IOException I/O in case something goes wrong
|
jaroslav@360
|
126 |
*/
|
jaroslav@360
|
127 |
public static byte[] read(String name) throws IOException {
|
jaroslav@360
|
128 |
URL u = null;
|
jaroslav@360
|
129 |
Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
|
jaroslav@360
|
130 |
while (en.hasMoreElements()) {
|
jaroslav@360
|
131 |
u = en.nextElement();
|
jaroslav@360
|
132 |
}
|
jaroslav@360
|
133 |
if (u == null) {
|
jaroslav@360
|
134 |
throw new IOException("Can't find " + name);
|
jaroslav@360
|
135 |
}
|
jaroslav@360
|
136 |
try (InputStream is = u.openStream()) {
|
jaroslav@360
|
137 |
byte[] arr;
|
jaroslav@360
|
138 |
arr = new byte[is.available()];
|
jaroslav@360
|
139 |
int offset = 0;
|
jaroslav@360
|
140 |
while (offset < arr.length) {
|
jaroslav@360
|
141 |
int len = is.read(arr, offset, arr.length - offset);
|
jaroslav@360
|
142 |
if (len == -1) {
|
jaroslav@360
|
143 |
throw new IOException("Can't read " + name);
|
jaroslav@360
|
144 |
}
|
jaroslav@360
|
145 |
offset += len;
|
jaroslav@360
|
146 |
}
|
jaroslav@360
|
147 |
return arr;
|
jaroslav@360
|
148 |
}
|
jaroslav@360
|
149 |
}
|
jaroslav@360
|
150 |
|
jaroslav@332
|
151 |
private static Object invokeMethod(String clazz, String method)
|
jaroslav@332
|
152 |
throws ClassNotFoundException, InvocationTargetException,
|
jaroslav@412
|
153 |
SecurityException, IllegalAccessException, IllegalArgumentException,
|
jaroslav@412
|
154 |
InstantiationException {
|
jaroslav@323
|
155 |
Method found = null;
|
jaroslav@323
|
156 |
Class<?> c = Class.forName(clazz);
|
jaroslav@323
|
157 |
for (Method m : c.getMethods()) {
|
jaroslav@323
|
158 |
if (m.getName().equals(method)) {
|
jaroslav@323
|
159 |
found = m;
|
jaroslav@323
|
160 |
}
|
jaroslav@323
|
161 |
}
|
jaroslav@323
|
162 |
Object res;
|
jaroslav@323
|
163 |
if (found != null) {
|
jaroslav@412
|
164 |
if ((found.getModifiers() & Modifier.STATIC) != 0) {
|
jaroslav@412
|
165 |
res = found.invoke(null);
|
jaroslav@412
|
166 |
} else {
|
jaroslav@412
|
167 |
res = found.invoke(c.newInstance());
|
jaroslav@412
|
168 |
}
|
jaroslav@323
|
169 |
} else {
|
jaroslav@323
|
170 |
res = "Can't find method " + method + " in " + clazz;
|
jaroslav@323
|
171 |
}
|
jaroslav@332
|
172 |
return res;
|
jaroslav@323
|
173 |
}
|
jaroslav@342
|
174 |
|
jaroslav@342
|
175 |
private static final class Case {
|
jaroslav@342
|
176 |
private final Object data;
|
jaroslav@342
|
177 |
|
jaroslav@342
|
178 |
private Case(Object data) {
|
jaroslav@342
|
179 |
this.data = data;
|
jaroslav@342
|
180 |
}
|
jaroslav@342
|
181 |
|
jaroslav@342
|
182 |
public static Case parseData(String s) {
|
jaroslav@342
|
183 |
return new Case(toJSON(s));
|
jaroslav@342
|
184 |
}
|
jaroslav@342
|
185 |
|
jaroslav@342
|
186 |
public String getMethodName() {
|
jaroslav@342
|
187 |
return value("methodName", data);
|
jaroslav@342
|
188 |
}
|
jaroslav@342
|
189 |
|
jaroslav@342
|
190 |
public String getClassName() {
|
jaroslav@342
|
191 |
return value("className", data);
|
jaroslav@342
|
192 |
}
|
jaroslav@342
|
193 |
|
jaroslav@342
|
194 |
public String getRequestId() {
|
jaroslav@342
|
195 |
return value("request", data);
|
jaroslav@342
|
196 |
}
|
jaroslav@342
|
197 |
|
jaroslav@342
|
198 |
@JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
|
jaroslav@342
|
199 |
private static native Object toJSON(String s);
|
jaroslav@342
|
200 |
|
jaroslav@342
|
201 |
@JavaScriptBody(args = {"p", "d"}, body = "return d[p].toString();")
|
jaroslav@342
|
202 |
private static native String value(String p, Object d);
|
jaroslav@342
|
203 |
}
|
jaroslav@323
|
204 |
}
|