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