launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
branchlauncher
changeset 342 60f9aad12731
parent 332 6949044415df
child 348 4e9d576780ca
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Sun Dec 16 20:11:18 2012 +0100
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Mon Dec 17 11:58:48 2012 +0100
     1.3 @@ -25,6 +25,7 @@
     1.4  import java.net.URI;
     1.5  import java.net.URL;
     1.6  import java.util.Enumeration;
     1.7 +import java.util.concurrent.CountDownLatch;
     1.8  import static org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher.copyStream;
     1.9  import org.apidesign.vm4brwsr.Bck2Brwsr;
    1.10  import org.glassfish.grizzly.PortRange;
    1.11 @@ -40,6 +41,12 @@
    1.12   */
    1.13  public class Bck2BrwsrLauncher {
    1.14      public static void main( String[] args ) throws Exception {
    1.15 +        final Case[] cases = { 
    1.16 +            new Case("org.apidesign.bck2brwsr.launcher.Console", "welcome"),
    1.17 +            new Case("org.apidesign.bck2brwsr.launcher.Console", "multiply")
    1.18 +        };
    1.19 +        final CountDownLatch wait = new CountDownLatch(1);
    1.20 +        
    1.21          HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
    1.22          final ClassLoader loader = Bck2BrwsrLauncher.class.getClassLoader();
    1.23          
    1.24 @@ -70,13 +77,28 @@
    1.25              int cnt;
    1.26              @Override
    1.27              public void service(Request request, Response response) throws Exception {
    1.28 +                String id = request.getParameter("request");
    1.29 +                String value = request.getParameter("result");
    1.30 +                if (id != null && value != null) {
    1.31 +                    value = value.replace("%20", " ");
    1.32 +                    cases[Integer.parseInt(id)].result = value;
    1.33 +                }
    1.34 +                
    1.35 +                if (cnt >= cases.length) {
    1.36 +                    response.getWriter().write("");
    1.37 +                    wait.countDown();
    1.38 +                    cnt = 0;
    1.39 +                    return;
    1.40 +                }
    1.41 +                
    1.42                  response.getWriter().write("{"
    1.43 -                    + "className: 'org.apidesign.bck2brwsr.launcher.Console',"
    1.44 -                    + "methodName: 'welcome',"
    1.45 +                    + "className: '" + cases[cnt].className + "', "
    1.46 +                    + "methodName: '" + cases[cnt].methodName + "', "
    1.47                      + "request: " + cnt
    1.48                      + "}");
    1.49 +                cnt++;
    1.50              }
    1.51 -        }, "execute/data");
    1.52 +        }, "/data");
    1.53          conf.addHttpHandler(new Page("harness.xhtml"), "/execute");
    1.54          
    1.55          server.start();
    1.56 @@ -93,10 +115,14 @@
    1.57              Runtime.getRuntime().exec(cmd).waitFor();
    1.58          }
    1.59          
    1.60 -        System.in.read();
    1.61 +        wait.await();
    1.62 +        
    1.63 +        for (Case c : cases) {
    1.64 +            System.err.println(c.className + "." + c.methodName + " = " + c.result);
    1.65 +        }
    1.66      }
    1.67      
    1.68 -    static void copyStream(InputStream is, OutputStream os, String... params) throws IOException {
    1.69 +    static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
    1.70          for (;;) {
    1.71              int ch = is.read();
    1.72              if (ch == -1) {
    1.73 @@ -104,6 +130,9 @@
    1.74              }
    1.75              if (ch == '$') {
    1.76                  int cnt = is.read() - '0';
    1.77 +                if (cnt == 'U' - '0') {
    1.78 +                    os.write(baseURL.getBytes());
    1.79 +                }
    1.80                  if (cnt < params.length) {
    1.81                      os.write(params[cnt].getBytes());
    1.82                  }
    1.83 @@ -127,7 +156,7 @@
    1.84              response.setContentType("text/html");
    1.85              OutputStream os = response.getOutputStream();
    1.86              InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream(resource);
    1.87 -            copyStream(is, os, args);
    1.88 +            copyStream(is, os, request.getRequestURL().toString(), args);
    1.89          }
    1.90      }
    1.91  
    1.92 @@ -210,4 +239,15 @@
    1.93              w.append("\n]");
    1.94          }
    1.95      }
    1.96 +    
    1.97 +    private static final class Case {
    1.98 +        final String className;
    1.99 +        final String methodName;
   1.100 +        String result;
   1.101 +
   1.102 +        public Case(String className, String methodName) {
   1.103 +            this.className = className;
   1.104 +            this.methodName = methodName;
   1.105 +        }
   1.106 +    }
   1.107  }