1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java Mon May 26 17:52:56 2014 +0200
1.3 @@ -0,0 +1,157 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.aot;
1.22 +
1.23 +import java.io.BufferedReader;
1.24 +import java.io.File;
1.25 +import java.io.IOException;
1.26 +import java.io.InputStream;
1.27 +import java.io.InputStreamReader;
1.28 +import java.net.URL;
1.29 +import java.util.ArrayList;
1.30 +import java.util.Enumeration;
1.31 +import java.util.HashSet;
1.32 +import java.util.List;
1.33 +import java.util.Set;
1.34 +import java.util.jar.JarEntry;
1.35 +import java.util.jar.JarFile;
1.36 +import java.util.logging.Level;
1.37 +import java.util.logging.Logger;
1.38 +import java.util.zip.ZipEntry;
1.39 +import org.apidesign.vm4brwsr.Bck2Brwsr;
1.40 +
1.41 +/** Utilities to process JAR files and set a compiler
1.42 + * up
1.43 + *
1.44 + * @author Jaroslav Tulach
1.45 + */
1.46 +public final class Bck2BrwsrJars {
1.47 + private static final Logger LOG = Logger.getLogger(Bck2BrwsrJars.class.getName());
1.48 +
1.49 + private Bck2BrwsrJars() {
1.50 + }
1.51 +
1.52 + /** Creates new compiler pre-configured from the content of
1.53 + * provided JAR file. The compiler will compile all classes.
1.54 + * The system understands OSGi manifest entries and will export
1.55 + * all packages that are exported in the JAR file. The system
1.56 + * also recognizes META-INF/services and makes sure the file names
1.57 + * are not mangled.
1.58 + *
1.59 + * @param c the compiler to {@link Bck2Brwsr#addClasses(java.lang.String...) add classes},
1.60 + * {@link Bck2Brwsr#addResources(java.lang.String...) add resources} and
1.61 + * {@link Bck2Brwsr#addExported(java.lang.String...) exported objects} to.
1.62 + * Can be <code>null</code> - in such case an
1.63 + * {@link Bck2Brwsr#newCompiler() empty compiler} is constructed.
1.64 + * @param jar the file to process
1.65 + * @return newly configured compiler
1.66 + * @throws IOException if something goes wrong
1.67 + */
1.68 + public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
1.69 + try (JarFile jf = new JarFile(jar)) {
1.70 + List<String> classes = new ArrayList<>();
1.71 + List<String> resources = new ArrayList<>();
1.72 + Set<String> exported = new HashSet<>();
1.73 +
1.74 + listJAR(jf, classes, resources, exported);
1.75 +
1.76 + class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
1.77 +
1.78 + @Override
1.79 + public InputStream get(String resource) throws IOException {
1.80 + InputStream is = jf.getInputStream(new ZipEntry(resource));
1.81 + return is == null ? super.get(resource) : is;
1.82 + }
1.83 + }
1.84 + return Bck2Brwsr.newCompiler()
1.85 + .library(true)
1.86 + .addClasses(classes.toArray(new String[classes.size()]))
1.87 + .addExported(exported.toArray(new String[exported.size()]))
1.88 + .addResources(resources.toArray(new String[resources.size()]))
1.89 + .resources(new JarRes());
1.90 + }
1.91 + }
1.92 +
1.93 + private static void listJAR(
1.94 + JarFile j, List<String> classes,
1.95 + List<String> resources, Set<String> keep
1.96 + ) throws IOException {
1.97 + Enumeration<JarEntry> en = j.entries();
1.98 + while (en.hasMoreElements()) {
1.99 + JarEntry e = en.nextElement();
1.100 + final String n = e.getName();
1.101 + if (n.endsWith("/")) {
1.102 + continue;
1.103 + }
1.104 + int last = n.lastIndexOf('/');
1.105 + String pkg = n.substring(0, last + 1);
1.106 + if (pkg.startsWith("java/")) {
1.107 + keep.add(pkg);
1.108 + }
1.109 + if (n.endsWith(".class")) {
1.110 + classes.add(n.substring(0, n.length() - 6));
1.111 + } else {
1.112 + resources.add(n);
1.113 + if (n.startsWith("META-INF/services/") && keep != null) {
1.114 + BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
1.115 + for (;;) {
1.116 + String l = r.readLine();
1.117 + if (l == null) {
1.118 + break;
1.119 + }
1.120 + if (l.startsWith("#")) {
1.121 + continue;
1.122 + }
1.123 + keep.add(l.replace('.', '/'));
1.124 + }
1.125 + }
1.126 + }
1.127 + }
1.128 + String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
1.129 + if (exp != null && keep != null) {
1.130 + for (String def : exp.split(",")) {
1.131 + for (String sep : def.split(";")) {
1.132 + keep.add(sep.replace('.', '/') + "/");
1.133 + break;
1.134 + }
1.135 + }
1.136 + }
1.137 + }
1.138 +
1.139 + static class EmulationResources implements Bck2Brwsr.Resources {
1.140 +
1.141 + @Override
1.142 + public InputStream get(String name) throws IOException {
1.143 + Enumeration<URL> en = Bck2BrwsrJars.class.getClassLoader().getResources(name);
1.144 + URL u = null;
1.145 + while (en.hasMoreElements()) {
1.146 + u = en.nextElement();
1.147 + }
1.148 + if (u == null) {
1.149 + LOG.log(Level.WARNING, "Cannot find {0}", name);
1.150 + return null;
1.151 + }
1.152 + if (u.toExternalForm().contains("/rt.jar!")) {
1.153 + LOG.log(Level.WARNING, "{0}No bootdelegation for ", name);
1.154 + return null;
1.155 + }
1.156 + return u.openStream();
1.157 + }
1.158 + }
1.159 +
1.160 +}