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