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 javax.xml.namespace.QName;
19  
20  import org.apache.commons.lang.StringUtils;
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.registry.ServiceDescriptor;
24  import org.kuali.rice.ksb.api.registry.ServiceInfo;
25  import org.kuali.rice.ksb.api.registry.ServiceRegistry;
26  
27  public final class RemoteService {
28  	
29  	private final ServiceInfo serviceInfo;
30  	private final ServiceRegistry serviceRegistry;
31  	
32  	private final Object endpointAcquisitionLock = new Object();
33  	private volatile Endpoint endpoint;
34  	
35  	public RemoteService(ServiceInfo serviceInfo, ServiceRegistry serviceRegistry) {
36  		validateServiceInfo(serviceInfo);
37  		if (serviceRegistry == null) {
38  			throw new IllegalArgumentException("serviceRegistry cannot be null");
39  		}
40  		this.serviceInfo = serviceInfo;
41  		this.serviceRegistry = serviceRegistry;
42  	}
43  	
44  	private static void validateServiceInfo(ServiceInfo serviceInfo) {
45  		if (serviceInfo == null) {
46  			throw new IllegalArgumentException("serviceInfo cannot be null");
47  		}
48  		if (serviceInfo.getServiceId() == null) {
49  			throw new IllegalArgumentException("serviceInfo must have a serviceId but was null");
50  		}
51  	}
52  	
53  	public QName getServiceName() {
54  		return serviceInfo.getServiceName();
55  	}
56  		
57  	public ServiceInfo getServiceInfo() {
58  		return serviceInfo;
59  	}
60  	
61  	public Endpoint getEndpoint() {
62  		// double-checked locking idiom - see Effective Java, Item 71
63  		Endpoint internalEndpoint = this.endpoint;
64  		if (internalEndpoint == null) {
65  			synchronized (endpointAcquisitionLock) {
66  				internalEndpoint = this.endpoint;
67  				if (internalEndpoint == null) {
68  					this.endpoint = internalEndpoint = new LazyEndpoint(constructServiceConfiguration()); 
69  				}
70  			}
71  		}
72  		return internalEndpoint;
73  	}
74  	
75  	protected ServiceConfiguration constructServiceConfiguration() {
76  		ServiceDescriptor serviceDescriptor = serviceRegistry.getServiceDescriptor(serviceInfo.getServiceDescriptorId());
77  		if (serviceDescriptor == null) {
78  			throw new IllegalStateException("Failed to locate ServiceDescriptor for ServiceInfo with serviceDescriptorId=" + serviceInfo.getServiceDescriptorId());
79  		} else if (StringUtils.isBlank(serviceDescriptor.getDescriptor())) {
80  			throw new IllegalStateException("ServiceDescriptor descriptor value is blank or null for descriptor with serviceEndpointId=" + serviceInfo.getServiceId());
81  		}
82  		return ServiceConfigurationSerializationHandler.unmarshallFromXml(serviceDescriptor.getDescriptor());
83  	}
84  	
85  	@Override
86      public boolean equals(Object obj) {
87  		if (!(obj instanceof RemoteService)) {
88  			return false;
89  		}
90  		return serviceInfo.equals(((RemoteService)obj).getServiceInfo());
91      }
92  
93  	@Override
94      public int hashCode() {
95          return serviceInfo.hashCode();
96      }
97  	
98  }