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.vm4brwsr;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import org.apidesign.bck2brwsr.core.Exported;
25 import org.apidesign.bck2brwsr.core.JavaScriptBody;
27 /** Conversion from classpath to load function.
29 * @author Jaroslav Tulach <jtulach@netbeans.org>
31 final class ClassPath {
35 public static void init() {
37 @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
38 private static native int length(Object arr);
39 @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
40 private static native Object at(Object arr, int index);
41 @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
42 private static native Object set(Object arr, int index, Object value);
44 private static boolean doingToZip;
47 @Exported static byte[] loadBytes(String resource, Object classpath, int skip)
48 throws IOException, ClassNotFoundException {
49 for (int i = 0; i < length(classpath); i++) {
50 Object c = at(classpath, i);
51 if (c instanceof String && !doingToZip) {
54 String url = (String)c;
55 final Bck2Brwsr.Resources z = toZip(url);
56 c = set(classpath, i, z);
57 final byte[] man = readBytes(z, "META-INF/MANIFEST.MF");
59 String mainClass = processClassPathAttr(man, url, classpath);
60 // if (mainClass != null) {
61 // Class.forName(mainClass);
64 } catch (IOException ex) {
65 set(classpath, i, ex);
66 log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
71 if (resource != null) {
73 if (c instanceof Bck2Brwsr.Resources) {
74 checkRes = readBytes((Bck2Brwsr.Resources)c, resource);
75 if (checkRes != null && --skip < 0) {
79 checkRes = callFunction(c, resource, skip);
80 if (checkRes != null) {
89 @JavaScriptBody(args = { "fn", "res", "skip" }, body =
90 "if (typeof fn === 'function') return fn(res, skip);\n"
93 private static native byte[] callFunction(Object fn, String res, int skip);
95 @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
96 private static native void log(String msg);
98 private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
99 try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
100 String cp = is.toString();
103 for (int p = 0; p < cp.length();) {
104 int n = cp.indexOf(' ', p);
108 String el = cp.substring(p, n);
109 URL u = new URL(new URL(url), el);
110 classpath = addToArray(classpath, u.toString());
114 return is.getMainClass();
118 private static Object addToArray(Object arr, String value) {
119 final int last = length(arr);
120 Object ret = enlargeArray(arr, last + 1);
121 set(ret, last, value);
125 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
126 private static native Object enlargeArray(Object arr, int len);
128 private static Bck2Brwsr.Resources toZip(String path) throws IOException {
129 URL u = new URL(path);
130 byte[] zipData = (byte[]) u.getContent(new Class[]{byte[].class});
131 Bck2Brwsr.Resources r;
133 Class<?> fastJar = Class.forName("org.apidesign.bck2brwsr.vmzip.ZipResources");
134 return (Bck2Brwsr.Resources) fastJar.getConstructor(byte[].class).newInstance(zipData);
135 } catch (Exception ex) {
136 log("Reading JARs is only possible with enum.zip module included: " + ex.getMessage());
137 ex.printStackTrace();
138 throw new IOException(ex);
142 private static byte[] readBytes(Bck2Brwsr.Resources r, String res) throws IOException {
143 InputStream is = r.get(res);
147 byte[] arr = new byte[is.available()];
150 int len = is.read(arr, off, arr.length - off);