# HG changeset patch # User Jaroslav Tulach # Date 1356283458 -3600 # Node ID bafc670aa10d519552ba06bc11f34c4f3018b926 # Parent ed48023d1d85ad2051c6cf4d97539715c10790e0 Separating initialization and method execution on HTTP launcher as well diff -r ed48023d1d85 -r bafc670aa10d launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Sun Dec 23 17:02:34 2012 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Sun Dec 23 18:24:18 2012 +0100 @@ -32,7 +32,9 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; @@ -53,16 +55,23 @@ public class Bck2BrwsrLauncher { private static final Logger LOG = Logger.getLogger(Bck2BrwsrLauncher.class.getName()); private Set loaders = new LinkedHashSet<>(); - private List methods = new ArrayList<>(); + private BlockingQueue methods = new LinkedBlockingQueue<>(); private long timeOut; - private String showURL; private final Res resources = new Res(); + private Object[] brwsr; + private HttpServer server; + private CountDownLatch wait; - public MethodInvocation addMethod(Class clazz, String method) { + public MethodInvocation addMethod(Class clazz, String method) throws IOException { loaders.add(clazz.getClassLoader()); MethodInvocation c = new MethodInvocation(clazz.getName(), method); methods.add(c); + try { + c.await(timeOut); + } catch (InterruptedException ex) { + throw new IOException(ex); + } return c; } @@ -70,35 +79,29 @@ timeOut = ms; } - public void setStartPage(String startpage) { - if (!startpage.startsWith("/")) { - startpage = "/" + startpage; - } - this.showURL = startpage; - } - public void addClassLoader(ClassLoader url) { this.loaders.add(url); } public static void main( String[] args ) throws Exception { Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(); - l.setStartPage("org/apidesign/bck2brwsr/launcher/console.xhtml"); l.addClassLoader(Bck2BrwsrLauncher.class.getClassLoader()); - l.execute(); + l.showURL("org/apidesign/bck2brwsr/launcher/console.xhtml"); System.in.read(); } + public void showURL(String startpage) throws URISyntaxException, InterruptedException, IOException { + if (!startpage.startsWith("/")) { + startpage = "/" + startpage; + } + HttpServer s = initServer(); + s.getServerConfiguration().addHttpHandler(new Page(resources, null), "/"); + launchServerAndBrwsr(s, startpage); + } - public void execute() throws IOException { + public void initialize() throws IOException { try { - if (showURL != null) { - HttpServer server = initServer(); - server.getServerConfiguration().addHttpHandler(new Page(resources, null), "/"); - launchServerAndBrwsr(server, showURL); - } else { - executeInBrowser(); - } + executeInBrowser(); } catch (InterruptedException ex) { final InterruptedIOException iio = new InterruptedIOException(ex.getMessage()); iio.initCause(ex); @@ -115,9 +118,9 @@ } private HttpServer initServer() { - HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535)); + HttpServer s = HttpServer.createSimpleServer(".", new PortRange(8080, 65535)); - final ServerConfiguration conf = server.getServerConfiguration(); + final ServerConfiguration conf = s.getServerConfiguration(); conf.addHttpHandler(new Page(resources, "org/apidesign/bck2brwsr/launcher/console.xhtml", "org.apidesign.bck2brwsr.launcher.Console", "welcome", "false" @@ -125,21 +128,19 @@ conf.addHttpHandler(new VM(resources), "/bck2brwsr.js"); conf.addHttpHandler(new VMInit(), "/vm.js"); conf.addHttpHandler(new Classes(resources), "/classes/"); - return server; + return s; } private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException { - final CountDownLatch wait = new CountDownLatch(1); - final MethodInvocation[] cases = this.methods.toArray(new MethodInvocation[0]); - - HttpServer server = initServer(); + wait = new CountDownLatch(1); + server = initServer(); ServerConfiguration conf = server.getServerConfiguration(); conf.addHttpHandler(new Page(resources, "org/apidesign/bck2brwsr/launcher/harness.xhtml" ), "/execute"); - final int[] currentTest = { -1 }; conf.addHttpHandler(new HttpHandler() { int cnt; + List cases = new ArrayList<>(); @Override public void service(Request request, Response response) throws Exception { String id = request.getParameter("request"); @@ -148,19 +149,20 @@ if (id != null && value != null) { LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value}); value = value.replace("%20", " "); - cases[Integer.parseInt(id)].result = value; + cases.get(Integer.parseInt(id)).result(value, null); } - currentTest[0] = cnt; - if (cnt >= cases.length) { + MethodInvocation mi = methods.take(); + if (mi == null) { response.getWriter().write(""); wait.countDown(); cnt = 0; return; } - final String cn = cases[cnt].className; - final String mn = cases[cnt].methodName; + cases.add(mi); + final String cn = mi.className; + final String mn = mi.methodName; LOG.log(Level.INFO, "Request for {0} case. Sending {1}.{2}", new Object[]{cnt, cn, mn}); response.getWriter().write("{" + "className: '" + cn + "', " @@ -171,24 +173,26 @@ } }, "/data"); - Object[] brwsr = launchServerAndBrwsr(server, "/execute"); - + this.brwsr = launchServerAndBrwsr(server, "/execute"); + } + + public void shutdown() throws InterruptedException, IOException { for (;;) { - int prev = currentTest[0]; + int prev = methods.size(); if (wait.await(timeOut, TimeUnit.MILLISECONDS)) { break; } - if (prev == currentTest[0]) { + if (prev == methods.size()) { LOG.log( Level.WARNING, "Timeout and no test has been executed meanwhile (at {0}). Giving up.", - currentTest[0] + methods.size() ); break; } LOG.log(Level.INFO, "Timeout, but tests got from {0} to {1}. Trying again.", - new Object[]{prev, currentTest[0]} + new Object[]{prev, methods.size()} ); } stopServerAndBrwsr(server, brwsr); diff -r ed48023d1d85 -r bafc670aa10d launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java Sun Dec 23 17:02:34 2012 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java Sun Dec 23 18:24:18 2012 +0100 @@ -45,14 +45,12 @@ loaders.add(clazz.getClassLoader()); MethodInvocation mi = new MethodInvocation(clazz.getName(), method); try { - mi.result = code.invokeMethod( + mi.result(code.invokeMethod( console, "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2", - mi.className, mi.methodName).toString(); - } catch (ScriptException scriptException) { - mi.exception = scriptException; - } catch (NoSuchMethodException noSuchMethodException) { - mi.exception = noSuchMethodException; + mi.className, mi.methodName).toString(), null); + } catch (ScriptException | NoSuchMethodException ex) { + mi.result(null, ex); } return mi; } diff -r ed48023d1d85 -r bafc670aa10d launcher/src/main/java/org/apidesign/bck2brwsr/launcher/MethodInvocation.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/MethodInvocation.java Sun Dec 23 17:02:34 2012 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/MethodInvocation.java Sun Dec 23 18:24:18 2012 +0100 @@ -17,20 +17,34 @@ */ package org.apidesign.bck2brwsr.launcher; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + /** * * @author Jaroslav Tulach */ public final class MethodInvocation { + final CountDownLatch wait = new CountDownLatch(1); final String className; final String methodName; - String result; - Exception exception; + private String result; + private Exception exception; MethodInvocation(String className, String methodName) { this.className = className; this.methodName = methodName; } + + void await(long timeOut) throws InterruptedException { + wait.await(timeOut, TimeUnit.MILLISECONDS); + } + + void result(String r, Exception e) { + this.result = r; + this.exception = e; + wait.countDown(); + } @Override public String toString() { diff -r ed48023d1d85 -r bafc670aa10d vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java Sun Dec 23 17:02:34 2012 +0100 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java Sun Dec 23 18:24:18 2012 +0100 @@ -52,6 +52,7 @@ launcher = js; } else { Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(); + l.initialize(); l.setTimeout(180000); launcher = l; } @@ -65,9 +66,6 @@ void exec() throws Exception { Object l = clear(); - if (l instanceof Bck2BrwsrLauncher) { - ((Bck2BrwsrLauncher)l).execute(); - } } }