Activity Java Template

The Java code below can be copied and pasted into a new file and used as a template for implementing a new activity.

package uk.org.ogsadai.activity;

import org.w3c.dom.Element;

import uk.org.ogsadai.activity.Activity;
import uk.org.ogsadai.activity.ActivityCreationException;
import uk.org.ogsadai.activity.ActivityExecutionException;
import uk.org.ogsadai.activity.ActivitySpecificationException;
import uk.org.ogsadai.activity.ActivityStreamNotFoundException;
import uk.org.ogsadai.common.BlockReader;
import uk.org.ogsadai.common.BlockWriter;
import uk.org.ogsadai.service.OGSADAIConstants;

/**
 * This is a template class that can be copied into a new file,
 * renamed and modified in order to implement a new activity.
 *
 * @author
 */
public class ActivityTemplate extends Activity
{
    /** Activity input. */
    private BlockReader mInput;

    /** Activity output. */
    private BlockWriter mOutput;

    /** Activity setting of some kind. */
    private String mSetting;

    /**
     * Constructs activity with the specified element.
     *
     * @param  element  the activity element
     * @throws  ActivityCreationException  
     *     If there is a problem constructing the
     *     activity due to an implementation
     *     error or OGSA-DAI confguration problem.
     * @throws  ActivitySpecificationException    
     *     If there is a problem constructing the
     *     activity due to a user mistake such as
     *     an invalid setting.
     * @see uk.org.ogsadai.activity.Activity#Activity(org.w3c.dom.Element)
     */
    public ActivityTemplate(final Element element)
        throws ActivityCreationException, ActivitySpecificationException
    {
        super(element);

        // Publish the activity input(s) and output(s) by setting the inherited
        // mInternalInputs and mInternalOutputs fields.

        String inputName = ((Element) element.getElementsByTagNameNS(
            OGSADAIConstants.TYPES_NAMESPACE,
            "myInput").item(0)).getAttribute("from");
        String outputName = ((Element) element.getElementsByTagNameNS(
            OGSADAIConstants.TYPES_NAMESPACE,
            "myOutput").item(0)).getAttribute("name");
        mInternalInputs = new String[] {inputName};
        mInternalOutputs = new String[] {outputName};

        // Read the activity settings from other sub-elements of the
        // activity element

        Element setting = (Element) element.getElementsByTagNameNS(
            OGSADAIConstants.TYPES_NAMESPACE,"mySetting").item(0);
        mSetting = setting.getAttribute("value");
    }

    /*
     * (non-Javadoc)
     * @see uk.org.ogsadai.engine.Activity#initialise()
     */
    public void initialise() 
        throws ActivitySpecificationException, ActivityExecutionException 
    {
        try 
        {
            // set-up references to the activity input and output
            mInput = mContext.getInput(mInternalInputs[0]);
            mOutput = mContext.getOutput(mInternalOutputs[0]);
        } 
        catch (ActivityStreamNotFoundException e) 
        {
            // Throw an ActivitySpecificationException if the activity context does not
            // contain the specified inputs or outputs
            throw new ActivitySpecificationException(getName(), e);
        }
    }

    /*
     * (non-Javadoc)
     * @see uk.org.ogsadai.activity.Activity#processBlock()
     */
    public void processBlock()
    {
        // The implementation of the process block method will generally perform
        // a small iteration of the processing of the activity. This often means
        // reading a block of input data, performing some processing, then writing
        // a block of output data.

        if (mInput.hasNext())
        {
            Object inputBlock = mInput.next();

            try
            {
                // ...
                // ... do something ...
                // ...

                Object outputBlock = "nextOutputBlock";
                mOutput.put( outputBlock );
            }
            catch (Exception e)
            {
                // If any errors occur, an ActivitySpecificationException or
                // ActivityExecutionException should be passed to the setError
                // method. The engine will stop processing the activity.
                // ActivitySpecificationException should be used for cases
                // where the error was the client's fault e.g. due to an
                // incorrect parameter in the activity or the receipt of
                // incorrect data from a connected activity.
                // ActivityExecutionException should be used for cases
                // where the error was something internal to OGSA-DAI
                // which the client's request did not cause.
                
                setError(new ActivityExecutionException(getName(), e));
            }
        }
        else
        {
            // Once all the input blocks have been read and processed and there
            // is no more output data to write, the setCompelted method should
            // be invoked. The engine will understand that the activity has
            // completed successfully and cease to process it.

            setCompleted();
        }
    }
}

For further details it is recommended to download the OGSA-DAI source distribution and study the source code for the abstract OGSA-DAI/src/java/core/uk/org/ogsadai/activity/Activity.java class.