1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Sun Dec 23 17:02:34 2012 +0100
1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Sun Dec 23 18:24:18 2012 +0100
1.3 @@ -32,7 +32,9 @@
1.4 import java.util.LinkedHashSet;
1.5 import java.util.List;
1.6 import java.util.Set;
1.7 +import java.util.concurrent.BlockingQueue;
1.8 import java.util.concurrent.CountDownLatch;
1.9 +import java.util.concurrent.LinkedBlockingQueue;
1.10 import java.util.concurrent.TimeUnit;
1.11 import java.util.logging.Level;
1.12 import java.util.logging.Logger;
1.13 @@ -53,16 +55,23 @@
1.14 public class Bck2BrwsrLauncher {
1.15 private static final Logger LOG = Logger.getLogger(Bck2BrwsrLauncher.class.getName());
1.16 private Set<ClassLoader> loaders = new LinkedHashSet<>();
1.17 - private List<MethodInvocation> methods = new ArrayList<>();
1.18 + private BlockingQueue<MethodInvocation> methods = new LinkedBlockingQueue<>();
1.19 private long timeOut;
1.20 - private String showURL;
1.21 private final Res resources = new Res();
1.22 + private Object[] brwsr;
1.23 + private HttpServer server;
1.24 + private CountDownLatch wait;
1.25
1.26
1.27 - public MethodInvocation addMethod(Class<?> clazz, String method) {
1.28 + public MethodInvocation addMethod(Class<?> clazz, String method) throws IOException {
1.29 loaders.add(clazz.getClassLoader());
1.30 MethodInvocation c = new MethodInvocation(clazz.getName(), method);
1.31 methods.add(c);
1.32 + try {
1.33 + c.await(timeOut);
1.34 + } catch (InterruptedException ex) {
1.35 + throw new IOException(ex);
1.36 + }
1.37 return c;
1.38 }
1.39
1.40 @@ -70,35 +79,29 @@
1.41 timeOut = ms;
1.42 }
1.43
1.44 - public void setStartPage(String startpage) {
1.45 - if (!startpage.startsWith("/")) {
1.46 - startpage = "/" + startpage;
1.47 - }
1.48 - this.showURL = startpage;
1.49 - }
1.50 -
1.51 public void addClassLoader(ClassLoader url) {
1.52 this.loaders.add(url);
1.53 }
1.54
1.55 public static void main( String[] args ) throws Exception {
1.56 Bck2BrwsrLauncher l = new Bck2BrwsrLauncher();
1.57 - l.setStartPage("org/apidesign/bck2brwsr/launcher/console.xhtml");
1.58 l.addClassLoader(Bck2BrwsrLauncher.class.getClassLoader());
1.59 - l.execute();
1.60 + l.showURL("org/apidesign/bck2brwsr/launcher/console.xhtml");
1.61 System.in.read();
1.62 }
1.63
1.64 + public void showURL(String startpage) throws URISyntaxException, InterruptedException, IOException {
1.65 + if (!startpage.startsWith("/")) {
1.66 + startpage = "/" + startpage;
1.67 + }
1.68 + HttpServer s = initServer();
1.69 + s.getServerConfiguration().addHttpHandler(new Page(resources, null), "/");
1.70 + launchServerAndBrwsr(s, startpage);
1.71 + }
1.72
1.73 - public void execute() throws IOException {
1.74 + public void initialize() throws IOException {
1.75 try {
1.76 - if (showURL != null) {
1.77 - HttpServer server = initServer();
1.78 - server.getServerConfiguration().addHttpHandler(new Page(resources, null), "/");
1.79 - launchServerAndBrwsr(server, showURL);
1.80 - } else {
1.81 - executeInBrowser();
1.82 - }
1.83 + executeInBrowser();
1.84 } catch (InterruptedException ex) {
1.85 final InterruptedIOException iio = new InterruptedIOException(ex.getMessage());
1.86 iio.initCause(ex);
1.87 @@ -115,9 +118,9 @@
1.88 }
1.89
1.90 private HttpServer initServer() {
1.91 - HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
1.92 + HttpServer s = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
1.93
1.94 - final ServerConfiguration conf = server.getServerConfiguration();
1.95 + final ServerConfiguration conf = s.getServerConfiguration();
1.96 conf.addHttpHandler(new Page(resources,
1.97 "org/apidesign/bck2brwsr/launcher/console.xhtml",
1.98 "org.apidesign.bck2brwsr.launcher.Console", "welcome", "false"
1.99 @@ -125,21 +128,19 @@
1.100 conf.addHttpHandler(new VM(resources), "/bck2brwsr.js");
1.101 conf.addHttpHandler(new VMInit(), "/vm.js");
1.102 conf.addHttpHandler(new Classes(resources), "/classes/");
1.103 - return server;
1.104 + return s;
1.105 }
1.106
1.107 private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
1.108 - final CountDownLatch wait = new CountDownLatch(1);
1.109 - final MethodInvocation[] cases = this.methods.toArray(new MethodInvocation[0]);
1.110 -
1.111 - HttpServer server = initServer();
1.112 + wait = new CountDownLatch(1);
1.113 + server = initServer();
1.114 ServerConfiguration conf = server.getServerConfiguration();
1.115 conf.addHttpHandler(new Page(resources,
1.116 "org/apidesign/bck2brwsr/launcher/harness.xhtml"
1.117 ), "/execute");
1.118 - final int[] currentTest = { -1 };
1.119 conf.addHttpHandler(new HttpHandler() {
1.120 int cnt;
1.121 + List<MethodInvocation> cases = new ArrayList<>();
1.122 @Override
1.123 public void service(Request request, Response response) throws Exception {
1.124 String id = request.getParameter("request");
1.125 @@ -148,19 +149,20 @@
1.126 if (id != null && value != null) {
1.127 LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value});
1.128 value = value.replace("%20", " ");
1.129 - cases[Integer.parseInt(id)].result = value;
1.130 + cases.get(Integer.parseInt(id)).result(value, null);
1.131 }
1.132 - currentTest[0] = cnt;
1.133
1.134 - if (cnt >= cases.length) {
1.135 + MethodInvocation mi = methods.take();
1.136 + if (mi == null) {
1.137 response.getWriter().write("");
1.138 wait.countDown();
1.139 cnt = 0;
1.140 return;
1.141 }
1.142
1.143 - final String cn = cases[cnt].className;
1.144 - final String mn = cases[cnt].methodName;
1.145 + cases.add(mi);
1.146 + final String cn = mi.className;
1.147 + final String mn = mi.methodName;
1.148 LOG.log(Level.INFO, "Request for {0} case. Sending {1}.{2}", new Object[]{cnt, cn, mn});
1.149 response.getWriter().write("{"
1.150 + "className: '" + cn + "', "
1.151 @@ -171,24 +173,26 @@
1.152 }
1.153 }, "/data");
1.154
1.155 - Object[] brwsr = launchServerAndBrwsr(server, "/execute");
1.156 -
1.157 + this.brwsr = launchServerAndBrwsr(server, "/execute");
1.158 + }
1.159 +
1.160 + public void shutdown() throws InterruptedException, IOException {
1.161 for (;;) {
1.162 - int prev = currentTest[0];
1.163 + int prev = methods.size();
1.164 if (wait.await(timeOut, TimeUnit.MILLISECONDS)) {
1.165 break;
1.166 }
1.167 - if (prev == currentTest[0]) {
1.168 + if (prev == methods.size()) {
1.169 LOG.log(
1.170 Level.WARNING,
1.171 "Timeout and no test has been executed meanwhile (at {0}). Giving up.",
1.172 - currentTest[0]
1.173 + methods.size()
1.174 );
1.175 break;
1.176 }
1.177 LOG.log(Level.INFO,
1.178 "Timeout, but tests got from {0} to {1}. Trying again.",
1.179 - new Object[]{prev, currentTest[0]}
1.180 + new Object[]{prev, methods.size()}
1.181 );
1.182 }
1.183 stopServerAndBrwsr(server, brwsr);
2.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java Sun Dec 23 17:02:34 2012 +0100
2.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java Sun Dec 23 18:24:18 2012 +0100
2.3 @@ -45,14 +45,12 @@
2.4 loaders.add(clazz.getClassLoader());
2.5 MethodInvocation mi = new MethodInvocation(clazz.getName(), method);
2.6 try {
2.7 - mi.result = code.invokeMethod(
2.8 + mi.result(code.invokeMethod(
2.9 console,
2.10 "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
2.11 - mi.className, mi.methodName).toString();
2.12 - } catch (ScriptException scriptException) {
2.13 - mi.exception = scriptException;
2.14 - } catch (NoSuchMethodException noSuchMethodException) {
2.15 - mi.exception = noSuchMethodException;
2.16 + mi.className, mi.methodName).toString(), null);
2.17 + } catch (ScriptException | NoSuchMethodException ex) {
2.18 + mi.result(null, ex);
2.19 }
2.20 return mi;
2.21 }
3.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/MethodInvocation.java Sun Dec 23 17:02:34 2012 +0100
3.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/MethodInvocation.java Sun Dec 23 18:24:18 2012 +0100
3.3 @@ -17,20 +17,34 @@
3.4 */
3.5 package org.apidesign.bck2brwsr.launcher;
3.6
3.7 +import java.util.concurrent.CountDownLatch;
3.8 +import java.util.concurrent.TimeUnit;
3.9 +
3.10 /**
3.11 *
3.12 * @author Jaroslav Tulach <jtulach@netbeans.org>
3.13 */
3.14 public final class MethodInvocation {
3.15 + final CountDownLatch wait = new CountDownLatch(1);
3.16 final String className;
3.17 final String methodName;
3.18 - String result;
3.19 - Exception exception;
3.20 + private String result;
3.21 + private Exception exception;
3.22
3.23 MethodInvocation(String className, String methodName) {
3.24 this.className = className;
3.25 this.methodName = methodName;
3.26 }
3.27 +
3.28 + void await(long timeOut) throws InterruptedException {
3.29 + wait.await(timeOut, TimeUnit.MILLISECONDS);
3.30 + }
3.31 +
3.32 + void result(String r, Exception e) {
3.33 + this.result = r;
3.34 + this.exception = e;
3.35 + wait.countDown();
3.36 + }
3.37
3.38 @Override
3.39 public String toString() {
4.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java Sun Dec 23 17:02:34 2012 +0100
4.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java Sun Dec 23 18:24:18 2012 +0100
4.3 @@ -52,6 +52,7 @@
4.4 launcher = js;
4.5 } else {
4.6 Bck2BrwsrLauncher l = new Bck2BrwsrLauncher();
4.7 + l.initialize();
4.8 l.setTimeout(180000);
4.9 launcher = l;
4.10 }
4.11 @@ -65,9 +66,6 @@
4.12
4.13 void exec() throws Exception {
4.14 Object l = clear();
4.15 - if (l instanceof Bck2BrwsrLauncher) {
4.16 - ((Bck2BrwsrLauncher)l).execute();
4.17 - }
4.18 }
4.19
4.20 }