Personal tools
You are here: Home GRIA Documentation Documentation 5.2 Tutorials Java Interface Tutorial

Java Interface Tutorial

Note: Return to tutorial view.

Tutorials on how to use the GRIA Client Interface API

Introduction

An introduction to the GRIA Client Java Interface

Introduction

The standard GRIA client allows you to invoke service operations manually using a point-and-click graphical interface. However, it is often necessary to perform the same sequence of operations (such as processing some data using three different applications in series) many times. This can be done by writing some Java code, using GRIA's Java API. The API provides access to all of the service functionality. In fact, the standard GRIA client is just another application that uses the API.

The library has two parts:

  • The core provides Java proxy objects (corresponding to remote services and resources), and a repository in which to store them.
  • The Swing interface components let users view the repository and invoke operations manually.

The library handles security and encryption for you.

There are several ways to write code using GRIA:

  • You can extend your own application to invoke service operations directly. For example, a 3D design tool could have a Render button that submits the scene to a rendering service and then downloads and visualises the results.
  • You can write a plug-in for the standard client, extending its functionality.
  • You can embed the Swing components into your application, so that users don't have to switch to a different application to perform tasks such as setting access control rights on data stagers.

Note that writing new GRIA services is outside the scope of this document. See the GRIA Service Developer Kit documentation for that.

This document is just a tutorial, so we only cover the most common cases. See the full JavaDoc API documentation for more details.

Creating a stand-alone client

To use the GRIA libraries from your own applications (rather than writing a plug-in for the standard client), ensure that all of the following are in your application's classpath:

  1. All the jars from the standard client's lib directory, except for itinnov-grid-client-cli-VERSION.jar, which is the default client itself.

  2. The client-config.wsdd and implementationfactory.properties files (from inside itinnov-grid-client-cli-VERSION.jar).

    The WSDD file causes messages to be signed correctly and contains type mappings telling Axis how to serialise various types, while the implementationfactory file says where to find classes implementing various interfaces.

  3. The crypto.properties file from the client's conf directory, which gives details of the keystore to use.

Creating a plug-in

Plugins extend the functionality of the standard GRIA client. To create a plug-in:

  1. Create an implementation of the GridClientPluginProvider interface.

  2. List the implementation in your jar's META-INF/services/uk.ac.soton.itinnovation.grid.comms.client file.

  3. Place your jar in the client's conf/plugins directory and restart the client.

The plug-in can then extend the client. For example, it could add extra items to the normal menus by adding a PluginHook to the Swing interface.

The StateRepository

A StateRepository stores resources (accounts, SLAs, jobs and data) and services. There are two implementations provided with the library:

Creating a StateRepository

To create a new memory repository:

StateRepository repository = new MemoryStateRepository();

To create a new repository that stores its state in a file called client.state (this is what the standard client uses):

StateRepository repository = new FileStateRepository("client.state");

It is probably easiest to use FileStateRepository as you can create objects using the standard client and then use them from your own program.

Creating a Proxy to a Service XXX

Just as when using the graphical client interface, services must be added to the repository before they can be used. All the details about an object in a StateRepository are represented as a WS-Addressing EndpointReference. For example, to create a RemoteDataService proxy:

EndpointReference serviceEPR = ConversationID.getEPR("https://.../services/DataService");
RemoteDataService dataService = (RemoteSLAService)repository.getOrCreateObject(RemoteDataService.class, serviceEPR);

The getOrCreateObject method takes an EPR for the object you want, and a Java interface for the resulting object. If the object is already in the respository then it returns it, otherwise it creates a new one, adds it to the repository, and returns that.

Note: getOrCreateObject uses the ImplementationFactory class to find a suitable implementation of an interface (such as RemoteDataService) when creating new objects.

Basic Application Services Java Interface

A Java tutorial for the job and data service

Using the Data Service

Having added a data service to our repository and created a RemoteDataService object as above, we can invoke a number of operations on it to create new stagers. This Method returns DataConversation proxy object for the Data Stager, the first parameter is a human readable label for the data stager.

DataConversation data = dataService.createStagingArea("My data stager");

Some services are unmanaged (free), whereas managed services require billing information. The method above automatically finds out the policy of the service and will try to find an appropriate billing EndpointReference in your StateRepository to send to the service. With the method below you can specify the billing information to use to use. This could be the endpoint of an SLA.

DataConversation data = dataService.createStagingArea(billingInfo, "My data stager");

To upload data from a DataSource to a stager, use the save method:

DataSource src = new FileDataSource("picture.jpg");
DataHandler inputHandler = new DataHandler(src);
data.save(inputHandler);

Using the Job Service

A job service RemoteJobService can be added to the repository in the same way we added the data service above:

EndpointReference jobServiceEPR = new EndpointReference("https://example.com/services/JobService");
RemoteJobService jobService = repository.getOrCreateObject(RemoteJobService.class, jobServiceEPR);

Creating a job

We can then use the RemoteJobService object to create a processing job (JobConversation):

JobConversation swirl = jobService.createJob("http://it-innovation.soton.ac.uk/grid/imagemagick/swirl","My Swirl Job");
JobConversation swirl = jobService.createJob("http://it-innovation.soton.ac.uk/grid/imagemagick/swirl",billingInfo,"My Swirl Job");

Again here we can specify billing information, or automaticaly choose one from our StateRepository. The first argument to createJob uniquely identifies the application to use (swirl in this case). This is NOT the service provider being used, which is example.com here.

Running the job

Each job has a number of inputs and outputs. These are data stagers, and so input data is set in the same way as data is added to any other stager: using save(local data), copyFrom(another stager), or copyFromURL(an ordinary HTTP or FTP URL).

Here, we'll copy from our existing data stager to the swirl job's input:

swirl.getInputs()[0].copyFrom(data);

Once all the inputs are set we can start the job using submitJob:

swirl.submitJob(null, new String[] {});

The first paramenter is a job description file to be passed to the execution platform. This can include constraints on how long the job should run, for example. If no information needs to be passed, this can be null, as above. The second parameter is a list of arguments to pass to the application. The swirl application doesn't take any arguments, so our array above is empty.

Collecting the results

Wait until the job is complete:

while (swirl.stillActive()) {
	// Wait for a bit...
}

Then download the results:

swirl.getOutputs()[0].read(new File("output.jpg"));

Of course, you don't have to download the data to your own machine. You could copy directly from the output of one job to the input of another.

When everything is complete, finish the job:

swirl.finishJob();

Service Provider Management Java Interface

A Java tutorial for the Trade Account and SLA Service

Opening a Trade Account

In order to use some services a Trade Account is required. First we create a proxy to the RemoteTradeAccountService

EndpointReferenceType tradeAccountServiceEPR = 
	ConversationID.getEPR("http://.../gria-service-provider-mgt/services/TradeAccountService");
RemoteTradeAccountService tradeAccountService = 
	(RemoteTradeAccountService) repository.getOrCreateObject(RemoteTradeAccountService.class,tradeAccountServiceEPR);

Then we can request a TradeAccountConversation using the openAccount method

TradeAccountConversation tradeAccount 
	= tradeAccountService.openAccount(Name","1234","email@mycompany.com",
		new AddressType(),"1234-1234-1234","TradeAccount1");

Creating an SLA

In order to use some services an SLA is required. First we create a proxy to the RemoteSLAService

EndpointReferenceType slaServiceEPR = ConversationID.getEPR("http://.../gria-service-provider-mgt/services/SLAService");
RemoteSLAService slaService = (RemoteSLAService) repository.getOrCreateObject(RemoteSLAService.class,slaServiceEPR);

Then we get a list of resources on the SLA Service. This list will include SLATemplate's and any SLA's  we have access to. We can identify sla templates by getting the type of the resource in the metadata using ConversationID.getResourceType

EndpointReferenceType[] eprs = slaService.getResources();
EndpointReferenceType slaTemplateEPR = null;
for(EndpointReferenceType epr : eprs){
    String resourceType = ConversationID.getResourceType(epr);
     if (resourceType.equals("http://www.it-innovation.soton.ac.uk/grid/resource/sla-template")){
          slaTemplateEPR = epr;
          break;
     }
}

If we find a valid SLATemplate and agree to its terms we can then propose an SLA:

SLATemplateConversation slaTemplateConversation = repository.getOrCreateObject(SLATemplateConversation.class,slaTemplateEPR);
SLATemplate slaTemplate = slaTemplateConversation.getSLATemplate();
SLAProposal slaProposal = new SLAProposal();
slaProposal.setSlaTemplate(slaTemplate);
slaProposal.setStartTime(new GregorianCalendar());
EndpointReferenceType epr = slaService.createSLA(slaProposal, "Name");

Client Management Interface

A Java tutorial for the Registry, Membership and Private Account Service

Creating a MembershipGroup

Membership Groups can be used to control access to resources. First we create a proxy to the Membership Service.

EndpointReferenceType membershipServiceEPR = (RemoteMembershipService) 
	ConversationID.getEPR("http://.../gria-client-mgt/services/MembershipService");
RemoteMembershipService membershipService = 
	(RemoteMembershipService) repository.getOrCreateObject(RemoteMembershipService.class,membershipServiceEPR);

A MembershipGroup is created using the createGroup method.

MembershipGroupConversation group = membershipService.createGroup("Group1");

Users can be added to a group using the addPolicy rule in the PolicyManagement Interface.

// Need to have the users and issuers certficate
group.addPolicyRule(new PolicyRule(new MatchPattern(userCertificate,issuerCertifcate),"member"));

Users can then be given access to a resource if they are members of the group, by adding a rule to the resource.

String[] possibleRoles = resource.getValidRoles();
// Choose a role for members of the group to have on this resource
String role = ....;
resource.addPolicyRule(new PolicyRule(group.getMembershipPattern(), role));

Creating a Registry

A Registry can be created and other resource added to it. First we create a proxy to the Registry Service

EndpointReferenceType registryServiceEPR = 
	ConversationID.getEPR("http://.../gria-service-provider-mgt/services/RegistryService");
RemoteRegistryService registryService = 
	(RemoteRegistryService) repository.getOrCreateObject(RemoteRegistryService.class,registryServiceEPR);

A Registry can bew created using the createRegisty method

RegistryConversation registry = registryService.createRegistry("Registry1");

Resources can be added to the registry with the registerResource method. Use getEndpointRef() to get the endpoint of a resource and pass this as the first parameter to registerResource.

registry.registerResource(resource.getEndpointRef());

Note: if you register an SLA then the registry will try to get usage information from the SLA service. You need to add a rule to the SLA resource allowing the registry to do this:

SLAConversation sla ...
MatchRule rule = registry.getMatchRuleForResource();
rule.setProcessRole("monitor");
sla.addPolicyRule(new PolicyRule(rule));
registry.registerResource(sla.getEndpointRef());

Opening a Private Account

First we create a proxy to the Private Account Service

EndpointReferenceType privateAccountServiceEPR = 
	ConversationID.getEPR("http://.../gria-client-mgt/services/PrivateAccountService");
RemotePrivateAccountService privateAccountService = 
	(RemotePrivateAccountService) repository.getOrCreateObject(RemotePrivateAccountService.class,privateAccountServiceEPR);

Then we can open a Private Account using the openAccount method

PrivateAccountConversation privateAccount 
	= privateAccountService.openAccount("Name","1234","email@mycompany.com",
		new AddressType(),null,"PrivateAccount1");

Workflow Tutorial 1 - Application Discovery

Simple application discovery

Introduction

The learning objectives for this exercise are:

1. Understand how to discover the applications that have been deployed to the GRIA 5.x Job service; and

2. Understand how to discover further details of deployed applications, including details of inputs and outputs.

Step 1: Create a client proxy to the remote job service.

First create a State Repository which holds references to our services and resources. Then use the getOrCreateObject method on the repository with the class of object we wish to create and the EndpointReference of the Object.

StateRepository repository = new MemoryStateRepository();
RemoteJobService jobService =
(RemoteJobService)  repository.getOrCreateObject(RemoteJobService.class,ConversationID.getEPR(JOB_SERVICE_ENDPOINT));

Be sure to use an appropriate String value for JOB_SERVICE_ENDPOINT. The job service endpoint must be of the following format:

<PROTOCOL>://<HOST>:<PORT>/gria-basic-app-services/services/JobService

or

<PROTOCOL>://<HOST>/gria-basic-app-services/services/JobService

For example:

https://example.company.com:8443/gria-basic-app-services/services/JobService

https://griademo1.it-innovation.soton.ac.uk/gria-basic-app-services/services/JobService

Step 2: List the applications that are deployed at the job service.

String[] applicationList = jobService.getApplications();

The getApplications method on the RemoteJobService returns a list of URIs. Each URI is a unique name for an application that has been deployed at the job service.

The URI is taken from the ApplicationMetadata.xml file that was used when deploying the application using the Job service administration web pages.

Step 3: Get futher details of a specific application.

String applicationURI = applicationList[0];
Document appDescription = jobService.getApplicationMetadata(applicationUri);

Here, appDescription is a DOM Document that represents the ApplicationMetadata.xml file that the job service administrator used when they deployed the application. Its contents can be examined to determine the number and types of inputs and outputs.

The following code can be used to output the document to standard output.

String strAppDescription = XMLUtils.DocumentToString(appDescription);
System.out.println("\t" + strAppDescription);

Completed Java

If you need some help here is a completed version of the java file

import java.net.URL;

import org.apache.axis.utils.XMLUtils;
import org.w3c.dom.Document;

import uk.ac.soton.ecs.iam.grid.comms.client.StateRepository;
import uk.ac.soton.ecs.iam.grid.client.staterepos.MemoryStateRepository;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteJobService;
import uk.ac.soton.itinnovation.grid.types.ConversationID;

public class ApplicationDiscovery {

	private static String JOB_SERVICE_ENDPOINT = "https://.../gria-basic-app-services/services/JobService";

	public static void main(String[] args) throws Exception {

		StateRepository repository = new MemoryStateRepository();
		RemoteJobService jobService = 
			(RemoteJobService)repository.getOrCreateObject(RemoteJobService.class,ConversationID.getEPR(JOB_SERVICE_ENDPOINT));

		String[] applicationList = jobService.getApplications();

		if(applicationList.length > 0){
			System.out.println("Found "+applicationList.length+" applications.");
			for(String applicationURI : applicationList){
				Document appDescription = jobService.getApplicationMetadata(applicationURI);

				String strApplicationDescription = XMLUtils.DocumentToString(appDescription);
				System.out.println("\t"+strApplicationDescription);
			}
		}else{
			System.out.println("No applications found on server.");
		}
	}
}

This tutorial has covered discovering details of applications that have been deployed at a GRIA 5.x job service.

Workflow Tutorial 2 - Job Execution

Data and job orchestration

Introduction

The learning objectives for this exercise are:

  1. Understand how to create and manage jobs at the Job service; and
  2. Learn how to correctly monitor job execution.

Step 1. Create a client proxy to the remote job service as seen in Exercise 1.

StateRepository repository = new MemoryStateRepository();
RemoteJobService jobService = (RemoteJobService)  
repository.getOrCreateObject(RemoteJobService.class,ConversationID.getEPR(JOB_SERVICE_ENDPOINT));

Step 2. Create a new Job

JobConversation jobConv = jobService.createJob(APPLICATION, null, "");

The APPLICATION parameter is the URI for the application for which we wish to create a new job. In this example we will use the swirl application for simple image processing. The URI for the swirl application is:

http://it-innovation.soton.ac.uk/grid/imagemagick/swirl

Note that we are assuming that the Job Service is free. Therefore a null endpoint reference for the billing argument is used. If instead the service were not free, we would pass an endpoint reference for an SLA or account instead (see Exercise 4).

Step 3. Get the inputs and outputs that for new job.

These are used to upload input files and download the results, respectively.

DataConversation[] inputs = jobConv.getInputs();
DataConversation[] outputs = jobConv.getOutputs();

Step 4. Upload the input image

inputs[0].save(new DataHandler(new FileDataSource(INPUT_FILE)));

Step 5. Submit the job.

Command line arguments can be passed to the remote application in place of the empty string array.

jobConv.submitJob(null, new String[]{});

Step 6. Monitor execution

The checkJob() method makes a remote method invocation on the Job service, so the thread is made to sleep between rounds of polling job status, to reduce resource use.

while(jobConv.checkJob().getInProgress()) {	
	Thread.sleep(2000);
}

Step 7. Check the applications exit status and deal with errors appropriately.

JobStatus jobStatus = jobConv.checkJob();
if(jobStatus.getExitStatus() != 0) {
  // handle the failure
  throw new RuntimeException("Swirl job failed. Here is the log:\n" +   
                             jobStatus.getLogText());
}

Step 8. Download the output.

OUTPUT_FILE should be a String path to the file where you wish the output to be saved.

outputs[0].read(new File(OUTPUT_FILE));

Step 9. Clean up server resources by finishing the job.

jobConv.destroy();

Completed Java

If you need some help here is a completed version of the java file

import java.io.File;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import uk.ac.soton.ecs.iam.grid.client.staterepos.MemoryStateRepository;
import uk.ac.soton.ecs.iam.grid.client.staterepos.RemoteJobServiceImpl;
import uk.ac.soton.ecs.iam.grid.comms.client.DataConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.JobConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteJobService;
import uk.ac.soton.itinnovation.grid.service.types.JobStatus;
import uk.ac.soton.itinnovation.grid.types.ConversationID;
/**
* Simple data and job orchestration example. Edit JOB_SERVICE_ENDPOINT to be the soap endpoint of your job service. */

public class JobExecutionCompleted {
	
	/** Change this Variable to point to your job service!!! */
	private static final String JOB_SERVICE_ENDPOINT = "https://....:8443/gria-basic-app-services/services/JobService";
	
	/** The application to execute, identified by its URI */
	private static final String APPLICATION = "http://it-innovation.soton.ac.uk/grid/imagemagick/swirl";
	
	/** The input file to upload to the job */
	private static final String INPUT_FILE = "data/input-image.png";
	
	/** The ouptut file to download the results to */
	private static final String OUTPUT_FILE = "data/output-image.png";
	
	public static void main(String[] args) throws Exception {

		StateRepository repository = new MemoryStateRepository();
		RemoteJobService jobService = (RemoteJobService)  
			repository.getOrCreateObject(RemoteJobService.class,ConversationID.getEPR(JOB_SERVICE_ENDPOINT));
		JobConversation jobConv = jobService.createJob(APPLICATION, null, "");

		DataConversation[] inputs = jobConv.getInputs();
		DataConversation[] outputs = jobConv.getOutputs();

		inputs[0].save(new DataHandler(new FileDataSource(INPUT_FILE)));

		jobConv.submitJob(null, new String[] {});
		// poll job status
		while(jobConv.checkJob().getInProgress()) {
			System.out.println(".");
			Thread.sleep(2000);
		}

		JobStatus jobStatus = jobConv.checkJob();

		if(jobStatus.getExitStatus() != 0) {
			// handle the failure
			throw new RuntimeException("Swirl job failed. Here is the log:\n" + jobStatus.getLogText());
		}
		outputs[0].read(new File(OUTPUT_FILE));

		System.out.println("Swirl job completed. Output downloaded to: " + OUTPUT_FILE);

		// clean up resources at the server
		jobConv.destroy();
	}
}

This tutorial has covered how to create, manage, execute and monitor jobs at a remote Job Service.

Workflow Tutorial 3 - Cross Domain Orchestration

Cross domain data and job orchestration

Introduction

The learning objectives for this exercise are:

  1. Learn how to delegate access to resources across domains.

Step 1:

Create a client proxy to the first remote job service and start a job similar to Exercise 2.
StateRepository repository = new MemoryStateRepository();
RemoteJobService jobService1 = (RemoteJobService)  
		repository.getOrCreateObject(RemoteJobService.class,ConversationID.getEPR(JOB_SERVICE_ENDPOINT1));

JobConversation jobConv1 = jobService1.createJob(APPLICATION1, null, "");
DataConversation[] inputs1 = jobConv1.getInputs();
DataConversation[] outputs1 = jobConv1.getOutputs();

inputs1[0].save(new DataHandler(new FileDataSource(INPUT_FILE)));

jobConv1.submitJob(null, new String[]{});
while(jobConv1.stillActive()) {	
	Thread.sleep(2000);
}
JobStatus jobStatus = jobConv.checkJob();
if(jobStatus.getExitStatus() != 0) {
  // handle the failure
  throw new RuntimeException("Swirl job failed. Here is the log:\n" + jobStatus.getLogText());
}

Step 2:

Create the second job at the second Service Provider.
RemoteJobService jobService2 = (RemoteJobService) 
		repository.getOrCreateObject(RemoteJobService.class,ConversationID.getEPR(JOB_SERVICE_ENDPOINT2));
JobConversation jobConv2 = jobService2.createJob(APPLICATION2, null, "");

Step 3:

Instead of uploading a file as input for the job, copy the output from Job1. First we must allow access to the output stager by adding a MatchRule on the output resource. The MatchRule states that jobService2 is allowed 'read' access to the output of job 1.
DataConversation[] inputs2 = jobConv2.getInputs();
DataConversation[] outputs2 = jobConv2.getOutputs();
outputs1[0].addPolicyRule(new PolicyRule(new MatchPattern(jobService2.getServiceProviderID(),jobService2.getServiceProviderIssuer()),"reader"));
inputs2[0].copyFrom(outputs1[0]);

Step 4:

We run the job in the normal way, and then finish both jobs.
jobConv2.submitJob(null, new String[]{});

while(jobConv2.stillActive()) {
	Thread.sleep(2000);
}

JobStatus jobStatus2 = jobConv2.checkJob();

if(jobStatus2.getExitStatus() != 0) {
// handle the failure
throw new RuntimeException("Job failed. Here is the log:\n" +   
						 jobStatus2.getLogText());
}
		
outputs2[0].read(new File(OUTPUT_FILE));

jobConv1.finish();
jobConv2.finish();

Completed Java

If you need some help here is a completed version of the java file.

import java.io.File;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import uk.ac.soton.ecs.iam.grid.client.staterepos.MemoryStateRepository;
import uk.ac.soton.ecs.iam.grid.client.staterepos.RemoteJobServiceImpl;
import uk.ac.soton.ecs.iam.grid.comms.client.DataConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.JobConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteJobService;
import uk.ac.soton.itinnovation.grid.service.types.JobStatus;
import uk.ac.soton.itinnovation.grid.types.ConversationID;
/**
* Simple data and job orchestration example. Edit JOB_SERVICE_ENDPOINT to be the soap endpoint of your job service. */

public class JobExecutionCompleted {
	
	/** Change this Variable to point to your job service!!! */
	private static final String JOB_SERVICE_ENDPOINT = "https://....:8443/gria-basic-app-services/services/JobService";
	
	/** The application to execute, identified by its URI */
	private static final String APPLICATION = "http://it-innovation.soton.ac.uk/grid/imagemagick/swirl";
	
	/** The input file to upload to the job */
	private static final String INPUT_FILE = "data/input-image.png";
	
	/** The ouptut file to download the results to */
	private static final String OUTPUT_FILE = "data/output-image.png";
	
	public static void main(String[] args) throws Exception {

		RemoteJobService jobService = new RemoteJobServiceImpl(
						new MemoryStateRepository(), ConversationID.getEPR(JOB_SERVICE_ENDPOINT));
		JobConversation jobConv = jobService.createJob(APPLICATION, null, "");

		DataConversation[] inputs = jobConv.getInputs();
		DataConversation[] outputs = jobConv.getOutputs();

		inputs[0].save(new DataHandler(new FileDataSource(INPUT_FILE)));

		jobConv.submitJob(null, new String[] {});
		// poll job status
		while(jobConv.stillActive()) {
			System.out.println(".");
			Thread.sleep(2000);
		}

		JobStatus jobStatus = jobConv.checkJob();

		if(jobStatus.getExitStatus() != 0) {
			// handle the failure
			throw new RuntimeException("Swirl job failed. Here is the log:\n" + jobStatus.getLogText());
		}
		outputs[0].read(new File(OUTPUT_FILE));

		System.out.println("Swirl job completed. Output downloaded to: " + OUTPUT_FILE);

		// clean up resources at the server
		jobConv.finish();
	}
}

Workflow Tutorial 4 - Using SLA's

How to use Service Level Agreements to access managed services.

Introduction

The learning objects from this excercise are:

  1. Learn how to use an SLA to gain access to a service.

Step 1: Create a Proxy to the SLA Service

StateRepository repository = new MemoryStateRepository();
RemoteSLAService slaService = (RemoteSLAService) 
		repository.getOrCreateObject(RemoteSLAService.class,ConversationID.getEPR(SLA_SERVICE_ENDPOINT));

Step 2: Find the available resources on the SLA Service

The SLA Service will return a list of Endpoint References for resources. Some of these will be SLA templates and some will be SLA's. You need to check the type of the Endpoint Reference using ConversationID.getType(EndpointReference epr).

EndpointReferenceType[] eprs = slaService.getResources();
SLAConversation sla = null;

for(EndpointReferenceType epr : eprs){
	if(ConversationID.getType(epr).equals(SLAConversation.class.getName()))
		sla = (SLAConversation)repository.getOrCreateObject(SLAConversation.class ,epr);
}

if(sla == null){
	System.out.println("No SLA Found");
	System.exit(1);
}

Step 3: Create a client proxy to the Job Service and invoke createJob

The second parameter to the createJob request should be the EPR of the SLA you wish to use. Then start the job in the usual way.

RemoteJobService jobService = 
	(RemoteJobService) repository.getOrCreateObject(RemoteJobService.class,ConversationID.getEPR(JOB_SERVICE_ENDPOINT));
	
// Create a Job 
JobConversation jobConv = jobService.createJob(APPLICATION, sla.getEndpointRef() , "");
		
DataConversation[] inputs = jobConv.getInputs();
DataConversation[] outputs = jobConv.getOutputs();

inputs[0].save(new DataHandler(new FileDataSource(INPUT_FILE)));
		
jobConv.submitJob(null, new String[]{});

while(jobConv.stillActive()) {
	System.out.println(".");
	Thread.sleep(2000);
}

JobStatus jobStatus = jobConv.checkJob();

if(jobStatus.getExitStatus() != 0) {
	// handle the failure
	throw new RuntimeException("Job failed. Here is the log:\n" +   
					 jobStatus.getLogText());
	}

outputs[0].read(new File(OUTPUT_FILE));

jobConv.finish();

Completed Java

import java.io.File;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import uk.ac.soton.ecs.iam.grid.comms.client.StateRepository;
import uk.ac.soton.ecs.iam.grid.client.staterepos.MemoryStateRepository;
import uk.ac.soton.ecs.iam.grid.comms.client.DataConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.JobConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.SLAConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteJobService;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteSLAService;
import uk.ac.soton.itinnovation.grid.service.types.JobStatus;
import uk.ac.soton.itinnovation.grid.types.ConversationID;
import org.apache.axis.message.addressing.EndpointReferenceType;

public class UsingSLAsCompleted {
	/** Job Service Endpoint */
	private static final String JOB_SERVICE_ENDPOINT = "https://.../gria-basic-app-services/services/JobService";
	/** SLA Service Endpoint */
	private static final String SLA_SERVICE_ENDPOINT = "https://.../gria-service-provider-mgt/services/SLAService";

	/** The application to execute, identified by its URI */
	private static final String APPLICATION = "http://it-innovation.soton.ac.uk/grid/imagemagick/paint";

	/** The input file to upload to the job */
	private static final String INPUT_FILE = "data/input-image.png";

	/** The ouptut file to download the results to */
	private static final String OUTPUT_FILE = "data/output-image-ex4.png";
	
	public static void main(String[] args) throws Exception {
		
		StateRepository repository = new MemoryStateRepository();
		// Create the First Job Service Proxy
		RemoteJobService jobService = (RemoteJobService)
			repository.getOrCreateObject(RemoteJobService.class,ConversationID.getEPR(JOB_SERVICE_ENDPOINT));
		RemoteSLAService slaService = (RemoteSLAService)
			repository.getOrCreateObject(RemoteSLAService.class,ConversationID.getEPR(SLA_SERVICE_ENDPOINT));
		EndpointReferenceType[] eprs = slaService.getResources();

		SLAConversation sla = null;
		
		System.out.println("Searching for SLA's to use");
		for(EndpointReferenceType epr : eprs){
			if(ConversationID.getType(epr).equals(SLAConversation.class.getName()))
				sla = (SLAConversation)repository.getOrCreateObject(SLAConversation.class,epr);
		}

		if(sla == null){
			System.out.println("No SLA's Found");
			System.exit(1);
		}else{
			System.out.println("SLA Found '"+sla+"'");
		}
		
		// Create a Job 
		JobConversation jobConv = jobService.createJob(APPLICATION, sla.getEndpointRef(), "");
		
		DataConversation[] inputs = jobConv.getInputs();
		DataConversation[] outputs = jobConv.getOutputs();

		System.out.println("Uploading input '"+INPUT_FILE+"'");
		inputs[0].save(new DataHandler(new FileDataSource(INPUT_FILE)));
		
		jobConv.submitJob(null, new String[]{});

		System.out.print("Waiting for Job ");
		while(jobConv.stillActive()) {	
			System.out.print(".");
			Thread.sleep(2000);
		}
		System.out.println(" Done!");

		JobStatus jobStatus = jobConv.checkJob();

		if(jobStatus.getExitStatus() != 0) {
		// handle the failure
		throw new RuntimeException("Job failed. Here is the log:\n" +   
						 jobStatus.getLogText());
		}

		System.out.println("Downloading output to '"+OUTPUT_FILE+"'");
		outputs[0].read(new File(OUTPUT_FILE));

		jobConv.finish();
	}
}

Membership Service Tutorial 1 - Creating a Group

How to create a Membership Group using the Membership Service

In order to create a Membership Group we must first contact the Membership service and try to call 'createGroup' on it.First, define the location of the Membership Service

private static String MEMBERSHIP_SERVICE_ENDPOINT = "https://hostname:8443/gria-client-mgt/services/MembershipService";

Declare a new state repoitory

StateRepository repository = new MemoryStateRepository();		

Create a proxy to the membership service

RemoteMembershipService membershipService = 
	repository.getOrCreateObject(RemoteMembershipService.class, ConversationID.getEPR(MEMBERSHIP_SERVICE_ENDPOINT));

Try to call 'createGroup' on the membership service

MembershipGroupConversation group = membershipService.createGroup("My Group");

System.out.println(group.getEndpointRef());

Full java code (cut and paste)

import java.rmi.RemoteException;

import uk.ac.soton.ecs.iam.grid.client.staterepos.MemoryStateRepository;
import uk.ac.soton.ecs.iam.grid.comms.client.ObjectAlreadyExists;
import uk.ac.soton.ecs.iam.grid.comms.client.StateRepository;
import uk.ac.soton.itinnovation.grid.client.membership.MembershipGroupConversation;
import uk.ac.soton.itinnovation.grid.client.membership.RemoteMembershipService;
import uk.ac.soton.itinnovation.grid.types.ConversationID;

public class MembershipGroupTutorial1 {

	private static String MEMBERSHIP_SERVICE_ENDPOINT 
			= "https://hostname:8443/gria-client-mgt/services/MembershipService";

	public static void main(String[] args) {
		StateRepository repository = new MemoryStateRepository();
		RemoteMembershipService membershipService =   
			repository.getOrCreateObject(RemoteMembershipService.class,
					ConversationID.getEPR(MEMBERSHIP_SERVICE_ENDPOINT));
		MembershipGroupConversation group = null;
		try {
			group = membershipService.createGroup("My Group");
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		} catch (ObjectAlreadyExists e) {
			throw new RuntimeException(e);
		}
		
		System.out.println(group.getEndpointRef());
	}

}

Membership Service Tutorial 2 - Adding Members to a Group

Using the Membership Service to add Members to a Membership group

In order to add members to a Membership Group we must first get a proxy to the Membership Service and try to find the resource and get a proxy to it. If you allready have a proxy to the Membership Group then you can skip that part.

private static String MEMBERSHIP_SERVICE_ENDPOINT = "https://hostname:8443/gria-client-mgt/services/MembershipService";	
private static String MEMBERSHIP_RESOURCE_ENDPOINT = "https://hostname:8443/gria-client-mgt/services/MembershipGroup";
// Change this to the id of your group	
private static String MEMBERSHIP_GROUP_ID = "40894e36-15faf09a-0115-fb07f5dd-0008";	

As in Membership Service Tutorial 1 we create a state repository and create a proxy to the Membership Group Resource.

StateRepository repository = new MemoryStateRepository();		
RemoteMembershipService membershipService =  
			repository.getOrCreateObject(RemoteMembershipService.class,
					ConversationID.getEPR(MEMBERSHIP_SERVICE_ENDPOINT));
EndpointReferenceType epr = ConversationID.getEPR(MEMBERSHIP_RESOURCE_ENDPOINT+"#"+MEMBERSHIP_GROUP_ID);
MembershipGroupConversation group = repository.getOrCreateObject(MembershipGroupConversation.class,epr);

Now we have the group, we define a rule to match the subject we wish to add to the group. We also need to tell the Membership Group what the issuer certificate of the subject is. We define that the subject gets 'member' role on the group and the last parameter 'false' define whether this is a deny rule or not.

We then call 'addPolicyRule' on the Membership Group.

try {	
	MatchRule rule = new MatchRule("EMAILADDRESS=Email, CN=CommonName, OU=OrganisationUnit, " 
		+ "O=Organisation, L=Locality, ST=State, C=Country",
		membershipService.getServiceProviderIssuer().getX509Certificate(),
		"member",false);
 
	group.addPolicyRule(new PolicyRule(rule));						
	System.out.println("Added Rule to '"+ConversationID.getLabel(group.getEndpointRef())+"' ("+MEMBERSHIP_GROUP_ID+")");

} catch (RemoteException e) {
	throw new RuntimeException(e);		
}

Full Java code

package workflow;

import java.rmi.RemoteException;

import org.apache.axis.message.addressing.EndpointReferenceType;

import uk.ac.soton.ecs.iam.grid.client.staterepos.MemoryStateRepository;
import uk.ac.soton.ecs.iam.grid.comms.client.StateRepository;
import uk.ac.soton.itinnovation.grid.client.membership.MembershipGroupConversation;
import uk.ac.soton.itinnovation.grid.client.membership.RemoteMembershipService;
import uk.ac.soton.itinnovation.grid.types.ConversationID;
import uk.ac.soton.itinnovation.grid.types.MatchRule;
import uk.ac.soton.itinnovation.grid.types.PolicyRule;

public class MembershipGroupTutorial2 {

	private static String MEMBERSHIP_SERVICE_ENDPOINT 
			= "https://hostname:8443/gria-client-mgt/services/MembershipService";
	private static String MEMBERSHIP_RESOURCE_ENDPOINT 
			= "https://hostname:8443/gria-client-mgt/services/MembershipGroup";

	// Change this to the id of your group
	private static String MEMBERSHIP_GROUP_ID
			= "40894e36-15faf09a-0115-fb07f5dd-0008";

	public static void main(String[] args) {
		StateRepository repository = new MemoryStateRepository();
		RemoteMembershipService membershipService =  
			repository.getOrCreateObject(RemoteMembershipService.class,
					ConversationID.getEPR(MEMBERSHIP_SERVICE_ENDPOINT));
		EndpointReferenceType epr = ConversationID.getEPR(MEMBERSHIP_RESOURCE_ENDPOINT+"#"+MEMBERSHIP_GROUP_ID);
		MembershipGroupConversation group = repository.getOrCreateObject(MembershipGroupConversation.class,epr);

		
		try {
			MatchRule rule = new MatchRule("EMAILADDRESS=Email, CN=CommonName, OU=OrganisationUnit, " +
				"O=Organisation, L=Locality, ST=State, C=Country",
				membershipService.getServiceProviderIssuer().getX509Certificate(),
				"member",false);
			
			group.addPolicyRule(new PolicyRule(rule));
			
			System.out.println("Added Rule to '"+ConversationID.getLabel(group.getEndpointRef())
					+"' ("+MEMBERSHIP_GROUP_ID+")");
			
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
	}
}

Membership Service Tutorial 3 - Delegating Access using a Group

Using the Membership Service to delegate access to another resource.

We need to declare the location of our Membership Service and the service at which the resource we wish to give access to is location. In this example we will give access to a Data Stager located at a Data Service.

private static String MEMBERSHIP_SERVICE_ENDPOINT 
	= "https://hostname:8443/gria-client-mgt/services/MembershipService";
private static String MEMBERSHIP_GROUP_ID
	= "40894e36-15faf09a-0115-fb07f5dd-0008";
private static String DATA_SERVICE_ENDPOINT 
	= "https://hostname:8443/gria-basic-app-services/services/DataService";
private static String DATA_STAGER_ID
	= "40894e36-15f08ce0-0115-f0be917c-0009";

We declare a StateRepository and locate our Membership Group resource on the Membership Service.

StateRepository repository = new MemoryStateRepository();
RemoteMembershipService membershipService = (RemoteMembershipService)  
	repository.getOrCreateObject(RemoteMembershipService.class,ConversationID.getEPR(MEMBERSHIP_SERVICE_ENDPOINT));

MembershipGroupConversation group = null;
try {
	EndpointReferenceType eprs[] = membershipService.getResources();
			
	for (EndpointReferenceType epr : eprs){
		if(ConversationID.getConversationFromEPR(epr).equals(MEMBERSHIP_GROUP_ID)){
			group = repository.getOrCreateObject(MembershipGroupConversation.class,epr);
		}
	}
	
	if(group==null)
		throw new RuntimeException("No Group found with ID:"+MEMBERSHIP_GROUP_ID);
	else
		System.out.println("Found Group '"+ConversationID.getLabel(group.getEndpointRef())+"'");

} catch (RemoteException e) {
	throw new RuntimeException(e);
}

We follow the same pattern as above to locate the Data Stager on the Data Service.

RemoteDataService dataService = (RemoteDataService)  
	repository.getOrCreateObject(RemoteDataService.class,ConversationID.getEPR(DATA_SERVICE_ENDPOINT));

DataConversation data = null;
try {
	EndpointReferenceType eprs[] = dataService.getResources();
			
	for (EndpointReferenceType epr : eprs){
		if(ConversationID.getConversationFromEPR(epr).equals(DATA_STAGER_ID)){
			data = repository.getOrCreateObject(DataConversation.class,epr);
		}
	}
	if(data==null)
		throw new RuntimeException("No Data found with ID:"+DATA_STAGER_ID);
	else
	System.out.println("Found Data '"+ConversationID.getLabel(data.getEndpointRef())+"'");
} catch (RemoteException e) {
	throw new RuntimeException(e);
}

We then call addPolicyRule on the Data Stager to add a rule to it allowing access to anyone with the 'member' role on the group. We give subjects the 'reader' role on the Data Stager.  We identify the Membership Group by calling 'getMembershipPattern' on the group which will return a rule identifying itself.

try {
	data.addPolicyRule(new PolicyRule(group.getMembershipPattern(),"reader"));
			
	System.out.println("Added  Rule to '"+ConversationID.getLabel(data.getEndpointRef())+"' ("+DATA_STAGER_ID+")");
} catch (RemoteException e) {
	throw new RuntimeException(e);
}

Full Java Code

import java.rmi.RemoteException;

import org.apache.axis.message.addressing.EndpointReferenceType;

import uk.ac.soton.ecs.iam.grid.client.staterepos.MemoryStateRepository;
import uk.ac.soton.ecs.iam.grid.comms.client.DataConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteDataService;
import uk.ac.soton.ecs.iam.grid.comms.client.StateRepository;
import uk.ac.soton.itinnovation.grid.client.membership.MembershipGroupConversation;
import uk.ac.soton.itinnovation.grid.client.membership.RemoteMembershipService;
import uk.ac.soton.itinnovation.grid.types.ConversationID;
import uk.ac.soton.itinnovation.grid.types.MatchRule;
import uk.ac.soton.itinnovation.grid.types.PolicyRule;

public class MembershipGroupTutorial3 {

	private static String MEMBERSHIP_SERVICE_ENDPOINT 
			= "https://hostname:8443/gria-client-mgt/services/MembershipService";
	private static String MEMBERSHIP_GROUP_ID
			= "40894e36-15faf09a-0115-fb07f5dd-0008";
	private static String DATA_SERVICE_ENDPOINT 
			= "https://hostname:8443/gria-basic-app-services/services/DataService";
	private static String DATA_STAGER_ID
			= "40894e36-15f08ce0-0115-f0be917c-0009";

	public static void main(String[] args) {
		StateRepository repository = new MemoryStateRepository();
		RemoteMembershipService membershipService = (RemoteMembershipService)  
			repository.getOrCreateObject(RemoteMembershipService.class,
					ConversationID.getEPR(MEMBERSHIP_SERVICE_ENDPOINT));
		MembershipGroupConversation group = null;
		try {
			EndpointReferenceType eprs[] = membershipService.getResources();
			
			for (EndpointReferenceType epr : eprs){
				if(ConversationID.getConversationFromEPR(epr).equals(MEMBERSHIP_GROUP_ID)){
					group = repository.getOrCreateObject(MembershipGroupConversation.class,epr);
				}
			}
			if(group==null)
				throw new RuntimeException("No Group found with ID:"+MEMBERSHIP_GROUP_ID);
			else
				System.out.println("Found Group '"+ConversationID.getLabel(group.getEndpointRef())+"'");
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
		
		RemoteDataService dataService = (RemoteDataService)  
			repository.getOrCreateObject(RemoteDataService.class,
					ConversationID.getEPR(DATA_SERVICE_ENDPOINT));
		DataConversation data = null;
		try {
			EndpointReferenceType eprs[] = dataService.getResources();
			
			for (EndpointReferenceType epr : eprs){
				System.out.println(""+ConversationID.getConversationFromEPR(epr));
				if(ConversationID.getConversationFromEPR(epr).equals(DATA_STAGER_ID)){
					data = repository.getOrCreateObject(DataConversation.class,epr);
				}
			}
			if(data==null)
				throw new RuntimeException("No Data found with ID:"+DATA_STAGER_ID);
			else
				System.out.println("Found Data '"+ConversationID.getLabel(data.getEndpointRef())+"'");
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
		
		try {
			MatchRule rule = new MatchRule("EMAILADDRESS=Email, CN=CommonName, OU=OrganisationUnit, " +
				"O=Organisation, L=Locality, ST=State, C=Country",
				membershipService.getServiceProviderIssuer().getX509Certificate(),
				"reader",false);
			
			data.addPolicyRule(new PolicyRule(rule));
			
			System.out.println("Added  Rule to '"+ConversationID.getLabel(data.getEndpointRef())
					+"' ("+DATA_STAGER_ID+")");
			
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
		
		
	}

}

Membership Service Tutorial 4 - Using a Group to access a Resource

How to use a Membership Service to gain access to a resource.

First we declare the location of the Membership and Data Service, and the resource id's of the Data Stager and the Membership group

private static String MEMBERSHIP_SERVICE_ENDPOINT 
	= "https://hostname:8443/gria-client-mgt/services/MembershipService";
private static String MEMBERSHIP_GROUP_ID
	= "40894e36-15a43ff0-0115-a445d0c0-0001";
private static String DATA_SERVICE_ENDPOINT 
	= "https://hostname:8443/gria-basic-app-services/services/DataService";
private static String DATA_STAGER_ID
	= "40894e36-15f08ce0-0115-f0be917c-0009";

We need to setup a token cache which will store tokens from the membership group, and a InvocationListener which will add tokens from the cache to outgoing messages as a header.

private static TokenCache tokenCache = ImplementationFactory.getSingletonInstance(TokenCache.class);
UserInputHandler inputHandler = ImplementationFactory.getSingletonInstance(UserInputHandler.class);
inputHandler.addInvocationListener(new MembershipInvocationListener(tokenCache));

Find the Membership Group as before

StateRepository repository = new MemoryStateRepository();
RemoteMembershipService membershipService = (RemoteMembershipService)  
	repository.getOrCreateObject(RemoteMembershipService.class,
		ConversationID.getEPR(MEMBERSHIP_SERVICE_ENDPOINT));
MembershipGroupConversation group = null;
try {
	EndpointReferenceType eprs[] = membershipService.getResources();
			
	for (EndpointReferenceType epr : eprs){
		if(ConversationID.getConversationFromEPR(epr).equals(MEMBERSHIP_GROUP_ID)){
			group = repository.getOrCreateObject(MembershipGroupConversation.class,epr);
		}
	}
	
	if(group==null)
		throw new RuntimeException("No Group found with ID:"+MEMBERSHIP_GROUP_ID);
	else
		System.out.println("Found Group '"+ConversationID.getLabel(group.getEndpointRef())+"'");

} catch (RemoteException e) {
	throw new RuntimeException(e);
}

Set the Membership Group as the default group for this session, token will be added to messages from this group.

group.setMembershipGroup();

Now we access the Data Service with the Membership token attached to the message and should have access to the Data Stager.

RemoteDataService dataService = (RemoteDataService)  
	repository.getOrCreateObject(RemoteDataService.class,
		ConversationID.getEPR(DATA_SERVICE_ENDPOINT));
DataConversation data = null;
try {
	EndpointReferenceType eprs[] = dataService.getResources();
			
	for (EndpointReferenceType epr : eprs){
		if(ConversationID.getConversationFromEPR(epr).equals(DATA_STAGER_ID)){
			data = repository.getOrCreateObject(DataConversation.class,epr);
		}
	}
	
	if(data==null)
		throw new RuntimeException("No Data found with ID:"+DATA_STAGER_ID);
	else
		System.out.println("Found Data '"+ConversationID.getLabel(data.getEndpointRef())+"'");

} catch (RemoteException e) {
	throw new RuntimeException(e);
}

Full java Code

import java.rmi.RemoteException;

import org.apache.axis.message.addressing.EndpointReferenceType;

import uk.ac.soton.ecs.iam.grid.client.staterepos.MemoryStateRepository;
import uk.ac.soton.ecs.iam.grid.comms.client.DataConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteDataService;
import uk.ac.soton.ecs.iam.grid.comms.client.StateRepository;
import uk.ac.soton.ecs.iam.grid.comms.client.helpers.UserInputHandler;
import uk.ac.soton.ecs.iam.grid.utils.ImplementationFactory;
import uk.ac.soton.itinnovation.grid.client.membership.MembershipGroupConversation;
import uk.ac.soton.itinnovation.grid.client.membership.MembershipInvocationListener;
import uk.ac.soton.itinnovation.grid.client.membership.RemoteMembershipService;
import uk.ac.soton.itinnovation.grid.comms.client.TokenCache;
import uk.ac.soton.itinnovation.grid.comms.wstrust.WSTrust;
import uk.ac.soton.itinnovation.grid.types.ConversationID;

public class MembershipGroupTutorial4 {

	private static String MEMBERSHIP_SERVICE_ENDPOINT 
			= "https://hostname:8443/gria-client-mgt/services/MembershipService";
	private static String MEMBERSHIP_GROUP_ID
			= "40894e36-15a43ff0-0115-a445d0c0-0001";
	private static String DATA_SERVICE_ENDPOINT 
			= "https://hostname:8443/gria-basic-app-services/services/DataService";
	private static String DATA_STAGER_ID
			= "40894e36-15f08ce0-0115-f0be917c-0009";
	private static TokenCache tokenCache = ImplementationFactory.getSingletonInstance(TokenCache.class);

	public static void main(String[] args) {
		
		UserInputHandler inputHandler = ImplementationFactory.getSingletonInstance(UserInputHandler.class);
		inputHandler.addInvocationListener(new MembershipInvocationListener(tokenCache));
		
		StateRepository repository = new MemoryStateRepository();
		RemoteMembershipService membershipService = (RemoteMembershipService)  
			repository.getOrCreateObject(RemoteMembershipService.class,
					ConversationID.getEPR(MEMBERSHIP_SERVICE_ENDPOINT));
		MembershipGroupConversation group = null;
		try {
			EndpointReferenceType eprs[] = membershipService.getResources();
			
			for (EndpointReferenceType epr : eprs){
				if(ConversationID.getConversationFromEPR(epr).equals(MEMBERSHIP_GROUP_ID)){
					group = repository.getOrCreateObject(MembershipGroupConversation.class,epr);
				}
			}
			if(group==null)
				throw new RuntimeException("No Group found with ID:"+MEMBERSHIP_GROUP_ID);
			else
				System.out.println("Found Group '"+ConversationID.getLabel(group.getEndpointRef())+"'");
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
		
		group.setMembershipGroup();
		
		RemoteDataService dataService = (RemoteDataService)  
			repository.getOrCreateObject(RemoteDataService.class,
					ConversationID.getEPR(DATA_SERVICE_ENDPOINT));
		DataConversation data = null;
		try {
			EndpointReferenceType eprs[] = dataService.getResources();
			
			for (EndpointReferenceType epr : eprs){
				if(ConversationID.getConversationFromEPR(epr).equals(DATA_STAGER_ID)){
					data = repository.getOrCreateObject(DataConversation.class,epr);
				}
			}
			if(data==null)
				throw new RuntimeException("No Data found with ID:"+DATA_STAGER_ID);
			else
				System.out.println("Found Data '"+ConversationID.getLabel(data.getEndpointRef())+"'");
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
	}

}

Registry Service Tutorial 1 - Creating a Registry

How to create a registry

In order to create a Registry we must first contact the Registry Service and try to call 'createRegistry' on it. First, define the location of the Registry Service.

private static String REGISTRY_SERVICE_ENDPOINT 
			= "https://hostname:port/gria-client-mgt/services/CltMgtRegistryService";

Then create a proxy to the Registry Service.

		
StateRepository repository = new MemoryStateRepository();
RemoteCltMgtRegistryService registryService = (RemoteCltMgtRegistryService)  
	repository.getOrCreateObject(RemoteCltMgtRegistryService.class,
			ConversationID.getEPR(REGISTRY_SERVICE_ENDPOINT));

Then try to call 'createRegistry' and print out the EPR

try {
	CltMgtRegistryResourceConversation reg = registryService.createRegistry("Registry 1");
	System.out.println("Created Registry '"+ConversationID.getLabel(reg.getEndpointRef())+"'"+"\n"+reg.getEndpointRef());
} catch (RemoteException e) {
	throw new RuntimeException(e);
} catch (ObjectAlreadyExists e){
	throw new RuntimeException(e);
}

Full Java code

import java.rmi.RemoteException;
import uk.ac.soton.ecs.iam.grid.client.staterepos.MemoryStateRepository;
import uk.ac.soton.ecs.iam.grid.comms.client.ObjectAlreadyExists;
import uk.ac.soton.ecs.iam.grid.comms.client.StateRepository;
import uk.ac.soton.itinnovation.grid.client.registry.CltMgtRegistryResourceConversation;
import uk.ac.soton.itinnovation.grid.client.registry.RemoteCltMgtRegistryService;
import uk.ac.soton.itinnovation.grid.types.ConversationID;

public class RegistryTutorial1 {

	private static String REGISTRY_SERVICE_ENDPOINT 
			= "https://hostname:port/gria-client-mgt/services/CltMgtRegistryService";

	public static void main(String[] args) {
		
		StateRepository repository = new MemoryStateRepository();
		RemoteCltMgtRegistryService registryService = (RemoteCltMgtRegistryService)  
			repository.getOrCreateObject(RemoteCltMgtRegistryService.class,
					ConversationID.getEPR(REGISTRY_SERVICE_ENDPOINT));

		try {
			CltMgtRegistryResourceConversation reg = registryService.createRegistry("Registry 1");
			System.out.println("Created Registry '"+ConversationID.getLabel(reg.getEndpointRef())+"'"+"\n"+reg.getEndpointRef());
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		} catch (ObjectAlreadyExists e){
			throw new RuntimeException(e);
		}
	}

}

Registry Service Tutorial 2 - Adding Resources to a Registry

This tutorial describes how to add resources to a registry

In order to register resources in a registry we must first create a proxy to our Registry.

private static String REG_ID
		= "40894e36-16240474-0116-241035c3-0001";
private static String REGISTRY_SERVICE_ENDPOINT 
		= "https://hostname:port/gria-client-mgt/services/CltMgtRegistryService";

StateRepository repository = new MemoryStateRepository();
		RemoteCltMgtRegistryService registryService = (RemoteCltMgtRegistryService)  
			repository.getOrCreateObject(RemoteCltMgtRegistryService.class,
					ConversationID.getEPR(REGISTRY_SERVICE_ENDPOINT));

As an example, we will attempt to register resources from the data service

private static String DATA_SERVICE_ENDPOINT 
		= "https://hostname:port/gria-basic-app-services/services/DataService";

RemoteDataService dataService = (RemoteDataService)  
		repository.getOrCreateObject(RemoteDataService.class,
				ConversationID.getEPR(DATA_SERVICE_ENDPOINT));

Now we create a proxy to our registry resource

EndpointReferenceType regepr = ConversationID.getEPR(REGISTRY_SERVICE_ENDPOINT+"#"+REG_ID);
CltMgtRegistryResourceConversation reg = repository.getOrCreateObject(CltMgtRegistryResourceConversation.class,regepr);

Now try to register the resource in the registry using the 'registerResource' call.

Note : If you are trying to register an SLA in a registry you need to use 'registerMonitorableResource' as this sets an access control rule on the SLA Service allowing the Registry Service to monitor usage of the SLA.

try {
	X509Certificate x509 = dataService.getServiceProviderID().getX509Certificate();
	EndpointReferenceType eprs[] = dataService.getResources();
	for (EndpointReferenceType epr : eprs){
		ConversationID.addKeyInfo(epr,x509);
		ConversationID.setType(epr, DataConversation.class);
		reg.registerResource(epr);
		System.out.println("Adding '"+ConversationID.getLabel(epr)+"' to registry.");
	}		
} catch (Exception e) {
	throw new RuntimeException(e);
}

Full Java Code

package workflow;

import java.rmi.RemoteException;

import javax.swing.JOptionPane;

import org.apache.axis.message.addressing.EndpointReferenceType;

import com.sun.corba.se.spi.legacy.connection.GetEndPointInfoAgainException;

import uk.ac.soton.ecs.iam.grid.client.staterepos.MemoryStateRepository;
import uk.ac.soton.ecs.iam.grid.comms.client.Conversation;
import uk.ac.soton.ecs.iam.grid.comms.client.DataConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteDataService;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteService;
import uk.ac.soton.ecs.iam.grid.comms.client.SLAConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.StateRepository;
import uk.ac.soton.itinnovation.grid.client.registry.CltMgtRegistryResourceConversation;
import uk.ac.soton.itinnovation.grid.client.registry.RemoteCltMgtRegistryService;
import uk.ac.soton.itinnovation.grid.types.ConversationID;
import uk.ac.soton.itinnovation.grid.types.MatchRule;
import uk.ac.soton.itinnovation.grid.types.PolicyRule;
import uk.ac.soton.itinnovation.grid.types.SubjectDescription;

public class RegistryTutorial2 {

	private static String DATA_SERVICE_ENDPOINT 
		= "https://hostname:port/gria-basic-app-services/services/DataService";
	private static String REG_ID
		= "40894e36-16240474-0116-241035c3-0001";
	private static String REGISTRY_SERVICE_ENDPOINT 
			= "https://hostname:port/gria-client-mgt/services/CltMgtRegistryService";

	public static void main(String[] args) {
		
		StateRepository repository = new MemoryStateRepository();
		RemoteCltMgtRegistryService registryService = (RemoteCltMgtRegistryService)  
			repository.getOrCreateObject(RemoteCltMgtRegistryService.class,
					ConversationID.getEPR(REGISTRY_SERVICE_ENDPOINT));

		EndpointReferenceType regepr = ConversationID.getEPR(REGISTRY_SERVICE_ENDPOINT+"#"+REG_ID);
		CltMgtRegistryResourceConversation reg = repository.getOrCreateObject(CltMgtRegistryResourceConversation.class,regepr);
		
		RemoteDataService dataService = (RemoteDataService)  
		repository.getOrCreateObject(RemoteDataService.class,
				ConversationID.getEPR(DATA_SERVICE_ENDPOINT));
		try {
			X509Certificate x509 = dataService.getServiceProviderID().getX509Certificate();
			EndpointReferenceType eprs[] = dataService.getResources();
			for (EndpointReferenceType epr : eprs){
				ConversationID.addKeyInfo(epr,x509);
				ConversationID.setType(epr, DataConversation.class);
				reg.registerResource(epr);
				System.out.println("Adding '"+ConversationID.getLabel(epr)+"' to registry.");
			}		
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}

}

Registry Service Tutorial 3 - Discovering Resources in a Registry

This tutorial describes how to discover resources in a registry

We must declare the proxy to our Registry Service and find our registry resource.

private static String REG_ID
	= "40894e36-16240474-0116-241035c3-0001";
private static String REGISTRY_SERVICE_ENDPOINT 
	= "https://hostname:port/gria-client-mgt/services/CltMgtRegistryService";
		
StateRepository repository = new MemoryStateRepository();
	RemoteCltMgtRegistryService registryService = (RemoteCltMgtRegistryService)  
		repository.getOrCreateObject(RemoteCltMgtRegistryService.class,
			ConversationID.getEPR(REGISTRY_SERVICE_ENDPOINT));

CltMgtRegistryResourceConversation reg = null;
try {
	EndpointReferenceType[] eprs = registryService.getResources();
	for(EndpointReferenceType epr : eprs){
		if(ConversationID.getConversationFromEPR(epr).equals(REG_ID))
			reg = repository.getOrCreateObject(CltMgtRegistryResourceConversation.class,epr);
	}
} catch (RemoteException e) {
	throw new RuntimeException(e);
}
		
if(reg==null)
	throw new RuntimeException("Could not get Resource '"+REG_ID+"'");
else
	System.out.println("Found Registry '"+reg+"'");

Now we call getRegisteredResources on the Registry with the parameter 'Reference' and it returns a list of EPR's registered.

try {
	for(EndpointReferenceType epr : reg.getRegisteredResources("Reference"))
		System.out.println("Resource :"+ConversationID.getURLReferenceFromEPR(epr));
} catch (RemoteException e) {
	throw new RuntimeException(e);
}

Full Java Code

package workflow;

import java.rmi.RemoteException;

import javax.swing.JOptionPane;

import org.apache.axis.message.addressing.EndpointReferenceType;

import com.sun.corba.se.spi.legacy.connection.GetEndPointInfoAgainException;

import uk.ac.soton.ecs.iam.grid.client.staterepos.MemoryStateRepository;
import uk.ac.soton.ecs.iam.grid.comms.client.Conversation;
import uk.ac.soton.ecs.iam.grid.comms.client.DataConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteDataService;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteService;
import uk.ac.soton.ecs.iam.grid.comms.client.SLAConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.StateRepository;
import uk.ac.soton.itinnovation.grid.client.registry.CltMgtRegistryResourceConversation;
import uk.ac.soton.itinnovation.grid.client.registry.RemoteCltMgtRegistryService;
import uk.ac.soton.itinnovation.grid.types.ConversationID;
import uk.ac.soton.itinnovation.grid.types.MatchRule;
import uk.ac.soton.itinnovation.grid.types.PolicyRule;
import uk.ac.soton.itinnovation.grid.types.SubjectDescription;

public class RegistryTutorial3 {

	private static String REG_ID
		= "40894e36-16240474-0116-241035c3-0001";
	private static String REGISTRY_SERVICE_ENDPOINT 
			= "https://hostname:port/gria-client-mgt/services/CltMgtRegistryService";

	public static void main(String[] args) {
		
		StateRepository repository = new MemoryStateRepository();
		RemoteCltMgtRegistryService registryService = (RemoteCltMgtRegistryService)  
			repository.getOrCreateObject(RemoteCltMgtRegistryService.class,
					ConversationID.getEPR(REGISTRY_SERVICE_ENDPOINT));

		CltMgtRegistryResourceConversation reg = null;
		try {
			EndpointReferenceType[] eprs = registryService.getResources();
			
			for(EndpointReferenceType epr : eprs){
				if(ConversationID.getConversationFromEPR(epr).equals(REG_ID))
					reg = repository.getOrCreateObject(CltMgtRegistryResourceConversation.class,epr);
			}
			
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
		
		if(reg==null)
			throw new RuntimeException("Could not get Resource '"+REG_ID+"'");
		else
			System.out.println("Found Registry '"+reg+"'");
		
		try {
			for(EndpointReferenceType epr : reg.getRegisteredResources("Reference"))
				System.out.println("Resource :"+ConversationID.getURLReferenceFromEPR(epr));
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		}
	}

}