use of org.eclipse.jetty.cdi.core.ScopedInstance in project jetty.project by eclipse.
the class WebSocketScopeBaselineTest method newInstance.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> ScopedInstance<T> newInstance(Class<T> clazz) throws Exception {
ScopedInstance sbean = new ScopedInstance();
Set<Bean<?>> beans = container.getBeanManager().getBeans(clazz, AnyLiteral.INSTANCE);
if (beans.size() > 0) {
sbean.bean = beans.iterator().next();
sbean.creationalContext = container.getBeanManager().createCreationalContext(sbean.bean);
sbean.instance = container.getBeanManager().getReference(sbean.bean, clazz, sbean.creationalContext);
return sbean;
} else {
throw new Exception(String.format("Can't find class %s", clazz));
}
}
use of org.eclipse.jetty.cdi.core.ScopedInstance in project jetty.project by eclipse.
the class WebSocketScopeContext method get.
@SuppressWarnings("unchecked")
@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} get({},{})", this, contextual, creationalContext);
}
Bean<T> bean = (Bean<T>) contextual;
if (bean.getBeanClass().isAssignableFrom(Session.class)) {
return (T) this.session;
}
if (beanStore == null) {
beanStore = new SimpleBeanStore();
}
List<ScopedInstance<?>> beans = beanStore.getBeans(contextual);
if ((beans != null) && (!beans.isEmpty())) {
for (ScopedInstance<?> instance : beans) {
if (instance.bean.equals(bean)) {
return (T) instance.instance;
}
}
}
// no bean found, create it
T t = bean.create(creationalContext);
ScopedInstance<T> customInstance = new ScopedInstance<>();
customInstance.bean = bean;
customInstance.creationalContext = creationalContext;
customInstance.instance = t;
beanStore.addBean(customInstance);
return t;
}
use of org.eclipse.jetty.cdi.core.ScopedInstance in project jetty.project by eclipse.
the class ScopeBasicsTest method newInstance.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> ScopedInstance<T> newInstance(Class<T> clazz) throws Exception {
ScopedInstance sbean = new ScopedInstance();
Set<Bean<?>> beans = container.getBeanManager().getBeans(clazz, AnyLiteral.INSTANCE);
if (beans.size() > 0) {
sbean.bean = beans.iterator().next();
sbean.creationalContext = container.getBeanManager().createCreationalContext(sbean.bean);
sbean.instance = container.getBeanManager().getReference(sbean.bean, clazz, sbean.creationalContext);
return sbean;
} else {
throw new Exception(String.format("Can't find class %s", clazz));
}
}
use of org.eclipse.jetty.cdi.core.ScopedInstance in project jetty.project by eclipse.
the class WebSocketCdiListener method newInstance.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> ScopedInstance<T> newInstance(Class<T> clazz) {
BeanManager bm = CDI.current().getBeanManager();
ScopedInstance sbean = new ScopedInstance();
Set<Bean<?>> beans = bm.getBeans(clazz, AnyLiteral.INSTANCE);
if (beans.size() > 0) {
sbean.bean = beans.iterator().next();
sbean.creationalContext = bm.createCreationalContext(sbean.bean);
sbean.instance = bm.getReference(sbean.bean, clazz, sbean.creationalContext);
return sbean;
} else {
throw new RuntimeException(String.format("Can't find class %s", clazz));
}
}
use of org.eclipse.jetty.cdi.core.ScopedInstance in project jetty.project by eclipse.
the class WebSocketScopeSessionTest method newInstance.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> ScopedInstance<T> newInstance(Class<T> clazz) {
ScopedInstance sbean = new ScopedInstance();
Set<Bean<?>> beans = container.getBeanManager().getBeans(clazz, AnyLiteral.INSTANCE);
if (beans.size() > 0) {
sbean.bean = beans.iterator().next();
sbean.creationalContext = container.getBeanManager().createCreationalContext(sbean.bean);
sbean.instance = container.getBeanManager().getReference(sbean.bean, clazz, sbean.creationalContext);
return sbean;
} else {
throw new RuntimeException(String.format("Can't find class %s", clazz));
}
}
Aggregations