launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 13 Sep 2014 14:19:43 +0200
branchjdk8
changeset 1679 93f4fbc4d1b7
parent 1614 8eba262bd8cd
child 1747 58fd5ea7ad4e
permissions -rw-r--r--
Support processing of lambda also in directories
jaroslav@1489
     1
/**
jaroslav@1489
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1489
     3
 * Copyright (C) 2012 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@1489
    21
import java.io.IOException;
jaroslav@1489
    22
import java.io.InputStream;
jaroslav@1489
    23
import java.io.StringWriter;
jaroslav@1492
    24
import java.net.JarURLConnection;
jaroslav@1489
    25
import java.net.URISyntaxException;
jaroslav@1489
    26
import java.net.URL;
jaroslav@1489
    27
import java.util.ArrayList;
jaroslav@1489
    28
import java.util.Enumeration;
jaroslav@1489
    29
import java.util.List;
jaroslav@1525
    30
import java.util.Set;
jaroslav@1505
    31
import java.util.logging.Level;
jaroslav@1505
    32
import java.util.logging.Logger;
jaroslav@1599
    33
import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
jaroslav@1492
    34
import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res;
jaroslav@1489
    35
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@1489
    36
jaroslav@1489
    37
/**
jaroslav@1489
    38
 *
jaroslav@1489
    39
 * @author Jaroslav Tulach
jaroslav@1489
    40
 */
jaroslav@1489
    41
class CompileCP {
jaroslav@1523
    42
    private static final Logger LOG = Logger.getLogger(CompileCP.class.getName());
jaroslav@1599
    43
    static String compileJAR(final File jar, Set<String> testClasses) 
jaroslav@1525
    44
    throws IOException {
jaroslav@1489
    45
        StringWriter w = new StringWriter();
jaroslav@1489
    46
        try {
jaroslav@1606
    47
            Bck2BrwsrJars.configureFrom(null, jar)
jaroslav@1606
    48
                .addExported(testClasses.toArray(new String[0]))
jaroslav@1606
    49
                .generate(w);
jaroslav@1489
    50
            w.flush();
jaroslav@1489
    51
            return w.toString();
jaroslav@1523
    52
        } catch (IOException ex) {
jaroslav@1523
    53
            throw ex;
jaroslav@1489
    54
        } catch (Throwable ex) {
jaroslav@1489
    55
            throw new IOException("Cannot compile: ", ex);
jaroslav@1489
    56
        } finally {
jaroslav@1489
    57
            w.close();
jaroslav@1489
    58
        }
jaroslav@1489
    59
    }
jaroslav@1489
    60
    
jaroslav@1505
    61
    static String compileFromClassPath(URL u, final Res r) throws IOException {
jaroslav@1505
    62
        File f;
jaroslav@1505
    63
        try {
jaroslav@1505
    64
            f = new File(u.toURI());
jaroslav@1505
    65
        } catch (URISyntaxException ex) {
jaroslav@1505
    66
            throw new IOException(ex);
jaroslav@1505
    67
        }
jaroslav@1614
    68
        String s = f.isDirectory() ? f.getPath() : null;
jaroslav@1614
    69
        
jaroslav@1614
    70
        for (String candidate : System.getProperty("java.class.path").split(File.pathSeparator)) {
jaroslav@1614
    71
            if (s != null) {
jaroslav@1614
    72
                break;
jaroslav@1489
    73
            }
jaroslav@1614
    74
            if (f.getPath().startsWith(candidate)) {
jaroslav@1614
    75
                s = candidate;
jaroslav@1614
    76
            }
jaroslav@1614
    77
        }
jaroslav@1614
    78
        if (s != null) {
jaroslav@1489
    79
            File root = new File(s);
jaroslav@1489
    80
            StringWriter w = new StringWriter();
jaroslav@1489
    81
            try {
jaroslav@1679
    82
                Bck2BrwsrJars.configureFrom(null, root)
jaroslav@1489
    83
                    .generate(w);
jaroslav@1489
    84
                w.flush();
jaroslav@1489
    85
                return w.toString();
jaroslav@1489
    86
            } catch (ClassFormatError ex) {
jaroslav@1489
    87
                throw new IOException(ex);
jaroslav@1489
    88
            } finally {
jaroslav@1489
    89
                w.close();
jaroslav@1489
    90
            }
jaroslav@1489
    91
        }
jaroslav@1489
    92
        return null;
jaroslav@1489
    93
    }
jaroslav@1489
    94
    
jaroslav@1504
    95
    static void compileVM(StringBuilder sb, final Res r) throws IOException {
jaroslav@1599
    96
        final Bck2Brwsr rt;
jaroslav@1599
    97
        try {
jaroslav@1552
    98
            URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
jaroslav@1552
    99
            JarURLConnection juc = (JarURLConnection)u.openConnection();
jaroslav@1599
   100
            rt = Bck2BrwsrJars.configureFrom(null, new File(juc.getJarFileURL().toURI()));
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@1552
   107
            JarURLConnection juc = (JarURLConnection)u.openConnection();
jaroslav@1599
   108
            all = Bck2BrwsrJars.configureFrom(rt, new File(juc.getJarFileURL().toURI()));
jaroslav@1599
   109
        } catch (URISyntaxException ex) {
jaroslav@1599
   110
            throw new IOException(ex);
jaroslav@1552
   111
        }
jaroslav@1492
   112
jaroslav@1604
   113
        all
jaroslav@1605
   114
            .standalone(true)
jaroslav@1586
   115
            //.obfuscation(ObfuscationLevel.FULL)
jaroslav@1504
   116
            .resources(new Bck2Brwsr.Resources() {
jaroslav@1504
   117
                @Override
jaroslav@1504
   118
                public InputStream get(String resource) throws IOException {
jaroslav@1523
   119
                    final URL url = r.get(resource, 0);
jaroslav@1523
   120
                    return url == null ? null : url.openStream();
jaroslav@1504
   121
                }
jaroslav@1504
   122
            }).generate(sb);
jaroslav@1492
   123
    }
jaroslav@1489
   124
}