1.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Tue May 28 21:49:38 2013 +0200
1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Fri Jun 21 22:34:09 2013 +0200
1.3 @@ -177,20 +177,19 @@
1.4 server = initServer(".", true);
1.5 final ServerConfiguration conf = server.getServerConfiguration();
1.6
1.7 - class DynamicResourceHandler extends HttpHandler {
1.8 + class DynamicResourceHandler extends HttpHandler implements InvocationContext.RegisterResource {
1.9 private final InvocationContext ic;
1.10 public DynamicResourceHandler(InvocationContext ic) {
1.11 - if (ic == null || ic.resources.isEmpty()) {
1.12 - throw new NullPointerException();
1.13 - }
1.14 this.ic = ic;
1.15 for (Resource r : ic.resources) {
1.16 conf.addHttpHandler(this, r.httpPath);
1.17 }
1.18 + InvocationContext.register(this);
1.19 }
1.20
1.21 public void close() {
1.22 conf.removeHttpHandler(this);
1.23 + InvocationContext.register(null);
1.24 }
1.25
1.26 @Override
1.27 @@ -231,6 +230,15 @@
1.28 }
1.29 }
1.30 }
1.31 +
1.32 + @Override
1.33 + public URI registerResource(Resource r) {
1.34 + if (!ic.resources.contains(r)) {
1.35 + ic.resources.add(r);
1.36 + conf.addHttpHandler(this, r.httpPath);
1.37 + }
1.38 + return pageURL(server, r.httpPath);
1.39 + }
1.40 }
1.41
1.42 conf.addHttpHandler(new Page(resources, harnessResource()), "/execute");
1.43 @@ -286,9 +294,7 @@
1.44 return;
1.45 }
1.46
1.47 - if (!mi.resources.isEmpty()) {
1.48 - prev = new DynamicResourceHandler(mi);
1.49 - }
1.50 + prev = new DynamicResourceHandler(mi);
1.51
1.52 cases.add(mi);
1.53 final String cn = mi.clazz.getName();
1.54 @@ -381,10 +387,7 @@
1.55
1.56 private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
1.57 server.start();
1.58 - NetworkListener listener = server.getListeners().iterator().next();
1.59 - int port = listener.getPort();
1.60 -
1.61 - URI uri = new URI("http://localhost:" + port + page);
1.62 + URI uri = pageURL(server, page);
1.63 return showBrwsr(uri);
1.64 }
1.65 private static String toUTF8(String value) throws UnsupportedEncodingException {
1.66 @@ -496,6 +499,16 @@
1.67 abstract void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException;
1.68 abstract String harnessResource();
1.69
1.70 + private static URI pageURL(HttpServer server, final String page) {
1.71 + NetworkListener listener = server.getListeners().iterator().next();
1.72 + int port = listener.getPort();
1.73 + try {
1.74 + return new URI("http://localhost:" + port + page);
1.75 + } catch (URISyntaxException ex) {
1.76 + throw new IllegalStateException(ex);
1.77 + }
1.78 + }
1.79 +
1.80 class Res {
1.81 public InputStream get(String resource) throws IOException {
1.82 URL u = null;