launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 11 Mar 2015 08:50:51 +0100
changeset 1809 72a0dbfa2ae8
parent 1808 68c0229e193e
child 1942 f8cc2a015ece
permissions -rw-r--r--
Can run from unpackaged directories. E.g. mvn clean install -DskipTests; mvn test works now on JDK8.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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.FileNotFoundException;
    22 import java.io.IOException;
    23 import java.io.InputStream;
    24 import java.io.StringWriter;
    25 import java.net.JarURLConnection;
    26 import java.net.URISyntaxException;
    27 import java.net.URL;
    28 import java.net.URLConnection;
    29 import java.util.Set;
    30 import java.util.logging.Logger;
    31 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
    32 import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res;
    33 import org.apidesign.vm4brwsr.Bck2Brwsr;
    34 
    35 /**
    36  *
    37  * @author Jaroslav Tulach
    38  */
    39 class CompileCP {
    40     private static final Logger LOG = Logger.getLogger(CompileCP.class.getName());
    41     static String compileJAR(final File jar, Set<String> testClasses) 
    42     throws IOException {
    43         StringWriter w = new StringWriter();
    44         try {
    45             Bck2BrwsrJars.configureFrom(null, jar)
    46                 .addExported(testClasses.toArray(new String[0]))
    47                 .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         String s = f.isDirectory() ? f.getPath() : null;
    67         
    68         for (String candidate : System.getProperty("java.class.path").split(File.pathSeparator)) {
    69             if (s != null) {
    70                 break;
    71             }
    72             if (f.getPath().startsWith(candidate)) {
    73                 s = candidate;
    74             }
    75         }
    76         if (s != null) {
    77             File root = new File(s);
    78             StringWriter w = new StringWriter();
    79             try {
    80                 Bck2BrwsrJars.configureFrom(null, root)
    81                     .generate(w);
    82                 w.flush();
    83                 return w.toString();
    84             } catch (ClassFormatError ex) {
    85                 throw new IOException(ex);
    86             } finally {
    87                 w.close();
    88             }
    89         }
    90         return null;
    91     }
    92     
    93     static void compileVM(StringBuilder sb, final Res r) throws IOException {
    94         final Bck2Brwsr rt;
    95         try {
    96             URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
    97             if (u == null) {
    98                 throw new IOException("Cannot find InterruptedException class on classpath: " + System.getProperty("java.class.path"));
    99             }
   100             rt = configureFrom(u, null, 3);
   101         } catch (URISyntaxException ex) {
   102             throw new IOException(ex);
   103         }
   104         final Bck2Brwsr all;
   105         try {
   106             URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
   107             all = configureFrom(u, rt, 4);
   108         } catch (URISyntaxException ex) {
   109             throw new IOException(ex);
   110         }
   111 
   112         all
   113             .standalone(true)
   114             //.obfuscation(ObfuscationLevel.FULL)
   115             .resources(new Bck2Brwsr.Resources() {
   116                 @Override
   117                 public InputStream get(String resource) throws IOException {
   118                     final URL url = r.get(resource, 0);
   119                     return url == null ? null : url.openStream();
   120                 }
   121             }).generate(sb);
   122     }
   123 
   124     static Bck2Brwsr configureFrom(URL u, Bck2Brwsr rt, int parents) throws IOException, URISyntaxException {
   125         final URLConnection conn = u.openConnection();
   126         if (conn instanceof JarURLConnection) {
   127             JarURLConnection juc = (JarURLConnection)conn;
   128             rt = Bck2BrwsrJars.configureFrom(null, new File(juc.getJarFileURL().toURI()));
   129         } else if (u.getProtocol().equals("file")) {
   130             File dir = new File(u.toURI());
   131             while (parents-- > 0) {
   132                 dir = dir.getParentFile();
   133             }
   134             rt = Bck2BrwsrJars.configureFrom(null, dir);
   135         } else {
   136             throw new FileNotFoundException("Not a JAR or file URL: " + u);
   137         }
   138         return rt;
   139     }
   140 }