View Javadoc

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