Client Initialisation
Introduction
Before invoking an operation on a GRIA service, there are various standard objects that must be created and connected together (summarised in the key concepts and classes page). The GRIA "gridcli" client uses the Spring Framework to configure and build these objects (see the client-beans.xml file in the client distribution). As GRIA groovy scripts are run through the gridcli command the objects are already available to groovy. Java client programs can either use Spring to get hold of the objects or they can create them as described below.
The code below is available in the TutorialHelpers.java class file and is used by the subsequent tutorials.
Code
There are many different "identity providers" available. For this example code we will use the TestIdentityProvider which creates new (self-signed) identities. For production code, the KeyStoreIdentityProvider is often what is required.
Create a test identity of a user 'bob':
IdentityProvider idp = new TestIdentityProvider("bob");
Create the 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 the InteractiveX509TrustManager which displays a dialogue box asking the user if they trust the service's certificate, 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 the EndpointReferences (addresses) of services and resources:
ArrayListregistries = new ArrayList (); 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 adds the appropriate management context (SLAs, Trade Accounts) to the message 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);