public final class Scripts
extends java.lang.Object
Fn.Presenter
that delegates
to Java scripting
API. Initialize your presenter
like this:
and your runnable can make extensive use ofRunnable
run = () -> { executed[0] = "OK"; };Fn
.Presenter p =Scripts
.newPresenter().build();BrowserBuilder
.newBrowser(p) .loadFinished(run) .loadPage("empty.html") .showAndWait();
JavaScriptBody
directly or
indirectly via APIs using such annotation
themselves.
Alternatively one can manipulate the presenter manually, which is especially useful when writing tests:
@JavaScriptBody
(args = { "a", "b" }, body = "return a * b;") private static native int mul(int a, int b); private static int activatePresenterDirectly() throwsException
{Fn
.Presenter p =Scripts
.newPresenter().build(); try (Closeable
c =Fn
.activate(p)) { int fortyTwo = mul(2, mul(7, 3)); assert fortyTwo == 42; return fortyTwo; } }
The previous code snippet relies on try-with-resources Java7 syntax. The same block of code can be used on older versions of Java, but it is slightly more verbose.
Modifier and Type | Method and Description |
---|---|
Fn.Presenter |
build()
Builds new instance of the scripting presenter.
|
Scripts |
engine(javax.script.ScriptEngine engine)
Associates a scripting engine.
|
Scripts |
executor(java.util.concurrent.Executor exc)
Associates new executor.
|
static Scripts |
newPresenter()
Creates new scripting
Fn.Presenter builder. |
Scripts |
sanitize(boolean yesOrNo)
Turn sandboxing of the engine on or off.
|
public static Scripts newPresenter()
Fn.Presenter
builder. Simplest way
to use is:
Fn
.Presenter presenter =Scripts
.newPresenter().build();
It is possible to specify own
scripting engine
and Scripts.executor(java.util.concurrent.Executor)
and control the thread that executes the scripts:
return Scripts
.newPresenter()
.engine(engine)
.executor(someExecutor)
.build();
By default the created presenters are sanitized.
public Scripts executor(java.util.concurrent.Executor exc)
Executor
interface, and passes all runnables from its own
Executor.execute(java.lang.Runnable)
method
to here in provided exc
instance of executor.exc
- dedicated executor to usepublic Scripts engine(javax.script.ScriptEngine engine)
build
an
implementation of Fn.Presenter
that delegates
to Java scripting
API. The presenter runs headless
without appropriate simulation of browser APIs.engine
- dedicated script engine to usepublic Scripts sanitize(boolean yesOrNo)
yesOrNo
- do the sanitization or notpublic Fn.Presenter build()
Copyright © 2021 The Apache Software Foundation. All rights reserved.