Search in sources :

Example 1 with CdiContainer

use of org.osgi.service.cdi.CdiContainer in project aries by apache.

the class ConfigurationTests method testConfiguration.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testConfiguration() throws Exception {
    Bundle tb3Bundle = installBundle("tb3.jar");
    Configuration configurationA = null, configurationB = null;
    try {
        Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + "))");
        ServiceTracker<CdiContainer, CdiContainer> containerTracker = new ServiceTracker<>(bundleContext, filter, null);
        containerTracker.open();
        containerTracker.waitForService(timeout);
        ServiceReference<CdiContainer> serviceReference = containerTracker.getServiceReference();
        assertNotNull(serviceReference);
        assertEquals(CdiEvent.Type.WAITING_FOR_CONFIGURATIONS, serviceReference.getProperty(CdiConstants.CDI_CONTAINER_STATE));
        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);
        containerTracker.close();
        filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=CREATED))");
        containerTracker = new ServiceTracker<>(bundleContext, filter, null);
        containerTracker.open();
        containerTracker.waitForService(timeout);
        ServiceTracker<BeanService, BeanService> stA = new ServiceTracker<BeanService, BeanService>(bundleContext, bundleContext.createFilter("(&(objectClass=org.apache.aries.cdi.test.interfaces.BeanService)(bean=A))"), null);
        stA.open(true);
        BeanService<Callable<int[]>> beanService = stA.waitForService(timeout);
        assertNotNull(beanService);
        assertEquals("blue", beanService.doSomething());
        assertArrayEquals(new int[] { 12, 4567 }, beanService.get().call());
        ServiceTracker<BeanService, BeanService> stB = new ServiceTracker<BeanService, BeanService>(bundleContext, bundleContext.createFilter("(&(objectClass=org.apache.aries.cdi.test.interfaces.BeanService)(bean=B))"), null);
        stB.open(true);
        beanService = stB.waitForService(timeout);
        assertNotNull(beanService);
        assertEquals("green", beanService.doSomething());
        assertArrayEquals(new int[] { 80 }, beanService.get().call());
    } finally {
        if (configurationA != null) {
            try {
                configurationA.delete();
            } catch (Exception e) {
            // ignore
            }
        }
        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) Callable(java.util.concurrent.Callable) Filter(org.osgi.framework.Filter) BeanService(org.apache.aries.cdi.test.interfaces.BeanService) CdiContainer(org.osgi.service.cdi.CdiContainer)

Example 2 with CdiContainer

use of org.osgi.service.cdi.CdiContainer in project aries by apache.

the class AbstractTestCase method getServiceTracker.

ServiceTracker<CdiContainer, CdiContainer> getServiceTracker(long bundleId) throws InvalidSyntaxException {
    Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + bundleId + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=" + CdiEvent.Type.CREATED + "))");
    ServiceTracker<CdiContainer, CdiContainer> serviceTracker = new ServiceTracker<>(bundleContext, filter, null);
    serviceTracker.open();
    return serviceTracker;
}
Also used : Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) CdiContainer(org.osgi.service.cdi.CdiContainer)

Example 3 with CdiContainer

use of org.osgi.service.cdi.CdiContainer 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 4 with CdiContainer

use of org.osgi.service.cdi.CdiContainer 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 5 with CdiContainer

use of org.osgi.service.cdi.CdiContainer in project aries by apache.

the class ConfigurationTests method testOptionalConfiguration.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testOptionalConfiguration() throws Exception {
    Bundle tb5Bundle = installBundle("tb5.jar");
    Configuration configurationC = null;
    try {
        Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb5Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=CREATED))");
        ServiceTracker<CdiContainer, CdiContainer> containerTracker = new ServiceTracker<>(bundleContext, filter, null);
        containerTracker.open();
        containerTracker.waitForService(timeout);
        ServiceTracker<BeanService, BeanService> stC = new ServiceTracker<BeanService, BeanService>(bundleContext, bundleContext.createFilter("(&(objectClass=org.apache.aries.cdi.test.interfaces.BeanService)(bean=C))"), null);
        stC.open(true);
        BeanService<Callable<int[]>> beanService = stC.waitForService(timeout);
        int t = stC.getTrackingCount();
        assertNotNull(beanService);
        assertEquals("blue", beanService.doSomething());
        assertArrayEquals(new int[] { 35777 }, beanService.get().call());
        configurationC = configurationAdmin.getConfiguration("foo.bar", "?");
        Dictionary<String, Object> properties = new Hashtable<>();
        properties.put("ports", new int[] { 12, 4567 });
        configurationC.update(properties);
        while (t == stC.getTrackingCount()) {
            Thread.sleep(10);
        }
        t = stC.getTrackingCount();
        while (t == stC.getTrackingCount()) {
            Thread.sleep(10);
        }
        t = stC.getTrackingCount();
        beanService = stC.waitForService(timeout);
        assertNotNull(beanService);
        assertEquals("blue", beanService.doSomething());
        assertArrayEquals(new int[] { 12, 4567 }, beanService.get().call());
        configurationC.delete();
        while (t == stC.getTrackingCount()) {
            Thread.sleep(10);
        }
        beanService = stC.waitForService(timeout);
        assertNotNull(beanService);
        assertEquals("blue", beanService.doSomething());
        assertArrayEquals(new int[] { 35777 }, beanService.get().call());
    } finally {
        if (configurationC != null) {
            try {
                configurationC.delete();
            } catch (Exception e) {
            // ignore
            }
        }
        tb5Bundle.uninstall();
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Callable(java.util.concurrent.Callable) Filter(org.osgi.framework.Filter) BeanService(org.apache.aries.cdi.test.interfaces.BeanService) CdiContainer(org.osgi.service.cdi.CdiContainer)

Aggregations

CdiContainer (org.osgi.service.cdi.CdiContainer)8 Bundle (org.osgi.framework.Bundle)6 Hashtable (java.util.Hashtable)4 Filter (org.osgi.framework.Filter)4 ServiceTracker (org.osgi.util.tracker.ServiceTracker)4 BeanService (org.apache.aries.cdi.test.interfaces.BeanService)3 Configuration (org.osgi.service.cm.Configuration)3 Callable (java.util.concurrent.Callable)2 Bean (javax.enterprise.inject.spi.Bean)2 BeanManager (javax.enterprise.inject.spi.BeanManager)2 List (java.util.List)1 Map (java.util.Map)1 BundleEvent (org.osgi.framework.BundleEvent)1 ServiceReference (org.osgi.framework.ServiceReference)1 BundleCapability (org.osgi.framework.wiring.BundleCapability)1 Capability (org.osgi.resource.Capability)1 CdiEvent (org.osgi.service.cdi.CdiEvent)1 Type (org.osgi.service.cdi.CdiEvent.Type)1 BundleTracker (org.osgi.util.tracker.BundleTracker)1