Search in sources :

Example 6 with SimpleServiceImpl

use of org.apache.felix.scr.integration.components.SimpleServiceImpl in project felix by apache.

the class MinimumCardinalityTest method required.

private void required(ServiceTracker<SimpleComponent, SimpleComponent> tracker, int count) {
    delay();
    List<SimpleServiceImpl> services = new ArrayList<SimpleServiceImpl>();
    for (int i = 0; i < count; i++) {
        assertNull("Expected no tracked with " + i + " services present, count " + count, tracker.getService());
        services.add(SimpleServiceImpl.create(bundleContext, String.valueOf(i)));
    }
    assertNotNull(tracker.getService());
    for (SimpleServiceImpl service : services) {
        service.drop();
        assertNull(tracker.getService());
    }
}
Also used : ArrayList(java.util.ArrayList) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl)

Example 7 with SimpleServiceImpl

use of org.apache.felix.scr.integration.components.SimpleServiceImpl in project felix by apache.

the class PersistentComponentFactoryTest method test_component_factory_with_target_filters.

@Test
public void test_component_factory_with_target_filters() throws Exception {
    final String componentfactory = "factory.component.reference.targetfilter";
    getConfigurationsDisabledThenEnable(componentfactory, 0, -1);
    SimpleServiceImpl s1 = SimpleServiceImpl.create(bundleContext, "service1");
    SimpleServiceImpl s2 = SimpleServiceImpl.create(bundleContext, "service2");
    // supply configuration now and ensure active
    configure(componentfactory);
    delay();
    TestCase.assertNull(SimpleComponent.INSTANCE);
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(PROP_NAME_FACTORY, PROP_NAME_FACTORY);
    props.put("ref.target", "(value=service2)");
    final ComponentInstance instance = createFactoryComponentInstance(componentfactory, props);
    log.log(LogService.LOG_WARNING, "Bound Services: " + SimpleComponent.INSTANCE.m_multiRef);
    TestCase.assertFalse(SimpleComponent.INSTANCE.m_multiRef.contains(s1));
    TestCase.assertTrue(SimpleComponent.INSTANCE.m_multiRef.contains(s2));
    instance.dispose();
    TestCase.assertNull(SimpleComponent.INSTANCE);
    // SCR 112.12.6.2
    TestCase.assertNull(instance.getInstance());
    s2.drop();
    s1.drop();
}
Also used : Hashtable(java.util.Hashtable) ComponentInstance(org.osgi.service.component.ComponentInstance) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl) Test(org.junit.Test)

Example 8 with SimpleServiceImpl

use of org.apache.felix.scr.integration.components.SimpleServiceImpl in project felix by apache.

the class PersistentComponentFactoryTest method test_component_factory_reference.

@Test
public void test_component_factory_reference() throws Exception {
    final String componentname = "factory.component.reference";
    final String componentfactory = "factory.component.factory.reference";
    SimpleServiceImpl.create(bundleContext, "ignored").setFilterProperty("ignored");
    getConfigurationsDisabledThenEnable(componentname, 0, -1);
    TestCase.assertNull(SimpleComponent.INSTANCE);
    // register a service : filterprop=match
    SimpleServiceImpl match = SimpleServiceImpl.create(bundleContext, "required").setFilterProperty("required");
    delay();
    TestCase.assertNull(SimpleComponent.INSTANCE);
    final ComponentInstance instance = createFactoryComponentInstance(componentfactory);
    TestCase.assertEquals(1, SimpleComponent.INSTANCE.m_multiRef.size());
    TestCase.assertTrue(SimpleComponent.INSTANCE.m_multiRef.contains(match));
    // check registered components
    checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
    instance.dispose();
    TestCase.assertNull(SimpleComponent.INSTANCE);
    // SCR 112.12.6.2
    TestCase.assertNull(instance.getInstance());
    checkConfigurationCount(componentname, 0, ComponentConfigurationDTO.ACTIVE);
    // overwritten filterprop
    Hashtable<String, String> propsNonMatch = new Hashtable<String, String>();
    propsNonMatch.put(PROP_NAME_FACTORY, PROP_NAME_FACTORY);
    propsNonMatch.put("ref.target", "(filterprop=nomatch)");
    ComponentFactory factory = getComponentFactory(componentfactory);
    // works even without required reference
    final ComponentInstance instanceNonMatch = factory.newInstance(propsNonMatch);
    checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    final SimpleServiceImpl noMatch = SimpleServiceImpl.create(bundleContext, "nomatch").setFilterProperty("nomatch");
    delay();
    TestCase.assertNotNull(instanceNonMatch.getInstance());
    TestCase.assertEquals(SimpleComponent.INSTANCE, instanceNonMatch.getInstance());
    TestCase.assertEquals(PROP_NAME_FACTORY, SimpleComponent.INSTANCE.getProperty(PROP_NAME_FACTORY));
    TestCase.assertEquals(1, SimpleComponent.INSTANCE.m_multiRef.size());
    TestCase.assertTrue(SimpleComponent.INSTANCE.m_multiRef.contains(noMatch));
    // check registered components
    checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
    match.getRegistration().unregister();
    delay();
    // check registered components (ComponentFactory is still present)
    checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
    // it has already been deactivated.... this should cause an exception?
    noMatch.getRegistration().unregister();
    delay();
    // check registered components (ComponentFactory is still present)
    checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    // deactivated due to unsatisfied reference
    TestCase.assertNull(instanceNonMatch.getInstance());
    TestCase.assertNull(SimpleComponent.INSTANCE);
    // Check that calling dispose on a deactivated instance has no effect
    instanceNonMatch.dispose();
    TestCase.assertNull(SimpleComponent.INSTANCE);
    // SCR 112.12.6.2
    TestCase.assertNull(instanceNonMatch.getInstance());
}
Also used : Hashtable(java.util.Hashtable) ComponentFactory(org.osgi.service.component.ComponentFactory) ComponentInstance(org.osgi.service.component.ComponentInstance) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl) Test(org.junit.Test)

Example 9 with SimpleServiceImpl

use of org.apache.felix.scr.integration.components.SimpleServiceImpl in project felix by apache.

the class ServiceBindGreedyTest method test_required_single_static.

@Test
public void test_required_single_static() throws Exception {
    final SimpleServiceImpl srv1 = SimpleServiceImpl.create(bundleContext, "srv1");
    String name = "test_required_single_static";
    ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp10 = SimpleComponent.INSTANCE;
    TestCase.assertNotNull(comp10);
    TestCase.assertEquals(srv1, comp10.m_singleRef);
    TestCase.assertTrue(comp10.m_multiRef.isEmpty());
    srv1.drop();
    // async reactivate
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    final SimpleComponent comp11 = SimpleComponent.INSTANCE;
    TestCase.assertNull(comp11);
    final SimpleServiceImpl srv2 = SimpleServiceImpl.create(bundleContext, "srv2");
    // async binding
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp12 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp10, comp12);
    TestCase.assertEquals(srv2, comp12.m_singleRef);
    TestCase.assertTrue(comp12.m_multiRef.isEmpty());
    disableAndCheck(cc);
    // async disabling
    delay();
    final SimpleServiceImpl srv3 = SimpleServiceImpl.create(bundleContext, "srv3");
    // enable component with two services available, expect srv2 bind
    // async enabling
    enableAndCheck(cc.description);
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp20 = SimpleComponent.INSTANCE;
    TestCase.assertNotNull(comp20);
    TestCase.assertNotSame(comp10, comp20);
    TestCase.assertEquals(srv2, comp20.m_singleRef);
    TestCase.assertTrue(comp20.m_multiRef.isEmpty());
    // drop srv2, expect rebind to srv3
    srv2.drop();
    // async reactivate
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp21 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp20, comp21);
    TestCase.assertEquals(srv3, comp21.m_singleRef);
    TestCase.assertTrue(comp21.m_multiRef.isEmpty());
    // create srv4, expect no rebind
    final SimpleServiceImpl srv4 = SimpleServiceImpl.create(bundleContext, "srv4");
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp22 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp20, comp22);
    TestCase.assertSame(comp21, comp22);
    TestCase.assertEquals(srv3, comp22.m_singleRef);
    TestCase.assertTrue(comp22.m_multiRef.isEmpty());
    // drop srv4 again, expect no rebind
    srv4.drop();
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp23 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp20, comp23);
    TestCase.assertSame(comp21, comp23);
    TestCase.assertSame(comp22, comp23);
    TestCase.assertEquals(srv3, comp23.m_singleRef);
    TestCase.assertTrue(comp23.m_multiRef.isEmpty());
    // "reset"
    disableAndCheck(cc);
    srv3.drop();
    delay();
    // two services with service ranking (srv6 > srv5)
    final SimpleServiceImpl srv5 = SimpleServiceImpl.create(bundleContext, "srv5", 10);
    final SimpleServiceImpl srv6 = SimpleServiceImpl.create(bundleContext, "srv6", 20);
    enableAndCheck(cc.description);
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp30 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp23, comp30);
    TestCase.assertEquals(srv6, comp30.m_singleRef);
    TestCase.assertTrue(comp30.m_multiRef.isEmpty());
    // another service with higher ranking -- rebind !
    final SimpleServiceImpl srv7 = SimpleServiceImpl.create(bundleContext, "srv7", 30);
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp31 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp30, comp31);
    TestCase.assertEquals(srv7, comp31.m_singleRef);
    TestCase.assertTrue(comp31.m_multiRef.isEmpty());
    // srv6 goes, no rebind to srv7
    srv6.drop();
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp32 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp30, comp32);
    TestCase.assertSame(comp31, comp32);
    TestCase.assertEquals(srv7, comp32.m_singleRef);
    TestCase.assertTrue(comp32.m_multiRef.isEmpty());
}
Also used : SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) ComponentConfigurationDTO(org.osgi.service.component.runtime.dto.ComponentConfigurationDTO) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl) Test(org.junit.Test)

Example 10 with SimpleServiceImpl

use of org.apache.felix.scr.integration.components.SimpleServiceImpl in project felix by apache.

the class ServiceBindGreedyTest method test_optional_multiple_static.

@Test
public void test_optional_multiple_static() throws Exception {
    final SimpleServiceImpl srv1 = SimpleServiceImpl.create(bundleContext, "srv1");
    String name = "test_optional_multiple_static";
    ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp10 = SimpleComponent.INSTANCE;
    TestCase.assertNotNull(comp10);
    TestCase.assertNull(comp10.m_singleRef);
    TestCase.assertTrue(comp10.m_multiRef.contains(srv1));
    srv1.drop();
    // async reactivate
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp11 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp10, comp11);
    TestCase.assertNull(comp11.m_singleRef);
    TestCase.assertTrue(comp11.m_multiRef.isEmpty());
    final SimpleServiceImpl srv2 = SimpleServiceImpl.create(bundleContext, "srv2");
    // async binding
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp12 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp10, comp12);
    TestCase.assertNotSame(comp11, comp12);
    TestCase.assertNull(comp12.m_singleRef);
    TestCase.assertTrue(comp12.m_multiRef.contains(srv2));
    disableAndCheck(cc);
    // async disabling
    delay();
    final SimpleServiceImpl srv3 = SimpleServiceImpl.create(bundleContext, "srv3");
    // enable component with two services available, expect both bind
    // async enabling
    enableAndCheck(cc.description);
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp20 = SimpleComponent.INSTANCE;
    TestCase.assertNotNull(comp20);
    TestCase.assertNotSame(comp10, comp20);
    TestCase.assertNotSame(comp11, comp20);
    TestCase.assertNotSame(comp12, comp20);
    TestCase.assertNull(comp20.m_singleRef);
    TestCase.assertTrue(comp20.m_multiRef.contains(srv2));
    TestCase.assertTrue(comp20.m_multiRef.contains(srv3));
    srv2.drop();
    // async reactivate
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp21 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp20, comp21);
    TestCase.assertNull(comp21.m_singleRef);
    TestCase.assertFalse(comp21.m_multiRef.contains(srv2));
    TestCase.assertTrue(comp21.m_multiRef.contains(srv3));
    // create srv4, expect greedy bind (static case)
    final SimpleServiceImpl srv4 = SimpleServiceImpl.create(bundleContext, "srv4");
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp22 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp20, comp22);
    TestCase.assertNotSame(comp21, comp22);
    TestCase.assertNull(comp22.m_singleRef);
    TestCase.assertFalse(comp22.m_multiRef.contains(srv2));
    TestCase.assertTrue(comp22.m_multiRef.contains(srv3));
    TestCase.assertTrue(comp22.m_multiRef.contains(srv4));
    // drop srv4 again, expect rebind
    srv4.drop();
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp23 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp20, comp23);
    TestCase.assertNotSame(comp21, comp23);
    TestCase.assertNotSame(comp22, comp23);
    TestCase.assertNull(comp23.m_singleRef);
    TestCase.assertFalse(comp23.m_multiRef.contains(srv2));
    TestCase.assertTrue(comp23.m_multiRef.contains(srv3));
    TestCase.assertFalse(comp23.m_multiRef.contains(srv4));
    // "reset"
    disableAndCheck(cc);
    srv3.drop();
    delay();
    // two services with service ranking (srv6 > srv5)
    final SimpleServiceImpl srv5 = SimpleServiceImpl.create(bundleContext, "srv5", 10);
    final SimpleServiceImpl srv6 = SimpleServiceImpl.create(bundleContext, "srv6", 20);
    enableAndCheck(cc.description);
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp30 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp23, comp30);
    TestCase.assertNull(comp30.m_singleRef);
    TestCase.assertTrue(comp30.m_multiRef.contains(srv5));
    TestCase.assertTrue(comp30.m_multiRef.contains(srv6));
    // another service with higher ranking -- greedy rebind !
    final SimpleServiceImpl srv7 = SimpleServiceImpl.create(bundleContext, "srv7", 30);
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp31 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp30, comp31);
    TestCase.assertNull(comp31.m_singleRef);
    TestCase.assertTrue(comp31.m_multiRef.contains(srv5));
    TestCase.assertTrue(comp31.m_multiRef.contains(srv6));
    TestCase.assertTrue(comp31.m_multiRef.contains(srv7));
    // srv6 goes, rebind to srv7
    srv6.drop();
    delay();
    findComponentConfigurationByName(name, ComponentConfigurationDTO.ACTIVE);
    final SimpleComponent comp32 = SimpleComponent.INSTANCE;
    TestCase.assertNotSame(comp30, comp32);
    TestCase.assertNotSame(comp31, comp32);
    TestCase.assertNull(comp32.m_singleRef);
    TestCase.assertTrue(comp32.m_multiRef.contains(srv5));
    TestCase.assertFalse(comp32.m_multiRef.contains(srv6));
    TestCase.assertTrue(comp32.m_multiRef.contains(srv7));
}
Also used : SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) ComponentConfigurationDTO(org.osgi.service.component.runtime.dto.ComponentConfigurationDTO) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl) Test(org.junit.Test)

Aggregations

SimpleServiceImpl (org.apache.felix.scr.integration.components.SimpleServiceImpl)42 Test (org.junit.Test)36 SimpleComponent (org.apache.felix.scr.integration.components.SimpleComponent)32 ComponentConfigurationDTO (org.osgi.service.component.runtime.dto.ComponentConfigurationDTO)22 Hashtable (java.util.Hashtable)7 ComponentInstance (org.osgi.service.component.ComponentInstance)7 ComponentFactory (org.osgi.service.component.ComponentFactory)5 ServiceReference (org.osgi.framework.ServiceReference)4 SimpleComponent2 (org.apache.felix.scr.integration.components.SimpleComponent2)2 SimpleService2Impl (org.apache.felix.scr.integration.components.SimpleService2Impl)2 ArrayList (java.util.ArrayList)1 ActivatorComponent (org.apache.felix.scr.integration.components.ActivatorComponent)1 MutatingService (org.apache.felix.scr.integration.components.MutatingService)1 ComponentException (org.osgi.service.component.ComponentException)1