Class GroovyEngine
- All Implemented Interfaces:
ScriptEngine
Thread Safety: This class is thread-safe. Each cached entry holds its own GroovyClassLoader. The cache uses Guava Cache with segment-based locking for lock-free concurrent reads. Each evaluate() call creates a new Script instance to ensure thread isolation of bindings.
Note on class-level isolation: Compiled Script classes are cached and reused. Class-level state (static fields, metaclass mutations) persists across evaluations of the same script. In Fess, scripts are short expressions configured by administrators (e.g., "data1 > 10", "10 * boost1 + boost2") and do not use static state, so this is acceptable.
Resource Management: Each cached entry's GroovyClassLoader is closed on eviction via RemovalListener. All remaining entries are cleaned up via close() (@PreDestroy).
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intMaximum length of script text included in warning log messages.protected booleanWhether to log script execution details for auditing purposes.protected intMaximum number of compiled scripts to cache. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes all cached GroovyClassLoaders and clears the script cache.Evaluates a Groovy script template with the provided parameters.protected org.codelibs.fess.opensearch.config.exentity.ScheduledJobGets the current scheduled job from the thread-local job runtime.protected StringgetName()Returns the name identifier for this script engine.voidinit()Rebuilds the script cache after DI injection.protected voidlogScriptExecution(String script, String result) Logs script execution to the audit log.voidsetMaxScriptLogLength(int maxScriptLogLength) Sets the maximum length of script text included in warning log messages.voidsetScriptCacheSize(int scriptCacheSize) Sets the maximum number of compiled scripts to cache.Methods inherited from class org.codelibs.fess.script.AbstractScriptEngine
register
-
Field Details
-
scriptCacheSize
protected int scriptCacheSizeMaximum number of compiled scripts to cache. Configurable via DI. -
maxScriptLogLength
protected int maxScriptLogLengthMaximum length of script text included in warning log messages. Configurable via DI. -
scriptAuditLogEnabled
protected boolean scriptAuditLogEnabledWhether to log script execution details for auditing purposes. Configurable via DI.
-
-
Constructor Details
-
GroovyEngine
public GroovyEngine()Default constructor for GroovyEngine.
-
-
Method Details
-
init
@PostConstruct public void init()Rebuilds the script cache after DI injection. Called by the DI container after property injection. -
setScriptCacheSize
public void setScriptCacheSize(int scriptCacheSize) Sets the maximum number of compiled scripts to cache.- Parameters:
scriptCacheSize- the cache size
-
setMaxScriptLogLength
public void setMaxScriptLogLength(int maxScriptLogLength) Sets the maximum length of script text included in warning log messages.- Parameters:
maxScriptLogLength- the max length
-
evaluate
Evaluates a Groovy script template with the provided parameters.This method caches compiled Script classes per script text. Each evaluation creates a new Script instance to ensure thread-safe binding isolation. The DI container is automatically injected into the binding map as "container".
- Parameters:
template- the Groovy script to evaluate (null-safe, returns null if empty)paramMap- the parameters to bind to the script (null-safe, treated as empty map if null)- Returns:
- the result of script evaluation, or null if the template is empty or evaluation fails
- Throws:
JobProcessingException- if the script explicitly throws this exception (allows scripts to signal job-specific errors that should propagate)
-
close
@PreDestroy public void close()Closes all cached GroovyClassLoaders and clears the script cache. Called by the DI container on shutdown. -
getName
Returns the name identifier for this script engine.- Specified by:
getNamein classAbstractScriptEngine- Returns:
- "groovy" - the identifier used to register and retrieve this engine
-
getCurrentScheduledJob
protected org.codelibs.fess.opensearch.config.exentity.ScheduledJob getCurrentScheduledJob()Gets the current scheduled job from the thread-local job runtime.- Returns:
- the scheduled job if available, null otherwise
-
logScriptExecution
Logs script execution to the audit log.- Parameters:
script- the script content that was executedresult- the execution result (e.g., "success" or "failure:ExceptionType")
-