rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 09 May 2014 08:37:20 +0200
branchclosure
changeset 1551 0002739b6d1f
parent 1550 cb9e273dfd51
child 1554 7ba27baf5f3f
permissions -rw-r--r--
Moving Zip tests into right locations
jaroslav@644
     1
/**
jaroslav@644
     2
 * Back 2 Browser Bytecode Translator
jaroslav@644
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@644
     4
 *
jaroslav@644
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@644
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@644
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@644
     8
 *
jaroslav@644
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@644
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@644
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@644
    12
 * GNU General Public License for more details.
jaroslav@644
    13
 *
jaroslav@644
    14
 * You should have received a copy of the GNU General Public License
jaroslav@644
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@644
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@644
    17
 */
jaroslav@644
    18
package org.apidesign.vm4brwsr;
jaroslav@644
    19
jaroslav@672
    20
import java.io.ByteArrayInputStream;
jaroslav@644
    21
import java.io.IOException;
jaroslav@1551
    22
import java.io.InputStream;
jaroslav@644
    23
import java.net.URL;
jaroslav@644
    24
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@644
    25
jaroslav@644
    26
/** Conversion from classpath to load function.
jaroslav@644
    27
 *
jaroslav@644
    28
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@644
    29
 */
jaroslav@1550
    30
final class ClassPath {
jaroslav@1550
    31
    private ClassPath() {
jaroslav@644
    32
    }
jaroslav@644
    33
    
jaroslav@644
    34
    public static void init() {
jaroslav@644
    35
    }
jaroslav@729
    36
    @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
jaroslav@729
    37
    private static native int length(Object arr);
jaroslav@729
    38
    @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
jaroslav@729
    39
    private static native Object at(Object arr, int index);
jaroslav@729
    40
    @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
jaroslav@729
    41
    private static native Object set(Object arr, int index, Object value);
jaroslav@644
    42
    
jaroslav@1375
    43
    public static byte[] loadFromCp(Object classpath, String res, int skip) 
jaroslav@729
    44
    throws IOException, ClassNotFoundException {
jaroslav@729
    45
        for (int i = 0; i < length(classpath); i++) {
jaroslav@729
    46
            Object c = at(classpath, i);
jaroslav@644
    47
            if (c instanceof String) {
jaroslav@644
    48
                try {
jaroslav@672
    49
                    String url = (String)c;
jaroslav@1551
    50
                    final Bck2Brwsr.Resources z = toZip(url);
jaroslav@729
    51
                    c = set(classpath, i, z);
jaroslav@1551
    52
                    final byte[] man = readBytes(z, "META-INF/MANIFEST.MF");
jaroslav@672
    53
                    if (man != null) {
jaroslav@702
    54
                        String mainClass = processClassPathAttr(man, url, classpath);
jaroslav@743
    55
//                        if (mainClass != null) {
jaroslav@743
    56
//                            Class.forName(mainClass);
jaroslav@743
    57
//                        }
jaroslav@672
    58
                    }
jaroslav@743
    59
                } catch (IOException ex) {
jaroslav@729
    60
                    set(classpath, i, ex);
jaroslav@746
    61
                    log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
jaroslav@644
    62
                }
jaroslav@644
    63
            }
jaroslav@729
    64
            if (res != null) {
jaroslav@729
    65
                byte[] checkRes;
jaroslav@1551
    66
                if (c instanceof Bck2Brwsr.Resources) {
jaroslav@1551
    67
                    checkRes = readBytes((Bck2Brwsr.Resources)c, res);
jaroslav@1387
    68
                    if (checkRes != null && --skip < 0) {
jaroslav@1387
    69
                        return checkRes;
jaroslav@1387
    70
                    }
jaroslav@729
    71
                } else {
jaroslav@1375
    72
                    checkRes = callFunction(c, res, skip);
jaroslav@1387
    73
                    if (checkRes != null) {
jaroslav@1387
    74
                        return checkRes;
jaroslav@1387
    75
                    }
jaroslav@644
    76
                }
jaroslav@644
    77
            }
jaroslav@644
    78
        }
jaroslav@644
    79
        return null;
jaroslav@644
    80
    }
jaroslav@706
    81
    
jaroslav@1375
    82
    @JavaScriptBody(args = { "fn", "res", "skip" }, body = 
jaroslav@1375
    83
        "if (typeof fn === 'function') return fn(res, skip);\n"
jaroslav@729
    84
      + "return null;"
jaroslav@729
    85
    )
jaroslav@1375
    86
    private static native byte[] callFunction(Object fn, String res, int skip);
jaroslav@729
    87
    
jaroslav@1358
    88
    @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
jaroslav@706
    89
    private static native void log(String msg);
jaroslav@706
    90
jaroslav@729
    91
    private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
jaroslav@702
    92
        try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
jaroslav@672
    93
            String cp = is.toString();
jaroslav@702
    94
            if (cp != null) {
jaroslav@702
    95
                cp = cp.trim();
jaroslav@702
    96
                for (int p = 0; p < cp.length();) {
jaroslav@702
    97
                    int n = cp.indexOf(' ', p);
jaroslav@702
    98
                    if (n == -1) {
jaroslav@702
    99
                        n = cp.length();
jaroslav@702
   100
                    }
jaroslav@702
   101
                    String el = cp.substring(p, n);
jaroslav@702
   102
                    URL u = new URL(new URL(url), el);
jaroslav@702
   103
                    classpath = addToArray(classpath, u.toString());
jaroslav@702
   104
                    p = n + 1;
jaroslav@702
   105
                }
jaroslav@672
   106
            }
jaroslav@702
   107
            return is.getMainClass();
jaroslav@672
   108
        }
jaroslav@672
   109
    }
jaroslav@672
   110
jaroslav@729
   111
    private static Object addToArray(Object arr, String value) {
jaroslav@729
   112
        final int last = length(arr);
jaroslav@729
   113
        Object ret = enlargeArray(arr, last + 1);
jaroslav@729
   114
        set(ret, last, value);
jaroslav@672
   115
        return ret;
jaroslav@672
   116
    }
jaroslav@672
   117
jaroslav@729
   118
    @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
jaroslav@729
   119
    private static native Object enlargeArray(Object arr, int len);
jaroslav@1551
   120
    
jaroslav@1551
   121
    private static Bck2Brwsr.Resources toZip(String path) throws IOException {
jaroslav@1551
   122
        URL u = new URL(path);
jaroslav@1551
   123
        byte[] zipData = (byte[]) u.getContent(new Class[]{byte[].class});
jaroslav@1551
   124
        Bck2Brwsr.Resources r;
jaroslav@1551
   125
        try {
jaroslav@1551
   126
            Class<?> fastJar = Class.forName("org.apidesign.bck2brwsr.vmzip.ZipResources");
jaroslav@1551
   127
            return (Bck2Brwsr.Resources) fastJar.getConstructor(byte[].class).newInstance(zipData);
jaroslav@1551
   128
        } catch (Exception ex) {
jaroslav@1551
   129
            log("Reading JARs is only possible with enum.zip module included: " + ex.getMessage());
jaroslav@1551
   130
            ex.printStackTrace();
jaroslav@1551
   131
            throw new IOException(ex);
jaroslav@1551
   132
        }
jaroslav@1551
   133
    }
jaroslav@1551
   134
    
jaroslav@1551
   135
    private static byte[] readBytes(Bck2Brwsr.Resources r, String res) throws IOException {
jaroslav@1551
   136
        InputStream is = r.get(res);
jaroslav@1551
   137
        if (is == null) {
jaroslav@1551
   138
            return null;
jaroslav@1551
   139
        }
jaroslav@1551
   140
        byte[] arr = new byte[is.available()];
jaroslav@1551
   141
        int off = 0;
jaroslav@1551
   142
        for (;;) {
jaroslav@1551
   143
            int len = is.read(arr, off, arr.length - off);
jaroslav@1551
   144
            if (len == -1) {
jaroslav@1551
   145
                break;
jaroslav@1551
   146
            }
jaroslav@1551
   147
            off += len;
jaroslav@1551
   148
        }
jaroslav@1551
   149
        is.close();
jaroslav@1551
   150
        return arr;
jaroslav@1551
   151
    }
jaroslav@644
   152
}