* @param sourceWire
* @throws BuilderConfigException
*/
private void connect(SCAObject source, OutboundWire sourceWire, SCAObject target) throws BuilderConfigException {
assert sourceWire.getTargetName() != null : "Wire target name was null";
QualifiedName targetName = sourceWire.getTargetName();
if (target instanceof AtomicComponent) {
AtomicComponent targetComponent = (AtomicComponent) target;
InboundWire targetWire = targetComponent.getInboundWire(targetName.getPortName());
if (targetWire == null) {
String refName = sourceWire.getReferenceName();
BuilderConfigException e = new BuilderConfigException("No target service for reference " + refName);
e.setIdentifier(targetName.getPortName());
throw e;
}
checkIfWireable(sourceWire, targetWire);
boolean optimizable = isOptimizable(source.getScope(), target.getScope());
connect(sourceWire, targetWire, optimizable);
} else if (target instanceof Reference) {
InboundWire targetWire = ((Reference) target).getInboundWire();
assert targetWire != null;
checkIfWireable(sourceWire, targetWire);
boolean optimizable = isOptimizable(source.getScope(), target.getScope());
connect(sourceWire, targetWire, optimizable);
} else if (target instanceof CompositeComponent) {
CompositeComponent composite = (CompositeComponent) target;
InboundWire targetWire = null;
if (source.isSystem()) {
for (Object child : composite.getSystemChildren()) {
if (child instanceof CompositeService) {
CompositeService compServ = (CompositeService) child;
targetWire = compServ.getInboundWire();
assert targetWire != null;
Class<?> sourceInterface = sourceWire.getServiceContract().getInterfaceClass();
Class<?> targetInterface = targetWire.getServiceContract().getInterfaceClass();
if (sourceInterface.isAssignableFrom(targetInterface)) {
target = compServ;
break;
} else {
targetWire = null;
}
}
}
} else {
for (Object child : composite.getChildren()) {
if (child instanceof CompositeService) {
CompositeService compServ = (CompositeService) child;
targetWire = compServ.getInboundWire();
assert targetWire != null;
Class<?> sourceInterface = sourceWire.getServiceContract().getInterfaceClass();
Class<?> targetInterface = targetWire.getServiceContract().getInterfaceClass();
if (sourceInterface.isAssignableFrom(targetInterface)) {
target = compServ;
break;
} else {
targetWire = null;
}
}
}
}
if (targetWire == null) {
throw new BuilderConfigException("No target composite service in composite");
}
boolean optimizable = isOptimizable(source.getScope(), target.getScope());
connect(sourceWire, targetWire, optimizable);
} else {
String name = sourceWire.getReferenceName();
BuilderConfigException e = new BuilderConfigException("Invalid target type for reference " + name);
e.setIdentifier(targetName.getQualifiedName());
throw e;
}
}