Can tell the server to exit
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Dec 2015 08:22:59 +0100
changeset 185023babc7d5bb8
parent 1849 bc07f6a1e639
child 1851 c78122e85b31
Can tell the server to exit
javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java
launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java
rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/ShowMojo.java
     1.1 --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java	Sat Sep 26 07:59:41 2015 +0200
     1.2 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calc.java	Sat Dec 26 08:22:59 2015 +0100
     1.3 @@ -18,6 +18,7 @@
     1.4  package org.apidesign.bck2brwsr.demo.calc.staticcompilation;
     1.5  
     1.6  import java.util.List;
     1.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
     1.8  import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
     1.9  import org.apidesign.bck2brwsr.htmlpage.api.On;
    1.10  import static org.apidesign.bck2brwsr.htmlpage.api.OnEvent.*;
    1.11 @@ -41,6 +42,7 @@
    1.12  public class Calc {
    1.13      public static void main(String... args) throws Exception {
    1.14          new Calculator().applyBindings().setOperation("plus");
    1.15 +        notifyFinish();
    1.16      }
    1.17      
    1.18      @On(event = CLICK, id="clear")
    1.19 @@ -131,4 +133,11 @@
    1.20      static boolean emptyHistory(List<?> history) {
    1.21          return history.isEmpty();
    1.22      }
    1.23 +
    1.24 +    @JavaScriptBody(args = {  }, body =
    1.25 +        "var xhttp = new XMLHttpRequest();\n" +
    1.26 +        "xhttp.open('GET', '/?exit=true', true);\n" +
    1.27 +        "xhttp.send();\n"
    1.28 +    )
    1.29 +    private static native void notifyFinish();
    1.30  }
     2.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Sat Sep 26 07:59:41 2015 +0200
     2.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Sat Dec 26 08:22:59 2015 +0100
     2.3 @@ -21,6 +21,7 @@
     2.4  import java.io.ByteArrayOutputStream;
     2.5  import java.io.Closeable;
     2.6  import java.io.File;
     2.7 +import java.io.Flushable;
     2.8  import java.io.IOException;
     2.9  import java.io.InputStream;
    2.10  import java.io.InterruptedIOException;
    2.11 @@ -73,7 +74,7 @@
    2.12   * Supports execution in native browser as well as Java's internal
    2.13   * execution engine.
    2.14   */
    2.15 -abstract class BaseHTTPLauncher extends Launcher implements Closeable, Callable<HttpServer> {
    2.16 +abstract class BaseHTTPLauncher extends Launcher implements Flushable, Closeable, Callable<HttpServer> {
    2.17      static final Logger LOG = Logger.getLogger(BaseHTTPLauncher.class.getName());
    2.18      private static final InvocationContext END = new InvocationContext(null, null, null);
    2.19      private final Set<ClassLoader> loaders = new LinkedHashSet<ClassLoader>();
    2.20 @@ -84,6 +85,7 @@
    2.21      private Object[] brwsr;
    2.22      private HttpServer server;
    2.23      private CountDownLatch wait;
    2.24 +    private Thread flushing;
    2.25  
    2.26      public BaseHTTPLauncher(String cmd) {
    2.27          this.cmd = cmd;
    2.28 @@ -418,6 +420,12 @@
    2.29  
    2.30      @Override
    2.31      public void shutdown() throws IOException {
    2.32 +        synchronized (this) {
    2.33 +            if (flushing != null) {
    2.34 +                flushing.interrupt();
    2.35 +                flushing = null;
    2.36 +            }
    2.37 +        }
    2.38          methods.offer(END);
    2.39          for (;;) {
    2.40              int prev = methods.size();
    2.41 @@ -547,6 +555,18 @@
    2.42      }
    2.43  
    2.44      @Override
    2.45 +    public synchronized void flush() throws IOException {
    2.46 +        flushing = Thread.currentThread();
    2.47 +        while (flushing == Thread.currentThread()) {
    2.48 +            try {
    2.49 +                wait();
    2.50 +            } catch (InterruptedException ex) {
    2.51 +                LOG.log(Level.FINE, null, ex);
    2.52 +            }
    2.53 +        }
    2.54 +    }
    2.55 +
    2.56 +    @Override
    2.57      public void close() throws IOException {
    2.58          shutdown();
    2.59      }
    2.60 @@ -776,6 +796,10 @@
    2.61  
    2.62          @Override
    2.63          public void service(Request request, Response response) throws Exception {
    2.64 +            if ("true".equals(request.getParameter("exit"))) {
    2.65 +                LOG.info("Exit request received. Shutting down!");
    2.66 +                shutdown();
    2.67 +            }
    2.68              if (request.getRequestURI().equals(vmResource)) {
    2.69                  response.setCharacterEncoding("UTF-8");
    2.70                  response.setContentType("text/javascript");
     3.1 --- a/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/ShowMojo.java	Sat Sep 26 07:59:41 2015 +0200
     3.2 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/ShowMojo.java	Sat Dec 26 08:22:59 2015 +0100
     3.3 @@ -21,6 +21,7 @@
     3.4  import org.apache.maven.plugin.AbstractMojo;
     3.5  
     3.6  import java.io.File;
     3.7 +import java.io.Flushable;
     3.8  import java.io.IOException;
     3.9  import java.net.MalformedURLException;
    3.10  import java.net.URL;
    3.11 @@ -75,7 +76,11 @@
    3.12          try {
    3.13              Closeable httpServer;
    3.14              httpServer = Launcher.showDir(launcher, directory, null, startpage);
    3.15 -            System.in.read();
    3.16 +            if (httpServer instanceof Flushable) {
    3.17 +                ((Flushable)httpServer).flush();
    3.18 +            } else {
    3.19 +                System.in.read();
    3.20 +            }
    3.21              httpServer.close();
    3.22          } catch (IOException ex) {
    3.23              throw new MojoExecutionException("Can't show the browser", ex);