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:
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:
<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.
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:
String inputName = ((Element) element.getElementsByTagName(
"stringBlocksInput").item(0)).getAttribute("from");
String outputName = ((Element) element.getElementsByTagName(
"stringTokensOutput").item(0)).getAttribute("name");
NodeList list = element.getElementsByTagName("delimiters");
mDelimiters = (list.getLength()>0)
? ((Element) list.item(0)).getAttribute("value")
: " ";
An example of the completed StringTokenizerActivity.java class is available here.
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:
/**
* 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);
}
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.
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:
$ javac -cp StringTokenizerActivity.java
<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.
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.
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.
| Up: OGSA-DAI User Guide | ||
| © International Business Machines Corporation, 2004. | © The University of Edinburgh, 2004. |