Can mix static pages and generated bck2brwsr.js
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 09 Sep 2013 06:08:00 +0200
changeset 127146e2b4ef85a4
parent 1261 50c09cb0a3fb
child 1272 3ee4ec9577bc
Can mix static pages and generated bck2brwsr.js
launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java
launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java
rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrwsrMojo.java
     1.1 --- a/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java	Sat Sep 07 20:59:25 2013 +0200
     1.2 +++ b/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java	Mon Sep 09 06:08:00 2013 +0200
     1.3 @@ -135,14 +135,34 @@
     1.4       * The <code>startpage</code> should be relative location inside the root 
     1.5       * directory. Opens a browser with URL showing the start page.
     1.6       * 
     1.7 +     * @param brwsr type of the browser to use
     1.8 +     * @param directory the root directory on disk
     1.9 +     * @param classes additional classloader with access to classes or <code>null</code>
    1.10 +     * @param startpage relative path from the root to the page
    1.11 +     * @return instance of server that can be closed
    1.12 +     * @exception IOException if something goes wrong.
    1.13 +     * @since 0.8
    1.14 +     */
    1.15 +    public static Closeable showDir(String brwsr, File directory, ClassLoader classes, String startpage) throws IOException {
    1.16 +        Launcher l = createBrowser(brwsr);
    1.17 +        if (classes != null) {
    1.18 +            l.addClassLoader(classes);
    1.19 +        }
    1.20 +        l.showDirectory(directory, startpage);
    1.21 +        return (Closeable) l;
    1.22 +    }
    1.23 +    
    1.24 +    /** Starts an HTTP server which provides access to certain directory.
    1.25 +     * The <code>startpage</code> should be relative location inside the root 
    1.26 +     * directory. Opens a browser with URL showing the start page.
    1.27 +     * 
    1.28       * @param directory the root directory on disk
    1.29       * @param startpage relative path from the root to the page
    1.30 +     * @return instance of server that can be closed
    1.31       * @exception IOException if something goes wrong.
    1.32       */
    1.33      public static Closeable showDir(File directory, String startpage) throws IOException {
    1.34 -        Launcher l = createBrowser(null);
    1.35 -        l.showDirectory(directory, startpage);
    1.36 -        return (Closeable) l;
    1.37 +        return showDir(null, directory, null, startpage);
    1.38      }
    1.39  
    1.40      abstract InvocationContext runMethod(InvocationContext c) throws IOException; 
     2.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Sat Sep 07 20:59:25 2013 +0200
     2.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Mon Sep 09 06:08:00 2013 +0200
     2.3 @@ -52,6 +52,7 @@
     2.4  import org.glassfish.grizzly.http.server.Request;
     2.5  import org.glassfish.grizzly.http.server.Response;
     2.6  import org.glassfish.grizzly.http.server.ServerConfiguration;
     2.7 +import org.glassfish.grizzly.http.server.StaticHttpHandler;
     2.8  import org.glassfish.grizzly.http.util.HttpStatus;
     2.9  import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
    2.10  import org.glassfish.grizzly.websockets.WebSocket;
    2.11 @@ -111,7 +112,7 @@
    2.12          if (!startpage.startsWith("/")) {
    2.13              startpage = "/" + startpage;
    2.14          }
    2.15 -        HttpServer s = initServer(".", true);
    2.16 +        HttpServer s = initServer(".", true, "");
    2.17          int last = startpage.lastIndexOf('/');
    2.18          String prefix = startpage.substring(0, last);
    2.19          String simpleName = startpage.substring(last);
    2.20 @@ -128,7 +129,12 @@
    2.21          if (!startpage.startsWith("/")) {
    2.22              startpage = "/" + startpage;
    2.23          }
    2.24 -        HttpServer s = initServer(dir.getPath(), false);
    2.25 +        String prefix = "";
    2.26 +        int last = startpage.lastIndexOf('/');
    2.27 +        if (last >= 0) {
    2.28 +            prefix = startpage.substring(0, last);
    2.29 +        }
    2.30 +        HttpServer s = initServer(dir.getPath(), false, prefix);
    2.31          try {
    2.32              launchServerAndBrwsr(s, startpage);
    2.33          } catch (Exception ex) {
    2.34 @@ -155,8 +161,8 @@
    2.35          }
    2.36      }
    2.37      
    2.38 -    private HttpServer initServer(String path, boolean addClasses) throws IOException {
    2.39 -        HttpServer s = HttpServer.createSimpleServer(path, new PortRange(8080, 65535));
    2.40 +    private HttpServer initServer(String path, boolean addClasses, String vmPrefix) throws IOException {
    2.41 +        HttpServer s = HttpServer.createSimpleServer(null, new PortRange(8080, 65535));
    2.42          /*
    2.43          ThreadPoolConfig fewThreads = ThreadPoolConfig.defaultConfig().copy().
    2.44              setPoolName("Fx/Bck2 Brwsr").
    2.45 @@ -172,8 +178,15 @@
    2.46          }
    2.47          */
    2.48          final ServerConfiguration conf = s.getServerConfiguration();
    2.49 +        VMAndPages vm = new VMAndPages();
    2.50 +        conf.addHttpHandler(vm, "/");
    2.51 +        if (vmPrefix != null) {
    2.52 +            vm.registerVM(vmPrefix + "/bck2brwsr.js");
    2.53 +        }
    2.54 +        if (path != null) {
    2.55 +            vm.addDocRoot(path);
    2.56 +        }
    2.57          if (addClasses) {
    2.58 -            conf.addHttpHandler(new VM(), "/bck2brwsr.js");
    2.59              conf.addHttpHandler(new Classes(resources), "/classes/");
    2.60          }
    2.61          final WebSocketAddOn addon = new WebSocketAddOn();
    2.62 @@ -185,7 +198,7 @@
    2.63      
    2.64      private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
    2.65          wait = new CountDownLatch(1);
    2.66 -        server = initServer(".", true);
    2.67 +        server = initServer(".", true, "");
    2.68          final ServerConfiguration conf = server.getServerConfiguration();
    2.69          
    2.70          class DynamicResourceHandler extends HttpHandler {
    2.71 @@ -642,14 +655,28 @@
    2.72          
    2.73      }
    2.74  
    2.75 -    private class VM extends HttpHandler {
    2.76 +    private class VMAndPages extends StaticHttpHandler {
    2.77 +        private String vmResource;
    2.78 +        
    2.79 +        public VMAndPages() {
    2.80 +            super((String[]) null);
    2.81 +        }
    2.82 +        
    2.83          @Override
    2.84          public void service(Request request, Response response) throws Exception {
    2.85 -            response.setCharacterEncoding("UTF-8");
    2.86 -            response.setContentType("text/javascript");
    2.87 -            StringBuilder sb = new StringBuilder();
    2.88 -            generateBck2BrwsrJS(sb, BaseHTTPLauncher.this.resources);
    2.89 -            response.getWriter().write(sb.toString());
    2.90 +            if (request.getRequestURI().equals(vmResource)) {
    2.91 +                response.setCharacterEncoding("UTF-8");
    2.92 +                response.setContentType("text/javascript");
    2.93 +                StringBuilder sb = new StringBuilder();
    2.94 +                generateBck2BrwsrJS(sb, BaseHTTPLauncher.this.resources);
    2.95 +                response.getWriter().write(sb.toString());
    2.96 +            } else {
    2.97 +                super.service(request, response);
    2.98 +            }
    2.99 +        }
   2.100 +
   2.101 +        private void registerVM(String vmResource) {
   2.102 +            this.vmResource = vmResource;
   2.103          }
   2.104      }
   2.105  
     3.1 --- a/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrwsrMojo.java	Sat Sep 07 20:59:25 2013 +0200
     3.2 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrwsrMojo.java	Mon Sep 09 06:08:00 2013 +0200
     3.3 @@ -79,7 +79,8 @@
     3.4          try {
     3.5              Closeable httpServer;
     3.6              if (directory != null) {
     3.7 -                httpServer = Launcher.showDir(directory, startpage);
     3.8 +                URLClassLoader url = buildClassLoader(classes, prj.getArtifacts());
     3.9 +                httpServer = Launcher.showDir(launcher, directory, url, startpage);
    3.10              } else {
    3.11                  URLClassLoader url = buildClassLoader(classes, prj.getArtifacts());
    3.12                  try {