Search in sources :

Example 11 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project sling by apache.

the class ITConfigPrinter method waitForPrinter.

private void waitForPrinter() throws InterruptedException {
    if (configPrinter == null) {
        tracker = new ServiceTracker(bundleContext, "org.apache.sling.commons.log.logback.internal.SlingConfigurationPrinter", null);
        tracker.open();
        configPrinter = tracker.waitForService(0);
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker)

Example 12 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project aries by apache.

the class OSGiBeanDescriptorTests method testServices.

public void testServices() throws Exception {
    Bundle tb2Bundle = installBundle("tb2.jar");
    ServiceTracker<Pojo, Pojo> st = new ServiceTracker<Pojo, Pojo>(bundleContext, Pojo.class, null);
    st.open(true);
    try {
        Pojo pojo = st.waitForService(timeout);
        assertNotNull(pojo);
    } finally {
        tb2Bundle.uninstall();
    }
}
Also used : Pojo(org.apache.aries.cdi.test.interfaces.Pojo) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Bundle(org.osgi.framework.Bundle)

Example 13 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker in project aries by apache.

the class ComponentTest method testComponent.

@Test
public void testComponent() throws IOException {
    OSGi<?> program = configurations("org.components.MyComponent").flatMap(props -> services(Service.class).flatMap(ms -> just(new Component(props, ms)).flatMap(component -> register(Component.class, component, new HashMap<>()).distribute(ign -> dynamic(highestService(ServiceOptional.class), component::setOptional, c -> component.setOptional(null)), ign -> dynamic(services(ServiceForList.class), component::addService, component::removeService)))));
    ServiceTracker<Component, Component> serviceTracker = new ServiceTracker<>(_bundleContext, Component.class, null);
    serviceTracker.open();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    _bundleContext.registerService(ManagedService.class, dictionary -> countDownLatch.countDown(), new Hashtable<String, Object>() {

        {
            put("service.pid", "org.components.MyComponent");
        }
    });
    Configuration factoryConfiguration = null;
    try (OSGiResult<?> run = program.run(_bundleContext)) {
        factoryConfiguration = _configurationAdmin.createFactoryConfiguration("org.components.MyComponent");
        factoryConfiguration.update(new Hashtable<>());
        countDownLatch.await(10, TimeUnit.SECONDS);
        assertNull(serviceTracker.getService());
        ServiceRegistration<Service> serviceRegistration = _bundleContext.registerService(Service.class, new Service(), new Hashtable<>());
        Component component = serviceTracker.waitForService(10 * 1000);
        assertNotNull(component);
        assertNull(component.getOptional());
        ServiceRegistration<ServiceOptional> serviceRegistration2 = _bundleContext.registerService(ServiceOptional.class, new ServiceOptional(), new Hashtable<>());
        Thread.sleep(1000L);
        assertNotNull(component.getOptional());
        ServiceOptional serviceOptional = new ServiceOptional();
        ServiceRegistration<ServiceOptional> serviceRegistration3 = _bundleContext.registerService(ServiceOptional.class, serviceOptional, new Hashtable<String, Object>() {

            {
                put("service.ranking", 1);
            }
        });
        assertEquals(serviceOptional, component.getOptional());
        serviceRegistration3.unregister();
        assertNotNull(component.getOptional());
        serviceRegistration2.unregister();
        assertNull(component.getOptional());
        ServiceRegistration<ServiceForList> serviceRegistration4 = _bundleContext.registerService(ServiceForList.class, new ServiceForList(), new Hashtable<>());
        ServiceRegistration<ServiceForList> serviceRegistration5 = _bundleContext.registerService(ServiceForList.class, new ServiceForList(), new Hashtable<>());
        assertEquals(2, component.getServiceForLists().size());
        serviceRegistration4.unregister();
        assertEquals(1, component.getServiceForLists().size());
        serviceRegistration5.unregister();
        assertEquals(0, component.getServiceForLists().size());
        serviceRegistration.unregister();
        assertNull(serviceTracker.getService());
    } catch (IOException ioe) {
    } catch (InterruptedException e) {
        Assert.fail("Timeout waiting for configuration");
    } finally {
        serviceTracker.close();
        if (factoryConfiguration != null) {
            factoryConfiguration.delete();
        }
    }
}
Also used : OSGi.configurations(org.apache.aries.osgi.functional.OSGi.configurations) BeforeClass(org.junit.BeforeClass) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) OSGi.onClose(org.apache.aries.osgi.functional.OSGi.onClose) ArrayList(java.util.ArrayList) OSGi.register(org.apache.aries.osgi.functional.OSGi.register) HighestRankingRouter.highest(org.apache.aries.osgi.functional.test.HighestRankingRouter.highest) Configuration(org.osgi.service.cm.Configuration) OSGi.apply(org.apache.aries.osgi.functional.OSGi.apply) ServiceReference(org.osgi.framework.ServiceReference) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration) AfterClass(org.junit.AfterClass) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) IOException(java.io.IOException) BundleContext(org.osgi.framework.BundleContext) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) OSGi.just(org.apache.aries.osgi.functional.OSGi.just) Assert.assertNull(org.junit.Assert.assertNull) OSGi.services(org.apache.aries.osgi.functional.OSGi.services) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) OSGi(org.apache.aries.osgi.functional.OSGi) ManagedService(org.osgi.service.cm.ManagedService) Assert(org.junit.Assert) OSGi.bundleContext(org.apache.aries.osgi.functional.OSGi.bundleContext) FrameworkUtil(org.osgi.framework.FrameworkUtil) OSGiResult(org.apache.aries.osgi.functional.OSGiResult) Dictionary(java.util.Dictionary) Assert.assertEquals(org.junit.Assert.assertEquals) Configuration(org.osgi.service.cm.Configuration) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ManagedService(org.osgi.service.cm.ManagedService) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 14 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker 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 15 with ServiceTracker

use of org.osgi.util.tracker.ServiceTracker 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

ServiceTracker (org.osgi.util.tracker.ServiceTracker)321 ServiceReference (org.osgi.framework.ServiceReference)65 Filter (org.osgi.framework.Filter)41 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)32 BundleContext (org.osgi.framework.BundleContext)28 Hashtable (java.util.Hashtable)26 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)23 Activate (org.apache.felix.scr.annotations.Activate)20 IContainerManager (org.eclipse.ecf.core.IContainerManager)20 Bundle (org.osgi.framework.Bundle)19 LogService (org.osgi.service.log.LogService)17 Dictionary (java.util.Dictionary)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 IOException (java.io.IOException)11 Test (org.junit.Test)10 ConfigurationException (org.osgi.service.cm.ConfigurationException)10 Map (java.util.Map)7 List (java.util.List)6 Properties (java.util.Properties)6