View Javadoc

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  		this(instanceId, serviceDefinition, null);
32  	}
33  	
34  	LocalService(String instanceId, ServiceDefinition serviceDefinition, ServiceEndpoint serviceEndpoint) {
35  		if (StringUtils.isBlank(instanceId)) {
36  			throw new IllegalArgumentException("instanceId was blank or null");
37  		}
38  		if (serviceDefinition == null) {
39  			throw new IllegalArgumentException("serviceDefinition was null");
40  		}
41  		this.serviceDefinition = serviceDefinition;
42  		this.endpoint = serviceDefinition.establishEndpoint();
43  		if (serviceEndpoint != null) {
44  			this.serviceEndpoint = serviceEndpoint;
45  		} else {
46  			this.serviceEndpoint = constructServiceEndpoint(instanceId, this.endpoint);
47  		}
48  	}
49  	
50  	LocalService(LocalService currentLocalService, ServiceEndpoint newServiceEndpoint) {
51  		this(newServiceEndpoint.getInfo().getInstanceId(), currentLocalService.getServiceDefinition(), newServiceEndpoint);
52  	}
53  	
54  	public QName getServiceName() {
55  		return endpoint.getServiceConfiguration().getServiceName();
56  	}
57  		
58  	public ServiceDefinition getServiceDefinition() {
59  		return serviceDefinition;
60  	}
61  	
62  	public Endpoint getEndpoint() {
63  		return endpoint;
64  	}
65  	
66  	public ServiceEndpoint getServiceEndpoint() {
67  		return this.serviceEndpoint;
68  	}
69  		
70  	static ServiceEndpoint constructServiceEndpoint(String instanceId, Endpoint endpoint) {
71  		ServiceInfo.Builder serviceInfo = constructServiceInfo(instanceId, endpoint.getServiceConfiguration());
72  		ServiceDescriptor.Builder serviceDescriptor = constructDescriptor(endpoint.getServiceConfiguration());
73  		ServiceEndpoint.Builder builder = ServiceEndpoint.Builder.create(serviceInfo, serviceDescriptor);
74  		return builder.build();
75  	}
76  	
77  	static ServiceInfo.Builder constructServiceInfo(String instanceId, ServiceConfiguration serviceConfiguration) {
78  		ServiceInfo.Builder builder = ServiceInfo.Builder.create();
79  		builder.setInstanceId(instanceId);
80  		builder.setApplicationId(serviceConfiguration.getApplicationId());
81  		builder.setChecksum(calculateChecksum(serviceConfiguration));
82  		builder.setEndpointUrl(serviceConfiguration.getEndpointUrl().toExternalForm());
83  		builder.setServerIpAddress(RiceUtilities.getIpNumber());
84  		builder.setServiceName(serviceConfiguration.getServiceName());
85  		builder.setServiceVersion(serviceConfiguration.getServiceVersion());
86  		builder.setStatus(ServiceEndpointStatus.ONLINE);
87  		builder.setType(serviceConfiguration.getType());
88  		return builder;
89  	}
90  	
91  	static ServiceDescriptor.Builder constructDescriptor(ServiceConfiguration serviceConfiguration) {
92  		ServiceDescriptor.Builder builder = ServiceDescriptor.Builder.create();
93  		builder.setDescriptor(ServiceConfigurationSerializationHandler.marshallToXml(serviceConfiguration));
94  		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 		ByteArrayOutputStream bos = new ByteArrayOutputStream();
105         ObjectOutput out = null;
106         try {
107             out = new ObjectOutputStream(bos);
108             out.writeObject(serviceConfiguration);
109         } catch (IOException e) {
110             throw new RiceRuntimeException(e);
111         } finally {
112             try {
113                 out.close();
114             } catch (IOException e) {}
115         }
116         try {
117             MessageDigest md = MessageDigest.getInstance("SHA-1");
118             return new String( Base64.encodeBase64( md.digest( bos.toByteArray() ) ), "UTF-8");
119         } catch( GeneralSecurityException ex ) {
120         	throw new RiceRuntimeException(ex);
121         } catch( UnsupportedEncodingException ex ) {
122         	throw new RiceRuntimeException(ex);
123         }
124 	}
125 	
126 }