Recognizing Main-Class attribute, so initializing the VM is just about passing in correct classpath emul
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Feb 2013 13:41:56 +0100
branchemul
changeset 702fa42b3d8cbbc
parent 701 bfb3f72249de
child 705 6de8252246b5
Recognizing Main-Class attribute, so initializing the VM is just about passing in correct classpath
javaquery/demo-calculator/pom.xml
javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml
vm/src/main/java/org/apidesign/vm4brwsr/ParseCPAttr.java
vm/src/main/java/org/apidesign/vm4brwsr/ParseMan.java
vm/src/main/java/org/apidesign/vm4brwsr/VM.java
vm/src/main/java/org/apidesign/vm4brwsr/Zips.java
     1.1 --- a/javaquery/demo-calculator/pom.xml	Thu Feb 07 13:15:41 2013 +0100
     1.2 +++ b/javaquery/demo-calculator/pom.xml	Thu Feb 07 13:41:56 2013 +0100
     1.3 @@ -50,6 +50,7 @@
     1.4                       <manifest>
     1.5                           <addClasspath>true</addClasspath>
     1.6                           <classpathPrefix>lib/</classpathPrefix>
     1.7 +                         <mainClass>org.apidesign.bck2brwsr.demo.calc.staticcompilation.Calc</mainClass>
     1.8                       </manifest>
     1.9                   </archive>
    1.10               </configuration>
     2.1 --- a/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml	Thu Feb 07 13:15:41 2013 +0100
     2.2 +++ b/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml	Thu Feb 07 13:41:56 2013 +0100
     2.3 @@ -79,9 +79,9 @@
     2.4          <div data-bind="text: displayPreview"></div>
     2.5          <script src="bck2brwsr.js"/>
     2.6          <script>
     2.7 -            var classpath = [ 'demo.static.calculator-0.3-SNAPSHOT.jar' ];
     2.8 -            var vm = bck2brwsr(classpath);
     2.9 -            vm.loadClass('org.apidesign.bck2brwsr.demo.calc.staticcompilation.Calc');
    2.10 +            var vm = new bck2brwsr(
    2.11 +                [ 'demo.static.calculator-0.3-SNAPSHOT.jar' ]
    2.12 +            );
    2.13          </script>
    2.14      </body>
    2.15  </html>
     3.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ParseCPAttr.java	Thu Feb 07 13:15:41 2013 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,49 +0,0 @@
     3.4 -/**
     3.5 - * Back 2 Browser Bytecode Translator
     3.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 - *
     3.8 - * This program is free software: you can redistribute it and/or modify
     3.9 - * it under the terms of the GNU General Public License as published by
    3.10 - * the Free Software Foundation, version 2 of the License.
    3.11 - *
    3.12 - * This program is distributed in the hope that it will be useful,
    3.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 - * GNU General Public License for more details.
    3.16 - *
    3.17 - * You should have received a copy of the GNU General Public License
    3.18 - * along with this program. Look for COPYING file in the top folder.
    3.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 - */
    3.21 -package org.apidesign.vm4brwsr;
    3.22 -
    3.23 -import java.io.IOException;
    3.24 -import java.io.InputStream;
    3.25 -import org.apidesign.bck2brwsr.emul.lang.ManifestInputStream;
    3.26 -
    3.27 -/**
    3.28 - *
    3.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.30 - */
    3.31 -final class ParseCPAttr extends ManifestInputStream {
    3.32 -    private String cp;
    3.33 -
    3.34 -    public ParseCPAttr(InputStream is) throws IOException {
    3.35 -        super(is);
    3.36 -        readAttributes(new byte[512]);
    3.37 -    }
    3.38 -
    3.39 -    @Override
    3.40 -    protected String putValue(String key, String value) {
    3.41 -        if ("Class-Path".equals(key)) {
    3.42 -            cp = value;
    3.43 -        }
    3.44 -        return null;
    3.45 -    }
    3.46 -
    3.47 -    @Override
    3.48 -    public String toString() {
    3.49 -        return cp;
    3.50 -    }
    3.51 -    
    3.52 -}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ParseMan.java	Thu Feb 07 13:41:56 2013 +0100
     4.3 @@ -0,0 +1,57 @@
     4.4 +/**
     4.5 + * Back 2 Browser Bytecode Translator
     4.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details.
    4.16 + *
    4.17 + * You should have received a copy of the GNU General Public License
    4.18 + * along with this program. Look for COPYING file in the top folder.
    4.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    4.20 + */
    4.21 +package org.apidesign.vm4brwsr;
    4.22 +
    4.23 +import java.io.IOException;
    4.24 +import java.io.InputStream;
    4.25 +import org.apidesign.bck2brwsr.emul.lang.ManifestInputStream;
    4.26 +
    4.27 +/**
    4.28 + *
    4.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.30 + */
    4.31 +final class ParseMan extends ManifestInputStream {
    4.32 +    private String cp;
    4.33 +    private String mc;
    4.34 +
    4.35 +    public ParseMan(InputStream is) throws IOException {
    4.36 +        super(is);
    4.37 +        readAttributes(new byte[512]);
    4.38 +    }
    4.39 +
    4.40 +    @Override
    4.41 +    protected String putValue(String key, String value) {
    4.42 +        if ("Class-Path".equals(key)) {
    4.43 +            cp = value;
    4.44 +        }
    4.45 +        if ("Main-Class".equals(key)) {
    4.46 +            mc = value;
    4.47 +        }
    4.48 +        return null;
    4.49 +    }
    4.50 +    
    4.51 +    String getMainClass() {
    4.52 +        return mc;
    4.53 +    }
    4.54 +
    4.55 +    @Override
    4.56 +    public String toString() {
    4.57 +        return cp;
    4.58 +    }
    4.59 +    
    4.60 +}
     5.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Thu Feb 07 13:15:41 2013 +0100
     5.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Thu Feb 07 13:41:56 2013 +0100
     5.3 @@ -120,10 +120,11 @@
     5.4                "  global.bck2brwsr = function() {\n"
     5.5              + "    var args = arguments;\n"
     5.6              + "    var loader = {};\n"
     5.7 +            + "    var init = null;\n"
     5.8              + "    loader.vm = vm;\n"
     5.9              + "    if (args.length == 1 && typeof args[0] !== 'function') {;\n"
    5.10              + "      var classpath = args[0];\n"
    5.11 -            + "      args[0] = function(name) {\n"
    5.12 +            + "      init = args[0] = function(name) {\n"
    5.13              + "        return vm.org_apidesign_vm4brwsr_Zips(false).loadFromCp___3B_3Ljava_lang_Object_2Ljava_lang_String_2(classpath, name);\n"
    5.14              + "      };\n"
    5.15              + "    };\n"
    5.16 @@ -142,6 +143,7 @@
    5.17              + "        return args[0](name);\n"
    5.18              + "      }\n"
    5.19              + "    }\n"
    5.20 +            + "    if (init) init(null);\n"
    5.21              + "    return loader;\n"
    5.22              + "  };\n");
    5.23          out.append("}(this));");
     6.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java	Thu Feb 07 13:15:41 2013 +0100
     6.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java	Thu Feb 07 13:41:56 2013 +0100
     6.3 @@ -19,7 +19,6 @@
     6.4  
     6.5  import java.io.ByteArrayInputStream;
     6.6  import java.io.IOException;
     6.7 -import java.io.InputStream;
     6.8  import java.net.URL;
     6.9  import java.util.zip.ZipEntry;
    6.10  import java.util.zip.ZipInputStream;
    6.11 @@ -36,7 +35,7 @@
    6.12      public static void init() {
    6.13      }
    6.14      
    6.15 -    public static byte[] loadFromCp(Object[] classpath, String res) {
    6.16 +    public static byte[] loadFromCp(Object[] classpath, String res) throws Exception {
    6.17          for (int i = 0; i < classpath.length; i++) {
    6.18              Object c = classpath[i];
    6.19              if (c instanceof String) {
    6.20 @@ -46,13 +45,17 @@
    6.21                      c = classpath[i] = z;
    6.22                      final byte[] man = z.findRes("META-INF/MANIFEST.MF");
    6.23                      if (man != null) {
    6.24 -                        processClassPathAttr(man, url, classpath);
    6.25 +                        String mainClass = processClassPathAttr(man, url, classpath);
    6.26 +                        if (mainClass != null) {
    6.27 +                            Class.forName(mainClass);
    6.28 +                        }
    6.29                      }
    6.30 -                } catch (IOException ex) {
    6.31 +                } catch (Exception ex) {
    6.32                      classpath[i] = ex;
    6.33 +                    throw ex;
    6.34                  }
    6.35              }
    6.36 -            if (c instanceof Zips) {
    6.37 +            if (res != null && c instanceof Zips) {
    6.38                  Object checkRes = ((Zips)c).findRes(res);
    6.39                  if (checkRes instanceof byte[]) {
    6.40                      return (byte[])checkRes;
    6.41 @@ -95,30 +98,26 @@
    6.42          return z;
    6.43      }
    6.44  
    6.45 -    private static void processClassPathAttr(final byte[] man, String url, Object[] classpath) throws IOException {
    6.46 -        try (InputStream is = initIS(new ByteArrayInputStream(man))) {
    6.47 +    private static String processClassPathAttr(final byte[] man, String url, Object[] classpath) throws IOException {
    6.48 +        try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
    6.49              String cp = is.toString();
    6.50 -            if (cp == null) {
    6.51 -                return;
    6.52 +            if (cp != null) {
    6.53 +                cp = cp.trim();
    6.54 +                for (int p = 0; p < cp.length();) {
    6.55 +                    int n = cp.indexOf(' ', p);
    6.56 +                    if (n == -1) {
    6.57 +                        n = cp.length();
    6.58 +                    }
    6.59 +                    String el = cp.substring(p, n);
    6.60 +                    URL u = new URL(new URL(url), el);
    6.61 +                    classpath = addToArray(classpath, u.toString());
    6.62 +                    p = n + 1;
    6.63 +                }
    6.64              }
    6.65 -            cp = cp.trim();
    6.66 -            for (int p = 0; p < cp.length();) {
    6.67 -                int n = cp.indexOf(' ', p);
    6.68 -                if (n == -1) {
    6.69 -                    n = cp.length();
    6.70 -                }
    6.71 -                String el = cp.substring(p, n);
    6.72 -                URL u = new URL(new URL(url), el);
    6.73 -                classpath = addToArray(classpath, u.toString());
    6.74 -                p = n + 1;
    6.75 -            }
    6.76 +            return is.getMainClass();
    6.77          }
    6.78      }
    6.79  
    6.80 -    private static InputStream initIS(InputStream is) throws IOException {
    6.81 -        return new ParseCPAttr(is);
    6.82 -    }
    6.83 -    
    6.84      private static Object[] addToArray(Object[] arr, String value) {
    6.85          final int last = arr.length;
    6.86          Object[] ret = enlargeArray(arr, last + 1);