launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 27 May 2014 15:22:04 +0200
branchclosure
changeset 1606 4e05b6eabbc5
parent 1605 c74047c30b8f
child 1614 8eba262bd8cd
permissions -rw-r--r--
Make sure test classes remain exported
     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.Set;
    31 import java.util.logging.Level;
    32 import java.util.logging.Logger;
    33 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
    34 import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res;
    35 import org.apidesign.vm4brwsr.Bck2Brwsr;
    36 
    37 /**
    38  *
    39  * @author Jaroslav Tulach
    40  */
    41 class CompileCP {
    42     private static final Logger LOG = Logger.getLogger(CompileCP.class.getName());
    43     static String compileJAR(final File jar, Set<String> testClasses) 
    44     throws IOException {
    45         StringWriter w = new StringWriter();
    46         try {
    47             Bck2BrwsrJars.configureFrom(null, jar)
    48                 .addExported(testClasses.toArray(new String[0]))
    49                 .generate(w);
    50             w.flush();
    51             return w.toString();
    52         } catch (IOException ex) {
    53             throw ex;
    54         } catch (Throwable ex) {
    55             throw new IOException("Cannot compile: ", ex);
    56         } finally {
    57             w.close();
    58         }
    59     }
    60     
    61     static String compileFromClassPath(URL u, final Res r) throws IOException {
    62         File f;
    63         try {
    64             f = new File(u.toURI());
    65         } catch (URISyntaxException ex) {
    66             throw new IOException(ex);
    67         }
    68         for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
    69             if (!f.getPath().startsWith(s)) {
    70                 continue;
    71             }
    72             File root = new File(s);
    73             List<String> arr = new ArrayList<>();
    74             List<String> classes = new ArrayList<>();
    75             listDir(root, null, classes, arr);
    76             StringWriter w = new StringWriter();
    77             try {
    78                 Bck2Brwsr.newCompiler()
    79                     .addRootClasses(classes.toArray(new String[0]))
    80                     .addResources(arr.toArray(new String[0]))
    81                     .library()
    82                     //.obfuscation(ObfuscationLevel.FULL)
    83                     .resources(new EmulationResources() {
    84                         @Override
    85                         public InputStream get(String resource) throws IOException {
    86                             if (r != null) {
    87                                 final URL url = r.get(resource, 0);
    88                                 return url == null ? null : url.openStream();
    89                             }
    90                             return super.get(resource);
    91                         }
    92                     })
    93                     .generate(w);
    94                 w.flush();
    95                 return w.toString();
    96             } catch (ClassFormatError ex) {
    97                 throw new IOException(ex);
    98             } finally {
    99                 w.close();
   100             }
   101         }
   102         return null;
   103     }
   104     
   105 
   106     private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
   107         File[] arr = f.listFiles();
   108         if (arr == null) {
   109             if (f.getName().endsWith(".class")) {
   110                 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
   111             } else {
   112                 resources.add(pref + f.getName());
   113             }
   114         } else {
   115             for (File ch : arr) {
   116                 
   117                 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
   118             }
   119         }
   120     }
   121 
   122     static void compileVM(StringBuilder sb, final Res r) throws IOException {
   123         final Bck2Brwsr rt;
   124         try {
   125             URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
   126             JarURLConnection juc = (JarURLConnection)u.openConnection();
   127             rt = Bck2BrwsrJars.configureFrom(null, new File(juc.getJarFileURL().toURI()));
   128         } catch (URISyntaxException ex) {
   129             throw new IOException(ex);
   130         }
   131         final Bck2Brwsr all;
   132         try {
   133             URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
   134             JarURLConnection juc = (JarURLConnection)u.openConnection();
   135             all = Bck2BrwsrJars.configureFrom(rt, new File(juc.getJarFileURL().toURI()));
   136         } catch (URISyntaxException ex) {
   137             throw new IOException(ex);
   138         }
   139 
   140         all
   141             .standalone(true)
   142             //.obfuscation(ObfuscationLevel.FULL)
   143             .resources(new Bck2Brwsr.Resources() {
   144                 @Override
   145                 public InputStream get(String resource) throws IOException {
   146                     final URL url = r.get(resource, 0);
   147                     return url == null ? null : url.openStream();
   148                 }
   149             }).generate(sb);
   150     }
   151 
   152     static class EmulationResources implements Bck2Brwsr.Resources {
   153 
   154         @Override
   155         public InputStream get(String name) throws IOException {
   156             Enumeration<URL> en = Bck2BrwsrJars.class.getClassLoader().getResources(name);
   157             URL u = null;
   158             while (en.hasMoreElements()) {
   159                 u = en.nextElement();
   160             }
   161             if (u == null) {
   162                 LOG.log(Level.WARNING, "Cannot find {0}", name);
   163                 return null;
   164             }
   165             if (u.toExternalForm().contains("/rt.jar!")) {
   166                 LOG.log(Level.WARNING, "{0}No bootdelegation for ", name);
   167                 return null;
   168             }
   169             return u.openStream();
   170         }
   171     }
   172     
   173 }