Starting and also shutdowning the browser. Currently optimized from chrome launcher
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 22 Dec 2012 21:00:23 +0100
branchlauncher
changeset 3654c8388f797b2
parent 364 91f5b1f604d8
child 366 ca2be963f3b9
Starting and also shutdowning the browser. Currently optimized from chrome
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Sat Dec 22 15:01:20 2012 +0100
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Sat Dec 22 21:00:23 2012 +0100
     1.3 @@ -18,6 +18,7 @@
     1.4  package org.apidesign.bck2brwsr.launcher;
     1.5  
     1.6  import java.awt.Desktop;
     1.7 +import java.io.File;
     1.8  import java.io.IOException;
     1.9  import java.io.InputStream;
    1.10  import java.io.InterruptedIOException;
    1.11 @@ -26,6 +27,7 @@
    1.12  import java.net.URI;
    1.13  import java.net.URISyntaxException;
    1.14  import java.net.URL;
    1.15 +import java.nio.file.Files;
    1.16  import java.util.ArrayList;
    1.17  import java.util.Arrays;
    1.18  import java.util.Enumeration;
    1.19 @@ -213,7 +215,7 @@
    1.20              }
    1.21          }, "/data");
    1.22  
    1.23 -        launchServerAndBrwsr(server, "/execute");
    1.24 +        Object[] brwsr = launchServerAndBrwsr(server, "/execute");
    1.25          
    1.26          for (;;) {
    1.27              int prev = currentTest[0];
    1.28 @@ -233,7 +235,7 @@
    1.29                  new Object[]{prev, currentTest[0]}
    1.30              );
    1.31          }
    1.32 -        server.stop();
    1.33 +        stopServerAndBrwsr(server, brwsr);
    1.34      }
    1.35      
    1.36      static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
    1.37 @@ -256,7 +258,7 @@
    1.38          }
    1.39      }
    1.40  
    1.41 -    private void launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
    1.42 +    private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
    1.43          server.start();
    1.44          NetworkListener listener = server.getListeners().iterator().next();
    1.45          int port = listener.getPort();
    1.46 @@ -265,21 +267,36 @@
    1.47          LOG.log(Level.INFO, "Showing {0}", uri);
    1.48          try {
    1.49              Desktop.getDesktop().browse(uri);
    1.50 +            return null;
    1.51          } catch (UnsupportedOperationException ex) {
    1.52 +            File dir = File.createTempFile("chrome", ".dir");
    1.53 +            dir.delete();
    1.54 +            dir.mkdirs();
    1.55              String[] cmd = { 
    1.56 -                "xdg-open", uri.toString()
    1.57 +                "google-chrome", "--user-data-dir=" + dir, "--app=" + uri.toString()
    1.58              };
    1.59              LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmd));
    1.60              final Process process = Runtime.getRuntime().exec(cmd);
    1.61 -            InputStream stdout = process.getInputStream();
    1.62 -            InputStream stderr = process.getErrorStream();
    1.63 -            int res = process.waitFor();
    1.64 -            LOG.log(Level.INFO, "Exit code: {0}", res);
    1.65 -            drain("StdOut", stdout);
    1.66 -            drain("StdErr", stderr);
    1.67 +            return new Object[] { process, dir };
    1.68          }
    1.69      }
    1.70      
    1.71 +    private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException, InterruptedException {
    1.72 +        Process process = (Process)brwsr[0];
    1.73 +        
    1.74 +        server.stop();
    1.75 +        InputStream stdout = process.getInputStream();
    1.76 +        InputStream stderr = process.getErrorStream();
    1.77 +        drain("StdOut", stdout);
    1.78 +        drain("StdErr", stderr);
    1.79 +        Thread.sleep(5000);
    1.80 +        process.destroy();
    1.81 +        int res = process.waitFor();
    1.82 +        LOG.log(Level.INFO, "Exit code: {0}", res);
    1.83 +
    1.84 +        deleteTree((File)brwsr[1]);
    1.85 +    }
    1.86 +    
    1.87      private static void drain(String name, InputStream is) throws IOException {
    1.88          int av = is.available();
    1.89          if (av > 0) {
    1.90 @@ -293,6 +310,16 @@
    1.91          }
    1.92      }
    1.93  
    1.94 +    private void deleteTree(File file) {
    1.95 +        File[] arr = file.listFiles();
    1.96 +        if (arr != null) {
    1.97 +            for (File s : arr) {
    1.98 +                deleteTree(s);
    1.99 +            }
   1.100 +        }
   1.101 +        file.delete();
   1.102 +    }
   1.103 +
   1.104      private class Res implements Bck2Brwsr.Resources {
   1.105          @Override
   1.106          public InputStream get(String resource) throws IOException {