in cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/model/SingleActivator.java [95:220]
public boolean open() {
try (Syncro synchro = _lock.open()) {
if (containerState.bundleContext() == null) {
// this bundle was already removed
return false;
}
if (!_instance.referencesResolved() || _instance.active) {
return false;
}
final BeanManager beanManager = containerState.beanManager();
if (beanManager == null) {
return false;
}
ExtendedActivationTemplateDTO activationTemplate =
(ExtendedActivationTemplateDTO)_instance.template.activations.get(0);
_instance.template.references.stream().map(ExtendedReferenceTemplateDTO.class::cast).forEach(
t -> {
_instance.references.stream().filter(
r -> r.template == t
).findFirst().map(
ExtendedReferenceDTO.class::cast
).ifPresent(
r -> {
ReferenceBean bean = t.bean;
bean.setBeanManager(beanManager);
bean.setReferenceDTO(r);
}
);
}
);
ExtendedComponentTemplateDTO extended = (ExtendedComponentTemplateDTO)_instance.template;
Set<Bean<?>> beans = beanManager.getBeans(
extended.beanClass, extended.qualifiers.toArray(new Annotation[0]));
Bean<? extends Object> bean = beanManager.resolve(beans);
if (activationTemplate.serviceClasses.isEmpty() /* immediate */) {
activate(bean, activationTemplate, beanManager);
_log.debug(l -> l.debug("CCR `immediate component` {} activated on {}", _instance.ident(), bundle()));
}
else if (activationTemplate.scope == ServiceScope.SINGLETON) {
Entry<ExtendedActivationDTO, Object> entry = activate(
bean, activationTemplate, beanManager);
serviceRegistration = containerState.bundleContext().registerService(
activationTemplate.serviceClasses.toArray(new String[0]),
entry.getValue(),
Maps.dict(_instance.properties));
entry.getKey().service = SRs.from(serviceRegistration.getReference());
_log.debug(l -> l.debug("CCR `singleton scope service` {} activated on {}", _instance.ident(), bundle()));
}
else if (activationTemplate.scope == ServiceScope.BUNDLE) {
serviceRegistration = containerState.bundleContext().registerService(
activationTemplate.serviceClasses.toArray(new String[0]),
new ServiceFactory() {
@Override
public Object getService(Bundle bundle, ServiceRegistration registration) {
Entry<ExtendedActivationDTO, Object> entry = activate(
bean, activationTemplate, beanManager);
entry.getKey().service = SRs.from(registration.getReference());
_locals.put(entry.getValue(), entry.getKey());
return entry.getValue();
}
@Override
public void ungetService(Bundle bundle, ServiceRegistration registration, Object object) {
ExtendedActivationDTO activationDTO = _locals.remove(object);
if (activationDTO != null) {
activationDTO.onClose.accept(activationDTO);
}
}
final Map<Object, ExtendedActivationDTO> _locals = new ConcurrentHashMap<>();
},
Maps.dict(_instance.properties)
);
_log.debug(l -> l.debug("CCR `bundle scope service` {} activated on {}", _instance.ident(), bundle()));
}
else if (activationTemplate.scope == ServiceScope.PROTOTYPE) {
serviceRegistration = containerState.bundleContext().registerService(
activationTemplate.serviceClasses.toArray(new String[0]),
new PrototypeServiceFactory() {
@Override
public Object getService(Bundle bundle, ServiceRegistration registration) {
Entry<ExtendedActivationDTO, Object> entry = activate(
bean, activationTemplate, beanManager);
entry.getKey().service = SRs.from(registration.getReference());
_locals.put(entry.getValue(), entry.getKey());
return entry.getValue();
}
@Override
public void ungetService(Bundle bundle, ServiceRegistration registration, Object object) {
ExtendedActivationDTO activationDTO = _locals.remove(object);
if (activationDTO != null) {
activationDTO.onClose.accept(activationDTO);
}
}
final Map<Object, ExtendedActivationDTO> _locals = new ConcurrentHashMap<>();
},
Maps.dict(_instance.properties)
);
_log.debug(l -> l.debug("CCR `prototype scope service` {} activated on {}", _instance.ident(), bundle()));
}
_instance.active = true;
return true;
}
}