rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 09 Jan 2015 21:38:00 +0100
changeset 1763 647282885e6f
parent 1729 384468666b2d
child 1764 26d709601e91
permissions -rw-r--r--
Process package-info.java normally
     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.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         if (jar.isDirectory()) {
   100             return configureDir(c, jar, classpath);
   101         }
   102         final JarFile jf = new JarFile(jar);
   103         final List<String> classes = new ArrayList<>();
   104         List<String> resources = new ArrayList<>();
   105         Set<String> exported = new HashSet<>();
   106         class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
   107             JarRes() {
   108                 super(classpath, classes);
   109             }
   110             @Override
   111             public InputStream get(String resource) throws IOException {
   112                 InputStream is = getConverted(resource);
   113                 if (is != null) {
   114                     return is;
   115                 }
   116                 is = jf.getInputStream(new ZipEntry(resource));
   117                 return is == null ? super.get(resource) : is;
   118             }
   119         }
   120         JarRes jarRes = new JarRes();
   121 
   122         listJAR(jf, jarRes, resources, exported);
   123         
   124         String cp = jf.getManifest().getMainAttributes().getValue("Class-Path"); // NOI18N
   125         String[] parts = cp == null ? new String[0] : cp.split(" ");
   126 
   127         if (c == null) {
   128             c = Bck2Brwsr.newCompiler();
   129         }
   130         
   131         return c
   132             .library(parts)
   133             .addClasses(classes.toArray(new String[classes.size()]))
   134             .addExported(exported.toArray(new String[exported.size()]))
   135             .addResources(resources.toArray(new String[resources.size()]))
   136             .resources(jarRes);
   137     }
   138     
   139     private static void listJAR(
   140         JarFile j, EmulationResources classes,
   141         List<String> resources, Set<String> keep
   142     ) throws IOException {
   143         Enumeration<JarEntry> en = j.entries();
   144         while (en.hasMoreElements()) {
   145             JarEntry e = en.nextElement();
   146             final String n = e.getName();
   147             if (n.endsWith("/")) {
   148                 continue;
   149             }
   150             if (n.startsWith("META-INF/maven/")) {
   151                 continue;
   152             }
   153             int last = n.lastIndexOf('/');
   154             String pkg = n.substring(0, last + 1);
   155             if (pkg.startsWith("java/")) {
   156                 keep.add(pkg);
   157             }
   158             if (n.endsWith(".class")) {
   159                 classes.addClassResource(n);
   160             } else {
   161                 resources.add(n);
   162                 if (n.startsWith("META-INF/services/") && keep != null) {
   163                     BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
   164                     for (;;) {
   165                         String l = r.readLine();
   166                         if (l == null) {
   167                             break;
   168                         }
   169                         if (l.startsWith("#")) {
   170                             continue;
   171                         }
   172                         keep.add(l.replace('.', '/'));
   173                     }
   174                 }
   175             }
   176         }
   177         if (keep != null) {
   178             final Attributes mainAttr = j.getManifest().getMainAttributes();
   179             exportPublicPackages(mainAttr, keep);
   180         }
   181     }
   182 
   183     static void exportPublicPackages(final Attributes mainAttr, Set<String> keep) {
   184         String exp = mainAttr.getValue("Export-Package"); // NOI18N
   185         if (exp != null) {
   186             for (String def : exp.split(",")) {
   187                 for (String sep : def.split(";")) {
   188                     keep.add(sep.replace('.', '/') + "/");
   189                     break;
   190                 }
   191             }
   192             return;
   193         }
   194         exp = mainAttr.getValue("OpenIDE-Module-Public-Packages");
   195         if (exp != null) {
   196             for (String def : exp.split(",")) {
   197                 def = def.trim();
   198                 if (def.endsWith(".*")) {
   199                     keep.add(def.substring(0, def.length() - 1).replace('.', '/'));
   200                 }
   201             }
   202         }
   203     }
   204     
   205     static byte[] readFrom(InputStream is) throws IOException {
   206         int expLen = is.available();
   207         if (expLen < 1) {
   208             expLen = 1;
   209         }
   210         byte[] arr = new byte[expLen];
   211         int pos = 0;
   212         for (;;) {
   213             int read = is.read(arr, pos, arr.length - pos);
   214             if (read == -1) {
   215                 break;
   216             }
   217             pos += read;
   218             if (pos == arr.length) {
   219                 byte[] tmp = new byte[arr.length * 2];
   220                 System.arraycopy(arr, 0, tmp, 0, arr.length);
   221                 arr = tmp;
   222             }
   223         }
   224         if (pos != arr.length) {
   225             byte[] tmp = new byte[pos];
   226             System.arraycopy(arr, 0, tmp, 0, pos);
   227             arr = tmp;
   228         }
   229         return arr;
   230     }
   231     
   232 
   233     static class EmulationResources implements Bck2Brwsr.Resources {
   234         private final List<String> classes;
   235         private final Map<String,byte[]> converted = new HashMap<>();
   236         private final BytecodeProcessor proc;
   237         private final ClassLoader cp;
   238 
   239         protected EmulationResources(ClassLoader cp, List<String> classes) {
   240             this.classes = classes;
   241             this.cp = cp != null ? cp : Bck2BrwsrJars.class.getClassLoader();
   242             BytecodeProcessor p;
   243             try {
   244                 Class<?> bpClass = Class.forName("org.apidesign.bck2brwsr.aot.RetroLambda");
   245                 p = (BytecodeProcessor) bpClass.newInstance();
   246             } catch (Throwable t) {
   247                 p = null;
   248             }
   249             this.proc = p;
   250         }
   251 
   252         protected final InputStream getConverted(String name) throws IOException {
   253             byte[] arr = converted.get(name);
   254             if (arr != null) {
   255                 return new ByteArrayInputStream(arr);
   256             }
   257             return null;
   258         }
   259         
   260         @Override
   261         public InputStream get(String name) throws IOException {
   262             InputStream is = getConverted(name);
   263             if (is != null) {
   264                 return is;
   265             }
   266             Enumeration<URL> en = cp.getResources(name);
   267             URL u = null;
   268             while (en.hasMoreElements()) {
   269                 u = en.nextElement();
   270             }
   271             if (u == null) {
   272                 LOG.log(Level.FINE, "Cannot find {0}", name);
   273                 return null;
   274             }
   275             if (u.toExternalForm().contains("/rt.jar!")) {
   276                 LOG.log(Level.WARNING, "No bootdelegation for {0}", name);
   277                 return null;
   278             }
   279             return u.openStream();
   280         }
   281 
   282         private void addClassResource(String n) throws IOException {
   283             if (proc != null) {
   284                 try (InputStream is = this.get(n)) {
   285                     Map<String, byte[]> conv = proc.process(n, readFrom(is), this);
   286                     if (conv != null) {
   287                         boolean found = false;
   288                         for (Map.Entry<String, byte[]> entrySet : conv.entrySet()) {
   289                             String res = entrySet.getKey();
   290                             byte[] bytes = entrySet.getValue();
   291                             if (res.equals(n)) {
   292                                 found = true;
   293                             }
   294                             assert res.endsWith(".class") : "Wrong resource: " + res;
   295                             converted.put(res, bytes);
   296                             classes.add(res.substring(0, res.length() - 6));
   297                         }
   298                         if (!found) {
   299                             throw new IOException("Cannot find " + n + " among " + conv);
   300                         }
   301                         return;
   302                     }
   303                 }
   304             }
   305             classes.add(n.substring(0, n.length() - 6));
   306         }
   307     }
   308     
   309     private static Bck2Brwsr configureDir(Bck2Brwsr c, final File dir, ClassLoader cp) throws IOException {
   310         List<String> arr = new ArrayList<>();
   311         List<String> classes = new ArrayList<>();
   312         class DirRes extends EmulationResources {
   313             public DirRes(ClassLoader cp, List<String> classes) {
   314                 super(cp, classes);
   315             }
   316 
   317             @Override
   318             public InputStream get(String name) throws IOException {
   319                 InputStream is = super.get(name);
   320                 if (is != null) {
   321                     return is;
   322                 }
   323                 File r = new File(dir, name.replace('/', File.separatorChar));
   324                 if (r.exists()) {
   325                     return new FileInputStream(r);
   326                 }
   327                 return null;
   328             }
   329         }
   330         DirRes dirRes = new DirRes(cp, classes);
   331         listDir(dir, null, dirRes, arr);
   332         if (c == null) {
   333             c = Bck2Brwsr.newCompiler();
   334         }
   335         return c
   336         .addRootClasses(classes.toArray(new String[0]))
   337         .addResources(arr.toArray(new String[0]))
   338         .library()
   339         //.obfuscation(ObfuscationLevel.FULL)
   340         .resources(dirRes);
   341     }
   342 
   343     private static void listDir(
   344         File f, String pref, EmulationResources res, List<String> resources
   345     ) throws IOException {
   346         File[] arr = f.listFiles();
   347         if (arr == null) {
   348             if (f.getName().endsWith(".class")) {
   349                 res.addClassResource(pref + f.getName());
   350             } else {
   351                 resources.add(pref + f.getName());
   352             }
   353         } else {
   354             for (File ch : arr) {
   355                 listDir(ch, pref == null ? "" : pref + f.getName() + "/", res, resources);
   356             }
   357         }
   358     }
   359     
   360 }