Use additional libraries for scripts

When using Scripting process actions, you may want to use additional libraries in your scripts. You can add additional libraries and classes by adding the libraries to a module, and configuring the process action to use that module.

Constraints

Beanshell, Rhino, Nashorn, and JavaScript are fully supported. Third-party Jython and Groovy libraries, as well as the java.lang.Class.forName function, require a more complex module assignment.

Furthermore, you can only benefit from additional scripting libraries in scripting process actions. Module assignment does not work for scripts in text fields, which are limited to built-in functions.

Assign a module in action parameters

When you need to use additional libraries or classes for your scripts, you can specify a module containing the related libraries in the scripting action actions. All additional libraries in the module are imported automatically. This is the recommended method to use additional libraries in scripting process actions.

In the Modules Management preferences and wizard, you can create Modules in the Generic category. These modules are not linked to any particular technology, so are useful when you want to use specific libraries for scripts.

To specify a module in a scripting process action:

  1. Open a process in xDI Designer.

  2. Create a scripting action from the Process Palette.

  3. Open the scripting action properties.

  4. In the properties Standard tab, activate the Module field, and select a module.

The following example defines a module containing a JDBC driver, retrieves the driver, and create a JDBC connection in a Rhino script.

Image of a scripting process with an assigned module

The syntax to import and use classes varies depending on the scripting language you use.

Reference a module in action scripts

Inside of a process action script, you can also manually define which module to retrieve classes from. This is useful if you use a scripting process action, and run into one of the constraints.

The process consists of creating a Java class loader object, from which you can references scripting classes afterwards.

Retrieve class loader from a module

Use the following function to retrieve a Java class loader object for a specified Module:

__ctx__.getModule("Module name").getClassLoader()

This returns the class loader related to a Module, which you can then use as you want to search for classes.

This function works whether or not you have assigned a module in the scripting action parameters.

Retrieve classes with java.lang.Class.forName

The java.lang.Class.forName function works with the class loader to retrieve a given class. You must specify a module class loader as one of the function arguments.

Example 1. Syntax
object = java.lang.Class.forName("<class to be retrieved>", true, __ctx__.getModule("<Module name from which to retrieve the class from>"));
driver = object.newInstance();
Example 2. Example
clazz = java.lang.Class.forName("oracle.jdbc.driver.OracleDriver", true, __ctx__.getModule("ORACLE_11_MODULE"));
driver = clazz.newInstance();

Using modules for Jython and Groovy

To use classes from additional Jython or Groovy libraries in scripts, use the java.lang.Class.forName function to retrieve them directly from within the script.

Example 3. Example Jython script
import java.util
import java.lang
import com.indy.engine.core.module.classloader as classloader

clazz = java.lang.Class.forName("oracle.jdbc.driver.OracleDriver",True,__ctx__.getModule("Oracle").getClassLoader())

driver = clazz.newInstance()

properties = java.util.Properties()
properties.setProperty("user","SYSTEM")
properties.setProperty("password","password")

conn = driver.connect("jdbc:oracle:thin:@local.database:1521:ORA112",properties)

conn.isValid(5)

Retrieve classes from an assigned module

If you defined a module in the scripting process action’s Module parameter, you can retrieve its class loader using the following syntax:

__cl__

This syntax is a shorthand of the __ctx__.getModule function for the module defined in the scripting process action’s Module parameter. It requires an assigned module to work.

Example 4. Example Rhino script with shorthand
clazz = java.lang.Class.forName("oracle.jdbc.driver.OracleDriver",true,__cl__)
driver = clazz.newInstance()

properties = java.util.Properties()
properties.setProperty("user","SYSTEM");
properties.setProperty("password","password");

conn = driver.connect("jdbc:oracle:thin:@local.database:1521:ORA112",properties)

conn.isValid(5)