Search in sources :

Example 1 with Bean

use of javax.enterprise.inject.spi.Bean 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 Bean

use of javax.enterprise.inject.spi.Bean 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 3 with Bean

use of javax.enterprise.inject.spi.Bean 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 4 with Bean

use of javax.enterprise.inject.spi.Bean 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 5 with Bean

use of javax.enterprise.inject.spi.Bean 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)

Aggregations

Bean (javax.enterprise.inject.spi.Bean)259 Test (org.junit.Test)119 CreationalContext (javax.enterprise.context.spi.CreationalContext)110 BeanManager (javax.enterprise.inject.spi.BeanManager)76 URL (java.net.URL)68 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 HashSet (java.util.HashSet)31 Annotation (java.lang.annotation.Annotation)18 ArrayList (java.util.ArrayList)14 Module (org.guvnor.common.services.project.model.Module)13 AnnotationLiteral (javax.enterprise.util.AnnotationLiteral)11 Type (java.lang.reflect.Type)9 Set (java.util.Set)9 AfterDeploymentValidation (javax.enterprise.inject.spi.AfterDeploymentValidation)9 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)9 Any (javax.enterprise.inject.Any)8 AbstractBuiltInBean (org.jboss.weld.bean.builtin.AbstractBuiltInBean)8 Weld (org.jboss.weld.environment.se.Weld)8