View Javadoc

1   package org.kuali.rice.ksb.impl.bus;
2   
3   import javax.xml.namespace.QName;
4   
5   import org.apache.commons.lang.StringUtils;
6   import org.kuali.rice.ksb.api.bus.Endpoint;
7   import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
8   import org.kuali.rice.ksb.api.registry.ServiceDescriptor;
9   import org.kuali.rice.ksb.api.registry.ServiceInfo;
10  import org.kuali.rice.ksb.api.registry.ServiceRegistry;
11  
12  public final class RemoteService {
13  	
14  	private final ServiceInfo serviceInfo;
15  	private final ServiceRegistry serviceRegistry;
16  	
17  	private final Object endpointAcquisitionLock = new Object();
18  	private volatile Endpoint endpoint;
19  	
20  	public RemoteService(ServiceInfo serviceInfo, ServiceRegistry serviceRegistry) {
21  		validateServiceInfo(serviceInfo);
22  		if (serviceRegistry == null) {
23  			throw new IllegalArgumentException("serviceRegistry cannot be null");
24  		}
25  		this.serviceInfo = serviceInfo;
26  		this.serviceRegistry = serviceRegistry;
27  	}
28  	
29  	private static void validateServiceInfo(ServiceInfo serviceInfo) {
30  		if (serviceInfo == null) {
31  			throw new IllegalArgumentException("serviceInfo cannot be null");
32  		}
33  		if (serviceInfo.getServiceId() == null) {
34  			throw new IllegalArgumentException("serviceInfo must have a serviceId but was null");
35  		}
36  	}
37  	
38  	public QName getServiceName() {
39  		return serviceInfo.getServiceName();
40  	}
41  		
42  	public ServiceInfo getServiceInfo() {
43  		return serviceInfo;
44  	}
45  	
46  	public Endpoint getEndpoint() {
47  		// double-checked locking idiom - see Effective Java, Item 71
48  		Endpoint internalEndpoint = this.endpoint;
49  		if (internalEndpoint == null) {
50  			synchronized (endpointAcquisitionLock) {
51  				internalEndpoint = this.endpoint;
52  				if (internalEndpoint == null) {
53  					this.endpoint = internalEndpoint = new LazyEndpoint(constructServiceConfiguration()); 
54  				}
55  			}
56  		}
57  		return internalEndpoint;
58  	}
59  	
60  	protected ServiceConfiguration constructServiceConfiguration() {
61  		ServiceDescriptor serviceDescriptor = serviceRegistry.getServiceDescriptor(serviceInfo.getServiceDescriptorId());
62  		if (serviceDescriptor == null) {
63  			throw new IllegalStateException("Failed to locate ServiceDescriptor for ServiceInfo with serviceDescriptorId=" + serviceInfo.getServiceDescriptorId());
64  		} else if (StringUtils.isBlank(serviceDescriptor.getDescriptor())) {
65  			throw new IllegalStateException("ServiceDescriptor descriptor value is blank or null for descriptor with serviceEndpointId=" + serviceInfo.getServiceId());
66  		}
67  		return ServiceConfigurationSerializationHandler.unmarshallFromXml(serviceDescriptor.getDescriptor());
68  	}
69  	
70  	@Override
71      public boolean equals(Object obj) {
72  		if (!(obj instanceof RemoteService)) {
73  			return false;
74  		}
75  		return serviceInfo.equals(((RemoteService)obj).getServiceInfo());
76      }
77  
78  	@Override
79      public int hashCode() {
80          return serviceInfo.hashCode();
81      }
82  	
83  }