# HG changeset patch # User Jaroslav Tulach # Date 1378699680 -7200 # Node ID 46e2b4ef85a443453942bc15e9b4f270a3af16eb # Parent 50c09cb0a3fb729cc12a20af698acb61d56f2fb7 Can mix static pages and generated bck2brwsr.js diff -r 50c09cb0a3fb -r 46e2b4ef85a4 launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java --- a/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java Sat Sep 07 20:59:25 2013 +0200 +++ b/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java Mon Sep 09 06:08:00 2013 +0200 @@ -135,14 +135,34 @@ * The startpage should be relative location inside the root * directory. Opens a browser with URL showing the start page. * + * @param brwsr type of the browser to use + * @param directory the root directory on disk + * @param classes additional classloader with access to classes or null + * @param startpage relative path from the root to the page + * @return instance of server that can be closed + * @exception IOException if something goes wrong. + * @since 0.8 + */ + public static Closeable showDir(String brwsr, File directory, ClassLoader classes, String startpage) throws IOException { + Launcher l = createBrowser(brwsr); + if (classes != null) { + l.addClassLoader(classes); + } + l.showDirectory(directory, startpage); + return (Closeable) l; + } + + /** Starts an HTTP server which provides access to certain directory. + * The startpage should be relative location inside the root + * directory. Opens a browser with URL showing the start page. + * * @param directory the root directory on disk * @param startpage relative path from the root to the page + * @return instance of server that can be closed * @exception IOException if something goes wrong. */ public static Closeable showDir(File directory, String startpage) throws IOException { - Launcher l = createBrowser(null); - l.showDirectory(directory, startpage); - return (Closeable) l; + return showDir(null, directory, null, startpage); } abstract InvocationContext runMethod(InvocationContext c) throws IOException; diff -r 50c09cb0a3fb -r 46e2b4ef85a4 launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Sat Sep 07 20:59:25 2013 +0200 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Mon Sep 09 06:08:00 2013 +0200 @@ -52,6 +52,7 @@ import org.glassfish.grizzly.http.server.Request; import org.glassfish.grizzly.http.server.Response; import org.glassfish.grizzly.http.server.ServerConfiguration; +import org.glassfish.grizzly.http.server.StaticHttpHandler; import org.glassfish.grizzly.http.util.HttpStatus; import org.glassfish.grizzly.threadpool.ThreadPoolConfig; import org.glassfish.grizzly.websockets.WebSocket; @@ -111,7 +112,7 @@ if (!startpage.startsWith("/")) { startpage = "/" + startpage; } - HttpServer s = initServer(".", true); + HttpServer s = initServer(".", true, ""); int last = startpage.lastIndexOf('/'); String prefix = startpage.substring(0, last); String simpleName = startpage.substring(last); @@ -128,7 +129,12 @@ if (!startpage.startsWith("/")) { startpage = "/" + startpage; } - HttpServer s = initServer(dir.getPath(), false); + String prefix = ""; + int last = startpage.lastIndexOf('/'); + if (last >= 0) { + prefix = startpage.substring(0, last); + } + HttpServer s = initServer(dir.getPath(), false, prefix); try { launchServerAndBrwsr(s, startpage); } catch (Exception ex) { @@ -155,8 +161,8 @@ } } - private HttpServer initServer(String path, boolean addClasses) throws IOException { - HttpServer s = HttpServer.createSimpleServer(path, new PortRange(8080, 65535)); + private HttpServer initServer(String path, boolean addClasses, String vmPrefix) throws IOException { + HttpServer s = HttpServer.createSimpleServer(null, new PortRange(8080, 65535)); /* ThreadPoolConfig fewThreads = ThreadPoolConfig.defaultConfig().copy(). setPoolName("Fx/Bck2 Brwsr"). @@ -172,8 +178,15 @@ } */ final ServerConfiguration conf = s.getServerConfiguration(); + VMAndPages vm = new VMAndPages(); + conf.addHttpHandler(vm, "/"); + if (vmPrefix != null) { + vm.registerVM(vmPrefix + "/bck2brwsr.js"); + } + if (path != null) { + vm.addDocRoot(path); + } if (addClasses) { - conf.addHttpHandler(new VM(), "/bck2brwsr.js"); conf.addHttpHandler(new Classes(resources), "/classes/"); } final WebSocketAddOn addon = new WebSocketAddOn(); @@ -185,7 +198,7 @@ private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException { wait = new CountDownLatch(1); - server = initServer(".", true); + server = initServer(".", true, ""); final ServerConfiguration conf = server.getServerConfiguration(); class DynamicResourceHandler extends HttpHandler { @@ -642,14 +655,28 @@ } - private class VM extends HttpHandler { + private class VMAndPages extends StaticHttpHandler { + private String vmResource; + + public VMAndPages() { + super((String[]) null); + } + @Override public void service(Request request, Response response) throws Exception { - response.setCharacterEncoding("UTF-8"); - response.setContentType("text/javascript"); - StringBuilder sb = new StringBuilder(); - generateBck2BrwsrJS(sb, BaseHTTPLauncher.this.resources); - response.getWriter().write(sb.toString()); + if (request.getRequestURI().equals(vmResource)) { + response.setCharacterEncoding("UTF-8"); + response.setContentType("text/javascript"); + StringBuilder sb = new StringBuilder(); + generateBck2BrwsrJS(sb, BaseHTTPLauncher.this.resources); + response.getWriter().write(sb.toString()); + } else { + super.service(request, response); + } + } + + private void registerVM(String vmResource) { + this.vmResource = vmResource; } } diff -r 50c09cb0a3fb -r 46e2b4ef85a4 rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrwsrMojo.java --- a/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrwsrMojo.java Sat Sep 07 20:59:25 2013 +0200 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrwsrMojo.java Mon Sep 09 06:08:00 2013 +0200 @@ -79,7 +79,8 @@ try { Closeable httpServer; if (directory != null) { - httpServer = Launcher.showDir(directory, startpage); + URLClassLoader url = buildClassLoader(classes, prj.getArtifacts()); + httpServer = Launcher.showDir(launcher, directory, url, startpage); } else { URLClassLoader url = buildClassLoader(classes, prj.getArtifacts()); try {