/////////////////////////////////////////////////////////////////////////
//
// © University of Southampton IT Innovation Centre, 2008
//
// Copyright in this library belongs to the University of Southampton
// University Road, Highfield, Southampton, UK, SO17 1BJ
//
// This software may not be used, sold, licensed, transferred, copied
// or reproduced in whole or in part in any manner or form or in or
// on any media by any person other than in accordance with the terms
// of the Licence Agreement supplied with the software, or otherwise
// without the prior written consent of the copyright owners.
//
// This software is distributed WITHOUT ANY WARRANTY, without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE, except where stated in the Licence Agreement supplied with
// the software.
//
//  Created By:            Mark McArdle
//  Created Date:          20/05/2008
//  Created for Project:   CRISP
//
/////////////////////////////////////////////////////////////////////////
//
//  Dependencies : none
//
/////////////////////////////////////////////////////////////////////////
//
//  Last commit info:      $Author: scp $
//                         $Date: 2008-07-09 11:17:09 +0100 (Wed, 09 Jul 2008) $
//                         $Revision: 9907 $
//
/////////////////////////////////////////////////////////////////////////

package uk.ac.soton.itinnovation.grid.client.tutorial;

import java.rmi.RemoteException;
import org.apache.axis.message.addressing.EndpointReferenceType;
import uk.ac.soton.ecs.iam.grid.comms.client.DataConversation;
import uk.ac.soton.ecs.iam.grid.comms.client.RemoteDataService;
import uk.ac.soton.itinnovation.grid.client.proxy.ProxyFactory;
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;

/**
 *
 * @author mm
 */
public class RegistryTutorial {
	
	static String REGISTRY_SERVICE_ENDPOINT = null;
	static String DATA_SERVICE_ENDPOINT = null;

	public RegistryTutorial(String registryservice_url, String dataservice_url){
		REGISTRY_SERVICE_ENDPOINT = registryservice_url;
		DATA_SERVICE_ENDPOINT = dataservice_url;
	}
	
	public static void main(String args[]) throws Exception{

		/* Create a TutorialHelpers object and get a ProxyFactory
		 * object from it
		 */
		TutorialHelpers helpers = new TutorialHelpers();
		ProxyFactory proxyFactory = helpers.getFactory();
		
		/* Create a Proxy to the Registry Service
		 */
		RemoteCltMgtRegistryService registryService =   
			proxyFactory.createProxy(ConversationID.getEPR(REGISTRY_SERVICE_ENDPOINT),RemoteCltMgtRegistryService.class);

		/* Create a Registry at the Registry Service
		 */
		CltMgtRegistryResourceConversation registry = registryService.createRegistry("My Registry");

		/* Create a Proxy to the Data Service
		 */
		RemoteDataService dataService = proxyFactory.createProxy(ConversationID.getEPR(DATA_SERVICE_ENDPOINT),RemoteDataService.class);

		/* Create a Data Stager at the Data Service
		 */
		DataConversation data = dataService.createStagingArea("My Data");

		try {
			/* Register the data stagers EPR at the registry
			 */
			registry.registerResource(data.getEndpointRef());
			
			/* In order to discover resources in the registry
			 * call getResources on the registry.
			 */
			for (EndpointReferenceType epr : registry.getRegisteredResources(null)) {
				System.out.println("Resource :" + ConversationID.getURLReferenceFromEPR(epr));
			}
			
			/* Now we can unregister the data stager
			 */			
			registry.unregisterResource(data.getEndpointRef());
			
		} catch (RemoteException e) {
			throw new RuntimeException(e);
		} finally {
			/* Clean up resources at the services
			 */
			data.destroy();
			registry.destroy();
		}
	}
}
