Search in sources :

Example 46 with Bean

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

Example 47 with Bean

use of javax.enterprise.inject.spi.Bean in project camel by apache.

the class CamelCdiRunner method createTest.

@Override
protected Object createTest() {
    BeanManager manager = context.getBeanManager();
    Set<Bean<?>> beans = manager.getBeans(getTestClass().getJavaClass(), AnyLiteral.INSTANCE);
    Bean<?> bean = beans.iterator().next();
    // TODO: manage lifecycle of @Dependent beans
    return manager.getReference(bean, bean.getBeanClass(), manager.createCreationalContext(bean));
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 48 with Bean

use of javax.enterprise.inject.spi.Bean in project Activiti by Activiti.

the class BusinessProcessContext method get.

@Override
public <T> T get(Contextual<T> contextual) {
    Bean<T> bean = (Bean<T>) contextual;
    String variableName = bean.getName();
    BusinessProcess businessProcess = getBusinessProcess();
    Object variable = businessProcess.getVariable(variableName);
    if (variable != null) {
        if (logger.isDebugEnabled()) {
            if (businessProcess.isAssociated()) {
                logger.debug("Getting instance of bean '{}' from Execution[{}]", variableName, businessProcess.getExecutionId());
            } else {
                logger.debug("Getting instance of bean '{}' from transient bean store", variableName);
            }
        }
        return (T) variable;
    } else {
        return null;
    }
}
Also used : Bean(javax.enterprise.inject.spi.Bean) BusinessProcess(org.activiti.cdi.BusinessProcess)

Example 49 with Bean

use of javax.enterprise.inject.spi.Bean in project wildfly by wildfly.

the class WeldContextSetup method teardown.

@SuppressWarnings("unchecked")
public void teardown(Map<String, Object> properties) {
    try {
        final BeanManager manager = (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
        if (manager != null && Container.available()) {
            final Bean<BoundSessionContext> sessionContextBean = (Bean<BoundSessionContext>) manager.resolve(manager.getBeans(BoundSessionContext.class, BoundLiteral.INSTANCE));
            CreationalContext<?> ctx = manager.createCreationalContext(sessionContextBean);
            final BoundSessionContext sessionContext = (BoundSessionContext) manager.getReference(sessionContextBean, BoundSessionContext.class, ctx);
            sessionContext.invalidate();
            sessionContext.deactivate();
            sessionContext.dissociate(sessionContexts.get());
            final Bean<BoundRequestContext> requestContextBean = (Bean<BoundRequestContext>) manager.resolve(manager.getBeans(BoundRequestContext.class, BoundLiteral.INSTANCE));
            ctx = manager.createCreationalContext(requestContextBean);
            final BoundRequestContext requestContext = (BoundRequestContext) manager.getReference(requestContextBean, BoundRequestContext.class, ctx);
            requestContext.invalidate();
            requestContext.deactivate();
            requestContext.dissociate(requestContexts.get());
            final Bean<BoundConversationContext> conversationContextBean = (Bean<BoundConversationContext>) manager.resolve(manager.getBeans(BoundConversationContext.class, BoundLiteral.INSTANCE));
            ctx = manager.createCreationalContext(conversationContextBean);
            final BoundConversationContext conversationContext = (BoundConversationContext) manager.getReference(conversationContextBean, BoundConversationContext.class, ctx);
            conversationContext.invalidate();
            conversationContext.deactivate();
            conversationContext.dissociate(boundRequests.get());
        }
    } catch (NamingException e) {
        WeldLogger.ROOT_LOGGER.failedToTearDownWeldContexts(e);
    } finally {
        sessionContexts.remove();
        requestContexts.remove();
        boundRequests.remove();
    }
}
Also used : BoundRequestContext(org.jboss.weld.context.bound.BoundRequestContext) BoundSessionContext(org.jboss.weld.context.bound.BoundSessionContext) BoundConversationContext(org.jboss.weld.context.bound.BoundConversationContext) NamingException(javax.naming.NamingException) BeanManager(javax.enterprise.inject.spi.BeanManager) InitialContext(javax.naming.InitialContext) Bean(javax.enterprise.inject.spi.Bean)

Example 50 with Bean

use of javax.enterprise.inject.spi.Bean in project wildfly by wildfly.

the class WeldClassIntrospector method getInjectionTarget.

private InjectionTarget getInjectionTarget(Class<?> clazz) {
    InjectionTarget<?> target = injectionTargets.get(clazz);
    if (target != null) {
        return target;
    }
    final BeanManagerImpl beanManager = BeanManagerProxy.unwrap(this.beanManager.getValue());
    Bean<?> bean = null;
    Set<Bean<?>> beans = new HashSet<>(beanManager.getBeans(clazz, AnyLiteral.INSTANCE));
    Iterator<Bean<?>> it = beans.iterator();
    //go through and remove them from the bean set
    while (it.hasNext()) {
        Bean<?> b = it.next();
        if (b.getBeanClass() != clazz) {
            it.remove();
        }
    }
    if (beans.size() == 1) {
        bean = beans.iterator().next();
    }
    InjectionTarget<?> newTarget = InjectionTargets.createInjectionTarget(clazz, bean, beanManager, true);
    target = injectionTargets.putIfAbsent(clazz, newTarget);
    if (target == null) {
        return newTarget;
    } else {
        return target;
    }
}
Also used : BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) HashSet(java.util.HashSet) Bean(javax.enterprise.inject.spi.Bean)

Aggregations

Bean (javax.enterprise.inject.spi.Bean)74 BeanManager (javax.enterprise.inject.spi.BeanManager)33 Annotation (java.lang.annotation.Annotation)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)8 CreationalContext (javax.enterprise.context.spi.CreationalContext)7 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)7 Test (org.junit.Test)7 Set (java.util.Set)5 ProcessBean (javax.enterprise.inject.spi.ProcessBean)5 ScopedInstance (org.eclipse.jetty.cdi.core.ScopedInstance)5 IOException (java.io.IOException)4 Type (java.lang.reflect.Type)4 Map (java.util.Map)4 ContextControl (org.apache.deltaspike.cdise.api.ContextControl)4 HashMap (java.util.HashMap)3 ServletException (javax.servlet.ServletException)3 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)3 CdiContainer (org.apache.deltaspike.cdise.api.CdiContainer)3 CarRepair (org.apache.deltaspike.cdise.tck.beans.CarRepair)3