Class DefaultScriptEngineResolver

java.lang.Object
org.camunda.bpm.engine.impl.scripting.engine.DefaultScriptEngineResolver
All Implemented Interfaces:
ScriptEngineResolver

public class DefaultScriptEngineResolver extends Object implements ScriptEngineResolver
  • Field Details

    • scriptEngineManager

      protected final ScriptEngineManager scriptEngineManager
    • cachedEngines

      protected final Map<String,ScriptEngine> cachedEngines
    • sharedPolyglotEngines

      protected final Map<String,Object> sharedPolyglotEngines
      The GraalJS org.graalvm.polyglot.Engines this resolver hands out script engines for, by configuration key - see getSharedPolyglotEngine(). Typed Object because GraalJS classes are not a compile dependency of this module.
  • Constructor Details

    • DefaultScriptEngineResolver

      public DefaultScriptEngineResolver(ScriptEngineManager scriptEngineManager)
  • Method Details

    • addScriptEngineFactory

      public void addScriptEngineFactory(ScriptEngineFactory scriptEngineFactory)
      Specified by:
      addScriptEngineFactory in interface ScriptEngineResolver
    • getScriptEngineManager

      public ScriptEngineManager getScriptEngineManager()
      Specified by:
      getScriptEngineManager in interface ScriptEngineResolver
    • getScriptEngine

      public ScriptEngine getScriptEngine(String language, boolean resolveFromCache)
      Returns a cached script engine or creates a new script engine if no such engine is currently cached.

      GraalJS is deliberately never cached here - see issue #3174. It reports THREADING = null via the JSR-223 factory parameter (see isCachable(javax.script.ScriptEngine)), so a fresh engine is created for every call, and the caller is responsible for explicitly releasing it once done (see ScriptingEngines.disposeScriptEngine(javax.script.ScriptEngine)).

      An engine-level cache was tried twice for GraalJS (issues #3101, #3148) and both attempts had to be reverted: GraalJS never releases the native/heap-resident Truffle state behind a discarded engine via garbage collection alone, so any cache needs to explicitly close evicted entries - but GraalJS also enforces thread affinity (a Context is bound to its creating thread) and, separately, BPMN/DMN each keep their own long-lived cache of a CompiledScript tied to whichever engine compiled it (see SourceExecutableScript/CompiledExecutableScript and DMN's CachedCompiledScriptSupport). Any engine-level cache's own eviction timing (LRU capacity, or a GraalJS config change - see issue #3174) can end up closing an engine that one of those long-lived compiled-script caches still depends on, since the two caches have no way to coordinate their lifetimes. Not caching at all avoids this entirely: the decision of whether an engine may be closed is made once, locally, by whichever call site just used it - see ScriptingEngines.disposeScriptEngine(javax.script.ScriptEngine).

      Specified by:
      getScriptEngine in interface ScriptEngineResolver
      Parameters:
      language - the language (such as 'groovy' for the script engine)
      Returns:
      the cached engine or null if no script engine can be created for the given language
    • isJsEngineNameStale

      protected boolean isJsEngineNameStale(ScriptEngine cached, ProcessEngineConfigurationImpl config)
      Returns true if the cached engine's factory no longer matches the effective JavaScript engine name for the current configuration.

      The effective name is determined as follows:

      1. If scriptEngineNameJavaScript is explicitly configured, that name is used.
      2. Otherwise, if the default JavaScript engine (ScriptingEngines.DEFAULT_JS_SCRIPTING_LANGUAGE, i.e. GraalJS) is available on the classpath, that is the effective name — a cached engine from a previous explicit configuration that has since been reset to null is therefore correctly detected as stale.
      3. Otherwise (default engine unavailable — fallback path in getJavaScriptScriptEngine(java.lang.String) is in effect), the cached fallback engine is treated as not stale to prevent unnecessary recreation on every lookup.

      If config is null (no process engine context available), the entry is conservatively treated as not stale, mirroring the pre-existing behavior for this edge case.

    • isDefaultJsEngineAvailable

      protected boolean isDefaultJsEngineAvailable()
      Returns true if the default JavaScript engine (ScriptingEngines.DEFAULT_JS_SCRIPTING_LANGUAGE, i.e. GraalJS) appears in the JDK ServiceLoader/SPI metadata already visible to the ScriptEngineManager, without instantiating a new engine. Used by isJsEngineNameStale(javax.script.ScriptEngine, org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl) to correctly distinguish "no explicit engine configured, use the default" from "default unavailable, fallback is in effect".

      Treats a null ScriptEngineManager.getEngineFactories() result (never returned by the JDK's own implementation, but possible for a custom/mocked ScriptEngineManager) the same as an empty one - i.e. the default engine is unavailable.

    • isJavaScriptAlias

      protected boolean isJavaScriptAlias(String language)
    • getScriptEngine

      protected ScriptEngine getScriptEngine(String language)
    • getJavaScriptScriptEngine

      protected ScriptEngine getJavaScriptScriptEngine(String language)
    • getEngineByName

      protected ScriptEngine getEngineByName(String name)
      Resolves a script engine by name. GraalJS is backed by an org.graalvm.polyglot.Engine owned by this resolver (see getSharedPolyglotEngine()) instead of going through scriptEngineManager.getEngineByName(...) directly - see issue #3174.

      GraalJSScriptEngine.create() (which is what ScriptEngineManager calls internally when no explicit Engine is supplied) participates in GraalVM's JVM-wide default Engine, which this resolver can neither configure nor ever close. Supplying the Engine explicitly keeps its lifecycle - and its configuration - under the control of whoever owns this resolver.

      What has to be released after an execution is not the Engine but the polyglot Contexts created for it: one per Bindings the script was evaluated against. They are closed by ScriptingEngines.disposeBindings(ScriptEngine, javax.script.Bindings) and ScriptingEngines.disposeScriptEngine(ScriptEngine). A Context that is left open stays reachable through its Engine for as long as that lives, which is the unbounded growth reported in issue #3174.

      name is matched against every alias GraalJS's own factory registers (e.g. "js", "graal.js", "ecmascript" - not just the canonical ScriptingEngines.GRAAL_JS_SCRIPT_ENGINE_NAME), via isGraalJsAlias(String), so any of them - whether from a script task/DMN literal expression's language attribute or an explicitly configured scriptEngineNameJavaScript - reaches the substitution below.

      That match is deliberately metadata-only (ScriptEngineFactory.getNames()), never by first asking scriptEngineManager for an actual engine: doing the latter would itself construct a real GraalJSScriptEngine just to inspect it.

      Falls back to the plain ScriptEngineManager lookup for every other language, and for GraalJS itself if no matching factory is registered with scriptEngineManager (e.g. a classloader-isolated deployment where the caller-supplied manager has no GraalJS factory registered, even though GraalJS classes are reflectively loadable from this module's own classloader - see ScriptEngineNameJavaScriptTest#shouldFallbackToAnyEngineForJavaScriptIfDefaultUnavailable), or if the reflective construction itself fails (e.g. an incompatible GraalJS version without the expected API).

    • isGraalJsAlias

      protected boolean isGraalJsAlias(String name)
      Returns true if scriptEngineManager has a GraalJS factory registered whose ScriptEngineFactory.getNames() contains name (case-insensitively) - i.e. name is any alias GraalJS itself registers, not just ScriptingEngines.GRAAL_JS_SCRIPT_ENGINE_NAME. Purely metadata-based - never instantiates a ScriptEngine - see getEngineByName(String).
    • createGraalJsScriptEngineOnSharedEngine

      protected ScriptEngine createGraalJsScriptEngineOnSharedEngine()
      Constructs a GraalJS ScriptEngine on this resolver's shared org.graalvm.polyglot.Engine (see getSharedPolyglotEngine()). Passing null for the Context.Builder argument lets GraalJSScriptEngine build its own default set of Context options (the same ones a plain ScriptEngineManager#getEngineByName("Graal.js") lookup would use), so behavior is unaffected except for which Engine backs it.

      A new ScriptEngine is created for every call - it is a cheap wrapper, and the options applied in configureGraalJsScriptEngine(javax.script.ScriptEngine) are specific to it. What the shared Engine saves is parsing and warm-up: scripts that were already seen are not parsed again.

      Accessed via reflection because org.graalvm.polyglot/GraalJS classes are only a test-scope dependency of this module - see getScriptEngine(String, boolean) Javadoc.

      Returns:
      the constructed engine, or null if this GraalJS version doesn't expose the expected Engine.Builder/GraalJSScriptEngine.create(Engine, Context.Builder) API, in which case the caller falls back to the plain ScriptEngineManager lookup.
    • getSharedPolyglotEngine

      protected Object getSharedPolyglotEngine() throws ReflectiveOperationException
      Returns the org.graalvm.polyglot.Engine GraalJS script engines of this resolver run on, creating it on first use.

      There is one Engine per distinct script engine configuration (see getPolyglotEngineKey(org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl)) rather than a single one: all Contexts of an Engine must agree on host access, and GraalJS rejects a Context that does not with IllegalStateException: Found different host access configuration for a context with a shared engine. The configuration is read from the process engine configuration, which a deployment normally never changes - but tests do, and a process application may be used by engines configured differently.

      The Engines are never closed: an Engine without open Contexts holds nothing that is not released by garbage collection once the resolver itself is unreachable.

      Throws:
      ReflectiveOperationException
    • buildPolyglotEngine

      protected Object buildPolyglotEngine() throws ReflectiveOperationException
      Throws:
      ReflectiveOperationException
    • getPolyglotEngineKey

      protected String getPolyglotEngineKey(ProcessEngineConfigurationImpl config)
      Returns:
      the key identifying the Engine to use for the given configuration: every option configureGraalJsScriptEngine(javax.script.ScriptEngine) applies to a script engine, so that Contexts sharing an Engine are always configured alike.
    • isCachable

      protected boolean isCachable(ScriptEngine scriptEngine)
      Allows checking whether the script engine can be cached. Cache if the engine declares thread-safety via the THREADING factory parameter.

      GraalJS reports THREADING = null, so it is - deliberately - never cached here. See the getScriptEngine(String, boolean) Javadoc for why.

      Parameters:
      scriptEngine - the script engine to check.
      Returns:
      true if the script engine may be cached.
    • configureScriptEngines

      protected void configureScriptEngines(String language, ScriptEngine scriptEngine)
    • isGraalJs

      protected boolean isGraalJs(ScriptEngine scriptEngine)
      Returns:
      true if scriptEngine's factory reports the canonical GraalJS engine name. Unlike isGraalJsAlias(String), this checks an already-resolved engine's own (fixed, alias-independent) ScriptEngineFactory.getEngineName(), so a plain equals is sufficient here.
    • configureGroovyScriptEngine

      protected void configureGroovyScriptEngine(ScriptEngine scriptEngine)
      Allows providing custom configuration for the groovy script engine.
      Parameters:
      scriptEngine - the groovy script engine to configure.
    • configureGraalJsScriptEngine

      protected void configureGraalJsScriptEngine(ScriptEngine scriptEngine)
      Allows providing custom configuration for the Graal JS script engine.
      Parameters:
      scriptEngine - the Graal JS script engine to configure.