Implementing a Flow4J Task in a Scripting Language

Bean Scripting Framework

Flow4J uses the Bean Scripting Framework (BSF) to execute scripts written in scripting languages like Python, Groovy, JudoScript and others. Here is a short excerpt from the Apache Jakarta site that explains BSF

Bean Scripting Framework (BSF) is a set of Java classes which provides scripting language support within Java applications, and access to Java objects and methods from scripting languages.

The Jakarta BSF implementation supports a lot of scripting languages out of the box. A registered language has the following components:

  • language name
  • engine class
  • list of file extensions

Scripting in Flow4J

The BSF implementation (bsf.jar) is by default part of the Flow4J-Eclipse plug-in. Thus all scripting langauges available in the BSF implementation are also available in Flow4J. To see a list of these languages open the Eclipse preferences through menu Window- > Preferences- > Flow4J- > BSF. The following image shows the Flow4J BSF panel where you can add a new scripting enine or change/extend the language extensions.

Note:

The Python language has two scripting engines registered Jython and JPython. To avoid ambuguity, the extensions for both engines are not set. So if you want to scripty Python, then you have to choose your prefered Python scripting engine and set the extension there to "py" or "py, python". These settings will be stored in the eclipse .metadata hierarchy, so you only have to set it once per eclipse installation.

Scripted Task Flowlets

As already described in the Flow4J user manual, a task flowlet is an atomic functional part of the flow. Or in other terms, it is a reusable process step in a process. In the uer manual I described tasks all written in Java. With the help of BSF a task can be written in any scripting language that is BSF compliant and registered at the BSF framework. So it is possible to model a flow that has task flowlets implemented in different scripting languages!. The following image shows a flow that first executes a Java task, then a Python task and after this a Groovy task.

Accessing the Flow Dictionary from Scripts

Accessing and manipulating the flow dictionary from inside a script is possible. The flow dictionary extends the Java HashMap and can be used in scripts in the same way as built in maps or dictionaries are used in the appropriate scripting language. BSF makes this possible through declared beans . Here is an official description of declared beans from the BSFEngine class's javadoc:

Declared beans are beans that are named and which the engine must make available to the scripts it runs in the most first class way possible.

The name of the declared flow dictionary is: dictionary

Here is a list of accessing the dictionary in some scripting languages:

Python print dictionary["name" ]
Groovy System.out.println(dictionary.get("name"))

Changing or deleting the dictionary's contents is of coururse possible too.

Adding Script Flowlets to a Flow

May be you have recognized that there is no script task tool in the Flow4J designer's tool palette. The reason for this is that script tasks won't be used so often as ordinary task flowlets written in Java. Currently there is only one way to add a scripted task flowlet to the designer, through drag and drop . All script files which extension is defined in the Flow4J BSF properties can be dropped on the designer.

Note:

While runtime the flow class loads the scripts from the classpath. So make sure that the script is in one of the project source folders. Otherwise dropping the script on the designer will have no effect.