launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 21 May 2014 23:42:24 +0200
branchclosure
changeset 1583 89b6b369c13d
parent 1552 5a840853d1a8
child 1586 d4ee65642d8d
permissions -rw-r--r--
Support for exporting whole packages. addRootClasses defined in terms of addClasses and addExported.
     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> keep = new HashSet<String>(testClasses);
    53         listJAR(jar, classes, arr, keep);
    54         
    55         StringWriter w = new StringWriter();
    56         try {
    57             class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
    58                 @Override
    59                 public InputStream get(String resource) throws IOException {
    60                     InputStream is = jar.getInputStream(new ZipEntry(resource));
    61                     return is == null ? super.get(resource) : is;
    62                 }
    63             }
    64             
    65             Bck2Brwsr.newCompiler()
    66                 .addClasses(classes.toArray(new String[0]))
    67                 .addExported(keep.toArray(new String[0]))
    68                 .addResources(arr.toArray(new String[0]))
    69                 .library(true)
    70                 .resources(new JarRes())
    71                 .generate(w);
    72             w.flush();
    73             return w.toString();
    74         } catch (IOException ex) {
    75             throw ex;
    76         } catch (Throwable ex) {
    77             throw new IOException("Cannot compile: ", ex);
    78         } finally {
    79             w.close();
    80         }
    81     }
    82     
    83     static String compileFromClassPath(URL u, final Res r) throws IOException {
    84         File f;
    85         try {
    86             f = new File(u.toURI());
    87         } catch (URISyntaxException ex) {
    88             throw new IOException(ex);
    89         }
    90         for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
    91             if (!f.getPath().startsWith(s)) {
    92                 continue;
    93             }
    94             File root = new File(s);
    95             List<String> arr = new ArrayList<>();
    96             List<String> classes = new ArrayList<>();
    97             listDir(root, null, classes, arr);
    98             StringWriter w = new StringWriter();
    99             try {
   100                 Bck2Brwsr.newCompiler()
   101                     .addRootClasses(classes.toArray(new String[0]))
   102                     .addResources(arr.toArray(new String[0]))
   103                     .library(true)
   104                     .resources(new EmulationResources() {
   105                         @Override
   106                         public InputStream get(String resource) throws IOException {
   107                             if (r != null) {
   108                                 final URL url = r.get(resource, 0);
   109                                 return url == null ? null : url.openStream();
   110                             }
   111                             return super.get(resource);
   112                         }
   113                     })
   114                     .generate(w);
   115                 w.flush();
   116                 return w.toString();
   117             } catch (ClassFormatError ex) {
   118                 throw new IOException(ex);
   119             } finally {
   120                 w.close();
   121             }
   122         }
   123         return null;
   124     }
   125     
   126     private static void listJAR(
   127         JarFile j, List<String> classes,
   128         List<String> resources, Set<String> keep
   129     ) throws IOException {
   130         Enumeration<JarEntry> en = j.entries();
   131         while (en.hasMoreElements()) {
   132             JarEntry e = en.nextElement();
   133             final String n = e.getName();
   134             if (n.endsWith("/")) {
   135                 continue;
   136             }
   137             int last = n.lastIndexOf('/');
   138             String pkg = n.substring(0, last + 1);
   139             if (skipPkg(pkg)) {
   140                 continue;
   141             }
   142             if (n.endsWith(".class")) {
   143                 classes.add(n.substring(0, n.length() - 6));
   144             } else {
   145                 resources.add(n);
   146                 if (n.startsWith("META-INF/services/") && keep != null) {
   147                     BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
   148                     for (;;) {
   149                         String l = r.readLine();
   150                         if (l == null) {
   151                             break;
   152                         }
   153                         if (l.startsWith("#")) {
   154                             continue;
   155                         }
   156                         keep.add(l.replace('.', '/'));
   157                     }
   158                 }
   159             }
   160         }
   161         String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
   162         if (exp != null && keep != null) {
   163             for (String def : exp.split(",")) {
   164                 for (String sep : def.split(";")) {
   165                     keep.add(sep.replace('.', '/') + "/");
   166                     break;
   167                 }
   168             }
   169         }
   170     }
   171 
   172     private static boolean skipPkg(String pkg) {
   173         return pkg.equals("org/apidesign/bck2brwsr/launcher/");
   174     }
   175     
   176     private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
   177         File[] arr = f.listFiles();
   178         if (arr == null) {
   179             if (f.getName().endsWith(".class")) {
   180                 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
   181             } else {
   182                 resources.add(pref + f.getName());
   183             }
   184         } else {
   185             for (File ch : arr) {
   186                 
   187                 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
   188             }
   189         }
   190     }
   191 
   192     static void compileVM(StringBuilder sb, final Res r) throws IOException {
   193         List<String> arr = new ArrayList<>();
   194         List<String> classes = new ArrayList<>();
   195 
   196         {
   197             URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
   198             JarURLConnection juc = (JarURLConnection)u.openConnection();
   199             listJAR(juc.getJarFile(), classes, arr, null);
   200         }
   201         {
   202             URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
   203             JarURLConnection juc = (JarURLConnection)u.openConnection();
   204             listJAR(juc.getJarFile(), classes, arr, null);
   205         }
   206 
   207         Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
   208             .resources(new Bck2Brwsr.Resources() {
   209                 @Override
   210                 public InputStream get(String resource) throws IOException {
   211                     final URL url = r.get(resource, 0);
   212                     return url == null ? null : url.openStream();
   213                 }
   214             }).generate(sb);
   215     }
   216 
   217     static class EmulationResources implements Bck2Brwsr.Resources {
   218 
   219         @Override
   220         public InputStream get(String name) throws IOException {
   221             Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
   222             URL u = null;
   223             while (en.hasMoreElements()) {
   224                 u = en.nextElement();
   225             }
   226             if (u == null) {
   227                 LOG.log(Level.WARNING, "Cannot find {0}", name);
   228                 return null;
   229             }
   230             if (u.toExternalForm().contains("/rt.jar!")) {
   231                 LOG.warning(name + "No bootdelegation for ");
   232                 return null;
   233             }
   234             return u.openStream();
   235         }
   236     }
   237 }