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.apache.log4j.Logger;
import org.w3c.dom.Element;

import uk.org.ogsadai.common.exception.common.CommonUserException;
import uk.org.ogsadai.common.exception.engine.activity.ActivitySystemException;
import uk.org.ogsadai.common.exception.engine.activity.ActivityUserException;
import uk.org.ogsadai.engine.Activity;
import uk.org.ogsadai.engine.BlockReader;
import uk.org.ogsadai.engine.BlockWriter;

/**
 * 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
{
    // Logger object for this activity
    private static final Logger LOG = Logger.getLogger(ActivityTemplate.class);

    /** Activity input. */
    private BlockReader mInput;

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

    /** Activity configuration of some kind. */
    private String mConfiguration;

    /**
     * Constructs activity with the specified element.
     *
     * @param  element  the activity element
     */
    public ActivityTemplate(final Element element)
        throws ActivitySystemException, ActivityUserException
    {
        super(element);

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

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

        // Read the configuration details for other sub-elements of the
        // activity element

        Element configuration = (Element)
        	element.getElementsByTagName("myConfiguration").item(0);
        mConfiguration = configuration.getAttribute("value");
    }

    /*
     * (non-Javadoc)
     * @see uk.org.ogsadai.engine.Activity#initialise()
     */
    public void initialise() throws ActivityUserException
    {
        try
        {
            // Set-up references to the activity input and output.

            mInput = mContext.getInput(mInternalInputs[0]);
            mOutput = mContext.getOutput(mInternalOutputs[0]);
        }
        catch (CommonUserException e)
        {
            // Throw an ActivityUserException if the activity context does not
            // contain the specified inputs or outputs

            throw new ActivityUserException(getName(), e, LOG);
        }
    }

    /*
     * (non-Javadoc)
     * @see uk.org.ogsadai.engine.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 based on
        // the activity configuration, and 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 ActivityUserException or
                // ActivitySystemException should be passed to the setError
                // method. The engine will stop processing the activity.

                setError(new ActivitySystemException(getName(), 0, e, LOG));
            }
        }
        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/engine/Activity.javaclass.