View Javadoc

1   /**
2    * Copyright 2005-2012 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 java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import javax.xml.namespace.QName;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
26  import org.kuali.rice.ksb.api.registry.RemoveAndPublishResult;
27  import org.kuali.rice.ksb.api.registry.ServiceDescriptor;
28  import org.kuali.rice.ksb.api.registry.ServiceEndpoint;
29  import org.kuali.rice.ksb.api.registry.ServiceEndpointStatus;
30  import org.kuali.rice.ksb.api.registry.ServiceInfo;
31  import org.kuali.rice.ksb.api.registry.ServiceRegistry;
32  
33  /**
34   * Reference implementation of the {@link ServiceRegistry} which is backed by a
35   * data access object that handles reading and writing data related to registry
36   * entries from a backend datastore.
37   * 
38   * <p>In order for this class to function properly, a valid {@link ServiceRegistryDao}
39   * must be injected into it via the {@link #setServiceRegistryDao(ServiceRegistryDao)}
40   * method.
41   * 
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   *
44   */
45  public class ServiceRegistryImpl implements ServiceRegistry {
46  
47  	private ServiceRegistryDao serviceRegistryDao;
48  
49  	@Override
50  	public List<ServiceInfo> getOnlineServicesByName(QName serviceName)
51  			throws RiceIllegalArgumentException {
52  		if (serviceName == null) {
53  			throw new RiceIllegalArgumentException("serviceName cannot be null");
54  		}
55  		List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getOnlineServiceInfosByName(serviceName);
56  		return convertServiceInfoBoList(serviceInfoBos);
57  	}
58  
59  	@Override
60  	public List<ServiceInfo> getAllOnlineServices() {
61  		List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getAllOnlineServiceInfos();
62  		return convertServiceInfoBoList(serviceInfoBos);
63  	}
64  	
65  	@Override
66  	public List<ServiceInfo> getAllServices() {
67  		List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getAllServiceInfos();
68  		return convertServiceInfoBoList(serviceInfoBos);
69  	}
70  	
71  	@Override
72  	public List<ServiceInfo> getAllServicesForInstance(String instanceId) throws RiceIllegalArgumentException {
73  		if (StringUtils.isBlank(instanceId)) {
74  			throw new RiceIllegalArgumentException("instanceId cannot be blank");
75  		}
76  		List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getAllServiceInfosForInstance(instanceId);
77  		return convertServiceInfoBoList(serviceInfoBos);
78  	}
79  
80      @Override
81      public List<ServiceInfo> getAllServicesForApplication(String applicationId) throws RiceIllegalArgumentException {
82          if (StringUtils.isBlank(applicationId)) {
83              throw new RiceIllegalArgumentException("applicationId cannot be blank");
84          }
85          List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getAllServiceInfosForApplication(applicationId);
86          return convertServiceInfoBoList(serviceInfoBos);
87      }
88  
89  	@Override
90  	public ServiceDescriptor getServiceDescriptor(String serviceDescriptorId)
91  			throws RiceIllegalArgumentException {
92  		if (StringUtils.isBlank(serviceDescriptorId)) {
93  			throw new RiceIllegalArgumentException("serviceDescriptorId cannot be blank");
94  		}
95  		ServiceDescriptorBo serviceDescriptorBo = serviceRegistryDao.getServiceDescriptor(serviceDescriptorId);
96  		return ServiceDescriptorBo.to(serviceDescriptorBo);
97  	}
98  
99  	@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 }