launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 03 Jun 2014 16:05:21 +0200
branchclosure
changeset 1614 8eba262bd8cd
parent 1606 4e05b6eabbc5
child 1679 93f4fbc4d1b7
permissions -rw-r--r--
Directly derive the parent root folder based on resource name rather than going through classpath
     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         String s = f.isDirectory() ? f.getPath() : null;
    69         
    70         for (String candidate : System.getProperty("java.class.path").split(File.pathSeparator)) {
    71             if (s != null) {
    72                 break;
    73             }
    74             if (f.getPath().startsWith(candidate)) {
    75                 s = candidate;
    76             }
    77         }
    78         if (s != null) {
    79             File root = new File(s);
    80             List<String> arr = new ArrayList<>();
    81             List<String> classes = new ArrayList<>();
    82             listDir(root, null, classes, arr);
    83             StringWriter w = new StringWriter();
    84             try {
    85                 Bck2Brwsr.newCompiler()
    86                     .addRootClasses(classes.toArray(new String[0]))
    87                     .addResources(arr.toArray(new String[0]))
    88                     .library()
    89                     //.obfuscation(ObfuscationLevel.FULL)
    90                     .resources(new EmulationResources() {
    91                         @Override
    92                         public InputStream get(String resource) throws IOException {
    93                             if (r != null) {
    94                                 final URL url = r.get(resource, 0);
    95                                 return url == null ? null : url.openStream();
    96                             }
    97                             return super.get(resource);
    98                         }
    99                     })
   100                     .generate(w);
   101                 w.flush();
   102                 return w.toString();
   103             } catch (ClassFormatError ex) {
   104                 throw new IOException(ex);
   105             } finally {
   106                 w.close();
   107             }
   108         }
   109         return null;
   110     }
   111     
   112 
   113     private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
   114         File[] arr = f.listFiles();
   115         if (arr == null) {
   116             if (f.getName().endsWith(".class")) {
   117                 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
   118             } else {
   119                 resources.add(pref + f.getName());
   120             }
   121         } else {
   122             for (File ch : arr) {
   123                 
   124                 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
   125             }
   126         }
   127     }
   128 
   129     static void compileVM(StringBuilder sb, final Res r) throws IOException {
   130         final Bck2Brwsr rt;
   131         try {
   132             URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
   133             JarURLConnection juc = (JarURLConnection)u.openConnection();
   134             rt = Bck2BrwsrJars.configureFrom(null, new File(juc.getJarFileURL().toURI()));
   135         } catch (URISyntaxException ex) {
   136             throw new IOException(ex);
   137         }
   138         final Bck2Brwsr all;
   139         try {
   140             URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
   141             JarURLConnection juc = (JarURLConnection)u.openConnection();
   142             all = Bck2BrwsrJars.configureFrom(rt, new File(juc.getJarFileURL().toURI()));
   143         } catch (URISyntaxException ex) {
   144             throw new IOException(ex);
   145         }
   146 
   147         all
   148             .standalone(true)
   149             //.obfuscation(ObfuscationLevel.FULL)
   150             .resources(new Bck2Brwsr.Resources() {
   151                 @Override
   152                 public InputStream get(String resource) throws IOException {
   153                     final URL url = r.get(resource, 0);
   154                     return url == null ? null : url.openStream();
   155                 }
   156             }).generate(sb);
   157     }
   158 
   159     static class EmulationResources implements Bck2Brwsr.Resources {
   160 
   161         @Override
   162         public InputStream get(String name) throws IOException {
   163             Enumeration<URL> en = Bck2BrwsrJars.class.getClassLoader().getResources(name);
   164             URL u = null;
   165             while (en.hasMoreElements()) {
   166                 u = en.nextElement();
   167             }
   168             if (u == null) {
   169                 LOG.log(Level.WARNING, "Cannot find {0}", name);
   170                 return null;
   171             }
   172             if (u.toExternalForm().contains("/rt.jar!")) {
   173                 LOG.log(Level.WARNING, "{0}No bootdelegation for ", name);
   174                 return null;
   175             }
   176             return u.openStream();
   177         }
   178     }
   179     
   180 }