Simple Grid Service (No security support)  from the Globus Toolkit Programmer's Tutorial implemented with an Operation Provider with
  • Service Data
  • Notification
  • Logging
  • Life-cycle control

/*
This file is licensed under the terms of the Globus Toolkit public
License, found at http://www.globus.org/toolkit/download/license.html.
*/

package org.globus.progtutorial.services.core.providers_sd_notif.impl;

import org.globus.ogsa.GridServiceBase;
import org.globus.ogsa.OperationProvider;
import org.gridforum.ogsi.HandleType;

import javax.xml.namespace.QName;

import org.globus.ogsa.ServiceData;
import org.globus.ogsa.impl.ogsi.GridServiceImpl;
import org.globus.ogsa.GridContext;
import org.globus.ogsa.GridServiceException;

import org.gridforum.ogsi.TargetInvalidFaultType;
import org.gridforum.ogsi.ExtensibilityType;
import org.gridforum.ogsi.HandleType;

import org.globus.ogsa.GridServiceCallback;

import org.globus.ogsa.NotificationProvider;
import org.globus.progtutorial.stubs.MathService_sd_notif.MathPortType;
import org.globus.progtutorial.stubs.MathService_sd_notif.servicedata.MathDataType;
import java.rmi.RemoteException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class MathProvider implements OperationProvider, GridServiceCallback
{

    static Log logger = LogFactory.getLog(MathProvider.class.getName());   
    private static final String namespace =           "http://www.globus.org/namespaces/2004/02/progtutorial/MathService_sd_notif";

    private static final QName[] operations =
    new QName[]
    {
        new QName(namespace, "add"),
        new QName(namespace, "subtract"),
    };
 

    // Operation Provider methods
    public void initialize(GridServiceBase base) throws GridServiceException
    {
        this.base = base;
    }

    public QName[] getOperations()
    {
        return operations;
    }

    ////////////////////////////////////////////////////////////////////////////////////

    private GridServiceBase base;

    private ServiceData mathDataSDE;
    private MathDataType mathDataValue;


    public void postCreate(GridContext context) throws GridServiceException
    {

        /////////////////////////////////////////////////////
        logger.info("Instance has been created (postCreate)");
        /////////////////////////////////////////////////////
        // Create Service Data Element
        System.out.println("Created instance!");
       
mathDataSDE = base.getServiceDataSet().create("MathData");

        // Create a MathDataType instance and set intial values
       
mathDataValue = new MathDataType();
        mathDataValue.setLastOp("NONE");
        mathDataValue.setNumOps(0);
        mathDataValue.setValue(0)
;

        // Set the value of the SDE to the MathDataType instance
       
mathDataSDE.setValue(mathDataValue);

        // Add SDE to Service Data Set
       
base.getServiceDataSet().add(mathDataSDE);
    }

    public void preCreate(GridServiceBase base) throws GridServiceException{
       
logger.info("Instance is going to be created (preCreate)");
    }
 

    public void preDestroy(GridContext context) throws GridServiceException{
       
logger.info("Instance is going to be destroyed (preDestroy)");
    }
 

    public void activate(GridContext context) throws GridServiceException{
       
logger.info("Instance has been activated (activate)");
    }
   

    public void deactivate(GridContext context) throws GridServiceException{
       
logger.info("Instance has been deactivated (deactivate)");
    }


    // This method updates the MathData SDE increasing the
    // number of operations by one
    private void incrementOps()
    {
        int numOps = mathDataValue.getNumOps();
        mathDataValue.setNumOps(numOps + 1);
    }

    ///////////////////////////////////////////////////////////////////////////////////////////

    public void add(int a) throws RemoteException
    {
        //value = value + a;
        ////////////////////
        logger.info("Addition invoked with parameter a=" + String.valueOf(a));
        if (a==0)
            logger.warn("Adding zero doesn't modify the internal value!");

        ////////////////////
        mathDataValue.setLastOp("Addition");
        incrementOps();
        mathDataValue.setValue(mathDataValue.getValue() + a);

        ////////////////////////////////////////////////////
        mathDataSDE.notifyChange();
    }


    public void subtract(int a) throws RemoteException
    {
        //value = value - a;
        ///////////////////////////////////
        logger.info("Addition invoked with parameter a=" + String.valueOf(a));
        if (a==0)
            logger.warn("Subtracting zero doesn't modify the internal value!");

        //////////////////////////////////////////////////////////////
        mathDataValue.setLastOp("Subtraction");
        incrementOps();
        mathDataValue.setValue(mathDataValue.getValue() - a);

        ///////////////////////////////////////////////////////
        mathDataSDE.notifyChange();
    }

/////////////////////////////////////////////////////////////////////////////////////////////
}