| 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 | 19 | private final Object endpointAcquisitionLock = new Object(); |
| 18 | |
private volatile Endpoint endpoint; |
| 19 | |
|
| 20 | 19 | public RemoteService(ServiceInfo serviceInfo, ServiceRegistry serviceRegistry) { |
| 21 | 19 | validateServiceInfo(serviceInfo); |
| 22 | 19 | if (serviceRegistry == null) { |
| 23 | 0 | throw new IllegalArgumentException("serviceRegistry cannot be null"); |
| 24 | |
} |
| 25 | 19 | this.serviceInfo = serviceInfo; |
| 26 | 19 | this.serviceRegistry = serviceRegistry; |
| 27 | 19 | } |
| 28 | |
|
| 29 | |
private static void validateServiceInfo(ServiceInfo serviceInfo) { |
| 30 | 19 | if (serviceInfo == null) { |
| 31 | 0 | throw new IllegalArgumentException("serviceInfo cannot be null"); |
| 32 | |
} |
| 33 | 19 | if (serviceInfo.getServiceId() == null) { |
| 34 | 0 | throw new IllegalArgumentException("serviceInfo must have a serviceId but was null"); |
| 35 | |
} |
| 36 | 19 | } |
| 37 | |
|
| 38 | |
public QName getServiceName() { |
| 39 | 0 | return serviceInfo.getServiceName(); |
| 40 | |
} |
| 41 | |
|
| 42 | |
public ServiceInfo getServiceInfo() { |
| 43 | 44 | return serviceInfo; |
| 44 | |
} |
| 45 | |
|
| 46 | |
public Endpoint getEndpoint() { |
| 47 | |
|
| 48 | 0 | Endpoint internalEndpoint = this.endpoint; |
| 49 | 0 | if (internalEndpoint == null) { |
| 50 | 0 | synchronized (endpointAcquisitionLock) { |
| 51 | 0 | internalEndpoint = this.endpoint; |
| 52 | 0 | if (internalEndpoint == null) { |
| 53 | 0 | this.endpoint = internalEndpoint = new LazyEndpoint(constructServiceConfiguration()); |
| 54 | |
} |
| 55 | 0 | } |
| 56 | |
} |
| 57 | 0 | return internalEndpoint; |
| 58 | |
} |
| 59 | |
|
| 60 | |
protected ServiceConfiguration constructServiceConfiguration() { |
| 61 | 0 | ServiceDescriptor serviceDescriptor = serviceRegistry.getServiceDescriptor(serviceInfo.getServiceDescriptorId()); |
| 62 | 0 | if (serviceDescriptor == null) { |
| 63 | 0 | throw new IllegalStateException("Failed to locate ServiceDescriptor for ServiceInfo with serviceDescriptorId=" + serviceInfo.getServiceDescriptorId()); |
| 64 | 0 | } else if (StringUtils.isBlank(serviceDescriptor.getDescriptor())) { |
| 65 | 0 | throw new IllegalStateException("ServiceDescriptor descriptor value is blank or null for descriptor with serviceEndpointId=" + serviceInfo.getServiceId()); |
| 66 | |
} |
| 67 | 0 | return ServiceConfigurationSerializationHandler.unmarshallFromXml(serviceDescriptor.getDescriptor()); |
| 68 | |
} |
| 69 | |
|
| 70 | |
@Override |
| 71 | |
public boolean equals(Object obj) { |
| 72 | 0 | if (!(obj instanceof RemoteService)) { |
| 73 | 0 | return false; |
| 74 | |
} |
| 75 | 0 | return serviceInfo.equals(((RemoteService)obj).getServiceInfo()); |
| 76 | |
} |
| 77 | |
|
| 78 | |
@Override |
| 79 | |
public int hashCode() { |
| 80 | 0 | return serviceInfo.hashCode(); |
| 81 | |
} |
| 82 | |
|
| 83 | |
} |