/* * @(#)RSClient_DII.java * * Copyright 2002 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms * */ package com.sun.eportal; import java.util.Hashtable; import java.util.Enumeration; import java.rmi.RemoteException; import javax.xml.rpc.Call; import javax.xml.rpc.Service; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException; import javax.xml.rpc.JAXRPCException; import javax.xml.namespace.QName; import javax.xml.rpc.encoding.TypeMapping; import javax.xml.rpc.encoding.TypeMappingRegistry; //import com.sun.xml.rpc.client.ServiceImpl; import com.sun.xml.rpc.client.dii.CallInvokerImpl; import com.sun.xml.rpc.client.dii.CallPropertyConstants; import com.sun.xml.rpc.encoding.Initializable; import com.sun.xml.rpc.encoding.TypeMappingImpl; import com.sun.xml.rpc.encoding.SerializerConstants; import com.sun.xml.rpc.encoding.TypeMappingRegistryImpl; import com.sun.xml.rpc.encoding.soap.SOAPConstants; import com.sun.xml.rpc.encoding.soap.StandardSOAPTypeMappings; import com.sun.xml.rpc.soap.streaming.SOAPNamespaceConstants; import com.sun.xml.rpc.encoding.CombinedSerializer; import com.sun.xml.rpc.encoding.SimpleTypeArraySerializer; import com.sun.xml.rpc.encoding.SimpleTypeSerializer; import com.sun.xml.rpc.encoding.simpletype.XSDStringEncoder; import javax.xml.rpc.encoding.SerializerFactory; import javax.xml.rpc.encoding.DeserializerFactory; import com.sun.xml.rpc.encoding.ObjectArraySerializer; import javax.xml.rpc.ServiceFactory; import com.sun.xml.rpc.encoding.TypeMappingImpl; import com.sun.xml.rpc.encoding.ReferenceableSerializerImpl; import com.sun.xml.rpc.encoding.SingletonSerializerFactory; import com.sun.xml.rpc.encoding.SingletonDeserializerFactory; import com.sun.xml.rpc.soap.streaming.SOAPNamespaceConstants; import com.sun.xml.rpc.wsdl.document.schema.SchemaConstants; /** * Retirement Service Client using DII client invocation mechanism * @author Ramesh Mandava and Edward Ort * */ public class RSClient_DII implements CallPropertyConstants,SerializerConstants, SchemaConstants, PortalConstants { QName fundInfoQname = null; QName fundInfoArrayTypeQname = null; QName fundInfoArrayElementQname = null; QName employeeInfoQname = null; ServiceFactory factory = null; public static void main(String[] args) { RSClient_DII rsClient_DII = new RSClient_DII(); String[] endpointArray = {"http://localhost:8080/PanAmericanRS/jaxrpc/RetirementServiceIF" , "http://localhost:8080/RetirementSpecialistsRS/jaxrpc/RetirementServiceIF" }; rsClient_DII.getAllAvailableFunds ( endpointArray ); } public RSClient_DII ( ) { try { factory = ServiceFactory.newInstance(); fundInfoQname = new QName(RETIREMENT_TYPE_NAMESPACE, "FundInfo"); fundInfoArrayTypeQname = new QName(RETIREMENT_TYPE_NAMESPACE , "ArrayOfFundInfo"); fundInfoArrayElementQname = new QName("" , "FundInfo"); employeeInfoQname = new QName ( RETIREMENT_TYPE_NAMESPACE, "EmployeeInfo" ); } catch ( Exception e ) { System.err.println("ERROR: Unable to create Service " + " Factory newInstance"); } } /** * This method aggregates all the available funds from different * endpoints ( Retirement Service Providers ) */ public Hashtable getAllAvailableFunds ( String[] endpoints ) { if ( ( endpoints == null ) || ( endpoints.length == 0 ) ) { System.out.println("ERROR: No Endpoints are available. " + " Potential registry bug? Using default one"); endpoints = new String[2]; endpoints[0] = "http://localhost:8080/PanAmericanRS/jaxrpc/RetirementServiceIF"; endpoints[1] = "http://localhost:8080/RetirementSpecialistsRS/jaxrpc/RetirementServiceIF" ; //return null; } Hashtable availableFundHash = new Hashtable(); for ( int i=0; i < endpoints.length; i++ ) { String[] currentFunds = getAvailableFunds( endpoints[i] ); if ( ( currentFunds != null ) && ( currentFunds.length > 0 ) ) { for ( int j=0; j 0.0 ) { availableQuotes.put( endpoints[i] , new Double (myQuote ) ); } } Enumeration keys = availableQuotes.keys(); while ( keys.hasMoreElements() ) { String epoint = (String) keys.nextElement(); System.out.println ( epoint + "::" + ((Double)availableQuotes.get(epoint ) ).toString() ); } return availableQuotes; } /** * This method gets quote from passed * endpoint ( Retirement Service Provider ) for the funds requested * and monthly investment choosen by Employee * @paramater endpointAddress endpoint address for one retirement service * provider * @paramater reqFundInfos Employee requested funds * @parameter mi monthly investment * @return double quote value */ public double getQuote ( String endpointAddress , FundInfo[] reqFundInfos, double mi ) { try { fundInfoQname = new QName(RETIREMENT_TYPE_NAMESPACE, "FundInfo"); fundInfoArrayTypeQname = new QName(RETIREMENT_TYPE_NAMESPACE , "ArrayOfFundInfo"); fundInfoArrayElementQname = new QName("" , "FundInfo"); Double quoteObject = new Double(0.0 ); Call call = newBeanCall(endpointAddress, RETIREMENT_SERVICE, RETIREMENT_BODY_NAMESPACE, RETIREMENT_PORT ); call.setReturnType(QNAME_TYPE_DOUBLE); call.setOperationName(new QName(RETIREMENT_BODY_NAMESPACE, "getQuote")); call.addParameter( "arrayOfFundInfo_1", fundInfoArrayTypeQname, ParameterMode.IN ); call.addParameter( "double_2", QNAME_TYPE_DOUBLE, ParameterMode.IN ); // call.setTargetEndpointAddress( endpointAddress ); Object[] params = new Object[2]; params[0] = reqFundInfos; params[1] = new Double( mi ); quoteObject = (Double)call.invoke( params ); System.out.println("Got the quote as => " + quoteObject ); return quoteObject.doubleValue(); } catch (Exception ex) { ex.printStackTrace(); return 0.0; } } /** * This method is used to update Employee information at the Service * provider at the passed * endpoint address * @paramater endpointAddress endpoint address for one retirement service * provider * @paramater ei New Employee Info * @return boolean value indicating the success of updateEmployeeInfo * operation */ public boolean updateEmployeeInfo ( String endpointAddress , EmployeeInfo ei) { boolean status = false; try { employeeInfoQname = new QName ( RETIREMENT_TYPE_NAMESPACE, "EmployeeInfo" ); Call call = newBeanCall(endpointAddress, RETIREMENT_SERVICE, RETIREMENT_BODY_NAMESPACE, RETIREMENT_PORT ) ; call.setReturnType(QNAME_TYPE_BOOLEAN); call.setOperationName(new QName(RETIREMENT_BODY_NAMESPACE, "updateEmployeeInfo")); call.addParameter( "EmployeeInfo_1", employeeInfoQname, ParameterMode.IN ); // call.setTargetEndpointAddress( endpointAddress ); Object[] params = new Object[1]; params[0] = ei; Boolean updateResult = (Boolean)call.invoke( params ); if ( updateResult == null ) { status = false; } else { status = updateResult.booleanValue(); } System.out.println("Return value for updateEmployeeInfo => " + updateResult ); } catch (Exception ex) { ex.printStackTrace(); } finally { return status ; } } /** * This method is used to confirm the transaction with one provider * when Employee is satisfied with the quote provided by that provider * @paramater endpointAddress endpoint address for one retirement service * provider * @paramater reqFundInfos User selected FundInfos * @paramater mi Employee selected monthly investment amount * @paramater ei Employee Information * @return String acknowledgement/confirmation message from provider */ public String confirmQuote ( String endpointAddress , FundInfo[] reqFundInfos, double mi , EmployeeInfo ei) { String confirmationMessage = new String("" ); try { Call call = newBeanCall(endpointAddress, RETIREMENT_SERVICE, RETIREMENT_BODY_NAMESPACE, RETIREMENT_PORT ); call.setReturnType(QNAME_TYPE_STRING); call.setOperationName(new QName(RETIREMENT_BODY_NAMESPACE, "confirmQuote")); call.addParameter( "arrayOfFundInfo_1", fundInfoArrayTypeQname, ParameterMode.IN ); call.addParameter( "double_2", QNAME_TYPE_DOUBLE, ParameterMode.IN ); call.addParameter( "EmployeeInfo_3", employeeInfoQname, ParameterMode.IN ); Object[] params = new Object[3]; params[0] = reqFundInfos; params[1] = new Double( mi ); params[2] = ei; confirmationMessage = (String)call.invoke( params ); System.out.println("Got the Confirmation Message as => " + confirmationMessage ); } catch (Exception ex) { ex.printStackTrace(); } finally { return confirmationMessage; } } /** * Gets the available funds from the proivder at particular endpoint * address. * @paramater endpointAddress endpoint address for one retirement service * provider * @return String[] Array of availablefunds */ public String [] getAvailableFunds ( String endpointAddress ) { try { String bodyNamespaceValue = RETIREMENT_BODY_NAMESPACE; QName stringQname = new QName(SOAPNamespaceConstants.XSD, "string"); QName stringArrayTypeQname = new QName(RETIREMENT_TYPE_NAMESPACE , "ArrayOfString"); CombinedSerializer stringSerializer = new SimpleTypeSerializer( QNAME_TYPE_STRING, DONT_ENCODE_TYPE, NULLABLE, SOAPConstants.URI_ENCODING, XSDStringEncoder.getInstance()); CombinedSerializer stringArraySerializer = new SimpleTypeArraySerializer(stringArrayTypeQname, ENCODE_TYPE, NULLABLE, SOAPConstants.URI_ENCODING, null, QNAME_TYPE_STRING, String.class, 1, null, (SimpleTypeSerializer)stringSerializer); stringArraySerializer = new ReferenceableSerializerImpl( SERIALIZE_AS_REF, stringArraySerializer); SerializerFactory stringArraySerializerFactory = new SingletonSerializerFactory(stringArraySerializer); DeserializerFactory stringArrayDeserializerFactory = new SingletonDeserializerFactory(stringArraySerializer); ServiceFactory factory = ServiceFactory.newInstance(); Service service = factory.createService(new QName("PanAmericanRS")); TypeMappingRegistry registry = service.getTypeMappingRegistry( ); TypeMapping typeMapping = registry.getTypeMapping( SOAPConstants.URI_ENCODING ); typeMapping.register( String[].class, stringArrayTypeQname, stringArraySerializerFactory, stringArrayDeserializerFactory ); QName port = new QName(bodyNamespaceValue, "RetirementServiceIF"); Call call = service.createCall(); call.setPortTypeName(port); call.setTargetEndpointAddress(endpointAddress); call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean(true)); call.setProperty(Call.SOAPACTION_URI_PROPERTY, ""); call.setProperty(ENCODING_STYLE_PROPERTY, SOAPConstants.URI_ENCODING); call.setReturnType(stringArrayTypeQname); call.setOperationName(new QName(bodyNamespaceValue, "getAvailableFunds")); System.out.println("Invoking available Funds"); String[] avFunds = (String[])call.invoke(null); if ( avFunds != null ) { for ( int i=0; i< avFunds.length; i++ ) { System.out.println(" DII -Available Fund[" + i + " ]-> " + avFunds[i] ); } } else { System.out.println("Available Funds returned null"); } return avFunds; } catch (Exception ex) { ex.printStackTrace(); return null; } } /** * Creates a Call object for service at particular endpoint address. * Sets the port type name and endpoint address for the Call object. */ Call newBeanCall(String endpointAddress, String serviceName, String namespace, String portName ) throws Exception { CombinedSerializer fundInfoSerializer = new FundInfo_SOAPSerializer(fundInfoQname, ENCODE_TYPE, NULLABLE, SOAPConstants.URI_ENCODING); fundInfoSerializer = new ReferenceableSerializerImpl(SERIALIZE_AS_REF, fundInfoSerializer); SerializerFactory fundInfoSerializerFactory = new SingletonSerializerFactory(fundInfoSerializer); DeserializerFactory fundInfoDeserializerFactory = new SingletonDeserializerFactory(fundInfoSerializer); CombinedSerializer employeeInfoSerializer = new EmployeeInfo_SOAPSerializer(employeeInfoQname, ENCODE_TYPE, NULLABLE, SOAPConstants.URI_ENCODING); employeeInfoSerializer = new ReferenceableSerializerImpl( SERIALIZE_AS_REF, employeeInfoSerializer); SerializerFactory employeeInfoSerializerFactory = new SingletonSerializerFactory(employeeInfoSerializer); DeserializerFactory employeeInfoDeserializerFactory = new SingletonDeserializerFactory(employeeInfoSerializer); CombinedSerializer fundInfoArraySerializer = new ObjectArraySerializer(fundInfoArrayTypeQname, ENCODE_TYPE, NULLABLE, SOAPConstants.URI_ENCODING,fundInfoArrayElementQname, fundInfoQname, FundInfo.class, 1, null); fundInfoArraySerializer = new ReferenceableSerializerImpl( SERIALIZE_AS_REF, fundInfoArraySerializer); SingletonSerializerFactory fundInfoArraySerializerFactory = new SingletonSerializerFactory(fundInfoArraySerializer); SingletonDeserializerFactory fundInfoArrayDeserializerFactory = new SingletonDeserializerFactory(fundInfoArraySerializer); Service service = factory.createService(new QName(serviceName)); QName port = new QName(namespace, portName); TypeMappingRegistry registry = service.getTypeMappingRegistry(); TypeMapping typeMapping = registry.getTypeMapping( SOAPConstants.URI_ENCODING); typeMapping.register(FundInfo.class, fundInfoQname, fundInfoSerializerFactory, fundInfoDeserializerFactory); typeMapping.register(FundInfo[].class, fundInfoArrayTypeQname, fundInfoArraySerializerFactory, fundInfoArrayDeserializerFactory); typeMapping.register(EmployeeInfo.class, employeeInfoQname, employeeInfoSerializerFactory, employeeInfoDeserializerFactory); Call call = newCall( endpointAddress, service, port); return call; } /** * Creates a simple Call object for service. * Sets the port type name and endpoint address for the Call object. */ Call newCall(String endpoint,Service service,QName port) throws Exception { Call call = service.createCall(); call.setPortTypeName(port); call.setTargetEndpointAddress(endpoint); call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean(true)); call.setProperty(Call.SOAPACTION_URI_PROPERTY, ""); call.setProperty(ENCODING_STYLE_PROPERTY, SOAPConstants.URI_ENCODING); return call; } }