Search in sources :

Example 21 with BeanManager

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

the class InterceptorLookup method resolveInterceptors.

protected List<Interceptor<?>> resolveInterceptors(Object instance, Method method) {
    BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
    Annotation[] interceptorBindings = extractInterceptorBindings(beanManager, instance, method);
    if (interceptorBindings.length > 0) {
        return beanManager.resolveInterceptors(InterceptionType.AROUND_INVOKE, interceptorBindings);
    }
    return new ArrayList<Interceptor<?>>();
}
Also used : ArrayList(java.util.ArrayList) BeanManager(javax.enterprise.inject.spi.BeanManager) Annotation(java.lang.annotation.Annotation)

Example 22 with BeanManager

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

the class OSGiBeanDescriptorTests method testReferences.

@SuppressWarnings("serial")
public void testReferences() throws Exception {
    Bundle tb1Bundle = installBundle("tb1.jar");
    Bundle tb2Bundle = installBundle("tb2.jar");
    CdiContainer cdiContainer = waitForCdiContainer(tb1Bundle.getBundleId());
    try {
        BeanManager beanManager = cdiContainer.getBeanManager();
        Set<Bean<?>> beans = beanManager.getBeans("beanimpl");
        Bean<?> bean = beanManager.resolve(beans);
        CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
        BeanService<?> beanService = (BeanService<?>) beanManager.getReference(bean, new TypeLiteral<BeanService<?>>() {
        }.getType(), ctx);
        assertNotNull(beanService);
        assertEquals("POJO-IMPLBEAN-IMPL", beanService.doSomething());
    } finally {
        tb2Bundle.uninstall();
        tb1Bundle.uninstall();
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BeanManager(javax.enterprise.inject.spi.BeanManager) BeanService(org.apache.aries.cdi.test.interfaces.BeanService) CdiContainer(org.osgi.service.cdi.CdiContainer) Bean(javax.enterprise.inject.spi.Bean)

Example 23 with BeanManager

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

the class ConfigurationTests method testNamedConfiguration.

@SuppressWarnings({ "unchecked", "serial" })
public void testNamedConfiguration() throws Exception {
    Bundle tb3Bundle = installBundle("tb3.jar");
    Configuration configurationA = null, configurationB = null;
    try {
        configurationA = configurationAdmin.getConfiguration("configA", "?");
        Dictionary<String, Object> properties = new Hashtable<>();
        properties.put("ports", new int[] { 12, 4567 });
        configurationA.update(properties);
        configurationB = configurationAdmin.getConfiguration("configB", "?");
        properties = new Hashtable<>();
        properties.put("color", "green");
        properties.put("ports", new int[] { 80 });
        configurationB.update(properties);
        Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=CREATED))");
        ServiceTracker<CdiContainer, CdiContainer> st = new ServiceTracker<>(bundleContext, filter, null);
        st.open();
        CdiContainer container = st.waitForService(timeout);
        assertNotNull(container);
        int t = st.getTrackingCount();
        BeanManager beanManager = container.getBeanManager();
        Set<Bean<?>> beans = beanManager.getBeans("configB");
        assertNotNull(beans);
        Bean<? extends Object> bean = beanManager.resolve(beans);
        CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
        Map<String, Object> config = (Map<String, Object>) beanManager.getReference(bean, new TypeLiteral<Map<String, Object>>() {
        }.getType(), ctx);
        assertNotNull(config);
        assertEquals("green", config.get("color"));
        assertArrayEquals(new int[] { 80 }, (int[]) config.get("ports"));
        configurationA.delete();
        while (t == st.getTrackingCount()) {
            Thread.sleep(10);
        }
        assertTrue(st.isEmpty());
        st.close();
        filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=" + CdiEvent.Type.WAITING_FOR_CONFIGURATIONS + "))");
        st = new ServiceTracker<>(bundleContext, filter, null);
        st.open();
        assertFalse(st.isEmpty());
    } finally {
        if (configurationB != null) {
            try {
                configurationB.delete();
            } catch (Exception e) {
            // ignore
            }
        }
        tb3Bundle.uninstall();
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Bean(javax.enterprise.inject.spi.Bean) Filter(org.osgi.framework.Filter) BeanManager(javax.enterprise.inject.spi.BeanManager) Map(java.util.Map) CdiContainer(org.osgi.service.cdi.CdiContainer)

Example 24 with BeanManager

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

the class EventsTests method testEventsGetSent.

public void testEventsGetSent() throws Exception {
    BeanManager beanManager = cdiContainer.getBeanManager();
    assertNotNull(beanManager);
    @SuppressWarnings("serial") Set<Bean<?>> beans = beanManager.getBeans(Object.class, new AnnotationLiteral<CdiEventObserverQualifier>() {
    });
    Bean<?> bean = beanManager.resolve(beans);
    CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
    Object bcb = beanManager.getReference(bean, Object.class, ctx);
    assertNotNull(bcb);
    @SuppressWarnings("unchecked") BeanService<List<CdiEvent>> bti = (BeanService<List<CdiEvent>>) bcb;
    List<CdiEvent> list = bti.get();
    assertNotNull(list);
    assertEquals(3, list.size());
}
Also used : Bean(javax.enterprise.inject.spi.Bean) List(java.util.List) CdiEvent(org.osgi.service.cdi.CdiEvent) BeanManager(javax.enterprise.inject.spi.BeanManager) CdiEventObserverQualifier(org.apache.aries.cdi.test.interfaces.CdiEventObserverQualifier) BeanService(org.apache.aries.cdi.test.interfaces.BeanService)

Example 25 with BeanManager

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

the class JndiExtensionTests method testGetBeanManagerThroughJNDI.

public void testGetBeanManagerThroughJNDI() throws Exception {
    Hashtable<String, Object> env = new Hashtable<>();
    env.put(JNDIConstants.BUNDLE_CONTEXT, cdiBundle.getBundleContext());
    InitialContext context = new InitialContext(env);
    BeanManager beanManager = (BeanManager) context.lookup("java:comp/BeanManager");
    assertNotNull(beanManager);
    assertPojoExists(beanManager);
}
Also used : Hashtable(java.util.Hashtable) BeanManager(javax.enterprise.inject.spi.BeanManager) InitialContext(javax.naming.InitialContext)

Aggregations

BeanManager (javax.enterprise.inject.spi.BeanManager)83 Bean (javax.enterprise.inject.spi.Bean)34 Test (org.junit.Test)16 NamingException (javax.naming.NamingException)7 Annotation (java.lang.annotation.Annotation)6 ArrayList (java.util.ArrayList)6 InjectionTarget (javax.enterprise.inject.spi.InjectionTarget)6 CdiContainer (org.apache.deltaspike.cdise.api.CdiContainer)6 CreationalContext (javax.enterprise.context.spi.CreationalContext)5 InitialContext (javax.naming.InitialContext)5 ValidatorFactory (javax.validation.ValidatorFactory)5 CarRepair (org.apache.deltaspike.cdise.tck.beans.CarRepair)5 HashMap (java.util.HashMap)4 Set (java.util.Set)4 TransactionManager (javax.transaction.TransactionManager)4 ContextControl (org.apache.deltaspike.cdise.api.ContextControl)4 Type (java.lang.reflect.Type)3 Map (java.util.Map)3 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)3 Validator (javax.validation.Validator)3