Support processing of lambda also in directories jdk8
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 13 Sep 2014 14:19:43 +0200
branchjdk8
changeset 167993f4fbc4d1b7
parent 1678 35daab73e225
child 1680 3b553acbd931
Support processing of lambda also in directories
launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java
     1.1 --- a/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java	Sat Sep 13 13:44:01 2014 +0200
     1.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java	Sat Sep 13 14:19:43 2014 +0200
     1.3 @@ -77,26 +77,9 @@
     1.4          }
     1.5          if (s != null) {
     1.6              File root = new File(s);
     1.7 -            List<String> arr = new ArrayList<>();
     1.8 -            List<String> classes = new ArrayList<>();
     1.9 -            listDir(root, null, classes, arr);
    1.10              StringWriter w = new StringWriter();
    1.11              try {
    1.12 -                Bck2Brwsr.newCompiler()
    1.13 -                    .addRootClasses(classes.toArray(new String[0]))
    1.14 -                    .addResources(arr.toArray(new String[0]))
    1.15 -                    .library()
    1.16 -                    //.obfuscation(ObfuscationLevel.FULL)
    1.17 -                    .resources(new EmulationResources() {
    1.18 -                        @Override
    1.19 -                        public InputStream get(String resource) throws IOException {
    1.20 -                            if (r != null) {
    1.21 -                                final URL url = r.get(resource, 0);
    1.22 -                                return url == null ? null : url.openStream();
    1.23 -                            }
    1.24 -                            return super.get(resource);
    1.25 -                        }
    1.26 -                    })
    1.27 +                Bck2BrwsrJars.configureFrom(null, root)
    1.28                      .generate(w);
    1.29                  w.flush();
    1.30                  return w.toString();
    1.31 @@ -109,23 +92,6 @@
    1.32          return null;
    1.33      }
    1.34      
    1.35 -
    1.36 -    private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
    1.37 -        File[] arr = f.listFiles();
    1.38 -        if (arr == null) {
    1.39 -            if (f.getName().endsWith(".class")) {
    1.40 -                classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
    1.41 -            } else {
    1.42 -                resources.add(pref + f.getName());
    1.43 -            }
    1.44 -        } else {
    1.45 -            for (File ch : arr) {
    1.46 -                
    1.47 -                listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
    1.48 -            }
    1.49 -        }
    1.50 -    }
    1.51 -
    1.52      static void compileVM(StringBuilder sb, final Res r) throws IOException {
    1.53          final Bck2Brwsr rt;
    1.54          try {
    1.55 @@ -155,26 +121,4 @@
    1.56                  }
    1.57              }).generate(sb);
    1.58      }
    1.59 -
    1.60 -    static class EmulationResources implements Bck2Brwsr.Resources {
    1.61 -
    1.62 -        @Override
    1.63 -        public InputStream get(String name) throws IOException {
    1.64 -            Enumeration<URL> en = Bck2BrwsrJars.class.getClassLoader().getResources(name);
    1.65 -            URL u = null;
    1.66 -            while (en.hasMoreElements()) {
    1.67 -                u = en.nextElement();
    1.68 -            }
    1.69 -            if (u == null) {
    1.70 -                LOG.log(Level.WARNING, "Cannot find {0}", name);
    1.71 -                return null;
    1.72 -            }
    1.73 -            if (u.toExternalForm().contains("/rt.jar!")) {
    1.74 -                LOG.log(Level.WARNING, "{0}No bootdelegation for ", name);
    1.75 -                return null;
    1.76 -            }
    1.77 -            return u.openStream();
    1.78 -        }
    1.79 -    }
    1.80 -    
    1.81  }
     2.1 --- a/rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java	Sat Sep 13 13:44:01 2014 +0200
     2.2 +++ b/rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java	Sat Sep 13 14:19:43 2014 +0200
     2.3 @@ -19,6 +19,7 @@
     2.4  
     2.5  import java.io.BufferedReader;
     2.6  import java.io.File;
     2.7 +import java.io.FileInputStream;
     2.8  import java.io.IOException;
     2.9  import java.io.InputStream;
    2.10  import java.io.InputStreamReader;
    2.11 @@ -45,7 +46,7 @@
    2.12   */
    2.13  public final class Bck2BrwsrJars {
    2.14      private static final Logger LOG = Logger.getLogger(Bck2BrwsrJars.class.getName());
    2.15 -    
    2.16 +
    2.17      private Bck2BrwsrJars() {
    2.18      }
    2.19      
    2.20 @@ -66,6 +67,9 @@
    2.21       * @throws IOException if something goes wrong
    2.22       */
    2.23      public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
    2.24 +        if (jar.isDirectory()) {
    2.25 +            return configureDir(c, jar);
    2.26 +        }
    2.27          final JarFile jf = new JarFile(jar);
    2.28          final List<String> classes = new ArrayList<>();
    2.29          List<String> resources = new ArrayList<>();
    2.30 @@ -228,5 +232,55 @@
    2.31          }
    2.32      }
    2.33      
    2.34 +    private static Bck2Brwsr configureDir(Bck2Brwsr c, final File dir) throws IOException {
    2.35 +        List<String> arr = new ArrayList<>();
    2.36 +        List<String> classes = new ArrayList<>();
    2.37 +        class DirRes extends EmulationResources {
    2.38 +            public DirRes(List<String> classes) {
    2.39 +                super(classes);
    2.40 +            }
    2.41 +
    2.42 +            @Override
    2.43 +            public InputStream get(String name) throws IOException {
    2.44 +                InputStream is = super.get(name);
    2.45 +                if (is != null) {
    2.46 +                    return is;
    2.47 +                }
    2.48 +                File r = new File(dir, name.replace('/', File.separatorChar));
    2.49 +                if (r.exists()) {
    2.50 +                    return new FileInputStream(r);
    2.51 +                }
    2.52 +                return null;
    2.53 +            }
    2.54 +        }
    2.55 +        DirRes dirRes = new DirRes(classes);
    2.56 +        listDir(dir, null, dirRes, arr);
    2.57 +        if (c == null) {
    2.58 +            c = Bck2Brwsr.newCompiler();
    2.59 +        }
    2.60 +        return c
    2.61 +        .addRootClasses(classes.toArray(new String[0]))
    2.62 +        .addResources(arr.toArray(new String[0]))
    2.63 +        .library()
    2.64 +        //.obfuscation(ObfuscationLevel.FULL)
    2.65 +        .resources(dirRes);
    2.66 +    }
    2.67 +
    2.68 +    private static void listDir(
    2.69 +        File f, String pref, EmulationResources res, List<String> resources
    2.70 +    ) throws IOException {
    2.71 +        File[] arr = f.listFiles();
    2.72 +        if (arr == null) {
    2.73 +            if (f.getName().endsWith(".class")) {
    2.74 +                res.addClassResource(pref + f.getName());
    2.75 +            } else {
    2.76 +                resources.add(pref + f.getName());
    2.77 +            }
    2.78 +        } else {
    2.79 +            for (File ch : arr) {
    2.80 +                listDir(ch, pref == null ? "" : pref + f.getName() + "/", res, resources);
    2.81 +            }
    2.82 +        }
    2.83 +    }
    2.84      
    2.85  }