A client that accesses Service Data

We will now modify the original MathService client so it will also access the MathData SDE. Instead of just adding two numbers, it will also ask MathService for the MathData SDE. Before performing the addition, it will show us the content of the SDE (the current value, the previous operation, and the number of operations performed). First let's take a look at the complete source code:

package org.globus.progtutorial.clients.MathService_sd;

import org.gridforum.ogsi.ExtensibilityType;
import org.gridforum.ogsi.ServiceDataValuesType;
import org.globus.ogsa.utils.AnyHelper;
import org.globus.ogsa.utils.QueryHelper;

import org.globus.progtutorial.stubs.MathService_sd.service.MathServiceGridLocator;
import org.globus.progtutorial.stubs.MathService_sd.MathPortType;
import org.globus.progtutorial.stubs.MathService_sd.servicedata.MathDataType;
import java.net.URL;

public class Client
{
  public static void main(String[] args)
  {
    try
    {
      // Get command-line arguments
      URL GSH = new java.net.URL(args[0]);
      int a = Integer.parseInt(args[1]);

      // Get a reference to the Math PortType
      MathServiceGridLocator mathServiceLocator = new MathServiceGridLocator();
      MathPortType math = mathServiceLocator.getMathServicePort(GSH);

      
      // Get Service Data Element "MathData"
      ExtensibilityType extensibility =
        math.findServiceData(QueryHelper.getNamesQuery("MathData"));
      ServiceDataValuesType serviceData = AnyHelper.getAsServiceDataValues(extensibility);
      MathDataType mathData = 
        (MathDataType) AnyHelper.getAsSingleObject(serviceData, MathDataType.class);

      // Write service data
      System.out.println("Value: " + mathData.getValue());
      System.out.println("Previous operation: " + mathData.getLastOp());
      System.out.println("# of operations: " + mathData.getNumOps());

      // Call remote method
      math.add(a);
    }catch(Exception e)
    {
      System.out.println("ERROR!");
      e.printStackTrace();
    }
  }
}

[Note]

This file is $TUTORIAL_DIR/org/globus/progtutorial/clients/MathService_sd/Client.java

The only new code (besides the different package names) is shown in bold. First of all, we need to get the SDE called MathData. We use a GridService method called findServiceData, and a 'helper' class to resolve the name into something the Grid Service can understand. If you're unfamiliar with 'helper' classes, they're very usual in distributed systems programming (if you've used CORBA, you should be familiar with helper classes and the infamous narrow method). Just think of 'helper' classes as set of classes that allow us to perform really complicated casting operations.

ExtensibilityType extensibility = math.findServiceData(QueryHelper.getNamesQuery("MathData"));

The first thing that probably strikes you as odd from this code snippet is the fact that we're invoking the findServiceData directly on math (which is a MathPortType). Doesn't that portType only have an add and a subtract method? Yes, that's right, but remember that our Math portType extends from a standard portType called GridService. This means we can also use our math portType to invoke GridService methods (such as findServiceData).

Notice how the findServiceData doesn't return a MathDataType class, but an ExtensibilityType class. Now we need to cast it into a MathDataType using more helper classes:

ServiceDataValuesType serviceData =
  AnyHelper.getAsServiceDataValues(extensibility);
MathDataType mathData =
  (MathDataType) AnyHelper.getAsSingleObject(serviceData, MathDataType.class);

Now that we have a MathDataType object, we can use it like any other local object.

System.out.println("Value: " + mathData.getValue());
System.out.println("Previous operation: " + mathData.getLastOp());
System.out.println("# of operations: " + mathData.getNumOps());

Compile and run

Let's compile the client:

javac \
-classpath ./build/classes/:$CLASSPATH \
org/globus/progtutorial/clients/MathService_sd/Client.java

The client receives two parameters:

  1. The GSH

  2. Number to add

If you run it several times:

java \
-classpath ./build/classes/:$CLASSPATH \
org/globus/progtutorial/clients/MathService_sd/Client \
http://127.0.0.1:8080/ogsa/services/progtutorial/core/servicedata/MathService \
5

...you will notice how the number of operations increases with each call. However, since we're only performing addition, the 'previous operation' will always be the same.