Using internal server to execute the HTML page emul
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Jan 2013 18:27:57 +0100
branchemul
changeset 613a4a06840209a
parent 612 4bc57543ab71
child 614 dac6be4a1fbc
Using internal server to execute the HTML page
javaquery/demo-calculator/nbactions.xml
javaquery/demo-calculator/pom.xml
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java
mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrswrMojo.java
     1.1 --- a/javaquery/demo-calculator/nbactions.xml	Wed Jan 30 17:36:45 2013 +0100
     1.2 +++ b/javaquery/demo-calculator/nbactions.xml	Wed Jan 30 18:27:57 2013 +0100
     1.3 @@ -23,7 +23,7 @@
     1.4              <actionName>run</actionName>
     1.5              <goals>
     1.6                  <goal>process-classes</goal>
     1.7 -                <goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
     1.8 +                <goal>org.apidesign.bck2brwsr:mojo:0.3-SNAPSHOT:brwsr</goal>
     1.9              </goals>
    1.10          </action>
    1.11      </actions>
     2.1 --- a/javaquery/demo-calculator/pom.xml	Wed Jan 30 17:36:45 2013 +0100
     2.2 +++ b/javaquery/demo-calculator/pom.xml	Wed Jan 30 18:27:57 2013 +0100
     2.3 @@ -16,34 +16,21 @@
     2.4    </properties>
     2.5    <build>
     2.6        <plugins>
     2.7 -          <plugin>
     2.8 -              <groupId>org.apidesign.bck2brwsr</groupId>
     2.9 -              <artifactId>mojo</artifactId>
    2.10 -              <version>0.3-SNAPSHOT</version>
    2.11 -              <executions>
    2.12 -                  <execution>
    2.13 -                      <goals>
    2.14 -                          <goal>j2js</goal>
    2.15 -                      </goals>
    2.16 -                  </execution>
    2.17 -              </executions>
    2.18 -          </plugin>
    2.19              <plugin>
    2.20 -                <groupId>org.codehaus.mojo</groupId>
    2.21 -                <artifactId>exec-maven-plugin</artifactId>
    2.22 -                <version>1.2.1</version>
    2.23 +                <groupId>org.apidesign.bck2brwsr</groupId>
    2.24 +                <artifactId>mojo</artifactId>
    2.25 +                <version>0.3-SNAPSHOT</version>
    2.26                  <executions>
    2.27                      <execution>
    2.28                          <goals>
    2.29 -                            <goal>exec</goal>
    2.30 +                            <goal>j2js</goal>
    2.31 +                            <goal>brwsr</goal>
    2.32                          </goals>
    2.33                      </execution>
    2.34                  </executions>
    2.35                  <configuration>
    2.36 -                    <executable>xdg-open</executable>
    2.37 -                    <arguments>
    2.38 -                        <argument>${project.build.directory}/${project.build.finalName}-bck2brwsr/public_html/index.xhtml</argument>
    2.39 -                    </arguments>
    2.40 +                    <directory>${project.build.directory}/${project.build.finalName}-bck2brwsr/public_html/</directory>
    2.41 +                    <startpage>index.xhtml</startpage>
    2.42                  </configuration>
    2.43              </plugin>
    2.44           <plugin>
     3.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Wed Jan 30 17:36:45 2013 +0100
     3.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Wed Jan 30 18:27:57 2013 +0100
     3.3 @@ -64,7 +64,7 @@
     3.4      private Object[] brwsr;
     3.5      private HttpServer server;
     3.6      private CountDownLatch wait;
     3.7 -
     3.8 +    
     3.9      public Bck2BrwsrLauncher(String cmd) {
    3.10          this.cmd = cmd;
    3.11      }
    3.12 @@ -94,7 +94,7 @@
    3.13          if (!startpage.startsWith("/")) {
    3.14              startpage = "/" + startpage;
    3.15          }
    3.16 -        HttpServer s = initServer();
    3.17 +        HttpServer s = initServer(".", true);
    3.18          s.getServerConfiguration().addHttpHandler(new Page(resources, null), "/");
    3.19          try {
    3.20              launchServerAndBrwsr(s, startpage);
    3.21 @@ -103,6 +103,18 @@
    3.22          }
    3.23      }
    3.24  
    3.25 +    void showDirectory(File dir, String startpage) throws IOException {
    3.26 +        if (!startpage.startsWith("/")) {
    3.27 +            startpage = "/" + startpage;
    3.28 +        }
    3.29 +        HttpServer s = initServer(dir.getPath(), false);
    3.30 +        try {
    3.31 +            launchServerAndBrwsr(s, startpage);
    3.32 +        } catch (URISyntaxException | InterruptedException ex) {
    3.33 +            throw new IOException(ex);
    3.34 +        }
    3.35 +    }
    3.36 +
    3.37      @Override
    3.38      public void initialize() throws IOException {
    3.39          try {
    3.40 @@ -122,18 +134,20 @@
    3.41          }
    3.42      }
    3.43      
    3.44 -    private HttpServer initServer() throws IOException {
    3.45 -        HttpServer s = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
    3.46 +    private HttpServer initServer(String path, boolean addClasses) throws IOException {
    3.47 +        HttpServer s = HttpServer.createSimpleServer(path, new PortRange(8080, 65535));
    3.48  
    3.49          final ServerConfiguration conf = s.getServerConfiguration();
    3.50 -        conf.addHttpHandler(new VM(resources), "/vm.js");
    3.51 -        conf.addHttpHandler(new Classes(resources), "/classes/");
    3.52 +        if (addClasses) {
    3.53 +            conf.addHttpHandler(new VM(resources), "/vm.js");
    3.54 +            conf.addHttpHandler(new Classes(resources), "/classes/");
    3.55 +        }
    3.56          return s;
    3.57      }
    3.58      
    3.59      private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
    3.60          wait = new CountDownLatch(1);
    3.61 -        server = initServer();
    3.62 +        server = initServer(".", true);
    3.63          ServerConfiguration conf = server.getServerConfiguration();
    3.64          conf.addHttpHandler(new Page(resources, 
    3.65              "org/apidesign/bck2brwsr/launcher/harness.xhtml"
     4.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java	Wed Jan 30 17:36:45 2013 +0100
     4.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java	Wed Jan 30 18:27:57 2013 +0100
     4.3 @@ -18,6 +18,7 @@
     4.4  package org.apidesign.bck2brwsr.launcher;
     4.5  
     4.6  import java.io.Closeable;
     4.7 +import java.io.File;
     4.8  import java.io.IOException;
     4.9  import java.net.URLClassLoader;
    4.10  import org.apidesign.vm4brwsr.Bck2Brwsr;
    4.11 @@ -60,4 +61,10 @@
    4.12          l.showURL(startpage);
    4.13          return l;
    4.14      }
    4.15 +    public static Closeable showDir(File directory, String startpage) throws IOException {
    4.16 +        Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(null);
    4.17 +        l.showDirectory(directory, startpage);
    4.18 +        return l;
    4.19 +    }
    4.20 +
    4.21  }
     5.1 --- a/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrswrMojo.java	Wed Jan 30 17:36:45 2013 +0100
     5.2 +++ b/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrswrMojo.java	Wed Jan 30 18:27:57 2013 +0100
     5.3 @@ -51,21 +51,28 @@
     5.4      /** Root of the class files */
     5.5      @Parameter(defaultValue="${project.build.directory}/classes")
     5.6      private File classes;
     5.7 +    
     5.8 +    /** Root of all pages, and files, etc. */
     5.9 +    @Parameter
    5.10 +    private File directory;
    5.11  
    5.12      @Override
    5.13      public void execute() throws MojoExecutionException {
    5.14          if (startpage == null) {
    5.15              throw new MojoExecutionException("You have to provide a start page");
    5.16          }
    5.17 -
    5.18 +        
    5.19          try {
    5.20 -            URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    5.21 -            
    5.22              Closeable httpServer;
    5.23 -            try {
    5.24 -                httpServer = Launcher.showURL(url, startpage());
    5.25 -            } catch (Exception ex) {
    5.26 -                throw new MojoExecutionException("Can't open " + startpage(), ex);
    5.27 +            if (directory != null) {
    5.28 +                httpServer = Launcher.showDir(directory, startpage);
    5.29 +            } else {
    5.30 +                URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    5.31 +                try {
    5.32 +                    httpServer = Launcher.showURL(url, startpage());
    5.33 +                } catch (Exception ex) {
    5.34 +                    throw new MojoExecutionException("Can't open " + startpage(), ex);
    5.35 +                }
    5.36              }
    5.37              System.in.read();
    5.38              httpServer.close();