1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Tue Dec 25 14:07:02 2012 +0100
1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Tue Dec 25 15:08:39 2012 +0100
1.3 @@ -17,6 +17,7 @@
1.4 */
1.5 package org.apidesign.bck2brwsr.launcher;
1.6
1.7 +import java.io.Closeable;
1.8 import java.io.File;
1.9 import java.io.IOException;
1.10 import java.io.InputStream;
1.11 @@ -52,18 +53,23 @@
1.12 * Supports execution in native browser as well as Java's internal
1.13 * execution engine.
1.14 */
1.15 -public class Bck2BrwsrLauncher {
1.16 +final class Bck2BrwsrLauncher extends Launcher implements Closeable {
1.17 private static final Logger LOG = Logger.getLogger(Bck2BrwsrLauncher.class.getName());
1.18 private static final MethodInvocation END = new MethodInvocation(null, null);
1.19 private Set<ClassLoader> loaders = new LinkedHashSet<>();
1.20 private BlockingQueue<MethodInvocation> methods = new LinkedBlockingQueue<>();
1.21 private long timeOut;
1.22 private final Res resources = new Res();
1.23 + private final String cmd;
1.24 private Object[] brwsr;
1.25 private HttpServer server;
1.26 private CountDownLatch wait;
1.27 +
1.28 + public Bck2BrwsrLauncher(String cmd) {
1.29 + this.cmd = cmd;
1.30 + }
1.31
1.32 -
1.33 + @Override
1.34 public MethodInvocation addMethod(Class<?> clazz, String method) throws IOException {
1.35 loaders.add(clazz.getClassLoader());
1.36 MethodInvocation c = new MethodInvocation(clazz.getName(), method);
1.37 @@ -83,23 +89,21 @@
1.38 public void addClassLoader(ClassLoader url) {
1.39 this.loaders.add(url);
1.40 }
1.41 -
1.42 - public static void main( String[] args ) throws Exception {
1.43 - Bck2BrwsrLauncher l = new Bck2BrwsrLauncher();
1.44 - l.addClassLoader(Bck2BrwsrLauncher.class.getClassLoader());
1.45 - l.showURL("org/apidesign/bck2brwsr/launcher/console.xhtml");
1.46 - System.in.read();
1.47 - }
1.48
1.49 - public void showURL(String startpage) throws URISyntaxException, InterruptedException, IOException {
1.50 + public void showURL(String startpage) throws IOException {
1.51 if (!startpage.startsWith("/")) {
1.52 startpage = "/" + startpage;
1.53 }
1.54 HttpServer s = initServer();
1.55 s.getServerConfiguration().addHttpHandler(new Page(resources, null), "/");
1.56 - launchServerAndBrwsr(s, startpage);
1.57 + try {
1.58 + launchServerAndBrwsr(s, startpage);
1.59 + } catch (URISyntaxException | InterruptedException ex) {
1.60 + throw new IOException(ex);
1.61 + }
1.62 }
1.63
1.64 + @Override
1.65 public void initialize() throws IOException {
1.66 try {
1.67 executeInBrowser();
1.68 @@ -178,12 +182,17 @@
1.69 this.brwsr = launchServerAndBrwsr(server, "/execute");
1.70 }
1.71
1.72 - public void shutdown() throws InterruptedException, IOException {
1.73 + @Override
1.74 + public void shutdown() throws IOException {
1.75 methods.offer(END);
1.76 for (;;) {
1.77 int prev = methods.size();
1.78 - if (wait.await(timeOut, TimeUnit.MILLISECONDS)) {
1.79 - break;
1.80 + try {
1.81 + if (wait.await(timeOut, TimeUnit.MILLISECONDS)) {
1.82 + break;
1.83 + }
1.84 + } catch (InterruptedException ex) {
1.85 + throw new IOException(ex);
1.86 }
1.87 if (prev == methods.size()) {
1.88 LOG.log(
1.89 @@ -244,11 +253,12 @@
1.90 // return new Object[] { process, dir };
1.91 }
1.92 {
1.93 - String[] cmd = {
1.94 - "xdg-open", uri.toString()
1.95 + String cmdName = cmd == null ? "xdg-open" : cmd;
1.96 + String[] cmdArr = {
1.97 + cmdName, uri.toString()
1.98 };
1.99 - LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmd));
1.100 - final Process process = Runtime.getRuntime().exec(cmd);
1.101 + LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
1.102 + final Process process = Runtime.getRuntime().exec(cmdArr);
1.103 return new Object[] { process, null };
1.104 }
1.105 }
1.106 @@ -264,7 +274,7 @@
1.107 }
1.108 }
1.109
1.110 - private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException, InterruptedException {
1.111 + private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException {
1.112 Process process = (Process)brwsr[0];
1.113
1.114 server.stop();
1.115 @@ -273,7 +283,12 @@
1.116 drain("StdOut", stdout);
1.117 drain("StdErr", stderr);
1.118 process.destroy();
1.119 - int res = process.waitFor();
1.120 + int res;
1.121 + try {
1.122 + res = process.waitFor();
1.123 + } catch (InterruptedException ex) {
1.124 + throw new IOException(ex);
1.125 + }
1.126 LOG.log(Level.INFO, "Exit code: {0}", res);
1.127
1.128 deleteTree((File)brwsr[1]);
1.129 @@ -305,6 +320,11 @@
1.130 file.delete();
1.131 }
1.132
1.133 + @Override
1.134 + public void close() throws IOException {
1.135 + shutdown();
1.136 + }
1.137 +
1.138 private class Res implements Bck2Brwsr.Resources {
1.139 @Override
1.140 public InputStream get(String resource) throws IOException {