rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 27 May 2014 12:25:41 +0200
branchclosure
changeset 1604 7665471a56c1
parent 1603 3ee8458a6ca9
child 1622 2c0e2a58a4f2
permissions -rw-r--r--
The static calculator demo needs to reference just a single application .js file from its main HTML page. The rest is loaded based on classpath attribute.
jaroslav@1599
     1
/**
jaroslav@1599
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1599
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1599
     4
 *
jaroslav@1599
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1599
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1599
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1599
     8
 *
jaroslav@1599
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1599
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1599
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1599
    12
 * GNU General Public License for more details.
jaroslav@1599
    13
 *
jaroslav@1599
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1599
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1599
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1599
    17
 */
jaroslav@1599
    18
package org.apidesign.bck2brwsr.aot;
jaroslav@1599
    19
jaroslav@1599
    20
import java.io.BufferedReader;
jaroslav@1599
    21
import java.io.File;
jaroslav@1599
    22
import java.io.IOException;
jaroslav@1599
    23
import java.io.InputStream;
jaroslav@1599
    24
import java.io.InputStreamReader;
jaroslav@1599
    25
import java.net.URL;
jaroslav@1599
    26
import java.util.ArrayList;
jaroslav@1599
    27
import java.util.Enumeration;
jaroslav@1599
    28
import java.util.HashSet;
jaroslav@1599
    29
import java.util.List;
jaroslav@1599
    30
import java.util.Set;
jaroslav@1599
    31
import java.util.jar.JarEntry;
jaroslav@1599
    32
import java.util.jar.JarFile;
jaroslav@1599
    33
import java.util.logging.Level;
jaroslav@1599
    34
import java.util.logging.Logger;
jaroslav@1599
    35
import java.util.zip.ZipEntry;
jaroslav@1599
    36
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@1599
    37
jaroslav@1599
    38
/** Utilities to process JAR files and set a compiler
jaroslav@1603
    39
 * up.
jaroslav@1599
    40
 *
jaroslav@1603
    41
 * @since 0.9
jaroslav@1599
    42
 * @author Jaroslav Tulach
jaroslav@1599
    43
 */
jaroslav@1599
    44
public final class Bck2BrwsrJars {
jaroslav@1599
    45
    private static final Logger LOG = Logger.getLogger(Bck2BrwsrJars.class.getName());
jaroslav@1599
    46
    
jaroslav@1599
    47
    private Bck2BrwsrJars() {
jaroslav@1599
    48
    }
jaroslav@1599
    49
    
jaroslav@1599
    50
    /** Creates new compiler pre-configured from the content of 
jaroslav@1599
    51
     * provided JAR file. The compiler will compile all classes.
jaroslav@1599
    52
     * The system understands OSGi manifest entries and will export
jaroslav@1599
    53
     * all packages that are exported in the JAR file. The system
jaroslav@1599
    54
     * also recognizes META-INF/services and makes sure the file names
jaroslav@1599
    55
     * are not mangled.
jaroslav@1599
    56
     * 
jaroslav@1599
    57
     * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
jaroslav@1599
    58
     *    {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
jaroslav@1599
    59
     *    {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
jaroslav@1599
    60
     *    Can be <code>null</code> - in such case an 
jaroslav@1599
    61
     *    {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
jaroslav@1599
    62
     * @param jar the file to process
jaroslav@1599
    63
     * @return newly configured compiler
jaroslav@1599
    64
     * @throws IOException if something goes wrong
jaroslav@1599
    65
     */
jaroslav@1599
    66
    public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
jaroslav@1601
    67
        final JarFile jf = new JarFile(jar);
jaroslav@1601
    68
        List<String> classes = new ArrayList<>();
jaroslav@1601
    69
        List<String> resources = new ArrayList<>();
jaroslav@1601
    70
        Set<String> exported = new HashSet<>();
jaroslav@1599
    71
jaroslav@1601
    72
        listJAR(jf, classes, resources, exported);
jaroslav@1604
    73
        
jaroslav@1604
    74
        String cp = jf.getManifest().getMainAttributes().getValue("Class-Path"); // NOI18N
jaroslav@1604
    75
        String[] classpath = cp == null ? new String[0] : cp.split(" ");
jaroslav@1601
    76
jaroslav@1601
    77
        class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
jaroslav@1601
    78
jaroslav@1601
    79
            @Override
jaroslav@1601
    80
            public InputStream get(String resource) throws IOException {
jaroslav@1601
    81
                InputStream is = jf.getInputStream(new ZipEntry(resource));
jaroslav@1601
    82
                return is == null ? super.get(resource) : is;
jaroslav@1599
    83
            }
jaroslav@1599
    84
        }
jaroslav@1601
    85
        return Bck2Brwsr.newCompiler()
jaroslav@1604
    86
            .library(classpath)
jaroslav@1601
    87
            .addClasses(classes.toArray(new String[classes.size()]))
jaroslav@1601
    88
            .addExported(exported.toArray(new String[exported.size()]))
jaroslav@1601
    89
            .addResources(resources.toArray(new String[resources.size()]))
jaroslav@1601
    90
            .resources(new JarRes());
jaroslav@1599
    91
    }
jaroslav@1599
    92
    
jaroslav@1599
    93
    private static void listJAR(
jaroslav@1599
    94
        JarFile j, List<String> classes,
jaroslav@1599
    95
        List<String> resources, Set<String> keep
jaroslav@1599
    96
    ) throws IOException {
jaroslav@1599
    97
        Enumeration<JarEntry> en = j.entries();
jaroslav@1599
    98
        while (en.hasMoreElements()) {
jaroslav@1599
    99
            JarEntry e = en.nextElement();
jaroslav@1599
   100
            final String n = e.getName();
jaroslav@1599
   101
            if (n.endsWith("/")) {
jaroslav@1599
   102
                continue;
jaroslav@1599
   103
            }
jaroslav@1602
   104
            if (n.startsWith("META-INF/maven/")) {
jaroslav@1602
   105
                continue;
jaroslav@1602
   106
            }
jaroslav@1599
   107
            int last = n.lastIndexOf('/');
jaroslav@1599
   108
            String pkg = n.substring(0, last + 1);
jaroslav@1599
   109
            if (pkg.startsWith("java/")) {
jaroslav@1599
   110
                keep.add(pkg);
jaroslav@1599
   111
            }
jaroslav@1599
   112
            if (n.endsWith(".class")) {
jaroslav@1599
   113
                classes.add(n.substring(0, n.length() - 6));
jaroslav@1599
   114
            } else {
jaroslav@1599
   115
                resources.add(n);
jaroslav@1599
   116
                if (n.startsWith("META-INF/services/") && keep != null) {
jaroslav@1599
   117
                    BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
jaroslav@1599
   118
                    for (;;) {
jaroslav@1599
   119
                        String l = r.readLine();
jaroslav@1599
   120
                        if (l == null) {
jaroslav@1599
   121
                            break;
jaroslav@1599
   122
                        }
jaroslav@1599
   123
                        if (l.startsWith("#")) {
jaroslav@1599
   124
                            continue;
jaroslav@1599
   125
                        }
jaroslav@1599
   126
                        keep.add(l.replace('.', '/'));
jaroslav@1599
   127
                    }
jaroslav@1599
   128
                }
jaroslav@1599
   129
            }
jaroslav@1599
   130
        }
jaroslav@1599
   131
        String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
jaroslav@1599
   132
        if (exp != null && keep != null) {
jaroslav@1599
   133
            for (String def : exp.split(",")) {
jaroslav@1599
   134
                for (String sep : def.split(";")) {
jaroslav@1599
   135
                    keep.add(sep.replace('.', '/') + "/");
jaroslav@1599
   136
                    break;
jaroslav@1599
   137
                }
jaroslav@1599
   138
            }
jaroslav@1599
   139
        }
jaroslav@1599
   140
    }
jaroslav@1599
   141
jaroslav@1599
   142
    static class EmulationResources implements Bck2Brwsr.Resources {
jaroslav@1599
   143
jaroslav@1599
   144
        @Override
jaroslav@1599
   145
        public InputStream get(String name) throws IOException {
jaroslav@1599
   146
            Enumeration<URL> en = Bck2BrwsrJars.class.getClassLoader().getResources(name);
jaroslav@1599
   147
            URL u = null;
jaroslav@1599
   148
            while (en.hasMoreElements()) {
jaroslav@1599
   149
                u = en.nextElement();
jaroslav@1599
   150
            }
jaroslav@1599
   151
            if (u == null) {
jaroslav@1599
   152
                LOG.log(Level.WARNING, "Cannot find {0}", name);
jaroslav@1599
   153
                return null;
jaroslav@1599
   154
            }
jaroslav@1599
   155
            if (u.toExternalForm().contains("/rt.jar!")) {
jaroslav@1599
   156
                LOG.log(Level.WARNING, "{0}No bootdelegation for ", name);
jaroslav@1599
   157
                return null;
jaroslav@1599
   158
            }
jaroslav@1599
   159
            return u.openStream();
jaroslav@1599
   160
        }
jaroslav@1599
   161
    }
jaroslav@1599
   162
    
jaroslav@1599
   163
}