Implementation:APIJSON APIJSON Demo NashornScriptExecutor
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Scripting, Extensibility |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Concrete tool for executing server-side JavaScript functions via the Nashorn script engine (JSR 223), provided by the APIJSON Demo's Script module.
Description
The NashornScriptExecutor class extends JSR223ScriptExecutor and uses the Nashorn JavaScript engine (via NashornScriptEngineFactory) to execute server-side scripts. It provides:
- No per-script locking:
isLockScript()returnsfalse, meaning Nashorn handles thread safety internally without per-script synchronization. - Extension parameter:
extendParameter()returns a map with a"data"key, demonstrating how custom data can be passed to scripts. - Engine configuration: Initializes with
-doeand--global-per-engineflags for error display and shared global scope.
Usage
- Registering: Via
SCRIPT_EXECUTOR_MAP.put("nashorn", new NashornScriptExecutor().init())(or via database configuration). - Executing: The framework calls
execute()(inherited fromJSR223ScriptExecutor) when akey()function references a Nashorn-registered script.
Code Reference
Source Location
- Repository: APIJSON-Demo
- File: APIJSON-Java-Server/APIJSONDemo-Script/src/main/java/apijson/demo/script/NashornScriptExecutor.java
Signature
public class NashornScriptExecutor extends JSR223ScriptExecutor {
@Override
public ScriptExecutor init(); // Creates NashornScriptEngine
@Override
protected String scriptEngineName(); // Returns "nashornJS"
@Override
protected Object extendParameter(AbstractFunctionParser parser,
JSONObject currentObject, String methodName, Object[] args);
@Override
protected boolean isLockScript(String methodName); // Returns false (no locking)
}
Import
import apijson.demo.script.NashornScriptExecutor;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | String | Yes | Script function name (cache key) |
| script | String | Yes (load) | JavaScript source code |
| parser | AbstractFunctionParser | Yes | Function parser context |
| methodName | String | Yes | Function name to execute |
| args | Object[] | Yes | Function arguments |
Outputs
| Name | Type | Description |
|---|---|---|
| execute return | Object | Result of script execution (type depends on script) |
Usage Examples
Registration
NashornScriptExecutor executor = new NashornScriptExecutor();
executor.init();
APIJSONFunctionParser.SCRIPT_EXECUTOR_MAP.put("nashorn", executor);
// Load a script
executor.load("greet", "function greet(name) { return 'Hello, ' + name; }");
// Execute
Object result = executor.execute(parser, curObj, "greet", new Object[]{"World"});
// result = "Hello, World"
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment