Coverage Report - org.kuali.rice.ksb.messaging.ServiceInfo
 
Classes in this File Line Coverage Branch Coverage Complexity
ServiceInfo
0%
0/95
0%
0/14
1.444
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.ksb.messaging;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.ObjectOutput;
 21  
 import java.io.ObjectOutputStream;
 22  
 import java.io.Serializable;
 23  
 import java.io.UnsupportedEncodingException;
 24  
 import java.security.GeneralSecurityException;
 25  
 import java.security.MessageDigest;
 26  
 
 27  
 import javax.persistence.Column;
 28  
 import javax.persistence.Entity;
 29  
 import javax.persistence.Id;
 30  
 import javax.persistence.NamedQueries;
 31  
 import javax.persistence.NamedQuery;
 32  
 import javax.persistence.PrePersist;
 33  
 import javax.persistence.Table;
 34  
 import javax.persistence.Transient;
 35  
 import javax.persistence.Version;
 36  
 import javax.xml.namespace.QName;
 37  
 
 38  
 import org.apache.commons.codec.binary.Base64;
 39  
 import org.apache.commons.lang.StringUtils;
 40  
 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
 41  
 import org.kuali.rice.core.config.ConfigContext;
 42  
 import org.kuali.rice.core.exception.RiceRuntimeException;
 43  
 import org.kuali.rice.core.jpa.annotations.Sequence;
 44  
 import org.kuali.rice.core.util.OrmUtils;
 45  
 import org.kuali.rice.core.util.RiceUtilities;
 46  
 import org.kuali.rice.kns.util.cache.FastByteArrayOutputStream;
 47  
 import org.kuali.rice.ksb.service.KSBServiceLocator;
 48  
 
 49  
 /**
 50  
  * Model bean that represents the definition of a service on the bus.
 51  
  * 
 52  
  * @see ServiceDefinition
 53  
  *
 54  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 55  
  */
 56  
 @Entity
 57  
 @Table(name="KRSB_SVC_DEF_T")
 58  
 @Sequence(name="KRSB_SVC_DEF_S", property="messageEntryId")
 59  
 @NamedQueries({
 60  
         @NamedQuery(name="ServiceInfo.FetchAll", query="select s from ServiceInfo s"),
 61  
         @NamedQuery(name="ServiceInfo.FetchAllActive",query="select s from ServiceInfo s where s.alive = true"),
 62  
         @NamedQuery(name="ServiceInfo.FetchActiveByName",query="select s from ServiceInfo s where s.alive = true AND s.serviceName LIKE :serviceName"),
 63  
         @NamedQuery(name="ServiceInfo.FindLocallyPublishedServices",query="select s from ServiceInfo s where s.serverIp = :serverIp AND s.serviceNamespace = :serviceNamespace"),
 64  
         @NamedQuery(name="ServiceInfo.DeleteLocallyPublishedServices",query="delete from ServiceInfo s WHERE s.serverIp = :serverIp AND s.serviceNamespace = :serviceNamespace"),
 65  
         @NamedQuery(name="ServiceInfo.DeleteByEntry",query="delete from ServiceInfo s where s.messageEntryId = :messageEntryId")                        
 66  
 })
 67  
 public class ServiceInfo implements Serializable {
 68  
  
 69  
         private static final long serialVersionUID = -4244884858494208070L;
 70  
     @Transient
 71  
         private ServiceDefinition serviceDefinition;
 72  
         @Id
 73  
         @Column(name="SVC_DEF_ID")  
 74  
         private Long messageEntryId;
 75  
     @Transient
 76  
         private QName qname;
 77  
         @Column(name="SVC_URL", length=500)
 78  
         private String endpointUrl;
 79  
     @Transient
 80  
         private String endpointAlternateUrl;
 81  
     @Column(name="FLT_SVC_DEF_ID")
 82  
     private Long flattenedServiceDefinitionId;
 83  
     @Transient
 84  
     private FlattenedServiceDefinition serializedServiceNamespace;
 85  
         @Column(name="SVC_NM")
 86  
         private String serviceName;
 87  0
     @Column(name="SVC_ALIVE")
 88  
         private Boolean alive = true; 
 89  
         @Column(name="SVC_NMSPC")
 90  
         private String serviceNamespace;
 91  
         @Column(name="SRVR_IP")
 92  
         private String serverIp;
 93  
         @Version
 94  
         @Column(name="VER_NBR")
 95  
         private Integer lockVerNbr;
 96  
         @Column(name="SVC_DEF_CHKSM", length=30)
 97  
         private String checksum;
 98  
         
 99  
         @Transient
 100  
         private transient ClassLoader serviceClassLoader;
 101  
         
 102  0
         public ServiceInfo() {
 103  
             // default constructor with nothing to do
 104  0
         }
 105  
         
 106  
         @PrePersist
 107  
     public void beforeInsert(){
 108  0
         OrmUtils.populateAutoIncValue(this, KSBServiceLocator.getRegistryEntityManagerFactory().createEntityManager());
 109  0
     }
 110  
         
 111  0
         public ServiceInfo(ServiceDefinition serviceDefinition, String serverIp, String checksum) {
 112  0
                 this.setServiceDefinition(serviceDefinition);
 113  0
                 this.setQname(serviceDefinition.getServiceName());
 114  0
                 this.setServiceNamespace(ConfigContext.getCurrentContextConfig().getServiceNamespace());
 115  0
                 this.setServerIp(serverIp);
 116  0
                 this.setEndpointUrl(serviceDefinition.getServiceEndPoint().toString());
 117  0
                 this.setServiceName(this.getQname().toString());
 118  0
                 this.setServiceClassLoader(serviceDefinition.getServiceClassLoader());
 119  0
                 this.setChecksum((checksum != null) ? checksum : this.objectToChecksum(serviceDefinition));
 120  0
         }
 121  
         
 122  0
         public ServiceInfo(ServiceDefinition serviceDefinition) {
 123  0
                 this.setServiceDefinition(serviceDefinition);
 124  0
                 this.setQname(serviceDefinition.getServiceName());
 125  0
                 this.setServiceNamespace(ConfigContext.getCurrentContextConfig().getServiceNamespace());
 126  0
                 this.setServerIp(RiceUtilities.getIpNumber());
 127  0
                 this.setEndpointUrl(serviceDefinition.getServiceEndPoint().toString());
 128  0
                 this.setServiceName(this.getQname().toString());
 129  0
                 this.setServiceClassLoader(serviceDefinition.getServiceClassLoader());
 130  0
                 this.setChecksum(this.objectToChecksum(serviceDefinition));
 131  0
         }
 132  
 
 133  
         public Long getMessageEntryId() {
 134  0
                 return this.messageEntryId;
 135  
         }
 136  
 
 137  
         public void setMessageEntryId(Long messageEntryId) {
 138  0
                 this.messageEntryId = messageEntryId;
 139  0
         }
 140  
 
 141  
         public ServiceDefinition getServiceDefinition() {
 142  0
                 return getServiceDefinition(KSBServiceLocator.getMessageHelper());
 143  
         }
 144  
 
 145  
         public ServiceDefinition getServiceDefinition(MessageHelper enMessageHelper) {
 146  0
                 if (this.serviceDefinition == null) {
 147  0
                     this.serviceDefinition = (ServiceDefinition)
 148  
                                     (enMessageHelper==null
 149  
                                                     ?KSBServiceLocator.getMessageHelper().deserializeObject(this.getSerializedServiceNamespace().getFlattenedServiceDefinitionData())
 150  
                                                                     :enMessageHelper.deserializeObject(this.getSerializedServiceNamespace().getFlattenedServiceDefinitionData()));
 151  0
                     this.serviceDefinition.setServiceClassLoader(getServiceClassLoader());
 152  
                 }
 153  0
                 return this.serviceDefinition;
 154  
         }
 155  
         
 156  
         public void setServiceDefinition(ServiceDefinition serviceDefinition) {
 157  0
                 this.serviceDefinition = serviceDefinition;
 158  0
         }
 159  
 
 160  
         public QName getQname() {
 161  0
                 if (this.qname == null) {
 162  0
                     this.qname = QName.valueOf(this.getServiceName());
 163  
                 }
 164  0
                 return this.qname;
 165  
         }
 166  
 
 167  
         public void setQname(QName serviceName) {
 168  0
                 this.qname = serviceName;
 169  0
         }
 170  
         public String getEndpointUrl() {
 171  0
                 return this.endpointUrl;
 172  
         }
 173  
         public void setEndpointUrl(String ipNumber) {
 174  0
                 this.endpointUrl = ipNumber;
 175  0
         }
 176  
         
 177  
         /**
 178  
      * @return the endpointAlternateUrl
 179  
      */
 180  
     public String getEndpointAlternateUrl() {
 181  0
         return this.endpointAlternateUrl;
 182  
     }
 183  
     
 184  
     public String getActualEndpointUrl() {
 185  0
         if (!StringUtils.isBlank(getEndpointAlternateUrl())) {
 186  0
             return getEndpointAlternateUrl();
 187  
         }
 188  0
         return getEndpointUrl();
 189  
     }
 190  
 
 191  
     /**
 192  
      * @param endpointAlternateUrl the endpointAlternateUrl to set
 193  
      */
 194  
     public void setEndpointAlternateUrl(String endpointAlternateUrl) {
 195  0
         this.endpointAlternateUrl = endpointAlternateUrl;
 196  0
     }
 197  
     
 198  
     public ClassLoader getServiceClassLoader() {
 199  0
         return this.serviceClassLoader;
 200  
     }
 201  
 
 202  
     public void setServiceClassLoader(ClassLoader serviceClassLoader) {
 203  0
         this.serviceClassLoader = serviceClassLoader;
 204  0
     }
 205  
     
 206  
     public Integer getLockVerNbr() {
 207  0
                 return this.lockVerNbr;
 208  
         }
 209  
         public void setLockVerNbr(Integer lockVerNbr) {
 210  0
                 this.lockVerNbr = lockVerNbr;
 211  0
         }
 212  
         public String getServiceNamespace() {
 213  0
                 return this.serviceNamespace;
 214  
         }
 215  
 
 216  
         public void setServiceNamespace(String ServiceNamespace) {
 217  0
                 this.serviceNamespace = ServiceNamespace;
 218  0
         }
 219  
         public String getServerIp() {
 220  0
                 return this.serverIp;
 221  
         }
 222  
 
 223  
         public void setServerIp(String serverIp) {
 224  0
                 this.serverIp = serverIp;
 225  0
         }
 226  
         public Boolean getAlive() {
 227  0
                 return this.alive;
 228  
         }
 229  
 
 230  
         public void setAlive(Boolean alive) {
 231  0
                 this.alive = alive;
 232  0
         }
 233  
         public void setSerializedServiceNamespace(FlattenedServiceDefinition serializedServiceNamespace) {
 234  0
                 this.serializedServiceNamespace = serializedServiceNamespace;
 235  0
         }
 236  
 
 237  
         public FlattenedServiceDefinition getSerializedServiceNamespace() {
 238  0
                 if (this.serializedServiceNamespace == null && this.getFlattenedServiceDefinitionId() != null) {
 239  0
                         this.serializedServiceNamespace = KSBServiceLocator.getServiceRegistry().getFlattenedServiceDefinition(this.getFlattenedServiceDefinitionId());
 240  
                 }
 241  0
                 return this.serializedServiceNamespace;
 242  
         }
 243  
 
 244  
         public String getServiceName() {
 245  0
                 return this.serviceName;
 246  
         }
 247  
 
 248  
         public void setServiceName(String serviceName) {
 249  0
                 this.serviceName = serviceName;
 250  0
         }
 251  
 
 252  
         public String toString() {
 253  0
             return ReflectionToStringBuilder.toString(this);
 254  
         }
 255  
 
 256  
         public String getChecksum() {
 257  0
                 return this.checksum;
 258  
         }
 259  
 
 260  
         public void setChecksum(String checksum) {
 261  0
                 this.checksum = checksum;
 262  0
         }
 263  
         
 264  
         public Long getFlattenedServiceDefinitionId() {
 265  0
                 return this.flattenedServiceDefinitionId;
 266  
         }
 267  
 
 268  
         public void setFlattenedServiceDefinitionId(Long flattenedServiceDefinitionId) {
 269  0
                 this.flattenedServiceDefinitionId = flattenedServiceDefinitionId;
 270  0
         }
 271  
         
 272  
         /**
 273  
          * Creates a checksum for the given serializable object (usually a ServiceDefinition in this case).
 274  
          * 
 275  
          * @param object The object (possibly a ServiceDefinition) to serialize.
 276  
          * @return A checksum for the object.
 277  
          */
 278  
         public String objectToChecksum(Serializable object) {
 279  0
         FastByteArrayOutputStream bos = new FastByteArrayOutputStream();
 280  0
         ObjectOutput out = null;
 281  
         try {
 282  0
             out = new ObjectOutputStream(bos);
 283  0
             out.writeObject(object);
 284  0
         } catch (IOException e) {
 285  0
             throw new RiceRuntimeException(e);
 286  
         } finally {
 287  0
             try {
 288  0
                 out.close();
 289  0
             } catch (IOException e) {}
 290  0
         }
 291  
         try {
 292  0
             MessageDigest md = MessageDigest.getInstance("SHA-1");
 293  0
             return new String( Base64.encodeBase64( md.digest( bos.getByteArray() ) ), "UTF-8");
 294  0
         } catch( GeneralSecurityException ex ) {
 295  0
                 throw new RiceRuntimeException(ex);
 296  0
         } catch( UnsupportedEncodingException ex ) {
 297  0
                 throw new RiceRuntimeException(ex);
 298  
         }
 299  
         }
 300  
         
 301  
 }