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