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 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 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class ServiceRegistryImpl implements ServiceRegistry { |
46 | |
|
47 | |
private ServiceRegistryDao serviceRegistryDao; |
48 | |
|
49 | |
@Override |
50 | |
public List<ServiceInfo> getOnlineServicesByName(QName serviceName) |
51 | |
throws RiceIllegalArgumentException { |
52 | 0 | if (serviceName == null) { |
53 | 0 | throw new RiceIllegalArgumentException("serviceName cannot be null"); |
54 | |
} |
55 | 0 | List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getOnlineServiceInfosByName(serviceName); |
56 | 0 | return convertServiceInfoBoList(serviceInfoBos); |
57 | |
} |
58 | |
|
59 | |
@Override |
60 | |
public List<ServiceInfo> getAllOnlineServices() { |
61 | 0 | List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getAllOnlineServiceInfos(); |
62 | 0 | return convertServiceInfoBoList(serviceInfoBos); |
63 | |
} |
64 | |
|
65 | |
@Override |
66 | |
public List<ServiceInfo> getAllServices() { |
67 | 0 | List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getAllServiceInfos(); |
68 | 0 | return convertServiceInfoBoList(serviceInfoBos); |
69 | |
} |
70 | |
|
71 | |
@Override |
72 | |
public List<ServiceInfo> getAllServicesForInstance(String instanceId) throws RiceIllegalArgumentException { |
73 | 0 | if (StringUtils.isBlank(instanceId)) { |
74 | 0 | throw new RiceIllegalArgumentException("instanceId cannot be blank"); |
75 | |
} |
76 | 0 | List<ServiceInfoBo> serviceInfoBos = serviceRegistryDao.getAllServiceInfosForInstance(instanceId); |
77 | 0 | return convertServiceInfoBoList(serviceInfoBos); |
78 | |
} |
79 | |
|
80 | |
@Override |
81 | |
public ServiceDescriptor getServiceDescriptor(String serviceDescriptorId) |
82 | |
throws RiceIllegalArgumentException { |
83 | 0 | if (StringUtils.isBlank(serviceDescriptorId)) { |
84 | 0 | throw new RiceIllegalArgumentException("serviceDescriptorId cannot be blank"); |
85 | |
} |
86 | 0 | ServiceDescriptorBo serviceDescriptorBo = serviceRegistryDao.getServiceDescriptor(serviceDescriptorId); |
87 | 0 | return ServiceDescriptorBo.to(serviceDescriptorBo); |
88 | |
} |
89 | |
|
90 | |
@Override |
91 | |
public List<ServiceDescriptor> getServiceDescriptors(List<String> serviceDescriptorIds) |
92 | |
throws RiceIllegalArgumentException { |
93 | 0 | if (serviceDescriptorIds == null) { |
94 | 0 | throw new RiceIllegalArgumentException("serviceDescriptorIds cannot be null"); |
95 | |
} |
96 | 0 | List<ServiceDescriptor> serviceDescriptors = new ArrayList<ServiceDescriptor>(); |
97 | 0 | for (String serviceDescriptorId : serviceDescriptorIds) { |
98 | 0 | ServiceDescriptor serviceDescriptor = getServiceDescriptor(serviceDescriptorId); |
99 | 0 | if (serviceDescriptor != null) { |
100 | 0 | serviceDescriptors.add(serviceDescriptor); |
101 | |
} |
102 | 0 | } |
103 | 0 | return Collections.unmodifiableList(serviceDescriptors); |
104 | |
} |
105 | |
|
106 | |
@Override |
107 | |
public ServiceEndpoint publishService(ServiceEndpoint serviceEndpoint) |
108 | |
throws RiceIllegalArgumentException { |
109 | 0 | if (serviceEndpoint == null) { |
110 | 0 | throw new RiceIllegalArgumentException("serviceEndpoint cannot be null"); |
111 | |
} |
112 | 0 | ServiceDescriptor serviceDescriptor = serviceEndpoint.getDescriptor(); |
113 | 0 | ServiceDescriptorBo serviceDescriptorBo = ServiceDescriptorBo.from(serviceDescriptor); |
114 | 0 | ServiceInfo serviceInfo = serviceEndpoint.getInfo(); |
115 | 0 | ServiceInfoBo serviceInfoBo = ServiceInfoBo.from(serviceInfo); |
116 | 0 | serviceDescriptorBo = serviceRegistryDao.saveServiceDescriptor(serviceDescriptorBo); |
117 | 0 | serviceInfoBo.setServiceDescriptorId(serviceDescriptorBo.getId()); |
118 | 0 | serviceRegistryDao.saveServiceInfo(serviceInfoBo); |
119 | |
|
120 | |
|
121 | 0 | 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 | 0 | if (serviceEndpoints == null) { |
129 | 0 | throw new RiceIllegalArgumentException("serviceEndpoints cannot be null"); |
130 | |
} |
131 | 0 | List<ServiceEndpoint> publishedEndpoints = new ArrayList<ServiceEndpoint>(); |
132 | 0 | for (ServiceEndpoint serviceEndpoint : serviceEndpoints) { |
133 | 0 | publishedEndpoints.add(publishService(serviceEndpoint)); |
134 | |
} |
135 | 0 | return publishedEndpoints; |
136 | |
} |
137 | |
|
138 | |
@Override |
139 | |
public ServiceEndpoint removeServiceEndpoint(String serviceId) |
140 | |
throws RiceIllegalArgumentException { |
141 | 0 | if (StringUtils.isBlank(serviceId)) { |
142 | 0 | throw new RiceIllegalArgumentException("serviceId cannot be blank"); |
143 | |
} |
144 | 0 | ServiceInfoBo serviceInfoBo = serviceRegistryDao.getServiceInfo(serviceId); |
145 | 0 | if (serviceInfoBo != null) { |
146 | 0 | ServiceDescriptorBo serviceDescriptorBo = serviceRegistryDao.getServiceDescriptor(serviceInfoBo.getServiceDescriptorId()); |
147 | 0 | ServiceEndpoint endpointPriorRemoval = ServiceEndpoint.Builder.create(ServiceInfo.Builder.create(serviceInfoBo), |
148 | |
ServiceDescriptor.Builder.create(serviceDescriptorBo)).build(); |
149 | 0 | serviceRegistryDao.removeServiceInfo(serviceInfoBo.getServiceId()); |
150 | 0 | serviceRegistryDao.removeServiceDescriptor(serviceInfoBo.getServiceDescriptorId()); |
151 | 0 | return endpointPriorRemoval; |
152 | |
} |
153 | 0 | return null; |
154 | |
} |
155 | |
|
156 | |
@Override |
157 | |
public List<ServiceEndpoint> removeServiceEndpoints(List<String> serviceIds) |
158 | |
throws RiceIllegalArgumentException { |
159 | 0 | if (serviceIds == null) { |
160 | 0 | throw new RiceIllegalArgumentException("serviceIds canot be null"); |
161 | |
} |
162 | 0 | List<ServiceEndpoint> servicesRemoved = new ArrayList<ServiceEndpoint>(); |
163 | 0 | for (String serviceId : serviceIds) { |
164 | 0 | servicesRemoved.add(removeServiceEndpoint(serviceId)); |
165 | |
} |
166 | 0 | return servicesRemoved; |
167 | |
} |
168 | |
|
169 | |
@Override |
170 | |
public RemoveAndPublishResult removeAndPublish(List<String> removeServiceIds, |
171 | |
List<ServiceEndpoint> publishServiceEndpoints) { |
172 | 0 | List<ServiceEndpoint> servicesRemoved = new ArrayList<ServiceEndpoint>(); |
173 | 0 | List<ServiceEndpoint> servicesPublished = new ArrayList<ServiceEndpoint>(); |
174 | 0 | if (removeServiceIds != null && !removeServiceIds.isEmpty()) { |
175 | 0 | servicesRemoved = removeServiceEndpoints(removeServiceIds); |
176 | |
} |
177 | 0 | if (publishServiceEndpoints != null && !publishServiceEndpoints.isEmpty()) { |
178 | 0 | servicesPublished = publishServices(publishServiceEndpoints); |
179 | |
} |
180 | 0 | return RemoveAndPublishResult.create(servicesRemoved, servicesPublished); |
181 | |
} |
182 | |
|
183 | |
@Override |
184 | |
public boolean updateStatus(String serviceId, ServiceEndpointStatus status) throws RiceIllegalArgumentException { |
185 | 0 | if (StringUtils.isBlank(serviceId)) { |
186 | 0 | throw new RiceIllegalArgumentException("serviceId cannot be blank"); |
187 | |
} |
188 | 0 | if (status == null) { |
189 | 0 | throw new RiceIllegalArgumentException("status cannot be null"); |
190 | |
} |
191 | 0 | return serviceRegistryDao.updateStatus(serviceId, status.getCode()); |
192 | |
} |
193 | |
|
194 | |
@Override |
195 | |
public List<String> updateStatuses(List<String> serviceIds, ServiceEndpointStatus status) throws RiceIllegalArgumentException { |
196 | 0 | if (serviceIds == null) { |
197 | 0 | throw new RiceIllegalArgumentException("serviceIds canot be null"); |
198 | |
} |
199 | 0 | if (status == null) { |
200 | 0 | throw new RiceIllegalArgumentException("status cannot be null"); |
201 | |
} |
202 | 0 | List<String> updatedServiceIds = new ArrayList<String>(); |
203 | 0 | for (String serviceId : serviceIds) { |
204 | 0 | if (updateStatus(serviceId, status)) { |
205 | 0 | updatedServiceIds.add(serviceId); |
206 | |
} |
207 | |
} |
208 | 0 | return Collections.unmodifiableList(updatedServiceIds); |
209 | |
} |
210 | |
|
211 | |
@Override |
212 | |
public void takeInstanceOffline(String instanceId) |
213 | |
throws RiceIllegalArgumentException { |
214 | 0 | if (StringUtils.isBlank(instanceId)) { |
215 | 0 | throw new RiceIllegalArgumentException("instanceId cannot be blank"); |
216 | |
} |
217 | 0 | serviceRegistryDao.updateStatusForInstanceId(instanceId, ServiceEndpointStatus.OFFLINE.getCode()); |
218 | 0 | } |
219 | |
|
220 | |
private List<ServiceInfo> convertServiceInfoBoList(List<ServiceInfoBo> serviceInfoBos) { |
221 | 0 | List<ServiceInfo> serviceInfos = new ArrayList<ServiceInfo>(); |
222 | 0 | if (serviceInfoBos != null) { |
223 | 0 | for (ServiceInfoBo serviceInfoBo : serviceInfoBos) { |
224 | 0 | serviceInfos.add(ServiceInfoBo.to(serviceInfoBo)); |
225 | |
} |
226 | |
} else { |
227 | 0 | return Collections.emptyList(); |
228 | |
} |
229 | 0 | return Collections.unmodifiableList(serviceInfos); |
230 | |
} |
231 | |
|
232 | |
public void setServiceRegistryDao(ServiceRegistryDao serviceRegistryDao) { |
233 | 0 | this.serviceRegistryDao = serviceRegistryDao; |
234 | 0 | } |
235 | |
|
236 | |
} |