Writing an operation provider

Defining the service interface

For this example we'll be using the the exact same GWSDL file we used in the previous example. We can do this because, as long as the interface doesn't change (i.e. as long as we don't add methods, remove methods, add parameters to methods, etc.) we can reuse the GWSDL and the stub classes. Using operation providers instead of extending from GridServiceImpl is an implementation issue and, therefore doesn't affect the interface.

Implementing the service

The operation provider class (which we will call MathProvider) is very similar to all the MathImpl classes we saw in the previous example. The main difference is that, in this class, we need to implement the OperationProvider interface. Let's take a look at the whole code, and then take a closer look at the new code:

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

import org.globus.ogsa.GridServiceBase;
import org.globus.ogsa.GridServiceException;
import org.globus.ogsa.OperationProvider;

import java.rmi.RemoteException;
import javax.xml.namespace.QName;

public class MathProvider implements OperationProvider
{
  
  // Operation provider properties
  private static final String namespace = "http://www.globus.org/namespaces/2004/02/progtutorial/MathService";
  
  private static final QName[] operations = 
    new QName[]
      {
      new QName(namespace, "add"),
      new QName(namespace, "subtract"),
      new QName(namespace, "getValue")
      };

  private GridServiceBase base;

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

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


  private int value = 0;

  public void add(int a) throws RemoteException
  {
    value = value + a;
  }

  public void subtract(int a) throws RemoteException
  {
    value = value - a;
  }

  public int getValue() throws RemoteException
  {
    return value;
  }
}
[Note]

This file is $TUTORIAL_DIR/org/globus/progtutorial/services/core/providers/impl/MathProvider.java

As mentioned before, this class implements the OperationProvider interface, and doesn't extend from any base class:

public class MathProvider implements OperationProvider

Next, we need to implement the two methods in the OperationProvider interface: initialize() and getOperations(). These two methods will require three private attributes: namespace, operations, and base.

// Operation provider properties
private static final String namespace = "http://www.globus.org/namespaces/2004/02/progtutorial/MathService";
  
private static final QName[] operations = 
  new QName[]
    {
    new QName(namespace, "add"),
    new QName(namespace, "subtract"),
    new QName(namespace, "getValue")
    };

private GridServiceBase base;
public void initialize(GridServiceBase base) throws GridServiceException
{
  this.base = base;
}

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

These two methods and three attributes are basically what make the operation provider fit into the Grid Service. The operations attribute specifies what operations this class provides. The Grid Services container uses the getOperations() method to 'ask' our class what operations it provides.

This list of operations is an array of fully qualified names. This means we have to specify both the namespace of the operation and its name. The namespace has to be the target namespace specified in the GWSDL file which our operation provider is going to implement.

private static final String namespace = "http://www.globus.org/namespaces/2004/02/progtutorial/MathService";
  
private static final QName[] operations = 
  new QName[]
    {
    new QName(namespace, "add"),
    new QName(namespace, "subtract"),
    new QName(namespace, "getValue")
    };
[Note]

Remember we're reusing the GWSDL file from the previous chapter. If you take a look at it ($TUTORIAL_DIR/schema/progtutorial/MathService/Math.gwsdl) you'll see that the target namespace is indeed http://www.globus.org/namespaces/2004/02/progtutorial/MathService.

Besides the initialize and getOperations methods and the namespace, operations, and base attributes, the rest of the implementation is just like the one seen in the previous chapter:

private int value = 0;

public void add(int a) throws RemoteException
{
  value = value + a;
}

public void subtract(int a) throws RemoteException
{
  value = value - a;
}

public int getValue() throws RemoteException
{
  return value;
}