Search in sources :

Example 1 with InjectableContext

use of io.quarkus.arc.InjectableContext in project quarkus by quarkusio.

the class ClientProxies method getDelegate.

public static <T> T getDelegate(InjectableBean<T> bean) {
    List<InjectableContext> contexts = Arc.container().getContexts(bean.getScope());
    T result = null;
    if (contexts.size() == 1) {
        result = contexts.get(0).getIfActive(bean, ClientProxies::newCreationalContext);
    } else {
        InjectableContext selectedContext = null;
        for (int i = 0; i < contexts.size(); i++) {
            InjectableContext context = contexts.get(i);
            if (result != null) {
                if (context.isActive()) {
                    throw new IllegalArgumentException("More than one context object for the given scope: " + selectedContext + " " + context);
                }
            } else {
                result = context.getIfActive(bean, ClientProxies::newCreationalContext);
                if (result != null) {
                    selectedContext = context;
                }
            }
        }
    }
    if (result == null) {
        throw new ContextNotActiveException();
    }
    return result;
}
Also used : ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) InjectableContext(io.quarkus.arc.InjectableContext)

Example 2 with InjectableContext

use of io.quarkus.arc.InjectableContext in project camunda-bpm-platform by camunda.

the class BusinessProcessContextTest method shouldThrowUnsupportedOperationExceptionOnInjectableContextGetState.

@Test
public void shouldThrowUnsupportedOperationExceptionOnInjectableContextGetState() {
    InjectableContext businessProcessContext = Arc.container().getActiveContext(BusinessProcessScoped.class);
    assertThatThrownBy(businessProcessContext::getState).isInstanceOf(UnsupportedOperationException.class).hasMessage("io.quarkus.arc.InjectableContext#getState is unsupported");
}
Also used : InjectableContext(io.quarkus.arc.InjectableContext) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 3 with InjectableContext

use of io.quarkus.arc.InjectableContext in project camunda-bpm-platform by camunda.

the class BusinessProcessContextTest method shouldThrowUnsupportedOperationExceptionOnInjectableContextDestroyContextual.

@Test
public void shouldThrowUnsupportedOperationExceptionOnInjectableContextDestroyContextual() {
    Bean<?> bean = Arc.container().beanManager().getBeans(BusinessProcessScopedBean.class).stream().findFirst().get();
    InjectableContext businessProcessContext = Arc.container().getActiveContext(BusinessProcessScoped.class);
    assertThatThrownBy(() -> businessProcessContext.destroy(bean)).isInstanceOf(UnsupportedOperationException.class).hasMessage("io.quarkus.arc.InjectableContext#destroy(contextual) is unsupported");
}
Also used : InjectableContext(io.quarkus.arc.InjectableContext) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 4 with InjectableContext

use of io.quarkus.arc.InjectableContext in project camunda-bpm-platform by camunda.

the class BusinessProcessContextTest method shouldThrowUnsupportedOperationExceptionOnInjectableContextDestroy.

@Test
public void shouldThrowUnsupportedOperationExceptionOnInjectableContextDestroy() {
    InjectableContext businessProcessContext = Arc.container().getActiveContext(BusinessProcessScoped.class);
    assertThatThrownBy(businessProcessContext::destroy).isInstanceOf(UnsupportedOperationException.class).hasMessage("io.quarkus.arc.InjectableContext#destroy is unsupported");
}
Also used : InjectableContext(io.quarkus.arc.InjectableContext) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Example 5 with InjectableContext

use of io.quarkus.arc.InjectableContext in project quarkus by quarkusio.

the class InstanceImpl method destroy.

@Override
public void destroy(Object instance) {
    Objects.requireNonNull(instance);
    if (instance instanceof ClientProxy) {
        ClientProxy proxy = (ClientProxy) instance;
        InjectableContext context = Arc.container().getActiveContext(proxy.arc_bean().getScope());
        if (context == null) {
            throw new ContextNotActiveException("No active context found for: " + proxy.arc_bean().getScope());
        }
        context.destroy(proxy.arc_bean());
    } else {
        // First try to destroy a dependent instance
        if (!creationalContext.destroyDependentInstance(instance)) {
            // If not successful then try the singleton context
            SingletonContext singletonContext = (SingletonContext) Arc.container().getActiveContext(Singleton.class);
            singletonContext.destroyInstance(instance);
        }
    }
}
Also used : Singleton(javax.inject.Singleton) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) ClientProxy(io.quarkus.arc.ClientProxy) InjectableContext(io.quarkus.arc.InjectableContext)

Aggregations

InjectableContext (io.quarkus.arc.InjectableContext)9 Test (org.junit.jupiter.api.Test)5 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)3 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)3 CreationalContextImpl (io.quarkus.arc.impl.CreationalContextImpl)2 Singleton (javax.inject.Singleton)2 ClientProxy (io.quarkus.arc.ClientProxy)1