Search in sources :

Example 1 with ScopedInstance

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));
    }
}
Also used : ScopedInstance(org.eclipse.jetty.cdi.core.ScopedInstance) Bean(javax.enterprise.inject.spi.Bean)

Example 2 with ScopedInstance

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;
}
Also used : SimpleBeanStore(org.eclipse.jetty.cdi.core.SimpleBeanStore) ScopedInstance(org.eclipse.jetty.cdi.core.ScopedInstance) Bean(javax.enterprise.inject.spi.Bean)

Example 3 with ScopedInstance

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));
    }
}
Also used : ScopedInstance(org.eclipse.jetty.cdi.core.ScopedInstance) Bean(javax.enterprise.inject.spi.Bean)

Example 4 with ScopedInstance

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));
    }
}
Also used : ScopedInstance(org.eclipse.jetty.cdi.core.ScopedInstance) BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 5 with ScopedInstance

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));
    }
}
Also used : ScopedInstance(org.eclipse.jetty.cdi.core.ScopedInstance) Bean(javax.enterprise.inject.spi.Bean)

Aggregations

Bean (javax.enterprise.inject.spi.Bean)5 ScopedInstance (org.eclipse.jetty.cdi.core.ScopedInstance)5 BeanManager (javax.enterprise.inject.spi.BeanManager)1 SimpleBeanStore (org.eclipse.jetty.cdi.core.SimpleBeanStore)1