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.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   * The definition of a service on the service bus.
40   * 
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public abstract class AbstractServiceDefinition implements ServiceDefinition {
44  
45  	private static final Logger LOG = Logger.getLogger(AbstractServiceDefinition.class);
46  		
47  	// used internally to construct the service name
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  	// if the service is exported from a plugin, we need to ensure it's invoked within the proper classloading context!
67  	private ClassLoader serviceClassLoader;
68      private String cacheManager;
69  			
70  	protected AbstractServiceDefinition() {
71  		this.busSecurity = Boolean.TRUE;
72  		this.queue = true;
73  		this.serviceClassLoader = ClassLoaderUtils.getDefaultClassLoader();
74  	}
75  
76  	public Object getService() {
77  		return this.service;
78  	}
79  	public void setService(Object service) {
80  		this.service = service;
81  	}
82  	
83  	public String getLocalServiceName() {
84  		return this.localServiceName;
85  	}
86  	
87  	public void setLocalServiceName(String serviceName) {
88  		this.localServiceName = serviceName;
89  	}
90  	
91  	public String getMessageExceptionHandler() {
92  		return this.messageExceptionHandler;
93  	}
94  	
95  	public void setMessageExceptionHandler(String messageExceptionHandler) {
96  		this.messageExceptionHandler = messageExceptionHandler;
97  	}
98  	
99  	public Integer getPriority() {
100 		return this.priority;
101 	}
102 	
103 	public void setPriority(Integer priority) {
104 		this.priority = priority;
105 	}
106 	
107 	public boolean isQueue() {
108 		return this.queue;
109 	}
110 	
111 	public void setQueue(boolean queue) {
112 		this.queue = queue;
113 	}
114 	
115 	public Integer getRetryAttempts() {
116 		return this.retryAttempts;
117 	}
118 	
119 	public void setRetryAttempts(Integer retryAttempts) {
120 		this.retryAttempts = retryAttempts;
121 	}
122 
123 	public QName getServiceName() {
124 		if (this.serviceName == null) {
125 			if (this.serviceNameSpaceURI == null) {
126 			    return new QName(this.applicationId, this.localServiceName);	
127 			} else {
128 			    return new QName(this.serviceNameSpaceURI, this.localServiceName);
129 			}
130 			
131 		}
132 		return this.serviceName;
133 	}
134 	public void setServiceName(QName serviceName) {
135 		this.serviceName = serviceName;
136 	}
137 	
138 	@Override
139 	public URL getEndpointUrl() {
140 		return this.endpointUrl;
141 	}
142 	public void setEndpointUrl(URL endpointUrl) {
143 		this.endpointUrl = endpointUrl;
144 	}
145 	
146 	public void setCredentialsType(CredentialsType credentialsType) {
147 	    this.credentialsType = credentialsType;
148 	}
149 	
150 	public CredentialsType getCredentialsType() {
151 	    return this.credentialsType;
152 	}
153 	
154 	public String getServiceVersion() {
155 		return serviceVersion;
156 	}
157 
158 	public void setServiceVersion(String serviceVersion) {
159 		this.serviceVersion = serviceVersion;
160 	}
161 
162 	public String getApplicationId() {
163 		return applicationId;
164 	}
165 	
166 	public void setApplicationId(String applicationId) {
167 		this.applicationId = applicationId;
168 	}
169 
170     @Override
171     public String getInstanceId() {
172         return instanceId;
173     }
174 
175     public void setInstanceId(String instanceId) {
176         this.instanceId = instanceId;
177     }
178 
179     @Override
180 	public ClassLoader getServiceClassLoader() {
181 		return this.serviceClassLoader;
182 	}
183 	
184 	public void setServiceClassLoader(ClassLoader serviceClassLoader) {
185 		this.serviceClassLoader = serviceClassLoader;
186 	}
187 
188     @Override
189     public String getCacheManager() {
190         return cacheManager;
191     }
192 
193     public void setCacheManager(String cacheManager) {
194         this.cacheManager = cacheManager;
195     }
196 	
197 	@Override
198 	public void validate() {
199 		
200 		if (this.serviceName == null && this.localServiceName == null) {
201 			throw new ConfigurationException("Must give a serviceName or localServiceName");
202 		}
203 
204 		if (this.applicationId == null) {
205 			String applicationId = CoreConfigHelper.getApplicationId();
206 			if (applicationId == null) {
207 				throw new ConfigurationException("Must have an applicationId");
208 			}	
209 			this.applicationId = applicationId;
210 		}
211 
212         if (this.instanceId == null) {
213 			String instanceId = CoreConfigHelper.getInstanceId();
214 			if (instanceId == null) {
215 				throw new ConfigurationException("Must have an instanceId");
216 			}
217 			this.instanceId = instanceId;
218 		}
219 		
220 		if (this.serviceName != null && this.localServiceName == null) {
221 			this.localServiceName = this.getServiceName().getLocalPart();
222 		}
223 			
224 		if (this.servicePath != null){
225 			if (this.servicePath.endsWith("/")){
226 				this.servicePath = StringUtils.chop(servicePath);
227 			}
228 			if (!this.servicePath.startsWith("/")){
229 				this.servicePath = "/" + this.servicePath;
230 			}
231 		} else {
232             // default the serivce path to namespace
233 			this.servicePath = "/";
234 		}
235 		
236 		// default to 'unspecified' service version
237 		if (StringUtils.isBlank(serviceVersion)) {
238 			setServiceVersion(CoreConstants.Versions.UNSPECIFIED);
239 		}
240 
241 		LOG.debug("Validating service " + this.serviceName);
242 		
243 		
244 		if (this.endpointUrl == null) {
245 			String endPointURL = ConfigContext.getCurrentContextConfig().getEndPointUrl();
246 			if (endPointURL == null) {
247 				throw new ConfigurationException("Must provide a serviceEndPoint or serviceServletURL");
248 			}
249 			if (! endPointURL.endsWith("/")) {
250 				endPointURL += servicePath;
251 			} else {
252 				endPointURL = StringUtils.chop(endPointURL) + servicePath;
253 			}
254 			try {
255 				if (servicePath.equals("/")){
256 					this.endpointUrl = new URL(endPointURL + this.getServiceName().getLocalPart());
257 				} else {
258 					this.endpointUrl = new URL(endPointURL + "/" + this.getServiceName().getLocalPart());
259 				}
260             } catch (Exception e) {
261 				throw new ConfigurationException("Service Endpoint URL creation failed.", e);
262 			}
263 			
264 		}
265 				
266 		if (this.priority == null) {
267 			setPriority(5);
268 		}
269 		
270 		if (this.retryAttempts == null) {
271 			setRetryAttempts(0);
272 		}
273 		
274 		if (this.millisToLive == null) {
275 			setMillisToLive(new Long(-1));
276 		}
277 
278         if (StringUtils.isBlank(cacheManager)) {
279             LOG.warn("The cache manager was blank for " + (serviceName != null ? serviceName : localServiceName) + ". This service will not have client-side caching.");
280         } else if (GlobalResourceLoader.getService(cacheManager) == null){
281             //FIXME: it seems that the spring bean isn't available here or something....
282             //LOG.warn("The cache manager " + cacheManager + " was not found for " + (serviceName != null ? serviceName : localServiceName) + ". This service will not have client-side caching.");
283         }
284 
285 	}
286 	
287 	@Override
288 	public Endpoint establishEndpoint() {
289 		return BasicEndpoint.newEndpoint(configure(), getService());
290 	}
291 	
292 	protected abstract ServiceConfiguration configure();
293 	
294 	public String getServiceNameSpaceURI() {
295 		return this.serviceNameSpaceURI;
296 	}
297 	public void setServiceNameSpaceURI(String serviceNameSpaceURI) {
298 		this.serviceNameSpaceURI = serviceNameSpaceURI;
299 	}
300 	public Long getMillisToLive() {
301 		return this.millisToLive;
302 	}
303 	public void setMillisToLive(Long millisToLive) {
304 		this.millisToLive = millisToLive;
305 	}
306 	public Boolean getBusSecurity() {
307 		return this.busSecurity;
308 	}
309 	public void setBusSecurity(Boolean busSecurity) {
310 		this.busSecurity = busSecurity;
311 	}
312 	
313 	/**
314 	 * @return the servicePath
315 	 */
316 	public String getServicePath() {
317 		return this.servicePath;
318 	}
319 
320 	/**
321 	 * @param servicePath the servicePath to set
322 	 */
323 	public void setServicePath(String servicePath) {
324 		this.servicePath = servicePath;
325 	}
326 	
327 	public String toString() {
328 	    return ReflectionToStringBuilder.toString(this);
329 	}
330 	
331 	@Override
332     public boolean equals(Object object) {
333 		return EqualsBuilder.reflectionEquals(object, this);
334     }
335 
336 	@Override
337     public int hashCode() {
338         return HashCodeBuilder.reflectionHashCode(this);
339     }
340 
341 	
342 }