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