/////////////////////////////////////////////////////////////////////////
//
// © 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:          19/05/2008
//  Created for Project:   CRISP
//
/////////////////////////////////////////////////////////////////////////
//
//  Dependencies : none
//
/////////////////////////////////////////////////////////////////////////
//
//  Last commit info:      $Author: scp $
//                         $Date: 2008-06-02 16:32:39 +0100 (Mon, 02 Jun 2008) $
//                         $Revision: 9658 $
//
/////////////////////////////////////////////////////////////////////////

package uk.ac.soton.itinnovation.grid.client.tutorial;

import java.util.ArrayList;
import uk.ac.soton.ecs.iam.grid.client.swing.SwingInputHandler;
import uk.ac.soton.ecs.iam.grid.comms.client.AxisTransport;
import uk.ac.soton.itinnovation.grid.client.engine.impl.DefaultAttributeSelector;
import uk.ac.soton.itinnovation.grid.client.engine.impl.DefaultFederationSelector;
import uk.ac.soton.itinnovation.grid.client.engine.impl.DefaultInvocationEngine;
import uk.ac.soton.itinnovation.grid.client.engine.impl.LocalRegistry;
import uk.ac.soton.itinnovation.grid.client.plugins.GridClientPluginManager;
import uk.ac.soton.itinnovation.grid.client.proxy.HelperProxyFactory;
import uk.ac.soton.itinnovation.grid.client.proxy.InvocationEngineProxyFactory;
import uk.ac.soton.itinnovation.grid.client.proxy.ProxyFactory;
import uk.ac.soton.itinnovation.grid.comms.client.ITInnovSocketFactory;
import uk.ac.soton.itinnovation.grid.comms.client.InteractiveX509TrustManager;
import uk.ac.soton.itinnovation.grid.comms.client.SAMLTokenCache;
import uk.ac.soton.itinnovation.grid.comms.client.WSDLCache;
import uk.ac.soton.itinnovation.grid.comms.client.WSDLCacheImpl;
import uk.ac.soton.itinnovation.grid.service.types.SimpleRegistry;
import uk.ac.soton.itinnovation.grid.utils.CertificateTrustValidator;
import uk.ac.soton.itinnovation.grid.utils.IdentityProvider;
import uk.ac.soton.itinnovation.grid.utils.TestIdentityProvider;
import uk.ac.soton.itinnovation.grid.utils.Transport;

/**
 *
 * @author mm
 */
class TutorialHelpers {
	
	ProxyFactory proxyFactory = null;
	DefaultInvocationEngine engine = null;
	AxisTransport transport = null;

	public TutorialHelpers() {
		
		/* Create a test identity of a user 'bob'
		 */
		IdentityProvider idp = new TestIdentityProvider("bob");
		
		/* Create transport object which invokes operations
		 */
		transport = new AxisTransport(idp);

		/* We need a cache to store our wsdl files
		 */
		WSDLCache cache = new WSDLCacheImpl(transport);
		
		/* When we communicate with services we need to choose which 
		 * services we trust, we do this using a TrustValidator
		 * This example uses a graphical validator InteractiveX509TrustManager
		 * but a non interative TestIdentityProvider.TRUST_EVERYONE
		 * can be used but only for testing. It should NOT be used a 
		 * real deployment.
		 */
		CertificateTrustValidator manager = new InteractiveX509TrustManager(new SwingInputHandler());
		//CertificateTrustValidator manager = TestIdentityProvider.TRUST_EVERYONE;
		transport.setCertificateTrustValidator(manager);
		ITInnovSocketFactory.setHandlers(idp, manager);
		
		/* Define a registry where we will store EndpointReference's
		 * of services and resources
		 */
		ArrayList<SimpleRegistry> registries = new ArrayList<SimpleRegistry>();
		registries.add(new LocalRegistry());
		
		/* Define a cache for any tokens we get from Membership Groups
		 */
		SAMLTokenCache tokenCache = new SAMLTokenCache();
		
		/* Load the GRIA plugins.
		 */
		GridClientPluginManager pluginManager = new GridClientPluginManager();
		pluginManager.loadPlugins();
		
		/* Define an engine which uses the transport to invoke 
		 * operations
		 */
		engine = new DefaultInvocationEngine(transport);

		/* Add the registry to the engine
		 */ 
		engine.setSelectedRegistries(registries);
		
		/* Tell the engine how to find tokens by defining an
		 * attribute selector, which stores tokens in the token cache
		 */
		engine.setAttributeSelector(new DefaultAttributeSelector(tokenCache));
		
		/* Tell the engine to use a federation selector, which finds 
		 * management context (SLAs, Trade Accounts) when invoking an 
		 * operation.
		 */
		engine.setFederationSelector(new DefaultFederationSelector());
		
		/* The proxy factory creates proxy object from 
		 * EndpointReference's which help us communicate with 
		 * the services. The helper proxy factory adds some additional 
		 * useful methods
		 */
		ProxyFactory rawProxyFactory = new InvocationEngineProxyFactory(engine, cache);
		proxyFactory = new HelperProxyFactory(rawProxyFactory, pluginManager.getHelperRegistry());
		engine.setProxyFactory(proxyFactory);
	}

	ProxyFactory getFactory() {
		return proxyFactory;
	}
	
	SimpleRegistry getRegistry() {
		return engine.getSelectedRegistries().get(0);
	}
	
	Transport getTransport() {
		return transport;
	}

}
