Search in sources :

Example 16 with ComponentInstance

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

the class PersistentComponentFactoryTest method test_component_factory_referredTo.

@Test
public void test_component_factory_referredTo() throws Exception {
    // set up the component that refers to the service the factory will create.
    final String referringComponentName = "ComponentReferringToFactoryObject";
    getConfigurationsDisabledThenEnable(referringComponentName, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
    final String componentname = "factory.component.referred";
    final String componentfactory = "factory.component.factory.referred";
    getConfigurationsDisabledThenEnable(componentname, 0, -1);
    TestCase.assertNull(SimpleComponent.INSTANCE);
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put("service.pid", "myFactoryInstance");
    final ComponentFactory factory = getComponentFactory(componentfactory);
    final ComponentInstance instance = factory.newInstance(props);
    TestCase.assertNotNull(instance);
    TestCase.assertNotNull(instance.getInstance());
    TestCase.assertTrue(instance.getInstance() instanceof SimpleService);
    // The referring service should now be active
    checkConfigurationCount(referringComponentName, 1, ComponentConfigurationDTO.ACTIVE);
    instance.dispose();
    // SCR 112.12.6.2
    TestCase.assertNull(instance.getInstance());
    // make sure it's unsatisfied (service is no longer available)
    checkConfigurationCount(referringComponentName, 1, ComponentConfigurationDTO.UNSATISFIED_REFERENCE);
}
Also used : Hashtable(java.util.Hashtable) ComponentFactory(org.osgi.service.component.ComponentFactory) ComponentInstance(org.osgi.service.component.ComponentInstance) SimpleService(org.apache.felix.scr.integration.components.SimpleService) Test(org.junit.Test)

Example 17 with ComponentInstance

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

the class PersistentComponentFactoryTest method test_component_factory_disable_factory.

@Test
public void test_component_factory_disable_factory() throws Exception {
    // tests components remain alive after factory has been disabled
    final String componentname = "factory.component";
    final String componentfactory = "factory.component.factory";
    getConfigurationsDisabledThenEnable(componentname, 0, -1);
    TestCase.assertNull(SimpleComponent.INSTANCE);
    final ComponentInstance instance = createFactoryComponentInstance(componentfactory);
    checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.ACTIVE);
    // disable the factory
    disableAndCheck(componentname);
    delay();
    // factory is disabled but the instance is still alive
    TestCase.assertNotNull(SimpleComponent.INSTANCE);
    instance.dispose();
    TestCase.assertNull(SimpleComponent.INSTANCE);
    // SCR 112.12.6.2
    TestCase.assertNull(instance.getInstance());
}
Also used : ComponentInstance(org.osgi.service.component.ComponentInstance) Test(org.junit.Test)

Example 18 with ComponentInstance

use of org.osgi.service.component.ComponentInstance 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 19 with ComponentInstance

use of org.osgi.service.component.ComponentInstance 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)

Example 20 with ComponentInstance

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

the class AFactory method run.

public void run() {
    while (true) {
        try {
            // System.out.println("Creating A");
            ComponentInstance ci = _aFactory.newInstance(null);
            ci.dispose();
            if (Thread.currentThread().isInterrupted()) {
                return;
            }
        } catch (Throwable t) {
            if (!(t instanceof InterruptedException)) {
            // System.out.println("AFactory thread exiting: got exception: " + t.toString());
            }
            return;
        }
    }
}
Also used : ComponentInstance(org.osgi.service.component.ComponentInstance)

Aggregations

ComponentInstance (org.osgi.service.component.ComponentInstance)21 Test (org.junit.Test)13 Hashtable (java.util.Hashtable)11 ComponentFactory (org.osgi.service.component.ComponentFactory)10 SimpleServiceImpl (org.apache.felix.scr.integration.components.SimpleServiceImpl)7 SimpleComponent (org.apache.felix.scr.integration.components.SimpleComponent)3 SimpleService (org.apache.felix.scr.integration.components.SimpleService)3 ServiceReference (org.osgi.framework.ServiceReference)3 ComponentException (org.osgi.service.component.ComponentException)2 ExtFactoryComponentInstance (org.apache.felix.scr.component.ExtFactoryComponentInstance)1