launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java
changeset 1271 46e2b4ef85a4
parent 1253 a936dd8280dc
child 1273 37ad459579bc
     1.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Thu Aug 29 14:35:01 2013 +0000
     1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Mon Sep 09 06:08:00 2013 +0200
     1.3 @@ -52,6 +52,7 @@
     1.4  import org.glassfish.grizzly.http.server.Request;
     1.5  import org.glassfish.grizzly.http.server.Response;
     1.6  import org.glassfish.grizzly.http.server.ServerConfiguration;
     1.7 +import org.glassfish.grizzly.http.server.StaticHttpHandler;
     1.8  import org.glassfish.grizzly.http.util.HttpStatus;
     1.9  import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
    1.10  import org.glassfish.grizzly.websockets.WebSocket;
    1.11 @@ -111,7 +112,7 @@
    1.12          if (!startpage.startsWith("/")) {
    1.13              startpage = "/" + startpage;
    1.14          }
    1.15 -        HttpServer s = initServer(".", true);
    1.16 +        HttpServer s = initServer(".", true, "");
    1.17          int last = startpage.lastIndexOf('/');
    1.18          String prefix = startpage.substring(0, last);
    1.19          String simpleName = startpage.substring(last);
    1.20 @@ -128,7 +129,12 @@
    1.21          if (!startpage.startsWith("/")) {
    1.22              startpage = "/" + startpage;
    1.23          }
    1.24 -        HttpServer s = initServer(dir.getPath(), false);
    1.25 +        String prefix = "";
    1.26 +        int last = startpage.lastIndexOf('/');
    1.27 +        if (last >= 0) {
    1.28 +            prefix = startpage.substring(0, last);
    1.29 +        }
    1.30 +        HttpServer s = initServer(dir.getPath(), false, prefix);
    1.31          try {
    1.32              launchServerAndBrwsr(s, startpage);
    1.33          } catch (Exception ex) {
    1.34 @@ -155,8 +161,8 @@
    1.35          }
    1.36      }
    1.37      
    1.38 -    private HttpServer initServer(String path, boolean addClasses) throws IOException {
    1.39 -        HttpServer s = HttpServer.createSimpleServer(path, new PortRange(8080, 65535));
    1.40 +    private HttpServer initServer(String path, boolean addClasses, String vmPrefix) throws IOException {
    1.41 +        HttpServer s = HttpServer.createSimpleServer(null, new PortRange(8080, 65535));
    1.42          /*
    1.43          ThreadPoolConfig fewThreads = ThreadPoolConfig.defaultConfig().copy().
    1.44              setPoolName("Fx/Bck2 Brwsr").
    1.45 @@ -172,8 +178,15 @@
    1.46          }
    1.47          */
    1.48          final ServerConfiguration conf = s.getServerConfiguration();
    1.49 +        VMAndPages vm = new VMAndPages();
    1.50 +        conf.addHttpHandler(vm, "/");
    1.51 +        if (vmPrefix != null) {
    1.52 +            vm.registerVM(vmPrefix + "/bck2brwsr.js");
    1.53 +        }
    1.54 +        if (path != null) {
    1.55 +            vm.addDocRoot(path);
    1.56 +        }
    1.57          if (addClasses) {
    1.58 -            conf.addHttpHandler(new VM(), "/bck2brwsr.js");
    1.59              conf.addHttpHandler(new Classes(resources), "/classes/");
    1.60          }
    1.61          final WebSocketAddOn addon = new WebSocketAddOn();
    1.62 @@ -185,7 +198,7 @@
    1.63      
    1.64      private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
    1.65          wait = new CountDownLatch(1);
    1.66 -        server = initServer(".", true);
    1.67 +        server = initServer(".", true, "");
    1.68          final ServerConfiguration conf = server.getServerConfiguration();
    1.69          
    1.70          class DynamicResourceHandler extends HttpHandler {
    1.71 @@ -642,14 +655,28 @@
    1.72          
    1.73      }
    1.74  
    1.75 -    private class VM extends HttpHandler {
    1.76 +    private class VMAndPages extends StaticHttpHandler {
    1.77 +        private String vmResource;
    1.78 +        
    1.79 +        public VMAndPages() {
    1.80 +            super((String[]) null);
    1.81 +        }
    1.82 +        
    1.83          @Override
    1.84          public void service(Request request, Response response) throws Exception {
    1.85 -            response.setCharacterEncoding("UTF-8");
    1.86 -            response.setContentType("text/javascript");
    1.87 -            StringBuilder sb = new StringBuilder();
    1.88 -            generateBck2BrwsrJS(sb, BaseHTTPLauncher.this.resources);
    1.89 -            response.getWriter().write(sb.toString());
    1.90 +            if (request.getRequestURI().equals(vmResource)) {
    1.91 +                response.setCharacterEncoding("UTF-8");
    1.92 +                response.setContentType("text/javascript");
    1.93 +                StringBuilder sb = new StringBuilder();
    1.94 +                generateBck2BrwsrJS(sb, BaseHTTPLauncher.this.resources);
    1.95 +                response.getWriter().write(sb.toString());
    1.96 +            } else {
    1.97 +                super.service(request, response);
    1.98 +            }
    1.99 +        }
   1.100 +
   1.101 +        private void registerVM(String vmResource) {
   1.102 +            this.vmResource = vmResource;
   1.103          }
   1.104      }
   1.105