001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.ksb.impl.bus; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.core.api.util.ChecksumUtils; 020import org.kuali.rice.core.api.util.RiceUtilities; 021import org.kuali.rice.ksb.api.bus.Endpoint; 022import org.kuali.rice.ksb.api.bus.ServiceConfiguration; 023import org.kuali.rice.ksb.api.bus.ServiceDefinition; 024import org.kuali.rice.ksb.api.registry.ServiceDescriptor; 025import org.kuali.rice.ksb.api.registry.ServiceEndpoint; 026import org.kuali.rice.ksb.api.registry.ServiceEndpointStatus; 027import org.kuali.rice.ksb.api.registry.ServiceInfo; 028 029import javax.xml.namespace.QName; 030 031public final class LocalService { 032 033 private final ServiceDefinition serviceDefinition; 034 private final Endpoint endpoint; 035 private final ServiceEndpoint serviceEndpoint; 036 037 LocalService(String instanceId, ServiceDefinition serviceDefinition) { 038 this(instanceId, serviceDefinition, null); 039 } 040 041 LocalService(String instanceId, ServiceDefinition serviceDefinition, ServiceEndpoint serviceEndpoint) { 042 if (StringUtils.isBlank(instanceId)) { 043 throw new IllegalArgumentException("instanceId was blank or null"); 044 } 045 if (serviceDefinition == null) { 046 throw new IllegalArgumentException("serviceDefinition was null"); 047 } 048 this.serviceDefinition = serviceDefinition; 049 this.endpoint = serviceDefinition.establishEndpoint(); 050 if (serviceEndpoint != null) { 051 this.serviceEndpoint = serviceEndpoint; 052 } else { 053 this.serviceEndpoint = constructServiceEndpoint(instanceId, this.endpoint); 054 } 055 } 056 057 LocalService(LocalService currentLocalService, ServiceEndpoint newServiceEndpoint) { 058 this(newServiceEndpoint.getInfo().getInstanceId(), currentLocalService.getServiceDefinition(), newServiceEndpoint); 059 } 060 061 public QName getServiceName() { 062 return endpoint.getServiceConfiguration().getServiceName(); 063 } 064 065 public ServiceDefinition getServiceDefinition() { 066 return serviceDefinition; 067 } 068 069 public Endpoint getEndpoint() { 070 return endpoint; 071 } 072 073 public ServiceEndpoint getServiceEndpoint() { 074 return this.serviceEndpoint; 075 } 076 077 static ServiceEndpoint constructServiceEndpoint(String instanceId, Endpoint endpoint) { 078 ServiceInfo.Builder serviceInfo = constructServiceInfo(instanceId, endpoint.getServiceConfiguration()); 079 ServiceDescriptor.Builder serviceDescriptor = constructDescriptor(endpoint.getServiceConfiguration()); 080 ServiceEndpoint.Builder builder = ServiceEndpoint.Builder.create(serviceInfo, serviceDescriptor); 081 return builder.build(); 082 } 083 084 static ServiceInfo.Builder constructServiceInfo(String instanceId, ServiceConfiguration serviceConfiguration) { 085 ServiceInfo.Builder builder = ServiceInfo.Builder.create(); 086 builder.setInstanceId(instanceId); 087 builder.setApplicationId(serviceConfiguration.getApplicationId()); 088 builder.setChecksum(ChecksumUtils.calculateChecksum(serviceConfiguration)); 089 builder.setEndpointUrl(serviceConfiguration.getEndpointUrl().toExternalForm()); 090 builder.setServerIpAddress(RiceUtilities.getIpNumber()); 091 builder.setServiceName(serviceConfiguration.getServiceName()); 092 builder.setServiceVersion(serviceConfiguration.getServiceVersion()); 093 builder.setStatus(ServiceEndpointStatus.ONLINE); 094 builder.setType(serviceConfiguration.getType()); 095 return builder; 096 } 097 098 static ServiceDescriptor.Builder constructDescriptor(ServiceConfiguration serviceConfiguration) { 099 ServiceDescriptor.Builder builder = ServiceDescriptor.Builder.create(); 100 builder.setDescriptor(ServiceConfigurationSerializationHandler.marshallToXml(serviceConfiguration)); 101 return builder; 102 } 103 104}