rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 05 Mar 2016 10:28:10 +0100
changeset 1886 c4d37f95adf2
parent 1829 729dc3c25fd8
child 1979 0f100539ce6c
permissions -rw-r--r--
RetroLamda needs to access resources from classpath, not the converted ones to deal with lamda interfaces with default methods
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.Attributes;
    36 import java.util.jar.JarEntry;
    37 import java.util.jar.JarFile;
    38 import java.util.logging.Level;
    39 import java.util.logging.Logger;
    40 import java.util.zip.ZipEntry;
    41 import org.apidesign.vm4brwsr.Bck2Brwsr;
    42 
    43 /** Utilities to process JAR files and set a compiler
    44  * up.
    45  *
    46  * @since 0.9
    47  * @author Jaroslav Tulach
    48  */
    49 public final class Bck2BrwsrJars {
    50     private static final Logger LOG = Logger.getLogger(Bck2BrwsrJars.class.getName());
    51 
    52     private Bck2BrwsrJars() {
    53     }
    54     
    55     /** Creates new compiler pre-configured from the content of 
    56      * provided JAR file. The compiler will compile all classes.
    57      * The system understands OSGi manifest entries and NetBeans
    58      * module system manifest entries and will export
    59      * all packages that are exported in the JAR file. The system
    60      * also recognizes META-INF/services and makes sure the class names
    61      * are not mangled.
    62      * 
    63      * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
    64      *    {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
    65      *    {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
    66      *    Can be <code>null</code> - in such case an 
    67      *    {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
    68      * @param jar the file to process
    69      * @return newly configured compiler
    70      * @throws IOException if something goes wrong
    71      */
    72     public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
    73         return configureFrom(c, jar, null);
    74     }
    75     
    76     /** Creates new compiler pre-configured from the content of 
    77      * provided JAR file. The compiler will compile all classes.
    78      * The system understands OSGi manifest entries and NetBeans
    79      * module system manifest entries and will export
    80      * all packages that are exported in the JAR file. The system
    81      * also recognizes META-INF/services and makes sure the class names
    82      * are not mangled.
    83      * 
    84      * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
    85      *    {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
    86      *    {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
    87      *    Can be <code>null</code> - in such case an 
    88      *    {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
    89      * @param jar the file to process
    90      * @param classpath additional resources to make available during
    91      *   compilation, but not include them in the generated JavaScript
    92      * @return newly configured compiler
    93      * @throws IOException if something goes wrong
    94      * @since 0.11
    95      */
    96     public static Bck2Brwsr configureFrom(
    97         Bck2Brwsr c, File jar, final ClassLoader classpath
    98     ) throws IOException {
    99         return configureFrom(c, jar, classpath, true);
   100     }
   101     
   102     /** Creates new compiler pre-configured from the content of 
   103      * provided JAR file. The compiler will compile all classes.
   104      * The system understands OSGi manifest entries and NetBeans
   105      * module system manifest entries and will export
   106      * all packages that are exported in the JAR file. The system
   107      * also recognizes META-INF/services and makes sure the class names
   108      * are not mangled.
   109      * 
   110      * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
   111      *    {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
   112      *    {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
   113      *    Can be <code>null</code> - in such case an 
   114      *    {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
   115      * @param jar the file to process
   116      * @param classpath additional resources to make available during
   117      *   compilation, but not include them in the generated JavaScript
   118      * @param ignoreBootClassPath should we ignore classes on bootclasspath?
   119      * @return newly configured compiler
   120      * @throws IOException if something goes wrong
   121      * @since 0.14
   122      */
   123     public static Bck2Brwsr configureFrom(
   124         Bck2Brwsr c, File jar, final ClassLoader classpath, final boolean ignoreBootClassPath
   125     ) throws IOException {
   126         if (jar.isDirectory()) {
   127             return configureDir(ignoreBootClassPath, c, jar, classpath);
   128         }
   129         final JarFile jf = new JarFile(jar);
   130         final List<String> classes = new ArrayList<>();
   131         List<String> resources = new ArrayList<>();
   132         Set<String> exported = new HashSet<>();
   133         class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
   134             JarRes() {
   135                 super(ignoreBootClassPath, classpath, classes);
   136             }
   137             @Override
   138             public InputStream get(String resource) throws IOException {
   139                 InputStream is = getConverted(resource);
   140                 if (is != null) {
   141                     return is;
   142                 }
   143                 if (resource.startsWith("/")) {
   144                     resource = resource.substring(1);
   145                 }
   146                 ZipEntry ze = jf.getEntry(resource);
   147                 if (ze != null) {
   148                     is = jf.getInputStream(ze);
   149                 }
   150                 return is == null ? super.get(resource) : is;
   151             }
   152         }
   153         JarRes jarRes = new JarRes();
   154 
   155         listJAR(jf, jarRes, resources, exported);
   156         
   157         String cp = jf.getManifest().getMainAttributes().getValue("Class-Path"); // NOI18N
   158         String[] parts = cp == null ? new String[0] : cp.split(" ");
   159 
   160         if (c == null) {
   161             c = Bck2Brwsr.newCompiler();
   162         }
   163         
   164         return c
   165             .library(parts)
   166             .addClasses(classes.toArray(new String[classes.size()]))
   167             .addExported(exported.toArray(new String[exported.size()]))
   168             .addResources(resources.toArray(new String[resources.size()]))
   169             .resources(jarRes);
   170     }
   171     
   172     private static void listJAR(
   173         JarFile j, EmulationResources classes,
   174         List<String> resources, Set<String> keep
   175     ) throws IOException {
   176         Enumeration<JarEntry> en = j.entries();
   177         while (en.hasMoreElements()) {
   178             JarEntry e = en.nextElement();
   179             final String n = e.getName();
   180             if (n.endsWith("/")) {
   181                 continue;
   182             }
   183             if (n.startsWith("META-INF/maven/")) {
   184                 continue;
   185             }
   186             int last = n.lastIndexOf('/');
   187             String pkg = n.substring(0, last + 1);
   188             if (pkg.startsWith("java/")) {
   189                 keep.add(pkg);
   190             }
   191             if (n.endsWith(".class")) {
   192                 classes.addClassResource(n);
   193             } else {
   194                 resources.add(n);
   195                 if (n.startsWith("META-INF/services/") && keep != null) {
   196                     BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
   197                     for (;;) {
   198                         String l = r.readLine();
   199                         if (l == null) {
   200                             break;
   201                         }
   202                         if (l.startsWith("#")) {
   203                             continue;
   204                         }
   205                         keep.add(l.replace('.', '/'));
   206                     }
   207                     r.close();
   208                 }
   209             }
   210         }
   211         if (keep != null) {
   212             final Attributes mainAttr = j.getManifest().getMainAttributes();
   213             exportPublicPackages(mainAttr, keep);
   214         }
   215     }
   216 
   217     static void exportPublicPackages(final Attributes mainAttr, Set<String> keep) {
   218         String exp = mainAttr.getValue("Export-Package"); // NOI18N
   219         if (exp != null) {
   220             for (String def : exp.split(",")) {
   221                 for (String sep : def.split(";")) {
   222                     keep.add(sep.replace('.', '/') + "/");
   223                     break;
   224                 }
   225             }
   226             return;
   227         }
   228         exp = mainAttr.getValue("OpenIDE-Module-Public-Packages");
   229         if (exp != null) {
   230             for (String def : exp.split(",")) {
   231                 def = def.trim();
   232                 if (def.endsWith(".*")) {
   233                     keep.add(def.substring(0, def.length() - 1).replace('.', '/'));
   234                 }
   235             }
   236         }
   237     }
   238     
   239     static byte[] readFrom(InputStream is) throws IOException {
   240         int expLen = is.available();
   241         if (expLen < 1) {
   242             expLen = 1;
   243         }
   244         byte[] arr = new byte[expLen];
   245         int pos = 0;
   246         for (;;) {
   247             int read = is.read(arr, pos, arr.length - pos);
   248             if (read == -1) {
   249                 break;
   250             }
   251             pos += read;
   252             if (pos == arr.length) {
   253                 byte[] tmp = new byte[arr.length * 2];
   254                 System.arraycopy(arr, 0, tmp, 0, arr.length);
   255                 arr = tmp;
   256             }
   257         }
   258         if (pos != arr.length) {
   259             byte[] tmp = new byte[pos];
   260             System.arraycopy(arr, 0, tmp, 0, pos);
   261             arr = tmp;
   262         }
   263         return arr;
   264     }
   265     
   266 
   267     static class EmulationResources implements Bck2Brwsr.Resources {
   268         private final List<String> classes;
   269         private final Map<String,byte[]> converted = new HashMap<>();
   270         private final BytecodeProcessor proc;
   271         private final ClassLoader cp;
   272         private final boolean ignoreBootClassPath;
   273 
   274         protected EmulationResources(boolean ignoreBootClassPath, ClassLoader cp, List<String> classes) {
   275             this.ignoreBootClassPath = ignoreBootClassPath;
   276             this.classes = classes;
   277             this.cp = cp != null ? cp : Bck2BrwsrJars.class.getClassLoader();
   278             BytecodeProcessor p;
   279             try {
   280                 Class<?> bpClass = Class.forName("org.apidesign.bck2brwsr.aot.RetroLambda");
   281                 p = (BytecodeProcessor) bpClass.newInstance();
   282             } catch (Throwable t) {
   283                 p = null;
   284             }
   285             this.proc = p;
   286         }
   287 
   288         protected final InputStream getConverted(String name) throws IOException {
   289             byte[] arr = converted.get(name);
   290             if (arr != null) {
   291                 return new ByteArrayInputStream(arr);
   292             }
   293             return null;
   294         }
   295         
   296         @Override
   297         public InputStream get(String name) throws IOException {
   298             InputStream is = getConverted(name);
   299             if (is != null) {
   300                 return is;
   301             }
   302             return getFromCp(name);
   303         }
   304 
   305         private InputStream getFromCp(String name) throws IOException {
   306             Enumeration<URL> en = cp.getResources(name);
   307             URL u = null;
   308             while (en.hasMoreElements()) {
   309                 u = en.nextElement();
   310             }
   311             if (u == null) {
   312                 LOG.log(Level.FINE, "Cannot find {0}", name);
   313                 return null;
   314             }
   315             if (ignoreBootClassPath && u.toExternalForm().contains("/rt.jar!")) {
   316                 LOG.log(Level.WARNING, "No bootdelegation for {0}", name);
   317                 return null;
   318             }
   319             return u.openStream();
   320         }
   321 
   322         private final class NoConvRes implements Bck2Brwsr.Resources {
   323             @Override
   324             public InputStream get(String resource) throws IOException {
   325                 return getFromCp(resource);
   326             }
   327         }
   328 
   329         private void addClassResource(String n) throws IOException {
   330             if (proc != null) {
   331                 try (InputStream is = this.get(n)) {
   332                     Map<String, byte[]> conv = proc.process(n, readFrom(is), new NoConvRes());
   333                     if (conv != null) {
   334                         boolean found = false;
   335                         for (Map.Entry<String, byte[]> entrySet : conv.entrySet()) {
   336                             String res = entrySet.getKey();
   337                             byte[] bytes = entrySet.getValue();
   338                             if (res.equals(n)) {
   339                                 found = true;
   340                             }
   341                             assert res.endsWith(".class") : "Wrong resource: " + res;
   342                             converted.put(res, bytes);
   343                             classes.add(res.substring(0, res.length() - 6));
   344                         }
   345                         if (!found) {
   346                             throw new IOException("Cannot find " + n + " among " + conv);
   347                         }
   348                         return;
   349                     }
   350                 }
   351             }
   352             classes.add(n.substring(0, n.length() - 6));
   353         }
   354     }
   355     
   356     private static Bck2Brwsr configureDir(final boolean ignoreBootClassPath, Bck2Brwsr c, final File dir, ClassLoader cp) throws IOException {
   357         List<String> arr = new ArrayList<>();
   358         List<String> classes = new ArrayList<>();
   359         class DirRes extends EmulationResources {
   360             public DirRes(ClassLoader cp, List<String> classes) {
   361                 super(ignoreBootClassPath, cp, classes);
   362             }
   363 
   364             @Override
   365             public InputStream get(String name) throws IOException {
   366                 InputStream is = super.get(name);
   367                 if (is != null) {
   368                     return is;
   369                 }
   370                 File r = new File(dir, name.replace('/', File.separatorChar));
   371                 if (r.exists()) {
   372                     return new FileInputStream(r);
   373                 }
   374                 return null;
   375             }
   376         }
   377         DirRes dirRes = new DirRes(cp, classes);
   378         listDir(dir, null, dirRes, arr);
   379         if (c == null) {
   380             c = Bck2Brwsr.newCompiler();
   381         }
   382         return c
   383         .addRootClasses(classes.toArray(new String[0]))
   384         .addResources(arr.toArray(new String[0]))
   385         .library()
   386         //.obfuscation(ObfuscationLevel.FULL)
   387         .resources(dirRes);
   388     }
   389 
   390     private static void listDir(
   391         File f, String pref, EmulationResources res, List<String> resources
   392     ) throws IOException {
   393         File[] arr = f.listFiles();
   394         if (arr == null) {
   395             if (f.getName().endsWith(".class")) {
   396                 res.addClassResource(pref + f.getName());
   397             } else {
   398                 resources.add(pref + f.getName());
   399             }
   400         } else {
   401             for (File ch : arr) {
   402                 listDir(ch, pref == null ? "" : pref + f.getName() + "/", res, resources);
   403             }
   404         }
   405     }
   406     
   407 }