View Javadoc

1   /*
2    * Copyright 2006-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  
17  package org.kuali.rice.ksb.api.bus.support;
18  
19  import java.net.URL;
20  
21  import javax.xml.namespace.QName;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.commons.lang.builder.EqualsBuilder;
25  import org.apache.commons.lang.builder.HashCodeBuilder;
26  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
27  import org.apache.log4j.Logger;
28  import org.kuali.rice.core.api.CoreConstants;
29  import org.kuali.rice.core.api.config.ConfigurationException;
30  import org.kuali.rice.core.api.config.CoreConfigHelper;
31  import org.kuali.rice.core.api.config.property.ConfigContext;
32  import org.kuali.rice.core.api.security.credentials.CredentialsType;
33  import org.kuali.rice.core.util.ClassLoaderUtils;
34  import org.kuali.rice.ksb.api.bus.Endpoint;
35  import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
36  import org.kuali.rice.ksb.api.bus.ServiceDefinition;
37  
38  
39  /**
40   * The definition of a service on the service bus.
41   * 
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public abstract class AbstractServiceDefinition implements ServiceDefinition {
45  
46  	private static final Logger LOG = Logger.getLogger(AbstractServiceDefinition.class);
47  		
48  	// used internally to construct the service name
49  	private String localServiceName;
50  	private String serviceNameSpaceURI;
51  	
52  	private Object service;
53  	private QName serviceName;
54  	private boolean queue;
55  	private Integer priority;
56  	private Integer retryAttempts;
57  	private Long millisToLive;
58  	private String messageExceptionHandler;
59  	private String servicePath;
60  	private URL endpointUrl;
61  	private Boolean busSecurity;
62  	private CredentialsType credentialsType;
63  	private String serviceVersion;
64  	private String applicationId;
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 ClassLoader getServiceClassLoader() {
170 		return this.serviceClassLoader;
171 	}
172 	
173 	public void setServiceClassLoader(ClassLoader serviceClassLoader) {
174 		this.serviceClassLoader = serviceClassLoader;
175 	}
176 	
177 	@Override
178 	public void validate() {
179 		
180 		if (this.serviceName == null && this.localServiceName == null) {
181 			throw new ConfigurationException("Must give a serviceName or localServiceName");
182 		}
183 		
184 		if (this.applicationId == null) {
185 			String applicationId = CoreConfigHelper.getApplicationId();
186 			if (applicationId == null) {
187 				throw new ConfigurationException("Must have an applicationId");
188 			}	
189 			this.applicationId = applicationId;
190 		}
191 		
192 		if (this.serviceName != null && this.localServiceName == null) {
193 			this.localServiceName = this.getServiceName().getLocalPart();
194 		}
195 			
196 		if (this.servicePath != null){
197 			if (this.servicePath.endsWith("/")){
198 				this.servicePath = StringUtils.chop(servicePath);
199 			}
200 			if (!this.servicePath.startsWith("/")){
201 				this.servicePath = "/" + this.servicePath;
202 			}
203 		} else {
204 			this.servicePath = "/";
205 		}
206 		
207 		// default to 'unspecified' service version
208 		if (StringUtils.isBlank(serviceVersion)) {
209 			setServiceVersion(CoreConstants.Versions.UNSPECIFIED);
210 		}
211 		
212 		LOG.debug("Validating service " + this.serviceName);
213 		
214 		
215 		if (this.endpointUrl == null) {
216 			String endPointURL = ConfigContext.getCurrentContextConfig().getEndPointUrl();
217 			if (endPointURL == null) {
218 				throw new ConfigurationException("Must provide a serviceEndPoint or serviceServletURL");
219 			}
220 			if (! endPointURL.endsWith("/")) {
221 				endPointURL += servicePath;
222 			} else {
223 				endPointURL = StringUtils.chop(endPointURL) + servicePath;
224 			}
225 			try {
226 				if (servicePath.equals("/")){
227 					this.endpointUrl = new URL(endPointURL + this.getServiceName().getLocalPart());
228 				} else {
229 					this.endpointUrl = new URL(endPointURL + "/" + this.getServiceName().getLocalPart());
230 				}
231 			} catch (Exception e) {
232 				throw new ConfigurationException("Service Endpoint URL creation failed.", e);
233 			}
234 			
235 		}
236 				
237 		if (this.priority == null) {
238 			setPriority(5);
239 		}
240 		
241 		if (this.retryAttempts == null) {
242 			setRetryAttempts(0);
243 		}
244 		
245 		if (this.millisToLive == null) {
246 			setMillisToLive(new Long(-1));
247 		}
248 		
249 	}
250 	
251 	@Override
252 	public Endpoint establishEndpoint() {
253 		return BasicEndpoint.newEndpoint(configure(), getService());
254 	}
255 	
256 	protected abstract ServiceConfiguration configure();
257 	
258 	public String getServiceNameSpaceURI() {
259 		return this.serviceNameSpaceURI;
260 	}
261 	public void setServiceNameSpaceURI(String serviceNameSpaceURI) {
262 		this.serviceNameSpaceURI = serviceNameSpaceURI;
263 	}
264 	public Long getMillisToLive() {
265 		return this.millisToLive;
266 	}
267 	public void setMillisToLive(Long millisToLive) {
268 		this.millisToLive = millisToLive;
269 	}
270 	public Boolean getBusSecurity() {
271 		return this.busSecurity;
272 	}
273 	public void setBusSecurity(Boolean busSecurity) {
274 		this.busSecurity = busSecurity;
275 	}
276 	
277 	/**
278 	 * @return the servicePath
279 	 */
280 	public String getServicePath() {
281 		return this.servicePath;
282 	}
283 
284 	/**
285 	 * @param servicePath the servicePath to set
286 	 */
287 	public void setServicePath(String servicePath) {
288 		this.servicePath = servicePath;
289 	}
290 	
291 	public String toString() {
292 	    return ReflectionToStringBuilder.toString(this);
293 	}
294 	
295 	@Override
296     public boolean equals(Object object) {
297 		return EqualsBuilder.reflectionEquals(object, this);
298     }
299 
300 	@Override
301     public int hashCode() {
302         return HashCodeBuilder.reflectionHashCode(this);
303     }
304 
305 	
306 }