use of org.apache.felix.dm.Dependency in project felix by apache.
the class RemovedDependencyTest method testRemoveDependencyAndConsumerMustRemainStarted.
public void testRemoveDependencyAndConsumerMustRemainStarted() {
DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// Create two providers
Hashtable props = new Hashtable();
props.put("name", "provider1");
Component sp = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), props);
props = new Properties();
props.put("name", "provider2");
Component sp2 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), props);
props = new Properties();
props.put("name", "provider3");
Component sp3 = m.createComponent().setImplementation(new ServiceProvider(e)).setInterface(ServiceInterface.class.getName(), props);
// Create the consumer, and start it
Dependency d1 = m.createServiceDependency().setService(ServiceInterface.class, "(name=provider1)").setRequired(true).setCallbacks("add", "remove");
Dependency d2 = m.createServiceDependency().setService(ServiceInterface.class, "(name=provider2)").setRequired(true).setCallbacks("add", "remove");
Dependency d3 = m.createServiceDependency().setService(ServiceInterface.class, "(name=provider3)").setRequired(true).setCallbacks("add", "remove");
ServiceConsumer consumer = new ServiceConsumer(e, d3);
Component sc = m.createComponent().setImplementation(consumer).add(d1, d2);
// Add the first two providers and the consumer
m.add(sp);
m.add(sp2);
m.add(sp3);
m.add(sc);
// Check if consumer has been bound to the three providers
e.waitForStep(3, 5000);
Assert.assertEquals(3, consumer.getProvidersCount());
Assert.assertNotNull(consumer.getProvider("provider1"));
Assert.assertNotNull(consumer.getProvider("provider2"));
Assert.assertNotNull(consumer.getProvider("provider3"));
// Now remove the provider2, and check if the consumer is still alive
sc.remove(d2);
Assert.assertFalse(consumer.isStopped());
Assert.assertEquals(2, consumer.getProvidersCount());
Assert.assertNotNull(consumer.getProvider("provider1"));
Assert.assertNull(consumer.getProvider("provider2"));
Assert.assertNotNull(consumer.getProvider("provider3"));
// Now remove the provider3 (the consumer has an instance bound dependency on it), and check if the consumer is still alive
sc.remove(d3);
Assert.assertFalse(consumer.isStopped());
Assert.assertEquals(1, consumer.getProvidersCount());
Assert.assertNotNull(consumer.getProvider("provider1"));
Assert.assertNull(consumer.getProvider("provider2"));
Assert.assertNull(consumer.getProvider("provider3"));
m.clear();
}
use of org.apache.felix.dm.Dependency in project felix by apache.
the class ResourceAdapterDependencyAddAndRemoveTest method testBasicResourceAdapter.
public void testBasicResourceAdapter() throws Exception {
DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create and add a service provider
m.add(m.createComponent().setInterface(ServiceInterface.class.getName(), null).setImplementation(new ServiceProvider(e)));
// create and add a resource provider
ResourceProvider provider = new ResourceProvider(context, new URL("file://localhost/path/to/file1.txt"));
m.add(m.createComponent().setImplementation(provider).add(m.createServiceDependency().setService(ResourceHandler.class).setCallbacks("add", "remove")));
// create a resource adapter for our single resource
// note that we can provide an actual implementation instance here because there will be only one
// adapter, normally you'd want to specify a Class here
// also, create a callback instance which will be used for both callbacks on resource changes and
// life cycle callbacks on the adapters themselves
Dependency d = m.createServiceDependency().setService(ServiceInterface.class).setRequired(true);
CallbackInstance callbackInstance = new CallbackInstance(e, d);
Component component = m.createResourceAdapterService("(&(path=/path/to/*.txt)(host=localhost))", false, callbackInstance, "changed").setImplementation(new ResourceAdapter(e)).setCallbacks(callbackInstance, "init", "start", "stop", "destroy");
// add the resource adapter
m.add(component);
// wait until the single resource is available (the adapter has been started)
e.waitForStep(1, 5000);
// trigger a 'change' in our resource
provider.change();
// wait until the changed callback is invoked
e.waitForStep(2, 5000);
// and has completed (ensuring no "extra" steps are invoked in the mean time)
e.waitForStep(3, 5000);
// remove the resource adapter again
// add a component state listener, in order to track resource adapter destruction
component.add(new ComponentStateListenerImpl(e));
m.remove(component);
// wait for the stopped callback in the state listener
e.waitForStep(4, 5000);
m.clear();
}
use of org.apache.felix.dm.Dependency in project felix by apache.
the class FELIX4913_OptionalCallbackInvokedTwiceTest method test_A_Defines_BDependency_FromInitMethod.
public void test_A_Defines_BDependency_FromInitMethod() throws Throwable {
final DependencyManager m = getDM();
Ensure e = new Ensure();
Component bFactory = m.createComponent().setImplementation(new BFactory()).setInterface(BFactory.class.getName(), null);
Dependency dep = m.createServiceDependency().setService(B.class).setRequired(false).setCallbacks("bind", "unbind");
Component a = m.createComponent().setImplementation(new A(e, dep)).add(m.createServiceDependency().setService(BFactory.class).setRequired(false).setCallbacks("bind", "unbind"));
// Enable first bFactory.
m.add(bFactory);
// Then Enable A.
m.add(a);
// A should get BFactory, then it should instantiate B, and B should then be bound to A.
e.waitForStep(4, 5000);
// Now, remove BFactory. The AComponent should then call bFactory.removeB(), abd A.unbind(B) should be called.
m.remove(bFactory);
e.waitForStep(6, 5000);
e.ensure();
clearComponents();
}
use of org.apache.felix.dm.Dependency in project felix by apache.
the class ResourceAdapterDependencyAddAndRemoveTest2 method testBasicResourceAdapter.
public void testBasicResourceAdapter() throws Exception {
DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create a resource provider
ResourceProvider provider = new ResourceProvider(context, new URL("file://localhost/path/to/file1.txt"));
// activate it
Hashtable<String, String> props = new Hashtable<String, String>();
props.put("id", "1");
m.add(m.createComponent().setInterface(ServiceInterface.class.getName(), props).setImplementation(new ServiceProvider(e)));
props = new Hashtable<String, String>();
props.put("id", "2");
m.add(m.createComponent().setInterface(ServiceInterface.class.getName(), props).setImplementation(new ServiceProvider(e)));
m.add(m.createComponent().setImplementation(provider).add(m.createServiceDependency().setService(ResourceHandler.class).setCallbacks("add", "remove")));
// create a resource adapter for our single resource
// note that we can provide an actual implementation instance here because there will be only one
// adapter, normally you'd want to specify a Class here
Dependency d = m.createServiceDependency().setService(ServiceInterface.class, "(id=1)").setRequired(true);
ResourceAdapter service = new ResourceAdapter(e, d);
CallbackInstance callbackInstance = new CallbackInstance(e, d);
Component component = m.createResourceAdapterService("(&(path=/path/to/*.txt)(host=localhost))", false, callbackInstance, "changed").setImplementation(service).setCallbacks(callbackInstance, "init", "start", "stop", "destroy");
component.add(new ComponentStateListenerImpl(e));
m.add(component);
// wait until the single resource is available
e.waitForStep(1, 5000);
// trigger a 'change' in our resource
provider.change();
// wait until the changed callback is invoked
e.waitForStep(2, 5000);
System.out.println("Done!");
m.clear();
}
use of org.apache.felix.dm.Dependency in project felix by apache.
the class RemovedDependencyTest method testRemoveDependencyAndConsumerMustRemainStarted.
public void testRemoveDependencyAndConsumerMustRemainStarted() {
DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// Create two providers
Hashtable props = new Hashtable();
props.put("name", "provider1");
Component sp = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class, props).build();
props = new Properties();
props.put("name", "provider2");
Component sp2 = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class.getName(), props).build();
props = new Properties();
props.put("name", "provider3");
Component sp3 = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class.getName(), props).build();
// Create the consumer, and start it
Dependency d3 = m.createServiceDependency().setService(ServiceInterface.class, "(name=provider3)").setRequired(true).setCallbacks("add", "remove");
ServiceConsumer consumer = new ServiceConsumer(e, d3);
Component sc = component(m).impl(consumer).build();
Dependency d1 = serviceDependency(sc, ServiceInterface.class).filter("(name=provider1)").add("add").remove("remove").build();
Dependency d2 = serviceDependency(sc, ServiceInterface.class).filter("(name=provider2)").add("add").remove("remove").build();
sc.add(d1, d2);
// Add the first two providers and the consumer
m.add(sp);
m.add(sp2);
m.add(sp3);
m.add(sc);
// Check if consumer has been bound to the three providers
e.waitForStep(3, 5000);
Assert.assertEquals(3, consumer.getProvidersCount());
Assert.assertNotNull(consumer.getProvider("provider1"));
Assert.assertNotNull(consumer.getProvider("provider2"));
Assert.assertNotNull(consumer.getProvider("provider3"));
// Now remove the provider2, and check if the consumer is still alive
sc.remove(d2);
Assert.assertFalse(consumer.isStopped());
Assert.assertEquals(2, consumer.getProvidersCount());
Assert.assertNotNull(consumer.getProvider("provider1"));
Assert.assertNull(consumer.getProvider("provider2"));
Assert.assertNotNull(consumer.getProvider("provider3"));
// Now remove the provider3 (the consumer has an instance bound dependency on it), and check if the consumer is still alive
sc.remove(d3);
Assert.assertFalse(consumer.isStopped());
Assert.assertEquals(1, consumer.getProvidersCount());
Assert.assertNotNull(consumer.getProvider("provider1"));
Assert.assertNull(consumer.getProvider("provider2"));
Assert.assertNull(consumer.getProvider("provider3"));
m.clear();
}
Aggregations