rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 26 May 2014 18:29:46 +0200
branchclosure
changeset 1602 5591182757a0
parent 1601 a872960dce93
child 1603 3ee8458a6ca9
permissions -rw-r--r--
Skip Maven related info 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.HashSet;
    29 import java.util.List;
    30 import java.util.Set;
    31 import java.util.jar.JarEntry;
    32 import java.util.jar.JarFile;
    33 import java.util.logging.Level;
    34 import java.util.logging.Logger;
    35 import java.util.zip.ZipEntry;
    36 import org.apidesign.vm4brwsr.Bck2Brwsr;
    37 
    38 /** Utilities to process JAR files and set a compiler
    39  * up 
    40  *
    41  * @author Jaroslav Tulach
    42  */
    43 public final class Bck2BrwsrJars {
    44     private static final Logger LOG = Logger.getLogger(Bck2BrwsrJars.class.getName());
    45     
    46     private Bck2BrwsrJars() {
    47     }
    48     
    49     /** Creates new compiler pre-configured from the content of 
    50      * provided JAR file. The compiler will compile all classes.
    51      * The system understands OSGi manifest entries and will export
    52      * all packages that are exported in the JAR file. The system
    53      * also recognizes META-INF/services and makes sure the file names
    54      * are not mangled.
    55      * 
    56      * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
    57      *    {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
    58      *    {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
    59      *    Can be <code>null</code> - in such case an 
    60      *    {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
    61      * @param jar the file to process
    62      * @return newly configured compiler
    63      * @throws IOException if something goes wrong
    64      */
    65     public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
    66         final JarFile jf = new JarFile(jar);
    67         List<String> classes = new ArrayList<>();
    68         List<String> resources = new ArrayList<>();
    69         Set<String> exported = new HashSet<>();
    70 
    71         listJAR(jf, classes, resources, exported);
    72 
    73         class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
    74 
    75             @Override
    76             public InputStream get(String resource) throws IOException {
    77                 InputStream is = jf.getInputStream(new ZipEntry(resource));
    78                 return is == null ? super.get(resource) : is;
    79             }
    80         }
    81         return Bck2Brwsr.newCompiler()
    82             .library(true)
    83             .addClasses(classes.toArray(new String[classes.size()]))
    84             .addExported(exported.toArray(new String[exported.size()]))
    85             .addResources(resources.toArray(new String[resources.size()]))
    86             .resources(new JarRes());
    87     }
    88     
    89     private static void listJAR(
    90         JarFile j, List<String> classes,
    91         List<String> resources, Set<String> keep
    92     ) throws IOException {
    93         Enumeration<JarEntry> en = j.entries();
    94         while (en.hasMoreElements()) {
    95             JarEntry e = en.nextElement();
    96             final String n = e.getName();
    97             if (n.endsWith("/")) {
    98                 continue;
    99             }
   100             if (n.startsWith("META-INF/maven/")) {
   101                 continue;
   102             }
   103             int last = n.lastIndexOf('/');
   104             String pkg = n.substring(0, last + 1);
   105             if (pkg.startsWith("java/")) {
   106                 keep.add(pkg);
   107             }
   108             if (n.endsWith(".class")) {
   109                 classes.add(n.substring(0, n.length() - 6));
   110             } else {
   111                 resources.add(n);
   112                 if (n.startsWith("META-INF/services/") && keep != null) {
   113                     BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
   114                     for (;;) {
   115                         String l = r.readLine();
   116                         if (l == null) {
   117                             break;
   118                         }
   119                         if (l.startsWith("#")) {
   120                             continue;
   121                         }
   122                         keep.add(l.replace('.', '/'));
   123                     }
   124                 }
   125             }
   126         }
   127         String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
   128         if (exp != null && keep != null) {
   129             for (String def : exp.split(",")) {
   130                 for (String sep : def.split(";")) {
   131                     keep.add(sep.replace('.', '/') + "/");
   132                     break;
   133                 }
   134             }
   135         }
   136     }
   137 
   138     static class EmulationResources implements Bck2Brwsr.Resources {
   139 
   140         @Override
   141         public InputStream get(String name) throws IOException {
   142             Enumeration<URL> en = Bck2BrwsrJars.class.getClassLoader().getResources(name);
   143             URL u = null;
   144             while (en.hasMoreElements()) {
   145                 u = en.nextElement();
   146             }
   147             if (u == null) {
   148                 LOG.log(Level.WARNING, "Cannot find {0}", name);
   149                 return null;
   150             }
   151             if (u.toExternalForm().contains("/rt.jar!")) {
   152                 LOG.log(Level.WARNING, "{0}No bootdelegation for ", name);
   153                 return null;
   154             }
   155             return u.openStream();
   156         }
   157     }
   158     
   159 }