Coverage Report - org.kuali.rice.ksb.messaging.RoutingTableDiffCalculator
 
Classes in this File Line Coverage Branch Coverage Complexity
RoutingTableDiffCalculator
0%
0/72
0%
0/48
2.857
 
 1  
 /*
 2  
  * Copyright 2007 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.ksb.messaging;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import javax.xml.namespace.QName;
 24  
 
 25  
 import org.kuali.rice.kns.util.ObjectUtils;
 26  
 
 27  
 /**
 28  
  * Takes two lists of ServiceInfo objects.  One from the service def table and one from a piece 
 29  
  * of code and diffs the two.
 30  
  * 
 31  
  * 
 32  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 33  
  *
 34  
  */
 35  0
 public class RoutingTableDiffCalculator {
 36  
         
 37  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
 38  
                         .getLogger(RoutingTableDiffCalculator.class);
 39  
         
 40  0
         private List<ServiceInfo> servicesNeedUpdated = new ArrayList<ServiceInfo>();
 41  0
         private List<ServiceInfo> servicesNeedRemoved = new ArrayList<ServiceInfo>();
 42  0
         private List<ServiceInfo> masterServiceList = new ArrayList<ServiceInfo>();
 43  
         protected MessageHelper enMessageHelper;
 44  
         
 45  
         public boolean calculateClientSideUpdate(Map<QName, List<RemotedServiceHolder>> clients, List<ServiceInfo> fetchedServiceInfos) {
 46  0
                 List<ServiceInfo> clientServiceList = deconstructRemoteServiceLocatorClientMap(clients);
 47  0
                 if (clientServiceList.isEmpty() && ! fetchedServiceInfos.isEmpty()) {
 48  0
                         return true;
 49  
                 }
 50  0
                 Map<String, ServiceInfo> configuredServices = getRemotedService(clientServiceList);
 51  0
                 Map<String, ServiceInfo> deployedServices = getRemotedService(fetchedServiceInfos);
 52  0
                 for (Map.Entry<String, ServiceInfo> infoEntry : configuredServices.entrySet()) {
 53  0
                         if (deployedServices.containsKey(infoEntry.getKey())) {
 54  0
                                 ServiceInfo deployedServiceInfo = deployedServices.get(infoEntry.getKey());
 55  0
                                 if (! isSame(infoEntry.getValue(), deployedServiceInfo)) {
 56  0
                                         return true;
 57  
                                 }
 58  0
                         } else {
 59  0
                                 return true;
 60  
                         }
 61  
                 }
 62  
                 
 63  
                 //iterate the bus services and make sure the configured services represent the fetched ones
 64  0
                 for (Map.Entry<String, ServiceInfo> info : deployedServices.entrySet()) {
 65  0
                         if (! configuredServices.containsKey(info.getKey())) {
 66  0
                                 return true;
 67  
                         }
 68  
                 }
 69  
                 
 70  0
                 return false;
 71  
         }
 72  
         
 73  
         private List<ServiceInfo> deconstructRemoteServiceLocatorClientMap(Map<QName, List<RemotedServiceHolder>> clients) {
 74  0
                 List<ServiceInfo> clientServices = new ArrayList<ServiceInfo>();
 75  0
                 for (List<RemotedServiceHolder> remoteServiceHolders : clients.values()) {
 76  0
                         for (ServiceHolder holder : remoteServiceHolders) {
 77  0
                                 clientServices.add(holder.getServiceInfo());
 78  
                         }
 79  
                 }
 80  0
                 return clientServices;
 81  
         }
 82  
         
 83  
         public boolean calculateServerSideUpdateLists(List<ServiceInfo> memoryServiceInfos, List<ServiceInfo> fetchedServiceInfos) {
 84  0
                 Map<String, ServiceInfo> configuredServices = getRemotedService(memoryServiceInfos);
 85  0
                 Map<String, ServiceInfo> deployedServices = getRemotedService(fetchedServiceInfos);
 86  
                 
 87  
                 //iterate the configured services vs. the deployed services
 88  0
                 for (Map.Entry<String, ServiceInfo> infoEntry : configuredServices.entrySet()) {
 89  0
                         if (deployedServices.containsKey(infoEntry.getKey())) {
 90  0
                                 ServiceInfo deployedServiceInfo = deployedServices.get(infoEntry.getKey());
 91  0
                                 if (! isSame(infoEntry.getValue(), deployedServiceInfo)) {
 92  
                                     // if the ip number is changing and the url is the same then someone is 
 93  
                                     // trying to move a url to a new IP address.  This is bad
 94  
 //                                    if (infoEntry.getValue().getServerIp().equals(deployedServiceInfo.getServerIp())) {
 95  
 //                                        throw new RiceRuntimeException("You are trying to register a service already registered under an existing " +
 96  
 //                                                        "IP Address.");
 97  
 //                                    }
 98  0
                                     this.servicesNeedUpdated.add(deployedServiceInfo);
 99  
 //                                    if ( LOG.isInfoEnabled() ) {
 100  
 //                                            LOG.info( "servicesNeedUpdated.add( " + deployedServiceInfo.getActualEndpointUrl() + " )");
 101  
 //                                    }
 102  
                                 }
 103  0
                                 updateDeployedServiceInfo(infoEntry.getValue(), deployedServiceInfo);
 104  0
                                 this.masterServiceList.add(deployedServiceInfo);        
 105  
 //                            if ( LOG.isInfoEnabled() ) {
 106  
 //                                    LOG.info( "masterServiceList.add( " + deployedServiceInfo.getActualEndpointUrl() + " )");
 107  
 //                            }
 108  0
                         } else {
 109  0
                             this.servicesNeedUpdated.add(infoEntry.getValue());
 110  0
                             this.masterServiceList.add(infoEntry.getValue());
 111  
 //                            if ( LOG.isInfoEnabled() ) {
 112  
 //                                    LOG.info( "servicesNeedUpdated.add( " + infoEntry.getValue().getActualEndpointUrl() + " )");
 113  
 //                                    LOG.info( "masterServiceList.add( " + infoEntry.getValue().getActualEndpointUrl() + " )");
 114  
 //                            }
 115  
                         }
 116  
                 }
 117  
                 
 118  
                 //compare fetched vs. configured and determine if any fetched services need removed
 119  0
                 for (Map.Entry<String, ServiceInfo> infoEntry : deployedServices.entrySet()) {
 120  0
                         if (! configuredServices.containsKey(infoEntry.getKey())) {
 121  0
                             this.servicesNeedRemoved.add(infoEntry.getValue());
 122  
 //                            if ( LOG.isInfoEnabled() ) {
 123  
 //                                    LOG.info( "servicesNeedRemoved.add( " + infoEntry.getValue().getActualEndpointUrl() + " )");
 124  
 //                            }
 125  
                         }
 126  
                 }
 127  
                 
 128  0
                 return ! (this.servicesNeedRemoved.isEmpty() && this.servicesNeedUpdated.isEmpty());
 129  
         }
 130  
         
 131  
         public Map<String, ServiceInfo> getRemotedService(List<ServiceInfo> serviceInfos) {
 132  0
                 Map<String, ServiceInfo> serviceMap = new HashMap<String, ServiceInfo>();
 133  0
                 for (ServiceInfo info : serviceInfos) {
 134  0
                     if (ObjectUtils.isNotNull(info)) {
 135  0
                             String endpointURL = info.getEndpointUrl();
 136  0
                             if (serviceMap.containsKey(endpointURL)) {
 137  0
                                     LOG.trace("Multiple services with same endpoint url declared and saved in routing table.  " +
 138  
                                                     "Service will be ingored.  Endpoint " + endpointURL);
 139  
                             } else {
 140  0
                                     serviceMap.put(endpointURL, info);
 141  
                             }
 142  0
                     }
 143  
                 }
 144  0
                 return serviceMap;
 145  
         }
 146  
         
 147  
         private void updateDeployedServiceInfo(ServiceInfo configuredServiceInfo, ServiceInfo deployedServiceInfo) {
 148  0
                 deployedServiceInfo.setAlive(configuredServiceInfo.getAlive());
 149  0
                 deployedServiceInfo.setQname(configuredServiceInfo.getQname());
 150  0
                 deployedServiceInfo.setServiceName(configuredServiceInfo.getQname().toString());
 151  0
                 deployedServiceInfo.setServiceNamespace(configuredServiceInfo.getServiceNamespace());
 152  0
                 deployedServiceInfo.setServerIp(configuredServiceInfo.getServerIp());
 153  0
                 deployedServiceInfo.setServiceDefinition(configuredServiceInfo.getServiceDefinition(getEnMessageHelper()));
 154  0
                 deployedServiceInfo.setChecksum(configuredServiceInfo.getChecksum());
 155  0
         }
 156  
         
 157  
         private boolean isSame(ServiceInfo configured, ServiceInfo deployed) {
 158  0
                 return configured.getAlive().equals(deployed.getAlive()) &&  
 159  
                                 configured.getQname().equals(deployed.getQname()) &&
 160  
                                 configured.getServerIp().equals(deployed.getServerIp()) && 
 161  
                                 configured.getServiceNamespace().equals(deployed.getServiceNamespace()) &&
 162  
                                 configured.getChecksum().equals(deployed.getChecksum());
 163  
         }
 164  
 
 165  
         public List<ServiceInfo> getServicesNeedRemoved() {
 166  0
                 return this.servicesNeedRemoved;
 167  
         }
 168  
 
 169  
         public void setServicesNeedRemoved(List<ServiceInfo> servicesNeedRemoved) {
 170  0
                 this.servicesNeedRemoved = servicesNeedRemoved;
 171  0
         }
 172  
 
 173  
         public List<ServiceInfo> getServicesNeedUpdated() {
 174  0
                 return this.servicesNeedUpdated;
 175  
         }
 176  
 
 177  
         public void setServicesNeedUpdated(List<ServiceInfo> servicesNeedUpdated) {
 178  0
                 this.servicesNeedUpdated = servicesNeedUpdated;
 179  0
         }
 180  
 
 181  
         public List<ServiceInfo> getMasterServiceList() {
 182  0
                 return this.masterServiceList;
 183  
         }
 184  
 
 185  
         public void setMasterServiceList(List<ServiceInfo> masterServiceList) {
 186  0
                 this.masterServiceList = masterServiceList;
 187  0
         }
 188  
         
 189  
 
 190  
         /**
 191  
          * @return the enMessageHelper
 192  
          */
 193  
         public MessageHelper getEnMessageHelper() {
 194  0
                 return this.enMessageHelper;
 195  
         }
 196  
 
 197  
         /**
 198  
          * @param enMessageHelper the enMessageHelper to set
 199  
          */
 200  
         public void setEnMessageHelper(MessageHelper enMessageHelper) {
 201  0
                 this.enMessageHelper = enMessageHelper;
 202  0
         }
 203  
 
 204  
 }