launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 10 Mar 2015 20:35:20 +0100
changeset 1808 68c0229e193e
parent 1806 c186877cc85a
child 1809 72a0dbfa2ae8
permissions -rw-r--r--
More defensive type-checks.
     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             final URLConnection conn = u.openConnection();
   101             if (conn instanceof JarURLConnection) {
   102                 JarURLConnection juc = (JarURLConnection)conn;
   103                 rt = Bck2BrwsrJars.configureFrom(null, new File(juc.getJarFileURL().toURI()));
   104             } else {
   105                 throw new FileNotFoundException("Not a JAR URL: " + u);
   106             }
   107         } catch (URISyntaxException ex) {
   108             throw new IOException(ex);
   109         }
   110         final Bck2Brwsr all;
   111         try {
   112             URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
   113             JarURLConnection juc = (JarURLConnection)u.openConnection();
   114             all = Bck2BrwsrJars.configureFrom(rt, new File(juc.getJarFileURL().toURI()));
   115         } catch (URISyntaxException ex) {
   116             throw new IOException(ex);
   117         }
   118 
   119         all
   120             .standalone(true)
   121             //.obfuscation(ObfuscationLevel.FULL)
   122             .resources(new Bck2Brwsr.Resources() {
   123                 @Override
   124                 public InputStream get(String resource) throws IOException {
   125                     final URL url = r.get(resource, 0);
   126                     return url == null ? null : url.openStream();
   127                 }
   128             }).generate(sb);
   129     }
   130 }