Dynamic way of registering Http resources classloader
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 21 Jun 2013 22:34:09 +0200
branchclassloader
changeset 1186265edcee24ed
parent 1185 be346bd5a46d
child 1187 ce6327094088
Dynamic way of registering Http resources
launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java
launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java
     1.1 --- a/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java	Fri Jun 21 15:21:09 2013 +0200
     1.2 +++ b/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java	Fri Jun 21 22:34:09 2013 +0200
     1.3 @@ -19,6 +19,7 @@
     1.4  
     1.5  import java.io.IOException;
     1.6  import java.io.InputStream;
     1.7 +import java.net.URI;
     1.8  import java.util.ArrayList;
     1.9  import java.util.List;
    1.10  import java.util.concurrent.CountDownLatch;
    1.11 @@ -95,6 +96,31 @@
    1.12          wait.countDown();
    1.13      }
    1.14  
    1.15 +    private static RegisterResource RR;
    1.16 +    static void register(RegisterResource rr) {
    1.17 +        RR = rr;
    1.18 +    }
    1.19 +    /** A running {@link InvocationContext test} can register additional 
    1.20 +     * resources to the associated browser (if any). 
    1.21 +     * 
    1.22 +     * @param content the stream to deliver content from
    1.23 +     * @param mimeType mime type for the HTTP reply
    1.24 +     * @param path suggested path in the server to use
    1.25 +     * @param parameters additional parameters
    1.26 +     * @return the URI the resource is made available at
    1.27 +     * @throws NullPointerException if there is no {@link InvocationContext test} 
    1.28 +     *    currently running
    1.29 +     * @since 0.8
    1.30 +     */
    1.31 +    public static URI register(
    1.32 +        InputStream content, String mimeType, String path, String[] parameters
    1.33 +    ) {
    1.34 +        final Resource r = new Resource(content, mimeType, path, parameters);
    1.35 +        return RR.registerResource(r);
    1.36 +    }
    1.37 +    static interface RegisterResource {
    1.38 +        public URI registerResource(Resource r);
    1.39 +    }
    1.40  
    1.41      static final class Resource {
    1.42          final InputStream httpContent;
     2.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Fri Jun 21 15:21:09 2013 +0200
     2.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Fri Jun 21 22:34:09 2013 +0200
     2.3 @@ -177,20 +177,19 @@
     2.4          server = initServer(".", true);
     2.5          final ServerConfiguration conf = server.getServerConfiguration();
     2.6          
     2.7 -        class DynamicResourceHandler extends HttpHandler {
     2.8 +        class DynamicResourceHandler extends HttpHandler implements InvocationContext.RegisterResource {
     2.9              private final InvocationContext ic;
    2.10              public DynamicResourceHandler(InvocationContext ic) {
    2.11 -                if (ic == null || ic.resources.isEmpty()) {
    2.12 -                    throw new NullPointerException();
    2.13 -                }
    2.14                  this.ic = ic;
    2.15                  for (Resource r : ic.resources) {
    2.16                      conf.addHttpHandler(this, r.httpPath);
    2.17                  }
    2.18 +                InvocationContext.register(this);
    2.19              }
    2.20  
    2.21              public void close() {
    2.22                  conf.removeHttpHandler(this);
    2.23 +                InvocationContext.register(null);
    2.24              }
    2.25              
    2.26              @Override
    2.27 @@ -231,6 +230,15 @@
    2.28                      }
    2.29                  }
    2.30              }
    2.31 +
    2.32 +            @Override
    2.33 +            public URI registerResource(Resource r) {
    2.34 +                if (!ic.resources.contains(r)) {
    2.35 +                    ic.resources.add(r);
    2.36 +                    conf.addHttpHandler(this, r.httpPath);
    2.37 +                }
    2.38 +                return pageURL(server, r.httpPath);
    2.39 +            }
    2.40          }
    2.41          
    2.42          conf.addHttpHandler(new Page(resources, harnessResource()), "/execute");
    2.43 @@ -286,9 +294,7 @@
    2.44                      return;
    2.45                  }
    2.46                  
    2.47 -                if (!mi.resources.isEmpty()) {
    2.48 -                    prev = new DynamicResourceHandler(mi);
    2.49 -                }
    2.50 +                prev = new DynamicResourceHandler(mi);
    2.51                  
    2.52                  cases.add(mi);
    2.53                  final String cn = mi.clazz.getName();
    2.54 @@ -381,10 +387,7 @@
    2.55  
    2.56      private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
    2.57          server.start();
    2.58 -        NetworkListener listener = server.getListeners().iterator().next();
    2.59 -        int port = listener.getPort();
    2.60 -        
    2.61 -        URI uri = new URI("http://localhost:" + port + page);
    2.62 +        URI uri = pageURL(server, page);
    2.63          return showBrwsr(uri);
    2.64      }
    2.65      private static String toUTF8(String value) throws UnsupportedEncodingException {
    2.66 @@ -496,6 +499,16 @@
    2.67      abstract void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException;
    2.68      abstract String harnessResource();
    2.69  
    2.70 +    private static URI pageURL(HttpServer server, final String page) {
    2.71 +        NetworkListener listener = server.getListeners().iterator().next();
    2.72 +        int port = listener.getPort();
    2.73 +        try {
    2.74 +            return new URI("http://localhost:" + port + page);
    2.75 +        } catch (URISyntaxException ex) {
    2.76 +            throw new IllegalStateException(ex);
    2.77 +        }
    2.78 +    }
    2.79 +
    2.80      class Res {
    2.81          public InputStream get(String resource) throws IOException {
    2.82              URL u = null;