# HG changeset patch # User Jaroslav Tulach # Date 1451114579 -3600 # Node ID 23babc7d5bb8f43bdeacc86f1c39ac27e4fed0d1 # Parent bc07f6a1e63955c01477884edba1eef067653629 Can tell the server to exit diff -r bc07f6a1e639 -r 23babc7d5bb8 javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java Sat Sep 26 07:59:41 2015 +0200 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java Sat Dec 26 08:22:59 2015 +0100 @@ -18,6 +18,7 @@ package org.apidesign.bck2brwsr.demo.calc.staticcompilation; import java.util.List; +import org.apidesign.bck2brwsr.core.JavaScriptBody; import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty; import org.apidesign.bck2brwsr.htmlpage.api.On; import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*; @@ -41,6 +42,7 @@ public class Calc { public static void main(String... args) throws Exception { new Calculator().applyBindings().setOperation("plus"); + notifyFinish(); } @On(event = CLICK, id="clear") @@ -131,4 +133,11 @@ static boolean emptyHistory(List history) { return history.isEmpty(); } + + @JavaScriptBody(args = { }, body = + "var xhttp = new XMLHttpRequest();\n" + + "xhttp.open('GET', '/?exit=true', true);\n" + + "xhttp.send();\n" + ) + private static native void notifyFinish(); } diff -r bc07f6a1e639 -r 23babc7d5bb8 launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Sat Sep 26 07:59:41 2015 +0200 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Sat Dec 26 08:22:59 2015 +0100 @@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; +import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; @@ -73,7 +74,7 @@ * Supports execution in native browser as well as Java's internal * execution engine. */ -abstract class BaseHTTPLauncher extends Launcher implements Closeable, Callable { +abstract class BaseHTTPLauncher extends Launcher implements Flushable, Closeable, Callable { static final Logger LOG = Logger.getLogger(BaseHTTPLauncher.class.getName()); private static final InvocationContext END = new InvocationContext(null, null, null); private final Set loaders = new LinkedHashSet(); @@ -84,6 +85,7 @@ private Object[] brwsr; private HttpServer server; private CountDownLatch wait; + private Thread flushing; public BaseHTTPLauncher(String cmd) { this.cmd = cmd; @@ -418,6 +420,12 @@ @Override public void shutdown() throws IOException { + synchronized (this) { + if (flushing != null) { + flushing.interrupt(); + flushing = null; + } + } methods.offer(END); for (;;) { int prev = methods.size(); @@ -547,6 +555,18 @@ } @Override + public synchronized void flush() throws IOException { + flushing = Thread.currentThread(); + while (flushing == Thread.currentThread()) { + try { + wait(); + } catch (InterruptedException ex) { + LOG.log(Level.FINE, null, ex); + } + } + } + + @Override public void close() throws IOException { shutdown(); } @@ -776,6 +796,10 @@ @Override public void service(Request request, Response response) throws Exception { + if ("true".equals(request.getParameter("exit"))) { + LOG.info("Exit request received. Shutting down!"); + shutdown(); + } if (request.getRequestURI().equals(vmResource)) { response.setCharacterEncoding("UTF-8"); response.setContentType("text/javascript"); diff -r bc07f6a1e639 -r 23babc7d5bb8 rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/ShowMojo.java --- a/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/ShowMojo.java Sat Sep 26 07:59:41 2015 +0200 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/ShowMojo.java Sat Dec 26 08:22:59 2015 +0100 @@ -21,6 +21,7 @@ import org.apache.maven.plugin.AbstractMojo; import java.io.File; +import java.io.Flushable; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; @@ -75,7 +76,11 @@ try { Closeable httpServer; httpServer = Launcher.showDir(launcher, directory, null, startpage); - System.in.read(); + if (httpServer instanceof Flushable) { + ((Flushable)httpServer).flush(); + } else { + System.in.read(); + } httpServer.close(); } catch (IOException ex) { throw new MojoExecutionException("Can't show the browser", ex);