Use binding name to look up services

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@882371 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
rfeng 2009-11-20 00:51:09 +00:00
commit 765e058d13
2 changed files with 56 additions and 48 deletions

View file

@ -86,7 +86,7 @@ public class JSONRPCServiceBindingProvider implements ServiceBindingProvider {
Class<?> serviceInterface = getTargetJavaClass(serviceContract.getInterface()); Class<?> serviceInterface = getTargetJavaClass(serviceContract.getInterface());
// Create a Java proxy to the target service // Create a Java proxy to the target service
Object proxy = component.getComponentContext().createSelfReference(serviceInterface, service).getService(); Object proxy = component.getComponentContext().getServiceReference(serviceInterface, endpoint).getService();
// Create and register a Servlet for this service // Create and register a Servlet for this service
JSONRPCServiceServlet serviceServlet = JSONRPCServiceServlet serviceServlet =

View file

@ -23,10 +23,12 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import org.apache.tuscany.sca.assembly.AssemblyFactory; import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.assembly.Binding;
import org.apache.tuscany.sca.assembly.Component; import org.apache.tuscany.sca.assembly.Component;
import org.apache.tuscany.sca.assembly.ComponentProperty; import org.apache.tuscany.sca.assembly.ComponentProperty;
import org.apache.tuscany.sca.assembly.ComponentReference; import org.apache.tuscany.sca.assembly.ComponentReference;
import org.apache.tuscany.sca.assembly.ComponentService; import org.apache.tuscany.sca.assembly.ComponentService;
import org.apache.tuscany.sca.assembly.CompositeService;
import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.assembly.Endpoint;
import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.assembly.EndpointReference;
import org.apache.tuscany.sca.assembly.Multiplicity; import org.apache.tuscany.sca.assembly.Multiplicity;
@ -148,28 +150,36 @@ public class ComponentContextImpl implements RuntimeComponentContext {
return eprs.get(0); return eprs.get(0);
} }
} }
/** /**
* Select an endpoint reference from the component reference * Select an endpoint reference from the component reference
* @param ref * @param ref
* @return * @return
*/ */
private Endpoint getEndpoint(ComponentService service) { private Endpoint getEndpoint(ComponentService service, String bindingName) {
List<Endpoint> eps = service.getEndpoints(); if (bindingName == null) {
if (eps.size() == 1) { // The default binding name is the name of the promoted service
// Return 1st one bindingName = getPromotedService(service).getName();
return eps.get(0);
} else {
for (Endpoint ep : eps) {
// Try to see if there is an EPR using binding.sca
if (ep.getBinding().getType().equals(SCABinding.TYPE)) {
return ep;
}
}
return eps.get(0);
} }
List<Endpoint> eps = service.getEndpoints();
for (Endpoint ep : eps) {
Binding binding = ep.getBinding();
if (bindingName.equals(binding.getName()) || binding.getName() == null) {
return ep;
}
}
return null;
} }
private ComponentService getPromotedService(ComponentService componentService) {
Service service = componentService.getService();
if (service instanceof CompositeService) {
return getPromotedService(((CompositeService)service).getPromotedService());
} else {
return componentService;
}
}
/** /**
* Gets the value for the specified property with the specified type. * Gets the value for the specified property with the specified type.
@ -229,10 +239,23 @@ public class ComponentContextImpl implements RuntimeComponentContext {
} }
public <B> ServiceReference<B> createSelfReference(Class<B> businessInterface, String serviceName) { public <B> ServiceReference<B> createSelfReference(Class<B> businessInterface, String serviceName) {
if (serviceName == null) {
return createSelfReference(businessInterface);
}
try { try {
String bindingName = null;
int index = serviceName.indexOf('/');
if (index != -1) {
serviceName = serviceName.substring(0, index);
bindingName = serviceName.substring(index + 1);
}
for (ComponentService service : component.getServices()) { for (ComponentService service : component.getServices()) {
if (serviceName.equals(service.getName())) { if (serviceName.equals(service.getName())) {
return createSelfReference(businessInterface, service); Endpoint endpoint = getEndpoint(service, bindingName);
if (endpoint == null) {
break;
}
return getServiceReference(businessInterface, (RuntimeEndpoint)endpoint);
} }
} }
throw new ServiceRuntimeException("Service not found: " + serviceName); throw new ServiceRuntimeException("Service not found: " + serviceName);
@ -252,7 +275,7 @@ public class ComponentContextImpl implements RuntimeComponentContext {
public <B> ServiceReference<B> createSelfReference(Class<B> businessInterface, ComponentService service) { public <B> ServiceReference<B> createSelfReference(Class<B> businessInterface, ComponentService service) {
try { try {
RuntimeEndpointReference ref = RuntimeEndpointReference ref =
(RuntimeEndpointReference)createSelfReference(component, service, businessInterface); (RuntimeEndpointReference)createEndpointReference(component, service, null, businessInterface);
ref.setComponent(component); ref.setComponent(component);
return getServiceReference(businessInterface, ref); return getServiceReference(businessInterface, ref);
} catch (Exception e) { } catch (Exception e) {
@ -294,22 +317,21 @@ public class ComponentContextImpl implements RuntimeComponentContext {
} }
} }
ref.setComponent(component); ref.setComponent(component);
return new ServiceReferenceImpl<B>(businessInterface, endpointReference, component return new ServiceReferenceImpl<B>(businessInterface, endpointReference, component.getComponentContext()
.getComponentContext().getCompositeContext()); .getCompositeContext());
} catch (Exception e) { } catch (Exception e) {
throw new ServiceRuntimeException(e); throw new ServiceRuntimeException(e);
} }
} }
public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface, RuntimeEndpoint endpoint) {
RuntimeEndpoint endpoint) {
try { try {
if (businessInterface == null) { if (businessInterface == null) {
InterfaceContract contract = endpoint.getComponentTypeServiceInterfaceContract(); InterfaceContract contract = endpoint.getComponentTypeServiceInterfaceContract();
businessInterface = (Class<B>)((JavaInterface)contract.getInterface()).getJavaClass(); businessInterface = (Class<B>)((JavaInterface)contract.getInterface()).getJavaClass();
} }
RuntimeEndpointReference ref = RuntimeEndpointReference ref =
(RuntimeEndpointReference)createSelfReference(component, endpoint.getService(), businessInterface); (RuntimeEndpointReference)createEndpointReference(endpoint, businessInterface);
ref.setComponent(component); ref.setComponent(component);
return new ServiceReferenceImpl<B>(businessInterface, ref, compositeContext); return new ServiceReferenceImpl<B>(businessInterface, ref, compositeContext);
} catch (Exception e) { } catch (Exception e) {
@ -324,29 +346,23 @@ public class ComponentContextImpl implements RuntimeComponentContext {
* @throws CloneNotSupportedException * @throws CloneNotSupportedException
* @throws InvalidInterfaceException * @throws InvalidInterfaceException
*/ */
private EndpointReference createSelfReference(Component component, private EndpointReference createEndpointReference(Component component,
ComponentService service, ComponentService service,
Class<?> businessInterface) throws CloneNotSupportedException, String bindingName,
Class<?> businessInterface) throws CloneNotSupportedException,
InvalidInterfaceException { InvalidInterfaceException {
Endpoint endpoint = getEndpoint(service, bindingName);
return createEndpointReference(endpoint, businessInterface);
}
private EndpointReference createEndpointReference(Endpoint endpoint, Class<?> businessInterface)
throws CloneNotSupportedException, InvalidInterfaceException {
Component component = endpoint.getComponent();
ComponentService service = endpoint.getService();
ComponentReference componentReference = assemblyFactory.createComponentReference(); ComponentReference componentReference = assemblyFactory.createComponentReference();
componentReference.setName("$self$." + service.getName()); componentReference.setName("$self$." + service.getName());
Endpoint endpoint = getEndpoint(service);
/*
for (Binding binding : service.getBindings()) {
if (binding instanceof OptimizableBinding) {
OptimizableBinding optimizableBinding = (OptimizableBinding)((OptimizableBinding)binding).clone();
optimizableBinding.setTargetBinding(binding);
optimizableBinding.setTargetComponent(component);
optimizableBinding.setTargetComponentService(service);
componentReference.getBindings().add(optimizableBinding);
} else {
componentReference.getBindings().add(binding);
}
}
*/
componentReference.setCallback(service.getCallback()); componentReference.setCallback(service.getCallback());
componentReference.getTargets().add(service); componentReference.getTargets().add(service);
componentReference.getPolicySets().addAll(service.getPolicySets()); componentReference.getPolicySets().addAll(service.getPolicySets());
@ -375,16 +391,8 @@ public class ComponentContextImpl implements RuntimeComponentContext {
componentReference.getEndpointReferences().add(endpointReference); componentReference.getEndpointReferences().add(endpointReference);
((RuntimeComponentReference)componentReference).setComponent((RuntimeComponent)component); ((RuntimeComponentReference)componentReference).setComponent((RuntimeComponent)component);
((RuntimeEndpointReference) endpointReference).bind(compositeContext); ((RuntimeEndpointReference)endpointReference).bind(compositeContext);
/*
// do binding matching
boolean ok = eprBinder.bind(compositeContext.getEndpointRegistry(), endpointReference);
if (!ok) {
throw new SCARuntimeException("Unable to bind " + endpointReference);
}
*/
return endpointReference; return endpointReference;
} }