launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1942 f8cc2a015ece
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     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.core.ExtraJavaScript;
    33 import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res;
    34 import org.apidesign.vm4brwsr.Bck2Brwsr;
    35 
    36 /**
    37  *
    38  * @author Jaroslav Tulach
    39  */
    40 @ExtraJavaScript(processByteCode = false, resource="")
    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             StringWriter w = new StringWriter();
    81             try {
    82                 Bck2BrwsrJars.configureFrom(null, root)
    83                     .generate(w);
    84                 w.flush();
    85                 return w.toString();
    86             } catch (ClassFormatError ex) {
    87                 throw new IOException(ex);
    88             } finally {
    89                 w.close();
    90             }
    91         }
    92         return null;
    93     }
    94     
    95     static void compileVM(StringBuilder sb, final Res r) throws IOException {
    96         final Bck2Brwsr rt;
    97         try {
    98             URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
    99             if (u == null) {
   100                 throw new IOException("Cannot find InterruptedException class on classpath: " + System.getProperty("java.class.path"));
   101             }
   102             rt = configureFrom(u, null, 3);
   103         } catch (URISyntaxException ex) {
   104             throw new IOException(ex);
   105         }
   106         final Bck2Brwsr all = Bck2Brwsr.newCompiler()
   107             .standalone(false)
   108             //.obfuscation(ObfuscationLevel.FULL)
   109             .resources(new Bck2Brwsr.Resources() {
   110                 @Override
   111                 public InputStream get(String resource) throws IOException {
   112                     return null;
   113                 }
   114             });
   115         all.generate(sb);
   116     }
   117 
   118     static Bck2Brwsr configureFrom(URL u, Bck2Brwsr rt, int parents) throws IOException, URISyntaxException {
   119         final URLConnection conn = u.openConnection();
   120         if (conn instanceof JarURLConnection) {
   121             JarURLConnection juc = (JarURLConnection)conn;
   122             rt = Bck2BrwsrJars.configureFrom(null, new File(juc.getJarFileURL().toURI()));
   123         } else if (u.getProtocol().equals("file")) {
   124             File dir = new File(u.toURI());
   125             while (parents-- > 0) {
   126                 dir = dir.getParentFile();
   127             }
   128             rt = Bck2BrwsrJars.configureFrom(null, dir);
   129         } else {
   130             throw new FileNotFoundException("Not a JAR or file URL: " + u);
   131         }
   132         return rt;
   133     }
   134 }