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;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.StringWriter;
24 import java.net.JarURLConnection;
25 import java.net.URISyntaxException;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.List;
30 import java.util.jar.JarEntry;
31 import java.util.jar.JarFile;
32 import java.util.zip.ZipEntry;
33 import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res;
34 import org.apidesign.vm4brwsr.Bck2Brwsr;
38 * @author Jaroslav Tulach
41 static String compileJAR(final JarFile jar) throws IOException {
42 List<String> arr = new ArrayList<>();
43 List<String> classes = new ArrayList<>();
44 listJAR(jar, classes, arr);
45 StringWriter w = new StringWriter();
47 class JarRes extends EmulationResources implements Bck2Brwsr.Resources {
49 public InputStream get(String resource) throws IOException {
50 InputStream is = jar.getInputStream(new ZipEntry(resource));
51 return is == null ? super.get(resource) : is;
55 Bck2Brwsr.newCompiler()
56 .addClasses(classes.toArray(new String[0]))
58 .resources(new JarRes())
62 } catch (Throwable ex) {
63 throw new IOException("Cannot compile: ", ex);
69 static String compileFromClassPath(URL u) throws IOException, URISyntaxException {
70 File f = new File(u.toURI());
71 for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
72 if (!f.getPath().startsWith(s)) {
75 File root = new File(s);
76 List<String> arr = new ArrayList<>();
77 List<String> classes = new ArrayList<>();
78 listDir(root, null, classes, arr);
79 StringWriter w = new StringWriter();
81 Bck2Brwsr.newCompiler()
82 .addRootClasses(classes.toArray(new String[0]))
84 .resources(new EmulationResources())
88 } catch (ClassFormatError ex) {
89 throw new IOException(ex);
97 private static void listJAR(JarFile j, List<String> classes, List<String> resources) throws IOException {
98 Enumeration<JarEntry> en = j.entries();
99 while (en.hasMoreElements()) {
100 JarEntry e = en.nextElement();
101 final String n = e.getName();
102 if (n.contains("package-info")) {
105 if (n.endsWith("/")) {
108 int last = n.lastIndexOf('/');
109 String pkg = n.substring(0, last + 1);
113 if (n.endsWith(".class")) {
114 classes.add(n.substring(0, n.length() - 6));
121 private static boolean skipPkg(String pkg) {
122 return pkg.equals("org/apidesign/bck2brwsr/launcher/");
125 private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
126 File[] arr = f.listFiles();
128 if (f.getName().endsWith(".class")) {
129 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
131 resources.add(pref + f.getName());
134 for (File ch : arr) {
136 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
141 static void compileVM(StringBuilder sb, Res r) throws IOException {
142 URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class");
143 JarURLConnection juc = (JarURLConnection)u.openConnection();
145 List<String> arr = new ArrayList<>();
146 List<String> classes = new ArrayList<>();
147 listJAR(juc.getJarFile(), classes, arr);
149 Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
150 .resources(new EmulationResources())
154 static class EmulationResources implements Bck2Brwsr.Resources {
157 public InputStream get(String name) throws IOException {
158 Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
160 while (en.hasMoreElements()) {
161 u = en.nextElement();
164 throw new IOException("Can't find " + name);
166 if (u.toExternalForm().contains("rt.jar!")) {
167 throw new IOException("No emulation for " + u);
169 return u.openStream();