1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/dew/nbactions.xml Wed Jan 23 14:10:15 2013 +0100
1.3 @@ -0,0 +1,37 @@
1.4 +<?xml version="1.0" encoding="UTF-8"?>
1.5 +<actions>
1.6 + <action>
1.7 + <actionName>run</actionName>
1.8 + <goals>
1.9 + <goal>process-classes</goal>
1.10 + <goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
1.11 + </goals>
1.12 + <properties>
1.13 + <exec.args>-classpath %classpath org.apidesign.bck2brwsr.dew.Dew</exec.args>
1.14 + <exec.executable>java</exec.executable>
1.15 + </properties>
1.16 + </action>
1.17 + <action>
1.18 + <actionName>debug</actionName>
1.19 + <goals>
1.20 + <goal>process-classes</goal>
1.21 + <goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
1.22 + </goals>
1.23 + <properties>
1.24 + <exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath org.apidesign.bck2brwsr.dew.Dew</exec.args>
1.25 + <exec.executable>java</exec.executable>
1.26 + <jpda.listen>true</jpda.listen>
1.27 + </properties>
1.28 + </action>
1.29 + <action>
1.30 + <actionName>profile</actionName>
1.31 + <goals>
1.32 + <goal>process-classes</goal>
1.33 + <goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
1.34 + </goals>
1.35 + <properties>
1.36 + <exec.args>${profiler.args} -classpath %classpath org.apidesign.bck2brwsr.dew.Dew</exec.args>
1.37 + <exec.executable>${profiler.java}</exec.executable>
1.38 + </properties>
1.39 + </action>
1.40 + </actions>
2.1 --- a/dew/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java Wed Jan 23 13:56:54 2013 +0100
2.2 +++ b/dew/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java Wed Jan 23 14:10:15 2013 +0100
2.3 @@ -22,32 +22,39 @@
2.4 import java.io.InputStream;
2.5 import java.io.InputStreamReader;
2.6 import java.io.OutputStream;
2.7 -import java.io.Writer;
2.8 import java.util.List;
2.9 import java.util.Locale;
2.10 -import java.util.Locale;
2.11 -import java.util.logging.Logger;
2.12 import javax.tools.Diagnostic;
2.13 import javax.tools.JavaFileObject;
2.14 import org.apidesign.vm4brwsr.Bck2Brwsr;
2.15 import org.glassfish.grizzly.http.Method;
2.16 import org.glassfish.grizzly.http.server.HttpHandler;
2.17 +import org.glassfish.grizzly.http.server.HttpServer;
2.18 import org.glassfish.grizzly.http.server.Request;
2.19 import org.glassfish.grizzly.http.server.Response;
2.20 import org.glassfish.grizzly.http.util.HttpStatus;
2.21 import org.json.JSONArray;
2.22 import org.json.JSONObject;
2.23 -import org.json.JSONStringer;
2.24 import org.json.JSONTokener;
2.25
2.26 /**
2.27 *
2.28 * @author phrebejk
2.29 */
2.30 -public class Dew extends HttpHandler implements Bck2Brwsr.Resources {
2.31 +final class Dew extends HttpHandler implements Bck2Brwsr.Resources {
2.32 private String html = "";
2.33 private Compile data;
2.34
2.35 + public static void main(String... args) throws Exception {
2.36 + DewLauncher l = new DewLauncher(null);
2.37 + l.addClassLoader(DewLauncher.class.getClassLoader());
2.38 + final Dew dew = new Dew();
2.39 + HttpServer s = l.initServer(dew);
2.40 + s.getServerConfiguration().addHttpHandler(dew, "/dew/");
2.41 + l.launchServerAndBrwsr(s, "/dew/");
2.42 + System.in.read();
2.43 + }
2.44 +
2.45 @Override
2.46 public void service(Request request, Response response) throws Exception {
2.47
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/dew/src/main/java/org/apidesign/bck2brwsr/dew/DewLauncher.java Wed Jan 23 14:10:15 2013 +0100
3.3 @@ -0,0 +1,201 @@
3.4 +/**
3.5 + * Back 2 Browser Bytecode Translator
3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
3.7 + *
3.8 + * This program is free software: you can redistribute it and/or modify
3.9 + * it under the terms of the GNU General Public License as published by
3.10 + * the Free Software Foundation, version 2 of the License.
3.11 + *
3.12 + * This program is distributed in the hope that it will be useful,
3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.15 + * GNU General Public License for more details.
3.16 + *
3.17 + * You should have received a copy of the GNU General Public License
3.18 + * along with this program. Look for COPYING file in the top folder.
3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
3.20 + */
3.21 +package org.apidesign.bck2brwsr.dew;
3.22 +
3.23 +import java.io.IOException;
3.24 +import java.io.InputStream;
3.25 +import java.io.Writer;
3.26 +import java.net.URI;
3.27 +import java.net.URISyntaxException;
3.28 +import java.net.URL;
3.29 +import java.util.Arrays;
3.30 +import java.util.Enumeration;
3.31 +import java.util.LinkedHashSet;
3.32 +import java.util.Set;
3.33 +import java.util.logging.Level;
3.34 +import java.util.logging.Logger;
3.35 +import org.apidesign.vm4brwsr.Bck2Brwsr;
3.36 +import org.glassfish.grizzly.PortRange;
3.37 +import org.glassfish.grizzly.http.server.HttpHandler;
3.38 +import org.glassfish.grizzly.http.server.HttpServer;
3.39 +import org.glassfish.grizzly.http.server.NetworkListener;
3.40 +import org.glassfish.grizzly.http.server.Request;
3.41 +import org.glassfish.grizzly.http.server.Response;
3.42 +import org.glassfish.grizzly.http.server.ServerConfiguration;
3.43 +
3.44 +/**
3.45 + * Lightweight server to launch dew - the Development Environment for Web.
3.46 + */
3.47 +final class DewLauncher {
3.48 + private static final Logger LOG = Logger.getLogger(DewLauncher.class.getName());
3.49 + private Set<ClassLoader> loaders = new LinkedHashSet<>();
3.50 + private Set<Bck2Brwsr.Resources> xRes = new LinkedHashSet<>();
3.51 + private final Res resources = new Res();
3.52 + private final String cmd;
3.53 +
3.54 + public DewLauncher(String cmd) {
3.55 + this.cmd = cmd;
3.56 + }
3.57 +
3.58 + public void addClassLoader(ClassLoader url) {
3.59 + this.loaders.add(url);
3.60 + }
3.61 +
3.62 + final HttpServer initServer(Bck2Brwsr.Resources... extraResources) {
3.63 + xRes.addAll(Arrays.asList(extraResources));
3.64 +
3.65 + HttpServer s = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
3.66 +
3.67 + final ServerConfiguration conf = s.getServerConfiguration();
3.68 + conf.addHttpHandler(new VM(resources), "/bck2brwsr.js");
3.69 + conf.addHttpHandler(new VMInit(), "/vm.js");
3.70 + conf.addHttpHandler(new Classes(resources), "/classes/");
3.71 + return s;
3.72 + }
3.73 +
3.74 + final Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
3.75 + server.start();
3.76 + NetworkListener listener = server.getListeners().iterator().next();
3.77 + int port = listener.getPort();
3.78 +
3.79 + URI uri = new URI("http://localhost:" + port + page);
3.80 + LOG.log(Level.INFO, "Showing {0}", uri);
3.81 + if (cmd == null) {
3.82 + try {
3.83 + LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[] {
3.84 + System.getProperty("java.vm.name"),
3.85 + System.getProperty("java.vm.vendor"),
3.86 + System.getProperty("java.vm.version"),
3.87 + });
3.88 + java.awt.Desktop.getDesktop().browse(uri);
3.89 + LOG.log(Level.INFO, "Desktop.browse successfully finished");
3.90 + return null;
3.91 + } catch (UnsupportedOperationException ex) {
3.92 + LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
3.93 + LOG.log(Level.FINE, null, ex);
3.94 + }
3.95 + }
3.96 + {
3.97 + String cmdName = cmd == null ? "xdg-open" : cmd;
3.98 + String[] cmdArr = {
3.99 + cmdName, uri.toString()
3.100 + };
3.101 + LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
3.102 + final Process process = Runtime.getRuntime().exec(cmdArr);
3.103 + return new Object[] { process, null };
3.104 + }
3.105 + }
3.106 +
3.107 + private class Res implements Bck2Brwsr.Resources {
3.108 + @Override
3.109 + public InputStream get(String resource) throws IOException {
3.110 + for (ClassLoader l : loaders) {
3.111 + URL u = null;
3.112 + Enumeration<URL> en = l.getResources(resource);
3.113 + while (en.hasMoreElements()) {
3.114 + u = en.nextElement();
3.115 + }
3.116 + if (u != null) {
3.117 + return u.openStream();
3.118 + }
3.119 + }
3.120 + for (Bck2Brwsr.Resources r : xRes) {
3.121 + InputStream is = r.get(resource);
3.122 + if (is != null) {
3.123 + return is;
3.124 + }
3.125 + }
3.126 + throw new IOException("Can't find " + resource);
3.127 + }
3.128 + }
3.129 +
3.130 + private static class VM extends HttpHandler {
3.131 + private final Res loader;
3.132 +
3.133 + public VM(Res loader) {
3.134 + this.loader = loader;
3.135 + }
3.136 +
3.137 + @Override
3.138 + public void service(Request request, Response response) throws Exception {
3.139 + response.setCharacterEncoding("UTF-8");
3.140 + response.setContentType("text/javascript");
3.141 + Bck2Brwsr.generate(response.getWriter(), loader);
3.142 + }
3.143 + }
3.144 + private static class VMInit extends HttpHandler {
3.145 + public VMInit() {
3.146 + }
3.147 +
3.148 + @Override
3.149 + public void service(Request request, Response response) throws Exception {
3.150 + response.setCharacterEncoding("UTF-8");
3.151 + response.setContentType("text/javascript");
3.152 + response.getWriter().append(
3.153 + "function ldCls(res) {\n"
3.154 + + " var request = new XMLHttpRequest();\n"
3.155 + + " request.open('GET', '/classes/' + res, false);\n"
3.156 + + " request.send();\n"
3.157 + + " var arr = eval('(' + request.responseText + ')');\n"
3.158 + + " return arr;\n"
3.159 + + "}\n"
3.160 + + "var vm = new bck2brwsr(ldCls);\n");
3.161 + }
3.162 + }
3.163 +
3.164 + private static class Classes extends HttpHandler {
3.165 + private final Res loader;
3.166 +
3.167 + public Classes(Res loader) {
3.168 + this.loader = loader;
3.169 + }
3.170 +
3.171 + @Override
3.172 + public void service(Request request, Response response) throws Exception {
3.173 + String res = request.getHttpHandlerPath();
3.174 + if (res.startsWith("/")) {
3.175 + res = res.substring(1);
3.176 + }
3.177 + try (InputStream is = loader.get(res)) {
3.178 + response.setContentType("text/javascript");
3.179 + Writer w = response.getWriter();
3.180 + w.append("[");
3.181 + for (int i = 0;; i++) {
3.182 + int b = is.read();
3.183 + if (b == -1) {
3.184 + break;
3.185 + }
3.186 + if (i > 0) {
3.187 + w.append(", ");
3.188 + }
3.189 + if (i % 20 == 0) {
3.190 + w.write("\n");
3.191 + }
3.192 + if (b > 127) {
3.193 + b = b - 256;
3.194 + }
3.195 + w.append(Integer.toString(b));
3.196 + }
3.197 + w.append("\n]");
3.198 + } catch (IOException ex) {
3.199 + response.setError();
3.200 + response.setDetailMessage(ex.getMessage());
3.201 + }
3.202 + }
3.203 + }
3.204 +}
4.1 --- a/dew/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Wed Jan 23 13:56:54 2013 +0100
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,505 +0,0 @@
4.4 -/**
4.5 - * Back 2 Browser Bytecode Translator
4.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.7 - *
4.8 - * This program is free software: you can redistribute it and/or modify
4.9 - * it under the terms of the GNU General Public License as published by
4.10 - * the Free Software Foundation, version 2 of the License.
4.11 - *
4.12 - * This program is distributed in the hope that it will be useful,
4.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.15 - * GNU General Public License for more details.
4.16 - *
4.17 - * You should have received a copy of the GNU General Public License
4.18 - * along with this program. Look for COPYING file in the top folder.
4.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
4.20 - */
4.21 -package org.apidesign.bck2brwsr.launcher;
4.22 -
4.23 -import java.io.Closeable;
4.24 -import java.io.File;
4.25 -import java.io.IOException;
4.26 -import java.io.InputStream;
4.27 -import java.io.InterruptedIOException;
4.28 -import java.io.OutputStream;
4.29 -import java.io.Writer;
4.30 -import java.net.URI;
4.31 -import java.net.URISyntaxException;
4.32 -import java.net.URL;
4.33 -import java.util.ArrayList;
4.34 -import java.util.Arrays;
4.35 -import java.util.Enumeration;
4.36 -import java.util.LinkedHashSet;
4.37 -import java.util.List;
4.38 -import java.util.Set;
4.39 -import java.util.concurrent.BlockingQueue;
4.40 -import java.util.concurrent.CountDownLatch;
4.41 -import java.util.concurrent.LinkedBlockingQueue;
4.42 -import java.util.concurrent.TimeUnit;
4.43 -import java.util.logging.Level;
4.44 -import java.util.logging.Logger;
4.45 -import org.apidesign.bck2brwsr.dew.Dew;
4.46 -import org.apidesign.vm4brwsr.Bck2Brwsr;
4.47 -import org.glassfish.grizzly.PortRange;
4.48 -import org.glassfish.grizzly.http.server.HttpHandler;
4.49 -import org.glassfish.grizzly.http.server.HttpServer;
4.50 -import org.glassfish.grizzly.http.server.NetworkListener;
4.51 -import org.glassfish.grizzly.http.server.Request;
4.52 -import org.glassfish.grizzly.http.server.Response;
4.53 -import org.glassfish.grizzly.http.server.ServerConfiguration;
4.54 -
4.55 -/**
4.56 - * Lightweight server to launch Bck2Brwsr applications and tests.
4.57 - * Supports execution in native browser as well as Java's internal
4.58 - * execution engine.
4.59 - */
4.60 -final class Bck2BrwsrLauncher extends Launcher implements Closeable {
4.61 - private static final Logger LOG = Logger.getLogger(Bck2BrwsrLauncher.class.getName());
4.62 - private static final MethodInvocation END = new MethodInvocation(null, null, null);
4.63 - private Set<ClassLoader> loaders = new LinkedHashSet<>();
4.64 - private Set<Bck2Brwsr.Resources> xRes = new LinkedHashSet<>();
4.65 - private BlockingQueue<MethodInvocation> methods = new LinkedBlockingQueue<>();
4.66 - private long timeOut;
4.67 - private final Res resources = new Res();
4.68 - private final String cmd;
4.69 - private Object[] brwsr;
4.70 - private HttpServer server;
4.71 - private CountDownLatch wait;
4.72 -
4.73 - public Bck2BrwsrLauncher(String cmd) {
4.74 - this.cmd = cmd;
4.75 - }
4.76 -
4.77 - @Override
4.78 - MethodInvocation addMethod(Class<?> clazz, String method, String html) throws IOException {
4.79 - loaders.add(clazz.getClassLoader());
4.80 - MethodInvocation c = new MethodInvocation(clazz.getName(), method, html);
4.81 - methods.add(c);
4.82 - try {
4.83 - c.await(timeOut);
4.84 - } catch (InterruptedException ex) {
4.85 - throw new IOException(ex);
4.86 - }
4.87 - return c;
4.88 - }
4.89 -
4.90 - public void setTimeout(long ms) {
4.91 - timeOut = ms;
4.92 - }
4.93 -
4.94 - public void addClassLoader(ClassLoader url) {
4.95 - this.loaders.add(url);
4.96 - }
4.97 -
4.98 - public void showURL(String startpage) throws IOException {
4.99 - if (!startpage.startsWith("/")) {
4.100 - startpage = "/" + startpage;
4.101 - }
4.102 - HttpServer s = initServer();
4.103 - s.getServerConfiguration().addHttpHandler(new Page(resources, null), "/");
4.104 - try {
4.105 - launchServerAndBrwsr(s, startpage);
4.106 - } catch (URISyntaxException | InterruptedException ex) {
4.107 - throw new IOException(ex);
4.108 - }
4.109 - }
4.110 -
4.111 - public static void main(String... args) throws Exception {
4.112 - Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(null);
4.113 - l.addClassLoader(Bck2BrwsrLauncher.class.getClassLoader());
4.114 - HttpServer s = l.initServer();
4.115 - final Dew dew = new Dew();
4.116 - s.getServerConfiguration().addHttpHandler(dew, "/dew/");
4.117 - l.xRes.add(dew);
4.118 - l.launchServerAndBrwsr(s, "/dew/");
4.119 - System.in.read();
4.120 - }
4.121 -
4.122 - @Override
4.123 - public void initialize() throws IOException {
4.124 - try {
4.125 - executeInBrowser();
4.126 - } catch (InterruptedException ex) {
4.127 - final InterruptedIOException iio = new InterruptedIOException(ex.getMessage());
4.128 - iio.initCause(ex);
4.129 - throw iio;
4.130 - } catch (Exception ex) {
4.131 - if (ex instanceof IOException) {
4.132 - throw (IOException)ex;
4.133 - }
4.134 - if (ex instanceof RuntimeException) {
4.135 - throw (RuntimeException)ex;
4.136 - }
4.137 - throw new IOException(ex);
4.138 - }
4.139 - }
4.140 -
4.141 - private HttpServer initServer() {
4.142 - HttpServer s = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
4.143 -
4.144 - final ServerConfiguration conf = s.getServerConfiguration();
4.145 - conf.addHttpHandler(new Page(resources,
4.146 - "org/apidesign/bck2brwsr/launcher/console.xhtml",
4.147 - "org.apidesign.bck2brwsr.launcher.Console", "welcome", "false"
4.148 - ), "/console");
4.149 - conf.addHttpHandler(new VM(resources), "/bck2brwsr.js");
4.150 - conf.addHttpHandler(new VMInit(), "/vm.js");
4.151 - conf.addHttpHandler(new Classes(resources), "/classes/");
4.152 - return s;
4.153 - }
4.154 -
4.155 - private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
4.156 - wait = new CountDownLatch(1);
4.157 - server = initServer();
4.158 - ServerConfiguration conf = server.getServerConfiguration();
4.159 - conf.addHttpHandler(new Page(resources,
4.160 - "org/apidesign/bck2brwsr/launcher/harness.xhtml"
4.161 - ), "/execute");
4.162 - conf.addHttpHandler(new HttpHandler() {
4.163 - int cnt;
4.164 - List<MethodInvocation> cases = new ArrayList<>();
4.165 - @Override
4.166 - public void service(Request request, Response response) throws Exception {
4.167 - String id = request.getParameter("request");
4.168 - String value = request.getParameter("result");
4.169 -
4.170 - if (id != null && value != null) {
4.171 - LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value});
4.172 - value = decodeURL(value);
4.173 - cases.get(Integer.parseInt(id)).result(value, null);
4.174 - }
4.175 -
4.176 - MethodInvocation mi = methods.take();
4.177 - if (mi == END) {
4.178 - response.getWriter().write("");
4.179 - wait.countDown();
4.180 - cnt = 0;
4.181 - LOG.log(Level.INFO, "End of data reached. Exiting.");
4.182 - return;
4.183 - }
4.184 -
4.185 - cases.add(mi);
4.186 - final String cn = mi.className;
4.187 - final String mn = mi.methodName;
4.188 - LOG.log(Level.INFO, "Request for {0} case. Sending {1}.{2}", new Object[]{cnt, cn, mn});
4.189 - response.getWriter().write("{"
4.190 - + "className: '" + cn + "', "
4.191 - + "methodName: '" + mn + "', "
4.192 - + "request: " + cnt
4.193 - );
4.194 - if (mi.html != null) {
4.195 - response.getWriter().write(", html: '");
4.196 - response.getWriter().write(encodeJSON(mi.html));
4.197 - response.getWriter().write("'");
4.198 - }
4.199 - response.getWriter().write("}");
4.200 - cnt++;
4.201 - }
4.202 - }, "/data");
4.203 -
4.204 - this.brwsr = launchServerAndBrwsr(server, "/execute");
4.205 - }
4.206 -
4.207 - private static String encodeJSON(String in) {
4.208 - StringBuilder sb = new StringBuilder();
4.209 - for (int i = 0; i < in.length(); i++) {
4.210 - char ch = in.charAt(i);
4.211 - if (ch < 32 || ch == '\'' || ch == '"') {
4.212 - sb.append("\\u");
4.213 - String hs = "0000" + Integer.toHexString(ch);
4.214 - hs = hs.substring(hs.length() - 4);
4.215 - sb.append(hs);
4.216 - } else {
4.217 - sb.append(ch);
4.218 - }
4.219 - }
4.220 - return sb.toString();
4.221 - }
4.222 -
4.223 - @Override
4.224 - public void shutdown() throws IOException {
4.225 - methods.offer(END);
4.226 - for (;;) {
4.227 - int prev = methods.size();
4.228 - try {
4.229 - if (wait != null && wait.await(timeOut, TimeUnit.MILLISECONDS)) {
4.230 - break;
4.231 - }
4.232 - } catch (InterruptedException ex) {
4.233 - throw new IOException(ex);
4.234 - }
4.235 - if (prev == methods.size()) {
4.236 - LOG.log(
4.237 - Level.WARNING,
4.238 - "Timeout and no test has been executed meanwhile (at {0}). Giving up.",
4.239 - methods.size()
4.240 - );
4.241 - break;
4.242 - }
4.243 - LOG.log(Level.INFO,
4.244 - "Timeout, but tests got from {0} to {1}. Trying again.",
4.245 - new Object[]{prev, methods.size()}
4.246 - );
4.247 - }
4.248 - stopServerAndBrwsr(server, brwsr);
4.249 - }
4.250 -
4.251 - static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
4.252 - for (;;) {
4.253 - int ch = is.read();
4.254 - if (ch == -1) {
4.255 - break;
4.256 - }
4.257 - if (ch == '$' && params.length > 0) {
4.258 - int cnt = is.read() - '0';
4.259 - if (cnt == 'U' - '0') {
4.260 - os.write(baseURL.getBytes());
4.261 - }
4.262 - if (cnt >= 0 && cnt < params.length) {
4.263 - os.write(params[cnt].getBytes());
4.264 - }
4.265 - } else {
4.266 - os.write(ch);
4.267 - }
4.268 - }
4.269 - }
4.270 -
4.271 - private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
4.272 - server.start();
4.273 - NetworkListener listener = server.getListeners().iterator().next();
4.274 - int port = listener.getPort();
4.275 -
4.276 - URI uri = new URI("http://localhost:" + port + page);
4.277 - LOG.log(Level.INFO, "Showing {0}", uri);
4.278 - if (cmd == null) {
4.279 - try {
4.280 - LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[] {
4.281 - System.getProperty("java.vm.name"),
4.282 - System.getProperty("java.vm.vendor"),
4.283 - System.getProperty("java.vm.version"),
4.284 - });
4.285 - java.awt.Desktop.getDesktop().browse(uri);
4.286 - LOG.log(Level.INFO, "Desktop.browse successfully finished");
4.287 - return null;
4.288 - } catch (UnsupportedOperationException ex) {
4.289 - LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
4.290 - LOG.log(Level.FINE, null, ex);
4.291 - }
4.292 - }
4.293 - {
4.294 - String cmdName = cmd == null ? "xdg-open" : cmd;
4.295 - String[] cmdArr = {
4.296 - cmdName, uri.toString()
4.297 - };
4.298 - LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
4.299 - final Process process = Runtime.getRuntime().exec(cmdArr);
4.300 - return new Object[] { process, null };
4.301 - }
4.302 - }
4.303 -
4.304 - private static String decodeURL(String s) {
4.305 - for (;;) {
4.306 - int pos = s.indexOf('%');
4.307 - if (pos == -1) {
4.308 - return s;
4.309 - }
4.310 - int i = Integer.parseInt(s.substring(pos + 1, pos + 2), 16);
4.311 - s = s.substring(0, pos) + (char)i + s.substring(pos + 2);
4.312 - }
4.313 - }
4.314 -
4.315 - private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException {
4.316 - if (brwsr == null) {
4.317 - return;
4.318 - }
4.319 - Process process = (Process)brwsr[0];
4.320 -
4.321 - server.stop();
4.322 - InputStream stdout = process.getInputStream();
4.323 - InputStream stderr = process.getErrorStream();
4.324 - drain("StdOut", stdout);
4.325 - drain("StdErr", stderr);
4.326 - process.destroy();
4.327 - int res;
4.328 - try {
4.329 - res = process.waitFor();
4.330 - } catch (InterruptedException ex) {
4.331 - throw new IOException(ex);
4.332 - }
4.333 - LOG.log(Level.INFO, "Exit code: {0}", res);
4.334 -
4.335 - deleteTree((File)brwsr[1]);
4.336 - }
4.337 -
4.338 - private static void drain(String name, InputStream is) throws IOException {
4.339 - int av = is.available();
4.340 - if (av > 0) {
4.341 - StringBuilder sb = new StringBuilder();
4.342 - sb.append("v== ").append(name).append(" ==v\n");
4.343 - while (av-- > 0) {
4.344 - sb.append((char)is.read());
4.345 - }
4.346 - sb.append("\n^== ").append(name).append(" ==^");
4.347 - LOG.log(Level.INFO, sb.toString());
4.348 - }
4.349 - }
4.350 -
4.351 - private void deleteTree(File file) {
4.352 - if (file == null) {
4.353 - return;
4.354 - }
4.355 - File[] arr = file.listFiles();
4.356 - if (arr != null) {
4.357 - for (File s : arr) {
4.358 - deleteTree(s);
4.359 - }
4.360 - }
4.361 - file.delete();
4.362 - }
4.363 -
4.364 - @Override
4.365 - public void close() throws IOException {
4.366 - shutdown();
4.367 - }
4.368 -
4.369 - private class Res implements Bck2Brwsr.Resources {
4.370 - @Override
4.371 - public InputStream get(String resource) throws IOException {
4.372 - for (ClassLoader l : loaders) {
4.373 - URL u = null;
4.374 - Enumeration<URL> en = l.getResources(resource);
4.375 - while (en.hasMoreElements()) {
4.376 - u = en.nextElement();
4.377 - }
4.378 - if (u != null) {
4.379 - return u.openStream();
4.380 - }
4.381 - }
4.382 - for (Bck2Brwsr.Resources r : xRes) {
4.383 - InputStream is = r.get(resource);
4.384 - if (is != null) {
4.385 - return is;
4.386 - }
4.387 - }
4.388 - throw new IOException("Can't find " + resource);
4.389 - }
4.390 - }
4.391 -
4.392 - private static class Page extends HttpHandler {
4.393 - private final String resource;
4.394 - private final String[] args;
4.395 - private final Res res;
4.396 -
4.397 - public Page(Res res, String resource, String... args) {
4.398 - this.res = res;
4.399 - this.resource = resource;
4.400 - this.args = args.length == 0 ? new String[] { "$0" } : args;
4.401 - }
4.402 -
4.403 - @Override
4.404 - public void service(Request request, Response response) throws Exception {
4.405 - String r = resource;
4.406 - if (r == null) {
4.407 - r = request.getHttpHandlerPath();
4.408 - if (r.startsWith("/")) {
4.409 - r = r.substring(1);
4.410 - }
4.411 - }
4.412 - String[] replace = {};
4.413 - if (r.endsWith(".html")) {
4.414 - response.setContentType("text/html");
4.415 - LOG.info("Content type text/html");
4.416 - replace = args;
4.417 - }
4.418 - if (r.endsWith(".xhtml")) {
4.419 - response.setContentType("application/xhtml+xml");
4.420 - LOG.info("Content type application/xhtml+xml");
4.421 - replace = args;
4.422 - }
4.423 - OutputStream os = response.getOutputStream();
4.424 - try (InputStream is = res.get(r)) {
4.425 - copyStream(is, os, request.getRequestURL().toString(), replace);
4.426 - } catch (IOException ex) {
4.427 - response.setDetailMessage(ex.getLocalizedMessage());
4.428 - response.setError();
4.429 - response.setStatus(404);
4.430 - }
4.431 - }
4.432 - }
4.433 -
4.434 - private static class VM extends HttpHandler {
4.435 - private final Res loader;
4.436 -
4.437 - public VM(Res loader) {
4.438 - this.loader = loader;
4.439 - }
4.440 -
4.441 - @Override
4.442 - public void service(Request request, Response response) throws Exception {
4.443 - response.setCharacterEncoding("UTF-8");
4.444 - response.setContentType("text/javascript");
4.445 - Bck2Brwsr.generate(response.getWriter(), loader);
4.446 - }
4.447 - }
4.448 - private static class VMInit extends HttpHandler {
4.449 - public VMInit() {
4.450 - }
4.451 -
4.452 - @Override
4.453 - public void service(Request request, Response response) throws Exception {
4.454 - response.setCharacterEncoding("UTF-8");
4.455 - response.setContentType("text/javascript");
4.456 - response.getWriter().append(
4.457 - "function ldCls(res) {\n"
4.458 - + " var request = new XMLHttpRequest();\n"
4.459 - + " request.open('GET', '/classes/' + res, false);\n"
4.460 - + " request.send();\n"
4.461 - + " var arr = eval('(' + request.responseText + ')');\n"
4.462 - + " return arr;\n"
4.463 - + "}\n"
4.464 - + "var vm = new bck2brwsr(ldCls);\n");
4.465 - }
4.466 - }
4.467 -
4.468 - private static class Classes extends HttpHandler {
4.469 - private final Res loader;
4.470 -
4.471 - public Classes(Res loader) {
4.472 - this.loader = loader;
4.473 - }
4.474 -
4.475 - @Override
4.476 - public void service(Request request, Response response) throws Exception {
4.477 - String res = request.getHttpHandlerPath();
4.478 - if (res.startsWith("/")) {
4.479 - res = res.substring(1);
4.480 - }
4.481 - try (InputStream is = loader.get(res)) {
4.482 - response.setContentType("text/javascript");
4.483 - Writer w = response.getWriter();
4.484 - w.append("[");
4.485 - for (int i = 0;; i++) {
4.486 - int b = is.read();
4.487 - if (b == -1) {
4.488 - break;
4.489 - }
4.490 - if (i > 0) {
4.491 - w.append(", ");
4.492 - }
4.493 - if (i % 20 == 0) {
4.494 - w.write("\n");
4.495 - }
4.496 - if (b > 127) {
4.497 - b = b - 256;
4.498 - }
4.499 - w.append(Integer.toString(b));
4.500 - }
4.501 - w.append("\n]");
4.502 - } catch (IOException ex) {
4.503 - response.setError();
4.504 - response.setDetailMessage(ex.getMessage());
4.505 - }
4.506 - }
4.507 - }
4.508 -}
5.1 --- a/dew/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java Wed Jan 23 13:56:54 2013 +0100
5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
5.3 @@ -1,253 +0,0 @@
5.4 -/**
5.5 - * Back 2 Browser Bytecode Translator
5.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5.7 - *
5.8 - * This program is free software: you can redistribute it and/or modify
5.9 - * it under the terms of the GNU General Public License as published by
5.10 - * the Free Software Foundation, version 2 of the License.
5.11 - *
5.12 - * This program is distributed in the hope that it will be useful,
5.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
5.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5.15 - * GNU General Public License for more details.
5.16 - *
5.17 - * You should have received a copy of the GNU General Public License
5.18 - * along with this program. Look for COPYING file in the top folder.
5.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
5.20 - */
5.21 -package org.apidesign.bck2brwsr.launcher;
5.22 -
5.23 -import java.io.IOException;
5.24 -import java.io.InputStream;
5.25 -import java.lang.reflect.InvocationTargetException;
5.26 -import java.lang.reflect.Method;
5.27 -import java.lang.reflect.Modifier;
5.28 -import java.net.URL;
5.29 -import java.util.Enumeration;
5.30 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
5.31 -
5.32 -/**
5.33 - *
5.34 - * @author Jaroslav Tulach <jtulach@netbeans.org>
5.35 - */
5.36 -public class Console {
5.37 - static {
5.38 - turnAssetionStatusOn();
5.39 - }
5.40 -
5.41 - @JavaScriptBody(args = {"id", "attr"}, body =
5.42 - "return window.document.getElementById(id)[attr].toString();")
5.43 - private static native Object getAttr(String id, String attr);
5.44 -
5.45 - @JavaScriptBody(args = {"id", "attr", "value"}, body =
5.46 - "window.document.getElementById(id)[attr] = value;")
5.47 - private static native void setAttr(String id, String attr, Object value);
5.48 -
5.49 - @JavaScriptBody(args = {}, body = "return; window.close();")
5.50 - private static native void closeWindow();
5.51 -
5.52 - private static void log(String newText) {
5.53 - String id = "bck2brwsr.result";
5.54 - String attr = "value";
5.55 - setAttr(id, attr, getAttr(id, attr) + "\n" + newText);
5.56 - setAttr(id, "scrollTop", getAttr(id, "scrollHeight"));
5.57 - }
5.58 -
5.59 - public static void execute() throws Exception {
5.60 - String clazz = (String) getAttr("clazz", "value");
5.61 - String method = (String) getAttr("method", "value");
5.62 - Object res = invokeMethod(clazz, method);
5.63 - setAttr("bck2brwsr.result", "value", res);
5.64 - }
5.65 -
5.66 - @JavaScriptBody(args = { "url", "callback", "arr" }, body = ""
5.67 - + "var request = new XMLHttpRequest();\n"
5.68 - + "request.open('GET', url, true);\n"
5.69 - + "request.onreadystatechange = function() {\n"
5.70 - + " if (this.readyState!==4) return;\n"
5.71 - + " arr[0] = this.responseText;\n"
5.72 - + " callback.run__V();\n"
5.73 - + "};"
5.74 - + "request.send();"
5.75 - )
5.76 - private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
5.77 -
5.78 - public static void harness(String url) throws IOException {
5.79 - log("Connecting to " + url);
5.80 - Request r = new Request(url);
5.81 - }
5.82 -
5.83 - private static class Request implements Runnable {
5.84 - private final String[] arr = { null };
5.85 - private final String url;
5.86 -
5.87 - private Request(String url) throws IOException {
5.88 - this.url = url;
5.89 - loadText(url, this, arr);
5.90 - }
5.91 -
5.92 - @Override
5.93 - public void run() {
5.94 - try {
5.95 - String data = arr[0];
5.96 - log("\nGot \"" + data + "\"");
5.97 -
5.98 - if (data == null) {
5.99 - log("Some error exiting");
5.100 - closeWindow();
5.101 - return;
5.102 - }
5.103 -
5.104 - if (data.isEmpty()) {
5.105 - log("No data, exiting");
5.106 - closeWindow();
5.107 - return;
5.108 - }
5.109 -
5.110 - Case c = Case.parseData(data);
5.111 - if (c.getHtmlFragment() != null) {
5.112 - setAttr("bck2brwsr.fragment", "innerHTML", c.getHtmlFragment());
5.113 - }
5.114 - log("Invoking " + c.getClassName() + '.' + c.getMethodName() + " as request: " + c.getRequestId());
5.115 -
5.116 - Object result = invokeMethod(c.getClassName(), c.getMethodName());
5.117 -
5.118 - setAttr("bck2brwsr.fragment", "innerHTML", "");
5.119 - log("Result: " + result);
5.120 -
5.121 - result = encodeURL("" + result);
5.122 -
5.123 - log("Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + result);
5.124 - String u = url + "?request=" + c.getRequestId() + "&result=" + result;
5.125 -
5.126 - loadText(u, this, arr);
5.127 -
5.128 - } catch (Exception ex) {
5.129 - log(ex.getMessage());
5.130 - }
5.131 - }
5.132 - }
5.133 -
5.134 - private static String encodeURL(String r) {
5.135 - StringBuilder sb = new StringBuilder();
5.136 - for (int i = 0; i < r.length(); i++) {
5.137 - int ch = r.charAt(i);
5.138 - if (ch < 32 || ch == '%' || ch == '+') {
5.139 - sb.append("%").append(("0" + Integer.toHexString(ch)).substring(0, 2));
5.140 - } else {
5.141 - if (ch == 32) {
5.142 - sb.append("+");
5.143 - } else {
5.144 - sb.append((char)ch);
5.145 - }
5.146 - }
5.147 - }
5.148 - return sb.toString();
5.149 - }
5.150 -
5.151 - static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException {
5.152 - final Object r = invokeMethod(clazz, method);
5.153 - return r == null ? "null" : r.toString().toString();
5.154 - }
5.155 -
5.156 - /** Helper method that inspects the classpath and loads given resource
5.157 - * (usually a class file). Used while running tests in Rhino.
5.158 - *
5.159 - * @param name resource name to find
5.160 - * @return the array of bytes in the given resource
5.161 - * @throws IOException I/O in case something goes wrong
5.162 - */
5.163 - public static byte[] read(String name) throws IOException {
5.164 - URL u = null;
5.165 - Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
5.166 - while (en.hasMoreElements()) {
5.167 - u = en.nextElement();
5.168 - }
5.169 - if (u == null) {
5.170 - throw new IOException("Can't find " + name);
5.171 - }
5.172 - try (InputStream is = u.openStream()) {
5.173 - byte[] arr;
5.174 - arr = new byte[is.available()];
5.175 - int offset = 0;
5.176 - while (offset < arr.length) {
5.177 - int len = is.read(arr, offset, arr.length - offset);
5.178 - if (len == -1) {
5.179 - throw new IOException("Can't read " + name);
5.180 - }
5.181 - offset += len;
5.182 - }
5.183 - return arr;
5.184 - }
5.185 - }
5.186 -
5.187 - private static Object invokeMethod(String clazz, String method)
5.188 - throws ClassNotFoundException, InvocationTargetException,
5.189 - SecurityException, IllegalAccessException, IllegalArgumentException,
5.190 - InstantiationException {
5.191 - Method found = null;
5.192 - Class<?> c = Class.forName(clazz);
5.193 - for (Method m : c.getMethods()) {
5.194 - if (m.getName().equals(method)) {
5.195 - found = m;
5.196 - }
5.197 - }
5.198 - Object res;
5.199 - if (found != null) {
5.200 - try {
5.201 - if ((found.getModifiers() & Modifier.STATIC) != 0) {
5.202 - res = found.invoke(null);
5.203 - } else {
5.204 - res = found.invoke(c.newInstance());
5.205 - }
5.206 - } catch (Throwable ex) {
5.207 - res = ex.getClass().getName() + ":" + ex.getMessage();
5.208 - }
5.209 - } else {
5.210 - res = "Can't find method " + method + " in " + clazz;
5.211 - }
5.212 - return res;
5.213 - }
5.214 -
5.215 - @JavaScriptBody(args = {}, body = "vm.desiredAssertionStatus = true;")
5.216 - private static void turnAssetionStatusOn() {
5.217 - }
5.218 -
5.219 - private static final class Case {
5.220 - private final Object data;
5.221 -
5.222 - private Case(Object data) {
5.223 - this.data = data;
5.224 - }
5.225 -
5.226 - public static Case parseData(String s) {
5.227 - return new Case(toJSON(s));
5.228 - }
5.229 -
5.230 - public String getMethodName() {
5.231 - return value("methodName", data);
5.232 - }
5.233 -
5.234 - public String getClassName() {
5.235 - return value("className", data);
5.236 - }
5.237 -
5.238 - public String getRequestId() {
5.239 - return value("request", data);
5.240 - }
5.241 -
5.242 - public String getHtmlFragment() {
5.243 - return value("html", data);
5.244 - }
5.245 -
5.246 - @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
5.247 - private static native Object toJSON(String s);
5.248 -
5.249 - @JavaScriptBody(args = {"p", "d"}, body =
5.250 - "var v = d[p];\n"
5.251 - + "if (typeof v === 'undefined') return null;\n"
5.252 - + "return v.toString();"
5.253 - )
5.254 - private static native String value(String p, Object d);
5.255 - }
5.256 -}
6.1 --- a/dew/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java Wed Jan 23 13:56:54 2013 +0100
6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
6.3 @@ -1,126 +0,0 @@
6.4 -/**
6.5 - * Back 2 Browser Bytecode Translator
6.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
6.7 - *
6.8 - * This program is free software: you can redistribute it and/or modify
6.9 - * it under the terms of the GNU General Public License as published by
6.10 - * the Free Software Foundation, version 2 of the License.
6.11 - *
6.12 - * This program is distributed in the hope that it will be useful,
6.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
6.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6.15 - * GNU General Public License for more details.
6.16 - *
6.17 - * You should have received a copy of the GNU General Public License
6.18 - * along with this program. Look for COPYING file in the top folder.
6.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
6.20 - */
6.21 -package org.apidesign.bck2brwsr.launcher;
6.22 -
6.23 -import java.io.IOException;
6.24 -import java.io.InputStream;
6.25 -import java.net.URL;
6.26 -import java.util.Enumeration;
6.27 -import java.util.LinkedHashSet;
6.28 -import java.util.Set;
6.29 -import java.util.logging.Logger;
6.30 -import javax.script.Invocable;
6.31 -import javax.script.ScriptEngine;
6.32 -import javax.script.ScriptEngineManager;
6.33 -import javax.script.ScriptException;
6.34 -import org.apidesign.vm4brwsr.Bck2Brwsr;
6.35 -
6.36 -/**
6.37 - * Tests execution in Java's internal scripting engine.
6.38 - */
6.39 -final class JSLauncher extends Launcher {
6.40 - private static final Logger LOG = Logger.getLogger(JSLauncher.class.getName());
6.41 - private Set<ClassLoader> loaders = new LinkedHashSet<>();
6.42 - private final Res resources = new Res();
6.43 - private Invocable code;
6.44 - private StringBuilder codeSeq;
6.45 - private Object console;
6.46 -
6.47 -
6.48 - @Override MethodInvocation addMethod(Class<?> clazz, String method, String html) {
6.49 - loaders.add(clazz.getClassLoader());
6.50 - MethodInvocation mi = new MethodInvocation(clazz.getName(), method, html);
6.51 - try {
6.52 - mi.result(code.invokeMethod(
6.53 - console,
6.54 - "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
6.55 - mi.className, mi.methodName).toString(), null);
6.56 - } catch (ScriptException | NoSuchMethodException ex) {
6.57 - mi.result(null, ex);
6.58 - }
6.59 - return mi;
6.60 - }
6.61 -
6.62 - public void addClassLoader(ClassLoader url) {
6.63 - this.loaders.add(url);
6.64 - }
6.65 -
6.66 - @Override
6.67 - public void initialize() throws IOException {
6.68 - try {
6.69 - initRhino();
6.70 - } catch (Exception ex) {
6.71 - if (ex instanceof IOException) {
6.72 - throw (IOException)ex;
6.73 - }
6.74 - if (ex instanceof RuntimeException) {
6.75 - throw (RuntimeException)ex;
6.76 - }
6.77 - throw new IOException(ex);
6.78 - }
6.79 - }
6.80 -
6.81 - private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
6.82 - StringBuilder sb = new StringBuilder();
6.83 - Bck2Brwsr.generate(sb, new Res());
6.84 -
6.85 - ScriptEngineManager sem = new ScriptEngineManager();
6.86 - ScriptEngine mach = sem.getEngineByExtension("js");
6.87 -
6.88 - sb.append(
6.89 - "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.Console.read);"
6.90 - + "\nfunction initVM() { return vm; };"
6.91 - + "\n");
6.92 -
6.93 - Object res = mach.eval(sb.toString());
6.94 - if (!(mach instanceof Invocable)) {
6.95 - throw new IOException("It is invocable object: " + res);
6.96 - }
6.97 - code = (Invocable) mach;
6.98 - codeSeq = sb;
6.99 -
6.100 - Object vm = code.invokeFunction("initVM");
6.101 - console = code.invokeMethod(vm, "loadClass", Console.class.getName());
6.102 - }
6.103 -
6.104 - @Override
6.105 - public void shutdown() throws IOException {
6.106 - }
6.107 -
6.108 - @Override
6.109 - public String toString() {
6.110 - return codeSeq.toString();
6.111 - }
6.112 -
6.113 - private class Res implements Bck2Brwsr.Resources {
6.114 - @Override
6.115 - public InputStream get(String resource) throws IOException {
6.116 - for (ClassLoader l : loaders) {
6.117 - URL u = null;
6.118 - Enumeration<URL> en = l.getResources(resource);
6.119 - while (en.hasMoreElements()) {
6.120 - u = en.nextElement();
6.121 - }
6.122 - if (u != null) {
6.123 - return u.openStream();
6.124 - }
6.125 - }
6.126 - throw new IOException("Can't find " + resource);
6.127 - }
6.128 - }
6.129 -}
7.1 --- a/dew/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java Wed Jan 23 13:56:54 2013 +0100
7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
7.3 @@ -1,63 +0,0 @@
7.4 -/**
7.5 - * Back 2 Browser Bytecode Translator
7.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
7.7 - *
7.8 - * This program is free software: you can redistribute it and/or modify
7.9 - * it under the terms of the GNU General Public License as published by
7.10 - * the Free Software Foundation, version 2 of the License.
7.11 - *
7.12 - * This program is distributed in the hope that it will be useful,
7.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
7.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7.15 - * GNU General Public License for more details.
7.16 - *
7.17 - * You should have received a copy of the GNU General Public License
7.18 - * along with this program. Look for COPYING file in the top folder.
7.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
7.20 - */
7.21 -package org.apidesign.bck2brwsr.launcher;
7.22 -
7.23 -import java.io.Closeable;
7.24 -import java.io.IOException;
7.25 -import java.net.URLClassLoader;
7.26 -import org.apidesign.vm4brwsr.Bck2Brwsr;
7.27 -
7.28 -/** An abstraction for executing tests in a Bck2Brwsr virtual machine.
7.29 - * Either in JavaScript engine, or in external browser.
7.30 - *
7.31 - * @author Jaroslav Tulach <jtulach@netbeans.org>
7.32 - */
7.33 -public abstract class Launcher {
7.34 -
7.35 - Launcher() {
7.36 - }
7.37 -
7.38 - abstract MethodInvocation addMethod(Class<?> clazz, String method, String html) throws IOException;
7.39 -
7.40 - public abstract void initialize() throws IOException;
7.41 - public abstract void shutdown() throws IOException;
7.42 - public MethodInvocation invokeMethod(Class<?> clazz, String method, String html) throws IOException {
7.43 - return addMethod(clazz, method, html);
7.44 - }
7.45 -
7.46 -
7.47 -
7.48 - public static Launcher createJavaScript() {
7.49 - final JSLauncher l = new JSLauncher();
7.50 - l.addClassLoader(Bck2Brwsr.class.getClassLoader());
7.51 - return l;
7.52 - }
7.53 -
7.54 - public static Launcher createBrowser(String cmd) {
7.55 - final Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(cmd);
7.56 - l.addClassLoader(Bck2Brwsr.class.getClassLoader());
7.57 - l.setTimeout(180000);
7.58 - return l;
7.59 - }
7.60 - public static Closeable showURL(URLClassLoader classes, String startpage) throws IOException {
7.61 - Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(null);
7.62 - l.addClassLoader(classes);
7.63 - l.showURL(startpage);
7.64 - return l;
7.65 - }
7.66 -}
8.1 --- a/dew/src/main/java/org/apidesign/bck2brwsr/launcher/MethodInvocation.java Wed Jan 23 13:56:54 2013 +0100
8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
8.3 @@ -1,59 +0,0 @@
8.4 -/**
8.5 - * Back 2 Browser Bytecode Translator
8.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
8.7 - *
8.8 - * This program is free software: you can redistribute it and/or modify
8.9 - * it under the terms of the GNU General Public License as published by
8.10 - * the Free Software Foundation, version 2 of the License.
8.11 - *
8.12 - * This program is distributed in the hope that it will be useful,
8.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
8.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8.15 - * GNU General Public License for more details.
8.16 - *
8.17 - * You should have received a copy of the GNU General Public License
8.18 - * along with this program. Look for COPYING file in the top folder.
8.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
8.20 - */
8.21 -package org.apidesign.bck2brwsr.launcher;
8.22 -
8.23 -import java.util.concurrent.CountDownLatch;
8.24 -import java.util.concurrent.TimeUnit;
8.25 -
8.26 -/**
8.27 - *
8.28 - * @author Jaroslav Tulach <jtulach@netbeans.org>
8.29 - */
8.30 -public final class MethodInvocation {
8.31 - final CountDownLatch wait = new CountDownLatch(1);
8.32 - final String className;
8.33 - final String methodName;
8.34 - final String html;
8.35 - private String result;
8.36 - private Throwable exception;
8.37 -
8.38 - MethodInvocation(String className, String methodName, String html) {
8.39 - this.className = className;
8.40 - this.methodName = methodName;
8.41 - this.html = html;
8.42 - }
8.43 -
8.44 - void await(long timeOut) throws InterruptedException {
8.45 - wait.await(timeOut, TimeUnit.MILLISECONDS);
8.46 - }
8.47 -
8.48 - void result(String r, Throwable e) {
8.49 - this.result = r;
8.50 - this.exception = e;
8.51 - wait.countDown();
8.52 - }
8.53 -
8.54 - @Override
8.55 - public String toString() {
8.56 - if (exception != null) {
8.57 - return exception.toString();
8.58 - }
8.59 - return result;
8.60 - }
8.61 -
8.62 -}