launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Apr 2014 21:30:06 +0200
branchclosure
changeset 1491 4a1398eff4fb
parent 1489 8d0fc428ff72
child 1492 e38025e9536a
permissions -rw-r--r--
Different meaning of root vs. added classes. Ability to explicitly enumerate classes that should be exported and available with fully qualified name.
     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.File;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import java.io.StringWriter;
    24 import java.net.URISyntaxException;
    25 import java.net.URL;
    26 import java.util.ArrayList;
    27 import java.util.Enumeration;
    28 import java.util.List;
    29 import java.util.jar.JarEntry;
    30 import java.util.jar.JarFile;
    31 import java.util.zip.ZipEntry;
    32 import org.apidesign.vm4brwsr.Bck2Brwsr;
    33 
    34 /**
    35  *
    36  * @author Jaroslav Tulach
    37  */
    38 class CompileCP {
    39     static String compileJAR(final JarFile jar) throws IOException {
    40         List<String> arr = new ArrayList<>();
    41         List<String> classes = new ArrayList<>();
    42         listJAR(jar, classes, arr);
    43         StringWriter w = new StringWriter();
    44         try {
    45             class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
    46                 @Override
    47                 public InputStream get(String resource) throws IOException {
    48                     InputStream is = jar.getInputStream(new ZipEntry(resource));
    49                     return is == null ? super.get(resource) : is;
    50                 }
    51             }
    52             
    53             Bck2Brwsr.newCompiler()
    54                 .addClasses(classes.toArray(new String[0]))
    55                 .library(true)
    56                 .resources(new JarRes())
    57                 .generate(w);
    58             w.flush();
    59             return w.toString();
    60         } catch (Throwable ex) {
    61             throw new IOException("Cannot compile: ", ex);
    62         } finally {
    63             w.close();
    64         }
    65     }
    66     
    67     static String compileFromClassPath(URL u) throws IOException, URISyntaxException {
    68         File f = new File(u.toURI());
    69         for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
    70             if (!f.getPath().startsWith(s)) {
    71                 continue;
    72             }
    73             File root = new File(s);
    74             List<String> arr = new ArrayList<>();
    75             List<String> classes = new ArrayList<>();
    76             listDir(root, null, classes, arr);
    77             StringWriter w = new StringWriter();
    78             try {
    79                 Bck2Brwsr.newCompiler()
    80                     .addRootClasses(classes.toArray(new String[0]))
    81                     .library(true)
    82                     .resources(new EmulationResources())
    83                     .generate(w);
    84                 w.flush();
    85                 return w.toString();
    86             } catch (ClassFormatError ex) {
    87                 throw new IOException(ex);
    88             } finally {
    89                 w.close();
    90             }
    91         }
    92         return null;
    93     }
    94     
    95     private static void listJAR(JarFile j, List<String> classes, List<String> resources) throws IOException {
    96         Enumeration<JarEntry> en = j.entries();
    97         while (en.hasMoreElements()) {
    98             JarEntry e = en.nextElement();
    99             final String n = e.getName();
   100             if (n.endsWith("/")) {
   101                 continue;
   102             }
   103             int last = n.lastIndexOf('/');
   104             String pkg = n.substring(0, last + 1);
   105             if (skipPkg(pkg)) {
   106                 continue;
   107             }
   108             if (n.endsWith(".class")) {
   109                 classes.add(n.substring(0, n.length() - 6));
   110             } else {
   111                 resources.add(n);
   112             }
   113         }
   114     }
   115 
   116     private static boolean skipPkg(String pkg) {
   117         return pkg.equals("org/apidesign/bck2brwsr/launcher/");
   118     }
   119     
   120     private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
   121         File[] arr = f.listFiles();
   122         if (arr == null) {
   123             if (f.getName().endsWith(".class")) {
   124                 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
   125             } else {
   126                 resources.add(pref + f.getName());
   127             }
   128         } else {
   129             for (File ch : arr) {
   130                 
   131                 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
   132             }
   133         }
   134     }
   135 
   136     static class EmulationResources implements Bck2Brwsr.Resources {
   137 
   138         @Override
   139         public InputStream get(String name) throws IOException {
   140             Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
   141             URL u = null;
   142             while (en.hasMoreElements()) {
   143                 u = en.nextElement();
   144             }
   145             if (u == null) {
   146                 throw new IOException("Can't find " + name);
   147             }
   148             if (u.toExternalForm().contains("rt.jar!")) {
   149                 throw new IOException("No emulation for " + u);
   150             }
   151             return u.openStream();
   152         }
   153     }
   154 }