Class EvaluableScript


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

      • scriptLanguage

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

        @Nonnull
        @NotEmpty
        private final String script
        The script to execute.
      • scriptEngine

        @Nullable
        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.
    • Constructor Detail

      • EvaluableScript

        public EvaluableScript​(@ParameterName(name="engineName") @Nonnull @NotEmpty
                               String engineName,
                               @ParameterName(name="scriptSource") @Nonnull
                               Resource scriptSource)
                        throws ScriptException
        Constructor.
        Parameters:
        engineName - the JSR-223 scripting engine name
        scriptSource - the script source
        Throws:
        ScriptException - thrown if the script source file can not be read or the scripting engine supports compilation and the script does not compile
        Since:
        8.0.0
      • EvaluableScript

        public EvaluableScript​(@ParameterName(name="scriptSource") @Nonnull
                               Resource scriptSource)
                        throws ScriptException
        Constructor.
        Parameters:
        scriptSource - the script source
        Throws:
        ScriptException - thrown if the script source file can not be read or the scripting engine supports compilation and the script does not compile
        Since:
        8.0.0
      • EvaluableScript

        public EvaluableScript​(@ParameterName(name="engineName") @Nonnull @NotEmpty
                               String engineName,
                               @ParameterName(name="scriptSource") @Nonnull
                               InputStream scriptSource)
                        throws ScriptException
        Constructor. The provided stream is not closed.
        Parameters:
        engineName - the JSR-223 scripting engine name
        scriptSource - the script source
        Throws:
        ScriptException - thrown if the script source file can not be read or the scripting engine supports compilation and the script does not compile
      • EvaluableScript

        public EvaluableScript​(@ParameterName(name="scriptSource") @Nonnull
                               InputStream scriptSource)
                        throws ScriptException
        Constructor. The provided stream is not closed.
        Parameters:
        scriptSource - the script source
        Throws:
        ScriptException - thrown if the script source file can not be read or the scripting engine supports compilation and the script does not compile
        Since:
        8.0.0
      • EvaluableScript

        public EvaluableScript​(@ParameterName(name="engineName") @Nonnull @NotEmpty
                               String engineName,
                               @ParameterName(name="scriptSource") @Nonnull
                               File scriptSource)
                        throws ScriptException
        Constructor.
        Parameters:
        engineName - the JSR-223 scripting engine name
        scriptSource - the script source
        Throws:
        ScriptException - thrown if the script source file can not be read or the scripting engine supports compilation and the script does not compile
      • EvaluableScript

        public EvaluableScript​(@ParameterName(name="scriptSource") @Nonnull
                               File scriptSource)
                        throws ScriptException
        Constructor.
        Parameters:
        scriptSource - the script source
        Throws:
        ScriptException - thrown if the script source file can not be read or the scripting engine supports compilation and the script does not compile
        Since:
        8.0.0
    • Method Detail

      • getScript

        @Nonnull
        @NotEmpty
        public String getScript()
        Gets the script source.
        Returns:
        the script source
      • getScriptLanguage

        @Nonnull
        @NotEmpty
        public String getScriptLanguage()
        Gets the script language.
        Returns:
        the script source
      • 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
      • initialize

        private void initialize()
                         throws ScriptException
        Initializes the scripting engine and compiles the script, if possible.
        Throws:
        ScriptException - thrown if the scripting engine supports compilation and the script does not compile