View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.impl.bus;
17  
18  import org.apache.commons.codec.binary.Base64;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.exception.RiceRuntimeException;
21  import org.kuali.rice.core.api.util.ChecksumUtils;
22  import org.kuali.rice.core.api.util.RiceUtilities;
23  import org.kuali.rice.ksb.api.bus.Endpoint;
24  import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
25  import org.kuali.rice.ksb.api.bus.ServiceDefinition;
26  import org.kuali.rice.ksb.api.registry.ServiceDescriptor;
27  import org.kuali.rice.ksb.api.registry.ServiceEndpoint;
28  import org.kuali.rice.ksb.api.registry.ServiceEndpointStatus;
29  import org.kuali.rice.ksb.api.registry.ServiceInfo;
30  
31  import javax.xml.namespace.QName;
32  import java.io.ByteArrayOutputStream;
33  import java.io.IOException;
34  import java.io.ObjectOutput;
35  import java.io.ObjectOutputStream;
36  import java.io.UnsupportedEncodingException;
37  import java.security.GeneralSecurityException;
38  import java.security.MessageDigest;
39  
40  public final class LocalService {
41  	
42  	private final ServiceDefinition serviceDefinition;
43  	private final Endpoint endpoint;
44  	private final ServiceEndpoint serviceEndpoint;
45  	
46  	LocalService(String instanceId, ServiceDefinition serviceDefinition) {
47  		this(instanceId, serviceDefinition, null);
48  	}
49  	
50  	LocalService(String instanceId, ServiceDefinition serviceDefinition, ServiceEndpoint serviceEndpoint) {
51  		if (StringUtils.isBlank(instanceId)) {
52  			throw new IllegalArgumentException("instanceId was blank or null");
53  		}
54  		if (serviceDefinition == null) {
55  			throw new IllegalArgumentException("serviceDefinition was null");
56  		}
57  		this.serviceDefinition = serviceDefinition;
58  		this.endpoint = serviceDefinition.establishEndpoint();
59  		if (serviceEndpoint != null) {
60  			this.serviceEndpoint = serviceEndpoint;
61  		} else {
62  			this.serviceEndpoint = constructServiceEndpoint(instanceId, this.endpoint);
63  		}
64  	}
65  	
66  	LocalService(LocalService currentLocalService, ServiceEndpoint newServiceEndpoint) {
67  		this(newServiceEndpoint.getInfo().getInstanceId(), currentLocalService.getServiceDefinition(), newServiceEndpoint);
68  	}
69  	
70  	public QName getServiceName() {
71  		return endpoint.getServiceConfiguration().getServiceName();
72  	}
73  		
74  	public ServiceDefinition getServiceDefinition() {
75  		return serviceDefinition;
76  	}
77  	
78  	public Endpoint getEndpoint() {
79  		return endpoint;
80  	}
81  	
82  	public ServiceEndpoint getServiceEndpoint() {
83  		return this.serviceEndpoint;
84  	}
85  		
86  	static ServiceEndpoint constructServiceEndpoint(String instanceId, Endpoint endpoint) {
87  		ServiceInfo.Builder serviceInfo = constructServiceInfo(instanceId, endpoint.getServiceConfiguration());
88  		ServiceDescriptor.Builder serviceDescriptor = constructDescriptor(endpoint.getServiceConfiguration());
89  		ServiceEndpoint.Builder builder = ServiceEndpoint.Builder.create(serviceInfo, serviceDescriptor);
90  		return builder.build();
91  	}
92  	
93  	static ServiceInfo.Builder constructServiceInfo(String instanceId, ServiceConfiguration serviceConfiguration) {
94  		ServiceInfo.Builder builder = ServiceInfo.Builder.create();
95  		builder.setInstanceId(instanceId);
96  		builder.setApplicationId(serviceConfiguration.getApplicationId());
97  		builder.setChecksum(ChecksumUtils.calculateChecksum(serviceConfiguration));
98  		builder.setEndpointUrl(serviceConfiguration.getEndpointUrl().toExternalForm());
99  		builder.setServerIpAddress(RiceUtilities.getIpNumber());
100 		builder.setServiceName(serviceConfiguration.getServiceName());
101 		builder.setServiceVersion(serviceConfiguration.getServiceVersion());
102 		builder.setStatus(ServiceEndpointStatus.ONLINE);
103 		builder.setType(serviceConfiguration.getType());
104 		return builder;
105 	}
106 	
107 	static ServiceDescriptor.Builder constructDescriptor(ServiceConfiguration serviceConfiguration) {
108 		ServiceDescriptor.Builder builder = ServiceDescriptor.Builder.create();
109 		builder.setDescriptor(ServiceConfigurationSerializationHandler.marshallToXml(serviceConfiguration));
110 		return builder;
111 	}
112 	
113 }