1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Wed Jan 16 12:44:54 2013 +0100
1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Wed Jan 23 12:53:23 2013 +0100
1.3 @@ -56,7 +56,7 @@
1.4 */
1.5 final class Bck2BrwsrLauncher extends Launcher implements Closeable {
1.6 private static final Logger LOG = Logger.getLogger(Bck2BrwsrLauncher.class.getName());
1.7 - private static final MethodInvocation END = new MethodInvocation(null, null);
1.8 + private static final MethodInvocation END = new MethodInvocation(null, null, null);
1.9 private Set<ClassLoader> loaders = new LinkedHashSet<>();
1.10 private Set<Bck2Brwsr.Resources> xRes = new LinkedHashSet<>();
1.11 private BlockingQueue<MethodInvocation> methods = new LinkedBlockingQueue<>();
1.12 @@ -72,9 +72,9 @@
1.13 }
1.14
1.15 @Override
1.16 - public MethodInvocation addMethod(Class<?> clazz, String method) throws IOException {
1.17 + MethodInvocation addMethod(Class<?> clazz, String method, String html) throws IOException {
1.18 loaders.add(clazz.getClassLoader());
1.19 - MethodInvocation c = new MethodInvocation(clazz.getName(), method);
1.20 + MethodInvocation c = new MethodInvocation(clazz.getName(), method, html);
1.21 methods.add(c);
1.22 try {
1.23 c.await(timeOut);
1.24 @@ -187,7 +187,13 @@
1.25 + "className: '" + cn + "', "
1.26 + "methodName: '" + mn + "', "
1.27 + "request: " + cnt
1.28 - + "}");
1.29 + );
1.30 + if (mi.html != null) {
1.31 + response.getWriter().write(", html: '");
1.32 + response.getWriter().write(encodeJSON(mi.html));
1.33 + response.getWriter().write("'");
1.34 + }
1.35 + response.getWriter().write("}");
1.36 cnt++;
1.37 }
1.38 }, "/data");
1.39 @@ -195,6 +201,22 @@
1.40 this.brwsr = launchServerAndBrwsr(server, "/execute");
1.41 }
1.42
1.43 + private static String encodeJSON(String in) {
1.44 + StringBuilder sb = new StringBuilder();
1.45 + for (int i = 0; i < in.length(); i++) {
1.46 + char ch = in.charAt(i);
1.47 + if (ch < 32 || ch == '\'' || ch == '"') {
1.48 + sb.append("\\u");
1.49 + String hs = "0000" + Integer.toHexString(ch);
1.50 + hs = hs.substring(hs.length() - 4);
1.51 + sb.append(hs);
1.52 + } else {
1.53 + sb.append(ch);
1.54 + }
1.55 + }
1.56 + return sb.toString();
1.57 + }
1.58 +
1.59 @Override
1.60 public void shutdown() throws IOException {
1.61 methods.offer(END);
1.62 @@ -229,12 +251,12 @@
1.63 if (ch == -1) {
1.64 break;
1.65 }
1.66 - if (ch == '$') {
1.67 + if (ch == '$' && params.length > 0) {
1.68 int cnt = is.read() - '0';
1.69 if (cnt == 'U' - '0') {
1.70 os.write(baseURL.getBytes());
1.71 }
1.72 - if (cnt < params.length) {
1.73 + if (cnt >= 0 && cnt < params.length) {
1.74 os.write(params[cnt].getBytes());
1.75 }
1.76 } else {
1.77 @@ -252,11 +274,17 @@
1.78 LOG.log(Level.INFO, "Showing {0}", uri);
1.79 if (cmd == null) {
1.80 try {
1.81 + LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[] {
1.82 + System.getProperty("java.vm.name"),
1.83 + System.getProperty("java.vm.vendor"),
1.84 + System.getProperty("java.vm.version"),
1.85 + });
1.86 java.awt.Desktop.getDesktop().browse(uri);
1.87 LOG.log(Level.INFO, "Desktop.browse successfully finished");
1.88 return null;
1.89 } catch (UnsupportedOperationException ex) {
1.90 - LOG.log(Level.INFO, "Desktop.browse not supported", ex);
1.91 + LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
1.92 + LOG.log(Level.FINE, null, ex);
1.93 }
1.94 }
1.95 {
1.96 @@ -366,7 +394,7 @@
1.97 public Page(Res res, String resource, String... args) {
1.98 this.res = res;
1.99 this.resource = resource;
1.100 - this.args = args;
1.101 + this.args = args.length == 0 ? new String[] { "$0" } : args;
1.102 }
1.103
1.104 @Override
1.105 @@ -378,17 +406,20 @@
1.106 r = r.substring(1);
1.107 }
1.108 }
1.109 + String[] replace = {};
1.110 if (r.endsWith(".html")) {
1.111 response.setContentType("text/html");
1.112 LOG.info("Content type text/html");
1.113 + replace = args;
1.114 }
1.115 if (r.endsWith(".xhtml")) {
1.116 response.setContentType("application/xhtml+xml");
1.117 LOG.info("Content type application/xhtml+xml");
1.118 + replace = args;
1.119 }
1.120 OutputStream os = response.getOutputStream();
1.121 try (InputStream is = res.get(r)) {
1.122 - copyStream(is, os, request.getRequestURL().toString(), args);
1.123 + copyStream(is, os, request.getRequestURL().toString(), replace);
1.124 } catch (IOException ex) {
1.125 response.setDetailMessage(ex.getLocalizedMessage());
1.126 response.setError();