use of javax.enterprise.inject.spi.BeanManager in project meecrowave by apache.
the class Meecrowave method inject.
public <T> AutoCloseable inject(final T instance) {
final BeanManager bm = CDI.current().getBeanManager();
final AnnotatedType<?> annotatedType = bm.createAnnotatedType(instance.getClass());
final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
final CreationalContext<Object> creationalContext = bm.createCreationalContext(null);
injectionTarget.inject(instance, creationalContext);
return creationalContext::release;
}
use of javax.enterprise.inject.spi.BeanManager in project meecrowave by apache.
the class CDIInstanceManager method newInstance.
@Override
public void newInstance(final Object o) throws IllegalAccessException, InvocationTargetException, NamingException {
if (WebBeansConfigurationListener.class.isInstance(o) || o.getClass().getName().startsWith("org.apache.catalina.servlets.")) {
return;
}
final BeanManager bm = CDI.current().getBeanManager();
final AnnotatedType<?> annotatedType = bm.createAnnotatedType(o.getClass());
final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
final CreationalContext<Object> creationalContext = bm.createCreationalContext(null);
injectionTarget.inject(o, creationalContext);
try {
injectionTarget.postConstruct(o);
} catch (final RuntimeException e) {
creationalContext.release();
throw e;
}
destroyables.put(o, () -> {
try {
injectionTarget.preDestroy(o);
} finally {
creationalContext.release();
}
});
}
use of javax.enterprise.inject.spi.BeanManager in project Payara by payara.
the class HealthCheckServletContainerInitializer method onStartup.
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
// Check if this context is the root one ("/")
if (ctx.getContextPath().isEmpty()) {
// Check if there is already a servlet for healthcheck
Map<String, ? extends ServletRegistration> registrations = ctx.getServletRegistrations();
for (ServletRegistration reg : registrations.values()) {
if (reg.getClass().equals(HealthCheckServlet.class)) {
return;
}
}
// Register servlet
ServletRegistration.Dynamic reg = ctx.addServlet("microprofile-healthcheck-servlet", HealthCheckServlet.class);
reg.addMapping("/health");
}
// Get the BeanManager
BeanManager beanManager = null;
try {
beanManager = CDI.current().getBeanManager();
} catch (Exception ex) {
Logger.getLogger(HealthCheckServletContainerInitializer.class.getName()).log(Level.FINE, "Exception getting BeanManager; this probably isn't a CDI application. " + "No HealthChecks will be registered", ex);
}
// Check for any Beans annotated with @Health
if (beanManager != null) {
HealthCheckService healthCheckService = Globals.getDefaultBaseServiceLocator().getService(HealthCheckService.class);
InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
// For each bean annotated with @Health and implementing the HealthCheck interface,
// register it to the HealthCheckService along with the application name
Set<Bean<?>> beans = beanManager.getBeans(HealthCheck.class, new AnnotationLiteral<Health>() {
});
for (Bean<?> bean : beans) {
HealthCheck healthCheck = (HealthCheck) beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(bean));
healthCheckService.registerHealthCheck(invocationManager.getCurrentInvocation().getAppName(), healthCheck);
healthCheckService.registerClassLoader(invocationManager.getCurrentInvocation().getAppName(), healthCheck.getClass().getClassLoader());
Logger.getLogger(HealthCheckServletContainerInitializer.class.getName()).log(Level.INFO, "Registered {0} as a HealthCheck for app: {1}", new Object[] { bean.getBeanClass().getCanonicalName(), invocationManager.getCurrentInvocation().getAppName() });
}
}
}
use of javax.enterprise.inject.spi.BeanManager in project Payara by payara.
the class CDISecondChanceResolver method justInTimeResolution.
/* (non-Javadoc)
* @see org.glassfish.hk2.api.JustInTimeInjectionResolver#justInTimeResolution(org.glassfish.hk2.api.Injectee)
*/
@SuppressWarnings({ "unchecked" })
@Override
public boolean justInTimeResolution(Injectee failedInjectionPoint) {
Type requiredType = failedInjectionPoint.getRequiredType();
Set<Annotation> setQualifiers = failedInjectionPoint.getRequiredQualifiers();
Annotation[] qualifiers = setQualifiers.toArray(new Annotation[setQualifiers.size()]);
BeanManager manager = getCurrentBeanManager();
if (manager == null)
return false;
Set<Bean<?>> beans = manager.getBeans(requiredType, qualifiers);
if (beans == null || beans.isEmpty()) {
return false;
}
DynamicConfiguration config = ServiceLocatorUtilities.createDynamicConfiguration(locator);
for (Bean<?> bean : beans) {
// Add a bean to the service locator
CDIHK2Descriptor<Object> descriptor = new CDIHK2Descriptor<Object>(manager, (Bean<Object>) bean, requiredType);
config.addActiveDescriptor(descriptor);
}
config.commit();
return true;
}
use of javax.enterprise.inject.spi.BeanManager in project Payara by payara.
the class WebComponentInjectionManager method decorate.
@SuppressWarnings("unchecked")
@Override
public void decorate(T webComponent, WebModule wm) {
if (wm.getWebBundleDescriptor().hasExtensionProperty(WeldDeployer.WELD_EXTENSION)) {
DeploymentContext deploymentContext = wm.getWebModuleConfig().getDeploymentContext();
WeldBootstrap weldBootstrap = deploymentContext.getTransientAppMetaData(WeldDeployer.WELD_BOOTSTRAP, org.jboss.weld.bootstrap.WeldBootstrap.class);
DeploymentImpl deploymentImpl = deploymentContext.getTransientAppMetaData(WeldDeployer.WELD_DEPLOYMENT, DeploymentImpl.class);
Collection<BeanDeploymentArchive> deployments = deploymentImpl.getBeanDeploymentArchives();
BeanDeploymentArchive beanDeploymentArchive = deployments.iterator().next();
BeanManager beanManager = weldBootstrap.getManager(beanDeploymentArchive);
// PENDING : Not available in this Web Beans Release
CreationalContext<T> ccontext = beanManager.createCreationalContext(null);
@SuppressWarnings("rawtypes") Class<T> clazz = (Class<T>) webComponent.getClass();
AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(clazz);
InjectionTarget<T> injectionTarget = beanManager.createInjectionTarget(annotatedType);
injectionTarget.inject(webComponent, ccontext);
}
}
Aggregations