TUSCANY-4027 - fix up SCA Client target resolution when only component name is provided. Thanks to the patch Greg.

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1299664 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
slaws 2012-03-12 13:12:37 +00:00
parent bce7b55e66
commit dfcc3369d3
2 changed files with 19 additions and 12 deletions

View file

@ -21,10 +21,12 @@ package org.apache.tuscany.sca.client.impl;
import java.util.List;
import org.apache.tuscany.sca.assembly.ComponentService;
import org.apache.tuscany.sca.assembly.Endpoint;
import org.apache.tuscany.sca.assembly.SCABinding;
import org.apache.tuscany.sca.runtime.DomainRegistry;
import org.oasisopen.sca.NoSuchServiceException;
import org.oasisopen.sca.ServiceRuntimeException;
public class DefaultEndpointFinder implements EndpointFinder {
@ -36,7 +38,18 @@ public class DefaultEndpointFinder implements EndpointFinder {
if (eps == null || eps.size() < 1) {
throw new NoSuchServiceException(serviceName);
}
// If lookup is by component name only and there are multiple matches, verify all matches
// are from the same service. Otherwise it is ambiguous which service the client wants.
if (serviceName.indexOf('/') == -1 && eps.size() > 1) {
ComponentService firstService = eps.get(0).getService();
for (int i=1; i<eps.size(); i++) {
if (firstService != eps.get(i).getService())
throw new ServiceRuntimeException("More than one service is declared on component " + serviceName
+ ". Service name is required to get the service.");
}
}
// If there is an Endpoint using the SCA binding use that
for (Endpoint ep : eps) {
if (SCABinding.TYPE.equals(ep.getBinding().getType())) {
@ -47,8 +60,9 @@ public class DefaultEndpointFinder implements EndpointFinder {
if (onlySCABinding) {
throw new NoSuchServiceException(serviceName + " not found using binding.sca");
}
// Otherwise just choose the first one
// There either is a single matching endpoint, or there are multiple endpoints (bindings)
// under a single service. Just choose the first one
return eps.get(0);
}
}

View file

@ -82,19 +82,12 @@ public class SCAClientFactoryImpl extends SCAClientFactory {
@Override
public <T> T getService(Class<T> serviceInterface, String serviceURI) throws NoSuchServiceException{
String serviceName = null;
if (serviceURI.contains("/")) {
int i = serviceURI.indexOf("/");
if (i < serviceURI.length() - 1) {
serviceName = serviceURI.substring(i + 1);
}
}
// The service is a component in a local runtime
if (!remoteClient) {
Endpoint ep = endpointFinder.findEndpoint(domainRegistry, serviceURI);
if (((RuntimeComponent)ep.getComponent()).getComponentContext() != null) {
String serviceName = ep.getService().getName() + '/' + ep.getBinding().getName();
return ((RuntimeComponent)ep.getComponent()).getServiceReference(serviceInterface, serviceName).getService();
}
}