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.
jaroslav@1489
     1
/**
jaroslav@1489
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1489
     4
 *
jaroslav@1489
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1489
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1489
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1489
     8
 *
jaroslav@1489
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1489
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1489
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1489
    12
 * GNU General Public License for more details.
jaroslav@1489
    13
 *
jaroslav@1489
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1489
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1489
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1489
    17
 */
jaroslav@1489
    18
package org.apidesign.bck2brwsr.launcher;
jaroslav@1489
    19
jaroslav@1489
    20
import java.io.File;
jaroslav@1808
    21
import java.io.FileNotFoundException;
jaroslav@1489
    22
import java.io.IOException;
jaroslav@1489
    23
import java.io.InputStream;
jaroslav@1489
    24
import java.io.StringWriter;
jaroslav@1492
    25
import java.net.JarURLConnection;
jaroslav@1489
    26
import java.net.URISyntaxException;
jaroslav@1489
    27
import java.net.URL;
jaroslav@1808
    28
import java.net.URLConnection;
jaroslav@1525
    29
import java.util.Set;
jaroslav@1505
    30
import java.util.logging.Logger;
jaroslav@1599
    31
import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
jaroslav@1492
    32
import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res;
jaroslav@1489
    33
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@1489
    34
jaroslav@1489
    35
/**
jaroslav@1489
    36
 *
jaroslav@1489
    37
 * @author Jaroslav Tulach
jaroslav@1489
    38
 */
jaroslav@1489
    39
class CompileCP {
jaroslav@1523
    40
    private static final Logger LOG = Logger.getLogger(CompileCP.class.getName());
jaroslav@1599
    41
    static String compileJAR(final File jar, Set<String> testClasses) 
jaroslav@1525
    42
    throws IOException {
jaroslav@1489
    43
        StringWriter w = new StringWriter();
jaroslav@1489
    44
        try {
jaroslav@1606
    45
            Bck2BrwsrJars.configureFrom(null, jar)
jaroslav@1606
    46
                .addExported(testClasses.toArray(new String[0]))
jaroslav@1606
    47
                .generate(w);
jaroslav@1489
    48
            w.flush();
jaroslav@1489
    49
            return w.toString();
jaroslav@1523
    50
        } catch (IOException ex) {
jaroslav@1523
    51
            throw ex;
jaroslav@1489
    52
        } catch (Throwable ex) {
jaroslav@1489
    53
            throw new IOException("Cannot compile: ", ex);
jaroslav@1489
    54
        } finally {
jaroslav@1489
    55
            w.close();
jaroslav@1489
    56
        }
jaroslav@1489
    57
    }
jaroslav@1489
    58
    
jaroslav@1505
    59
    static String compileFromClassPath(URL u, final Res r) throws IOException {
jaroslav@1505
    60
        File f;
jaroslav@1505
    61
        try {
jaroslav@1505
    62
            f = new File(u.toURI());
jaroslav@1505
    63
        } catch (URISyntaxException ex) {
jaroslav@1505
    64
            throw new IOException(ex);
jaroslav@1505
    65
        }
jaroslav@1614
    66
        String s = f.isDirectory() ? f.getPath() : null;
jaroslav@1614
    67
        
jaroslav@1614
    68
        for (String candidate : System.getProperty("java.class.path").split(File.pathSeparator)) {
jaroslav@1614
    69
            if (s != null) {
jaroslav@1614
    70
                break;
jaroslav@1489
    71
            }
jaroslav@1614
    72
            if (f.getPath().startsWith(candidate)) {
jaroslav@1614
    73
                s = candidate;
jaroslav@1614
    74
            }
jaroslav@1614
    75
        }
jaroslav@1614
    76
        if (s != null) {
jaroslav@1489
    77
            File root = new File(s);
jaroslav@1489
    78
            StringWriter w = new StringWriter();
jaroslav@1489
    79
            try {
jaroslav@1679
    80
                Bck2BrwsrJars.configureFrom(null, root)
jaroslav@1489
    81
                    .generate(w);
jaroslav@1489
    82
                w.flush();
jaroslav@1489
    83
                return w.toString();
jaroslav@1489
    84
            } catch (ClassFormatError ex) {
jaroslav@1489
    85
                throw new IOException(ex);
jaroslav@1489
    86
            } finally {
jaroslav@1489
    87
                w.close();
jaroslav@1489
    88
            }
jaroslav@1489
    89
        }
jaroslav@1489
    90
        return null;
jaroslav@1489
    91
    }
jaroslav@1489
    92
    
jaroslav@1504
    93
    static void compileVM(StringBuilder sb, final Res r) throws IOException {
jaroslav@1599
    94
        final Bck2Brwsr rt;
jaroslav@1599
    95
        try {
jaroslav@1552
    96
            URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
jaroslav@1806
    97
            if (u == null) {
jaroslav@1806
    98
                throw new IOException("Cannot find InterruptedException class on classpath: " + System.getProperty("java.class.path"));
jaroslav@1806
    99
            }
jaroslav@1809
   100
            rt = configureFrom(u, null, 3);
jaroslav@1599
   101
        } catch (URISyntaxException ex) {
jaroslav@1599
   102
            throw new IOException(ex);
jaroslav@1552
   103
        }
jaroslav@1599
   104
        final Bck2Brwsr all;
jaroslav@1599
   105
        try {
jaroslav@1552
   106
            URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
jaroslav@1809
   107
            all = configureFrom(u, rt, 4);
jaroslav@1599
   108
        } catch (URISyntaxException ex) {
jaroslav@1599
   109
            throw new IOException(ex);
jaroslav@1552
   110
        }
jaroslav@1492
   111
jaroslav@1604
   112
        all
jaroslav@1605
   113
            .standalone(true)
jaroslav@1586
   114
            //.obfuscation(ObfuscationLevel.FULL)
jaroslav@1504
   115
            .resources(new Bck2Brwsr.Resources() {
jaroslav@1504
   116
                @Override
jaroslav@1504
   117
                public InputStream get(String resource) throws IOException {
jaroslav@1523
   118
                    final URL url = r.get(resource, 0);
jaroslav@1523
   119
                    return url == null ? null : url.openStream();
jaroslav@1504
   120
                }
jaroslav@1504
   121
            }).generate(sb);
jaroslav@1492
   122
    }
jaroslav@1809
   123
jaroslav@1809
   124
    static Bck2Brwsr configureFrom(URL u, Bck2Brwsr rt, int parents) throws IOException, URISyntaxException {
jaroslav@1809
   125
        final URLConnection conn = u.openConnection();
jaroslav@1809
   126
        if (conn instanceof JarURLConnection) {
jaroslav@1809
   127
            JarURLConnection juc = (JarURLConnection)conn;
jaroslav@1809
   128
            rt = Bck2BrwsrJars.configureFrom(null, new File(juc.getJarFileURL().toURI()));
jaroslav@1809
   129
        } else if (u.getProtocol().equals("file")) {
jaroslav@1809
   130
            File dir = new File(u.toURI());
jaroslav@1809
   131
            while (parents-- > 0) {
jaroslav@1809
   132
                dir = dir.getParentFile();
jaroslav@1809
   133
            }
jaroslav@1809
   134
            rt = Bck2BrwsrJars.configureFrom(null, dir);
jaroslav@1809
   135
        } else {
jaroslav@1809
   136
            throw new FileNotFoundException("Not a JAR or file URL: " + u);
jaroslav@1809
   137
        }
jaroslav@1809
   138
        return rt;
jaroslav@1809
   139
    }
jaroslav@1489
   140
}