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
}
}
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;
}
}
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;
}
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());
}
}
}
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);
}
}
Aggregations