Class EvaluableScript

All Implemented Interfaces:
Component, DestructableComponent, InitializableComponent

public final class EvaluableScript extends AbstractInitializableComponent
This is a helper class that takes care of reading in, optionally compiling, and evaluating a script.
  • Field Details

    • scriptLanguage

      @Nonnull @NotEmpty private String scriptLanguage
      The scripting language.
    • script

      The script to execute.
    • scriptEngine

      @NonnullAfterInit private ScriptEngine scriptEngine
      The script engine to execute the script.
    • compiledScript

      @Nullable private CompiledScript compiledScript
      The compiled form of the script, if the script engine supports compiling.
    • log

      @Nonnull private final org.slf4j.Logger log
      The log.
  • Constructor Details

    • EvaluableScript

      public EvaluableScript()
      Constructor.
  • Method Details

    • getScript

      @NonnullAfterInit @NotEmpty public String getScript()
      Gets the script source.
      Returns:
      the script source
    • setScript

      public void setScript(@Nonnull @NotEmpty String what)
      Sets the script source.
      Parameters:
      what - the script source
    • setScript

      public void setScript(@Nonnull InputStream scriptSource) throws IOException
      Sets the script source.
      Parameters:
      scriptSource - how to get the script source
      Throws:
      IOException - if there were issues reading the script
    • setScript

      public void setScript(@Nonnull File scriptSource) throws IOException
      Sets the script source.
      Parameters:
      scriptSource - how to get the script source
      Throws:
      IOException - if there were issues reading the script
    • setScript

      public void setScript(@Nonnull Resource scriptSource) throws IOException
      Sets the script source.
      Parameters:
      scriptSource - how to get the script source
      Throws:
      IOException - if there were issues reading the script
    • getScriptLanguage

      @Nonnull @NotEmpty public String getScriptLanguage()
      Gets the script language.
      Returns:
      the script language
    • setEngineName

      public void setEngineName(@Nonnull @NotEmpty String what)
      Sets the script language.
      Parameters:
      what - the script language
    • eval

      @Nullable public Object eval(@Nonnull Bindings scriptBindings) throws ScriptException
      Evaluates this script against the given bindings.
      Parameters:
      scriptBindings - the script bindings
      Returns:
      the result of the script or null if the script did not return a result
      Throws:
      ScriptException - thrown if there was a problem evaluating the script
    • eval

      @Nullable public Object eval(@Nonnull ScriptContext scriptContext) throws ScriptException
      Evaluates this script against the given context.
      Parameters:
      scriptContext - the script context
      Returns:
      the result of the script or null if the script did not return a result
      Throws:
      ScriptException - thrown if there was a problem evaluating the script
    • doInitialize

      protected void doInitialize() throws ComponentInitializationException
      Performs the initialization of the component. This method is executed within the lock on the object being initialized. The default implementation of this method is a no-op. Initializes the scripting engine and compiles the script, if possible.
      Overrides:
      doInitialize in class AbstractInitializableComponent
      Throws:
      ComponentInitializationException - if the scripting engine supports compilation and the script does not compile