2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.launcher;
20 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.StringWriter;
26 import java.net.JarURLConnection;
27 import java.net.URISyntaxException;
29 import java.util.ArrayList;
30 import java.util.Enumeration;
31 import java.util.HashSet;
32 import java.util.List;
34 import java.util.jar.JarEntry;
35 import java.util.jar.JarFile;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38 import java.util.zip.ZipEntry;
39 import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res;
40 import org.apidesign.vm4brwsr.Bck2Brwsr;
44 * @author Jaroslav Tulach
47 private static final Logger LOG = Logger.getLogger(CompileCP.class.getName());
48 static String compileJAR(final JarFile jar, Set<String> testClasses)
50 List<String> arr = new ArrayList<>();
51 List<String> classes = new ArrayList<>();
52 Set<String> exported = new HashSet<String>();
53 Set<String> keep = new HashSet<String>(testClasses);
54 listJAR(jar, classes, arr, exported, keep);
55 List<String> root = new ArrayList<>();
56 for (String c : classes) {
57 if (keep.contains(c)) {
61 int slash = c.lastIndexOf('/');
62 String pkg = c.substring(0, slash + 1);
63 if (exported.contains(pkg)) {
68 StringWriter w = new StringWriter();
70 class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
72 public InputStream get(String resource) throws IOException {
73 InputStream is = jar.getInputStream(new ZipEntry(resource));
74 return is == null ? super.get(resource) : is;
78 Bck2Brwsr.newCompiler()
79 .addClasses(classes.toArray(new String[0]))
80 .addRootClasses(root.toArray(new String[0]))
81 .addResources(arr.toArray(new String[0]))
83 .resources(new JarRes())
87 } catch (IOException ex) {
89 } catch (Throwable ex) {
90 throw new IOException("Cannot compile: ", ex);
96 static String compileFromClassPath(URL u, final Res r) throws IOException {
99 f = new File(u.toURI());
100 } catch (URISyntaxException ex) {
101 throw new IOException(ex);
103 for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
104 if (!f.getPath().startsWith(s)) {
107 File root = new File(s);
108 List<String> arr = new ArrayList<>();
109 List<String> classes = new ArrayList<>();
110 listDir(root, null, classes, arr);
111 StringWriter w = new StringWriter();
113 Bck2Brwsr.newCompiler()
114 .addRootClasses(classes.toArray(new String[0]))
115 .addResources(arr.toArray(new String[0]))
117 .resources(new EmulationResources() {
119 public InputStream get(String resource) throws IOException {
121 final URL url = r.get(resource, 0);
122 return url == null ? null : url.openStream();
124 return super.get(resource);
130 } catch (ClassFormatError ex) {
131 throw new IOException(ex);
139 private static void listJAR(
140 JarFile j, List<String> classes,
141 List<String> resources, Set<String> exported, Set<String> keep
142 ) throws IOException {
143 Enumeration<JarEntry> en = j.entries();
144 while (en.hasMoreElements()) {
145 JarEntry e = en.nextElement();
146 final String n = e.getName();
147 if (n.endsWith("/")) {
150 int last = n.lastIndexOf('/');
151 String pkg = n.substring(0, last + 1);
155 if (n.endsWith(".class")) {
156 classes.add(n.substring(0, n.length() - 6));
159 if (n.startsWith("META-INF/services/") && keep != null) {
160 BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
162 String l = r.readLine();
166 if (l.startsWith("#")) {
169 keep.add(l.replace('.', '/'));
174 String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
175 if (exp != null && exported != null) {
176 for (String def : exp.split(",")) {
177 for (String sep : def.split(";")) {
178 exported.add(sep.replace('.', '/') + "/");
185 private static boolean skipPkg(String pkg) {
186 return pkg.equals("org/apidesign/bck2brwsr/launcher/");
189 private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
190 File[] arr = f.listFiles();
192 if (f.getName().endsWith(".class")) {
193 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
195 resources.add(pref + f.getName());
198 for (File ch : arr) {
200 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
205 static void compileVM(StringBuilder sb, final Res r) throws IOException {
206 List<String> arr = new ArrayList<>();
207 List<String> classes = new ArrayList<>();
210 URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
211 JarURLConnection juc = (JarURLConnection)u.openConnection();
212 listJAR(juc.getJarFile(), classes, arr, null, null);
215 URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
216 JarURLConnection juc = (JarURLConnection)u.openConnection();
217 listJAR(juc.getJarFile(), classes, arr, null, null);
220 Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
221 .resources(new Bck2Brwsr.Resources() {
223 public InputStream get(String resource) throws IOException {
224 final URL url = r.get(resource, 0);
225 return url == null ? null : url.openStream();
230 static class EmulationResources implements Bck2Brwsr.Resources {
233 public InputStream get(String name) throws IOException {
234 Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
236 while (en.hasMoreElements()) {
237 u = en.nextElement();
240 LOG.log(Level.WARNING, "Cannot find {0}", name);
243 if (u.toExternalForm().contains("/rt.jar!")) {
244 LOG.warning(name + "No bootdelegation for ");
247 return u.openStream();