/////////////////////////////////////////////////////////////////////////
//
// © 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 :			2008-02-28
//	Created for Project :		CRISP
//
/////////////////////////////////////////////////////////////////////////
//
//  Dependencies : none
//
/////////////////////////////////////////////////////////////////////////
//
//	Last commit info :  $Author: mm $
//                          $Date: 2008-05-19 17:19:44 +0100 (Mon, 19 May 2008) $
//                          $Revision: 9561 $
//
/////////////////////////////////////////////////////////////////////////

package uk.ac.soton.itinnovation.grid.client.tutorial;

import uk.ac.soton.itinnovation.grid.client.proxy.ProxyFactory;
import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import org.apache.axis.utils.XMLUtils;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;

import uk.ac.soton.ecs.iam.grid.comms.client.RemoteJobService;
import uk.ac.soton.itinnovation.grid.types.ConversationID;

public class ApplicationDiscovery {
	private static Logger logger = Logger.getLogger(ApplicationDiscovery.class);

	private static String JOB_SERVICE_ENDPOINT = "https://.../gria-basic-app-services/services/JobService";

	public ApplicationDiscovery(String jobservice) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
		this.JOB_SERVICE_ENDPOINT = jobservice;
	}

	public static void main(String[] args) throws Exception {
		
		/* Create a Proxy Factory using the Tutorial Helpers
		 */
		ProxyFactory proxyFactory = new TutorialHelpers().getFactory();	
		
		/* Use the Proxy Factory to create a Proxy to the Job Service
		 */
		RemoteJobService jobService = proxyFactory.createProxy(ConversationID.getEPR(JOB_SERVICE_ENDPOINT), RemoteJobService.class);

		/* Call getApplications on the Job Service returning a list
		 * of applications available.
		 */
		String[] applicationList = jobService.getApplications();

		/* Loop through these applications, and print out there details
		 * by invoking another method getApplicationMetadataDetailed
		 */
		if(applicationList.length > 0){
			System.out.println("Found "+applicationList.length+" applications.");
			for(String applicationURI : applicationList){
				Document appDescription = jobService.getApplicationMetadataDetailed(applicationURI);
				String strApplicationDescription = XMLUtils.DocumentToString(appDescription);
				System.out.println("\t"+strApplicationDescription);
			}
		}else{
			System.out.println("No applications found on server.");
		}
	}
}
