Java Code Examples for javax.enterprise.inject.spi.BeanManager#getContext()

The following examples show how to use javax.enterprise.inject.spi.BeanManager#getContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransactionBeanWithEvents.java    From quarkus with Apache License 2.0 6 votes vote down vote up
void transactionScopePreDestroy(@Observes @BeforeDestroyed(TransactionScoped.class) final Object event,
        final BeanManager beanManager) throws SystemException {
    Transaction tx = tm.getTransaction();
    if (tx == null) {
        log.error("@BeforeDestroyed expects an active transaction");
        throw new IllegalStateException("@BeforeDestroyed expects an active transaction");
    }
    Context ctx;
    try {
        ctx = beanManager.getContext(TransactionScoped.class);
    } catch (Exception e) {
        log.error("Context on @Initialized is not available");
        throw e;
    }
    if (!ctx.isActive()) {
        log.error("Context on @BeforeDestroyed has to be active");
        throw new IllegalStateException("Context on @BeforeDestroyed has to be active");
    }
    if (!(event instanceof Transaction)) {
        log.error("@Intialized scope expects event payload being the " + Transaction.class.getName());
        throw new IllegalStateException("@Intialized scope expects event payload being the " + Transaction.class.getName());
    }

    beforeDestroyedCount++;
}
 
Example 2
Source File: VertxProducer.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * Undeploy verticles backed by contextual instances of {@link ApplicationScoped} beans before the application context is
 * destroyed. Otherwise Vertx may attempt to stop the verticles after the CDI container is shut down.
 * 
 * @param event
 * @param beanManager
 */
void undeployVerticles(@Observes @BeforeDestroyed(ApplicationScoped.class) Object event,
        BeanManager beanManager, io.vertx.mutiny.core.Vertx mutiny) {
    // Only beans with the AbstractVerticle in the set of bean types are considered - we need a deployment id 
    Set<Bean<?>> beans = beanManager.getBeans(AbstractVerticle.class, Any.Literal.INSTANCE);
    Context applicationContext = beanManager.getContext(ApplicationScoped.class);
    for (Bean<?> bean : beans) {
        if (ApplicationScoped.class.equals(bean.getScope())) {
            // Only beans with @ApplicationScoped are considered
            Object instance = applicationContext.get(bean);
            if (instance != null) {
                // Only existing instances are considered
                try {
                    AbstractVerticle verticle = (AbstractVerticle) instance;
                    mutiny.undeploy(verticle.deploymentID()).await().indefinitely();
                    LOGGER.debugf("Undeployed verticle: %s", instance.getClass());
                } catch (Exception e) {
                    // In theory, a user can undeploy the verticle manually
                    LOGGER.debugf("Unable to undeploy verticle %s: %s", instance.getClass(), e.toString());
                }
            }
        }
    }
}
 
Example 3
Source File: ContextUtils.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if the context for the given scope annotation is active.
 *
 * @param scopeAnnotationClass The scope annotation (e.g. @RequestScoped.class)
 * @param beanManager The {@link BeanManager}
 * @return If the context is active.
 */
public static boolean isContextActive(Class<? extends Annotation> scopeAnnotationClass, BeanManager beanManager)
{
    try
    {
        if (beanManager.getContext(scopeAnnotationClass) == null
                || !beanManager.getContext(scopeAnnotationClass).isActive())
        {
            return false;
        }
    }
    catch (ContextNotActiveException e)
    {
        return false;
    }

    return true;
}
 
Example 4
Source File: TransactionBeanWithEvents.java    From quarkus with Apache License 2.0 5 votes vote down vote up
void transactionScopeActivated(@Observes @Initialized(TransactionScoped.class) final Object event,
        final BeanManager beanManager) throws SystemException {
    Transaction tx = tm.getTransaction();
    if (tx == null) {
        log.error("@Intialized expects an active transaction");
        throw new IllegalStateException("@Intialized expects an active transaction");
    }
    if (tx.getStatus() != Status.STATUS_ACTIVE) {
        log.error("@Initialized expects transaction is Status.STATUS_ACTIVE");
        throw new IllegalStateException("@Initialized expects transaction is Status.STATUS_ACTIVE");
    }
    Context ctx;
    try {
        ctx = beanManager.getContext(TransactionScoped.class);
    } catch (Exception e) {
        log.error("Context on @Initialized is not available");
        throw e;
    }
    if (!ctx.isActive()) {
        log.error("Context on @Initialized has to be active");
        throw new IllegalStateException("Context on @Initialized has to be active");
    }
    if (!(event instanceof Transaction)) {
        log.error("@Intialized scope expects event payload being the " + Transaction.class.getName());
        throw new IllegalStateException("@Intialized scope expects event payload being the " + Transaction.class.getName());
    }

    initializedCount++;
}
 
Example 5
Source File: TransactionBeanWithEvents.java    From quarkus with Apache License 2.0 5 votes vote down vote up
void transactionScopeDestroyed(@Observes @Destroyed(TransactionScoped.class) final Object event,
        final BeanManager beanManager) throws SystemException {
    Transaction tx = tm.getTransaction();
    if (tx != null)
        throw new IllegalStateException("@Destroyed expects no transaction");
    try {
        Context ctx = beanManager.getContext(TransactionScoped.class);
        throw new IllegalStateException("No bean in context expected but it's " + ctx);
    } catch (final ContextNotActiveException expected) {
    }

    destroyedCount++;
}
 
Example 6
Source File: DolphinContextListenerCdi.java    From dolphin-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void sessionDestroyed(ClientSession dolphinSession) {
    Assert.requireNonNull(dolphinSession, "dolphinSession");
    BeanManager bm = BeanManagerProvider.getInstance().getBeanManager();
    ClientScopeContext clientContext = (ClientScopeContext) bm.getContext(ClientScoped.class);
    clientContext.destroy();
}
 
Example 7
Source File: RepositoryRefreshAgent.java    From HotswapAgent with GNU General Public License v2.0 4 votes vote down vote up
private static <T> T getInstance(BeanManager beanManager, Bean<T> bean) {
    Context context = beanManager.getContext(bean.getScope());
    return context.get(bean);
}