launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 27 May 2014 14:52:43 +0200
branchclosure
changeset 1605 c74047c30b8f
parent 1604 7665471a56c1
child 1606 4e05b6eabbc5
permissions -rw-r--r--
Turning on standalone mode for the bck2brwsr.js
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@1599
    47
            Bck2BrwsrJars.configureFrom(null, jar).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@1489
    66
        for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
jaroslav@1489
    67
            if (!f.getPath().startsWith(s)) {
jaroslav@1489
    68
                continue;
jaroslav@1489
    69
            }
jaroslav@1489
    70
            File root = new File(s);
jaroslav@1489
    71
            List<String> arr = new ArrayList<>();
jaroslav@1489
    72
            List<String> classes = new ArrayList<>();
jaroslav@1489
    73
            listDir(root, null, classes, arr);
jaroslav@1489
    74
            StringWriter w = new StringWriter();
jaroslav@1489
    75
            try {
jaroslav@1489
    76
                Bck2Brwsr.newCompiler()
jaroslav@1489
    77
                    .addRootClasses(classes.toArray(new String[0]))
jaroslav@1498
    78
                    .addResources(arr.toArray(new String[0]))
jaroslav@1604
    79
                    .library()
jaroslav@1586
    80
                    //.obfuscation(ObfuscationLevel.FULL)
jaroslav@1504
    81
                    .resources(new EmulationResources() {
jaroslav@1504
    82
                        @Override
jaroslav@1504
    83
                        public InputStream get(String resource) throws IOException {
jaroslav@1517
    84
                            if (r != null) {
jaroslav@1517
    85
                                final URL url = r.get(resource, 0);
jaroslav@1517
    86
                                return url == null ? null : url.openStream();
jaroslav@1517
    87
                            }
jaroslav@1517
    88
                            return super.get(resource);
jaroslav@1504
    89
                        }
jaroslav@1504
    90
                    })
jaroslav@1489
    91
                    .generate(w);
jaroslav@1489
    92
                w.flush();
jaroslav@1489
    93
                return w.toString();
jaroslav@1489
    94
            } catch (ClassFormatError ex) {
jaroslav@1489
    95
                throw new IOException(ex);
jaroslav@1489
    96
            } finally {
jaroslav@1489
    97
                w.close();
jaroslav@1489
    98
            }
jaroslav@1489
    99
        }
jaroslav@1489
   100
        return null;
jaroslav@1489
   101
    }
jaroslav@1489
   102
    
jaroslav@1489
   103
jaroslav@1489
   104
    private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
jaroslav@1489
   105
        File[] arr = f.listFiles();
jaroslav@1489
   106
        if (arr == null) {
jaroslav@1489
   107
            if (f.getName().endsWith(".class")) {
jaroslav@1489
   108
                classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
jaroslav@1489
   109
            } else {
jaroslav@1489
   110
                resources.add(pref + f.getName());
jaroslav@1489
   111
            }
jaroslav@1489
   112
        } else {
jaroslav@1489
   113
            for (File ch : arr) {
jaroslav@1489
   114
                
jaroslav@1489
   115
                listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
jaroslav@1489
   116
            }
jaroslav@1489
   117
        }
jaroslav@1489
   118
    }
jaroslav@1489
   119
jaroslav@1504
   120
    static void compileVM(StringBuilder sb, final Res r) throws IOException {
jaroslav@1599
   121
        final Bck2Brwsr rt;
jaroslav@1599
   122
        try {
jaroslav@1552
   123
            URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
jaroslav@1552
   124
            JarURLConnection juc = (JarURLConnection)u.openConnection();
jaroslav@1599
   125
            rt = Bck2BrwsrJars.configureFrom(null, new File(juc.getJarFileURL().toURI()));
jaroslav@1599
   126
        } catch (URISyntaxException ex) {
jaroslav@1599
   127
            throw new IOException(ex);
jaroslav@1552
   128
        }
jaroslav@1599
   129
        final Bck2Brwsr all;
jaroslav@1599
   130
        try {
jaroslav@1552
   131
            URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
jaroslav@1552
   132
            JarURLConnection juc = (JarURLConnection)u.openConnection();
jaroslav@1599
   133
            all = Bck2BrwsrJars.configureFrom(rt, new File(juc.getJarFileURL().toURI()));
jaroslav@1599
   134
        } catch (URISyntaxException ex) {
jaroslav@1599
   135
            throw new IOException(ex);
jaroslav@1552
   136
        }
jaroslav@1492
   137
jaroslav@1604
   138
        all
jaroslav@1605
   139
            .standalone(true)
jaroslav@1586
   140
            //.obfuscation(ObfuscationLevel.FULL)
jaroslav@1504
   141
            .resources(new Bck2Brwsr.Resources() {
jaroslav@1504
   142
                @Override
jaroslav@1504
   143
                public InputStream get(String resource) throws IOException {
jaroslav@1523
   144
                    final URL url = r.get(resource, 0);
jaroslav@1523
   145
                    return url == null ? null : url.openStream();
jaroslav@1504
   146
                }
jaroslav@1504
   147
            }).generate(sb);
jaroslav@1492
   148
    }
jaroslav@1492
   149
jaroslav@1489
   150
    static class EmulationResources implements Bck2Brwsr.Resources {
jaroslav@1489
   151
jaroslav@1489
   152
        @Override
jaroslav@1489
   153
        public InputStream get(String name) throws IOException {
jaroslav@1599
   154
            Enumeration<URL> en = Bck2BrwsrJars.class.getClassLoader().getResources(name);
jaroslav@1489
   155
            URL u = null;
jaroslav@1489
   156
            while (en.hasMoreElements()) {
jaroslav@1489
   157
                u = en.nextElement();
jaroslav@1489
   158
            }
jaroslav@1489
   159
            if (u == null) {
jaroslav@1523
   160
                LOG.log(Level.WARNING, "Cannot find {0}", name);
jaroslav@1523
   161
                return null;
jaroslav@1489
   162
            }
jaroslav@1523
   163
            if (u.toExternalForm().contains("/rt.jar!")) {
jaroslav@1599
   164
                LOG.log(Level.WARNING, "{0}No bootdelegation for ", name);
jaroslav@1523
   165
                return null;
jaroslav@1489
   166
            }
jaroslav@1489
   167
            return u.openStream();
jaroslav@1489
   168
        }
jaroslav@1489
   169
    }
jaroslav@1599
   170
    
jaroslav@1489
   171
}