Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Script context



It is possible to use a global context in scripts. Note that it is not necessary to do anything to set the context for scripts, the framework will do it "under the hood".

The context is available through the context field in your script. For example:
      public void apply() {
         context.echo("Hello World");
      }

Context interface

The context must have the following interface:

public class ScriptContext


Modifier and Type Method and Description
void abort(String message)
Abort the current script. The Scripting engine will use the throwable exception to stop the execution
void echo(String message)
Echo a message
void echo(Number number)
Echo a numeric value
void error(String message)
Error a message
ScriptHelper getHelper()
Return the script helper. Return null by default
File getPath(String path)
Return the absolute path of a file defined relative to the script file
File getScriptFile()
Return the script file
ScriptLogger getScriptLogger()
Return the associated ScriptLogger

Note that abort(String message) will abort the script. It can be listened by an Exception listener

Constraint on script languages

If the ScriptWrapper.isMultiImplementSupported() method returns true, then the scripting language implementation support having one class implementing more than one Java interface. In that case, there is no constraint on the Script.

If the ScriptWrapper.isMultiImplementSupported() method returns false, to be able to use the context, the script interface must extend the ContextListener interface. For example:
      public interface Script extends ContextListener {
        public int computeResult(int value) {
        }
      }
Note that:
  • The Groovy implementation support the implementation of multiple interfaces
  • The Ruby implementation does not support the implementation of multiple interfaces

Using the context

The context is available as a context field in the script. For example to show a message on the logger:
      public void execute(int value) {
        context.echo("My value:" + value);
      }

Setting a custom context

It is possible to set a context with any class implementing the ScriptContext interface by calling the: Note that you can override the DefaultScriptContext class.

You will not need to do anything special in that case in your script, which will have your custom class imported by default.

Adding a Script helper

Main Article: Script helper

If you want to have a script helper, you must implement the ScriptContext.getHelper() Method.

Not using a context

If you don't want to use a context in your scripts, just extend the ScriptWrapper.getScriptContext() method to return null. For example:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
        protected ScriptContext getScriptContext() {
          return null;
        }
      };

See also


Categories: api

Copyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence