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