All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.refinitiv.ema.access.ServiceEndpointDiscoveryImpl Maven / Gradle / Ivy

There is a newer version: 3.8.2.0
Show newest version
///*|-----------------------------------------------------------------------------
// *|            This source code is provided under the Apache 2.0 license      --
// *|  and is provided AS IS with no warranty or guarantee of fit for purpose.  --
// *|                See the project's LICENSE.md for details.                  --
// *|           Copyright (C) 2019-2022 Refinitiv. All rights reserved.         --
///*|-----------------------------------------------------------------------------

package com.refinitiv.ema.access;
import java.util.concurrent.locks.ReentrantLock;

import com.refinitiv.ema.access.ServiceEndpointDiscovery;
import com.refinitiv.ema.access.ServiceEndpointDiscoveryClient;
import com.refinitiv.ema.access.ServiceEndpointDiscoveryOption;
import com.refinitiv.eta.codec.Buffer;
import com.refinitiv.eta.codec.CodecFactory;
import com.refinitiv.eta.valueadd.reactor.Reactor;
import com.refinitiv.eta.valueadd.reactor.ReactorCallbackReturnCodes;
import com.refinitiv.eta.valueadd.reactor.ReactorDiscoveryDataFormatProtocol;
import com.refinitiv.eta.valueadd.reactor.ReactorDiscoveryTransportProtocol;
import com.refinitiv.eta.valueadd.reactor.ReactorErrorInfo;
import com.refinitiv.eta.valueadd.reactor.ReactorFactory;
import com.refinitiv.eta.valueadd.reactor.ReactorOptions;
import com.refinitiv.eta.valueadd.reactor.ReactorReturnCodes;
import com.refinitiv.eta.valueadd.reactor.ReactorServiceDiscoveryOptions;
import com.refinitiv.eta.valueadd.reactor.ReactorServiceEndpointEvent;
import com.refinitiv.eta.valueadd.reactor.ReactorServiceEndpointEventCallback;
import com.refinitiv.eta.valueadd.reactor.ReactorServiceEndpointInfo;

class ServiceEndpointDiscoveryImpl implements ServiceEndpointDiscovery, ReactorServiceEndpointEventCallback
{
	private Reactor _reactor;
	private ReactorErrorInfo _reactorErrorInfo = ReactorFactory.createReactorErrorInfo();
	private StringBuilder _strBuilder;
	private ReentrantLock _userLock = new java.util.concurrent.locks.ReentrantLock();
	private ReactorServiceDiscoveryOptions _reactorServiceDiscoveryOptions = ReactorFactory.createReactorServiceDiscoveryOptions();
	private OmmInvalidUsageExceptionImpl _ommIUExcept;
	private ServiceEndpointDiscoveryRespImpl _serviceEndpointDiscoveryRespImpl = new ServiceEndpointDiscoveryRespImpl();
	private ServiceEndpointDiscoveryEventImpl _ServiceEndpointDiscoveryEventImpl = new ServiceEndpointDiscoveryEventImpl();
	private ServiceEndpointDiscoveryClient _client;
	private Buffer _tokenServiceURLV1Buf = CodecFactory.createBuffer();
	private Buffer _tokenServiceURLV2Buf = CodecFactory.createBuffer();
	private Buffer _serviceDiscoveryUrlBuf = CodecFactory.createBuffer();
	
	ServiceEndpointDiscoveryImpl()
	{
		ReactorOptions reactorOptions = ReactorFactory.createReactorOptions();
		reactorOptions.clear();
		_reactorErrorInfo.clear();
		
		reactorOptions.userSpecObj(this);
		_reactor = ReactorFactory.createReactor(reactorOptions, _reactorErrorInfo);
		if(_reactorErrorInfo.code() != ReactorReturnCodes.SUCCESS)
		{
			strBuilder().append("Failed to create ServiceEndpointDiscoveryImpl (ReactorFactory.createReactor).")
			.append("' Error Id='").append(_reactorErrorInfo.error().errorId()).append("' Internal sysError='")
			.append(_reactorErrorInfo.error().sysError()).append("' Error Location='")
			.append(_reactorErrorInfo.location()).append("' Error Text='")
			.append(_reactorErrorInfo.error().text()).append("'. ");
			
			throw ommIUExcept().message(_strBuilder.toString(), OmmInvalidUsageException.ErrorCode.INTERNAL_ERROR);
		}
	}
	
	ServiceEndpointDiscoveryImpl(String tokenServiceUrl, String serviceDiscoveryUrl)
	{
		ReactorOptions reactorOptions = ReactorFactory.createReactorOptions();
		reactorOptions.clear();
		_reactorErrorInfo.clear();
		
		reactorOptions.userSpecObj(this);
		
		if ((tokenServiceUrl != null) && (!tokenServiceUrl.isEmpty()))
		{
			_tokenServiceURLV1Buf.data(tokenServiceUrl);
			reactorOptions.tokenServiceURL(_tokenServiceURLV1Buf);
		}
		
		if((serviceDiscoveryUrl != null) && (!serviceDiscoveryUrl.isEmpty()))
		{
			_serviceDiscoveryUrlBuf.data(serviceDiscoveryUrl);
			reactorOptions.serviceDiscoveryURL(_serviceDiscoveryUrlBuf);
		}
		
		_reactor = ReactorFactory.createReactor(reactorOptions, _reactorErrorInfo);
		if(_reactorErrorInfo.code() != ReactorReturnCodes.SUCCESS)
		{
			strBuilder().append("Failed to create ServiceEndpointDiscoveryImpl (ReactorFactory.createReactor).")
			.append("' Error Id='").append(_reactorErrorInfo.error().errorId()).append("' Internal sysError='")
			.append(_reactorErrorInfo.error().sysError()).append("' Error Location='")
			.append(_reactorErrorInfo.location()).append("' Error Text='")
			.append(_reactorErrorInfo.error().text()).append("'. ");
			
			throw ommIUExcept().message(_strBuilder.toString(), OmmInvalidUsageException.ErrorCode.INTERNAL_ERROR);
		}
	}
	
	ServiceEndpointDiscoveryImpl(String tokenServiceUrlV1, String tokenServiceUrlV2, String serviceDiscoveryUrl)
	{
		ReactorOptions reactorOptions = ReactorFactory.createReactorOptions();
		reactorOptions.clear();
		_reactorErrorInfo.clear();
		
		reactorOptions.userSpecObj(this);
		
		if ((tokenServiceUrlV1 != null) && (!tokenServiceUrlV1.isEmpty()))
		{
			_tokenServiceURLV1Buf.data(tokenServiceUrlV1);
			reactorOptions.tokenServiceURL(_tokenServiceURLV1Buf);
		}
		
		if ((tokenServiceUrlV2 != null) && (!tokenServiceUrlV2.isEmpty()))
		{
			_tokenServiceURLV2Buf.data(tokenServiceUrlV2);
			reactorOptions.tokenServiceURL_V2(_tokenServiceURLV2Buf);
		}
		
		if((serviceDiscoveryUrl != null) && (!serviceDiscoveryUrl.isEmpty()))
		{
			_serviceDiscoveryUrlBuf.data(serviceDiscoveryUrl);
			reactorOptions.serviceDiscoveryURL(_serviceDiscoveryUrlBuf);
		}
		
		_reactor = ReactorFactory.createReactor(reactorOptions, _reactorErrorInfo);
		if(_reactorErrorInfo.code() != ReactorReturnCodes.SUCCESS)
		{
			strBuilder().append("Failed to create ServiceEndpointDiscoveryImpl (ReactorFactory.createReactor).")
			.append("' Error Id='").append(_reactorErrorInfo.error().errorId()).append("' Internal sysError='")
			.append(_reactorErrorInfo.error().sysError()).append("' Error Location='")
			.append(_reactorErrorInfo.location()).append("' Error Text='")
			.append(_reactorErrorInfo.error().text()).append("'. ");
			
			throw ommIUExcept().message(_strBuilder.toString(), OmmInvalidUsageException.ErrorCode.INTERNAL_ERROR);
		}
	}
	
	@Override
	public void registerClient(ServiceEndpointDiscoveryOption params, ServiceEndpointDiscoveryClient client)
	{
		if (client == null) {
			throw ommIUExcept().message("Client not set", OmmInvalidUsageException.ErrorCode.INVALID_ARGUMENT);
		}

		registerClient(params, client, null);
	}

	@Override
	public void registerClient(ServiceEndpointDiscoveryOption params, ServiceEndpointDiscoveryClient client, Object closure)
	{
		if (client == null) {
			throw ommIUExcept().message("Client not set", OmmInvalidUsageException.ErrorCode.INVALID_ARGUMENT);
		}

		_userLock.lock();
		try
		{
			ServiceEndpointDiscoveryOptionImpl serviceEndpointDiscoveryOptionImpl = (ServiceEndpointDiscoveryOptionImpl)params;
			_reactorErrorInfo.clear();
			_reactorServiceDiscoveryOptions.clear();
			
			_reactorServiceDiscoveryOptions.userName(serviceEndpointDiscoveryOptionImpl._userName);
			_reactorServiceDiscoveryOptions.password(serviceEndpointDiscoveryOptionImpl._password);
			_reactorServiceDiscoveryOptions.clientId(serviceEndpointDiscoveryOptionImpl._clientId);
			_reactorServiceDiscoveryOptions.clientSecret(serviceEndpointDiscoveryOptionImpl._clientSecret);
			_reactorServiceDiscoveryOptions.clientJWK(serviceEndpointDiscoveryOptionImpl._clientJwk);
			_reactorServiceDiscoveryOptions.tokenScope(serviceEndpointDiscoveryOptionImpl._tokenScope);
			_reactorServiceDiscoveryOptions.audience(serviceEndpointDiscoveryOptionImpl._audience);
			_reactorServiceDiscoveryOptions.transport(serviceEndpointDiscoveryOptionImpl._transport);
			_reactorServiceDiscoveryOptions.dataFormat(serviceEndpointDiscoveryOptionImpl._dataFormat);
			_reactorServiceDiscoveryOptions.takeExclusiveSignOnControl(serviceEndpointDiscoveryOptionImpl._takeExclusiveSignOnControl);
			
			if(serviceEndpointDiscoveryOptionImpl._proxyHostName != null && !serviceEndpointDiscoveryOptionImpl._proxyHostName.isEmpty())
			{
				_reactorServiceDiscoveryOptions.proxyHostName().data(serviceEndpointDiscoveryOptionImpl._proxyHostName);
			}
			
			if(serviceEndpointDiscoveryOptionImpl._proxyPort != null && !serviceEndpointDiscoveryOptionImpl._proxyPort.isEmpty())
			{
				_reactorServiceDiscoveryOptions.proxyPort().data(serviceEndpointDiscoveryOptionImpl._proxyPort);
			}
			
			if(serviceEndpointDiscoveryOptionImpl._proxyUserName != null && !serviceEndpointDiscoveryOptionImpl._proxyUserName.isEmpty())
			{
				_reactorServiceDiscoveryOptions.proxyUserName().data(serviceEndpointDiscoveryOptionImpl._proxyUserName);
			}
			
			if(serviceEndpointDiscoveryOptionImpl._proxyPassword != null && !serviceEndpointDiscoveryOptionImpl._proxyPassword.isEmpty())
			{
				_reactorServiceDiscoveryOptions.proxyPassword().data(serviceEndpointDiscoveryOptionImpl._proxyPassword);
			}
			
			if(serviceEndpointDiscoveryOptionImpl._proxyDomain != null && !serviceEndpointDiscoveryOptionImpl._proxyDomain.isEmpty())
			{
				_reactorServiceDiscoveryOptions.proxyDomain().data(serviceEndpointDiscoveryOptionImpl._proxyDomain);
			}
			
			if(serviceEndpointDiscoveryOptionImpl.proxyLocalHostName() != null && !serviceEndpointDiscoveryOptionImpl.proxyLocalHostName().isEmpty())
			{
				_reactorServiceDiscoveryOptions.proxyLocalHostName().data(serviceEndpointDiscoveryOptionImpl.proxyLocalHostName());
			}
			
			if(serviceEndpointDiscoveryOptionImpl._proxyKrb5ConfigFile != null && !serviceEndpointDiscoveryOptionImpl._proxyKrb5ConfigFile.isEmpty())
			{
				_reactorServiceDiscoveryOptions.proxyKRB5ConfigFile().data(serviceEndpointDiscoveryOptionImpl._proxyKrb5ConfigFile);
			}
			
			_reactorServiceDiscoveryOptions.reactorServiceEndpointEventCallback(this);
			_client = client;
			
			if(closure != null) _reactorServiceDiscoveryOptions.userSpecObj(closure);
			
			switch(serviceEndpointDiscoveryOptionImpl._transport)
			{
			case ServiceEndpointDiscoveryOption.TransportProtocol.UNKNOWN:
				break;
			case ServiceEndpointDiscoveryOption.TransportProtocol.TCP:
				_reactorServiceDiscoveryOptions.transport(ReactorDiscoveryTransportProtocol.RD_TP_TCP);
				break;
			case ServiceEndpointDiscoveryOption.TransportProtocol.WEB_SOCKET:
				_reactorServiceDiscoveryOptions.transport(ReactorDiscoveryTransportProtocol.RD_TP_WEBSOCKET);
				break;
			default:
				strBuilder().append("Invalid transport protocol ").append(serviceEndpointDiscoveryOptionImpl._transport)
					.append(" specified in ServiceEndpointDiscoveryOption.transport()");
				
				throw ommIUExcept().message(_strBuilder.toString(), OmmInvalidUsageException.ErrorCode.INVALID_ARGUMENT);
			}
			
			switch(serviceEndpointDiscoveryOptionImpl._dataFormat)
			{
			case ServiceEndpointDiscoveryOption.DataformatProtocol.UNKNOWN:
				break;
			case ServiceEndpointDiscoveryOption.DataformatProtocol.RWF:
				_reactorServiceDiscoveryOptions.dataFormat(ReactorDiscoveryDataFormatProtocol.RD_DP_RWF);
				break;
			case ServiceEndpointDiscoveryOption.DataformatProtocol.JSON2:
				_reactorServiceDiscoveryOptions.transport(ReactorDiscoveryDataFormatProtocol.RD_DP_JSON2);
				break;
			default:
				strBuilder().append("Invalid dataformat protocol ").append(serviceEndpointDiscoveryOptionImpl._dataFormat)
					.append(" specified in ServiceEndpointDiscoveryOption.dataFormat()");
				
				throw ommIUExcept().message(_strBuilder.toString(), OmmInvalidUsageException.ErrorCode.INVALID_ARGUMENT);
			}
			
			if (_reactor.queryServiceDiscovery(_reactorServiceDiscoveryOptions, _reactorErrorInfo) != ReactorReturnCodes.SUCCESS)
			{
				strBuilder().append("Failed to query service discovery (Reactor.queryServiceDiscovery).")
				.append("' Error Id='").append(_reactorErrorInfo.error().errorId()).append("' Internal sysError='")
				.append(_reactorErrorInfo.error().sysError()).append("' Error Location='")
				.append(_reactorErrorInfo.location()).append("' Error Text='")
				.append(_reactorErrorInfo.error().text()).append("'. ");
				
				throw ommIUExcept().message(_strBuilder.toString(), _reactorErrorInfo.code());
			}
		}
		finally
		{
			_userLock.unlock();
		}
	}

	@Override
	public void uninitialize()
	{	
		_userLock.lock();
		try
		{
			if(!_reactor.isShutdown())
			{
				_reactorErrorInfo.clear();
				_reactor.shutdown(_reactorErrorInfo);
			}
		}
		finally
		{
			_userLock.unlock();
		}
	}
	
	OmmInvalidUsageExceptionImpl ommIUExcept()
	{
		if (_ommIUExcept == null)
			_ommIUExcept = new OmmInvalidUsageExceptionImpl();

		return _ommIUExcept;
	}
	
	StringBuilder strBuilder()
	{
		if(_strBuilder == null)
		{
			_strBuilder = new StringBuilder(255);
		}
		else
		{
			_strBuilder.setLength(0);
		}
		
		return _strBuilder;
	}

	@Override
	public int reactorServiceEndpointEventCallback(ReactorServiceEndpointEvent event)
	{
		ReactorServiceEndpointInfo reactorServiceEndpointInfo;
		ServiceEndpointDiscoveryInfoImpl serviceEndpointDiscoveryInfoImpl;
		
		_ServiceEndpointDiscoveryEventImpl._closure = event.userSpecObject();
		_ServiceEndpointDiscoveryEventImpl._serviceEndpointDiscovery = this;
		if(event.errorInfo().code() == ReactorReturnCodes.SUCCESS)
		{
			_serviceEndpointDiscoveryRespImpl._serviceEndpointInfoList.clear();
			for(int index = 0 ; index < event.serviceEndpointInfo().size() ; index++ )
			{
				reactorServiceEndpointInfo = event.serviceEndpointInfo().get(index);
				serviceEndpointDiscoveryInfoImpl = new ServiceEndpointDiscoveryInfoImpl();
				
				for(int valueIndex = 0; valueIndex < reactorServiceEndpointInfo.dataFormatList().size(); valueIndex++)
				{
					serviceEndpointDiscoveryInfoImpl._dataFormatList.add(reactorServiceEndpointInfo.dataFormatList().get(valueIndex).toString());
				}
				
				for(int valueIndex = 0; valueIndex < reactorServiceEndpointInfo.locationList().size(); valueIndex++)
				{
					serviceEndpointDiscoveryInfoImpl._locationList.add(reactorServiceEndpointInfo.locationList().get(valueIndex).toString());
				}
				
				serviceEndpointDiscoveryInfoImpl._endpoint = reactorServiceEndpointInfo.endPoint();
				serviceEndpointDiscoveryInfoImpl._port = reactorServiceEndpointInfo.port();
				serviceEndpointDiscoveryInfoImpl._provider = reactorServiceEndpointInfo.provider();
				serviceEndpointDiscoveryInfoImpl._transport = reactorServiceEndpointInfo.transport();
				_serviceEndpointDiscoveryRespImpl._serviceEndpointInfoList.add(serviceEndpointDiscoveryInfoImpl);
			}
			
			_client.onSuccess(_serviceEndpointDiscoveryRespImpl, _ServiceEndpointDiscoveryEventImpl);
		}
		else
		{
			_client.onError(event.errorInfo().error().text(), _ServiceEndpointDiscoveryEventImpl);
		}
		
		return ReactorCallbackReturnCodes.SUCCESS;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy