1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
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 | |
|
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 | |
|
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 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | 0 | this.servicesNeedUpdated.add(deployedServiceInfo); |
99 | |
|
100 | |
|
101 | |
|
102 | |
} |
103 | 0 | updateDeployedServiceInfo(infoEntry.getValue(), deployedServiceInfo); |
104 | 0 | this.masterServiceList.add(deployedServiceInfo); |
105 | |
|
106 | |
|
107 | |
|
108 | 0 | } else { |
109 | 0 | this.servicesNeedUpdated.add(infoEntry.getValue()); |
110 | 0 | this.masterServiceList.add(infoEntry.getValue()); |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
} |
116 | |
} |
117 | |
|
118 | |
|
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 | |
|
123 | |
|
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 | |
|
192 | |
|
193 | |
public MessageHelper getEnMessageHelper() { |
194 | 0 | return this.enMessageHelper; |
195 | |
} |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public void setEnMessageHelper(MessageHelper enMessageHelper) { |
201 | 0 | this.enMessageHelper = enMessageHelper; |
202 | 0 | } |
203 | |
|
204 | |
} |