in core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java [163:211]
private void handleRefsUpdateOnRegister(MockServiceRegistration<?> registration) {
// handle DYNAMIC references to this registration
List<ReferenceInfo<?>> affectedDynamicReferences = OsgiServiceUtil.getMatchingDynamicReferences(registeredServices, registration);
for (ReferenceInfo<?> referenceInfo : affectedDynamicReferences) {
Reference reference = referenceInfo.getReference();
// Look for a target override
Object o = referenceInfo.getServiceRegistration().getProperties().get(reference.getName() + ComponentConstants.REFERENCE_TARGET_SUFFIX);
if (o instanceof String) {
reference = new DynamicReference(reference,(String)o);
}
if (reference.matchesTargetFilter(registration.getReference())) {
switch (reference.getCardinality()) {
case MANDATORY_UNARY:
// nothing to do - reference is already set
break;
case MANDATORY_MULTIPLE:
case OPTIONAL_MULTIPLE:
case OPTIONAL_UNARY:
OsgiServiceUtil.invokeBindMethod(reference, referenceInfo.getServiceRegistration().getService(), new ServiceInfo(registration));
break;
default:
throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
}
}
}
// handle STATIC+GREEDY references to this registration
List<ReferenceInfo<?>> affectedStaticGreedyReferences = OsgiServiceUtil.getMatchingStaticGreedyReferences(registeredServices, registration);
Set<MockServiceRegistration<?>> servicesToRestart = new HashSet<>();
for (ReferenceInfo<?> referenceInfo : affectedStaticGreedyReferences) {
Reference reference = referenceInfo.getReference();
if (reference.matchesTargetFilter(registration.getReference())) {
switch (reference.getCardinality()) {
case MANDATORY_UNARY:
// nothing to do - reference is already set
break;
case MANDATORY_MULTIPLE:
case OPTIONAL_MULTIPLE:
case OPTIONAL_UNARY:
servicesToRestart.add(referenceInfo.getServiceRegistration());
break;
default:
throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
}
}
}
servicesToRestart.forEach(this::restartService);
}