001 /**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.ksb.impl.registry;
017
018 import java.util.ArrayList;
019 import java.util.Collections;
020 import java.util.List;
021
022 import javax.xml.namespace.QName;
023
024 import org.apache.commons.lang.StringUtils;
025 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
026 import org.kuali.rice.ksb.api.registry.RemoveAndPublishResult;
027 import org.kuali.rice.ksb.api.registry.ServiceDescriptor;
028 import org.kuali.rice.ksb.api.registry.ServiceEndpoint;
029 import org.kuali.rice.ksb.api.registry.ServiceEndpointStatus;
030 import org.kuali.rice.ksb.api.registry.ServiceInfo;
031 import org.kuali.rice.ksb.api.registry.ServiceRegistry;
032
033 /**
034 * Reference implementation of the {@link ServiceRegistry} which is backed by a
035 * data access object that handles reading and writing data related to registry
036 * entries from a backend datastore.
037 *
038 * <p>In order for this class to function properly, a valid {@link ServiceRegistryDao}
039 * must be injected into it via the {@link #setServiceRegistryDao(ServiceRegistryDao)}
040 * method.
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 *
044 */
045 public class ServiceRegistryImpl implements ServiceRegistry {
046
047 private ServiceRegistryDao serviceRegistryDao;
048
049 @Override
050 public List<ServiceInfo> getOnlineServicesByName(QName serviceName)
051 throws RiceIllegalArgumentException {
052 if (serviceName == null) {
053 throw new RiceIllegalArgumentException("serviceName cannot be null");
054 }
055 List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getOnlineServiceInfosByName(serviceName);
056 return convertServiceInfoBoList(serviceInfoBos);
057 }
058
059 @Override
060 public List<ServiceInfo> getAllOnlineServices() {
061 List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getAllOnlineServiceInfos();
062 return convertServiceInfoBoList(serviceInfoBos);
063 }
064
065 @Override
066 public List<ServiceInfo> getAllServices() {
067 List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getAllServiceInfos();
068 return convertServiceInfoBoList(serviceInfoBos);
069 }
070
071 @Override
072 public List<ServiceInfo> getAllServicesForInstance(String instanceId) throws RiceIllegalArgumentException {
073 if (StringUtils.isBlank(instanceId)) {
074 throw new RiceIllegalArgumentException("instanceId cannot be blank");
075 }
076 List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getAllServiceInfosForInstance(instanceId);
077 return convertServiceInfoBoList(serviceInfoBos);
078 }
079
080 @Override
081 public List<ServiceInfo> getAllServicesForApplication(String applicationId) throws RiceIllegalArgumentException {
082 if (StringUtils.isBlank(applicationId)) {
083 throw new RiceIllegalArgumentException("applicationId cannot be blank");
084 }
085 List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getAllServiceInfosForApplication(applicationId);
086 return convertServiceInfoBoList(serviceInfoBos);
087 }
088
089 @Override
090 public ServiceDescriptor getServiceDescriptor(String serviceDescriptorId)
091 throws RiceIllegalArgumentException {
092 if (StringUtils.isBlank(serviceDescriptorId)) {
093 throw new RiceIllegalArgumentException("serviceDescriptorId cannot be blank");
094 }
095 ServiceDescriptorBo serviceDescriptorBo = serviceRegistryDao.getServiceDescriptor(serviceDescriptorId);
096 return ServiceDescriptorBo.to(serviceDescriptorBo);
097 }
098
099 @Override
100 public List<ServiceDescriptor> getServiceDescriptors(List<String> serviceDescriptorIds)
101 throws RiceIllegalArgumentException {
102 if (serviceDescriptorIds == null) {
103 throw new RiceIllegalArgumentException("serviceDescriptorIds cannot be null");
104 }
105 List<ServiceDescriptor> serviceDescriptors = new ArrayList<ServiceDescriptor>();
106 for (String serviceDescriptorId : serviceDescriptorIds) {
107 ServiceDescriptor serviceDescriptor = getServiceDescriptor(serviceDescriptorId);
108 if (serviceDescriptor != null) {
109 serviceDescriptors.add(serviceDescriptor);
110 }
111 }
112 return Collections.unmodifiableList(serviceDescriptors);
113 }
114
115 @Override
116 public ServiceEndpoint publishService(ServiceEndpoint serviceEndpoint)
117 throws RiceIllegalArgumentException {
118 if (serviceEndpoint == null) {
119 throw new RiceIllegalArgumentException("serviceEndpoint cannot be null");
120 }
121 ServiceDescriptor serviceDescriptor = serviceEndpoint.getDescriptor();
122 ServiceDescriptorBo serviceDescriptorBo = ServiceDescriptorBo.from(serviceDescriptor);
123 ServiceInfo serviceInfo = serviceEndpoint.getInfo();
124 ServiceInfoBo serviceInfoBo = ServiceInfoBo.from(serviceInfo);
125 serviceDescriptorBo = serviceRegistryDao.saveServiceDescriptor(serviceDescriptorBo);
126 serviceInfoBo.setServiceDescriptorId(serviceDescriptorBo.getId());
127 serviceRegistryDao.saveServiceInfo(serviceInfoBo);
128
129
130 return ServiceEndpoint.Builder.create(ServiceInfo.Builder.create(serviceInfoBo),
131 ServiceDescriptor.Builder.create(serviceDescriptorBo)).build();
132 }
133
134 @Override
135 public List<ServiceEndpoint> publishServices(List<ServiceEndpoint> serviceEndpoints)
136 throws RiceIllegalArgumentException {
137 if (serviceEndpoints == null) {
138 throw new RiceIllegalArgumentException("serviceEndpoints cannot be null");
139 }
140 List<ServiceEndpoint> publishedEndpoints = new ArrayList<ServiceEndpoint>();
141 for (ServiceEndpoint serviceEndpoint : serviceEndpoints) {
142 publishedEndpoints.add(publishService(serviceEndpoint));
143 }
144 return publishedEndpoints;
145 }
146
147 @Override
148 public ServiceEndpoint removeServiceEndpoint(String serviceId)
149 throws RiceIllegalArgumentException {
150 if (StringUtils.isBlank(serviceId)) {
151 throw new RiceIllegalArgumentException("serviceId cannot be blank");
152 }
153 ServiceInfoBo serviceInfoBo = serviceRegistryDao.getServiceInfo(serviceId);
154 if (serviceInfoBo != null) {
155 ServiceDescriptorBo serviceDescriptorBo = serviceRegistryDao.getServiceDescriptor(serviceInfoBo.getServiceDescriptorId());
156 ServiceEndpoint endpointPriorRemoval = ServiceEndpoint.Builder.create(ServiceInfo.Builder.create(serviceInfoBo),
157 ServiceDescriptor.Builder.create(serviceDescriptorBo)).build();
158 serviceRegistryDao.removeServiceInfo(serviceInfoBo.getServiceId());
159 serviceRegistryDao.removeServiceDescriptor(serviceInfoBo.getServiceDescriptorId());
160 return endpointPriorRemoval;
161 }
162 return null;
163 }
164
165 @Override
166 public List<ServiceEndpoint> removeServiceEndpoints(List<String> serviceIds)
167 throws RiceIllegalArgumentException {
168 if (serviceIds == null) {
169 throw new RiceIllegalArgumentException("serviceIds canot be null");
170 }
171 List<ServiceEndpoint> servicesRemoved = new ArrayList<ServiceEndpoint>();
172 for (String serviceId : serviceIds) {
173 servicesRemoved.add(removeServiceEndpoint(serviceId));
174 }
175 return servicesRemoved;
176 }
177
178 @Override
179 public RemoveAndPublishResult removeAndPublish(List<String> removeServiceIds,
180 List<ServiceEndpoint> publishServiceEndpoints) {
181 List<ServiceEndpoint> servicesRemoved = new ArrayList<ServiceEndpoint>();
182 List<ServiceEndpoint> servicesPublished = new ArrayList<ServiceEndpoint>();
183 if (removeServiceIds != null && !removeServiceIds.isEmpty()) {
184 servicesRemoved = removeServiceEndpoints(removeServiceIds);
185 }
186 if (publishServiceEndpoints != null && !publishServiceEndpoints.isEmpty()) {
187 servicesPublished = publishServices(publishServiceEndpoints);
188 }
189 return RemoveAndPublishResult.create(servicesRemoved, servicesPublished);
190 }
191
192 @Override
193 public boolean updateStatus(String serviceId, ServiceEndpointStatus status) throws RiceIllegalArgumentException {
194 if (StringUtils.isBlank(serviceId)) {
195 throw new RiceIllegalArgumentException("serviceId cannot be blank");
196 }
197 if (status == null) {
198 throw new RiceIllegalArgumentException("status cannot be null");
199 }
200 return serviceRegistryDao.updateStatus(serviceId, status.getCode());
201 }
202
203 @Override
204 public List<String> updateStatuses(List<String> serviceIds, ServiceEndpointStatus status) throws RiceIllegalArgumentException {
205 if (serviceIds == null) {
206 throw new RiceIllegalArgumentException("serviceIds canot be null");
207 }
208 if (status == null) {
209 throw new RiceIllegalArgumentException("status cannot be null");
210 }
211 List<String> updatedServiceIds = new ArrayList<String>();
212 for (String serviceId : serviceIds) {
213 if (updateStatus(serviceId, status)) {
214 updatedServiceIds.add(serviceId);
215 }
216 }
217 return Collections.unmodifiableList(updatedServiceIds);
218 }
219
220 @Override
221 public void takeInstanceOffline(String instanceId)
222 throws RiceIllegalArgumentException {
223 if (StringUtils.isBlank(instanceId)) {
224 throw new RiceIllegalArgumentException("instanceId cannot be blank");
225 }
226 serviceRegistryDao.updateStatusForInstanceId(instanceId, ServiceEndpointStatus.OFFLINE.getCode());
227 }
228
229 private List<ServiceInfo> convertServiceInfoBoList(List<ServiceInfoBo> serviceInfoBos) {
230 List<ServiceInfo> serviceInfos = new ArrayList<ServiceInfo>();
231 if (serviceInfoBos != null) {
232 for (ServiceInfoBo serviceInfoBo : serviceInfoBos) {
233 serviceInfos.add(ServiceInfoBo.to(serviceInfoBo));
234 }
235 } else {
236 return Collections.emptyList();
237 }
238 return Collections.unmodifiableList(serviceInfos);
239 }
240
241 public void setServiceRegistryDao(ServiceRegistryDao serviceRegistryDao) {
242 this.serviceRegistryDao = serviceRegistryDao;
243 }
244
245 }