Search in sources :

Example 11 with ComponentFactory

use of org.osgi.service.component.ComponentFactory in project felix by apache.

the class ServiceBindGreedyTest method test_required_multiple_dynamic_factory.

@Test
public void test_required_multiple_dynamic_factory() throws Exception {
    // also pid
    String name = "test_required_multiple_dynamic_factory";
    final String factoryPid = "factory_" + name;
    getConfigurationsDisabledThenEnable(name, 0, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    // final String pid = "test_required_multiple_dynamic_factory";
    // 
    // final Component component = findComponentDescriptorByName( pid );
    // TestCase.assertNotNull( component );
    // TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
    // 
    // // async enabling (unsatisfied)
    // enableAndCheck(cc.description);
    // delay();
    // findComponentConfigurationByName(name, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    // register service, satisfying
    final SimpleServiceImpl srv1 = SimpleServiceImpl.create(bundleContext, "srv1");
    delay();
    // TestCase.assertEquals( Component.STATE_FACTORY, component.getState() );
    // create a component instance
    final ServiceReference[] refs = bundleContext.getServiceReferences(ComponentFactory.class.getName(), "(" + ComponentConstants.COMPONENT_FACTORY + "=" + factoryPid + ")");
    TestCase.assertNotNull(refs);
    TestCase.assertEquals(1, refs.length);
    final ComponentFactory factory = (ComponentFactory) bundleContext.getService(refs[0]);
    TestCase.assertNotNull(factory);
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(PROP_NAME_FACTORY, PROP_NAME_FACTORY);
    final ComponentInstance instance = factory.newInstance(props);
    TestCase.assertNotNull(instance);
    TestCase.assertNotNull(instance.getInstance());
    TestCase.assertEquals(SimpleComponent.INSTANCE, instance.getInstance());
    // ensure instance is bound
    final SimpleComponent sc = SimpleComponent.INSTANCE;
    TestCase.assertEquals(1, sc.m_multiRef.size());
    TestCase.assertTrue(sc.m_multiRef.contains(srv1));
    // ensure factory is not bound
    // TestCase.assertNull( component.getReferences()[0].getServiceReferences() );
    // assert two components managed
    checkConfigurationCount(name, 1, ComponentConfigurationDTO.ACTIVE);
    // final Component[] allFactoryComponents = findComponentConfigurationsByName( pid, -1 );
    // TestCase.assertNotNull( allFactoryComponents );
    // TestCase.assertEquals( 2, allFactoryComponents.length );
    // for ( int i = 0; i < allFactoryComponents.length; i++ )
    // {
    // final Component c = allFactoryComponents[i];
    // if ( c.getId() == component.getId() )
    // {
    // TestCase.assertEquals( Component.STATE_FACTORY, c.getState() );
    // }
    // else if ( c.getId() == SimpleComponent.INSTANCE.m_id )
    // {
    // TestCase.assertEquals( Component.STATE_ACTIVE, c.getState() );
    // }
    // else
    // {
    // TestCase.fail( "Unexpected Component " + c );
    // }
    // }
    // register second service
    final SimpleServiceImpl srv11 = SimpleServiceImpl.create(bundleContext, "srv11");
    delay();
    // ensure instance is bound
    TestCase.assertEquals(2, sc.m_multiRef.size());
    TestCase.assertTrue(sc.m_multiRef.contains(srv1));
    TestCase.assertTrue(sc.m_multiRef.contains(srv11));
    // ensure factory is not bound
    // TestCase.assertNull( component.getReferences()[0].getServiceReferences() );
    // drop second service and ensure unbound (and active)
    srv11.drop();
    delay();
    TestCase.assertNotNull(instance.getInstance());
    TestCase.assertEquals(SimpleComponent.INSTANCE, instance.getInstance());
    TestCase.assertEquals(1, sc.m_multiRef.size());
    TestCase.assertTrue(sc.m_multiRef.contains(srv1));
    // TestCase.assertNull( component.getReferences()[0].getServiceReferences() );
    // remove the service, expect factory to deactivate and instance to dispose
    srv1.drop();
    delay();
    checkConfigurationCount(name, 0, -1);
    TestCase.assertNull(instance.getInstance());
    // assert component factory only managed
    // TODO this check should be whether the service is registered.
    // final Component[] allFactoryComponents2 = findComponentConfigurationsByName( pid, -1 );
    // TestCase.assertNotNull( allFactoryComponents2 );
    // TestCase.assertEquals( 1, allFactoryComponents2.length );
    // for ( int i = 0; i < allFactoryComponents2.length; i++ )
    // {
    // final Component c = allFactoryComponents2[i];
    // if ( c.getId() == component.getId() )
    // {
    // TestCase.assertEquals( Component.STATE_UNSATISFIED, c.getState() );
    // }
    // else
    // {
    // TestCase.fail( "Unexpected Component " + c );
    // }
    // }
    // registeranother service, factory must come back, instance not
    final SimpleServiceImpl srv2 = SimpleServiceImpl.create(bundleContext, "srv2");
    delay();
    // TestCase.assertEquals( Component.STATE_FACTORY, component.getState() );
    TestCase.assertNull(instance.getInstance());
    // assert component factory only managed
    checkConfigurationCount(name, 0, -1);
// final Component[] allFactoryComponents3 = findComponentConfigurationsByName( pid, -1 );
// TestCase.assertNotNull( allFactoryComponents3 );
// TestCase.assertEquals( 1, allFactoryComponents3.length );
// for ( int i = 0; i < allFactoryComponents3.length; i++ )
// {
// final Component c = allFactoryComponents3[i];
// if ( c.getId() == component.getId() )
// {
// TestCase.assertEquals( Component.STATE_FACTORY, c.getState() );
// }
// else
// {
// TestCase.fail( "Unexpected Component " + c );
// }
// }
}
Also used : Hashtable(java.util.Hashtable) ComponentFactory(org.osgi.service.component.ComponentFactory) ComponentInstance(org.osgi.service.component.ComponentInstance) SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 12 with ComponentFactory

use of org.osgi.service.component.ComponentFactory in project felix by apache.

the class ServiceBindTest method test_required_multiple_dynamic_factory.

@Test
public void test_required_multiple_dynamic_factory() throws Exception {
    // also pid
    String name = "test_required_multiple_dynamic_factory";
    final String factoryPid = "factory_" + name;
    getConfigurationsDisabledThenEnable(name, 0, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    // final String pid = "test_required_multiple_dynamic_factory";
    // 
    // final Component component = findComponentDescriptorByName( pid );
    // TestCase.assertNotNull( component );
    // TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
    // 
    // // async enabling (unsatisfied)
    // enableAndCheck(cc.description);
    // delay();
    // findComponentConfigurationByName(name, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    // register service, satisfying
    final SimpleServiceImpl srv1 = SimpleServiceImpl.create(bundleContext, "srv1");
    delay();
    // TestCase.assertEquals( Component.STATE_FACTORY, component.getState() );
    // create a component instance
    final ServiceReference[] refs = bundleContext.getServiceReferences(ComponentFactory.class.getName(), "(" + ComponentConstants.COMPONENT_FACTORY + "=" + factoryPid + ")");
    TestCase.assertNotNull(refs);
    TestCase.assertEquals(1, refs.length);
    final ComponentFactory factory = (ComponentFactory) bundleContext.getService(refs[0]);
    TestCase.assertNotNull(factory);
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(PROP_NAME_FACTORY, PROP_NAME_FACTORY);
    final ComponentInstance instance = factory.newInstance(props);
    TestCase.assertNotNull(instance);
    TestCase.assertNotNull(instance.getInstance());
    TestCase.assertEquals(SimpleComponent.INSTANCE, instance.getInstance());
    // ensure instance is bound
    final SimpleComponent sc = SimpleComponent.INSTANCE;
    TestCase.assertEquals(1, sc.m_multiRef.size());
    TestCase.assertTrue(sc.m_multiRef.contains(srv1));
    // ensure factory is not bound
    // TestCase.assertNull( component.getReferences()[0].getServiceReferences() );
    // assert two components managed
    checkConfigurationCount(name, 1, ComponentConfigurationDTO.ACTIVE);
    // final Component[] allFactoryComponents = findComponentConfigurationsByName( pid, -1 );
    // TestCase.assertNotNull( allFactoryComponents );
    // TestCase.assertEquals( 2, allFactoryComponents.length );
    // for ( int i = 0; i < allFactoryComponents.length; i++ )
    // {
    // final Component c = allFactoryComponents[i];
    // if ( c.getId() == component.getId() )
    // {
    // TestCase.assertEquals( Component.STATE_FACTORY, c.getState() );
    // }
    // else if ( c.getId() == SimpleComponent.INSTANCE.m_id )
    // {
    // TestCase.assertEquals( Component.STATE_ACTIVE, c.getState() );
    // }
    // else
    // {
    // TestCase.fail( "Unexpected Component " + c );
    // }
    // }
    // register second service
    final SimpleServiceImpl srv11 = SimpleServiceImpl.create(bundleContext, "srv11");
    delay();
    // ensure instance is bound
    TestCase.assertEquals(2, sc.m_multiRef.size());
    TestCase.assertTrue(sc.m_multiRef.contains(srv1));
    TestCase.assertTrue(sc.m_multiRef.contains(srv11));
    // ensure factory is not bound
    // TestCase.assertNull( component.getReferences()[0].getServiceReferences() );
    // drop second service and ensure unbound (and active)
    srv11.drop();
    delay();
    TestCase.assertNotNull(instance.getInstance());
    TestCase.assertEquals(SimpleComponent.INSTANCE, instance.getInstance());
    TestCase.assertEquals(1, sc.m_multiRef.size());
    TestCase.assertTrue(sc.m_multiRef.contains(srv1));
    // TestCase.assertNull( component.getReferences()[0].getServiceReferences() );
    // remove the service, expect factory to deactivate and instance to dispose
    srv1.drop();
    delay();
    checkConfigurationCount(name, 0, -1);
    TestCase.assertNull(instance.getInstance());
    // assert component factory only managed
    // TODO this check should be whether the service is registered.
    // final Component[] allFactoryComponents2 = findComponentConfigurationsByName( pid, -1 );
    // TestCase.assertNotNull( allFactoryComponents2 );
    // TestCase.assertEquals( 1, allFactoryComponents2.length );
    // for ( int i = 0; i < allFactoryComponents2.length; i++ )
    // {
    // final Component c = allFactoryComponents2[i];
    // if ( c.getId() == component.getId() )
    // {
    // TestCase.assertEquals( Component.STATE_UNSATISFIED, c.getState() );
    // }
    // else
    // {
    // TestCase.fail( "Unexpected Component " + c );
    // }
    // }
    // registeranother service, factory must come back, instance not
    final SimpleServiceImpl srv2 = SimpleServiceImpl.create(bundleContext, "srv2");
    delay();
    // TestCase.assertEquals( Component.STATE_FACTORY, component.getState() );
    TestCase.assertNull(instance.getInstance());
    // assert component factory only managed
    checkConfigurationCount(name, 0, -1);
// final Component[] allFactoryComponents3 = findComponentConfigurationsByName( pid, -1 );
// TestCase.assertNotNull( allFactoryComponents3 );
// TestCase.assertEquals( 1, allFactoryComponents3.length );
// for ( int i = 0; i < allFactoryComponents3.length; i++ )
// {
// final Component c = allFactoryComponents3[i];
// if ( c.getId() == component.getId() )
// {
// TestCase.assertEquals( Component.STATE_FACTORY, c.getState() );
// }
// else
// {
// TestCase.fail( "Unexpected Component " + c );
// }
// }
}
Also used : Hashtable(java.util.Hashtable) ComponentFactory(org.osgi.service.component.ComponentFactory) ComponentInstance(org.osgi.service.component.ComponentInstance) SimpleComponent(org.apache.felix.scr.integration.components.SimpleComponent) SimpleServiceImpl(org.apache.felix.scr.integration.components.SimpleServiceImpl) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

ComponentFactory (org.osgi.service.component.ComponentFactory)12 ComponentInstance (org.osgi.service.component.ComponentInstance)10 Hashtable (java.util.Hashtable)9 Test (org.junit.Test)8 SimpleServiceImpl (org.apache.felix.scr.integration.components.SimpleServiceImpl)5 ServiceReference (org.osgi.framework.ServiceReference)4 SimpleComponent (org.apache.felix.scr.integration.components.SimpleComponent)3 SimpleService (org.apache.felix.scr.integration.components.SimpleService)3 Configuration (org.osgi.service.cm.Configuration)1 ComponentException (org.osgi.service.component.ComponentException)1