Search in sources :

Example 1 with ContextNotActiveException

use of jakarta.enterprise.context.ContextNotActiveException in project core by weld.

the class RequestScopedActiveInterceptorTest method requestScopedNotActive.

@Test
public void requestScopedNotActive() {
    try (WeldContainer container = new Weld().initialize()) {
        Foo foo = container.select(Foo.class).get();
        foo.notInterceptedCall();
        Assert.fail();
    } catch (ContextNotActiveException e) {
    // expected exception
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) ContextNotActiveException(jakarta.enterprise.context.ContextNotActiveException) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 2 with ContextNotActiveException

use of jakarta.enterprise.context.ContextNotActiveException in project core by weld.

the class AbstractContext method get.

/**
 * Get the bean if it exists in the contexts.
 *
 * @return An instance of the bean
 * @throws ContextNotActiveException if the context is not active
 * @seejakarta.enterprise.context.spi.Context#get(BaseBean, boolean)
 */
@Override
@SuppressFBWarnings(value = "UL_UNRELEASED_LOCK", justification = "False positive from FindBugs")
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
    if (!isActive()) {
        throw new ContextNotActiveException();
    }
    checkContextInitialized();
    final BeanStore beanStore = getBeanStore();
    if (beanStore == null) {
        return null;
    }
    if (contextual == null) {
        throw ContextLogger.LOG.contextualIsNull();
    }
    BeanIdentifier id = getId(contextual);
    ContextualInstance<T> beanInstance = beanStore.get(id);
    if (beanInstance != null) {
        return beanInstance.getInstance();
    } else if (creationalContext != null) {
        LockedBean lock = null;
        try {
            if (multithreaded) {
                lock = beanStore.lock(id);
                beanInstance = beanStore.get(id);
                if (beanInstance != null) {
                    return beanInstance.getInstance();
                }
            }
            T instance = contextual.create(creationalContext);
            if (instance != null) {
                beanInstance = new SerializableContextualInstanceImpl<Contextual<T>, T>(contextual, instance, creationalContext, serviceRegistry.get(ContextualStore.class));
                beanStore.put(id, beanInstance);
            }
            return instance;
        } finally {
            if (lock != null) {
                lock.unlock();
            }
        }
    } else {
        return null;
    }
}
Also used : BeanStore(org.jboss.weld.contexts.beanstore.BeanStore) ContextualStore(org.jboss.weld.serialization.spi.ContextualStore) ContextNotActiveException(jakarta.enterprise.context.ContextNotActiveException) BeanIdentifier(org.jboss.weld.serialization.spi.BeanIdentifier) LockedBean(org.jboss.weld.contexts.beanstore.LockedBean) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with ContextNotActiveException

use of jakarta.enterprise.context.ContextNotActiveException in project helidon by oracle.

the class JpaTransactionScopedEntityManager method acquireDelegate.

/**
 * Acquires and returns a delegate {@link EntityManager}, adhering
 * to the rules spelled out by the JPA specification around
 * transaction-scoped {@link EntityManager}s.
 *
 * <p>This method never returns {@code null}.</p>
 *
 * <p>If a {@linkplain TransactionSupport#inTransaction() JTA
 * transaction is active}, then an {@link EntityManager} that is
 * joined to it is returned.  Otherwise a non-transactional {@link
 * EntityManager} is returned.</p>
 *
 * <p>Recall that this method is invoked by all {@link
 * DelegatingEntityManager} methods.</p>
 *
 * @return a non-{@code null} {@link EntityManager} that will be
 * used as this {@link JpaTransactionScopedEntityManager}'s
 * delegate
 */
@Override
protected EntityManager acquireDelegate() {
    final EntityManager returnValue;
    final int status = this.transactionSupport.getStatus();
    switch(status) {
        case TransactionSupport.STATUS_ACTIVE:
            // If we are or were just in a transaction, then we're
            // obligated to see if there's a transaction-scoped
            // EntityManager already affiliated with the current
            // transaction.  If there is, and its synchronization type
            // doesn't match ours, we're supposed to throw an
            // exception.  Remember that the transaction-scoped
            // context can go inactive at any point due to a rollback
            // on another thread, and may already be inactive at
            // *this* point.  This also means the status that dropped
            // us into this case statement may be stale.
            final Context transactionScopedContext = this.transactionSupport.getContext();
            if (transactionScopedContext == null || !transactionScopedContext.isActive()) {
                returnValue = this.nonTransactionalEntityManager;
            } else {
                EntityManager candidateReturnValue = this.cdiTransactionScopedEntityManager;
                try {
                    final Object existingContextualInstance = transactionScopedContext.get(this.oppositeSynchronizationBean);
                    if (existingContextualInstance != null) {
                        // exception.
                        throw new PersistenceException(Messages.format("mixedSynchronizationTypes", this.oppositeSynchronizationBean, existingContextualInstance));
                    }
                } catch (final ContextNotActiveException contextNotActiveException) {
                    candidateReturnValue = this.nonTransactionalEntityManager;
                } finally {
                    returnValue = candidateReturnValue;
                }
            }
            break;
        default:
            returnValue = this.nonTransactionalEntityManager;
            break;
    }
    assert returnValue != null;
    return returnValue;
}
Also used : Context(jakarta.enterprise.context.spi.Context) ReferenceCountedContext(io.helidon.integrations.cdi.referencecountedcontext.ReferenceCountedContext) EntityManager(jakarta.persistence.EntityManager) ContextNotActiveException(jakarta.enterprise.context.ContextNotActiveException) PersistenceException(jakarta.persistence.PersistenceException)

Example 4 with ContextNotActiveException

use of jakarta.enterprise.context.ContextNotActiveException in project wicket by apache.

the class ConversationPropagator method onRequestHandlerExecuted.

@Override
public void onRequestHandlerExecuted(RequestCycle cycle, IRequestHandler handler) {
    // page
    try {
        if (conversation.isTransient()) {
            return;
        }
    } catch (ContextNotActiveException cnax) {
        logger.debug("There is no active context for the requested scope!", cnax);
        return;
    }
    if (propagation.propagatesVia(handler, IPageRequestHandler.getPage(handler))) {
        logger.debug("Propagating non-transient conversation {} via page parameters of handler {}", conversation.getId(), handler);
        PageParameters parameters = getPageParameters(handler);
        if (parameters != null) {
            parameters.set(CID, conversation.getId());
            markPageWithConversationId(handler, conversation.getId());
        }
    }
}
Also used : ContextNotActiveException(jakarta.enterprise.context.ContextNotActiveException) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters)

Example 5 with ContextNotActiveException

use of jakarta.enterprise.context.ContextNotActiveException in project core by weld.

the class AbstractContext method destroy.

@Override
public void destroy(Contextual<?> contextual) {
    if (!isActive()) {
        throw new ContextNotActiveException();
    }
    checkContextInitialized();
    if (contextual == null) {
        throw ContextLogger.LOG.contextualIsNull();
    }
    final BeanStore beanStore = getBeanStore();
    if (beanStore == null) {
        throw ContextLogger.LOG.noBeanStoreAvailable(this);
    }
    BeanIdentifier id = getId(contextual);
    ContextualInstance<?> beanInstance = beanStore.remove(id);
    if (beanInstance != null) {
        RequestScopedCache.invalidate();
        destroyContextualInstance(beanInstance);
    }
}
Also used : BeanStore(org.jboss.weld.contexts.beanstore.BeanStore) ContextNotActiveException(jakarta.enterprise.context.ContextNotActiveException) BeanIdentifier(org.jboss.weld.serialization.spi.BeanIdentifier)

Aggregations

ContextNotActiveException (jakarta.enterprise.context.ContextNotActiveException)10 Test (org.junit.Test)3 Context (jakarta.enterprise.context.spi.Context)2 BeanStore (org.jboss.weld.contexts.beanstore.BeanStore)2 BeanIdentifier (org.jboss.weld.serialization.spi.BeanIdentifier)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 ReferenceCountedContext (io.helidon.integrations.cdi.referencecountedcontext.ReferenceCountedContext)1 Conversation (jakarta.enterprise.context.Conversation)1 CreationalContext (jakarta.enterprise.context.spi.CreationalContext)1 BeanManager (jakarta.enterprise.inject.spi.BeanManager)1 PassivationCapable (jakarta.enterprise.inject.spi.PassivationCapable)1 ConfigurableNavigationHandler (jakarta.faces.application.ConfigurableNavigationHandler)1 NavigationCase (jakarta.faces.application.NavigationCase)1 UICommand (jakarta.faces.component.UICommand)1 FacesContext (jakarta.faces.context.FacesContext)1 Flow (jakarta.faces.flow.Flow)1 FlowHandler (jakarta.faces.flow.FlowHandler)1 EntityManager (jakarta.persistence.EntityManager)1 PersistenceException (jakarta.persistence.PersistenceException)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1