Advanced Topics

This chapter contains advanced use cases and classes for special tasks.

For a normal script these items are not relevant.

Java Development

It is also possible to write automation scripts in plain Java code, but this is not recommended. There are some items in the API, which need a different usage in Java code.

This chapter describes the differences in the Automation API when used from Java code.

Script Task Creation in Java Code

Java code could not use the Groovy syntax to provide script tasks. So another way is needed for this. The IScriptFactory interface provides the entry point that Java code could provide script tasks. The createScript(IScriptCreationApi) method is called when the script is loaded.

This interface is not necessary for Groovy clients.

Java code usage of the IScriptFactory to contribute script tasks
public class MyScriptFactoryAsJavaCode implements IScriptFactory {
    @Override
    public void createScript(final IScriptCreationApi creation) {
        creation.scriptTask("TaskFromFactory", IScriptTaskTypeApi.DV_APPLICATION,
                (taskBuilder) -> {
                    taskBuilder.code(
                            (scriptExecutionContext, taskArgs) -> {
                                // Your script task code here
                                return null;
                            });
                });

        creation.scriptTask("Task2", IScriptTaskTypeApi.DV_PROJECT,
                (taskBuilder) -> {
                    taskBuilder.code(
                            (scriptExecutionContext, taskArgs) -> {
                                // Your script task code for Task2  here
                                return null;
                            });
                });
    }
}

You should try to use Groovy when possible, because it is more concise than the Java code, without any difference at script task creation and execution.

Java Code accessing Groovy API

Most of the Automation API is usable from both languages Java and Groovy, but some methods are written for Groovy clients. To use it from Java you have to write some glue code.

Differences are:

  • Accessing Properties
  • Using API entry points.
  • Creating Closures

Accessing Properties

Properties are not supported by Java so you have to use the getter/setter methods instead.

API Entry Points

Most of the Automation API is added to the object by so called DynamicObjects. This is not available in Java, so you have to call IScriptExecutionContext.getInstance(Class) instead. So if you want to access The IWorkflowApi you have to write:

//Java code:
IScriptExecutionContext scriptCtx = ...;
IWorkflowApi workflow = scriptCtx.getInstance(IWorkflowApiEntryPoint.class).getWorkflow()

//Instead of Groovy code:
workflow {

}

Creating Closure instances from Java lambdas

The class Closures provides API to create Closure instances from Java FunctionalInterfaces.

The from() methods could be used to call Groovy API from Java classes, which only accepts Closure instances.

Sample:

 Closure<?> c = Closures.from((param) -> {
   // Java lambda
 });

Creating Closure Instances from Java Methods

You could also create arbitrary Closures from any Java method with the class MethodClosure. This is describe in:
http://melix.github.io/blog/2010/04/19/coding_a_groovy_closure_in.html

Java Code in dvgroovy Scripts

It is not possible to write Java classes when using the .dv.groovy script file. You have to create an automation script project.