Hiding the ScriptPresenter implementation behind few factory methods envjs
authorJaroslav Tulach <jtulach@netbeans.org>
Thu, 05 Jun 2014 20:27:54 +0200
branchenvjs
changeset 69982773e03ff40
parent 698 15f2eb71b416
child 700 055a0f3766f5
Hiding the ScriptPresenter implementation behind few factory methods
boot-script/src/main/java/net/java/html/boot/script/ScriptPresenter.java
boot-script/src/main/java/net/java/html/boot/script/Scripts.java
boot-script/src/test/java/net/java/html/boot/script/ko4j/KnockoutEnvJSTest.java
     1.1 --- a/boot-script/src/main/java/net/java/html/boot/script/ScriptPresenter.java	Thu Jun 05 19:42:03 2014 +0200
     1.2 +++ b/boot-script/src/main/java/net/java/html/boot/script/ScriptPresenter.java	Thu Jun 05 20:27:54 2014 +0200
     1.3 @@ -65,12 +65,12 @@
     1.4   * <p>
     1.5   * One can load in browser simulation for example from 
     1.6   * <a href="http://www.envjs.com/">env.js</a>. The best way to achieve so,
     1.7 - * is to add dependency on XXX
     1.8 + * is to wait until JDK-8046013 gets fixed....
     1.9   * 
    1.10   *
    1.11   * @author Jaroslav Tulach
    1.12   */
    1.13 -public final class ScriptPresenter 
    1.14 +final class ScriptPresenter 
    1.15  implements Presenter, Fn.FromJavaScript, Fn.ToJavaScript, Executor {
    1.16      private static final Logger LOG = Logger.getLogger(ScriptPresenter.class.getName());
    1.17      private final ScriptEngine eng;
    1.18 @@ -226,7 +226,12 @@
    1.19                  }
    1.20              }
    1.21          }
    1.22 -        exc.execute(new Wrap());
    1.23 +        final Runnable wrap = new Wrap();
    1.24 +        if (exc == null) {
    1.25 +            wrap.run();
    1.26 +        } else {
    1.27 +            exc.execute(wrap);
    1.28 +        }
    1.29      }
    1.30  
    1.31      private class FnImpl extends Fn {
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/boot-script/src/main/java/net/java/html/boot/script/Scripts.java	Thu Jun 05 20:27:54 2014 +0200
     2.3 @@ -0,0 +1,86 @@
     2.4 +/**
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     2.8 + *
     2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 + * Other names may be trademarks of their respective owners.
    2.11 + *
    2.12 + * The contents of this file are subject to the terms of either the GNU
    2.13 + * General Public License Version 2 only ("GPL") or the Common
    2.14 + * Development and Distribution License("CDDL") (collectively, the
    2.15 + * "License"). You may not use this file except in compliance with the
    2.16 + * License. You can obtain a copy of the License at
    2.17 + * http://www.netbeans.org/cddl-gplv2.html
    2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 + * specific language governing permissions and limitations under the
    2.20 + * License.  When distributing the software, include this License Header
    2.21 + * Notice in each file and include the License file at
    2.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 + * particular file as subject to the "Classpath" exception as provided
    2.24 + * by Oracle in the GPL Version 2 section of the License file that
    2.25 + * accompanied this code. If applicable, add the following below the
    2.26 + * License Header, with the fields enclosed by brackets [] replaced by
    2.27 + * your own identifying information:
    2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 + *
    2.30 + * Contributor(s):
    2.31 + *
    2.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    2.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    2.34 + *
    2.35 + * If you wish your version of this file to be governed by only the CDDL
    2.36 + * or only the GPL Version 2, indicate your decision by adding
    2.37 + * "[Contributor] elects to include this software in this distribution
    2.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.39 + * single choice of license, a recipient has the option to distribute
    2.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.41 + * to extend the choice of license to its licensees as provided above.
    2.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.43 + * Version 2 license, then the option applies only if the new code is
    2.44 + * made subject to such option by the copyright holder.
    2.45 + */
    2.46 +package net.java.html.boot.script;
    2.47 +
    2.48 +import java.util.concurrent.Executor;
    2.49 +import javax.script.ScriptEngine;
    2.50 +import org.apidesign.html.boot.spi.Fn.Presenter;
    2.51 +
    2.52 +/** Implementations of {@link Presenter}s that delegate
    2.53 + * to Java {@link ScriptEngine scripting} API.
    2.54 + * 
    2.55 + * @author Jaroslav Tulach
    2.56 + */
    2.57 +public final class Scripts {
    2.58 +    private Scripts() {
    2.59 +    }
    2.60 +    
    2.61 +    /** Simple implementation of {@link Presenter} that delegates
    2.62 +     * to Java {@link ScriptEngine scripting} API. The presenter runs headless
    2.63 +     * without appropriate simulation of browser APIs. Its primary usefulness
    2.64 +     * is inside testing environments. The presenter implements {@link Executor}
    2.65 +     * interface, but invokes all runnables passed to {@link Executor#execute(java.lang.Runnable)}
    2.66 +     * immediately.
    2.67 +     * 
    2.68 +     * @return new instance of a presenter that is using its own
    2.69 +     *   {@link ScriptEngine} for <code>text/javascript</code> mimetype
    2.70 +     */
    2.71 +    public static Presenter createPresenter() {
    2.72 +        return new ScriptPresenter(null);
    2.73 +    }
    2.74 +
    2.75 +    /** Simple implementation of {@link Presenter} that delegates
    2.76 +     * to Java {@link ScriptEngine scripting} API. The presenter runs headless
    2.77 +     * without appropriate simulation of browser APIs. Its primary usefulness
    2.78 +     * is inside testing environments. The presenter implements {@link Executor}
    2.79 +     * interface, and passes all runnables from {@link Executor#execute(java.lang.Runnable)}
    2.80 +     * to here in provided <code>exc</code> instance.
    2.81 +     * 
    2.82 +     * @param exc the executor to re-schedule all asynchronous requests to
    2.83 +     * @return new instance of a presenter that is using its own
    2.84 +     *   {@link ScriptEngine} for <code>text/javascript</code> mimetype
    2.85 +     */
    2.86 +    public static Presenter createPresenter(Executor exc) {
    2.87 +        return new ScriptPresenter(exc);
    2.88 +    }
    2.89 +}
     3.1 --- a/boot-script/src/test/java/net/java/html/boot/script/ko4j/KnockoutEnvJSTest.java	Thu Jun 05 19:42:03 2014 +0200
     3.2 +++ b/boot-script/src/test/java/net/java/html/boot/script/ko4j/KnockoutEnvJSTest.java	Thu Jun 05 20:27:54 2014 +0200
     3.3 @@ -59,7 +59,7 @@
     3.4  import java.util.concurrent.Executors;
     3.5  import net.java.html.BrwsrCtx;
     3.6  import net.java.html.boot.BrowserBuilder;
     3.7 -import net.java.html.boot.script.ScriptPresenter;
     3.8 +import net.java.html.boot.script.Scripts;
     3.9  import net.java.html.js.JavaScriptBody;
    3.10  import org.apidesign.html.boot.spi.Fn;
    3.11  import org.apidesign.html.context.spi.Contexts;
    3.12 @@ -107,7 +107,7 @@
    3.13          
    3.14          baseUri = DynamicHTTP.initServer();
    3.15          
    3.16 -        final ScriptPresenter p = new ScriptPresenter(KOCase.JS);
    3.17 +        final Fn.Presenter p = Scripts.createPresenter(KOCase.JS);
    3.18          InputStream is = KnockoutEnvJSTest.class.getResourceAsStream("env.nashorn.1.2-debug.js");
    3.19          p.loadScript(new InputStreamReader(is));
    3.20          is.close();