1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ksb.api.bus.support; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.apache.commons.lang.builder.EqualsBuilder; |
20 | |
import org.apache.commons.lang.builder.HashCodeBuilder; |
21 | |
import org.apache.commons.lang.builder.ReflectionToStringBuilder; |
22 | |
import org.apache.log4j.Logger; |
23 | |
import org.kuali.rice.core.api.CoreConstants; |
24 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
25 | |
import org.kuali.rice.core.api.config.CoreConfigHelper; |
26 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
27 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
28 | |
import org.kuali.rice.core.api.security.credentials.CredentialsType; |
29 | |
import org.kuali.rice.core.api.util.ClassLoaderUtils; |
30 | |
import org.kuali.rice.ksb.api.bus.Endpoint; |
31 | |
import org.kuali.rice.ksb.api.bus.ServiceConfiguration; |
32 | |
import org.kuali.rice.ksb.api.bus.ServiceDefinition; |
33 | |
|
34 | |
import javax.xml.namespace.QName; |
35 | |
import java.net.URL; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public abstract class AbstractServiceDefinition implements ServiceDefinition { |
44 | |
|
45 | 1 | private static final Logger LOG = Logger.getLogger(AbstractServiceDefinition.class); |
46 | |
|
47 | |
|
48 | |
private String localServiceName; |
49 | |
private String serviceNameSpaceURI; |
50 | |
|
51 | |
private Object service; |
52 | |
private QName serviceName; |
53 | |
private boolean queue; |
54 | |
private Integer priority; |
55 | |
private Integer retryAttempts; |
56 | |
private Long millisToLive; |
57 | |
private String messageExceptionHandler; |
58 | |
private String servicePath; |
59 | |
private URL endpointUrl; |
60 | |
private Boolean busSecurity; |
61 | |
private CredentialsType credentialsType; |
62 | |
private String serviceVersion; |
63 | |
private String applicationId; |
64 | |
private String instanceId; |
65 | |
|
66 | |
|
67 | |
private ClassLoader serviceClassLoader; |
68 | |
private String cacheManager; |
69 | |
|
70 | 8 | protected AbstractServiceDefinition() { |
71 | 8 | this.busSecurity = Boolean.TRUE; |
72 | 8 | this.queue = true; |
73 | 8 | this.serviceClassLoader = ClassLoaderUtils.getDefaultClassLoader(); |
74 | 8 | } |
75 | |
|
76 | |
public Object getService() { |
77 | 3 | return this.service; |
78 | |
} |
79 | |
public void setService(Object service) { |
80 | 1 | this.service = service; |
81 | 1 | } |
82 | |
|
83 | |
public String getLocalServiceName() { |
84 | 0 | return this.localServiceName; |
85 | |
} |
86 | |
|
87 | |
public void setLocalServiceName(String serviceName) { |
88 | 0 | this.localServiceName = serviceName; |
89 | 0 | } |
90 | |
|
91 | |
public String getMessageExceptionHandler() { |
92 | 8 | return this.messageExceptionHandler; |
93 | |
} |
94 | |
|
95 | |
public void setMessageExceptionHandler(String messageExceptionHandler) { |
96 | 1 | this.messageExceptionHandler = messageExceptionHandler; |
97 | 1 | } |
98 | |
|
99 | |
public Integer getPriority() { |
100 | 8 | return this.priority; |
101 | |
} |
102 | |
|
103 | |
public void setPriority(Integer priority) { |
104 | 2 | this.priority = priority; |
105 | 2 | } |
106 | |
|
107 | |
public boolean isQueue() { |
108 | 8 | return this.queue; |
109 | |
} |
110 | |
|
111 | |
public void setQueue(boolean queue) { |
112 | 1 | this.queue = queue; |
113 | 1 | } |
114 | |
|
115 | |
public Integer getRetryAttempts() { |
116 | 8 | return this.retryAttempts; |
117 | |
} |
118 | |
|
119 | |
public void setRetryAttempts(Integer retryAttempts) { |
120 | 2 | this.retryAttempts = retryAttempts; |
121 | 2 | } |
122 | |
|
123 | |
public QName getServiceName() { |
124 | 9 | if (this.serviceName == null) { |
125 | 0 | if (this.serviceNameSpaceURI == null) { |
126 | 0 | return new QName(this.applicationId, this.localServiceName); |
127 | |
} else { |
128 | 0 | return new QName(this.serviceNameSpaceURI, this.localServiceName); |
129 | |
} |
130 | |
|
131 | |
} |
132 | 9 | return this.serviceName; |
133 | |
} |
134 | |
public void setServiceName(QName serviceName) { |
135 | 8 | this.serviceName = serviceName; |
136 | 8 | } |
137 | |
|
138 | |
@Override |
139 | |
public URL getEndpointUrl() { |
140 | 8 | return this.endpointUrl; |
141 | |
} |
142 | |
public void setEndpointUrl(URL endpointUrl) { |
143 | 8 | this.endpointUrl = endpointUrl; |
144 | 8 | } |
145 | |
|
146 | |
public void setCredentialsType(CredentialsType credentialsType) { |
147 | 1 | this.credentialsType = credentialsType; |
148 | 1 | } |
149 | |
|
150 | |
public CredentialsType getCredentialsType() { |
151 | 8 | return this.credentialsType; |
152 | |
} |
153 | |
|
154 | |
public String getServiceVersion() { |
155 | 8 | return serviceVersion; |
156 | |
} |
157 | |
|
158 | |
public void setServiceVersion(String serviceVersion) { |
159 | 8 | this.serviceVersion = serviceVersion; |
160 | 8 | } |
161 | |
|
162 | |
public String getApplicationId() { |
163 | 8 | return applicationId; |
164 | |
} |
165 | |
|
166 | |
public void setApplicationId(String applicationId) { |
167 | 8 | this.applicationId = applicationId; |
168 | 8 | } |
169 | |
|
170 | |
@Override |
171 | |
public String getInstanceId() { |
172 | 8 | return instanceId; |
173 | |
} |
174 | |
|
175 | |
public void setInstanceId(String instanceId) { |
176 | 3 | this.instanceId = instanceId; |
177 | 3 | } |
178 | |
|
179 | |
@Override |
180 | |
public ClassLoader getServiceClassLoader() { |
181 | 0 | return this.serviceClassLoader; |
182 | |
} |
183 | |
|
184 | |
public void setServiceClassLoader(ClassLoader serviceClassLoader) { |
185 | 0 | this.serviceClassLoader = serviceClassLoader; |
186 | 0 | } |
187 | |
|
188 | |
@Override |
189 | |
public String getCacheManager() { |
190 | 8 | return cacheManager; |
191 | |
} |
192 | |
|
193 | |
public void setCacheManager(String cacheManager) { |
194 | 0 | this.cacheManager = cacheManager; |
195 | 0 | } |
196 | |
|
197 | |
@Override |
198 | |
public void validate() { |
199 | |
|
200 | 1 | if (this.serviceName == null && this.localServiceName == null) { |
201 | 0 | throw new ConfigurationException("Must give a serviceName or localServiceName"); |
202 | |
} |
203 | |
|
204 | 1 | if (this.applicationId == null) { |
205 | 0 | String applicationId = CoreConfigHelper.getApplicationId(); |
206 | 0 | if (applicationId == null) { |
207 | 0 | throw new ConfigurationException("Must have an applicationId"); |
208 | |
} |
209 | 0 | this.applicationId = applicationId; |
210 | |
} |
211 | |
|
212 | 1 | if (this.instanceId == null) { |
213 | 0 | String instanceId = CoreConfigHelper.getInstanceId(); |
214 | 0 | if (instanceId == null) { |
215 | 0 | throw new ConfigurationException("Must have an instanceId"); |
216 | |
} |
217 | 0 | this.instanceId = instanceId; |
218 | |
} |
219 | |
|
220 | 1 | if (this.serviceName != null && this.localServiceName == null) { |
221 | 1 | this.localServiceName = this.getServiceName().getLocalPart(); |
222 | |
} |
223 | |
|
224 | 1 | if (this.servicePath != null){ |
225 | 0 | if (this.servicePath.endsWith("/")){ |
226 | 0 | this.servicePath = StringUtils.chop(servicePath); |
227 | |
} |
228 | 0 | if (!this.servicePath.startsWith("/")){ |
229 | 0 | this.servicePath = "/" + this.servicePath; |
230 | |
} |
231 | |
} else { |
232 | |
|
233 | 1 | this.servicePath = "/"; |
234 | |
} |
235 | |
|
236 | |
|
237 | 1 | if (StringUtils.isBlank(serviceVersion)) { |
238 | 0 | setServiceVersion(CoreConstants.Versions.UNSPECIFIED); |
239 | |
} |
240 | |
|
241 | 1 | LOG.debug("Validating service " + this.serviceName); |
242 | |
|
243 | |
|
244 | 1 | if (this.endpointUrl == null) { |
245 | 0 | String endPointURL = ConfigContext.getCurrentContextConfig().getEndPointUrl(); |
246 | 0 | if (endPointURL == null) { |
247 | 0 | throw new ConfigurationException("Must provide a serviceEndPoint or serviceServletURL"); |
248 | |
} |
249 | 0 | if (! endPointURL.endsWith("/")) { |
250 | 0 | endPointURL += servicePath; |
251 | |
} else { |
252 | 0 | endPointURL = StringUtils.chop(endPointURL) + servicePath; |
253 | |
} |
254 | |
try { |
255 | 0 | if (servicePath.equals("/")){ |
256 | 0 | this.endpointUrl = new URL(endPointURL + this.getServiceName().getLocalPart()); |
257 | |
} else { |
258 | 0 | this.endpointUrl = new URL(endPointURL + "/" + this.getServiceName().getLocalPart()); |
259 | |
} |
260 | 0 | } catch (Exception e) { |
261 | 0 | throw new ConfigurationException("Service Endpoint URL creation failed.", e); |
262 | 0 | } |
263 | |
|
264 | |
} |
265 | |
|
266 | 1 | if (this.priority == null) { |
267 | 1 | setPriority(5); |
268 | |
} |
269 | |
|
270 | 1 | if (this.retryAttempts == null) { |
271 | 1 | setRetryAttempts(0); |
272 | |
} |
273 | |
|
274 | 1 | if (this.millisToLive == null) { |
275 | 1 | setMillisToLive(new Long(-1)); |
276 | |
} |
277 | |
|
278 | 1 | if (StringUtils.isBlank(cacheManager)) { |
279 | 1 | LOG.warn("The cache manager was blank for " + (serviceName != null ? serviceName : localServiceName) + ". This service will not have client-side caching."); |
280 | 0 | } else if (GlobalResourceLoader.getService(cacheManager) == null){ |
281 | |
|
282 | |
|
283 | |
} |
284 | |
|
285 | 1 | } |
286 | |
|
287 | |
@Override |
288 | |
public Endpoint establishEndpoint() { |
289 | 0 | return BasicEndpoint.newEndpoint(configure(), getService()); |
290 | |
} |
291 | |
|
292 | |
protected abstract ServiceConfiguration configure(); |
293 | |
|
294 | |
public String getServiceNameSpaceURI() { |
295 | 0 | return this.serviceNameSpaceURI; |
296 | |
} |
297 | |
public void setServiceNameSpaceURI(String serviceNameSpaceURI) { |
298 | 0 | this.serviceNameSpaceURI = serviceNameSpaceURI; |
299 | 0 | } |
300 | |
public Long getMillisToLive() { |
301 | 8 | return this.millisToLive; |
302 | |
} |
303 | |
public void setMillisToLive(Long millisToLive) { |
304 | 2 | this.millisToLive = millisToLive; |
305 | 2 | } |
306 | |
public Boolean getBusSecurity() { |
307 | 8 | return this.busSecurity; |
308 | |
} |
309 | |
public void setBusSecurity(Boolean busSecurity) { |
310 | 7 | this.busSecurity = busSecurity; |
311 | 7 | } |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
public String getServicePath() { |
317 | 0 | return this.servicePath; |
318 | |
} |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
public void setServicePath(String servicePath) { |
324 | 0 | this.servicePath = servicePath; |
325 | 0 | } |
326 | |
|
327 | |
public String toString() { |
328 | 0 | return ReflectionToStringBuilder.toString(this); |
329 | |
} |
330 | |
|
331 | |
@Override |
332 | |
public boolean equals(Object object) { |
333 | 0 | return EqualsBuilder.reflectionEquals(object, this); |
334 | |
} |
335 | |
|
336 | |
@Override |
337 | |
public int hashCode() { |
338 | 0 | return HashCodeBuilder.reflectionHashCode(this); |
339 | |
} |
340 | |
|
341 | |
|
342 | |
} |