View Javadoc

1   /**
2    * Copyright 2005-2013 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.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   * Reference implementation of the {@link ServiceRegistry} which is backed by a
40   * data access object that handles reading and writing data related to registry
41   * entries from a backend datastore.
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
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         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 			ServiceEndpoint endpointPriorRemoval = ServiceEndpoint.Builder.create(ServiceInfo.Builder.create(serviceInfoBo),
176 					ServiceDescriptor.Builder.create(serviceDescriptorBo)).build();
177 
178             QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
179             builder.setPredicates(equal("serviceId",serviceInfoBo.getServiceId()));
180             getDataObjectService().deleteMatching(ServiceInfoBo.class,builder.build());
181 
182             builder = QueryByCriteria.Builder.create();
183             builder.setPredicates(equal("descriptor",serviceInfoBo.getServiceDescriptorId()));
184             getDataObjectService().deleteMatching(ServiceDescriptorBo.class,builder.build());
185 			return endpointPriorRemoval;
186 		}
187 		return null;
188 	}
189 
190 	@Override
191 	public List<ServiceEndpoint> removeServiceEndpoints(List<String> serviceIds)
192 			throws RiceIllegalArgumentException {
193 		if (serviceIds == null) {
194 			throw new RiceIllegalArgumentException("serviceIds canot be null");
195 		}
196 		List<ServiceEndpoint> servicesRemoved = new ArrayList<ServiceEndpoint>();
197 		for (String serviceId : serviceIds) {
198 			servicesRemoved.add(removeServiceEndpoint(serviceId));
199 		}
200 		return servicesRemoved;
201 	}
202 
203 	@Override
204 	public RemoveAndPublishResult removeAndPublish(List<String> removeServiceIds,
205 			List<ServiceEndpoint> publishServiceEndpoints) {
206 		List<ServiceEndpoint> servicesRemoved = new ArrayList<ServiceEndpoint>();
207 		List<ServiceEndpoint> servicesPublished = new ArrayList<ServiceEndpoint>();
208 		if (removeServiceIds != null && !removeServiceIds.isEmpty()) {
209 			servicesRemoved = removeServiceEndpoints(removeServiceIds);
210 		}
211 		if (publishServiceEndpoints != null && !publishServiceEndpoints.isEmpty()) {
212 			servicesPublished = publishServices(publishServiceEndpoints);
213 		}
214 		return RemoveAndPublishResult.create(servicesRemoved, servicesPublished);
215 	}
216 
217 	@Override
218 	public boolean updateStatus(String serviceId, ServiceEndpointStatus status) throws RiceIllegalArgumentException {
219 		if (StringUtils.isBlank(serviceId)) {
220 			throw new RiceIllegalArgumentException("serviceId cannot be blank");
221 		}
222 		if (status == null) {
223 			throw new RiceIllegalArgumentException("status cannot be null");
224 		}
225         ServiceInfoBo serviceInfoBo = getDataObjectService().find(ServiceInfoBo.class,serviceId);
226         if (serviceInfoBo == null) {
227             return false;
228         }
229         serviceInfoBo.setStatusCode(status.getCode());
230         getDataObjectService().save(serviceInfoBo);
231         return true;
232 	}
233 
234 	@Override
235 	public List<String> updateStatuses(List<String> serviceIds, ServiceEndpointStatus status) throws RiceIllegalArgumentException {
236 		if (serviceIds == null) {
237 			throw new RiceIllegalArgumentException("serviceIds canot be null");
238 		}
239 		if (status == null) {
240 			throw new RiceIllegalArgumentException("status cannot be null");
241 		}
242 		List<String> updatedServiceIds = new ArrayList<String>();
243 		for (String serviceId : serviceIds) {
244 			if (updateStatus(serviceId, status)) {
245 				updatedServiceIds.add(serviceId);
246 			}
247 		}
248 		return Collections.unmodifiableList(updatedServiceIds);
249 	}
250 
251 	@Override
252 	public void takeInstanceOffline(String instanceId)
253 			throws RiceIllegalArgumentException {
254 		if (StringUtils.isBlank(instanceId)) {
255 			throw new RiceIllegalArgumentException("instanceId cannot be blank");
256 		}
257         QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
258         builder.setPredicates(equal("instanceId", instanceId));
259         QueryResults<ServiceInfoBo> results = getDataObjectService().findMatching(ServiceInfoBo.class, builder.build());
260         for (ServiceInfoBo serviceInfo : results.getResults()) {
261             serviceInfo.setStatusCode(ServiceEndpointStatus.OFFLINE.getCode());
262             getDataObjectService().save(serviceInfo);
263         }
264 	}
265 
266 	private List<ServiceInfo> convertServiceInfoBoList(List<ServiceInfoBo> serviceInfoBos) {
267 		List<ServiceInfo> serviceInfos = new ArrayList<ServiceInfo>();
268 		if (serviceInfoBos != null) {
269 			for (ServiceInfoBo serviceInfoBo : serviceInfoBos) {
270 				serviceInfos.add(ServiceInfoBo.to(serviceInfoBo));
271 			}
272 		} else {
273 			return Collections.emptyList();
274 		}
275 		return Collections.unmodifiableList(serviceInfos);
276 	}
277 
278     public DataObjectService getDataObjectService() {
279         return dataObjectService;
280     }
281 
282     @Required
283     public void setDataObjectService(DataObjectService dataObjectService) {
284         this.dataObjectService = dataObjectService;
285     }
286 
287 
288 
289 }