rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 07 May 2014 17:24:29 +0200
branchclosure
changeset 1550 cb9e273dfd51
parent 1387 rt/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java@350f8aee0f60
child 1551 0002739b6d1f
permissions -rw-r--r--
Splitting into two classes: one that deals with classpath, one that deals with ZIP content
     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.net.URL;
    23 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    24 
    25 /** Conversion from classpath to load function.
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 final class ClassPath {
    30     private ClassPath() {
    31     }
    32     
    33     public static void init() {
    34     }
    35     @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
    36     private static native int length(Object arr);
    37     @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
    38     private static native Object at(Object arr, int index);
    39     @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
    40     private static native Object set(Object arr, int index, Object value);
    41     
    42     public static byte[] loadFromCp(Object classpath, String res, int skip) 
    43     throws IOException, ClassNotFoundException {
    44         for (int i = 0; i < length(classpath); i++) {
    45             Object c = at(classpath, i);
    46             if (c instanceof String) {
    47                 try {
    48                     String url = (String)c;
    49                     final ZipHandler z = ZipHandler.toZip(url);
    50                     c = set(classpath, i, z);
    51                     final byte[] man = z.findRes("META-INF/MANIFEST.MF");
    52                     if (man != null) {
    53                         String mainClass = processClassPathAttr(man, url, classpath);
    54 //                        if (mainClass != null) {
    55 //                            Class.forName(mainClass);
    56 //                        }
    57                     }
    58                 } catch (IOException ex) {
    59                     set(classpath, i, ex);
    60                     log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
    61                 }
    62             }
    63             if (res != null) {
    64                 byte[] checkRes;
    65                 if (c instanceof ZipHandler) {
    66                     checkRes = ((ZipHandler)c).findRes(res);
    67                     if (checkRes != null && --skip < 0) {
    68                         return checkRes;
    69                     }
    70                 } else {
    71                     checkRes = callFunction(c, res, skip);
    72                     if (checkRes != null) {
    73                         return checkRes;
    74                     }
    75                 }
    76             }
    77         }
    78         return null;
    79     }
    80     
    81     @JavaScriptBody(args = { "fn", "res", "skip" }, body = 
    82         "if (typeof fn === 'function') return fn(res, skip);\n"
    83       + "return null;"
    84     )
    85     private static native byte[] callFunction(Object fn, String res, int skip);
    86     
    87     @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
    88     private static native void log(String msg);
    89 
    90     private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
    91         try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
    92             String cp = is.toString();
    93             if (cp != null) {
    94                 cp = cp.trim();
    95                 for (int p = 0; p < cp.length();) {
    96                     int n = cp.indexOf(' ', p);
    97                     if (n == -1) {
    98                         n = cp.length();
    99                     }
   100                     String el = cp.substring(p, n);
   101                     URL u = new URL(new URL(url), el);
   102                     classpath = addToArray(classpath, u.toString());
   103                     p = n + 1;
   104                 }
   105             }
   106             return is.getMainClass();
   107         }
   108     }
   109 
   110     private static Object addToArray(Object arr, String value) {
   111         final int last = length(arr);
   112         Object ret = enlargeArray(arr, last + 1);
   113         set(ret, last, value);
   114         return ret;
   115     }
   116 
   117     @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
   118     private static native Object enlargeArray(Object arr, int len);
   119 }