Can use precompiled versions even when running tests
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 08 Dec 2014 00:22:49 +0100
changeset 1746001275142bf7
parent 1745 40d676c86a87
child 1747 58fd5ea7ad4e
Can use precompiled versions even when running tests
launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java
launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
     1.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Sat Dec 06 11:11:45 2014 +0100
     1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java	Mon Dec 08 00:22:49 2014 +0100
     1.3 @@ -32,19 +32,24 @@
     1.4  import java.net.URI;
     1.5  import java.net.URISyntaxException;
     1.6  import java.net.URL;
     1.7 +import java.net.URLConnection;
     1.8  import java.util.ArrayList;
     1.9  import java.util.Arrays;
    1.10  import java.util.Enumeration;
    1.11  import java.util.HashSet;
    1.12  import java.util.LinkedHashSet;
    1.13  import java.util.List;
    1.14 +import java.util.Map;
    1.15 +import java.util.Properties;
    1.16  import java.util.Set;
    1.17  import java.util.concurrent.BlockingQueue;
    1.18  import java.util.concurrent.Callable;
    1.19  import java.util.concurrent.CountDownLatch;
    1.20  import java.util.concurrent.LinkedBlockingQueue;
    1.21  import java.util.concurrent.TimeUnit;
    1.22 +import java.util.jar.Attributes;
    1.23  import java.util.jar.JarFile;
    1.24 +import java.util.jar.Manifest;
    1.25  import java.util.logging.Level;
    1.26  import java.util.logging.Logger;
    1.27  import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
    1.28 @@ -567,7 +572,7 @@
    1.29  
    1.30      abstract void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException;
    1.31      abstract String harnessResource();
    1.32 -    String compileJar(URL jar) throws IOException {
    1.33 +    Object compileJar(URL jar, URL precompiled) throws IOException {
    1.34          return null;
    1.35      }
    1.36      String compileFromClassPath(URL f, Res loader) throws IOException {
    1.37 @@ -587,8 +592,53 @@
    1.38      final class Res {
    1.39          private final Set<URL> ignore = new HashSet<URL>();
    1.40          
    1.41 -        String compileJar(URL jarURL) throws IOException {
    1.42 -            String ret = BaseHTTPLauncher.this.compileJar(jarURL);
    1.43 +        Object compileJar(URL jarURL) throws IOException {
    1.44 +            List<String[]> libraries = new ArrayList<String[]>();
    1.45 +            for (ClassLoader loader : loaders) {
    1.46 +                Enumeration<URL> en = loader.getResources("META-INF/MANIFEST.MF");
    1.47 +                while (en.hasMoreElements()) {
    1.48 +                    URL e = en.nextElement();
    1.49 +                    Manifest mf = new Manifest(e.openStream());
    1.50 +                    for (Map.Entry<String, Attributes> entrySet : mf.getEntries().entrySet()) {
    1.51 +                        String key = entrySet.getKey();
    1.52 +                        Attributes attr = entrySet.getValue();
    1.53 +                        
    1.54 +                        final String a = attr.getValue("Bck2BrwsrArtifactId");
    1.55 +                        final String g = attr.getValue("Bck2BrwsrGroupId");
    1.56 +                        final String v = attr.getValue("Bck2BrwsrVersion");
    1.57 +                        final String d = attr.getValue("Bck2BrwsrDebug");
    1.58 +                        
    1.59 +                        if (g != null && a != null && v != null && "true".equals(d)) {
    1.60 +                            libraries.add(new String[] {
    1.61 +                                a, g, v, key
    1.62 +                            });
    1.63 +                        }
    1.64 +                    }
    1.65 +                }
    1.66 +            }
    1.67 +            URL precompiled = null;
    1.68 +            for (ClassLoader loader : loaders) {
    1.69 +                for (String[] lib : libraries) {
    1.70 +                    final String res = "META-INF/maven/" + lib[1] + "/" + lib[0] + "/pom.properties";
    1.71 +                    URL props = loader.getResource(res);
    1.72 +                    if (props != null) {
    1.73 +                        URLConnection c = props.openConnection();
    1.74 +                        Properties load = new Properties();
    1.75 +                        final InputStream is = c.getInputStream();
    1.76 +                        load.load(is);
    1.77 +                        is.close();
    1.78 +                        if (lib[2].equals(load.getProperty("version"))) {
    1.79 +                            if (c instanceof JarURLConnection) {
    1.80 +                                final URL definedInURL = ((JarURLConnection)c).getJarFileURL();
    1.81 +                                if (definedInURL.equals(jarURL)) {
    1.82 +                                    precompiled = loader.getResource(lib[3]);
    1.83 +                                }
    1.84 +                            }
    1.85 +                        }
    1.86 +                    }
    1.87 +                }
    1.88 +            }
    1.89 +            Object ret = BaseHTTPLauncher.this.compileJar(jarURL, precompiled);
    1.90              ignore.add(jarURL);
    1.91              return ret;
    1.92          }
    1.93 @@ -750,18 +800,22 @@
    1.94                  response.setCharacterEncoding("UTF-8");
    1.95                  if (url.getProtocol().equals("jar")) {
    1.96                      JarURLConnection juc = (JarURLConnection) url.openConnection();
    1.97 -                    String s = null;
    1.98 +                    Object s = null;
    1.99                      try {
   1.100                          s = loader.compileJar(juc.getJarFileURL());
   1.101                      } catch (IOException iOException) {
   1.102                          throw new IOException("Can't compile " + url.toExternalForm(), iOException);
   1.103                      }
   1.104 -                    if (s != null) {
   1.105 +                    if (s instanceof String) {
   1.106                          Writer w = response.getWriter();
   1.107 -                        w.append(s);
   1.108 +                        w.append((String)s);
   1.109                          w.close();
   1.110                          return;
   1.111                      }
   1.112 +                    if (s instanceof InputStream) {
   1.113 +                        copyStream((InputStream) s, response.getOutputStream(), null);
   1.114 +                        return;
   1.115 +                    }
   1.116                  }
   1.117                  if (url.getProtocol().equals("file")) {
   1.118                      final String filePart = url.getFile();
     2.1 --- a/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Sat Dec 06 11:11:45 2014 +0100
     2.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Mon Dec 08 00:22:49 2014 +0100
     2.3 @@ -24,7 +24,9 @@
     2.4  import java.net.MalformedURLException;
     2.5  import java.net.URISyntaxException;
     2.6  import java.net.URL;
     2.7 +import java.util.Arrays;
     2.8  import java.util.HashSet;
     2.9 +import java.util.List;
    2.10  import java.util.Set;
    2.11  import java.util.logging.Level;
    2.12  
    2.13 @@ -46,13 +48,18 @@
    2.14      }
    2.15  
    2.16      @Override
    2.17 -    String compileJar(URL jar) throws IOException {
    2.18 +    Object compileJar(URL jar, URL precompiled) throws IOException {
    2.19 +        if (precompiled != null) {
    2.20 +            LOG.log(Level.INFO, "Found precompiled JAR version of {0} at {1}. Using.", new Object[]{jar, precompiled});
    2.21 +            return precompiled.openStream();
    2.22 +        }
    2.23          File f;
    2.24          try {
    2.25              f = new File(jar.toURI());
    2.26          } catch (URISyntaxException ex) {
    2.27              throw new IOException(ex);
    2.28          }
    2.29 +        LOG.log(Level.INFO, "No precompiled version for {0} found. Compiling.", jar);
    2.30          return CompileCP.compileJAR(f, testClasses);
    2.31      }
    2.32