Importing and accessing external modules and objects

This example indicates how you can make external code (such as Java™ classes) and external objects accessible to expressions. An external object is an object that an application creates to communicate with expressions.

Before you can access external code from an expression, you must make that code accessible to expressions.

An import is a programming language-specific way to make external code accessible to expressions. The <import> element contains a special type of expression that specifies the external modules (such as Java classes) to import for use in other expressions within rules. An import can be defined at the level of the rule set or a rule block.

The following <import> element contains an expression, written in the Java programming language, that imports the StaticHelper class and the Queue class, which can be referenced from other expressions:
<import expressionLanguage="java">
	import com.ibm.act.sample.StaticHelper;
	import com.ibm.act.test.Queue;
</import>

Although the use of the full class name is not required in the import statement, you should specify the full name to prevent a long compilation time. For example, the Java class should be specified as com.ibm.act.sample.StaticHelper rather than com.ibm.act.sample.* or com.ibm.act.*.

Accessing a static method

The following example indicates how an expression within a rule response action references the StaticHelper class after the class is imported:
<onDetection>
	<action expressionLanguage="java">
		StaticHelper.pageAdministrator("Too many login attempts for " + act_event.getAttribute("userID"));
	</action>
</onDetection>

Accessing an instance method for an object

The following example indicates how an expression within a rule response action references the Queue class after the class is imported. In this example, an external object with a name of OutputQueueOne and a type of Queue is obtained and used to put an event in a specific queue.
<onDetection>
	<action expressionLanguage="java">
		Queue myQueue = (Queue)act_lib.getExternalContext("OutputQueueOne");
		myQueue.enqueue(act_event);
	</action>
</onDetection>