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