launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
branchclosure
changeset 1525 777bd3ed81ba
parent 1523 d1eeb43a75a3
child 1547 7c10f6d5635c
     1.1 --- a/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java	Mon May 05 08:36:50 2014 +0200
     1.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java	Mon May 05 10:16:30 2014 +0200
     1.3 @@ -17,16 +17,20 @@
     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 @@ -41,10 +45,26 @@
    1.25   */
    1.26  class CompileCP {
    1.27      private static final Logger LOG = Logger.getLogger(CompileCP.class.getName());
    1.28 -    static String compileJAR(final JarFile jar) throws IOException {
    1.29 +    static String compileJAR(final JarFile jar, Set<String> testClasses) 
    1.30 +    throws IOException {
    1.31          List<String> arr = new ArrayList<>();
    1.32          List<String> classes = new ArrayList<>();
    1.33 -        listJAR(jar, classes, arr);
    1.34 +        Set<String> exported = new HashSet<String>();
    1.35 +        Set<String> keep = new HashSet<String>(testClasses);
    1.36 +        listJAR(jar, classes, arr, exported, keep);
    1.37 +        List<String> root = new ArrayList<>();
    1.38 +        for (String c : classes) {
    1.39 +            if (keep.contains(c)) {
    1.40 +                root.add(c);
    1.41 +                continue;
    1.42 +            }
    1.43 +            int slash = c.lastIndexOf('/');
    1.44 +            String pkg = c.substring(0, slash + 1);
    1.45 +            if (exported.contains(pkg)) {
    1.46 +                root.add(c);
    1.47 +            }
    1.48 +        }
    1.49 +        
    1.50          StringWriter w = new StringWriter();
    1.51          try {
    1.52              class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
    1.53 @@ -57,6 +77,7 @@
    1.54              
    1.55              Bck2Brwsr.newCompiler()
    1.56                  .addClasses(classes.toArray(new String[0]))
    1.57 +                .addRootClasses(root.toArray(new String[0]))
    1.58                  .addResources(arr.toArray(new String[0]))
    1.59                  .library(true)
    1.60                  .resources(new JarRes())
    1.61 @@ -115,7 +136,10 @@
    1.62          return null;
    1.63      }
    1.64      
    1.65 -    private static void listJAR(JarFile j, List<String> classes, List<String> resources) throws IOException {
    1.66 +    private static void listJAR(
    1.67 +        JarFile j, List<String> classes,
    1.68 +        List<String> resources, Set<String> exported, Set<String> keep
    1.69 +    ) throws IOException {
    1.70          Enumeration<JarEntry> en = j.entries();
    1.71          while (en.hasMoreElements()) {
    1.72              JarEntry e = en.nextElement();
    1.73 @@ -135,6 +159,28 @@
    1.74                  classes.add(n.substring(0, n.length() - 6));
    1.75              } else {
    1.76                  resources.add(n);
    1.77 +                if (n.startsWith("META-INF/services/") && keep != null) {
    1.78 +                    BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
    1.79 +                    for (;;) {
    1.80 +                        String l = r.readLine();
    1.81 +                        if (l == null) {
    1.82 +                            break;
    1.83 +                        }
    1.84 +                        if (l.startsWith("#")) {
    1.85 +                            continue;
    1.86 +                        }
    1.87 +                        keep.add(l.replace('.', '/'));
    1.88 +                    }
    1.89 +                }
    1.90 +            }
    1.91 +        }
    1.92 +        String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
    1.93 +        if (exp != null && exported != null) {
    1.94 +            for (String def : exp.split(",")) {
    1.95 +                for (String sep : def.split(";")) {
    1.96 +                    exported.add(sep.replace('.', '/') + "/");
    1.97 +                    break;
    1.98 +                }
    1.99              }
   1.100          }
   1.101      }
   1.102 @@ -168,7 +214,7 @@
   1.103          
   1.104          List<String> arr = new ArrayList<>();
   1.105          List<String> classes = new ArrayList<>();
   1.106 -        listJAR(juc.getJarFile(), classes, arr);
   1.107 +        listJAR(juc.getJarFile(), classes, arr, null, null);
   1.108  
   1.109          Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
   1.110              .resources(new Bck2Brwsr.Resources() {