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