Implementation:APIJSON APIJSON JSR223ScriptExecutor execute
| Knowledge Sources | |
|---|---|
| Domains | Script_Execution, JSR223, Sandboxing |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
Abstract base class for JSR-223 script engine execution that compiles, caches, and evaluates script functions (JavaScript, Lua, Python) with sandboxing via Nashorn ClassFilter, wrapping the standard javax.script API with APIJSON-specific context injection.
Description
JSR223ScriptExecutor is an abstract class that implements the ScriptExecutor interface using the JSR-223 scripting framework. It provides:
- Script compilation and caching: Scripts are compiled once via Compilable.compile() and cached in a ConcurrentHashMap (compiledScriptMap) for repeated execution
- Sandboxed Nashorn engine: For JavaScript, it creates a Nashorn engine with a ClassFilter that blocks Java class access by default (isClassExposureAllowed returns false), preventing script injection attacks
- Context injection: Binds extParam (extended parameters) and _meta (version, tag, args) into the script's Bindings
- Script conversion: Subclasses can override convertScript() to transform script source before compilation
This is a Wrapper Doc: it wraps the external JSR-223 javax.script API with APIJSON-specific script loading, caching, and execution patterns. Concrete subclasses (JavaScriptExecutor, etc.) provide the scriptEngineName() and extendParameter() implementations.
External Reference
Usage
Subclass JSR223ScriptExecutor and implement scriptEngineName(), extendParameter(), and isLockScript(). Register the executor in SCRIPT_EXECUTOR_MAP on AbstractFunctionParser. Enable script functions by setting ENABLE_SCRIPT_FUNCTION = true.
Code Reference
Source Location
- Repository: APIJSON
- File: src/main/java/apijson/orm/script/JSR223ScriptExecutor.java
Signature
public abstract class JSR223ScriptExecutor<T, M extends Map<String, Object>, L extends List<Object>>
implements ScriptExecutor<T, M, L> {
// Initialize the script engine
public ScriptExecutor<T, M, L> init()
// Compile and cache a script
public void load(String name, String script)
// Execute a cached script with context bindings
public Object execute(
AbstractFunctionParser<T, M, L> parser,
Map<String, Object> currentObject,
String methodName,
Object[] args
) throws Exception
// Clear the compiled script cache
public void cleanCache()
// Subclass: return engine name (e.g., "javascript", "lua", "python")
protected abstract String scriptEngineName();
// Subclass: provide extended parameters for script context
protected abstract Object extendParameter(
AbstractFunctionParser<T, M, L> parser,
Map<String, Object> currentObject,
String methodName,
Object[] args
);
// Subclass: whether to lock script execution (synchronous)
protected abstract boolean isLockScript(String methodName);
}
Import
import apijson.orm.script.JSR223ScriptExecutor;
import apijson.orm.script.ScriptExecutor;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| parser | AbstractFunctionParser<T,M,L> | Yes | Provides version, tag, method context |
| currentObject | Map<String,Object> | Yes | Current row data |
| methodName | String | Yes | Script function name |
| args | Object[] | Yes | Function arguments |
| ENABLE_SCRIPT_FUNCTION | boolean | Yes | Master switch (default false) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Object | Script evaluation result |
Usage Examples
JavaScript Executor Subclass
public class MyJavaScriptExecutor extends JSR223ScriptExecutor<Long, JSONObject, JSONArray> {
@Override
protected String scriptEngineName() {
return "javascript";
}
@Override
protected Object extendParameter(
AbstractFunctionParser<Long, JSONObject, JSONArray> parser,
Map<String, Object> currentObject,
String methodName,
Object[] args) {
// Provide additional context to the script
Map<String, Object> params = new HashMap<>();
params.put("request", parser.getRequest());
params.put("method", parser.getMethod().name());
return params;
}
@Override
protected boolean isLockScript(String methodName) {
return true; // Synchronous execution
}
}
// Register the executor:
AbstractFunctionParser.SCRIPT_EXECUTOR_MAP.put("javascript", new MyJavaScriptExecutor().init());
// Load a script (from Script table):
// executor.load("multiply", "function multiply(a, b) { return a * b; }");
// Execute:
// Object result = executor.execute(parser, currentObj, "multiply", new Object[]{100, 3});
// result = 300
Nashorn Sandboxing
// The createScriptEngine() method creates a sandboxed Nashorn engine:
// ClassFilter filter = (className) -> isClassExposureAllowed(className);
// Default: isClassExposureAllowed returns false for ALL classes
// This prevents scripts from accessing Java classes like java.lang.Runtime
// To allow specific classes, override:
@Override
protected boolean isClassExposureAllowed(String className) {
// Only allow safe utility classes
return "java.lang.Math".equals(className) || "java.util.Date".equals(className);
}