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