Class GroovyEngine

java.lang.Object
org.codelibs.fess.script.AbstractScriptEngine
org.codelibs.fess.script.groovy.GroovyEngine
All Implemented Interfaces:
ScriptEngine

public class GroovyEngine extends AbstractScriptEngine
Groovy script engine implementation that extends AbstractScriptEngine. This class provides support for executing Groovy scripts with parameter binding and DI container integration.

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

    Fields
    Modifier and Type
    Field
    Description
    protected int
    Maximum length of script text included in warning log messages.
    protected boolean
    Whether to log script execution details for auditing purposes.
    protected int
    Maximum number of compiled scripts to cache.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor for GroovyEngine.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes all cached GroovyClassLoaders and clears the script cache.
    evaluate(String template, Map<String,Object> paramMap)
    Evaluates a Groovy script template with the provided parameters.
    protected org.codelibs.fess.opensearch.config.exentity.ScheduledJob
    Gets the current scheduled job from the thread-local job runtime.
    protected String
    Returns the name identifier for this script engine.
    void
    Rebuilds the script cache after DI injection.
    protected void
    logScriptExecution(String script, String result)
    Logs script execution to the audit log.
    void
    setMaxScriptLogLength(int maxScriptLogLength)
    Sets the maximum length of script text included in warning log messages.
    void
    setScriptCacheSize(int scriptCacheSize)
    Sets the maximum number of compiled scripts to cache.

    Methods inherited from class org.codelibs.fess.script.AbstractScriptEngine

    register

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • scriptCacheSize

      protected int scriptCacheSize
      Maximum number of compiled scripts to cache. Configurable via DI.
    • maxScriptLogLength

      protected int maxScriptLogLength
      Maximum length of script text included in warning log messages. Configurable via DI.
    • scriptAuditLogEnabled

      protected boolean scriptAuditLogEnabled
      Whether 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

      public Object evaluate(String template, Map<String,Object> paramMap)
      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

      protected String getName()
      Returns the name identifier for this script engine.
      Specified by:
      getName in class AbstractScriptEngine
      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

      protected void logScriptExecution(String script, String result)
      Logs script execution to the audit log.
      Parameters:
      script - the script content that was executed
      result - the execution result (e.g., "success" or "failure:ExceptionType")