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