How to Write a Basic Activity

Activities provide one of the extension points of OGSA-DAI through which users can add new functionality. This tutorial describes how to extend OGSA-DAI by writing and deploying a new activity. We use a stringTokenizer activity (which we already described here) as a basis for the example.

The stringTokenizer activity will sit between two activities at an intermediate stage in an activity chain. It will separate a sequence of string input data into smaller tokens for output. The set of delimiters used to separate tokens may be specified. For example, using comma, colon and space as delimiters, the following string input data:

nichiyoobi,getsuyoobi:kayoobi suiyoobi,mokuyoobi:kinyoobi doyoobi

will be broken into the following seven tokens:

nichiyoobi
getsuyoobi
kayoobi
suiyoobi
mokuyoobi
kinyoobi
doyoobi

There are four stages involved in writing a basic activity:

  1. Define the XML Schema
  2. Write an implementation class in Java
  3. Write a corresponding Client Toolkit class in Java (optional)
  4. Install the activity

1. Define the XML Schema

Every activity has an XML-Schema that describes the structure of the activity XML element that will be written by clients in perform documents. This element is known as an activity element and its XML-Schema will define the inputs and outputs of the activity as well as any additional configuration details. Occurences of activity elements are used within perform documents to indicate which activities should be performed by an OGSA-DAI service.

For this example, the XML Schema for the stringTokenizer activity will be constructed by modifying an XML Schema template.

Start by copying the template (available here) into a new file named string_tokenizer.xsd. Next make the following changes:

  1. Alter the name of the activity's type to something meaningful:
  2. Alter the names of the activity's input and output to be more informative:
  3. The stringTokenizer will require an element to allow the tokenizing delimiters to be specified. To do this, replace the definition of the myConfiguration element with one for a delimiters element:
    <xsd:element name="delimiters" minOccurs="0" maxOccurs="1">
      <xsd:complexType>
        <xsd:attribute name="value" use="required">
          <xsd:simpleType>
            <xsd:restriction base="xsd:string"/>
          </xsd:simpleType>
        </xsd:attribute>
      </xsd:complexType>
    </xsd:element>
    

    This configuration will allow a user to specify the set of characters that they wish to use as separators. For example:

    <separators value=", :"/>
    

    will use comma, space and colon characters as separators.

An example of the completed string_tokenizer.xsd XML Schema is available here.

2. Write an implementation class in Java

Every activity has an implementation class that performs the action associated with the activity, for instance querying a database, transforming some data, or delivering results to a third party. The stringTokenizer activity will need an implementation class that knows how to divide its input data into a sequence of tokens for output.

For this example, the implementation class will be written by modifying a Java template.

Start by copying the template (available here) into a new file named StringTokenizerActivity.java, then make the following changes:

  1. Alter the name of the class to correspond with the name of the activity it implements. This is usually done by capitalising the first letter of the activity element and appending Activity to the end. For this example, the stringTokenizer activity element should have an implementation class called StringTokenizerActivity.
  2. Remove the mConfiguration instance variable and add two new instance variables:
  3. Modify the constructor by updating the names of the input and output elements to reference those defined in the activity's schema:
    String inputName = ((Element) element.getElementsByTagName(
        "stringBlocksInput").item(0)).getAttribute("from");
    String outputName = ((Element) element.getElementsByTagName(
        "stringTokensOutput").item(0)).getAttribute("name");
    
  4. Continue to modify the constructor by changing the code that extracts the configuration information from the activity element. We need to extract the value attribute of the delimiters element and set the mDelimiters instance variable. If this optional element has been ommitted or the attribute value has been left blank, the default value should be set to space.
    NodeList list = element.getElementsByTagName("delimiters");
    mDelimiters = (list.getLength()>0)
        ? ((Element) list.item(0)).getAttribute("value")
        : " ";
    
  5. Modify the processBlock method so that on each call a token of input data is outputted. When there is no more input data to tokenize and no more tokens to output, the activity processing is complete. Some guidelines are given below:

An example of the completed StringTokenizerActivity.java class is available here.

3. Write a corresponding Client Toolkit class in Java (optional)

If you do not wish to interact with your new activity using the Client Toolkit then this step can be ommitted.

You can write a class to allow the use of your activity to be supported by the OGSA-DAI Client Toolkit.

For this example, the Client Toolkit activity implementation will be written by modifying a template Java class.

This template class is available here.

Note that the class in the template extends uk.org.ogsadai.client.toolkit.activity.Activity. This is an abstract base class for all client activity implementations. It provides common functionality and defines the abstract generateXML method that must be implemented by concrete sub-classes. An implementation of this method must generate the XML activity element suitable for embedding into a Perform document.

Start by copying the template into a new file named StringTokenizerActivity.java. Make sure the implementation file from step 2 is not overwritten! Save this file in a different directory.

To complete the client activity implementation, follow the steps below:

  1. Define instance variables for storing configuration information for the activity. These variables will normally be initialised via constructors or set methods of the class. For the stringTokenizer activity, an instance variable is needed to store the tokenizing delimiters.
  2. Rename the class and constructor to StringTokenizerActivity. Next modify the constructor code to register any inputs and outputs with the superclass using the addInput and addOutput methods. The constructor should be parameterised to enable the configuration instance variables to be initialised. For the stringTokenizer activity, the constructor should register one input, one output and initialise the mDelimiters instance variable. Note that the strings passed into the setInput and setOutput methods are merely descriptive.
    /**
     * Constructs an activity to tokenize the input using the specified
     * delimiters.
     *
     * @param  delimiters  the delimiters used to separate tokens
     */
    public StringTokenizerActivity(final String delimiters)
    {
        addInput("String blocks input");
        addOutput("String tokens output");
        setDelimiters(delimiters);
    }
    
  3. The next step is to define set methods for any additional configuration. These are normally used for optional settings that are not required to construct a valid activity. The stringTokenizer activity does not require any additional configuration, so simply remove the setSetting method from the template code.
  4. Define setter methods for any inputs to the activity. These are used to allow activities to be chained together. For example, the output of an sqlQuery activity can be connected to the input a xslTransform activity by invoking the XSLTransform.setInput method. The stringTokenizer activity has one input and the setInput method defined in the template allows the output of another activity to be connected to it, so no changes are required for this stage.
  5. Define getter methods for accessing any outputs of the activity. These outputs can be connected to the inputs of other activities to form activity chains. The stringTokenizer activity has one output for the tokenized data. The getOutput method defined in the template provides access to this output, so no changes are required for this stage.
  6. The abstract uk.org.ogsadai.client.toolkit.Activity class defines some methods for accessing the output data written by an activity directly. For some activities it may be desirable to define get methods for accessing this data, or part of this data, in a more usable form. For example, the SQLQuery client activity class provides a getResultSet method that gets the output of the activity as a java.sql.ResultSet rather than an XML string. The stringTokenizer activity is designed to sit at an intermediate stage in a chain of activities, so there is no need for any special get methods for accessing the output data. Remove the getSomething method from the template code.
  7. Finally, and most importantly, modify the generateXML method to construct the XML that will be embedded into a perform document for this activity. This usually consists of appending element and attribute XML to a StringBuffer and depends on the configuration information passed to the constructor and set methods.
    protected String generateXML()
    {
        final StringBuffer sb = new StringBuffer();
    
        sb.append("<stringTokenizer name=\"");
        sb.append(getName());
        sb.append("\">\n");
    
        // stringBlocksInput sub-element
    
        sb.append("  <stringBlocksInput from=\"");
        sb.append(getInputParameters()[0].getOutputName());
        sb.append("\"/>\n");
    
        // delimiters sub-element
    
        sb.append("  <delimiters value=\"");
        sb.append(mDelimiters);
        sb.append("\"/>\n");
    
        // stringTokensOutput sub-element
    
        sb.append("  <stringTokensOutput name=\"");
        sb.append(getOutputs()[0].getName());
        sb.append("\"/>");
    
        sb.append("</stringTokenizer>");
    
        return sb.toString();
    }
    

An example of the completed StringTokenizerActivity.java Client Toolkit class is available here.

4. Install the Activity

Once you have defined an XML schema, provided a Java implementation, and optionally written a Client Toolkit activity, your activity is ready to be installed into OGSA-DAI. To install a new activity:

  1. Compile the activity class. For example, for StringTokenizerActivity.java:
    $ javac -cp StringTokenizerActivity.java
    
  2. Make the classes that implement the activity available to OGSA-DAI. Either:
  3. Add an entry for the activity to the activity configuration documents used by any services you wish to expose the activity (activity configuration documents were described on these pages). This entry should be of the following format:
    <activity name="<ACTIVITY-NAME>"
      implementation="<ACTIVITY-IMPLEMENTATION-CLASS>"
      schema="<ACTIVITY-XML-SCHEMA"/>
    
    For example:
    <activity name="stringTokenizer"
      implementation="my.package.StringTokenizerActivity"
      schema="string_tokenizer.xsd"/>
    

    It should be noted that the name attribute here is case sensitive, and specifies the name that will be used to access the activity within Perform documents.

  4. Place the XML Schema file for the activity in the location pointed to by the base attribute of the activityMap element in the activity configuration document. Usually this will be the in the directory:
    CONTAINER/webapps/ogsa/schema/ogsadai/xsd/activities/
    

Now the new activity will be available when the service container is restarted. The activity element can be embedded into Perform documents sent to any OGSA-DAI services that are configured using the activity configuration documents you modified above.

If you wrote a Client Toolkit activity class for the new activity, then clients written using the Client Toolkit APIs can also make use of the new activity.

Further Information

This example has shown how to write a simple activity and provided some general guidelines. For more information it is recommended that you download the OGSA-DAI source distribution and examine the source code. The activities provided with OGSA-DAI are stored in sub-packages of uk.org.ogsadai.activity and the client activities within sub-packages of uk.org.ogsadai.client.toolkit.activity. We have already described the standard OGSA-DAI activities on these pages.