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]))
57 .addResources(arr.toArray(new String[0]))
59 .resources(new JarRes())
63 } catch (Throwable ex) {
64 throw new IOException("Cannot compile: ", ex);
70 static String compileFromClassPath(URL u, final Res r) throws IOException, URISyntaxException {
71 File f = new File(u.toURI());
72 for (String s : System.getProperty("java.class.path").split(File.pathSeparator)) {
73 if (!f.getPath().startsWith(s)) {
76 File root = new File(s);
77 List<String> arr = new ArrayList<>();
78 List<String> classes = new ArrayList<>();
79 listDir(root, null, classes, arr);
80 StringWriter w = new StringWriter();
82 Bck2Brwsr.newCompiler()
83 .addRootClasses(classes.toArray(new String[0]))
84 .addResources(arr.toArray(new String[0]))
86 .resources(new EmulationResources() {
88 public InputStream get(String resource) throws IOException {
89 return r != null ? r.get(resource).openStream() : super.get(resource);
95 } catch (ClassFormatError ex) {
96 throw new IOException(ex);
104 private static void listJAR(JarFile j, List<String> classes, List<String> resources) throws IOException {
105 Enumeration<JarEntry> en = j.entries();
106 while (en.hasMoreElements()) {
107 JarEntry e = en.nextElement();
108 final String n = e.getName();
109 if (n.contains("package-info")) {
112 if (n.endsWith("/")) {
115 int last = n.lastIndexOf('/');
116 String pkg = n.substring(0, last + 1);
120 if (n.endsWith(".class")) {
121 classes.add(n.substring(0, n.length() - 6));
128 private static boolean skipPkg(String pkg) {
129 return pkg.equals("org/apidesign/bck2brwsr/launcher/");
132 private static void listDir(File f, String pref, List<String> classes, List<String> resources) throws IOException {
133 File[] arr = f.listFiles();
135 if (f.getName().equals("package-info.class")) {
138 if (f.getName().endsWith(".class")) {
139 classes.add(pref + f.getName().substring(0, f.getName().length() - 6));
141 resources.add(pref + f.getName());
144 for (File ch : arr) {
146 listDir(ch, pref == null ? "" : pref + f.getName() + "/", classes, resources);
151 static void compileVM(StringBuilder sb, final Res r) throws IOException {
152 URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class");
153 JarURLConnection juc = (JarURLConnection)u.openConnection();
155 List<String> arr = new ArrayList<>();
156 List<String> classes = new ArrayList<>();
157 listJAR(juc.getJarFile(), classes, arr);
159 Bck2Brwsr.newCompiler().addRootClasses(classes.toArray(new String[0]))
160 .resources(new Bck2Brwsr.Resources() {
162 public InputStream get(String resource) throws IOException {
163 return r.get(resource).openStream();
168 static class EmulationResources implements Bck2Brwsr.Resources {
171 public InputStream get(String name) throws IOException {
172 Enumeration<URL> en = CompileCP.class.getClassLoader().getResources(name);
174 while (en.hasMoreElements()) {
175 u = en.nextElement();
178 throw new IOException("Can't find " + name);
180 if (u.toExternalForm().contains("rt.jar!")) {
181 throw new IOException("No emulation for " + u);
183 return u.openStream();