Search in sources :

Example 1 with WeldBean

use of org.jboss.weld.bean.WeldBean in project core by weld.

the class WeldInternalConstructsTest method testInterceptedProxiedBean.

@Test
public void testInterceptedProxiedBean() {
    InterceptedProxiedBean interceptedBean = holder.getInterceptedProxiedBean();
    // trigger interception and assert it works
    Assert.assertTrue(interceptedBean.ping());
    // should be instance of WeldConstruct and WeldClientProxy
    Assert.assertTrue(interceptedBean instanceof WeldConstruct);
    Assert.assertTrue(interceptedBean instanceof WeldClientProxy);
    // cast to WeldClientProxy and test the methods
    WeldClientProxy wcp = (WeldClientProxy) interceptedBean;
    WeldClientProxy.Metadata cm = wcp.getMetadata();
    Object contextualInstance = cm.getContextualInstance();
    // kind of indirect check that this is the actual contextual instance
    Assert.assertTrue(contextualInstance instanceof InterceptedProxiedBean);
    Assert.assertFalse(contextualInstance instanceof WeldClientProxy);
    // NOTE - contextual instance is still a Weld subclass because of interception/decoration
    Assert.assertTrue(contextualInstance instanceof WeldConstruct);
    Bean<?> bean = cm.getBean();
    Set<Bean<?>> beans = bm.getBeans(InterceptedProxiedBean.class);
    Assert.assertEquals(1, beans.size());
    Assert.assertEquals(((WeldBean) beans.iterator().next()).getIdentifier().asString(), ((WeldBean) bean).getIdentifier().asString());
}
Also used : WeldBean(org.jboss.weld.bean.WeldBean) WeldClientProxy(org.jboss.weld.proxy.WeldClientProxy) WeldConstruct(org.jboss.weld.proxy.WeldConstruct) WeldBean(org.jboss.weld.bean.WeldBean) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 2 with WeldBean

use of org.jboss.weld.bean.WeldBean in project core by weld.

the class AfterBeanDiscoveryImpl method processBeanRegistration.

private <T> void processBeanRegistration(BeanRegistration registration, GlobalEnablementBuilder globalEnablementBuilder) {
    Bean<?> bean = registration.initBean();
    BeanManagerImpl beanManager = registration.initBeanManager();
    if (beanManager == null) {
        // Get the bean manager for beans added via ABD#addBean()
        beanManager = getOrCreateBeanDeployment(bean.getBeanClass()).getBeanManager();
    } else {
        // Also validate the bean produced by a builder
        ExternalBeanAttributesFactory.validateBeanAttributes(bean, getBeanManager());
        validateBean(bean);
    }
    // Custom beans (alternatives, interceptors, decorators) may also implement javax.enterprise.inject.spi.Prioritized
    Integer priority = (bean instanceof Prioritized) ? ((Prioritized) bean).getPriority() : null;
    // if added via WeldBeanConfigurator, there might be a priority specified as well
    if (priority == null && bean instanceof WeldBean) {
        priority = ((WeldBean) bean).getPriority();
    }
    if (bean instanceof Interceptor<?>) {
        beanManager.addInterceptor((Interceptor<?>) bean);
        if (priority != null) {
            globalEnablementBuilder.addInterceptor(bean.getBeanClass(), priority);
        }
    } else if (bean instanceof Decorator<?>) {
        beanManager.addDecorator(CustomDecoratorWrapper.of((Decorator<?>) bean, beanManager));
        if (priority != null) {
            globalEnablementBuilder.addDecorator(bean.getBeanClass(), priority);
        }
    } else {
        beanManager.addBean(bean);
        if (priority != null && bean.isAlternative()) {
            globalEnablementBuilder.addAlternative(bean.getBeanClass(), priority);
        }
    }
    containerLifecycleEvents.fireProcessBean(beanManager, bean, registration.extension);
}
Also used : WeldBean(org.jboss.weld.bean.WeldBean) Prioritized(javax.enterprise.inject.spi.Prioritized) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) Interceptor(javax.enterprise.inject.spi.Interceptor)

Example 3 with WeldBean

use of org.jboss.weld.bean.WeldBean in project core by weld.

the class WeldInternalConstructsTest method testClientProxyBean.

@Test
public void testClientProxyBean() {
    ClientProxyBean clientProxyBean = holder.getClientProxyBean();
    // trigger proxy creation
    clientProxyBean.ping();
    // injected bean should be instance of WeldConstruct and WeldClientProxy
    Assert.assertTrue(clientProxyBean instanceof WeldConstruct);
    Assert.assertTrue(clientProxyBean instanceof WeldClientProxy);
    // cast to WeldClientProxy and test the methods
    WeldClientProxy wcp = (WeldClientProxy) clientProxyBean;
    WeldClientProxy.Metadata cm = wcp.getMetadata();
    Object contextualInstance = cm.getContextualInstance();
    // kind of indirect check that this is the actual contextual instance
    Assert.assertTrue(contextualInstance instanceof ClientProxyBean);
    Assert.assertFalse(contextualInstance instanceof WeldConstruct);
    Bean<?> bean = cm.getBean();
    Set<Bean<?>> beans = bm.getBeans(ClientProxyBean.class);
    Assert.assertEquals(1, beans.size());
    Assert.assertEquals(((WeldBean) beans.iterator().next()).getIdentifier().asString(), ((WeldBean) bean).getIdentifier().asString());
}
Also used : WeldBean(org.jboss.weld.bean.WeldBean) WeldClientProxy(org.jboss.weld.proxy.WeldClientProxy) WeldConstruct(org.jboss.weld.proxy.WeldConstruct) WeldBean(org.jboss.weld.bean.WeldBean) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 4 with WeldBean

use of org.jboss.weld.bean.WeldBean in project core by weld.

the class WeldInternalConstructsTest method testDecoratedProxiedBean.

@Test
public void testDecoratedProxiedBean() {
    DecoratedProxiedBean decoratedBean = holder.getDecoratedProxiedBean();
    // trigger decoration and assert it works
    Assert.assertTrue(decoratedBean.ping());
    // should be instance of WeldConstruct and WeldClientProxy
    Assert.assertTrue(decoratedBean instanceof WeldConstruct);
    Assert.assertTrue(decoratedBean instanceof WeldClientProxy);
    // cast to WeldClientProxy and test the methods
    WeldClientProxy wcp = (WeldClientProxy) decoratedBean;
    WeldClientProxy.Metadata cm = wcp.getMetadata();
    Object contextualInstance = cm.getContextualInstance();
    // kind of indirect check that this is the actual contextual instance
    Assert.assertTrue(contextualInstance instanceof DecoratedProxiedBean);
    Assert.assertFalse(contextualInstance instanceof WeldClientProxy);
    // NOTE - contextual instance is still a Weld subclass because of interception/decoration
    Assert.assertTrue(contextualInstance instanceof WeldConstruct);
    Bean<?> bean = cm.getBean();
    Set<Bean<?>> beans = bm.getBeans(DecoratedProxiedBean.class);
    Assert.assertEquals(1, beans.size());
    Assert.assertEquals(((WeldBean) beans.iterator().next()).getIdentifier().asString(), ((WeldBean) bean).getIdentifier().asString());
}
Also used : WeldBean(org.jboss.weld.bean.WeldBean) WeldClientProxy(org.jboss.weld.proxy.WeldClientProxy) WeldConstruct(org.jboss.weld.proxy.WeldConstruct) WeldBean(org.jboss.weld.bean.WeldBean) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Aggregations

WeldBean (org.jboss.weld.bean.WeldBean)4 Bean (javax.enterprise.inject.spi.Bean)3 WeldClientProxy (org.jboss.weld.proxy.WeldClientProxy)3 WeldConstruct (org.jboss.weld.proxy.WeldConstruct)3 Test (org.junit.Test)3 Interceptor (javax.enterprise.inject.spi.Interceptor)1 Prioritized (javax.enterprise.inject.spi.Prioritized)1 BeanManagerImpl (org.jboss.weld.manager.BeanManagerImpl)1