Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2012 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.opensource.org/licenses/ecl2.php Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.kuali.rice.ksb.impl.bus.diff;
 
 import java.util.List;
 import java.util.Map;
 
 
Default implementation of the ServiceRegistryDiffCalculator which calculates differences between client service bus state and service registry state.

Author(s):
Kuali Rice Team (rice.collab@kuali.org)
 
 
 	private static final Logger LOG = Logger.getLogger(ServiceRegistryDiffCalculatorImpl.class);
 	
 	
 	public void setServiceRegistry(ServiceRegistry serviceRegistry) {
 		this. = serviceRegistry;
 	}
 	
 	public CompleteServiceDiff diffServices(String instanceIdList<LocalServicelocalServicesList<RemoteServiceclientRegistryCache) {
         List<ServiceInfoallRegistryServices = .getAllOnlineServices();
         List<ServiceInfoallRegistryServicesForInstance = .getAllServicesForInstance(instanceId);
 		LocalServicesDiff localServicesDiff = calculateLocalServicesDiff(allRegistryServicesForInstanceinstanceIdlocalServices);
 		RemoteServicesDiff remoteServicesDiff = calculateRemoteServicesDiff(allRegistryServicesclientRegistryCache);
 		return new CompleteServiceDiff(localServicesDiffremoteServicesDiff);
 	}
 
 	protected LocalServicesDiff calculateLocalServicesDiff(List<ServiceInfoallRegistryServicesForInstanceString instanceIdList<LocalServicelocalServices) {
 		
 		List<ServiceInfoservicesToRemoveFromRegistry = new ArrayList<ServiceInfo>();
 		List<LocalServicelocalServicesToPublish = new ArrayList<LocalService>();
 		Map<LocalServiceServiceInfolocalServicesToUpdate = new HashMap<LocalServiceServiceInfo>();
 		
 		Map<QNameLocalServicelocalServiceIndex = indexLocalServices(instanceIdlocalServices);
 		for (ServiceInfo serviceInfo : allRegistryServicesForInstance) {
 			// first validate that the service has a valid instance id
 			if (!instanceId.equals(serviceInfo.getInstanceId())) {
 				throw new IllegalArgumentException("ServiceInfo given for local service diff does not have a valid instance id.  Should have been '" + instanceId + "' but was '" + serviceInfo.getInstanceId() + "'");
 			}
 			LocalService localService = localServiceIndex.get(serviceInfo.getServiceName());
 			if (localService == null) {
 				// this means the registry has the service but there is no local service, it has been unregistered
 				servicesToRemoveFromRegistry.add(serviceInfo);
 			} else {
 				// if the LocalService is not null, that means that it exists but it may have changed, or this may be the first time the service
 				// is being published upon startup in which case it's service id will be null
 				if (!localService.getServiceEndpoint().getInfo().equals(serviceInfo)) {
 					// if the service infos don't match, that means we need to re-publish our current copy of the local service
 					localServicesToUpdate.put(localServiceserviceInfo);
 				}
 				// whether or not it matches, remove it from the index
 				localServiceIndex.remove(serviceInfo.getServiceName());
 			}
 		}
 		// what's left in the localServiceIndex will be services that weren't in the registry at all, they need to be published
 		localServicesToPublish.addAll(localServiceIndex.values());
 		
 		if (.isDebugEnabled()) {
 			.info("For instance '" + instanceId + "', found " + servicesToRemoveFromRegistry.size() + " services to remove from registry, "+
 				localServicesToPublish.size() + " local services to publish");
 		}
 		
 		return new LocalServicesDiff(servicesToRemoveFromRegistrylocalServicesToPublishlocalServicesToUpdate);
 				
 	}
 	
 	private Map<QNameLocalServiceindexLocalServices(String instanceIdList<LocalServicelocalServices) {
 		Map<QNameLocalServicelocalServiceIndex = new HashMap<QNameLocalService>(localServices.size());
 		for (LocalService localService : localServices) {
			String localServiceInstanceId = localService.getServiceEndpoint().getInfo().getInstanceId(); 
			if (!instanceId.equals(localServiceInstanceId)) {
				throw new IllegalStateException("Instance id of local service (" + localServiceInstanceId + ") does not match instance id given to the diff calculator (" + instanceId + ")");
			}
			localServiceIndex.put(localService.getServiceName(), localService);
		}
		return localServiceIndex;
	}
	protected RemoteServicesDiff calculateRemoteServicesDiff(List<ServiceInfoallRegistryServicesList<RemoteServiceclientRegistryCache) {
        Map<StringServiceInfoindexedRegistryServices = indexRegistryServices(allRegistryServices);
		Map<StringServiceInfoservicesToAddToClientRegistryCache = new HashMap<StringServiceInfo>(indexedRegistryServices);
		List<RemoteServiceservicesToRemoveFromClientRegistryCache = new ArrayList<RemoteService>();
		for (RemoteService remoteService : clientRegistryCache) {
			ServiceInfo indexedRegistryService = indexedRegistryServices.get(remoteService.getServiceInfo().getServiceId());
			if (indexedRegistryService == null) {
				servicesToRemoveFromClientRegistryCache.add(remoteService);
else {
				if (!remoteService.getServiceInfo().getChecksum().equals(indexedRegistryService.getChecksum())) {
					servicesToRemoveFromClientRegistryCache.add(remoteService);
else {
					servicesToAddToClientRegistryCache.remove(remoteService.getServiceInfo().getServiceId());
				}
			}
		}
			.debug("For instance found " + servicesToRemoveFromClientRegistryCache.size() + " services to remove from client registry cache, "+
				servicesToAddToClientRegistryCache.size() + " services to add to client registry cache");
		}
		return new RemoteServicesDiff(new ArrayList<ServiceInfo>(servicesToAddToClientRegistryCache.values()),
                servicesToRemoveFromClientRegistryCache);
	}
	private Map<StringServiceInfoindexRegistryServices(List<ServiceInfoallRegistryServices) {
		Map<StringServiceInfoindexedRegistryServices = new HashMap<StringServiceInfo>(allRegistryServices.size());
		for (ServiceInfo serviceInfo : allRegistryServices) {
			indexedRegistryServices.put(serviceInfo.getServiceId(), serviceInfo);
		}
		return indexedRegistryServices;
	}
New to GrepCode? Check out our FAQ X