use of javax.enterprise.inject.spi.Bean in project camel by apache.
the class CdiCamelExtension method afterDeploymentValidation.
private void afterDeploymentValidation(@Observes AfterDeploymentValidation adv, BeanManager manager) {
// Send event for Camel CDI configuration
manager.fireEvent(configuration);
configuration.unmodifiable();
Collection<CamelContext> contexts = new ArrayList<>();
for (Bean<?> context : manager.getBeans(CamelContext.class, ANY)) {
contexts.add(getReference(manager, CamelContext.class, context));
}
// Add type converters to Camel contexts
CdiTypeConverterLoader loader = new CdiTypeConverterLoader();
for (Class<?> converter : converters) {
for (CamelContext context : contexts) {
loader.loadConverterMethods(context.getTypeConverterRegistry(), converter);
}
}
// Add routes to Camel contexts
if (configuration.autoConfigureRoutes()) {
boolean deploymentException = false;
Set<Bean<?>> routes = new HashSet<>(manager.getBeans(RoutesBuilder.class, ANY));
routes.addAll(manager.getBeans(RouteContainer.class, ANY));
for (Bean<?> context : manager.getBeans(CamelContext.class, ANY)) {
for (Bean<?> route : routes) {
Set<Annotation> qualifiers = new HashSet<>(context.getQualifiers());
qualifiers.retainAll(route.getQualifiers());
if (qualifiers.size() > 1) {
deploymentException |= !addRouteToContext(route, context, manager, adv);
}
}
}
// Let's return to avoid starting misconfigured contexts
if (deploymentException) {
return;
}
}
// Trigger eager beans instantiation (calling toString is necessary to force
// the initialization of normal-scoped beans).
// FIXME: This does not work with OpenWebBeans for bean whose bean type is an
// interface as the Object methods does not get forwarded to the bean instances!
eagerBeans.forEach(type -> getReferencesByType(manager, type.getJavaClass(), ANY).toString());
manager.getBeans(Object.class, ANY, STARTUP).forEach(bean -> getReference(manager, bean.getBeanClass(), bean).toString());
// Start Camel contexts
for (CamelContext context : contexts) {
if (ServiceStatus.Started.equals(context.getStatus())) {
continue;
}
logger.info("Camel CDI is starting Camel context [{}]", context.getName());
try {
context.start();
} catch (Exception exception) {
adv.addDeploymentProblem(exception);
}
}
// Clean-up
Stream.of(converters, camelBeans, eagerBeans, cdiBeans).forEach(Set::clear);
Stream.of(producerBeans, producerQualifiers).forEach(Map::clear);
}
use of javax.enterprise.inject.spi.Bean in project deltaspike by apache.
the class AuditEntityListener method update.
@PreUpdate
public void update(Object entity) {
BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
Set<Bean<?>> beans = beanManager.getBeans(PreUpdateAuditListener.class);
for (Bean<?> bean : beans) {
PreUpdateAuditListener result = (PreUpdateAuditListener) beanManager.getReference(bean, PreUpdateAuditListener.class, beanManager.createCreationalContext(bean));
result.preUpdate(entity);
}
}
use of javax.enterprise.inject.spi.Bean in project deltaspike by apache.
the class EntityManagerRefLookup method initGlobalEntityManager.
private synchronized void initGlobalEntityManager() {
// switch into paranoia mode
if (this.globalEntityManagerInitialized == null) {
this.globalEntityManagerInitialized = true;
BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
Set<Bean<?>> beans = beanManager.getBeans(EntityManager.class);
Bean<?> bean = beanManager.resolve(beans);
if (bean == null) {
throw new IllegalStateException("Could not find EntityManager with default qualifier.");
}
globalEntityManagerIsNormalScope = beanManager.isNormalScope(bean.getScope());
if (globalEntityManagerIsNormalScope) {
globalEntityManager = (EntityManager) beanManager.getReference(bean, EntityManager.class, beanManager.createCreationalContext(bean));
}
}
}
use of javax.enterprise.inject.spi.Bean in project deltaspike by apache.
the class PrincipalProvider method resolvePrincipal.
private Object resolvePrincipal(Object entity, Property<Object> property) {
CurrentUser principal = AnnotationInstanceProvider.of(CurrentUser.class);
Class<?> propertyClass = property.getJavaClass();
Set<Bean<?>> beans = manager.getBeans(propertyClass, principal);
if (!beans.isEmpty() && beans.size() == 1) {
Bean<?> bean = beans.iterator().next();
Object result = manager.getReference(bean, propertyClass, manager.createCreationalContext(bean));
return result;
}
throw new IllegalArgumentException("Principal " + (beans.isEmpty() ? "not found" : "not unique") + " for " + propertyName(entity, property));
}
use of javax.enterprise.inject.spi.Bean in project deltaspike by apache.
the class RequestServlet method getRequestScopedBean.
private RequestScopedBean getRequestScopedBean() {
BeanManager beanManager = CdiContainerLoader.getCdiContainer().getBeanManager();
if (beanManager == null) {
return null;
}
Set<Bean<?>> beans = beanManager.getBeans(RequestScopedBean.class);
Bean<RequestScopedBean> reqScpdBean = (Bean<RequestScopedBean>) beanManager.resolve(beans);
CreationalContext<RequestScopedBean> reqScpdCC = beanManager.createCreationalContext(reqScpdBean);
RequestScopedBean instance = (RequestScopedBean) beanManager.getReference(reqScpdBean, RequestScopedBean.class, reqScpdCC);
return instance;
}
Aggregations