jtulach@699
|
1 |
/**
|
jtulach@699
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jtulach@699
|
3 |
*
|
jtulach@699
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jtulach@699
|
5 |
*
|
jtulach@699
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jtulach@699
|
7 |
* Other names may be trademarks of their respective owners.
|
jtulach@699
|
8 |
*
|
jtulach@699
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jtulach@699
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jtulach@699
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jtulach@699
|
12 |
* "License"). You may not use this file except in compliance with the
|
jtulach@699
|
13 |
* License. You can obtain a copy of the License at
|
jtulach@699
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jtulach@699
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jtulach@699
|
16 |
* specific language governing permissions and limitations under the
|
jtulach@699
|
17 |
* License. When distributing the software, include this License Header
|
jtulach@699
|
18 |
* Notice in each file and include the License file at
|
jtulach@699
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jtulach@699
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@699
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jtulach@699
|
22 |
* accompanied this code. If applicable, add the following below the
|
jtulach@699
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jtulach@699
|
24 |
* your own identifying information:
|
jtulach@699
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jtulach@699
|
26 |
*
|
jtulach@699
|
27 |
* Contributor(s):
|
jtulach@699
|
28 |
*
|
jtulach@699
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jtulach@699
|
30 |
* Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
|
jtulach@699
|
31 |
*
|
jtulach@699
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jtulach@699
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jtulach@699
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jtulach@699
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jtulach@699
|
36 |
* single choice of license, a recipient has the option to distribute
|
jtulach@699
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jtulach@699
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jtulach@699
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jtulach@699
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jtulach@699
|
41 |
* made subject to such option by the copyright holder.
|
jtulach@699
|
42 |
*/
|
jtulach@699
|
43 |
package net.java.html.boot.script;
|
jtulach@699
|
44 |
|
jaroslav@716
|
45 |
import java.io.Closeable;
|
jtulach@699
|
46 |
import java.util.concurrent.Executor;
|
jtulach@699
|
47 |
import javax.script.ScriptEngine;
|
jtulach@700
|
48 |
import net.java.html.boot.BrowserBuilder;
|
jtulach@700
|
49 |
import net.java.html.js.JavaScriptBody;
|
jtulach@838
|
50 |
import org.netbeans.html.boot.spi.Fn;
|
jtulach@838
|
51 |
import org.netbeans.html.boot.spi.Fn.Presenter;
|
jtulach@699
|
52 |
|
jtulach@699
|
53 |
/** Implementations of {@link Presenter}s that delegate
|
jtulach@700
|
54 |
* to Java {@link ScriptEngine scripting} API. Initialize your presenter
|
jtulach@700
|
55 |
* like this:
|
jtulach@700
|
56 |
*
|
jtulach@700
|
57 |
* <pre>
|
jtulach@700
|
58 |
*
|
jtulach@700
|
59 |
* {@link Runnable} <em>run</em> = ...; // your own init code
|
jaroslav@716
|
60 |
* {@link Presenter Fn.Presenter} <b>p</b> = Scripts.{@link Scripts#createPresenter()};
|
jaroslav@716
|
61 |
* BrowserBuilder.{@link BrowserBuilder#newBrowser(java.lang.Object...) newBrowser(<b>p</b>)}.
|
jtulach@700
|
62 |
* {@link BrowserBuilder#loadFinished(java.lang.Runnable) loadFinished(run)}.
|
jtulach@700
|
63 |
* {@link BrowserBuilder#showAndWait()};
|
jtulach@700
|
64 |
* </pre>
|
jtulach@700
|
65 |
*
|
jtulach@700
|
66 |
* and your runnable can make extensive use of {@link JavaScriptBody} directly or
|
jtulach@700
|
67 |
* indirectly via APIs using {@link JavaScriptBody such annotation} themselves.
|
jaroslav@716
|
68 |
* <p>
|
jaroslav@716
|
69 |
* Alternatively one can manipulate the presenter manually, which is
|
jaroslav@716
|
70 |
* especially useful when writing tests:
|
jaroslav@716
|
71 |
* <pre>
|
jaroslav@716
|
72 |
* {@code @Test} public void runInASimulatedBrowser() throws Exception {
|
jaroslav@716
|
73 |
* {@link Presenter Fn.Presenter} <b>p</b> = Scripts.{@link Scripts#createPresenter()};
|
jtulach@838
|
74 |
* try ({@link Closeable} c = {@link Fn#activate(org.netbeans.html.boot.spi.Fn.Presenter) Fn.activate}(<b>p</b>)) {
|
jaroslav@716
|
75 |
* // your code operating in context of <b>p</b>
|
jaroslav@716
|
76 |
* }
|
jaroslav@716
|
77 |
* }
|
jaroslav@716
|
78 |
* </pre>
|
jaroslav@716
|
79 |
* The previous code snippet requires Java 7 language syntax, as it relies
|
jaroslav@716
|
80 |
* on try-with-resources language syntactic sugar feature. The same block
|
jaroslav@716
|
81 |
* of code can be used on older versions of Java, but it is slightly more
|
jaroslav@716
|
82 |
* verbose.
|
jtulach@699
|
83 |
*
|
jtulach@699
|
84 |
* @author Jaroslav Tulach
|
jtulach@699
|
85 |
*/
|
jtulach@699
|
86 |
public final class Scripts {
|
jtulach@699
|
87 |
private Scripts() {
|
jtulach@699
|
88 |
}
|
jtulach@699
|
89 |
|
jtulach@699
|
90 |
/** Simple implementation of {@link Presenter} that delegates
|
jtulach@699
|
91 |
* to Java {@link ScriptEngine scripting} API. The presenter runs headless
|
jtulach@699
|
92 |
* without appropriate simulation of browser APIs. Its primary usefulness
|
jtulach@699
|
93 |
* is inside testing environments. The presenter implements {@link Executor}
|
jtulach@699
|
94 |
* interface, but invokes all runnables passed to {@link Executor#execute(java.lang.Runnable)}
|
jtulach@699
|
95 |
* immediately.
|
jtulach@699
|
96 |
*
|
jtulach@699
|
97 |
* @return new instance of a presenter that is using its own
|
jtulach@699
|
98 |
* {@link ScriptEngine} for <code>text/javascript</code> mimetype
|
jtulach@699
|
99 |
*/
|
jtulach@699
|
100 |
public static Presenter createPresenter() {
|
jtulach@699
|
101 |
return new ScriptPresenter(null);
|
jtulach@699
|
102 |
}
|
jtulach@699
|
103 |
|
jtulach@700
|
104 |
/** Implementation of {@link Presenter} that delegates
|
jtulach@700
|
105 |
* to Java {@link ScriptEngine scripting} API and can control execution
|
jtulach@700
|
106 |
* thread. The presenter runs headless
|
jtulach@699
|
107 |
* without appropriate simulation of browser APIs. Its primary usefulness
|
jtulach@699
|
108 |
* is inside testing environments. The presenter implements {@link Executor}
|
jtulach@699
|
109 |
* interface, and passes all runnables from {@link Executor#execute(java.lang.Runnable)}
|
jtulach@699
|
110 |
* to here in provided <code>exc</code> instance.
|
jtulach@699
|
111 |
*
|
jtulach@699
|
112 |
* @param exc the executor to re-schedule all asynchronous requests to
|
jtulach@699
|
113 |
* @return new instance of a presenter that is using its own
|
jtulach@699
|
114 |
* {@link ScriptEngine} for <code>text/javascript</code> mimetype
|
jtulach@699
|
115 |
*/
|
jtulach@699
|
116 |
public static Presenter createPresenter(Executor exc) {
|
jtulach@699
|
117 |
return new ScriptPresenter(exc);
|
jtulach@699
|
118 |
}
|
jtulach@699
|
119 |
}
|