jaroslav@1489: /** jaroslav@1489: * Back 2 Browser Bytecode Translator jaroslav@1787: * Copyright (C) 2012-2015 Jaroslav Tulach jaroslav@1489: * jaroslav@1489: * This program is free software: you can redistribute it and/or modify jaroslav@1489: * it under the terms of the GNU General Public License as published by jaroslav@1489: * the Free Software Foundation, version 2 of the License. jaroslav@1489: * jaroslav@1489: * This program is distributed in the hope that it will be useful, jaroslav@1489: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@1489: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@1489: * GNU General Public License for more details. jaroslav@1489: * jaroslav@1489: * You should have received a copy of the GNU General Public License jaroslav@1489: * along with this program. Look for COPYING file in the top folder. jaroslav@1489: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@1489: */ jaroslav@1489: package org.apidesign.bck2brwsr.launcher; jaroslav@1489: jaroslav@1489: import java.io.File; jaroslav@1808: import java.io.FileNotFoundException; jaroslav@1489: import java.io.IOException; jaroslav@1489: import java.io.InputStream; jaroslav@1489: import java.io.StringWriter; jaroslav@1492: import java.net.JarURLConnection; jaroslav@1489: import java.net.URISyntaxException; jaroslav@1489: import java.net.URL; jaroslav@1808: import java.net.URLConnection; jaroslav@1525: import java.util.Set; jaroslav@1505: import java.util.logging.Logger; jaroslav@1599: import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars; jaroslav@1957: import org.apidesign.bck2brwsr.core.ExtraJavaScript; jaroslav@1492: import org.apidesign.bck2brwsr.launcher.BaseHTTPLauncher.Res; jaroslav@1489: import org.apidesign.vm4brwsr.Bck2Brwsr; jaroslav@1489: jaroslav@1489: /** jaroslav@1489: * jaroslav@1489: * @author Jaroslav Tulach jaroslav@1489: */ jaroslav@1957: @ExtraJavaScript(processByteCode = false, resource="") jaroslav@1489: class CompileCP { jaroslav@1523: private static final Logger LOG = Logger.getLogger(CompileCP.class.getName()); jaroslav@1599: static String compileJAR(final File jar, Set testClasses) jaroslav@1525: throws IOException { jaroslav@1489: StringWriter w = new StringWriter(); jaroslav@1489: try { jaroslav@1606: Bck2BrwsrJars.configureFrom(null, jar) jaroslav@1606: .addExported(testClasses.toArray(new String[0])) jaroslav@1606: .generate(w); jaroslav@1489: w.flush(); jaroslav@1489: return w.toString(); jaroslav@1523: } catch (IOException ex) { jaroslav@1523: throw ex; jaroslav@1489: } catch (Throwable ex) { jaroslav@1489: throw new IOException("Cannot compile: ", ex); jaroslav@1489: } finally { jaroslav@1489: w.close(); jaroslav@1489: } jaroslav@1489: } jaroslav@1489: jaroslav@1505: static String compileFromClassPath(URL u, final Res r) throws IOException { jaroslav@1505: File f; jaroslav@1505: try { jaroslav@1505: f = new File(u.toURI()); jaroslav@1505: } catch (URISyntaxException ex) { jaroslav@1505: throw new IOException(ex); jaroslav@1505: } jaroslav@1614: String s = f.isDirectory() ? f.getPath() : null; jaroslav@1614: jaroslav@1614: for (String candidate : System.getProperty("java.class.path").split(File.pathSeparator)) { jaroslav@1614: if (s != null) { jaroslav@1614: break; jaroslav@1489: } jaroslav@1614: if (f.getPath().startsWith(candidate)) { jaroslav@1614: s = candidate; jaroslav@1614: } jaroslav@1614: } jaroslav@1614: if (s != null) { jaroslav@1489: File root = new File(s); jaroslav@1489: StringWriter w = new StringWriter(); jaroslav@1489: try { jaroslav@1679: Bck2BrwsrJars.configureFrom(null, root) jaroslav@1489: .generate(w); jaroslav@1489: w.flush(); jaroslav@1489: return w.toString(); jaroslav@1489: } catch (ClassFormatError ex) { jaroslav@1489: throw new IOException(ex); jaroslav@1489: } finally { jaroslav@1489: w.close(); jaroslav@1489: } jaroslav@1489: } jaroslav@1489: return null; jaroslav@1489: } jaroslav@1489: jaroslav@1504: static void compileVM(StringBuilder sb, final Res r) throws IOException { jaroslav@1599: final Bck2Brwsr rt; jaroslav@1599: try { jaroslav@1552: URL u = r.get(InterruptedException.class.getName().replace('.', '/') + ".class", 0); jaroslav@1806: if (u == null) { jaroslav@1806: throw new IOException("Cannot find InterruptedException class on classpath: " + System.getProperty("java.class.path")); jaroslav@1806: } jaroslav@1809: rt = configureFrom(u, null, 3); jaroslav@1599: } catch (URISyntaxException ex) { jaroslav@1599: throw new IOException(ex); jaroslav@1552: } jaroslav@1942: final Bck2Brwsr all = Bck2Brwsr.newCompiler() jaroslav@1942: .standalone(false) jaroslav@1586: //.obfuscation(ObfuscationLevel.FULL) jaroslav@1504: .resources(new Bck2Brwsr.Resources() { jaroslav@1504: @Override jaroslav@1504: public InputStream get(String resource) throws IOException { jaroslav@1942: return null; jaroslav@1504: } jaroslav@1942: }); jaroslav@1942: all.generate(sb); jaroslav@1492: } jaroslav@1809: jaroslav@1809: static Bck2Brwsr configureFrom(URL u, Bck2Brwsr rt, int parents) throws IOException, URISyntaxException { jaroslav@1809: final URLConnection conn = u.openConnection(); jaroslav@1809: if (conn instanceof JarURLConnection) { jaroslav@1809: JarURLConnection juc = (JarURLConnection)conn; jaroslav@1809: rt = Bck2BrwsrJars.configureFrom(null, new File(juc.getJarFileURL().toURI())); jaroslav@1809: } else if (u.getProtocol().equals("file")) { jaroslav@1809: File dir = new File(u.toURI()); jaroslav@1809: while (parents-- > 0) { jaroslav@1809: dir = dir.getParentFile(); jaroslav@1809: } jaroslav@1809: rt = Bck2BrwsrJars.configureFrom(null, dir); jaroslav@1809: } else { jaroslav@1809: throw new FileNotFoundException("Not a JAR or file URL: " + u); jaroslav@1809: } jaroslav@1809: return rt; jaroslav@1809: } jaroslav@1489: }