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