launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
branchemul
changeset 623 4af0d3dedb9d
parent 622 a07253cf2ca4
child 626 f08eb4df84c1
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Thu Jan 31 16:58:27 2013 +0100
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Thu Jan 31 17:39:47 2013 +0100
     1.3 @@ -147,13 +147,40 @@
     1.4      private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
     1.5          wait = new CountDownLatch(1);
     1.6          server = initServer(".", true);
     1.7 -        ServerConfiguration conf = server.getServerConfiguration();
     1.8 +        final ServerConfiguration conf = server.getServerConfiguration();
     1.9 +        
    1.10 +        class DynamicResourceHandler extends HttpHandler {
    1.11 +            private final InvocationContext ic;
    1.12 +            public DynamicResourceHandler(InvocationContext ic) {
    1.13 +                if (ic == null || ic.httpPath == null) {
    1.14 +                    throw new NullPointerException();
    1.15 +                }
    1.16 +                this.ic = ic;
    1.17 +                conf.addHttpHandler(this, ic.httpPath);
    1.18 +            }
    1.19 +
    1.20 +            public void close() {
    1.21 +                conf.removeHttpHandler(this);
    1.22 +            }
    1.23 +            
    1.24 +            @Override
    1.25 +            public void service(Request request, Response response) throws Exception {
    1.26 +                if (ic.httpPath.equals(request.getRequestURI())) {
    1.27 +                    LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
    1.28 +                    response.setContentType(ic.httpType);
    1.29 +                    response.getWriter().write(ic.httpContent);
    1.30 +                }
    1.31 +            }
    1.32 +        }
    1.33 +        
    1.34          conf.addHttpHandler(new Page(resources, 
    1.35              "org/apidesign/bck2brwsr/launcher/harness.xhtml"
    1.36          ), "/execute");
    1.37 +        
    1.38          conf.addHttpHandler(new HttpHandler() {
    1.39              int cnt;
    1.40              List<InvocationContext> cases = new ArrayList<>();
    1.41 +            DynamicResourceHandler prev;
    1.42              @Override
    1.43              public void service(Request request, Response response) throws Exception {
    1.44                  String id = request.getParameter("request");
    1.45 @@ -165,6 +192,11 @@
    1.46                      cases.get(Integer.parseInt(id)).result(value, null);
    1.47                  }
    1.48                  
    1.49 +                if (prev != null) {
    1.50 +                    prev.close();
    1.51 +                    prev = null;
    1.52 +                }
    1.53 +                
    1.54                  InvocationContext mi = methods.take();
    1.55                  if (mi == END) {
    1.56                      response.getWriter().write("");
    1.57 @@ -174,6 +206,10 @@
    1.58                      return;
    1.59                  }
    1.60                  
    1.61 +                if (mi.httpPath != null) {
    1.62 +                    prev = new DynamicResourceHandler(mi);
    1.63 +                }
    1.64 +                
    1.65                  cases.add(mi);
    1.66                  final String cn = mi.clazz.getName();
    1.67                  final String mn = mi.methodName;