1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/CompileCP.java Sat Apr 26 19:13:56 2014 +0200
1.3 @@ -0,0 +1,154 @@
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.launcher;
1.22 +
1.23 +import java.io.File;
1.24 +import java.io.IOException;
1.25 +import java.io.InputStream;
1.26 +import java.io.StringWriter;
1.27 +import java.net.URISyntaxException;
1.28 +import java.net.URL;
1.29 +import java.util.ArrayList;
1.30 +import java.util.Enumeration;
1.31 +import java.util.List;
1.32 +import java.util.jar.JarEntry;
1.33 +import java.util.jar.JarFile;
1.34 +import java.util.zip.ZipEntry;
1.35 +import org.apidesign.vm4brwsr.Bck2Brwsr;
1.36 +
1.37 +/**
1.38 + *
1.39 + * @author Jaroslav Tulach
1.40 + */
1.41 +class CompileCP {
1.42 + static String compileJAR(final JarFile jar) throws IOException {
1.43 + List<String> arr = new ArrayList<>();
1.44 + List<String> classes = new ArrayList<>();
1.45 + listJAR(jar, classes, arr);
1.46 + StringWriter w = new StringWriter();
1.47 + try {
1.48 + class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
1.49 + @Override
1.50 + public InputStream get(String resource) throws IOException {
1.51 + InputStream is = jar.getInputStream(new ZipEntry(resource));
1.52 + return is == null ? super.get(resource) : is;
1.53 + }
1.54 + }
1.55 +
1.56 + Bck2Brwsr.newCompiler()
1.57 + .addRootClasses(classes.toArray(new String[0]))
1.58 + .extension(true)
1.59 + .resources(new JarRes())
1.60 + .generate(w);
1.61 + w.flush();
1.62 + return w.toString();
1.63 + } catch (Throwable ex) {
1.64 + throw new IOException("Cannot compile: ", ex);
1.65 + } finally {
1.66 + w.close();
1.67 + }
1.68 + }
1.69 +
1.70 + static String compileFromClassPath(URL u) throws IOException, URISyntaxException {
1.71 + File f = new File(u.toURI());
1.72 + for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
1.73 + if (!f.getPath().startsWith(s)) {
1.74 + continue;
1.75 + }
1.76 + File root = new File(s);
1.77 + List<String> arr = new ArrayList<>();
1.78 + List<String> classes = new ArrayList<>();
1.79 + listDir(root, null, classes, arr);
1.80 + StringWriter w = new StringWriter();
1.81 + try {
1.82 + Bck2Brwsr.newCompiler()
1.83 + .addRootClasses(classes.toArray(new String[0]))
1.84 + .extension(true)
1.85 + .resources(new EmulationResources())
1.86 + .generate(w);
1.87 + w.flush();
1.88 + return w.toString();
1.89 + } catch (ClassFormatError ex) {
1.90 + throw new IOException(ex);
1.91 + } finally {
1.92 + w.close();
1.93 + }
1.94 + }
1.95 + return null;
1.96 + }
1.97 +
1.98 + private static void listJAR(JarFile j, List<String> classes, List<String> resources) throws IOException {
1.99 + Enumeration<JarEntry> en = j.entries();
1.100 + while (en.hasMoreElements()) {
1.101 + JarEntry e = en.nextElement();
1.102 + final String n = e.getName();
1.103 + if (n.endsWith("/")) {
1.104 + continue;
1.105 + }
1.106 + int last = n.lastIndexOf('/');
1.107 + String pkg = n.substring(0, last + 1);
1.108 + if (skipPkg(pkg)) {
1.109 + continue;
1.110 + }
1.111 + if (n.endsWith(".class")) {
1.112 + classes.add(n.substring(0, n.length() - 6));
1.113 + } else {
1.114 + resources.add(n);
1.115 + }
1.116 + }
1.117 + }
1.118 +
1.119 + private static boolean skipPkg(String pkg) {
1.120 + return pkg.equals("org/apidesign/bck2brwsr/launcher/");
1.121 + }
1.122 +
1.123 + private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
1.124 + File[] arr = f.listFiles();
1.125 + if (arr == null) {
1.126 + if (f.getName().endsWith(".class")) {
1.127 + classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
1.128 + } else {
1.129 + resources.add(pref + f.getName());
1.130 + }
1.131 + } else {
1.132 + for (File ch : arr) {
1.133 +
1.134 + listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
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 = CompileCP.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 + throw new IOException("Can't find " + name);
1.150 + }
1.151 + if (u.toExternalForm().contains("rt.jar!")) {
1.152 + throw new IOException("No emulation for " + u);
1.153 + }
1.154 + return u.openStream();
1.155 + }
1.156 + }
1.157 +}