Search in sources :

Example 61 with Bean

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

the class PartialBeanBindingExtension method createBeans.

public <X> void createBeans(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) {
    if (!this.isActivated) {
        return;
    }
    if (this.definitionError != null) {
        afterBeanDiscovery.addDefinitionError(this.definitionError);
        return;
    }
    for (Map.Entry<Class<? extends Annotation>, PartialBeanDescriptor> entry : this.descriptors.entrySet()) {
        PartialBeanDescriptor descriptor = entry.getValue();
        if (descriptor.getClasses() != null) {
            for (Class partialBeanClass : descriptor.getClasses()) {
                Bean partialBean = createPartialBean(partialBeanClass, descriptor, afterBeanDiscovery, beanManager);
                if (partialBean != null) {
                    afterBeanDiscovery.addBean(partialBean);
                    List<Bean> partialProducerBeans = createPartialProducersDefinedIn(partialBean, afterBeanDiscovery, beanManager);
                    for (Bean partialProducerBean : partialProducerBeans) {
                        afterBeanDiscovery.addBean(partialProducerBean);
                    }
                }
            }
        }
    }
    this.descriptors.clear();
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) Annotation(java.lang.annotation.Annotation) Bean(javax.enterprise.inject.spi.Bean)

Example 62 with Bean

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

the class DeltaSpikeProxyContextualLifecycle method instantiateDelegateInvocationHandler.

protected H instantiateDelegateInvocationHandler() {
    Set<Bean<H>> handlerBeans = BeanProvider.getBeanDefinitions(this.delegateInvocationHandlerClass, false, true);
    if (handlerBeans.size() != 1) {
        StringBuilder beanInfo = new StringBuilder();
        for (Bean<H> bean : handlerBeans) {
            if (beanInfo.length() != 0) {
                beanInfo.append(", ");
            }
            beanInfo.append(bean);
            if (bean instanceof PassivationCapable) {
                beanInfo.append(" bean-id: ").append(((PassivationCapable) bean).getId());
            }
        }
        throw new IllegalStateException(handlerBeans.size() + " beans found for " + this.delegateInvocationHandlerClass + " found beans: " + beanInfo.toString());
    }
    Bean<H> handlerBean = handlerBeans.iterator().next();
    BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
    CreationalContext<?> creationalContext = beanManager.createCreationalContext(handlerBean);
    H handlerInstance = (H) beanManager.getReference(handlerBean, this.delegateInvocationHandlerClass, creationalContext);
    if (handlerBean.getScope().equals(Dependent.class)) {
        this.creationalContextOfDependentHandler = creationalContext;
    }
    return handlerInstance;
}
Also used : PassivationCapable(javax.enterprise.inject.spi.PassivationCapable) BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 63 with Bean

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

the class AbstractTestCase method assertPojoExists.

void assertPojoExists(BeanManager beanManager) {
    Set<Bean<?>> beans = beanManager.getBeans(Pojo.class, any);
    assertFalse(beans.isEmpty());
    Iterator<Bean<?>> iterator = beans.iterator();
    Bean<?> bean = iterator.next();
    assertTrue(Pojo.class.isAssignableFrom(bean.getBeanClass()));
    assertFalse(iterator.hasNext());
    bean = beanManager.resolve(beans);
    CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
    Pojo pojo = (Pojo) beanManager.getReference(bean, Pojo.class, ctx);
    assertNotNull(pojo);
}
Also used : Pojo(org.apache.aries.cdi.test.interfaces.Pojo) Bean(javax.enterprise.inject.spi.Bean)

Example 64 with Bean

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

the class CdiBeanTests method testBundleContextInjection.

public void testBundleContextInjection() throws Exception {
    BeanManager beanManager = cdiContainer.getBeanManager();
    assertNotNull(beanManager);
    @SuppressWarnings("serial") Set<Bean<?>> beans = beanManager.getBeans(Object.class, new AnnotationLiteral<BundleContextBeanQualifier>() {
    });
    Bean<?> bean = beanManager.resolve(beans);
    CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
    Object bcb = beanManager.getReference(bean, Object.class, ctx);
    assertNotNull(bcb);
    @SuppressWarnings("unchecked") BeanService<BundleContext> bti = (BeanService<BundleContext>) bcb;
    assertNotNull(bti.get());
    assertTrue(bti.get() instanceof BundleContext);
}
Also used : BundleContextBeanQualifier(org.apache.aries.cdi.test.interfaces.BundleContextBeanQualifier) BeanManager(javax.enterprise.inject.spi.BeanManager) BeanService(org.apache.aries.cdi.test.interfaces.BeanService) Bean(javax.enterprise.inject.spi.Bean) BundleContext(org.osgi.framework.BundleContext)

Example 65 with Bean

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

the class LazyRealm method instance.

private Realm instance() {
    if (delegate == null) {
        synchronized (this) {
            if (delegate == null) {
                final Object instance;
                ClassLoader cl = Thread.currentThread().getContextClassLoader();
                if (container != null && container.getLoader() != null && container.getLoader().getClassLoader() != null) {
                    cl = container.getLoader().getClassLoader();
                }
                final Class<?> clazz;
                try {
                    clazz = cl.loadClass(realmClass);
                } catch (final ClassNotFoundException e) {
                    throw new TomEERuntimeException(e);
                }
                if (!cdi) {
                    try {
                        final ObjectRecipe recipe = new ObjectRecipe(clazz);
                        recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
                        recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
                        recipe.allow(Option.FIELD_INJECTION);
                        recipe.allow(Option.PRIVATE_PROPERTIES);
                        if (properties != null) {
                            final Properties props = new PropertiesAdapter().unmarshal(properties.trim().replaceAll("\\p{Space}*(\\p{Alnum}*)=", "\n$1="));
                            recipe.setAllProperties(props);
                        }
                        instance = recipe.create();
                    } catch (final Exception e) {
                        throw new TomEERuntimeException(e);
                    }
                } else {
                    final WebBeansContext webBeansContext;
                    try {
                        webBeansContext = WebBeansContext.currentInstance();
                        if (webBeansContext == null) {
                            return null;
                        }
                    } catch (final IllegalStateException ise) {
                        // too early to have a cdi bean, skip these methods - mainly init() but @PostConstruct works then
                        return null;
                    }
                    final BeanManager bm = webBeansContext.getBeanManagerImpl();
                    final Set<Bean<?>> beans = bm.getBeans(clazz);
                    final Bean<?> bean = bm.resolve(beans);
                    if (bean == null) {
                        return null;
                    }
                    creationalContext = bm.createCreationalContext(null);
                    instance = bm.getReference(bean, clazz, creationalContext);
                }
                if (instance == null) {
                    throw new TomEERuntimeException("realm can't be retrieved from cdi");
                }
                if (instance instanceof Realm) {
                    delegate = (Realm) instance;
                    delegate.setContainer(container);
                    delegate.setCredentialHandler(credentialHandler);
                    if (Lifecycle.class.isInstance(delegate)) {
                        if (init) {
                            try {
                                final Lifecycle lifecycle = Lifecycle.class.cast(delegate);
                                lifecycle.init();
                                if (start) {
                                    lifecycle.start();
                                }
                            } catch (final LifecycleException e) {
                            // no-op
                            }
                        }
                    }
                } else {
                    delegate = new LowTypedRealm(instance);
                    delegate.setContainer(container);
                    delegate.setCredentialHandler(credentialHandler);
                }
                for (final PropertyChangeListener listener : support.getPropertyChangeListeners()) {
                    delegate.addPropertyChangeListener(listener);
                }
            }
        }
    }
    return delegate;
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) PropertyChangeListener(java.beans.PropertyChangeListener) Lifecycle(org.apache.catalina.Lifecycle) Properties(java.util.Properties) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) Bean(javax.enterprise.inject.spi.Bean) PropertiesAdapter(org.apache.openejb.config.sys.PropertiesAdapter) WebBeansContext(org.apache.webbeans.config.WebBeansContext) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) BeanManager(javax.enterprise.inject.spi.BeanManager) Realm(org.apache.catalina.Realm)

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