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