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;
41 import org.apidesign.vm4brwsr.ObfuscationLevel;
45 * @author Jaroslav Tulach
48 private static final Logger LOG = Logger.getLogger(CompileCP.class.getName());
49 static String compileJAR(final JarFile jar, Set<String> testClasses)
51 List<String> arr = new ArrayList<>();
52 List<String> classes = new ArrayList<>();
53 Set<String> keep = new HashSet<String>(testClasses);
54 listJAR(jar, classes, arr, keep);
56 StringWriter w = new StringWriter();
58 class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
60 public InputStream get(String resource) throws IOException {
61 InputStream is = jar.getInputStream(new ZipEntry(resource));
62 return is == null ? super.get(resource) : is;
66 Bck2Brwsr.newCompiler()
67 .addClasses(classes.toArray(new String[0]))
68 .addExported(keep.toArray(new String[0]))
69 .addResources(arr.toArray(new String[0]))
70 //.obfuscation(ObfuscationLevel.FULL)
72 .resources(new JarRes())
76 } catch (IOException ex) {
78 } catch (Throwable ex) {
79 throw new IOException("Cannot compile: ", ex);
85 static String compileFromClassPath(URL u, final Res r) throws IOException {
88 f = new File(u.toURI());
89 } catch (URISyntaxException ex) {
90 throw new IOException(ex);
92 for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
93 if (!f.getPath().startsWith(s)) {
96 File root = new File(s);
97 List<String> arr = new ArrayList<>();
98 List<String> classes = new ArrayList<>();
99 listDir(root, null, classes, arr);
100 StringWriter w = new StringWriter();
102 Bck2Brwsr.newCompiler()
103 .addRootClasses(classes.toArray(new String[0]))
104 .addResources(arr.toArray(new String[0]))
106 //.obfuscation(ObfuscationLevel.FULL)
107 .resources(new EmulationResources() {
109 public InputStream get(String resource) throws IOException {
111 final URL url = r.get(resource, 0);
112 return url == null ? null : url.openStream();
114 return super.get(resource);
120 } catch (ClassFormatError ex) {
121 throw new IOException(ex);
129 private static void listJAR(
130 JarFile j, List<String> classes,
131 List<String> resources, Set<String> keep
132 ) throws IOException {
133 Enumeration<JarEntry> en = j.entries();
134 while (en.hasMoreElements()) {
135 JarEntry e = en.nextElement();
136 final String n = e.getName();
137 if (n.endsWith("/")) {
140 int last = n.lastIndexOf('/');
141 String pkg = n.substring(0, last + 1);
145 if (n.endsWith(".class")) {
146 classes.add(n.substring(0, n.length() - 6));
149 if (n.startsWith("META-INF/services/") && keep != null) {
150 BufferedReader r = new BufferedReader(new InputStreamReader(j.getInputStream(e)));
152 String l = r.readLine();
156 if (l.startsWith("#")) {
159 keep.add(l.replace('.', '/'));
164 String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
165 if (exp != null && keep != null) {
166 for (String def : exp.split(",")) {
167 for (String sep : def.split(";")) {
168 keep.add(sep.replace('.', '/') + "/");
175 private static boolean skipPkg(String pkg) {
176 return pkg.equals("org/apidesign/bck2brwsr/launcher/");
179 private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
180 File[] arr = f.listFiles();
182 if (f.getName().endsWith(".class")) {
183 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
185 resources.add(pref + f.getName());
188 for (File ch : arr) {
190 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
195 static void compileVM(StringBuilder sb, final Res r) throws IOException {
196 List<String> arr = new ArrayList<>();
197 List<String> classes = new ArrayList<>();
200 URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
201 JarURLConnection juc = (JarURLConnection)u.openConnection();
202 listJAR(juc.getJarFile(), classes, arr, null);
205 URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
206 JarURLConnection juc = (JarURLConnection)u.openConnection();
207 listJAR(juc.getJarFile(), classes, arr, null);
210 Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
211 //.obfuscation(ObfuscationLevel.FULL)
212 .resources(new Bck2Brwsr.Resources() {
214 public InputStream get(String resource) throws IOException {
215 final URL url = r.get(resource, 0);
216 return url == null ? null : url.openStream();
221 static class EmulationResources implements Bck2Brwsr.Resources {
224 public InputStream get(String name) throws IOException {
225 Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
227 while (en.hasMoreElements()) {
228 u = en.nextElement();
231 LOG.log(Level.WARNING, "Cannot find {0}", name);
234 if (u.toExternalForm().contains("/rt.jar!")) {
235 LOG.warning(name + "No bootdelegation for ");
238 return u.openStream();