View Javadoc
1   /**
2    * Copyright 2005-2015 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.lang.StringUtils;
19  import org.kuali.rice.core.api.util.ChecksumUtils;
20  import org.kuali.rice.core.api.util.RiceUtilities;
21  import org.kuali.rice.ksb.api.bus.Endpoint;
22  import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
23  import org.kuali.rice.ksb.api.bus.ServiceDefinition;
24  import org.kuali.rice.ksb.api.registry.ServiceDescriptor;
25  import org.kuali.rice.ksb.api.registry.ServiceEndpoint;
26  import org.kuali.rice.ksb.api.registry.ServiceEndpointStatus;
27  import org.kuali.rice.ksb.api.registry.ServiceInfo;
28  
29  import javax.xml.namespace.QName;
30  
31  public final class LocalService {
32  	
33  	private final ServiceDefinition serviceDefinition;
34  	private final Endpoint endpoint;
35  	private final ServiceEndpoint serviceEndpoint;
36  	
37  	LocalService(String instanceId, ServiceDefinition serviceDefinition) {
38  		this(instanceId, serviceDefinition, null);
39  	}
40  	
41  	LocalService(String instanceId, ServiceDefinition serviceDefinition, ServiceEndpoint serviceEndpoint) {
42  		if (StringUtils.isBlank(instanceId)) {
43  			throw new IllegalArgumentException("instanceId was blank or null");
44  		}
45  		if (serviceDefinition == null) {
46  			throw new IllegalArgumentException("serviceDefinition was null");
47  		}
48  		this.serviceDefinition = serviceDefinition;
49  		this.endpoint = serviceDefinition.establishEndpoint();
50  		if (serviceEndpoint != null) {
51  			this.serviceEndpoint = serviceEndpoint;
52  		} else {
53  			this.serviceEndpoint = constructServiceEndpoint(instanceId, this.endpoint);
54  		}
55  	}
56  	
57  	LocalService(LocalService currentLocalService, ServiceEndpoint newServiceEndpoint) {
58  		this(newServiceEndpoint.getInfo().getInstanceId(), currentLocalService.getServiceDefinition(), newServiceEndpoint);
59  	}
60  	
61  	public QName getServiceName() {
62  		return endpoint.getServiceConfiguration().getServiceName();
63  	}
64  		
65  	public ServiceDefinition getServiceDefinition() {
66  		return serviceDefinition;
67  	}
68  	
69  	public Endpoint getEndpoint() {
70  		return endpoint;
71  	}
72  	
73  	public ServiceEndpoint getServiceEndpoint() {
74  		return this.serviceEndpoint;
75  	}
76  		
77  	static ServiceEndpoint constructServiceEndpoint(String instanceId, Endpoint endpoint) {
78  		ServiceInfo.Builder serviceInfo = constructServiceInfo(instanceId, endpoint.getServiceConfiguration());
79  		ServiceDescriptor.Builder serviceDescriptor = constructDescriptor(endpoint.getServiceConfiguration());
80  		ServiceEndpoint.Builder builder = ServiceEndpoint.Builder.create(serviceInfo, serviceDescriptor);
81  		return builder.build();
82  	}
83  	
84  	static ServiceInfo.Builder constructServiceInfo(String instanceId, ServiceConfiguration serviceConfiguration) {
85  		ServiceInfo.Builder builder = ServiceInfo.Builder.create();
86  		builder.setInstanceId(instanceId);
87  		builder.setApplicationId(serviceConfiguration.getApplicationId());
88  		builder.setChecksum(ChecksumUtils.calculateChecksum(serviceConfiguration));
89  		builder.setEndpointUrl(serviceConfiguration.getEndpointUrl().toExternalForm());
90  		builder.setServerIpAddress(RiceUtilities.getIpNumber());
91  		builder.setServiceName(serviceConfiguration.getServiceName());
92  		builder.setServiceVersion(serviceConfiguration.getServiceVersion());
93  		builder.setStatus(ServiceEndpointStatus.ONLINE);
94  		builder.setType(serviceConfiguration.getType());
95  		return builder;
96  	}
97  	
98  	static ServiceDescriptor.Builder constructDescriptor(ServiceConfiguration serviceConfiguration) {
99  		ServiceDescriptor.Builder builder = ServiceDescriptor.Builder.create();
100 		builder.setDescriptor(ServiceConfigurationSerializationHandler.marshallToXml(serviceConfiguration));
101 		return builder;
102 	}
103 	
104 }