for (ComponentService targetService : reference.getTargets()){
String targetName = targetService.getName();
String componentName = getComponentNameFromReference(targetName);
String serviceName = getServiceNameFromReference(targetName);
Service service = null;
Component serviceComponent = null;
// find the real target service in the domain
for(Composite tmpComposite : domainLevelComposite.getIncludes()) {
for (Component tmpComponent: tmpComposite.getComponents()) {
if (tmpComponent.getName().equals(componentName)){
serviceComponent = tmpComponent;
if (tmpComponent.getServices().size() > 1) {
for (Service tmpService: tmpComponent.getServices()) {
if (tmpService.getName().equals(serviceName)){
service = tmpService;
break;
}
}
} else if (tmpComponent.getServices().size() == 1) {
service = tmpComponent.getServices().get(0);
break;
}
}
}
}
if ( targetService.isUnresolved()){
if (service != null){
// Find the binding already in use for this target
Binding binding = null;
for (Binding tmpBinding : reference.getBindings()){
if ((tmpBinding.getName() != null) &&
(tmpBinding.getName().startsWith(reference.getName() + "#" + targetName))){
binding = tmpBinding;
}
}
// Resolve the binding that should be used for this target
// TODO - hang onto the old bindings at the domain level, i.e.
// don't rely on the target objects as we still need the
// bindings if we are going to do autowiring
List<Binding> source = targetService.getBindings();
List<Binding> target = service.getBindings();
Binding newBinding = BindingUtil.matchBinding(serviceComponent, (ComponentService)service, source, target);
// update the existing binding to the new binding if required
if (newBinding != null) {
if (binding != null){
// there is a binding already so see if the URI has changed
if ((binding.getURI() == null) ||
(!binding.getURI().equals(newBinding.getURI()))){
binding.setURI(newBinding.getURI());
compositeChanged = true;
}
} else {
// this is a newly configured binding so add it
reference.getBindings().add(newBinding);
compositeChanged = true;
}
}
} else {
// Do nothing - the target service hasn't been contributed yet
}
} else {
// find the reference binding with the right name
for (Binding refBinding : reference.getBindings()){
if ((refBinding.getName() != null) &&
(refBinding.getName().startsWith(reference.getName() + "#" + targetName))){
// find the matching service binding
for (Binding serviceBinding : service.getBindings()){
if (refBinding.getClass() == serviceBinding.getClass()){
refBinding.setURI(serviceBinding.getURI());
}
}
}