launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java
branchclosure
changeset 1489 8d0fc428ff72
parent 1098 fcf1d05b0d39
child 1500 345b8dd47e1d
     1.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Thu May 16 08:51:29 2013 +0200
     1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Sat Apr 26 19:13:56 2014 +0200
     1.3 @@ -26,6 +26,7 @@
     1.4  import java.io.Reader;
     1.5  import java.io.UnsupportedEncodingException;
     1.6  import java.io.Writer;
     1.7 +import java.net.JarURLConnection;
     1.8  import java.net.URI;
     1.9  import java.net.URISyntaxException;
    1.10  import java.net.URL;
    1.11 @@ -40,6 +41,7 @@
    1.12  import java.util.concurrent.CountDownLatch;
    1.13  import java.util.concurrent.LinkedBlockingQueue;
    1.14  import java.util.concurrent.TimeUnit;
    1.15 +import java.util.jar.JarFile;
    1.16  import java.util.logging.Level;
    1.17  import java.util.logging.Logger;
    1.18  import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
    1.19 @@ -495,16 +497,28 @@
    1.20  
    1.21      abstract void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException;
    1.22      abstract String harnessResource();
    1.23 +    String compileJar(JarFile jar) throws IOException {
    1.24 +        return null;
    1.25 +    }
    1.26 +    String compileFromClassPath(URL f) {
    1.27 +        return null;
    1.28 +    }
    1.29  
    1.30 -    class Res {
    1.31 -        public InputStream get(String resource) throws IOException {
    1.32 +    final class Res {
    1.33 +        String compileJar(JarFile jar) throws IOException {
    1.34 +            return BaseHTTPLauncher.this.compileJar(jar);
    1.35 +        }
    1.36 +        String compileFromClassPath(URL f) {
    1.37 +            return BaseHTTPLauncher.this.compileFromClassPath(f);
    1.38 +        }
    1.39 +        public URL get(String resource) throws IOException {
    1.40              URL u = null;
    1.41              for (ClassLoader l : loaders) {
    1.42                  Enumeration<URL> en = l.getResources(resource);
    1.43                  while (en.hasMoreElements()) {
    1.44                      u = en.nextElement();
    1.45                      if (u.toExternalForm().matches("^.*emul.*rt\\.jar.*$")) {
    1.46 -                        return u.openStream();
    1.47 +                        return u;
    1.48                      }
    1.49                  }
    1.50              }
    1.51 @@ -512,7 +526,7 @@
    1.52                  if (u.toExternalForm().contains("rt.jar")) {
    1.53                      LOG.log(Level.WARNING, "Fallback to bootclasspath for {0}", u);
    1.54                  }
    1.55 -                return u.openStream();
    1.56 +                return u;
    1.57              }
    1.58              throw new IOException("Can't find " + resource);
    1.59          }
    1.60 @@ -547,7 +561,7 @@
    1.61                  replace = args;
    1.62              }
    1.63              OutputStream os = response.getOutputStream();
    1.64 -            try (InputStream is = res.get(r)) {
    1.65 +            try (InputStream is = res.get(r).openStream()) {
    1.66                  copyStream(is, os, request.getRequestURL().toString(), replace);
    1.67              } catch (IOException ex) {
    1.68                  response.setDetailMessage(ex.getLocalizedMessage());
    1.69 @@ -603,10 +617,32 @@
    1.70              if (res.startsWith("/")) {
    1.71                  res = res.substring(1);
    1.72              }
    1.73 -            try (InputStream is = loader.get(res)) {
    1.74 +            URL url = loader.get(res);
    1.75 +            if (url.getProtocol().equals("jar")) {
    1.76 +                JarURLConnection juc = (JarURLConnection) url.openConnection();
    1.77 +                String s = loader.compileJar(juc.getJarFile());
    1.78 +                if (s != null) {
    1.79 +                    Writer w = response.getWriter();
    1.80 +                    w.append(s);
    1.81 +                    w.close();
    1.82 +                    return;
    1.83 +                }
    1.84 +            }
    1.85 +            if (url.getProtocol().equals("file")) {
    1.86 +                String s = loader.compileFromClassPath(url);
    1.87 +                if (s != null) {
    1.88 +                    Writer w = response.getWriter();
    1.89 +                    w.append(s);
    1.90 +                    w.close();
    1.91 +                    return;
    1.92 +                }
    1.93 +            }
    1.94 +            Exception ex = new Exception("Won't server bytes of " + url);
    1.95 +            /*
    1.96 +            try (InputStream is = url.openStream()) {
    1.97                  response.setContentType("text/javascript");
    1.98                  Writer w = response.getWriter();
    1.99 -                w.append("[");
   1.100 +                w.append("([");
   1.101                  for (int i = 0;; i++) {
   1.102                      int b = is.read();
   1.103                      if (b == -1) {
   1.104 @@ -623,12 +659,14 @@
   1.105                      }
   1.106                      w.append(Integer.toString(b));
   1.107                  }
   1.108 -                w.append("\n]");
   1.109 -            } catch (IOException ex) {
   1.110 +                w.append("\n])");
   1.111 +            } catch (IOException ex) 
   1.112 +            */ {
   1.113                  response.setStatus(HttpStatus.NOT_FOUND_404);
   1.114                  response.setError();
   1.115                  response.setDetailMessage(ex.getMessage());
   1.116              }
   1.117          }
   1.118 +
   1.119      }
   1.120  }