launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
branchclosure
changeset 1599 36746c46716a
parent 1586 d4ee65642d8d
child 1604 7665471a56c1
     1.1 --- a/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java	Thu May 22 15:29:40 2014 +0200
     1.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java	Mon May 26 17:52:56 2014 +0200
     1.3 @@ -17,28 +17,22 @@
     1.4   */
     1.5  package org.apidesign.bck2brwsr.launcher;
     1.6  
     1.7 -import java.io.BufferedReader;
     1.8  import java.io.File;
     1.9  import java.io.IOException;
    1.10  import java.io.InputStream;
    1.11 -import java.io.InputStreamReader;
    1.12  import java.io.StringWriter;
    1.13  import java.net.JarURLConnection;
    1.14  import java.net.URISyntaxException;
    1.15  import java.net.URL;
    1.16  import java.util.ArrayList;
    1.17  import java.util.Enumeration;
    1.18 -import java.util.HashSet;
    1.19  import java.util.List;
    1.20  import java.util.Set;
    1.21 -import java.util.jar.JarEntry;
    1.22 -import java.util.jar.JarFile;
    1.23  import java.util.logging.Level;
    1.24  import java.util.logging.Logger;
    1.25 -import java.util.zip.ZipEntry;
    1.26 +import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
    1.27  import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res;
    1.28  import org.apidesign.vm4brwsr.Bck2Brwsr;
    1.29 -import org.apidesign.vm4brwsr.ObfuscationLevel;
    1.30  
    1.31  /**
    1.32   *
    1.33 @@ -46,31 +40,11 @@
    1.34   */
    1.35  class CompileCP {
    1.36      private static final Logger LOG = Logger.getLogger(CompileCP.class.getName());
    1.37 -    static String compileJAR(final JarFile jar, Set<String> testClasses) 
    1.38 +    static String compileJAR(final File jar, Set<String> testClasses) 
    1.39      throws IOException {
    1.40 -        List<String> arr = new ArrayList<>();
    1.41 -        List<String> classes = new ArrayList<>();
    1.42 -        Set<String> keep = new HashSet<String>(testClasses);
    1.43 -        listJAR(jar, classes, arr, keep);
    1.44 -        
    1.45          StringWriter w = new StringWriter();
    1.46          try {
    1.47 -            class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
    1.48 -                @Override
    1.49 -                public InputStream get(String resource) throws IOException {
    1.50 -                    InputStream is = jar.getInputStream(new ZipEntry(resource));
    1.51 -                    return is == null ? super.get(resource) : is;
    1.52 -                }
    1.53 -            }
    1.54 -            
    1.55 -            Bck2Brwsr.newCompiler()
    1.56 -                .addClasses(classes.toArray(new String[0]))
    1.57 -                .addExported(keep.toArray(new String[0]))
    1.58 -                .addResources(arr.toArray(new String[0]))
    1.59 -                //.obfuscation(ObfuscationLevel.FULL)
    1.60 -                .library(true)
    1.61 -                .resources(new JarRes())
    1.62 -                .generate(w);
    1.63 +            Bck2BrwsrJars.configureFrom(null, jar).generate(w);
    1.64              w.flush();
    1.65              return w.toString();
    1.66          } catch (IOException ex) {
    1.67 @@ -126,56 +100,7 @@
    1.68          return null;
    1.69      }
    1.70      
    1.71 -    private static void listJAR(
    1.72 -        JarFile j, List<String> classes,
    1.73 -        List<String> resources, Set<String> keep
    1.74 -    ) throws IOException {
    1.75 -        Enumeration<JarEntry> en = j.entries();
    1.76 -        while (en.hasMoreElements()) {
    1.77 -            JarEntry e = en.nextElement();
    1.78 -            final String n = e.getName();
    1.79 -            if (n.endsWith("/")) {
    1.80 -                continue;
    1.81 -            }
    1.82 -            int last = n.lastIndexOf('/');
    1.83 -            String pkg = n.substring(0, last + 1);
    1.84 -            if (skipPkg(pkg)) {
    1.85 -                continue;
    1.86 -            }
    1.87 -            if (n.endsWith(".class")) {
    1.88 -                classes.add(n.substring(0, n.length() - 6));
    1.89 -            } else {
    1.90 -                resources.add(n);
    1.91 -                if (n.startsWith("META-INF/services/") && keep != null) {
    1.92 -                    BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
    1.93 -                    for (;;) {
    1.94 -                        String l = r.readLine();
    1.95 -                        if (l == null) {
    1.96 -                            break;
    1.97 -                        }
    1.98 -                        if (l.startsWith("#")) {
    1.99 -                            continue;
   1.100 -                        }
   1.101 -                        keep.add(l.replace('.', '/'));
   1.102 -                    }
   1.103 -                }
   1.104 -            }
   1.105 -        }
   1.106 -        String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
   1.107 -        if (exp != null && keep != null) {
   1.108 -            for (String def : exp.split(",")) {
   1.109 -                for (String sep : def.split(";")) {
   1.110 -                    keep.add(sep.replace('.', '/') + "/");
   1.111 -                    break;
   1.112 -                }
   1.113 -            }
   1.114 -        }
   1.115 -    }
   1.116  
   1.117 -    private static boolean skipPkg(String pkg) {
   1.118 -        return pkg.equals("org/apidesign/bck2brwsr/launcher/");
   1.119 -    }
   1.120 -    
   1.121      private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
   1.122          File[] arr = f.listFiles();
   1.123          if (arr == null) {
   1.124 @@ -193,21 +118,24 @@
   1.125      }
   1.126  
   1.127      static void compileVM(StringBuilder sb, final Res r) throws IOException {
   1.128 -        List<String> arr = new ArrayList<>();
   1.129 -        List<String> classes = new ArrayList<>();
   1.130 -
   1.131 -        {
   1.132 +        final Bck2Brwsr rt;
   1.133 +        try {
   1.134              URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
   1.135              JarURLConnection juc = (JarURLConnection)u.openConnection();
   1.136 -            listJAR(juc.getJarFile(), classes, arr, null);
   1.137 +            rt = Bck2BrwsrJars.configureFrom(null, new File(juc.getJarFileURL().toURI()));
   1.138 +        } catch (URISyntaxException ex) {
   1.139 +            throw new IOException(ex);
   1.140          }
   1.141 -        {
   1.142 +        final Bck2Brwsr all;
   1.143 +        try {
   1.144              URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
   1.145              JarURLConnection juc = (JarURLConnection)u.openConnection();
   1.146 -            listJAR(juc.getJarFile(), classes, arr, null);
   1.147 +            all = Bck2BrwsrJars.configureFrom(rt, new File(juc.getJarFileURL().toURI()));
   1.148 +        } catch (URISyntaxException ex) {
   1.149 +            throw new IOException(ex);
   1.150          }
   1.151  
   1.152 -        Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
   1.153 +        all.library(false)
   1.154              //.obfuscation(ObfuscationLevel.FULL)
   1.155              .resources(new Bck2Brwsr.Resources() {
   1.156                  @Override
   1.157 @@ -222,7 +150,7 @@
   1.158  
   1.159          @Override
   1.160          public InputStream get(String name) throws IOException {
   1.161 -            Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
   1.162 +            Enumeration<URL> en = Bck2BrwsrJars.class.getClassLoader().getResources(name);
   1.163              URL u = null;
   1.164              while (en.hasMoreElements()) {
   1.165                  u = en.nextElement();
   1.166 @@ -232,10 +160,11 @@
   1.167                  return null;
   1.168              }
   1.169              if (u.toExternalForm().contains("/rt.jar!")) {
   1.170 -                LOG.warning(name + "No bootdelegation for ");
   1.171 +                LOG.log(Level.WARNING, "{0}No bootdelegation for ", name);
   1.172                  return null;
   1.173              }
   1.174              return u.openStream();
   1.175          }
   1.176      }
   1.177 +    
   1.178  }