Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2013-2014 by Cisco Systems, Inc. All rights reserved.
  
  package com.cisco.oss.foundation.directory.test;
  
  import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 
The default Directory API RegistrationManager, LookupManager implementation for Test. It implements the RegistrationManager and LookupManager interfaces to operate the ServiceInstance at client side and does not invoke a remote ServiceDirectory server node, all ServiceInstances are stored in the local memory.

Author(s):
zuxiang
 
 public class DefaultTestServiceDirectoryManager implements
 	private static final Logger LOGGER = LoggerFactory
The position index for query instance by service name.
 
The internal Cache for ProvidedService.
 
The internal Cache for MetadataKey.
 
Mark whether component is started or not.
 
 	protected boolean isStarted = false;

Constructor
 
 	}

Start the DefaultTestServiceDirectoryManager.
 
 	public void start() {
 		if(! ){
 			 = true;
 		}
 	}

Stop the DefaultTestServiceDirectoryManager.
 
 	public void stop() {
 		if(){
 			 = false;
 		}
	}

Parameters:
serviceName the service name.
Returns:
the ServiceInstance.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
	public ServiceInstance lookupInstance(String serviceName)
		List<ServiceInstancelist = this.lookupInstances(serviceName);
		if(list == null || list.size() == 0){
			return null;
		}
		int index = getNextQueryInstanceIndex(serviceNamelist.size());
		return list.get(index);
	}

Parameters:
serviceName the service name.
Returns:
the ServiceInstance list.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
		ProvidedService service = null;
		if(.isCached(serviceName)){
			service = .getService(serviceName);
		}
		if(service == null || service.getServiceInstances().size() == 0){
			return Collections.emptyList();
		}else{
				if(model.getStatus() == .){
					list.add(new ServiceInstance(serviceNamemodel.getProviderId(), model.getUri(), 
							model.isMonitorEnabled(), model.getStatus(), model.getMetadata()));
				}
			}
			return list;
		}
	}

Parameters:
serviceName the service name.
query the ServiceInstanceQuery.
Returns:
the ServiceInstance.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
		List<ServiceInstancelist = this.queryInstancesByName(serviceNamequery);
		if(list == null || list.size() == 0){
			return null;
		}
		int index = getNextQueryInstanceIndex(serviceNamelist.size());
		return list.get(index);
	}

Parameters:
serviceName the service name.
query the ServiceInstanceQuery.
Returns:
the ServiceInstance list match the ServiceInstanceQuery.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
		List<ServiceInstanceinstances = this.lookupInstances(serviceName);
		return ServiceInstanceQueryHelper.filterServiceInstance(queryinstances);
	}

Parameters:
serviceInstance the ProvidedServiceInstance.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
	public void registerService(ProvidedServiceInstance serviceInstance)
		if(serviceInstance == null){
			throw new IllegalArgumentException("The ServiceInstance can not be null.");
		}
		ErrorCode code = ServiceInstanceUtils.validateProvidedServiceInstance(serviceInstance);
		if(! code.equals(.)){
			ServiceDirectoryError error = new ServiceDirectoryError(codeserviceInstance.getServiceName());
			throw new ServiceException(error);
		}
		ProvidedServiceInstance cachedInstance = this.getProvidedServiceInstance(serviceInstance.getServiceName(), serviceInstance.getProviderId());
		if(cachedInstance != null){
			throw new ServiceException(error);
		}
		String serviceName = serviceInstance.getServiceName();
		ProvidedService service = this.createServiceIfNotExists(serviceName);
		ProvidedServiceInstance cacheServiceInstance = deepCloneProvidedServiceInstance(serviceInstance);
		service.getServiceInstances().add(cacheServiceInstance);
		addMetadataKeyMap(serviceInstance);
		.info("Registered Service, name=" + serviceInstance.getServiceName());
	}

Parameters:
serviceInstance the ProvidedServiceInstance.
registryHealth the ServiceInstanceHealth callback of the ServiceInstance.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
	public void registerService(ProvidedServiceInstance serviceInstanceServiceInstanceHealth registryHealth)
		registerService(serviceInstance);
	}

Parameters:
serviceName the service name.
providerId the providerId.
status the OperatonalStatus of the ProvidedServiceInstance.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
	public void updateServiceOperationalStatus(String serviceName,
			String providerIdOperationalStatus status)
		ErrorCode code = ServiceInstanceUtils.isNameValid(serviceName);
		if(! code.equals(.)){
			ServiceDirectoryError error = new ServiceDirectoryError(codeserviceName);
			throw new ServiceException(error);
		}
		code = ServiceInstanceUtils.isIdValid(providerId);
		if(! code.equals(.)){
			ServiceDirectoryError error = new ServiceDirectoryError(codeserviceName);
			throw new ServiceException(error);
		}
		ProvidedServiceInstance model = getProvidedServiceInstance(serviceNameproviderId);
		if(model == null){
			.error("Update Service Instance OperationalStatus failed - can not find the ServiceInstance.");
					serviceName);
		}
		model.setStatus(status);
	}

	public void updateServiceUri(String serviceName,
			String providerIdString urithrows ServiceException {
		ErrorCode code = ServiceInstanceUtils.isNameValid(serviceName);
		if(! code.equals(.)){
			ServiceDirectoryError error = new ServiceDirectoryError(codeserviceName);
			throw new ServiceException(error);
		}
		code = ServiceInstanceUtils.isIdValid(providerId);
		if(! code.equals(.)){
			ServiceDirectoryError error = new ServiceDirectoryError(codeserviceName);
			throw new ServiceException(error);
		}
		code = ServiceInstanceUtils.isUriValid(uri);
		if(! code.equals(.)){
			ServiceDirectoryError error = new ServiceDirectoryError(codeserviceName);
			throw new ServiceException(error);
		}
		ProvidedServiceInstance model = getProvidedServiceInstance(serviceNameproviderId);
		if(model == null){
			.error("Update Service Instance URI failed - can not find the ServiceInstance.");
					serviceName);
		}
		model.setUri(uri);
	}

Parameters:
serviceInstance the ProvidedServiceInstance.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
	public void updateService(ProvidedServiceInstance serviceInstance)
		if(serviceInstance == null){
			throw new IllegalArgumentException("The ServiceInstance can not be null.");
		}
		ErrorCode code = ServiceInstanceUtils.validateProvidedServiceInstance(serviceInstance);
		if(! code.equals(.)){
			ServiceDirectoryError error = new ServiceDirectoryError(codeserviceInstance.getServiceName());
			throw new ServiceException(error);
		}
		updateMetadataKeyMap(serviceInstance);
		try{
			updateInstance(serviceInstance);
			throw new ServiceException(e);
		}
	}

Parameters:
serviceName the service name.
providerId the providerId
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
	public void unregisterService(String serviceNameString providerId)
		ErrorCode code = ServiceInstanceUtils.isNameValid(serviceName);
		if(! code.equals(.)){
			ServiceDirectoryError error = new ServiceDirectoryError(codeserviceName);
			throw new ServiceException(error);
		}
		code = ServiceInstanceUtils.isIdValid(providerId);
		if(! code.equals(.)){
			ServiceDirectoryError error = new ServiceDirectoryError(codeserviceName);
			throw new ServiceException(error);
		}
		deleteMetadataKeyMap(serviceNameproviderId);
		ProvidedServiceInstance model = getProvidedServiceInstance(serviceNameproviderId);
		if(model == null){
			.error("Unregister Service failed - can not find the ServiceInstance.");
					serviceName);
		}
		getService(serviceName).getServiceInstances().remove(model);
	}

		List<QueryCriterioncriteria = query.getCriteria();
		if (criteria != null && criteria.size() > 0) {
			String key = criteria.get(0).getMetadataKey();
			if(list == null || list.size() == 0){
				return null;
			}
			int index = getNextQueryInstanceIndex(keylist.size());
			return list.get(index);
		}
		return null;
	}

		List<QueryCriterioncriteria = query.getCriteria();
		if (criteria != null && criteria.size() > 0) {
			String key = criteria.get(0).getMetadataKey();
				for (ProvidedServiceInstance model : instances) {
					if (model.getStatus() == .) {
								model.getProviderId(), model.getUri(), model.isMonitorEnabled(),
								model.getStatus(), model.getMetadata()));
					}
				}
				return ServiceInstanceQueryHelper.filterServiceInstance(query,
						list);
			}
		}
		return Collections.emptyList();
	}

	public ServiceInstance getInstance(String serviceNameString instanceId)
		ProvidedServiceInstance instance = getProvidedServiceInstance(serviceNameinstanceId);
		return new ServiceInstance(serviceNameinstance.getProviderId(), instance.getUri(), 
				instance.isMonitorEnabled(), instance.getStatus(), instance.getMetadata());
	}

		ProvidedService service = null;
		if(.isCached(serviceName)){
			service = .getService(serviceName);
		}
		if(service == null || service.getServiceInstances().size() == 0){
			return Collections.emptyList();
		}else{
				list.add(new ServiceInstance(serviceNamemodel.getProviderId(), model.getUri(), 
						model.isMonitorEnabled(), model.getStatus(), model.getMetadata()));
			}
			return list;
		}
	}

		List<ServiceInstanceinstances = this.getAllInstances(serviceName);
		return ServiceInstanceQueryHelper.filterServiceInstance(queryinstances);
	}

		List<ServiceInstanceinstances = null;
			if(instances == null){
				instances = new ArrayList<ServiceInstance>();
			}
				instances.add(new ServiceInstance(model.getServiceName(), model.getProviderId(), model.getUri(), 
						model.isMonitorEnabled(), model.getStatus(), model.getMetadata()));
			}
		}
		if(instances == null ){
			return Collections.emptyList();
		}else{
			return instances;
		}
	}

		List<QueryCriterioncriteria = query.getCriteria();
		if (criteria != null && criteria.size() > 0) {
			String key = criteria.get(0).getMetadataKey();
				for (ProvidedServiceInstance model : instances) {
							model.getProviderId(), model.getUri(),
							model.isMonitorEnabled(), model.getStatus(), model.getMetadata()));
				}
				return ServiceInstanceQueryHelper.filterServiceInstance(query,
						list);
			}
		}
		return Collections.emptyList();
	}

Get the ServiceDirectoryCache.

Returns:
the ServiceDirectoryCache.
		return this.;
	}

Get the ProvidedServiceInstance from cache.

Parameters:
serviceName the service name.
instanceId the instanceId.
Returns:
the ProvidedServiceInstance.
	private ProvidedServiceInstance getProvidedServiceInstance(String serviceNameString instanceId){
		if(this..isCached(serviceName)){
			for(ProvidedServiceInstance ist : instances){
				if(ist.getProviderId().equals(instanceId)){
					return ist;
				}
			}
		}
		return null;
	}

Validate the ProvidedServiceInstance.

Parameters:
instance the ProvidedServiceInstance.
		if(instance.getUri() == null || instance.getUri().isEmpty()){
					instance.getServiceName());
		}
	}

Obtain a ProvidedService, and create a new one if not existed.

Parameters:
serviceName the service name.
Returns:
the ProvidedService.
		if(.isCached(serviceName)){
			return .getService(serviceName);
		}else{
			ProvidedService service = new ProvidedService(serviceName);
			service.setServiceInstances(ins);
			.putService(serviceNameservice);
			return service;
		}
	}

Get the ProvidedService with its instances.

Parameters:
serviceName the service name.
Returns:
the ProvidedService.
	private ProvidedService getService(String serviceName){
		if(.isCached(serviceName)){
			return .getService(serviceName);
		}
		return null;
	}

Update the ServiceInstance.

Parameters:
instance the ProvidedServiceInstance.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
	private void updateInstance(ProvidedServiceInstance instance)
		String serviceName = instance.getServiceName();
		String instanceId = instance.getProviderId();
		ProvidedServiceInstance model = getProvidedServiceInstance(serviceNameinstanceId);
		if(model == null){
			.error("Update Service failed - cannot find the ServiceInstance");
					instance.getServiceName());
		}
		model.setStatus(instance.getStatus());
		model.setUri(instance.getUri());
		// Change the metadata when metadata is not null.
		if(instance.getMetadata() != null){
			Map<StringStringmodelMetadata = new HashMap<StringString>();
			model.setMetadata(modelMetadata);
			for(Entry<StringStringentry : instance.getMetadata().entrySet()){
				modelMetadata.put(entry.getKey(), entry.getValue());
			}
		}
	}

Update the ProvidedServiceInstance OperationalStatus.

Parameters:
serviceName the service name.
instanceId the instanceId
status the OperationalStatus of the ServiceInstance.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException
	protected void updateInstanceStatus(String serviceNameString instanceId,
		ProvidedServiceInstance model = getProvidedServiceInstance(serviceNameinstanceId);
		if(model == null){
			.error("Update Service OperationalStatus failed - cannot find the ServiceInstance");
					serviceName);
		}
		model.setStatus(status);
	}

Add the ProvidedServiceInstance to metadata key map.

Parameters:
serviceInstance the ProvidedServiceInstance.
	private void addMetadataKeyMap(ProvidedServiceInstance serviceInstance){
		String serviceName = serviceInstance.getServiceName();
		String instanceId = serviceInstance.getProviderId();
		ProvidedServiceInstance cachedInstance = this.getProvidedServiceInstance(serviceNameinstanceId);
		if(cachedInstance == null){
			return;
		}
		for(Entry<StringStringen : serviceInstance.getMetadata().entrySet()){
			String key = en.getKey();
			if(! this..isCached(key)){
				this..putService(keyinstances);
			}
			this..getService(key).add(cachedInstance);
		}
	}

Update the metadata key for the new ProvidedServiceInstance. It removes the metadata key map from the old ProvidedServiceInstance, and adds metadata key to the new ProvidedServiceInstance.

Parameters:
serviceInstance the ProvidedServiceInstance.
	private void updateMetadataKeyMap(ProvidedServiceInstance serviceInstance){
		// Remove old mapping.
		deleteMetadataKeyMap(serviceInstance.getServiceName(), serviceInstance.getProviderId());
		addMetadataKeyMap(serviceInstance);
	}

Delete the metadatakey for the ProvidedServiceInstance.

Parameters:
serviceName the serviceName of ProvidedServiceInstance.
providerId the providerId of ProvidedServiceInstance.
	private void deleteMetadataKeyMap(String serviceNameString providerId){
		ProvidedServiceInstance cachedInstance = this.getProvidedServiceInstance(serviceNameproviderId);
		if(cachedInstance == null){
			return ;
		}
		for(Entry<StringStringen : cachedInstance.getMetadata().entrySet()){
			String key = en.getKey();
			if(! this..isCached(key)){
				this..putService(keyinstances);
			}
			this..getService(key).remove(cachedInstance);
		}
	}

Get the next index of ServiceInstance List in the queryList.

Parameters:
serviceName the name of the service.
max The max index in the ServiceInstance List, it is size()-1.
Returns:
the next ServiceInstance index in query.
	private int getNextQueryInstanceIndex(String serviceNameint max){
		if(max == 0){
			return 0;
		}
		if(this..containsKey(serviceName)){
			AtomicInteger pre = this..get(serviceName);
			int theIndex = pre.incrementAndGet();
			return theIndex % max;
		}else{
			this..put(serviceNamenew AtomicInteger(0));
			return 0;
		}
	}

Deep clone a ProvidedServiceInstance. It can clone all object reference and the Collections.

Parameters:
instance the ProvidedServiceInstance.
Returns:
the new ProvidedServiceInstance cloned.
				instance.getServiceName(), instance.getAddress(), instance.getPort());
		newInstance.setStatus(instance.getStatus());
		newInstance.setMonitorEnabled(instance.isMonitorEnabled());
		newInstance.setUri(instance.getUri());
		Map<StringStringmeta = new HashMap<StringString>();
		for(Entry<StringStringentry : instance.getMetadata().entrySet()){
			meta.put(entry.getKey(), entry.getValue());
		}
		newInstance.setMetadata(meta);
		return newInstance;
	}

The Service Object in Service Directory test framework. It is the logic Service Object in Service Directory. The ProvidedServiceInstances with the same service name belong to one ProvidedService. It is only used in the test framework.

Author(s):
zuxiang
	public static class ProvidedService {

The name.
		private String name;

The ServiceInstances of the Service.
Constructor.
		public ProvidedService(){
		}

Constructor.

Parameters:
name the name.
		public ProvidedService(String name){
			this. = name;
		}

Get service name.

Returns:
the service name.
		public String getName() {
			return ;
		}

Set service name.

Parameters:
name the service name.
		public void setName(String name) {
			this. = name;
		}

Get the ProvidedServiceInstance list.

Returns:
the ProviidedServiceInstance list.
		}

Set the ProviidedServiceInstance list.

Parameters:
serviceInstances the ProviidedServiceInstance list.
		public void setServiceInstances(List<ProvidedServiceInstanceserviceInstances) {
			this. = serviceInstances;
		}
	}
New to GrepCode? Check out our FAQ X