launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 29 Apr 2014 14:28:52 +0200
branchclosure
changeset 1504 d058edd87424
parent 1498 9583bcc50db3
child 1505 706b66d8c481
permissions -rw-r--r--
Use provided loader to load libraries
     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                 .addResources(arr.toArray(new String[0]))
    58                 .library(true)
    59                 .resources(new JarRes())
    60                 .generate(w);
    61             w.flush();
    62             return w.toString();
    63         } catch (Throwable ex) {
    64             throw new IOException("Cannot compile: ", ex);
    65         } finally {
    66             w.close();
    67         }
    68     }
    69     
    70     static String compileFromClassPath(URL u, final Res r) throws IOException, URISyntaxException {
    71         File f = new File(u.toURI());
    72         for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
    73             if (!f.getPath().startsWith(s)) {
    74                 continue;
    75             }
    76             File root = new File(s);
    77             List<String> arr = new ArrayList<>();
    78             List<String> classes = new ArrayList<>();
    79             listDir(root, null, classes, arr);
    80             StringWriter w = new StringWriter();
    81             try {
    82                 Bck2Brwsr.newCompiler()
    83                     .addRootClasses(classes.toArray(new String[0]))
    84                     .addResources(arr.toArray(new String[0]))
    85                     .library(true)
    86                     .resources(new EmulationResources() {
    87                         @Override
    88                         public InputStream get(String resource) throws IOException {
    89                             return r != null ? r.get(resource).openStream() : super.get(resource);
    90                         }
    91                     })
    92                     .generate(w);
    93                 w.flush();
    94                 return w.toString();
    95             } catch (ClassFormatError ex) {
    96                 throw new IOException(ex);
    97             } finally {
    98                 w.close();
    99             }
   100         }
   101         return null;
   102     }
   103     
   104     private static void listJAR(JarFile j, List<String> classes, List<String> resources) throws IOException {
   105         Enumeration<JarEntry> en = j.entries();
   106         while (en.hasMoreElements()) {
   107             JarEntry e = en.nextElement();
   108             final String n = e.getName();
   109             if (n.contains("package-info")) {
   110                 continue;
   111             }
   112             if (n.endsWith("/")) {
   113                 continue;
   114             }
   115             int last = n.lastIndexOf('/');
   116             String pkg = n.substring(0, last + 1);
   117             if (skipPkg(pkg)) {
   118                 continue;
   119             }
   120             if (n.endsWith(".class")) {
   121                 classes.add(n.substring(0, n.length() - 6));
   122             } else {
   123                 resources.add(n);
   124             }
   125         }
   126     }
   127 
   128     private static boolean skipPkg(String pkg) {
   129         return pkg.equals("org/apidesign/bck2brwsr/launcher/");
   130     }
   131     
   132     private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
   133         File[] arr = f.listFiles();
   134         if (arr == null) {
   135             if (f.getName().equals("package-info.class")) {
   136                 return;
   137             }
   138             if (f.getName().endsWith(".class")) {
   139                 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
   140             } else {
   141                 resources.add(pref + f.getName());
   142             }
   143         } else {
   144             for (File ch : arr) {
   145                 
   146                 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
   147             }
   148         }
   149     }
   150 
   151     static void compileVM(StringBuilder sb, final Res r) throws IOException {
   152         URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class");
   153         JarURLConnection juc = (JarURLConnection)u.openConnection();
   154         
   155         List<String> arr = new ArrayList<>();
   156         List<String> classes = new ArrayList<>();
   157         listJAR(juc.getJarFile(), classes, arr);
   158 
   159         Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
   160             .resources(new Bck2Brwsr.Resources() {
   161                 @Override
   162                 public InputStream get(String resource) throws IOException {
   163                     return r.get(resource).openStream();
   164                 }
   165             }).generate(sb);
   166     }
   167 
   168     static class EmulationResources implements Bck2Brwsr.Resources {
   169 
   170         @Override
   171         public InputStream get(String name) throws IOException {
   172             Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
   173             URL u = null;
   174             while (en.hasMoreElements()) {
   175                 u = en.nextElement();
   176             }
   177             if (u == null) {
   178                 throw new IOException("Can't find " + name);
   179             }
   180             if (u.toExternalForm().contains("rt.jar!")) {
   181                 throw new IOException("No emulation for " + u);
   182             }
   183             return u.openStream();
   184         }
   185     }
   186 }