launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 05 May 2014 10:16:30 +0200
branchclosure
changeset 1525 777bd3ed81ba
parent 1523 d1eeb43a75a3
child 1547 7c10f6d5635c
permissions -rw-r--r--
Export only test classes, classes from exported packages and those referenced in META-INF/services
     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.launcher;
    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.io.StringWriter;
    26 import java.net.JarURLConnection;
    27 import java.net.URISyntaxException;
    28 import java.net.URL;
    29 import java.util.ArrayList;
    30 import java.util.Enumeration;
    31 import java.util.HashSet;
    32 import java.util.List;
    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.bck2brwsr.launcher.BaseHTTPLauncher.Res;
    40 import org.apidesign.vm4brwsr.Bck2Brwsr;
    41 
    42 /**
    43  *
    44  * @author Jaroslav Tulach
    45  */
    46 class CompileCP {
    47     private static final Logger LOG = Logger.getLogger(CompileCP.class.getName());
    48     static String compileJAR(final JarFile jar, Set<String> testClasses) 
    49     throws IOException {
    50         List<String> arr = new ArrayList<>();
    51         List<String> classes = new ArrayList<>();
    52         Set<String> exported = new HashSet<String>();
    53         Set<String> keep = new HashSet<String>(testClasses);
    54         listJAR(jar, classes, arr, exported, keep);
    55         List<String> root = new ArrayList<>();
    56         for (String c : classes) {
    57             if (keep.contains(c)) {
    58                 root.add(c);
    59                 continue;
    60             }
    61             int slash = c.lastIndexOf('/');
    62             String pkg = c.substring(0, slash + 1);
    63             if (exported.contains(pkg)) {
    64                 root.add(c);
    65             }
    66         }
    67         
    68         StringWriter w = new StringWriter();
    69         try {
    70             class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
    71                 @Override
    72                 public InputStream get(String resource) throws IOException {
    73                     InputStream is = jar.getInputStream(new ZipEntry(resource));
    74                     return is == null ? super.get(resource) : is;
    75                 }
    76             }
    77             
    78             Bck2Brwsr.newCompiler()
    79                 .addClasses(classes.toArray(new String[0]))
    80                 .addRootClasses(root.toArray(new String[0]))
    81                 .addResources(arr.toArray(new String[0]))
    82                 .library(true)
    83                 .resources(new JarRes())
    84                 .generate(w);
    85             w.flush();
    86             return w.toString();
    87         } catch (IOException ex) {
    88             throw ex;
    89         } catch (Throwable ex) {
    90             throw new IOException("Cannot compile: ", ex);
    91         } finally {
    92             w.close();
    93         }
    94     }
    95     
    96     static String compileFromClassPath(URL u, final Res r) throws IOException {
    97         File f;
    98         try {
    99             f = new File(u.toURI());
   100         } catch (URISyntaxException ex) {
   101             throw new IOException(ex);
   102         }
   103         for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
   104             if (!f.getPath().startsWith(s)) {
   105                 continue;
   106             }
   107             File root = new File(s);
   108             List<String> arr = new ArrayList<>();
   109             List<String> classes = new ArrayList<>();
   110             listDir(root, null, classes, arr);
   111             StringWriter w = new StringWriter();
   112             try {
   113                 Bck2Brwsr.newCompiler()
   114                     .addRootClasses(classes.toArray(new String[0]))
   115                     .addResources(arr.toArray(new String[0]))
   116                     .library(true)
   117                     .resources(new EmulationResources() {
   118                         @Override
   119                         public InputStream get(String resource) throws IOException {
   120                             if (r != null) {
   121                                 final URL url = r.get(resource, 0);
   122                                 return url == null ? null : url.openStream();
   123                             }
   124                             return super.get(resource);
   125                         }
   126                     })
   127                     .generate(w);
   128                 w.flush();
   129                 return w.toString();
   130             } catch (ClassFormatError ex) {
   131                 throw new IOException(ex);
   132             } finally {
   133                 w.close();
   134             }
   135         }
   136         return null;
   137     }
   138     
   139     private static void listJAR(
   140         JarFile j, List<String> classes,
   141         List<String> resources, Set<String> exported, 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.contains("package-info")) {
   148                 continue;
   149             }
   150             if (n.endsWith("/")) {
   151                 continue;
   152             }
   153             int last = n.lastIndexOf('/');
   154             String pkg = n.substring(0, last + 1);
   155             if (skipPkg(pkg)) {
   156                 continue;
   157             }
   158             if (n.endsWith(".class")) {
   159                 classes.add(n.substring(0, n.length() - 6));
   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         String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
   178         if (exp != null && exported != null) {
   179             for (String def : exp.split(",")) {
   180                 for (String sep : def.split(";")) {
   181                     exported.add(sep.replace('.', '/') + "/");
   182                     break;
   183                 }
   184             }
   185         }
   186     }
   187 
   188     private static boolean skipPkg(String pkg) {
   189         return pkg.equals("org/apidesign/bck2brwsr/launcher/");
   190     }
   191     
   192     private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
   193         File[] arr = f.listFiles();
   194         if (arr == null) {
   195             if (f.getName().equals("package-info.class")) {
   196                 return;
   197             }
   198             if (f.getName().endsWith(".class")) {
   199                 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
   200             } else {
   201                 resources.add(pref + f.getName());
   202             }
   203         } else {
   204             for (File ch : arr) {
   205                 
   206                 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
   207             }
   208         }
   209     }
   210 
   211     static void compileVM(StringBuilder sb, final Res r) throws IOException {
   212         URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
   213         JarURLConnection juc = (JarURLConnection)u.openConnection();
   214         
   215         List<String> arr = new ArrayList<>();
   216         List<String> classes = new ArrayList<>();
   217         listJAR(juc.getJarFile(), classes, arr, null, null);
   218 
   219         Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
   220             .resources(new Bck2Brwsr.Resources() {
   221                 @Override
   222                 public InputStream get(String resource) throws IOException {
   223                     final URL url = r.get(resource, 0);
   224                     return url == null ? null : url.openStream();
   225                 }
   226             }).generate(sb);
   227     }
   228 
   229     static class EmulationResources implements Bck2Brwsr.Resources {
   230 
   231         @Override
   232         public InputStream get(String name) throws IOException {
   233             Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
   234             URL u = null;
   235             while (en.hasMoreElements()) {
   236                 u = en.nextElement();
   237             }
   238             if (u == null) {
   239                 LOG.log(Level.WARNING, "Cannot find {0}", name);
   240                 return null;
   241             }
   242             if (u.toExternalForm().contains("/rt.jar!")) {
   243                 LOG.warning(name + "No bootdelegation for ");
   244                 return null;
   245             }
   246             return u.openStream();
   247         }
   248     }
   249 }