launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Feb 2015 11:12:53 +0100
changeset 1787 ea12a3bb4b33
parent 1747 58fd5ea7ad4e
child 1806 c186877cc85a
permissions -rw-r--r--
Using year range 2012-2015 in copyright header
     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.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.Set;
    28 import java.util.logging.Logger;
    29 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
    30 import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res;
    31 import org.apidesign.vm4brwsr.Bck2Brwsr;
    32 
    33 /**
    34  *
    35  * @author Jaroslav Tulach
    36  */
    37 class CompileCP {
    38     private static final Logger LOG = Logger.getLogger(CompileCP.class.getName());
    39     static String compileJAR(final File jar, Set<String> testClasses) 
    40     throws IOException {
    41         StringWriter w = new StringWriter();
    42         try {
    43             Bck2BrwsrJars.configureFrom(null, jar)
    44                 .addExported(testClasses.toArray(new String[0]))
    45                 .generate(w);
    46             w.flush();
    47             return w.toString();
    48         } catch (IOException ex) {
    49             throw ex;
    50         } catch (Throwable ex) {
    51             throw new IOException("Cannot compile: ", ex);
    52         } finally {
    53             w.close();
    54         }
    55     }
    56     
    57     static String compileFromClassPath(URL u, final Res r) throws IOException {
    58         File f;
    59         try {
    60             f = new File(u.toURI());
    61         } catch (URISyntaxException ex) {
    62             throw new IOException(ex);
    63         }
    64         String s = f.isDirectory() ? f.getPath() : null;
    65         
    66         for (String candidate : System.getProperty("java.class.path").split(File.pathSeparator)) {
    67             if (s != null) {
    68                 break;
    69             }
    70             if (f.getPath().startsWith(candidate)) {
    71                 s = candidate;
    72             }
    73         }
    74         if (s != null) {
    75             File root = new File(s);
    76             StringWriter w = new StringWriter();
    77             try {
    78                 Bck2BrwsrJars.configureFrom(null, root)
    79                     .generate(w);
    80                 w.flush();
    81                 return w.toString();
    82             } catch (ClassFormatError ex) {
    83                 throw new IOException(ex);
    84             } finally {
    85                 w.close();
    86             }
    87         }
    88         return null;
    89     }
    90     
    91     static void compileVM(StringBuilder sb, final Res r) throws IOException {
    92         final Bck2Brwsr rt;
    93         try {
    94             URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
    95             JarURLConnection juc = (JarURLConnection)u.openConnection();
    96             rt = Bck2BrwsrJars.configureFrom(null, new File(juc.getJarFileURL().toURI()));
    97         } catch (URISyntaxException ex) {
    98             throw new IOException(ex);
    99         }
   100         final Bck2Brwsr all;
   101         try {
   102             URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
   103             JarURLConnection juc = (JarURLConnection)u.openConnection();
   104             all = Bck2BrwsrJars.configureFrom(rt, new File(juc.getJarFileURL().toURI()));
   105         } catch (URISyntaxException ex) {
   106             throw new IOException(ex);
   107         }
   108 
   109         all
   110             .standalone(true)
   111             //.obfuscation(ObfuscationLevel.FULL)
   112             .resources(new Bck2Brwsr.Resources() {
   113                 @Override
   114                 public InputStream get(String resource) throws IOException {
   115                     final URL url = r.get(resource, 0);
   116                     return url == null ? null : url.openStream();
   117                 }
   118             }).generate(sb);
   119     }
   120 }