View Javadoc

1   /**
2    * Copyright 2005-2011 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 ServiceDescriptor getServiceDescriptor(String serviceDescriptorId)
82  			throws RiceIllegalArgumentException {
83  		if (StringUtils.isBlank(serviceDescriptorId)) {
84  			throw new RiceIllegalArgumentException("serviceDescriptorId cannot be blank");
85  		}
86  		ServiceDescriptorBo serviceDescriptorBo = serviceRegistryDao.getServiceDescriptor(serviceDescriptorId);
87  		return ServiceDescriptorBo.to(serviceDescriptorBo);
88  	}
89  
90  	@Override
91  	public List<ServiceDescriptor> getServiceDescriptors(List<String> serviceDescriptorIds)
92  			throws RiceIllegalArgumentException {
93  		if (serviceDescriptorIds == null) {
94  			throw new RiceIllegalArgumentException("serviceDescriptorIds cannot be null");
95  		}
96  		List<ServiceDescriptor> serviceDescriptors = new ArrayList<ServiceDescriptor>();
97  		for (String serviceDescriptorId : serviceDescriptorIds) {
98  			ServiceDescriptor serviceDescriptor = getServiceDescriptor(serviceDescriptorId);
99  			if (serviceDescriptor != null) {
100 				serviceDescriptors.add(serviceDescriptor);
101 			}
102 		}
103 		return Collections.unmodifiableList(serviceDescriptors);
104 	}
105 
106 	@Override
107 	public ServiceEndpoint publishService(ServiceEndpoint serviceEndpoint)
108 			throws RiceIllegalArgumentException {
109 		if (serviceEndpoint == null) {
110 			throw new RiceIllegalArgumentException("serviceEndpoint cannot be null");
111 		}
112 		ServiceDescriptor serviceDescriptor = serviceEndpoint.getDescriptor();
113 		ServiceDescriptorBo serviceDescriptorBo = ServiceDescriptorBo.from(serviceDescriptor);
114 		ServiceInfo serviceInfo = serviceEndpoint.getInfo();
115 		ServiceInfoBo serviceInfoBo = ServiceInfoBo.from(serviceInfo);
116 		serviceDescriptorBo = serviceRegistryDao.saveServiceDescriptor(serviceDescriptorBo);
117 		serviceInfoBo.setServiceDescriptorId(serviceDescriptorBo.getId());
118 		serviceRegistryDao.saveServiceInfo(serviceInfoBo);
119 		
120 		
121 		return ServiceEndpoint.Builder.create(ServiceInfo.Builder.create(serviceInfoBo),
122 				ServiceDescriptor.Builder.create(serviceDescriptorBo)).build();
123 	}
124 
125 	@Override
126 	public List<ServiceEndpoint> publishServices(List<ServiceEndpoint> serviceEndpoints)
127 			throws RiceIllegalArgumentException {
128 		if (serviceEndpoints == null) {
129 			throw new RiceIllegalArgumentException("serviceEndpoints cannot be null");
130 		}
131 		List<ServiceEndpoint> publishedEndpoints = new ArrayList<ServiceEndpoint>();
132 		for (ServiceEndpoint serviceEndpoint : serviceEndpoints) {
133 			publishedEndpoints.add(publishService(serviceEndpoint));
134 		}
135 		return publishedEndpoints;
136 	}
137 
138 	@Override
139 	public ServiceEndpoint removeServiceEndpoint(String serviceId)
140 			throws RiceIllegalArgumentException {
141 		if (StringUtils.isBlank(serviceId)) {
142 			throw new RiceIllegalArgumentException("serviceId cannot be blank");
143 		}
144 		ServiceInfoBo serviceInfoBo = serviceRegistryDao.getServiceInfo(serviceId);
145 		if (serviceInfoBo != null) {
146 			ServiceDescriptorBo serviceDescriptorBo = serviceRegistryDao.getServiceDescriptor(serviceInfoBo.getServiceDescriptorId());
147 			ServiceEndpoint endpointPriorRemoval = ServiceEndpoint.Builder.create(ServiceInfo.Builder.create(serviceInfoBo),
148 					ServiceDescriptor.Builder.create(serviceDescriptorBo)).build();
149 			serviceRegistryDao.removeServiceInfo(serviceInfoBo.getServiceId());
150 			serviceRegistryDao.removeServiceDescriptor(serviceInfoBo.getServiceDescriptorId());
151 			return endpointPriorRemoval;
152 		}
153 		return null;
154 	}
155 
156 	@Override
157 	public List<ServiceEndpoint> removeServiceEndpoints(List<String> serviceIds)
158 			throws RiceIllegalArgumentException {
159 		if (serviceIds == null) {
160 			throw new RiceIllegalArgumentException("serviceIds canot be null");
161 		}
162 		List<ServiceEndpoint> servicesRemoved = new ArrayList<ServiceEndpoint>();
163 		for (String serviceId : serviceIds) {
164 			servicesRemoved.add(removeServiceEndpoint(serviceId));
165 		}
166 		return servicesRemoved;
167 	}
168 
169 	@Override
170 	public RemoveAndPublishResult removeAndPublish(List<String> removeServiceIds,
171 			List<ServiceEndpoint> publishServiceEndpoints) {
172 		List<ServiceEndpoint> servicesRemoved = new ArrayList<ServiceEndpoint>();
173 		List<ServiceEndpoint> servicesPublished = new ArrayList<ServiceEndpoint>();
174 		if (removeServiceIds != null && !removeServiceIds.isEmpty()) {
175 			servicesRemoved = removeServiceEndpoints(removeServiceIds);
176 		}
177 		if (publishServiceEndpoints != null && !publishServiceEndpoints.isEmpty()) {
178 			servicesPublished = publishServices(publishServiceEndpoints);
179 		}
180 		return RemoveAndPublishResult.create(servicesRemoved, servicesPublished);
181 	}
182 
183 	@Override
184 	public boolean updateStatus(String serviceId, ServiceEndpointStatus status) throws RiceIllegalArgumentException {
185 		if (StringUtils.isBlank(serviceId)) {
186 			throw new RiceIllegalArgumentException("serviceId cannot be blank");
187 		}
188 		if (status == null) {
189 			throw new RiceIllegalArgumentException("status cannot be null");
190 		}
191 		return serviceRegistryDao.updateStatus(serviceId, status.getCode());
192 	}
193 
194 	@Override
195 	public List<String> updateStatuses(List<String> serviceIds, ServiceEndpointStatus status) throws RiceIllegalArgumentException {
196 		if (serviceIds == null) {
197 			throw new RiceIllegalArgumentException("serviceIds canot be null");
198 		}
199 		if (status == null) {
200 			throw new RiceIllegalArgumentException("status cannot be null");
201 		}
202 		List<String> updatedServiceIds = new ArrayList<String>();
203 		for (String serviceId : serviceIds) {
204 			if (updateStatus(serviceId, status)) {
205 				updatedServiceIds.add(serviceId);
206 			}
207 		}
208 		return Collections.unmodifiableList(updatedServiceIds);
209 	}
210 
211 	@Override
212 	public void takeInstanceOffline(String instanceId)
213 			throws RiceIllegalArgumentException {
214 		if (StringUtils.isBlank(instanceId)) {
215 			throw new RiceIllegalArgumentException("instanceId cannot be blank");
216 		}
217 		serviceRegistryDao.updateStatusForInstanceId(instanceId, ServiceEndpointStatus.OFFLINE.getCode());
218 	}
219 
220 	private List<ServiceInfo> convertServiceInfoBoList(List<ServiceInfoBo> serviceInfoBos) {
221 		List<ServiceInfo> serviceInfos = new ArrayList<ServiceInfo>();
222 		if (serviceInfoBos != null) {
223 			for (ServiceInfoBo serviceInfoBo : serviceInfoBos) {
224 				serviceInfos.add(ServiceInfoBo.to(serviceInfoBo));
225 			}
226 		} else {
227 			return Collections.emptyList();
228 		}
229 		return Collections.unmodifiableList(serviceInfos);
230 	}
231 	
232 	public void setServiceRegistryDao(ServiceRegistryDao serviceRegistryDao) {
233 		this.serviceRegistryDao = serviceRegistryDao;
234 	}
235 	
236 }