More defensive type-checks.
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 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.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.StringWriter;
25 import java.net.JarURLConnection;
26 import java.net.URISyntaxException;
28 import java.net.URLConnection;
30 import java.util.logging.Logger;
31 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
32 import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res;
33 import org.apidesign.vm4brwsr.Bck2Brwsr;
37 * @author Jaroslav Tulach
40 private static final Logger LOG = Logger.getLogger(CompileCP.class.getName());
41 static String compileJAR(final File jar, Set<String> testClasses)
43 StringWriter w = new StringWriter();
45 Bck2BrwsrJars.configureFrom(null, jar)
46 .addExported(testClasses.toArray(new String[0]))
50 } catch (IOException ex) {
52 } catch (Throwable ex) {
53 throw new IOException("Cannot compile: ", ex);
59 static String compileFromClassPath(URL u, final Res r) throws IOException {
62 f = new File(u.toURI());
63 } catch (URISyntaxException ex) {
64 throw new IOException(ex);
66 String s = f.isDirectory() ? f.getPath() : null;
68 for (String candidate : System.getProperty("java.class.path").split(File.pathSeparator)) {
72 if (f.getPath().startsWith(candidate)) {
77 File root = new File(s);
78 StringWriter w = new StringWriter();
80 Bck2BrwsrJars.configureFrom(null, root)
84 } catch (ClassFormatError ex) {
85 throw new IOException(ex);
93 static void compileVM(StringBuilder sb, final Res r) throws IOException {
96 URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0);
98 throw new IOException("Cannot find InterruptedException class on classpath: " + System.getProperty("java.class.path"));
100 final URLConnection conn = u.openConnection();
101 if (conn instanceof JarURLConnection) {
102 JarURLConnection juc = (JarURLConnection)conn;
103 rt = Bck2BrwsrJars.configureFrom(null, new File(juc.getJarFileURL().toURI()));
105 throw new FileNotFoundException("Not a JAR URL: " + u);
107 } catch (URISyntaxException ex) {
108 throw new IOException(ex);
112 URL u = r.get(Bck2Brwsr.class.getName().replace('.', '/') + ".class", 0);
113 JarURLConnection juc = (JarURLConnection)u.openConnection();
114 all = Bck2BrwsrJars.configureFrom(rt, new File(juc.getJarFileURL().toURI()));
115 } catch (URISyntaxException ex) {
116 throw new IOException(ex);
121 //.obfuscation(ObfuscationLevel.FULL)
122 .resources(new Bck2Brwsr.Resources() {
124 public InputStream get(String resource) throws IOException {
125 final URL url = r.get(resource, 0);
126 return url == null ? null : url.openStream();