use of org.apache.felix.dm.ServiceDependency in project felix by apache.
the class ServiceRaceTest method doTest.
void doTest(int loop) throws Throwable {
debug("loop#%d -------------------------", loop);
final Ensure step = new Ensure(false);
// Create one client component, which depends on many service dependencies
final Component client = m_dm.createComponent();
final Client clientImpl = new Client(step);
client.setImplementation(clientImpl);
// Create client service dependencies
final ServiceDependency[] dependencies = new ServiceDependency[DEPENDENCIES];
for (int i = 0; i < DEPENDENCIES; i++) {
final String filter = "(id=loop" + loop + "." + i + ")";
dependencies[i] = m_dm.createServiceDependency().setService(Dep.class, filter).setRequired(true).setCallbacks("add", "remove");
client.add(dependencies[i]);
}
String pid = "pid." + loop;
final ConfigurationDependency confDependency = m_dm.createConfigurationDependency().setPid(pid);
client.add(confDependency);
// Create Configuration (concurrently).
final Configuration conf = m_cm.getConfiguration(pid, null);
final Hashtable props = new Hashtable();
props.put("foo", "bar");
schedule(new Runnable() {
public void run() {
try {
conf.update(props);
} catch (IOException e) {
error("update failed", e);
}
}
});
// Activate the client service dependencies concurrently.
List<Component> deps = new ArrayList();
for (int i = 0; i < DEPENDENCIES; i++) {
Hashtable h = new Hashtable();
h.put("id", "loop" + loop + "." + i);
final Component s = m_dm.createComponent().setInterface(Dep.class.getName(), h).setImplementation(new DepImpl());
deps.add(s);
schedule(new Runnable() {
public void run() {
m_dm.add(s);
}
});
}
// Start the client (concurrently)
schedule(new Runnable() {
public void run() {
m_dm.add(client);
}
});
// Ensure that client has been started.
int expectedStep = 1 + /* conf */
DEPENDENCIES + 1;
step.waitForStep(expectedStep, STEP_WAIT);
Assert.assertEquals(DEPENDENCIES, clientImpl.getDependencies());
Assert.assertNotNull(clientImpl.getConfiguration());
// Stop all dependencies concurrently.
for (Component dep : deps) {
final Component dependency = dep;
schedule(new Runnable() {
public void run() {
m_dm.remove(dependency);
}
});
}
// Stop client concurrently.
schedule(new Runnable() {
public void run() {
m_dm.remove(client);
}
});
// Remove configuration (asynchronously)
final Ensure stepConfDeleted = new Ensure(false);
schedule(new Runnable() {
public void run() {
try {
conf.delete();
stepConfDeleted.step(1);
} catch (IOException e) {
warn("error while unconfiguring", e);
}
}
});
// Ensure that client has been stopped, then destroyed, then unbound from all dependencies
// stop/destroy
expectedStep += 2;
// removed all dependencies
expectedStep += DEPENDENCIES;
step.waitForStep(expectedStep, STEP_WAIT);
// Make sure configuration is removed
stepConfDeleted.waitForStep(1, STEP_WAIT);
step.ensure();
Assert.assertEquals(0, clientImpl.getDependencies());
m_threadPool.awaitQuiescence(5000, TimeUnit.MILLISECONDS);
if (super.errorsLogged()) {
throw new IllegalStateException("Race test interrupted (some error occured, see previous logs)");
}
debug("finished one test loop");
if ((loop + 1) % 100 == 0) {
long duration = System.currentTimeMillis() - m_timeStamp;
warn("Performed 100 tests (total=%d) in %d ms.", (loop + 1), duration);
m_timeStamp = System.currentTimeMillis();
}
}
use of org.apache.felix.dm.ServiceDependency in project felix by apache.
the class TemporalServiceDependencyTest method testFELIX4858_ServiceAdapterConsumptionWithCallbackAndIntermittentAvailability.
// Same test as testServiceConsumptionWithCallbackAndIntermittentAvailability, but the consumer is now
// an adapter for the Adaptee interface.
public void testFELIX4858_ServiceAdapterConsumptionWithCallbackAndIntermittentAvailability() {
final DependencyManager m = getDM();
// helper class that ensures certain steps get executed in sequence
Ensure e = new Ensure();
// create a service provider and consumer
TemporalServiceProvider provider = new TemporalServiceProvider(e);
Component sp = m.createComponent().setImplementation(provider).setInterface(TemporalServiceInterface.class.getName(), null);
TemporalServiceProvider2 provider2 = new TemporalServiceProvider2(e);
Component sp2 = m.createComponent().setImplementation(provider2).setInterface(TemporalServiceInterface.class.getName(), null);
TemporalServiceConsumerAdapterWithCallback consumer = new TemporalServiceConsumerAdapterWithCallback(e);
ServiceDependency temporalDep = m.createTemporalServiceDependency(10000).setService(TemporalServiceInterface.class).setRequired(true).setCallbacks("add", "remove");
Component sc = m.createAdapterService(Adaptee.class, null).setImplementation(consumer).add(temporalDep);
Component adaptee = m.createComponent().setImplementation(new Adaptee()).setInterface(Adaptee.class.getName(), null);
// add the adapter service consumer
m.add(sc);
// add the adaptee (the adapter service depends on it)
m.add(adaptee);
// now add the first provider
m.add(sp);
e.waitForStep(2, 5000);
// and remove it again (this should not affect the consumer yet)
m.remove(sp);
// now add the second provider
m.add(sp2);
e.step(3);
e.waitForStep(4, 5000);
// and remove it again
m.remove(sp2);
// finally remove the consumer
m.remove(sc);
// Wait for the consumer.remove callback
e.waitForStep(6, 5000);
// ensure we executed all steps inside the component instance
e.step(7);
m.clear();
}
use of org.apache.felix.dm.ServiceDependency in project felix by apache.
the class AspectNotAlwaysInjectedWithRequiredDependencies method testAspectNotAlwaysInjectedWithRequiredDependency.
public void testAspectNotAlwaysInjectedWithRequiredDependency() {
DependencyManager m = getDM();
Component orig = m.createComponent().setImplementation(new OriginalServiceImpl()).setInterface(OriginalService.class.getName(), null);
Component aspect = m.createAspectService(OriginalService.class, null, 10).setImplementation(new Aspect());
m.add(orig);
m.add(aspect);
m_e.waitForStep(2, 5000);
Component aspectDep = m.createComponent().setImplementation(new AspectDependency()).setInterface(AspectDependency.class.getName(), null);
m.add(aspectDep);
m_e.waitForStep(3, 5000);
ServiceDependency sd = m.createServiceDependency().setService(AspectDependency.class).setRequired(true).setCallbacks("add", "remove");
aspect.add(sd);
m_e.waitForStep(4, 5000);
aspect.remove(sd);
m_e.waitForStep(5, 5000);
m.remove(aspect);
m_e.waitForStep(6, 5000);
}
use of org.apache.felix.dm.ServiceDependency in project felix by apache.
the class DiagnosticsTest method testServiceDependencyMissing.
public void testServiceDependencyMissing() throws Exception {
DependencyManager dm = getDM();
ServiceDependency serviceDependency1 = dm.createServiceDependency().setService(S1.class).setRequired(true);
ServiceDependency serviceDependency2 = dm.createServiceDependency().setService(S2.class).setRequired(true);
Component component1 = dm.createComponent().setImplementation(C0.class).add(serviceDependency1);
Component component2 = dm.createComponent().setImplementation(S1Impl1.class).setInterface(S1.class.getName(), null).add(serviceDependency2);
dm.add(component1);
dm.add(component2);
DependencyGraph graph = DependencyGraph.getGraph(ComponentState.UNREGISTERED, DependencyState.REQUIRED_UNAVAILABLE);
assertEquals(2, graph.getAllComponents().size());
assertEquals(2, graph.getAllDependencies().size());
List<MissingDependency> missingDependencies = graph.getMissingDependencies("service");
assertEquals(1, missingDependencies.size());
assertEquals(S2.class.getName(), missingDependencies.get(0).getName());
assertTrue(graph.getMissingDependencies("configuration").isEmpty());
assertTrue(graph.getMissingDependencies("bundle").isEmpty());
assertTrue(graph.getMissingDependencies("resource").isEmpty());
}
use of org.apache.felix.dm.ServiceDependency in project felix by apache.
the class ServiceDependencyBuilderImpl method build.
// Build final ServiceDependency object.
@Override
public ServiceDependency build() {
DependencyManager dm = m_component.getDependencyManager();
if (m_ref != null && m_filter != null) {
throw new IllegalArgumentException("Can not set ref and filter at the same time");
}
if (m_serviceIface == null && (m_ref == null || m_filter == null)) {
throw new IllegalArgumentException("service interface not specified, and no service reference or service filter specified.");
}
ServiceDependency sd = m_timeout > -1 ? dm.createTemporalServiceDependency(m_timeout) : dm.createServiceDependency();
if (m_ref != null) {
sd.setService(m_serviceIface, m_ref);
} else {
sd.setService(m_serviceIface, m_filter);
}
if (!m_requiredSet) {
m_required = Helpers.isDependencyRequiredByDefault(m_component);
}
sd.setRequired(m_required);
sd.setDefaultImplementation(m_defaultImpl);
if (m_debug != null) {
sd.setDebug(m_debug);
}
if (m_propagate) {
sd.setPropagate(true);
} else if (m_propagateInstance != null) {
if (m_propagateMethod == null) {
throw new IllegalArgumentException("propagate instance can't be null");
}
sd.setPropagate(m_propagateInstance, m_propagateMethod);
}
if (hasCallbacks()) {
sd.setCallbacks(m_callbackInstance, m_added, m_changed, m_removed, m_swapped);
} else if (hasRefs()) {
Object cb = createCallbackInstance();
sd.setCallbacks(cb, "add", "change", "remove", m_swapRefs.size() > 0 ? "swap" : null);
}
if (m_autoConfigField != null) {
sd.setAutoConfig(m_autoConfigField);
} else {
sd.setAutoConfig(m_autoConfig);
}
// false if the callback signature only contains service ref and/or component parameters.
sd.setDereference(m_dereferenceServiceInternally);
return sd;
}
Aggregations