How to Write a Configurable Activity

This tutorial follows on from the previous How to Write a Basic Activity tutorial. It describes how to develop and deploy a more advanced type of activity known as a "Configurable Activity". In the previous tutorial, the example of a stringTokenizer activity was used. This activity received all the settings that determined its behaviour from the user via a perform document. For example, a perform document containing the following XML fragment would tokenize its input data using the "," character as a delimiter:

...
<stringTokenizer name="tokenizer">
  <stringBlocksInput from="myOutput"/>
  <delimiters value=","/>
  <stringTokensOutput name="myTokens"/>
</stringTokenizer>
...

While the delimiters element is sufficient to provide user control of the simple stringTokenizer activity, there are many cases where an activity requires more complex configuration by the service provider. Consider the case of a distributedJoin activity that performs several queries against different data resources and joins the results in some way. In OGSA-DAI OGSI this could be realised as follows:

Distributed join activity

If designed to be compatible with OGSA-DAI OGSI, the distributedJoin activity may therefore need to be configured with a list of the Grid Data Services Factory (GDSF) URLs. These URLs could then be used by a distributedJoin activity to access the various GDSFs and create GDSs used to query their underlying data resources.

If designed to be compatible with OGSA-DAI WSI or OGSA-DAI WSRF, the distributedJoin activity may therefore need to be configured with a list of data service URLs. These URLs can then be used by a distributedJoin activity to query their associated data resources.

Two Levels of Activity Configuration

OGSA-DAI provides two mechanisms through which an activity can be configured:

  1. Property Configuration – this facilitates simple configuration of an activity using a sequence of one or more property key-value pairs.
  2. External File Configuration – this facilitates more complex configuration of an activity using arbitrary XML specified in an external file.

The remainer of the tutorial describes how to use one or both of these mechanisms to create a configurable activity.

1. Property Configuration

Property configuration allows a sequence of one or more property elements to be positioned within any activity element of the activity configuration file. This mechanism is best suited to simple, non-structured configuration information. For example, an sqlQuery activity might use property elements to specify the timeout and maximum number of rows that can be returned from a query, as shown below:

...
<activity name="sqlQuery"
          implementation="my.package.SQLQueryActivity"
          schemaFileName="path/to/my-sql-query-activity.xsd">
  <property key="maxResults" value="100"/>
  <property key="timeout" value="5000"/>
</activity>
...

The activity implementation can then access these properties using the following methods inherited from the base Activity class:

Method Returns Description
hasProperties() boolean Indicates whether or not the activity has any configuration properties.
getProperties() java.util.Properties Accesses the activity configuration properties, if the activity has any.

The activity implementation is responsible for performing validation to ensure that all the required properties have been specified and their values are suitable. The code fragment below shows how an SQLQueryActivity might perform property validation within the initialise method. An ActivitySystemException is raised whenever the properties are deemed invalid.

protected void initialise() throws ActivitySystemException
{
    if (hasProperties() &&
        getProperties().containsKey("maxResults") &&
        getProperties().containsKey("timeout"))
    {
        Properties properties = getProperties();

        try
        {
            mMaxResults =
                Integer.parseInt(properties.getProperty("maxResults"));
            mTimeout =
                Integer.parseInt(properties.getProperty("timeout"));
        }
        catch (NumberFormatException e)
        {
            throw new ActivitySystemException(
                getName(),
                "Activity configuration error. The values for the " +
                "sqlActivity \"maxResults\" and \"timeout\" " +
                "properties must be valid integers.",
                LOG);
        }
    }
    else
    {
        throw new ActivitySystemException(
            getName(),
            "Activity configuration error. The sqlActivity must be " +
            "configured with two properties: \"maxResults\" and " +
            "\"timeout\".",
            LOG);
    }

    ...etc...
}

Note that mMaxResults and mTimeout are both instance variables used by the activity for storing the property values. These can then be used elsewhere in the activity, for instance during processing. LOG is a static final variable referencing the Log4J org.apache.log4j.Logger object for the activity class. OGSA-DAI requires that a logger is passed into all exception constructors to ensure that exception messages are properly logged.

2. External File Configuration

External file configuration facilitates more complex activity configuration using arbitrary XML specified in an external file. This mechanism is suitable for activities that require structured configuration information, rather than a flat list of property key-value pairs.

To use an external configuration file, a config attribute should be added to the desired activity element within the activity configuration file. This attribute should contain either the absolute URL of the external configuration file, or alternatively, the path relative to a base URL specified using the configBase attribute of the surrounding activityMap element. For example:

...
<activityMap
  schemaBase="file:C:/ogsadai/config/">
  configBase="file:C:/ogsadai/schema/ogsadai/xsd/activities/">
  ...
  <activity name="distributedJoin"
    implementation="package.MyDistributedJoin"
    schema="my-distributed-join-activity.xsd"
    config="my-distributed-join-config.xml"/>
...

Note that the file URLs used in the above example are specified in a format that Windows accepts. A similar file URL in Unix could be expressed as file:/home/ogsadai/config/. Returning to the example of a distributedJoin activity, the external configuration file may contain the following XML data:

<configuration>
  <joinNodes>
    <node url="http://www.turnip.org/ogsa/services/MySQLService">
    <node url="http://www.parsnip.org/ogsa/services/DB2Service">
    <node url="http://www.pumpkin.org/ogsa/services/SQL2000Service">
  </joinNodes>
</configuration>

In order for this information to be accessed within the activity implementation, the developer must implement two interfaces:

Interface Description
uk.org.ogsadai.engine.ActivityConfiguration Defines an initialise method that receives a DOM Document containing the XML from the external configuration file. An implementation should validate and encapsulate this document, providing convenient methods for accessing the details that will be used by the Activity implementation.
uk.org.ogsadai.engine.ActivityConfigurationCreator Defines a createActivityConfiguration method that creates an instance of an ActivityConfiguration implementation. Any activity making use of an external configuration file must implement this interface. This is an example of the Factory Method design pattern.

The UML diagram below demonstrates how these interfaces would be used by the developer of the distributedJoin activity:

Activity configuration class diagram

In summary, the DistributedJoinConfiguration class implements the ActivityConfiguration interface, providing an initialise method to receive the configuration document and a number of convenient methods for exposing the details. The DistributedJoinActivity extends the abstract base Activity class and implements the ActivityConfigurationCreator interface, providing a method that creates an instance of DistributedJoinConfiguration. OGSA-DAI will invoke this method internally and make the ActivityConfiguration object accessible to the activity implementation via the inherited getActivityConfiguration method. A hasActivityConfiguration method is also provided to indicate whether or not a particular activity has been configured using an external configuration file.

The remainder of this tutorial contains some example code for the DistributedJoinActivity and DistributedJoinConfiguration classes. For a more comprehensive example, download the OGSA-DAI source distribution and refer to src/java/activities/uk/org/ogsadai/activity/indexedfiles/AddAndIndexFileActivity.java.

DistributedJoinActivity.java

package my.package;

import uk.org.ogsadai.engine.Activity;
import uk.org.ogsadai.engine.ActivityConfiguration;
import uk.org.ogsadai.engine.ActivityConfigurationCreator;
..etc...

public class DistributedJoinActivity extends Activity
    implements ActivityConfigurationCreator
{
    private DistributedJoinConfiguration mConfig;

    ...etc...

    public ActivityConfiguration createActivityConfiguration()
    {
        return new DistributedJoinConfiguration();
    }

    public void initialise()
    {
        if (hasActivityConfiguration())
        {
            mConfig = (DistributedJoinConfiguration)
                getActivityConfiguration();
        }
    }

    ...etc...
}

DistributedJoinConfiguration.java

package my.package;

import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import uk.org.ogsadai.engine.ActivityConfiguration;
import uk.org.ogsadai.engine.ActivityConfigurationException;
...etc...

public class DistributedJoinConfiguration
    implements ActivityConfiguration
{
    private List mURLs = new ArrayList();

    public void initialise(Document document)
        throws ActivityConfigurationException
    {
        Element configuration = document.getDocumentElement();

        NodeList nodes =
            configuration.getElementsByTagName("joinNodes");

        if (nodes.getLength()>0)
        {
            Element joinNodes = (Element) nodes.item(0);
            nodes = joinNodes.getElementsByTagName("node");

            for (int i = 0; i < nodes.getLength(); i++)
            {
                Element node = (Element) nodes.item(i);
                mURLs.add(node.getAttribute("url"));
            }
        }
        else
        {
            throw new ActivityConfigurationException(
                "Activity configuration error. The configuration " +
                "document for the distributedJoin activity does " +
                "not contain a joinNodes element under the root " +
                "element.",
                LOG);
        }
    }

    public int numberOfNodes()
    {
        return mURLs.size();
    }

    ...etc...
}