use of javax.enterprise.inject.spi.BeanManager in project Payara by payara.
the class TransactionScopedCDIUtil method fireEvent.
public static void fireEvent(String eventType) {
BeanManager beanManager = null;
try {
beanManager = CDI.current().getBeanManager();
} catch (Exception e) {
log("Can't get instance of BeanManager to process TransactionScoped CDI Event!");
}
if (beanManager != null) {
// TransactionScopedCDIEventHelperImpl AnnotatedType is created in Extension
Set<Bean<?>> availableBeans = beanManager.getBeans(TransactionScopedCDIEventHelperImpl.class);
if (null != availableBeans && !availableBeans.isEmpty()) {
Bean<?> bean = beanManager.resolve(availableBeans);
TransactionScopedCDIEventHelper eventHelper = (TransactionScopedCDIEventHelper) beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(null));
if (eventType.equalsIgnoreCase(INITIALIZED_EVENT)) {
eventHelper.fireInitializedEvent(new TransactionScopedCDIEventPayload());
} else {
eventHelper.fireDestroyedEvent(new TransactionScopedCDIEventPayload());
}
}
} else {
log("Can't get instance of BeanManager to process TransactionScoped CDI Event!");
}
}
use of javax.enterprise.inject.spi.BeanManager in project Payara by payara.
the class TransactionalInterceptorBase method getTransactionalAnnotation.
private Transactional getTransactionalAnnotation(InvocationContext invocationContext) {
Optional<Transactional> optionalTransactional;
// Try the Weld bindings first. This gives us the *exact* binding which caused this interceptor being called
@SuppressWarnings("unchecked") Set<Annotation> bindings = (Set<Annotation>) invocationContext.getContextData().get("org.jboss.weld.interceptor.bindings");
if (bindings != null) {
optionalTransactional = bindings.stream().filter(annotation -> annotation.annotationType().equals(Transactional.class)).findAny().map(annotation -> Transactional.class.cast(annotation));
if (optionalTransactional.isPresent()) {
return optionalTransactional.get();
}
}
BeanManager beanManager = CDI.current().getBeanManager();
// Failing the Weld binding, check the method first
optionalTransactional = getAnnotation(beanManager, invocationContext.getMethod(), Transactional.class);
if (optionalTransactional.isPresent()) {
return optionalTransactional.get();
}
// If nothing found on the method, check the the bean class
optionalTransactional = getAnnotation(beanManager, invocationContext.getTarget().getClass(), Transactional.class);
if (optionalTransactional.isPresent()) {
return optionalTransactional.get();
}
// find it signals a critical error.
throw new IllegalStateException("@Transactional not found on " + invocationContext.getTarget().getClass());
}
use of javax.enterprise.inject.spi.BeanManager in project Payara by payara.
the class ValidationNamingProxy method obtainBeanManager.
/**
* Obtain the BeanManagerNamingProxy from hk2, so the BeanManager can be looked up
*
* @throws NamingException
*/
private synchronized BeanManager obtainBeanManager() throws NamingException {
BeanManager beanManager = null;
// Use invocation context to find applicable BeanDeploymentArchive.
ComponentInvocation inv = invocationManager.getCurrentInvocation();
if (inv != null) {
JndiNameEnvironment componentEnv = compEnvManager.getJndiNameEnvironment(inv.getComponentId());
if (componentEnv != null) {
BundleDescriptor bundle = null;
if (componentEnv instanceof EjbDescriptor) {
bundle = (BundleDescriptor) ((EjbDescriptor) componentEnv).getEjbBundleDescriptor().getModuleDescriptor().getDescriptor();
} else if (componentEnv instanceof WebBundleDescriptor) {
bundle = (BundleDescriptor) componentEnv;
}
if (bundle != null) {
BeanDeploymentArchive bda = weldDeployer.getBeanDeploymentArchiveForBundle(bundle);
if (bda != null) {
WeldBootstrap bootstrap = weldDeployer.getBootstrapForApp(bundle.getApplication());
beanManager = bootstrap.getManager(bda);
}
}
}
}
return beanManager;
}
use of javax.enterprise.inject.spi.BeanManager in project Payara by payara.
the class ValidationNamingProxy method handle.
@Override
public Object handle(String name) throws NamingException {
// delegate to the java:comp/BeanManager handler to obtain the appropriate BeanManager
BeanManager beanManager = obtainBeanManager();
if (beanManager == null) {
// There is no bean manager available, return and let BeanValidatorNamingProxy handle lookup..
return null;
}
if (VALIDATOR_FACTORY_CONTEXT.equals(name)) {
try {
ValidatorFactory validatorFactory = (ValidatorFactory) getAndCreateBean(beanManager, ValidatorFactory.class);
if (validatorFactory != null) {
return validatorFactory;
} else {
throw new NamingException("Error retrieving " + name);
}
} catch (Throwable t) {
NamingException ne = new NamingException("Error retrieving " + name);
ne.initCause(t);
throw ne;
}
} else if (VALIDATOR_CONTEXT.equals(name)) {
try {
Validator validator = (Validator) getAndCreateBean(beanManager, Validator.class);
if (validator != null) {
return validator;
} else {
throw new NamingException("Error retrieving " + name);
}
} catch (Throwable t) {
NamingException ne = new NamingException("Error retrieving " + name);
ne.initCause(t);
throw ne;
}
} else {
throw new NamingException("wrong handler for " + name);
}
}
use of javax.enterprise.inject.spi.BeanManager in project tomee by apache.
the class HashServlet method service.
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
final BeanManager bm = HashCdiExtension.BMS.get(tccl);
// todo: do some lookup
resp.getWriter().write(Boolean.toString(bm != null));
}
Aggregations