launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 29 Apr 2014 10:23:55 +0200
branchclosure
changeset 1498 9583bcc50db3
parent 1492 e38025e9536a
child 1504 d058edd87424
permissions -rw-r--r--
Making sure resources can be loaded in brwsr testing mode with extensions
     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) 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                     .generate(w);
    88                 w.flush();
    89                 return w.toString();
    90             } catch (ClassFormatError ex) {
    91                 throw new IOException(ex);
    92             } finally {
    93                 w.close();
    94             }
    95         }
    96         return null;
    97     }
    98     
    99     private static void listJAR(JarFile j, List<String> classes, List<String> resources) throws IOException {
   100         Enumeration<JarEntry> en = j.entries();
   101         while (en.hasMoreElements()) {
   102             JarEntry e = en.nextElement();
   103             final String n = e.getName();
   104             if (n.contains("package-info")) {
   105                 continue;
   106             }
   107             if (n.endsWith("/")) {
   108                 continue;
   109             }
   110             int last = n.lastIndexOf('/');
   111             String pkg = n.substring(0, last + 1);
   112             if (skipPkg(pkg)) {
   113                 continue;
   114             }
   115             if (n.endsWith(".class")) {
   116                 classes.add(n.substring(0, n.length() - 6));
   117             } else {
   118                 resources.add(n);
   119             }
   120         }
   121     }
   122 
   123     private static boolean skipPkg(String pkg) {
   124         return pkg.equals("org/apidesign/bck2brwsr/launcher/");
   125     }
   126     
   127     private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
   128         File[] arr = f.listFiles();
   129         if (arr == null) {
   130             if (f.getName().endsWith(".class")) {
   131                 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
   132             } else {
   133                 resources.add(pref + f.getName());
   134             }
   135         } else {
   136             for (File ch : arr) {
   137                 
   138                 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
   139             }
   140         }
   141     }
   142 
   143     static void compileVM(StringBuilder sb, Res r) throws IOException {
   144         URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class");
   145         JarURLConnection juc = (JarURLConnection)u.openConnection();
   146         
   147         List<String> arr = new ArrayList<>();
   148         List<String> classes = new ArrayList<>();
   149         listJAR(juc.getJarFile(), classes, arr);
   150 
   151         Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
   152             .resources(new EmulationResources())
   153             .generate(sb);
   154     }
   155 
   156     static class EmulationResources implements Bck2Brwsr.Resources {
   157 
   158         @Override
   159         public InputStream get(String name) throws IOException {
   160             Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
   161             URL u = null;
   162             while (en.hasMoreElements()) {
   163                 u = en.nextElement();
   164             }
   165             if (u == null) {
   166                 throw new IOException("Can't find " + name);
   167             }
   168             if (u.toExternalForm().contains("rt.jar!")) {
   169                 throw new IOException("No emulation for " + u);
   170             }
   171             return u.openStream();
   172         }
   173     }
   174 }