rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 13 Sep 2014 16:11:42 +0200
branchjdk8
changeset 1684 3238bffeaf12
parent 1681 2082d4c4bf11
child 1696 ce34fdc36fac
permissions -rw-r--r--
Call RetroLambda during AOT compilation
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.aot;
    19 
    20 import java.io.BufferedReader;
    21 import java.io.ByteArrayInputStream;
    22 import java.io.File;
    23 import java.io.FileInputStream;
    24 import java.io.IOException;
    25 import java.io.InputStream;
    26 import java.io.InputStreamReader;
    27 import java.net.URL;
    28 import java.util.ArrayList;
    29 import java.util.Enumeration;
    30 import java.util.HashMap;
    31 import java.util.HashSet;
    32 import java.util.List;
    33 import java.util.Map;
    34 import java.util.Set;
    35 import java.util.jar.JarEntry;
    36 import java.util.jar.JarFile;
    37 import java.util.logging.Level;
    38 import java.util.logging.Logger;
    39 import java.util.zip.ZipEntry;
    40 import org.apidesign.vm4brwsr.Bck2Brwsr;
    41 
    42 /** Utilities to process JAR files and set a compiler
    43  * up.
    44  *
    45  * @since 0.9
    46  * @author Jaroslav Tulach
    47  */
    48 public final class Bck2BrwsrJars {
    49     private static final Logger LOG = Logger.getLogger(Bck2BrwsrJars.class.getName());
    50 
    51     private Bck2BrwsrJars() {
    52     }
    53     
    54     /** Creates new compiler pre-configured from the content of 
    55      * provided JAR file. The compiler will compile all classes.
    56      * The system understands OSGi manifest entries and will export
    57      * all packages that are exported in the JAR file. The system
    58      * also recognizes META-INF/services and makes sure the class names
    59      * are not mangled.
    60      * 
    61      * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
    62      *    {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
    63      *    {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
    64      *    Can be <code>null</code> - in such case an 
    65      *    {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
    66      * @param jar the file to process
    67      * @return newly configured compiler
    68      * @throws IOException if something goes wrong
    69      */
    70     public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
    71         if (jar.isDirectory()) {
    72             return configureDir(c, jar);
    73         }
    74         final JarFile jf = new JarFile(jar);
    75         final List<String> classes = new ArrayList<>();
    76         List<String> resources = new ArrayList<>();
    77         Set<String> exported = new HashSet<>();
    78         class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
    79             JarRes() {
    80                 super(classes);
    81             }
    82             @Override
    83             public InputStream get(String resource) throws IOException {
    84                 InputStream is = jf.getInputStream(new ZipEntry(resource));
    85                 return is == null ? super.get(resource) : is;
    86             }
    87         }
    88         JarRes jarRes = new JarRes();
    89 
    90         listJAR(jf, jarRes, resources, exported);
    91         
    92         String cp = jf.getManifest().getMainAttributes().getValue("Class-Path"); // NOI18N
    93         String[] classpath = cp == null ? new String[0] : cp.split(" ");
    94 
    95         if (c == null) {
    96             c = Bck2Brwsr.newCompiler();
    97         }
    98         
    99         return c
   100             .library(classpath)
   101             .addClasses(classes.toArray(new String[classes.size()]))
   102             .addExported(exported.toArray(new String[exported.size()]))
   103             .addResources(resources.toArray(new String[resources.size()]))
   104             .resources(jarRes);
   105     }
   106     
   107     private static void listJAR(
   108         JarFile j, EmulationResources classes,
   109         List<String> resources, Set<String> keep
   110     ) throws IOException {
   111         Enumeration<JarEntry> en = j.entries();
   112         while (en.hasMoreElements()) {
   113             JarEntry e = en.nextElement();
   114             final String n = e.getName();
   115             if (n.endsWith("/")) {
   116                 continue;
   117             }
   118             if (n.startsWith("META-INF/maven/")) {
   119                 continue;
   120             }
   121             int last = n.lastIndexOf('/');
   122             String pkg = n.substring(0, last + 1);
   123             if (pkg.startsWith("java/")) {
   124                 keep.add(pkg);
   125             }
   126             if (n.endsWith(".class")) {
   127                 classes.addClassResource(n);
   128             } else {
   129                 resources.add(n);
   130                 if (n.startsWith("META-INF/services/") && keep != null) {
   131                     BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
   132                     for (;;) {
   133                         String l = r.readLine();
   134                         if (l == null) {
   135                             break;
   136                         }
   137                         if (l.startsWith("#")) {
   138                             continue;
   139                         }
   140                         keep.add(l.replace('.', '/'));
   141                     }
   142                 }
   143             }
   144         }
   145         String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
   146         if (exp != null && keep != null) {
   147             for (String def : exp.split(",")) {
   148                 for (String sep : def.split(";")) {
   149                     keep.add(sep.replace('.', '/') + "/");
   150                     break;
   151                 }
   152             }
   153         }
   154     }
   155     
   156     static byte[] readFrom(InputStream is) throws IOException {
   157         int expLen = is.available();
   158         if (expLen < 1) {
   159             expLen = 1;
   160         }
   161         byte[] arr = new byte[expLen];
   162         int pos = 0;
   163         for (;;) {
   164             int read = is.read(arr, pos, arr.length - pos);
   165             if (read == -1) {
   166                 break;
   167             }
   168             pos += read;
   169             if (pos == arr.length) {
   170                 byte[] tmp = new byte[arr.length * 2];
   171                 System.arraycopy(arr, 0, tmp, 0, arr.length);
   172                 arr = tmp;
   173             }
   174         }
   175         if (pos != arr.length) {
   176             byte[] tmp = new byte[pos];
   177             System.arraycopy(arr, 0, tmp, 0, pos);
   178             arr = tmp;
   179         }
   180         return arr;
   181     }
   182     
   183 
   184     static class EmulationResources implements Bck2Brwsr.Resources {
   185         private final List<String> classes;
   186         private final Map<String,byte[]> converted = new HashMap<>();
   187         private final BytecodeProcessor proc;
   188 
   189         protected EmulationResources(List<String> classes) {
   190             this.classes = classes;
   191             BytecodeProcessor p;
   192             try {
   193                 Class<?> bpClass = Class.forName("org.apidesign.bck2brwsr.aot.RetroLambda");
   194                 p = (BytecodeProcessor) bpClass.newInstance();
   195             } catch (Throwable t) {
   196                 p = null;
   197             }
   198             this.proc = p;
   199         }
   200 
   201         @Override
   202         public InputStream get(String name) throws IOException {
   203             byte[] arr = converted.get(name);
   204             if (arr != null) {
   205                 return new ByteArrayInputStream(arr);
   206             }
   207             
   208             Enumeration<URL> en = Bck2BrwsrJars.class.getClassLoader().getResources(name);
   209             URL u = null;
   210             while (en.hasMoreElements()) {
   211                 u = en.nextElement();
   212             }
   213             if (u == null) {
   214                 LOG.log(Level.WARNING, "Cannot find {0}", name);
   215                 return null;
   216             }
   217             if (u.toExternalForm().contains("/rt.jar!")) {
   218                 LOG.log(Level.WARNING, "{0}No bootdelegation for ", name);
   219                 return null;
   220             }
   221             return u.openStream();
   222         }
   223 
   224         private void addClassResource(String n) throws IOException {
   225             if (proc != null) {
   226                 try (InputStream is = this.get(n)) {
   227                     Map<String, byte[]> conv = proc.process(n, readFrom(is), this);
   228                     if (conv != null) {
   229                         boolean found = false;
   230                         for (Map.Entry<String, byte[]> entrySet : conv.entrySet()) {
   231                             String res = entrySet.getKey();
   232                             byte[] bytes = entrySet.getValue();
   233                             if (res.equals(n)) {
   234                                 found = true;
   235                             }
   236                             assert res.endsWith(".class") : "Wrong resource: " + res;
   237                             converted.put(res, bytes);
   238                             classes.add(res.substring(0, res.length() - 6));
   239                         }
   240                         if (!found) {
   241                             throw new IOException("Cannot find " + n + " among " + conv);
   242                         }
   243                         return;
   244                     }
   245                 }
   246             }
   247             classes.add(n.substring(0, n.length() - 6));
   248         }
   249     }
   250     
   251     private static Bck2Brwsr configureDir(Bck2Brwsr c, final File dir) throws IOException {
   252         List<String> arr = new ArrayList<>();
   253         List<String> classes = new ArrayList<>();
   254         class DirRes extends EmulationResources {
   255             public DirRes(List<String> classes) {
   256                 super(classes);
   257             }
   258 
   259             @Override
   260             public InputStream get(String name) throws IOException {
   261                 InputStream is = super.get(name);
   262                 if (is != null) {
   263                     return is;
   264                 }
   265                 File r = new File(dir, name.replace('/', File.separatorChar));
   266                 if (r.exists()) {
   267                     return new FileInputStream(r);
   268                 }
   269                 return null;
   270             }
   271         }
   272         DirRes dirRes = new DirRes(classes);
   273         listDir(dir, null, dirRes, arr);
   274         if (c == null) {
   275             c = Bck2Brwsr.newCompiler();
   276         }
   277         return c
   278         .addRootClasses(classes.toArray(new String[0]))
   279         .addResources(arr.toArray(new String[0]))
   280         .library()
   281         //.obfuscation(ObfuscationLevel.FULL)
   282         .resources(dirRes);
   283     }
   284 
   285     private static void listDir(
   286         File f, String pref, EmulationResources res, List<String> resources
   287     ) throws IOException {
   288         File[] arr = f.listFiles();
   289         if (arr == null) {
   290             if (f.getName().endsWith(".class")) {
   291                 res.addClassResource(pref + f.getName());
   292             } else {
   293                 resources.add(pref + f.getName());
   294             }
   295         } else {
   296             for (File ch : arr) {
   297                 listDir(ch, pref == null ? "" : pref + f.getName() + "/", res, resources);
   298             }
   299         }
   300     }
   301     
   302 }