View Javadoc

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