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