rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Tue, 19 Mar 2013 13:08:44 +0100
branchclosure
changeset 860 35507d1a5069
parent 849 d95117153304
child 874 2bcbe348dbec
permissions -rw-r--r--
Added since tags
     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.IOException;
    21 import java.io.InputStream;
    22 import java.net.URL;
    23 import java.util.Enumeration;
    24 
    25 /** Build your own virtual machine! Use methods in this class to generate
    26  * a skeleton JVM in JavaScript that contains pre-compiled classes of your
    27  * choice. The generated script defines one JavaScript method that can
    28  * be used to bootstrap and load the virtual machine: <pre>
    29  * var vm = bck2brwsr();
    30  * var main = vm.loadClass('org.your.pkg.Main');
    31  * main.main__V_3Ljava_lang_String_2(null);
    32  * </pre>
    33  * In case one wants to initialize the virtual machine with ability to
    34  * load classes lazily when needed, one can provide a loader function to
    35  * when creating the virtual machine: <pre>
    36  * var vm = bck2brwsr(function(resource) { 
    37  *   return null; // byte[] for the resource
    38  * });
    39  * </pre>
    40  * In this scenario, when a request for an unknown class is made, the loader
    41  * function is asked for its byte code and the system dynamically transforms
    42  * it to JavaScript.
    43  * <p>
    44  * Instead of a loader function, one can also provide a URL to a JAR file.
    45  * The <code>bck2brwsr</code> system will do its best to download the file
    46  * and provide loader function for it automatically.
    47  * <p>
    48  * One can provide as many loader functions and JAR URL references as necessary.
    49  * Then the initialization code would look like:<pre>
    50  * var vm = bck2brwsr(url1, url2, fnctn1, url3, functn2);
    51  * </pre>
    52  * The provided URLs and loader functions will be consulted one by one.
    53  *
    54  * @author Jaroslav Tulach <jtulach@netbeans.org>
    55  */
    56 public final class Bck2Brwsr {
    57     private Bck2Brwsr() {
    58     }
    59 
    60     /** Generates virtual machine from bytes served by a <code>resources</code>
    61      * provider.
    62      *
    63      * @param out the output to write the generated JavaScript to
    64      * @param resources provider of class files to use
    65      * @param classes additional classes to include in the generated script
    66      * @throws IOException I/O exception can be thrown when something goes wrong
    67      */
    68     public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
    69         generate(out, ObfuscationLevel.NONE, resources, classes);
    70     }
    71 
    72     /** Generates virtual machine from bytes served by a class loader.
    73      *
    74      * @param out the output to write the generated JavaScript to
    75      * @param loader class loader to load needed classes from
    76      * @param classes additional classes to include in the generated script
    77      * @throws IOException I/O exception can be thrown when something goes wrong
    78      */
    79     public static void generate(Appendable out, ClassLoader loader, String... classes) throws IOException {
    80         generate(out, ObfuscationLevel.NONE, loader, classes);
    81     }
    82 
    83     /** Generates virtual machine from bytes served by a <code>resources</code>
    84      * provider.
    85      * 
    86      * @param out the output to write the generated JavaScript to
    87      * @param obfuscationLevel the obfuscation level for the generated
    88      *                         JavaScript
    89      * @param resources provider of class files to use
    90      * @param classes additional classes to include in the generated script
    91      * @throws IOException I/O exception can be thrown when something goes wrong
    92      * @since 0.5
    93      */
    94     public static void generate(Appendable out, ObfuscationLevel obfuscationLevel, Resources resources, String... classes) throws IOException {
    95         StringArray arr = StringArray.asList(classes);
    96         arr.add(VM.class.getName().replace('.', '/'));
    97 
    98         if (obfuscationLevel != ObfuscationLevel.NONE) {
    99             try {
   100                 ClosureWrapper.produceTo(out, obfuscationLevel, resources, arr);
   101                 return;
   102             } catch (IOException ex) {
   103                 throw ex;
   104             } catch (Throwable ex) {
   105                 out.append("/* Failed to obfuscate: " + ex.getMessage()
   106                                + " */\n");
   107             }
   108         }
   109 
   110         VM.compile(resources, out, arr);
   111     }
   112     
   113     /** Generates virtual machine from bytes served by a class loader.
   114      * 
   115      * @param out the output to write the generated JavaScript to
   116      * @param obfuscationLevel the obfuscation level for the generated
   117      *                         JavaScript
   118      * @param loader class loader to load needed classes from
   119      * @param classes additional classes to include in the generated script
   120      * @throws IOException I/O exception can be thrown when something goes wrong
   121      * @since 0.5
   122      */
   123     public static void generate(Appendable out, ObfuscationLevel obfuscationLevel, final ClassLoader loader, String... classes) throws IOException {
   124         class R implements Resources {
   125             @Override
   126             public InputStream get(String name) throws IOException {
   127                 Enumeration<URL> en = loader.getResources(name);
   128                 URL u = null;
   129                 while (en.hasMoreElements()) {
   130                     u = en.nextElement();
   131                 }
   132                 if (u == null) {
   133                     throw new IOException("Can't find " + name);
   134                 }
   135                 return u.openStream();
   136             }
   137         }
   138         generate(out, obfuscationLevel, new R(), classes);
   139     }
   140     
   141     /** Provider of resources (classes and other files). The 
   142      * {@link #generate(java.lang.Appendable, org.apidesign.vm4brwsr.Bck2Brwsr.Resources, java.lang.String[]) 
   143      * generator method} will call back here for all classes needed during
   144      * translation to JavaScript.
   145      */
   146     public interface Resources {
   147         /** Loads given resource (class or other file like image). The 
   148          * resource name to load bytes for the {@link String} class
   149          * would be <code>"java/lang/String.class"</code>.
   150          * 
   151          * @param resource path to resource to load
   152          * @return the input stream for the resource 
   153          * @throws IOException can be thrown if the loading fails on some error
   154          *   or the file cannot be found
   155          */
   156         public InputStream get(String resource) throws IOException;
   157     }
   158 }