1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.impl.registry;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.criteria.QueryByCriteria;
20 import org.kuali.rice.core.api.criteria.QueryResults;
21 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
22 import org.kuali.rice.krad.data.DataObjectService;
23 import org.kuali.rice.ksb.api.registry.RemoveAndPublishResult;
24 import org.kuali.rice.ksb.api.registry.ServiceDescriptor;
25 import org.kuali.rice.ksb.api.registry.ServiceEndpoint;
26 import org.kuali.rice.ksb.api.registry.ServiceEndpointStatus;
27 import org.kuali.rice.ksb.api.registry.ServiceInfo;
28 import org.kuali.rice.ksb.api.registry.ServiceRegistry;
29 import org.springframework.beans.factory.annotation.Required;
30
31 import javax.xml.namespace.QName;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35
36 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
37
38
39
40
41
42
43
44
45
46 public class ServiceRegistryImpl implements ServiceRegistry {
47
48 private DataObjectService dataObjectService;
49
50 @Override
51 public List<ServiceInfo> getOnlineServicesByName(QName serviceName)
52 throws RiceIllegalArgumentException {
53 if (serviceName == null) {
54 throw new RiceIllegalArgumentException("serviceName cannot be null");
55 }
56
57 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
58 builder.setPredicates(equal("serviceName",serviceName.toString()),
59 equal("statusCode",ServiceEndpointStatus.ONLINE.getCode()));
60 List<ServiceInfoBo> serviceInfoBos = getDataObjectService().findMatching(
61 ServiceInfoBo.class,builder.build()).getResults();
62 return convertServiceInfoBoList(serviceInfoBos);
63 }
64
65 @Override
66 public List<ServiceInfo> getAllOnlineServices() {
67 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
68 builder.setPredicates(equal("statusCode",ServiceEndpointStatus.ONLINE.getCode()));
69 List<ServiceInfoBo> serviceInfoBos = getDataObjectService().findMatching(
70 ServiceInfoBo.class,builder.build()).getResults();
71 return convertServiceInfoBoList(serviceInfoBos);
72 }
73
74 @Override
75 public List<ServiceInfo> getAllServices() {
76 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
77 List<ServiceInfoBo> serviceInfoBos = getDataObjectService().findMatching(
78 ServiceInfoBo.class,builder.build()).getResults();
79 return convertServiceInfoBoList(serviceInfoBos);
80 }
81
82 @Override
83 public List<ServiceInfo> getAllServicesForInstance(String instanceId) throws RiceIllegalArgumentException {
84 if (StringUtils.isBlank(instanceId)) {
85 throw new RiceIllegalArgumentException("instanceId cannot be blank");
86 }
87 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
88 builder.setPredicates(equal("instanceId",instanceId));
89 List<ServiceInfoBo> serviceInfoBos = getDataObjectService().findMatching(
90 ServiceInfoBo.class,builder.build()).getResults();
91 return convertServiceInfoBoList(serviceInfoBos);
92 }
93
94 @Override
95 public List<ServiceInfo> getAllServicesForApplication(String applicationId) throws RiceIllegalArgumentException {
96 if (StringUtils.isBlank(applicationId)) {
97 throw new RiceIllegalArgumentException("applicationId cannot be blank");
98 }
99 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
100 builder.setPredicates(equal("applicationId",applicationId));
101 List<ServiceInfoBo> serviceInfoBos = getDataObjectService().findMatching(
102 ServiceInfoBo.class,builder.build()).getResults();
103 return convertServiceInfoBoList(serviceInfoBos);
104 }
105
106 @Override
107 public ServiceDescriptor getServiceDescriptor(String serviceDescriptorId)
108 throws RiceIllegalArgumentException {
109 if (StringUtils.isBlank(serviceDescriptorId)) {
110 throw new RiceIllegalArgumentException("serviceDescriptorId cannot be blank");
111 }
112 ServiceDescriptorBo serviceDescriptorBo = getDataObjectService().find(
113 ServiceDescriptorBo.class,serviceDescriptorId);
114 return ServiceDescriptorBo.to(serviceDescriptorBo);
115 }
116
117 @Override
118 public List<ServiceDescriptor> getServiceDescriptors(List<String> serviceDescriptorIds)
119 throws RiceIllegalArgumentException {
120 if (serviceDescriptorIds == null) {
121 throw new RiceIllegalArgumentException("serviceDescriptorIds cannot be null");
122 }
123 List<ServiceDescriptor> serviceDescriptors = new ArrayList<ServiceDescriptor>();
124 for (String serviceDescriptorId : serviceDescriptorIds) {
125 ServiceDescriptor serviceDescriptor = getServiceDescriptor(serviceDescriptorId);
126 if (serviceDescriptor != null) {
127 serviceDescriptors.add(serviceDescriptor);
128 }
129 }
130 return Collections.unmodifiableList(serviceDescriptors);
131 }
132
133 @Override
134 public ServiceEndpoint publishService(ServiceEndpoint serviceEndpoint)
135 throws RiceIllegalArgumentException {
136 if (serviceEndpoint == null) {
137 throw new RiceIllegalArgumentException("serviceEndpoint cannot be null");
138 }
139 ServiceDescriptor serviceDescriptor = serviceEndpoint.getDescriptor();
140 ServiceDescriptorBo serviceDescriptorBo = ServiceDescriptorBo.from(serviceDescriptor);
141 ServiceInfo serviceInfo = serviceEndpoint.getInfo();
142 ServiceInfoBo serviceInfoBo = ServiceInfoBo.from(serviceInfo);
143 serviceDescriptorBo = getDataObjectService().save(serviceDescriptorBo);
144 serviceInfoBo.setServiceDescriptorId(serviceDescriptorBo.getId());
145 serviceInfoBo = getDataObjectService().save(serviceInfoBo);
146
147
148 return ServiceEndpoint.Builder.create(ServiceInfo.Builder.create(serviceInfoBo),
149 ServiceDescriptor.Builder.create(serviceDescriptorBo)).build();
150 }
151
152 @Override
153 public List<ServiceEndpoint> publishServices(List<ServiceEndpoint> serviceEndpoints)
154 throws RiceIllegalArgumentException {
155 if (serviceEndpoints == null) {
156 throw new RiceIllegalArgumentException("serviceEndpoints cannot be null");
157 }
158 List<ServiceEndpoint> publishedEndpoints = new ArrayList<ServiceEndpoint>();
159 for (ServiceEndpoint serviceEndpoint : serviceEndpoints) {
160 publishedEndpoints.add(publishService(serviceEndpoint));
161 }
162 return publishedEndpoints;
163 }
164
165 @Override
166 public ServiceEndpoint removeServiceEndpoint(String serviceId)
167 throws RiceIllegalArgumentException {
168 if (StringUtils.isBlank(serviceId)) {
169 throw new RiceIllegalArgumentException("serviceId cannot be blank");
170 }
171 ServiceInfoBo serviceInfoBo = getDataObjectService().find(ServiceInfoBo.class, serviceId);
172 if (serviceInfoBo != null) {
173 ServiceDescriptorBo serviceDescriptorBo = getDataObjectService().find(
174 ServiceDescriptorBo.class,serviceInfoBo.getServiceDescriptorId());
175 if(serviceDescriptorBo != null) {
176 ServiceEndpoint endpointPriorRemoval = ServiceEndpoint.Builder.create(ServiceInfo.Builder.create(serviceInfoBo),
177 ServiceDescriptor.Builder.create(serviceDescriptorBo)).build();
178
179 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
180 builder.setPredicates(equal("serviceId",serviceInfoBo.getServiceId()));
181 getDataObjectService().deleteMatching(ServiceInfoBo.class,builder.build());
182
183 builder = QueryByCriteria.Builder.create();
184 builder.setPredicates(equal("id",serviceInfoBo.getServiceDescriptorId()));
185 getDataObjectService().deleteMatching(ServiceDescriptorBo.class,builder.build());
186 return endpointPriorRemoval;
187 }else{
188 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
189 builder.setPredicates(equal("serviceId",serviceInfoBo.getServiceId()));
190 getDataObjectService().deleteMatching(ServiceInfoBo.class,builder.build());
191 }
192 }
193 return null;
194 }
195
196 @Override
197 public List<ServiceEndpoint> removeServiceEndpoints(List<String> serviceIds)
198 throws RiceIllegalArgumentException {
199 if (serviceIds == null) {
200 throw new RiceIllegalArgumentException("serviceIds canot be null");
201 }
202 List<ServiceEndpoint> servicesRemoved = new ArrayList<ServiceEndpoint>();
203 for (String serviceId : serviceIds) {
204 servicesRemoved.add(removeServiceEndpoint(serviceId));
205 }
206 return servicesRemoved;
207 }
208
209 @Override
210 public RemoveAndPublishResult removeAndPublish(List<String> removeServiceIds,
211 List<ServiceEndpoint> publishServiceEndpoints) {
212 List<ServiceEndpoint> servicesRemoved = new ArrayList<ServiceEndpoint>();
213 List<ServiceEndpoint> servicesPublished = new ArrayList<ServiceEndpoint>();
214 if (removeServiceIds != null && !removeServiceIds.isEmpty()) {
215 servicesRemoved = removeServiceEndpoints(removeServiceIds);
216 }
217 if (publishServiceEndpoints != null && !publishServiceEndpoints.isEmpty()) {
218 servicesPublished = publishServices(publishServiceEndpoints);
219 }
220 return RemoveAndPublishResult.create(servicesRemoved, servicesPublished);
221 }
222
223 @Override
224 public boolean updateStatus(String serviceId, ServiceEndpointStatus status) throws RiceIllegalArgumentException {
225 if (StringUtils.isBlank(serviceId)) {
226 throw new RiceIllegalArgumentException("serviceId cannot be blank");
227 }
228 if (status == null) {
229 throw new RiceIllegalArgumentException("status cannot be null");
230 }
231 ServiceInfoBo serviceInfoBo = getDataObjectService().find(ServiceInfoBo.class,serviceId);
232 if (serviceInfoBo == null) {
233 return false;
234 }
235 serviceInfoBo.setStatusCode(status.getCode());
236 getDataObjectService().save(serviceInfoBo);
237 return true;
238 }
239
240 @Override
241 public List<String> updateStatuses(List<String> serviceIds, ServiceEndpointStatus status) throws RiceIllegalArgumentException {
242 if (serviceIds == null) {
243 throw new RiceIllegalArgumentException("serviceIds canot be null");
244 }
245 if (status == null) {
246 throw new RiceIllegalArgumentException("status cannot be null");
247 }
248 List<String> updatedServiceIds = new ArrayList<String>();
249 for (String serviceId : serviceIds) {
250 if (updateStatus(serviceId, status)) {
251 updatedServiceIds.add(serviceId);
252 }
253 }
254 return Collections.unmodifiableList(updatedServiceIds);
255 }
256
257 @Override
258 public void takeInstanceOffline(String instanceId)
259 throws RiceIllegalArgumentException {
260 if (StringUtils.isBlank(instanceId)) {
261 throw new RiceIllegalArgumentException("instanceId cannot be blank");
262 }
263 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
264 builder.setPredicates(equal("instanceId", instanceId));
265 QueryResults<ServiceInfoBo> results = getDataObjectService().findMatching(ServiceInfoBo.class, builder.build());
266 for (ServiceInfoBo serviceInfo : results.getResults()) {
267 serviceInfo.setStatusCode(ServiceEndpointStatus.OFFLINE.getCode());
268 getDataObjectService().save(serviceInfo);
269 }
270 }
271
272 private List<ServiceInfo> convertServiceInfoBoList(List<ServiceInfoBo> serviceInfoBos) {
273 List<ServiceInfo> serviceInfos = new ArrayList<ServiceInfo>();
274 if (serviceInfoBos != null) {
275 for (ServiceInfoBo serviceInfoBo : serviceInfoBos) {
276 serviceInfos.add(ServiceInfoBo.to(serviceInfoBo));
277 }
278 } else {
279 return Collections.emptyList();
280 }
281 return Collections.unmodifiableList(serviceInfos);
282 }
283
284 public DataObjectService getDataObjectService() {
285 return dataObjectService;
286 }
287
288 @Required
289 public void setDataObjectService(DataObjectService dataObjectService) {
290 this.dataObjectService = dataObjectService;
291 }
292
293
294
295 }