Can load multiple resources of the same name even in testing and development mode
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.JavaScriptBody;
25 import org.apidesign.bck2brwsr.emul.zip.FastJar;
27 /** Conversion from classpath to load function.
29 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 private final FastJar fj;
34 private Zips(String path, byte[] zipData) throws IOException {
36 fj = new FastJar(zipData);
37 for (FastJar.Entry e : fj.list()) {
40 log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms");
43 public static void init() {
45 @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
46 private static native int length(Object arr);
47 @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
48 private static native Object at(Object arr, int index);
49 @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
50 private static native Object set(Object arr, int index, Object value);
52 public static byte[] loadFromCp(Object classpath, String res, int skip)
53 throws IOException, ClassNotFoundException {
54 for (int i = 0; i < length(classpath); i++) {
55 Object c = at(classpath, i);
56 if (c instanceof String) {
58 String url = (String)c;
59 final Zips z = toZip(url);
60 c = set(classpath, i, z);
61 final byte[] man = z.findRes("META-INF/MANIFEST.MF");
63 String mainClass = processClassPathAttr(man, url, classpath);
64 // if (mainClass != null) {
65 // Class.forName(mainClass);
68 } catch (IOException ex) {
69 set(classpath, i, ex);
70 log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
75 if (c instanceof Zips) {
76 checkRes = ((Zips)c).findRes(res);
78 checkRes = callFunction(c, res, skip);
81 if (checkRes != null && --skip < 0) {
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 byte[] findRes(String res) throws IOException {
99 Object arr = findResImpl(res);
100 if (arr instanceof FastJar.Entry) {
101 long bef = timeNow();
102 InputStream zip = fj.getInputStream((FastJar.Entry)arr);
103 arr = readFully(new byte[512], zip);
105 log("Reading " + res + " took " + (timeNow() - bef) + "ms");
110 @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
111 private native Object findResImpl(String res);
113 @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
114 private native void putRes(String res, Object arr);
116 private static Zips toZip(String path) throws IOException {
117 URL u = new URL(path);
118 byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class });
119 return new Zips(path, zipData);
122 private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
123 try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
124 String cp = is.toString();
127 for (int p = 0; p < cp.length();) {
128 int n = cp.indexOf(' ', p);
132 String el = cp.substring(p, n);
133 URL u = new URL(new URL(url), el);
134 classpath = addToArray(classpath, u.toString());
138 return is.getMainClass();
142 private static Object addToArray(Object arr, String value) {
143 final int last = length(arr);
144 Object ret = enlargeArray(arr, last + 1);
145 set(ret, last, value);
149 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
150 private static native Object enlargeArray(Object arr, int len);
151 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
152 private static native void enlargeBytes(byte[] arr, int len);
154 @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
155 private static native void sliceArray(byte[] arr, int len);
157 private static Object readFully(byte[] arr, InputStream zip) throws IOException {
160 int len = zip.read(arr, offset, arr.length - offset);
165 if (offset == arr.length) {
166 enlargeBytes(arr, arr.length + 4096);
169 sliceArray(arr, offset);
173 private static long timeNow() {
178 return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
180 @JavaScriptBody(args = {}, body =
181 "if (typeof window.performance === 'undefined') return -1;\n"
182 + "if (typeof window.performance.now === 'undefined') return -1;\n"
183 + "return window.performance.now();"
185 private static native double m();