Search in sources :

Example 1 with CreationalContext

use of javax.enterprise.context.spi.CreationalContext in project jetty.project by eclipse.

the class WebSocketScopeContext method newInstance.

@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> T newInstance(Class<T> clazz) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("newInstance({})", clazz);
    }
    Set<Bean<?>> beans = beanManager.getBeans(clazz, AnyLiteral.INSTANCE);
    if (beans.isEmpty()) {
        return null;
    }
    Bean bean = beans.iterator().next();
    CreationalContext cc = beanManager.createCreationalContext(bean);
    return (T) beanManager.getReference(bean, clazz, cc);
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) Bean(javax.enterprise.inject.spi.Bean)

Example 2 with CreationalContext

use of javax.enterprise.context.spi.CreationalContext in project Activiti by Activiti.

the class ProgrammaticBeanLookup method lookup.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Object lookup(String name, BeanManager bm) {
    Set<Bean<?>> beans = bm.getBeans(name);
    if (beans.isEmpty()) {
        throw new IllegalStateException("CDI BeanManager cannot find an instance of requested type '" + name + "'");
    }
    Bean bean = bm.resolve(beans);
    CreationalContext ctx = bm.createCreationalContext(bean);
    // select one beantype randomly. A bean has a non-empty set of beantypes.
    Type type = (Type) bean.getTypes().iterator().next();
    return bm.getReference(bean, type, ctx);
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) Type(java.lang.reflect.Type) Bean(javax.enterprise.inject.spi.Bean)

Example 3 with CreationalContext

use of javax.enterprise.context.spi.CreationalContext in project joynr by bmwcarit.

the class JoynrJeeMessageContextTest method testCreateNewInstanceAndSubsequentRetrievalOfExistingObject.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testCreateNewInstanceAndSubsequentRetrievalOfExistingObject() {
    CreationalContext creationalContext = mock(CreationalContext.class);
    Contextual contextual = mock(Contextual.class);
    when(contextual.create(creationalContext)).thenReturn("test");
    Object result = JoynrJeeMessageContext.getInstance().get(contextual, creationalContext);
    assertNotNull(result);
    assertEquals("test", result);
    verify(contextual).create(creationalContext);
    assertEquals("test", JoynrJeeMessageContext.getInstance().get(contextual));
    reset(contextual);
    reset(creationalContext);
    assertEquals("test", JoynrJeeMessageContext.getInstance().get(contextual, creationalContext));
    verify(contextual, never()).create(creationalContext);
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) Contextual(javax.enterprise.context.spi.Contextual) Test(org.junit.Test)

Example 4 with CreationalContext

use of javax.enterprise.context.spi.CreationalContext in project microservice_framework by CJSCommonPlatform.

the class BeanInstantiaterTest method shouldInstantiateABean.

@Test
@SuppressWarnings("unchecked")
public void shouldInstantiateABean() throws Exception {
    final Bean<Object> bean = mock(Bean.class);
    final Context context = mock(Context.class);
    final CreationalContext<Object> creationalContext = mock(CreationalContext.class);
    final Object instance = mock(Object.class);
    when(beanManager.getContext(bean.getScope())).thenReturn(context);
    when(beanManager.createCreationalContext(bean)).thenReturn(creationalContext);
    when(context.get(bean, creationalContext)).thenReturn(instance);
    final Object result = beanInstantiater.instantiate(bean);
    assertThat(result, is(instance));
}
Also used : Context(javax.enterprise.context.spi.Context) CreationalContext(javax.enterprise.context.spi.CreationalContext) Test(org.junit.Test)

Example 5 with CreationalContext

use of javax.enterprise.context.spi.CreationalContext in project microservice_framework by CJSCommonPlatform.

the class ResponseStrategyCache method strategyFromContext.

private ResponseStrategy strategyFromContext(final String responseStrategyName) {
    final Bean bean = beanManager.getBeans(responseStrategyName).stream().findFirst().orElseThrow(() -> new IllegalArgumentException(format("Response response not found: %s", responseStrategyName)));
    final CreationalContext ctx = beanManager.createCreationalContext(bean);
    return (ResponseStrategy) beanManager.getReference(bean, ResponseStrategy.class, ctx);
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) ResponseStrategy(uk.gov.justice.services.adapter.rest.processor.response.ResponseStrategy) Bean(javax.enterprise.inject.spi.Bean)

Aggregations

CreationalContext (javax.enterprise.context.spi.CreationalContext)129 Bean (javax.enterprise.inject.spi.Bean)110 Test (org.junit.Test)83 URL (java.net.URL)67 Path (org.uberfire.backend.vfs.Path)67 KieModuleService (org.kie.workbench.common.services.shared.project.KieModuleService)66 Package (org.guvnor.common.services.project.model.Package)43 Module (org.guvnor.common.services.project.model.Module)13 BeanManager (javax.enterprise.inject.spi.BeanManager)10 BuildResults (org.guvnor.common.services.project.builder.model.BuildResults)6 IncrementalBuildResults (org.guvnor.common.services.project.builder.model.IncrementalBuildResults)6 Before (org.junit.Before)6 ArrayList (java.util.ArrayList)4 Context (javax.enterprise.context.spi.Context)4 InjectionTarget (javax.enterprise.inject.spi.InjectionTarget)4 WebBeansContext (org.apache.webbeans.config.WebBeansContext)4 Annotation (java.lang.annotation.Annotation)3 Method (java.lang.reflect.Method)3 Type (java.lang.reflect.Type)3 HashMap (java.util.HashMap)3