Rewriting the (dynamic) calculator demo to be able to reference JARs, but load from a server emul
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 15 Feb 2013 19:24:34 +0100
branchemul
changeset 7411343f52164f3
parent 740 d3220b78c21a
child 742 3035beed09a1
Rewriting the (dynamic) calculator demo to be able to reference JARs, but load from a server
javaquery/demo-calculator-dynamic/pom.xml
javaquery/demo-calculator-dynamic/src/main/assembly/bck2brwsr.xml
javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/demo/calc/Calculator.xhtml
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
     1.1 --- a/javaquery/demo-calculator-dynamic/pom.xml	Fri Feb 15 19:21:40 2013 +0100
     1.2 +++ b/javaquery/demo-calculator-dynamic/pom.xml	Fri Feb 15 19:24:34 2013 +0100
     1.3 @@ -40,14 +40,47 @@
     1.4                 <target>1.7</target>
     1.5              </configuration>
     1.6           </plugin>
     1.7 +         <plugin>
     1.8 +             <groupId>org.apache.maven.plugins</groupId>
     1.9 +             <artifactId>maven-jar-plugin</artifactId>
    1.10 +             <version>2.4</version>
    1.11 +             <configuration>
    1.12 +                 <archive>
    1.13 +                     <manifest>
    1.14 +                         <addClasspath>true</addClasspath>
    1.15 +                         <classpathPrefix>lib/</classpathPrefix>
    1.16 +                         <mainClass>org.apidesign.bck2brwsr.demo.calc.staticcompilation.Calc</mainClass>
    1.17 +                     </manifest>
    1.18 +                 </archive>
    1.19 +             </configuration>
    1.20 +         </plugin>
    1.21 +         <plugin>
    1.22 +            <artifactId>maven-assembly-plugin</artifactId>
    1.23 +                <version>2.4</version>
    1.24 +                <executions>
    1.25 +                    <execution>
    1.26 +                        <id>distro-assembly</id>
    1.27 +                        <phase>package</phase>
    1.28 +                        <goals>
    1.29 +                            <goal>single</goal>
    1.30 +                        </goals>
    1.31 +                        <configuration>
    1.32 +                            <descriptors>
    1.33 +                                <descriptor>src/main/assembly/bck2brwsr.xml</descriptor>
    1.34 +                            </descriptors>
    1.35 +                        </configuration>
    1.36 +                    </execution>
    1.37 +                </executions>                
    1.38 +            </plugin>      
    1.39        </plugins>
    1.40    </build>
    1.41  
    1.42    <dependencies>
    1.43      <dependency>
    1.44        <groupId>org.apidesign.bck2brwsr</groupId>
    1.45 -      <artifactId>emul.mini</artifactId>
    1.46 +      <artifactId>emul</artifactId>
    1.47        <version>0.3-SNAPSHOT</version>
    1.48 +      <classifier>rt</classifier>
    1.49      </dependency>
    1.50      <dependency>
    1.51        <groupId>org.apidesign.bck2brwsr</groupId>
    1.52 @@ -60,5 +93,13 @@
    1.53        <version>6.5.2</version>
    1.54        <scope>test</scope>
    1.55      </dependency>
    1.56 +    <dependency>
    1.57 +      <groupId>org.apidesign.bck2brwsr</groupId>
    1.58 +      <artifactId>vm4brwsr</artifactId>
    1.59 +      <classifier>js</classifier>
    1.60 +      <type>zip</type>
    1.61 +      <version>0.3-SNAPSHOT</version>
    1.62 +      <scope>provided</scope>
    1.63 +    </dependency>
    1.64    </dependencies>
    1.65  </project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/javaquery/demo-calculator-dynamic/src/main/assembly/bck2brwsr.xml	Fri Feb 15 19:24:34 2013 +0100
     2.3 @@ -0,0 +1,62 @@
     2.4 +<?xml version="1.0"?>
     2.5 +<!--
     2.6 +
     2.7 +    Back 2 Browser Bytecode Translator
     2.8 +    Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.9 +
    2.10 +    This program is free software: you can redistribute it and/or modify
    2.11 +    it under the terms of the GNU General Public License as published by
    2.12 +    the Free Software Foundation, version 2 of the License.
    2.13 +
    2.14 +    This program is distributed in the hope that it will be useful,
    2.15 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.16 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.17 +    GNU General Public License for more details.
    2.18 +
    2.19 +    You should have received a copy of the GNU General Public License
    2.20 +    along with this program. Look for COPYING file in the top folder.
    2.21 +    If not, see http://opensource.org/licenses/GPL-2.0.
    2.22 +
    2.23 +-->
    2.24 +<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2.25 +  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    2.26 +  
    2.27 +  <id>bck2brwsr</id>
    2.28 +  <formats>
    2.29 +      <format>zip</format>
    2.30 +      <format>dir</format>
    2.31 +  </formats>
    2.32 +  <baseDirectory>public_html</baseDirectory>
    2.33 +  <dependencySets>
    2.34 +    <dependencySet>
    2.35 +        <useProjectArtifact>false</useProjectArtifact>
    2.36 +        <scope>runtime</scope>
    2.37 +        <outputDirectory>lib</outputDirectory>
    2.38 +        <includes>
    2.39 +            <include>*:jar</include>
    2.40 +            <include>*:rt</include>
    2.41 +        </includes>
    2.42 +    </dependencySet>
    2.43 +    <dependencySet>
    2.44 +        <useProjectArtifact>false</useProjectArtifact>
    2.45 +        <scope>provided</scope>
    2.46 +        <includes>
    2.47 +            <include>*:js</include>
    2.48 +        </includes>
    2.49 +        <unpack>true</unpack>
    2.50 +        <outputDirectory>/</outputDirectory>
    2.51 +    </dependencySet>
    2.52 +  </dependencySets> 
    2.53 +  <files>
    2.54 +    <file>
    2.55 +      <source>${project.build.directory}/${project.build.finalName}.jar</source>
    2.56 +      <outputDirectory>/</outputDirectory>
    2.57 +    </file>
    2.58 +    <file>
    2.59 +      <source>${project.build.directory}/classes/org/apidesign/bck2brwsr/demo/calc/Calculator.xhtml</source>
    2.60 +      <outputDirectory>/</outputDirectory>
    2.61 +      <destName>index.xhtml</destName>
    2.62 +    </file>
    2.63 +  </files>
    2.64 +
    2.65 +</assembly>
    2.66 \ No newline at end of file
     3.1 --- a/javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/demo/calc/Calculator.xhtml	Fri Feb 15 19:21:40 2013 +0100
     3.2 +++ b/javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/demo/calc/Calculator.xhtml	Fri Feb 15 19:24:34 2013 +0100
     3.3 @@ -78,9 +78,9 @@
     3.4          </table>
     3.5          <div data-bind="text: displayPreview"></div>
     3.6          
     3.7 -        <script src="/bck2brwsr.js"></script>
     3.8 -        <script src="/vm.js"></script>
     3.9 +        <script src="bck2brwsr.js"></script>
    3.10          <script type="text/javascript">
    3.11 +            var vm = bck2brwsr('demo.calculator-0.3-SNAPSHOT.jar');
    3.12              vm.loadClass('org.apidesign.bck2brwsr.demo.calc.Calc');
    3.13          </script>
    3.14          
     4.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Fri Feb 15 19:21:40 2013 +0100
     4.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Fri Feb 15 19:24:34 2013 +0100
     4.3 @@ -95,9 +95,10 @@
     4.4              startpage = "/" + startpage;
     4.5          }
     4.6          HttpServer s = initServer(".", true);
     4.7 +        s.getServerConfiguration().addHttpHandler(new Page(resources, startpage), "/index.xhtml");
     4.8          s.getServerConfiguration().addHttpHandler(new Page(resources, null), "/");
     4.9          try {
    4.10 -            launchServerAndBrwsr(s, startpage);
    4.11 +            launchServerAndBrwsr(s, "/index.xhtml");
    4.12          } catch (URISyntaxException | InterruptedException ex) {
    4.13              throw new IOException(ex);
    4.14          }
    4.15 @@ -139,7 +140,7 @@
    4.16  
    4.17          final ServerConfiguration conf = s.getServerConfiguration();
    4.18          if (addClasses) {
    4.19 -            conf.addHttpHandler(new VM(resources), "/vm.js");
    4.20 +            conf.addHttpHandler(new VM(resources), "/bck2brwsr.js");
    4.21              conf.addHttpHandler(new Classes(resources), "/classes/");
    4.22          }
    4.23          return s;
    4.24 @@ -450,9 +451,9 @@
    4.25              String r = resource;
    4.26              if (r == null) {
    4.27                  r = request.getHttpHandlerPath();
    4.28 -                if (r.startsWith("/")) {
    4.29 -                    r = r.substring(1);
    4.30 -                }
    4.31 +            }
    4.32 +            if (r.startsWith("/")) {
    4.33 +                r = r.substring(1);
    4.34              }
    4.35              String[] replace = {};
    4.36              if (r.endsWith(".html")) {
    4.37 @@ -483,14 +484,19 @@
    4.38              StringBuilder sb = new StringBuilder();
    4.39              Bck2Brwsr.generate(sb, loader);
    4.40              sb.append(
    4.41 -                "function ldCls(res) {\n"
    4.42 -                + "  var request = new XMLHttpRequest();\n"
    4.43 -                + "  request.open('GET', '/classes/' + res, false);\n"
    4.44 -                + "  request.send();\n"
    4.45 -                + "  var arr = eval('(' + request.responseText + ')');\n"
    4.46 -                + "  return arr;\n"
    4.47 -                + "}\n"
    4.48 -                + "var vm = new bck2brwsr(ldCls);\n"
    4.49 +                  "(function WrapperVM(global) {"
    4.50 +                + "  function ldCls(res) {\n"
    4.51 +                + "    var request = new XMLHttpRequest();\n"
    4.52 +                + "    request.open('GET', '/classes/' + res, false);\n"
    4.53 +                + "    request.send();\n"
    4.54 +                + "    var arr = eval('(' + request.responseText + ')');\n"
    4.55 +                + "    return arr;\n"
    4.56 +                + "  }\n"
    4.57 +                + "  var prevvm = global.bck2brwsr;\n"
    4.58 +                + "  global.bck2brwsr = function() {\n"
    4.59 +                + "    return prevvm(ldCls);\n"
    4.60 +                + "  };\n"
    4.61 +                + "})(this);\n"
    4.62              );
    4.63              this.bck2brwsr = sb.toString();
    4.64          }