rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Feb 2015 11:12:53 +0100
changeset 1787 ea12a3bb4b33
parent 1581 cf0f746f1a7f
child 1942 f8cc2a015ece
permissions -rw-r--r--
Using year range 2012-2015 in copyright header
jaroslav@644
     1
/**
jaroslav@644
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 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@1575
    24
import org.apidesign.bck2brwsr.core.Exported;
jaroslav@644
    25
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@644
    26
jaroslav@644
    27
/** Conversion from classpath to load function.
jaroslav@644
    28
 *
jaroslav@644
    29
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@644
    30
 */
jaroslav@1550
    31
final class ClassPath {
jaroslav@1550
    32
    private ClassPath() {
jaroslav@644
    33
    }
jaroslav@644
    34
    
jaroslav@644
    35
    public static void init() {
jaroslav@644
    36
    }
jaroslav@729
    37
    @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
jaroslav@729
    38
    private static native int length(Object arr);
jaroslav@729
    39
    @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
jaroslav@729
    40
    private static native Object at(Object arr, int index);
jaroslav@729
    41
    @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
jaroslav@729
    42
    private static native Object set(Object arr, int index, Object value);
jaroslav@644
    43
    
jaroslav@1554
    44
    private static boolean doingToZip;
jaroslav@1575
    45
    
jaroslav@1581
    46
    static byte[] loadBytes(String resource, Object classpath, int skip)
jaroslav@729
    47
    throws IOException, ClassNotFoundException {
jaroslav@729
    48
        for (int i = 0; i < length(classpath); i++) {
jaroslav@1581
    49
            byte[] arr = loadBytes(resource, classpath, i, skip);
jaroslav@1581
    50
            if (arr != null) {
jaroslav@1581
    51
                return arr;
jaroslav@1581
    52
            }
jaroslav@1581
    53
        }
jaroslav@1581
    54
        return null;
jaroslav@1581
    55
    }
jaroslav@1581
    56
    @Exported static byte[] loadBytes(String resource, Object classpath, int i, int skip) 
jaroslav@1581
    57
    throws IOException, ClassNotFoundException {
jaroslav@1581
    58
        Object c = at(classpath, i);
jaroslav@1581
    59
        if (c instanceof String && !doingToZip) {
jaroslav@1581
    60
            try {
jaroslav@1581
    61
                doingToZip = true;
jaroslav@1581
    62
                String url = (String)c;
jaroslav@1581
    63
                final Bck2Brwsr.Resources z = toZip(url);
jaroslav@1581
    64
                c = set(classpath, i, z);
jaroslav@1581
    65
                final byte[] man = readBytes(z, "META-INF/MANIFEST.MF");
jaroslav@1581
    66
                if (man != null) {
jaroslav@1581
    67
                    String mainClass = processClassPathAttr(man, url, classpath);
jaroslav@743
    68
//                        if (mainClass != null) {
jaroslav@743
    69
//                            Class.forName(mainClass);
jaroslav@743
    70
//                        }
jaroslav@644
    71
                }
jaroslav@1581
    72
            } catch (IOException ex) {
jaroslav@1581
    73
                set(classpath, i, ex);
jaroslav@1581
    74
                log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
jaroslav@1581
    75
            } finally {
jaroslav@1581
    76
                doingToZip = false;
jaroslav@644
    77
            }
jaroslav@1581
    78
        }
jaroslav@1581
    79
        if (resource != null) {
jaroslav@1581
    80
            byte[] checkRes;
jaroslav@1581
    81
            if (c instanceof Bck2Brwsr.Resources) {
jaroslav@1581
    82
                checkRes = readBytes((Bck2Brwsr.Resources)c, resource);
jaroslav@1581
    83
                if (checkRes != null && --skip < 0) {
jaroslav@1581
    84
                    return checkRes;
jaroslav@1581
    85
                }
jaroslav@1581
    86
            } else {
jaroslav@1581
    87
                checkRes = callFunction(c, resource, skip);
jaroslav@1581
    88
                if (checkRes != null) {
jaroslav@1581
    89
                    return checkRes;
jaroslav@644
    90
                }
jaroslav@644
    91
            }
jaroslav@644
    92
        }
jaroslav@644
    93
        return null;
jaroslav@644
    94
    }
jaroslav@706
    95
    
jaroslav@1375
    96
    @JavaScriptBody(args = { "fn", "res", "skip" }, body = 
jaroslav@1375
    97
        "if (typeof fn === 'function') return fn(res, skip);\n"
jaroslav@729
    98
      + "return null;"
jaroslav@729
    99
    )
jaroslav@1375
   100
    private static native byte[] callFunction(Object fn, String res, int skip);
jaroslav@729
   101
    
jaroslav@1358
   102
    @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
jaroslav@706
   103
    private static native void log(String msg);
jaroslav@706
   104
jaroslav@729
   105
    private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
jaroslav@702
   106
        try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
jaroslav@672
   107
            String cp = is.toString();
jaroslav@702
   108
            if (cp != null) {
jaroslav@702
   109
                cp = cp.trim();
jaroslav@702
   110
                for (int p = 0; p < cp.length();) {
jaroslav@702
   111
                    int n = cp.indexOf(' ', p);
jaroslav@702
   112
                    if (n == -1) {
jaroslav@702
   113
                        n = cp.length();
jaroslav@702
   114
                    }
jaroslav@702
   115
                    String el = cp.substring(p, n);
jaroslav@702
   116
                    URL u = new URL(new URL(url), el);
jaroslav@702
   117
                    classpath = addToArray(classpath, u.toString());
jaroslav@702
   118
                    p = n + 1;
jaroslav@702
   119
                }
jaroslav@672
   120
            }
jaroslav@702
   121
            return is.getMainClass();
jaroslav@672
   122
        }
jaroslav@672
   123
    }
jaroslav@672
   124
jaroslav@729
   125
    private static Object addToArray(Object arr, String value) {
jaroslav@729
   126
        final int last = length(arr);
jaroslav@729
   127
        Object ret = enlargeArray(arr, last + 1);
jaroslav@729
   128
        set(ret, last, value);
jaroslav@672
   129
        return ret;
jaroslav@672
   130
    }
jaroslav@672
   131
jaroslav@729
   132
    @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
jaroslav@729
   133
    private static native Object enlargeArray(Object arr, int len);
jaroslav@1554
   134
jaroslav@1551
   135
    private static Bck2Brwsr.Resources toZip(String path) throws IOException {
jaroslav@1551
   136
        URL u = new URL(path);
jaroslav@1551
   137
        byte[] zipData = (byte[]) u.getContent(new Class[]{byte[].class});
jaroslav@1551
   138
        Bck2Brwsr.Resources r;
jaroslav@1551
   139
        try {
jaroslav@1551
   140
            Class<?> fastJar = Class.forName("org.apidesign.bck2brwsr.vmzip.ZipResources");
jaroslav@1551
   141
            return (Bck2Brwsr.Resources) fastJar.getConstructor(byte[].class).newInstance(zipData);
jaroslav@1551
   142
        } catch (Exception ex) {
jaroslav@1551
   143
            log("Reading JARs is only possible with enum.zip module included: " + ex.getMessage());
jaroslav@1551
   144
            ex.printStackTrace();
jaroslav@1551
   145
            throw new IOException(ex);
jaroslav@1551
   146
        }
jaroslav@1551
   147
    }
jaroslav@1551
   148
    
jaroslav@1551
   149
    private static byte[] readBytes(Bck2Brwsr.Resources r, String res) throws IOException {
jaroslav@1551
   150
        InputStream is = r.get(res);
jaroslav@1551
   151
        if (is == null) {
jaroslav@1551
   152
            return null;
jaroslav@1551
   153
        }
jaroslav@1551
   154
        byte[] arr = new byte[is.available()];
jaroslav@1551
   155
        int off = 0;
jaroslav@1551
   156
        for (;;) {
jaroslav@1551
   157
            int len = is.read(arr, off, arr.length - off);
jaroslav@1551
   158
            if (len == -1) {
jaroslav@1551
   159
                break;
jaroslav@1551
   160
            }
jaroslav@1551
   161
            off += len;
jaroslav@1551
   162
        }
jaroslav@1551
   163
        is.close();
jaroslav@1551
   164
        return arr;
jaroslav@1551
   165
    }
jaroslav@644
   166
}