use of org.osgi.service.component.ComponentInstance in project felix by apache.
the class PersistentComponentFactoryTest method test_component_factory.
@Test
public void test_component_factory() throws Exception {
final String componentname = "factory.component";
final String componentfactory = "factory.component.factory";
getConfigurationsDisabledThenEnable(componentname, 0, -1);
TestCase.assertNull(SimpleComponent.INSTANCE);
final ComponentInstance instance = createFactoryComponentInstance(componentfactory);
// 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);
}
use of org.osgi.service.component.ComponentInstance 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();
}
use of org.osgi.service.component.ComponentInstance 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());
}
use of org.osgi.service.component.ComponentInstance in project felix by apache.
the class PersistentComponentFactoryTest method test_component_factory_newInstance_failure.
@Test
public void test_component_factory_newInstance_failure() throws Exception {
final String componentname = "factory.component";
final String componentfactory = "factory.component.factory";
getConfigurationsDisabledThenEnable(componentname, 0, -1);
TestCase.assertNull(SimpleComponent.INSTANCE);
Hashtable<String, String> props = new Hashtable<String, String>();
props.put(PROP_NAME_FACTORY, PROP_NAME_FACTORY);
props.put(SimpleComponent.PROP_ACTIVATE_FAILURE, "Requested Failure");
final ComponentFactory factory = getComponentFactory(componentfactory);
final ComponentInstance instance = factory.newInstance(props);
TestCase.assertNotNull(instance);
checkConfigurationCount(componentname, 1, ComponentConfigurationDTO.SATISFIED);
}
use of org.osgi.service.component.ComponentInstance in project felix by apache.
the class CFactory method run.
public void run() {
while (true) {
try {
ComponentInstance ci = _cFactory.newInstance(null);
ci.dispose();
if (Thread.currentThread().isInterrupted()) {
return;
}
} catch (Throwable t) {
if (!(t instanceof InterruptedException)) {
// System.out.println("CFactory thread exiting: got exception: " + t.toString());
}
return;
}
}
}
Aggregations