001    /**
002     * Copyright 2005-2014 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     */
016    package org.kuali.rice.ksb.impl.bus;
017    
018    import org.apache.commons.codec.binary.Base64;
019    import org.apache.commons.lang.StringUtils;
020    import org.kuali.rice.core.api.exception.RiceRuntimeException;
021    import org.kuali.rice.core.api.util.ChecksumUtils;
022    import org.kuali.rice.core.api.util.RiceUtilities;
023    import org.kuali.rice.ksb.api.bus.Endpoint;
024    import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
025    import org.kuali.rice.ksb.api.bus.ServiceDefinition;
026    import org.kuali.rice.ksb.api.registry.ServiceDescriptor;
027    import org.kuali.rice.ksb.api.registry.ServiceEndpoint;
028    import org.kuali.rice.ksb.api.registry.ServiceEndpointStatus;
029    import org.kuali.rice.ksb.api.registry.ServiceInfo;
030    
031    import javax.xml.namespace.QName;
032    import java.io.ByteArrayOutputStream;
033    import java.io.IOException;
034    import java.io.ObjectOutput;
035    import java.io.ObjectOutputStream;
036    import java.io.UnsupportedEncodingException;
037    import java.security.GeneralSecurityException;
038    import java.security.MessageDigest;
039    
040    public final class LocalService {
041            
042            private final ServiceDefinition serviceDefinition;
043            private final Endpoint endpoint;
044            private final ServiceEndpoint serviceEndpoint;
045            
046            LocalService(String instanceId, ServiceDefinition serviceDefinition) {
047                    this(instanceId, serviceDefinition, null);
048            }
049            
050            LocalService(String instanceId, ServiceDefinition serviceDefinition, ServiceEndpoint serviceEndpoint) {
051                    if (StringUtils.isBlank(instanceId)) {
052                            throw new IllegalArgumentException("instanceId was blank or null");
053                    }
054                    if (serviceDefinition == null) {
055                            throw new IllegalArgumentException("serviceDefinition was null");
056                    }
057                    this.serviceDefinition = serviceDefinition;
058                    this.endpoint = serviceDefinition.establishEndpoint();
059                    if (serviceEndpoint != null) {
060                            this.serviceEndpoint = serviceEndpoint;
061                    } else {
062                            this.serviceEndpoint = constructServiceEndpoint(instanceId, this.endpoint);
063                    }
064            }
065            
066            LocalService(LocalService currentLocalService, ServiceEndpoint newServiceEndpoint) {
067                    this(newServiceEndpoint.getInfo().getInstanceId(), currentLocalService.getServiceDefinition(), newServiceEndpoint);
068            }
069            
070            public QName getServiceName() {
071                    return endpoint.getServiceConfiguration().getServiceName();
072            }
073                    
074            public ServiceDefinition getServiceDefinition() {
075                    return serviceDefinition;
076            }
077            
078            public Endpoint getEndpoint() {
079                    return endpoint;
080            }
081            
082            public ServiceEndpoint getServiceEndpoint() {
083                    return this.serviceEndpoint;
084            }
085                    
086            static ServiceEndpoint constructServiceEndpoint(String instanceId, Endpoint endpoint) {
087                    ServiceInfo.Builder serviceInfo = constructServiceInfo(instanceId, endpoint.getServiceConfiguration());
088                    ServiceDescriptor.Builder serviceDescriptor = constructDescriptor(endpoint.getServiceConfiguration());
089                    ServiceEndpoint.Builder builder = ServiceEndpoint.Builder.create(serviceInfo, serviceDescriptor);
090                    return builder.build();
091            }
092            
093            static ServiceInfo.Builder constructServiceInfo(String instanceId, ServiceConfiguration serviceConfiguration) {
094                    ServiceInfo.Builder builder = ServiceInfo.Builder.create();
095                    builder.setInstanceId(instanceId);
096                    builder.setApplicationId(serviceConfiguration.getApplicationId());
097                    builder.setChecksum(ChecksumUtils.calculateChecksum(serviceConfiguration));
098                    builder.setEndpointUrl(serviceConfiguration.getEndpointUrl().toExternalForm());
099                    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    }