Search in sources :

Example 76 with BeanManager

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;
}
Also used : InjectionTarget(javax.enterprise.inject.spi.InjectionTarget) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 77 with BeanManager

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();
        }
    });
}
Also used : WebBeansConfigurationListener(org.apache.webbeans.servlet.WebBeansConfigurationListener) InjectionTarget(javax.enterprise.inject.spi.InjectionTarget) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 78 with BeanManager

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() });
        }
    }
}
Also used : HealthCheckService(fish.payara.microprofile.healthcheck.HealthCheckService) Health(org.eclipse.microprofile.health.Health) InvocationManager(org.glassfish.api.invocation.InvocationManager) HealthCheck(org.eclipse.microprofile.health.HealthCheck) ServletException(javax.servlet.ServletException) Bean(javax.enterprise.inject.spi.Bean) ServletRegistration(javax.servlet.ServletRegistration) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 79 with BeanManager

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;
}
Also used : Type(java.lang.reflect.Type) DynamicConfiguration(org.glassfish.hk2.api.DynamicConfiguration) BeanManager(javax.enterprise.inject.spi.BeanManager) Annotation(java.lang.annotation.Annotation) Bean(javax.enterprise.inject.spi.Bean)

Example 80 with BeanManager

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);
    }
}
Also used : WeldBootstrap(org.jboss.weld.bootstrap.WeldBootstrap) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) BeanManager(javax.enterprise.inject.spi.BeanManager)

Aggregations

BeanManager (javax.enterprise.inject.spi.BeanManager)83 Bean (javax.enterprise.inject.spi.Bean)34 Test (org.junit.Test)16 NamingException (javax.naming.NamingException)7 Annotation (java.lang.annotation.Annotation)6 ArrayList (java.util.ArrayList)6 InjectionTarget (javax.enterprise.inject.spi.InjectionTarget)6 CdiContainer (org.apache.deltaspike.cdise.api.CdiContainer)6 CreationalContext (javax.enterprise.context.spi.CreationalContext)5 InitialContext (javax.naming.InitialContext)5 ValidatorFactory (javax.validation.ValidatorFactory)5 CarRepair (org.apache.deltaspike.cdise.tck.beans.CarRepair)5 HashMap (java.util.HashMap)4 Set (java.util.Set)4 TransactionManager (javax.transaction.TransactionManager)4 ContextControl (org.apache.deltaspike.cdise.api.ContextControl)4 Type (java.lang.reflect.Type)3 Map (java.util.Map)3 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)3 Validator (javax.validation.Validator)3