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