Coverage Report - org.kuali.rice.ksb.impl.bus.LocalService
 
Classes in this File Line Coverage Branch Coverage Complexity
LocalService
71%
38/53
50%
3/6
2.182
 
 1  
 package org.kuali.rice.ksb.impl.bus;
 2  
 
 3  
 import org.apache.commons.codec.binary.Base64;
 4  
 import org.apache.commons.lang.StringUtils;
 5  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 6  
 import org.kuali.rice.core.api.util.RiceUtilities;
 7  
 import org.kuali.rice.ksb.api.bus.Endpoint;
 8  
 import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
 9  
 import org.kuali.rice.ksb.api.bus.ServiceDefinition;
 10  
 import org.kuali.rice.ksb.api.registry.ServiceDescriptor;
 11  
 import org.kuali.rice.ksb.api.registry.ServiceEndpoint;
 12  
 import org.kuali.rice.ksb.api.registry.ServiceEndpointStatus;
 13  
 import org.kuali.rice.ksb.api.registry.ServiceInfo;
 14  
 
 15  
 import javax.xml.namespace.QName;
 16  
 import java.io.ByteArrayOutputStream;
 17  
 import java.io.IOException;
 18  
 import java.io.ObjectOutput;
 19  
 import java.io.ObjectOutputStream;
 20  
 import java.io.UnsupportedEncodingException;
 21  
 import java.security.GeneralSecurityException;
 22  
 import java.security.MessageDigest;
 23  
 
 24  
 public final class LocalService {
 25  
         
 26  
         private final ServiceDefinition serviceDefinition;
 27  
         private final Endpoint endpoint;
 28  
         private final ServiceEndpoint serviceEndpoint;
 29  
         
 30  
         LocalService(String instanceId, ServiceDefinition serviceDefinition) {
 31  10
                 this(instanceId, serviceDefinition, null);
 32  10
         }
 33  
         
 34  10
         LocalService(String instanceId, ServiceDefinition serviceDefinition, ServiceEndpoint serviceEndpoint) {
 35  10
                 if (StringUtils.isBlank(instanceId)) {
 36  0
                         throw new IllegalArgumentException("instanceId was blank or null");
 37  
                 }
 38  10
                 if (serviceDefinition == null) {
 39  0
                         throw new IllegalArgumentException("serviceDefinition was null");
 40  
                 }
 41  10
                 this.serviceDefinition = serviceDefinition;
 42  10
                 this.endpoint = serviceDefinition.establishEndpoint();
 43  10
                 if (serviceEndpoint != null) {
 44  0
                         this.serviceEndpoint = serviceEndpoint;
 45  
                 } else {
 46  10
                         this.serviceEndpoint = constructServiceEndpoint(instanceId, this.endpoint);
 47  
                 }
 48  10
         }
 49  
         
 50  
         LocalService(LocalService currentLocalService, ServiceEndpoint newServiceEndpoint) {
 51  0
                 this(newServiceEndpoint.getInfo().getInstanceId(), currentLocalService.getServiceDefinition(), newServiceEndpoint);
 52  0
         }
 53  
         
 54  
         public QName getServiceName() {
 55  10
                 return endpoint.getServiceConfiguration().getServiceName();
 56  
         }
 57  
                 
 58  
         public ServiceDefinition getServiceDefinition() {
 59  0
                 return serviceDefinition;
 60  
         }
 61  
         
 62  
         public Endpoint getEndpoint() {
 63  0
                 return endpoint;
 64  
         }
 65  
         
 66  
         public ServiceEndpoint getServiceEndpoint() {
 67  20
                 return this.serviceEndpoint;
 68  
         }
 69  
                 
 70  
         static ServiceEndpoint constructServiceEndpoint(String instanceId, Endpoint endpoint) {
 71  10
                 ServiceInfo.Builder serviceInfo = constructServiceInfo(instanceId, endpoint.getServiceConfiguration());
 72  10
                 ServiceDescriptor.Builder serviceDescriptor = constructDescriptor(endpoint.getServiceConfiguration());
 73  10
                 ServiceEndpoint.Builder builder = ServiceEndpoint.Builder.create(serviceInfo, serviceDescriptor);
 74  10
                 return builder.build();
 75  
         }
 76  
         
 77  
         static ServiceInfo.Builder constructServiceInfo(String instanceId, ServiceConfiguration serviceConfiguration) {
 78  10
                 ServiceInfo.Builder builder = ServiceInfo.Builder.create();
 79  10
                 builder.setInstanceId(instanceId);
 80  10
                 builder.setApplicationId(serviceConfiguration.getApplicationId());
 81  10
                 builder.setChecksum(calculateChecksum(serviceConfiguration));
 82  10
                 builder.setEndpointUrl(serviceConfiguration.getEndpointUrl().toExternalForm());
 83  10
                 builder.setServerIpAddress(RiceUtilities.getIpNumber());
 84  10
                 builder.setServiceName(serviceConfiguration.getServiceName());
 85  10
                 builder.setServiceVersion(serviceConfiguration.getServiceVersion());
 86  10
                 builder.setStatus(ServiceEndpointStatus.ONLINE);
 87  10
                 builder.setType(serviceConfiguration.getType());
 88  10
                 return builder;
 89  
         }
 90  
         
 91  
         static ServiceDescriptor.Builder constructDescriptor(ServiceConfiguration serviceConfiguration) {
 92  10
                 ServiceDescriptor.Builder builder = ServiceDescriptor.Builder.create();
 93  10
                 builder.setDescriptor(ServiceConfigurationSerializationHandler.marshallToXml(serviceConfiguration));
 94  10
                 return builder;
 95  
         }
 96  
         
 97  
         /**
 98  
          * Creates a checksum for the given ServiceConfiguration.
 99  
          * 
 100  
          * @param serviceConfiguration The configuration for which to calcuate the checksum
 101  
          * @return A checksum value for the object.
 102  
          */
 103  
         static String calculateChecksum(ServiceConfiguration serviceConfiguration) {
 104  10
                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
 105  10
         ObjectOutput out = null;
 106  
         try {
 107  10
             out = new ObjectOutputStream(bos);
 108  10
             out.writeObject(serviceConfiguration);
 109  0
         } catch (IOException e) {
 110  0
             throw new RiceRuntimeException(e);
 111  
         } finally {
 112  0
             try {
 113  10
                 out.close();
 114  10
             } catch (IOException e) {}
 115  0
         }
 116  
         try {
 117  10
             MessageDigest md = MessageDigest.getInstance("SHA-1");
 118  10
             return new String( Base64.encodeBase64( md.digest( bos.toByteArray() ) ), "UTF-8");
 119  0
         } catch( GeneralSecurityException ex ) {
 120  0
                 throw new RiceRuntimeException(ex);
 121  0
         } catch( UnsupportedEncodingException ex ) {
 122  0
                 throw new RiceRuntimeException(ex);
 123  
         }
 124  
         }
 125  
         
 126  
 }