use of org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription in project felix by apache.
the class IPOJOPlugin method getRequiredServiceDetail.
/**
* Builds the JSON Array representing the required services.
* @param hd the dependency handler
* @return the array containing JSON object representing service
* dependencies, or null if there is no service dependency.
* @throws JSONException if the JSON array cannot be created.
*/
private void getRequiredServiceDetail(JSONWriter pw, HandlerDescription hd) throws IOException {
if (hd == null) {
return;
}
pw.key("reqs");
pw.array();
DependencyHandlerDescription desc = (DependencyHandlerDescription) hd;
for (DependencyDescription dep : desc.getDependencies()) {
pw.object();
pw.key("specification");
pw.value(dep.getSpecification());
pw.key("id");
pw.value(dep.getId());
pw.key("state");
pw.value(StateUtils.getDependencyState(dep.getState()));
pw.key("policy");
pw.value(StateUtils.getDependencyBindingPolicy(dep.getPolicy()));
pw.key("optional");
pw.value(dep.isOptional());
pw.key("aggregate");
pw.value(dep.isMultiple());
if (dep.getFilter() != null) {
pw.key("filter");
pw.value(dep.getFilter());
}
if (dep.getServiceReferences() != null && dep.getServiceReferences().size() != 0) {
pw.key("matching");
getServiceReferenceList(pw, dep.getServiceReferences());
}
if (dep.getUsedServices() != null && dep.getUsedServices().size() != 0) {
pw.key("used");
getServiceReferenceList(pw, dep.getUsedServices());
}
pw.endObject();
}
pw.endArray();
}
use of org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription in project felix by apache.
the class TestContextualFilters method testContextualFilterWithInstanceProperty.
@Test
public void testContextualFilterWithInstanceProperty() {
Properties configuration = new Properties();
Properties filters = new Properties();
filters.put("foo", "(id=${instance.id})");
configuration.put("requires.filters", filters);
configuration.put("instance.id", 2);
ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".components.context.ContextualFilterConsumer", configuration);
assertTrue(instance.getState() == ComponentInstance.VALID);
DependencyHandlerDescription desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix" + ".ipojo:requires");
// Only one dependency.
DependencyDescription dependency = desc.getDependencies()[0];
assertEquals("(id=2)", dependency.getFilter());
}
use of org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription in project felix by apache.
the class TestContextualFilters method testContextualFilterWithSystemProperty.
@Test
public void testContextualFilterWithSystemProperty() {
// Set the system property.
System.setProperty("env.id", "2");
Properties configuration = new Properties();
Properties filters = new Properties();
filters.put("foo", "(id=${env.id})");
configuration.put("requires.filters", filters);
ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".components.context.ContextualFilterConsumer", configuration);
assertTrue(instance.getState() == ComponentInstance.VALID);
DependencyHandlerDescription desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix" + ".ipojo:requires");
// Only one dependency.
DependencyDescription dependency = desc.getDependencies()[0];
assertEquals("(id=2)", dependency.getFilter());
}
use of org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription in project felix by apache.
the class TestContextualFiltersAndExternalSources method testContextualFilterUsingOneSource.
@Test
public void testContextualFilterUsingOneSource() {
MyContextSource source = new MyContextSource();
source.set("source.id", 2);
registration = context.registerService(ContextSource.class.getName(), source, null);
Properties configuration = new Properties();
Properties filters = new Properties();
filters.put("foo", "(id=${source.id})");
configuration.put("requires.filters", filters);
ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".components.context.ContextualFilterConsumer", configuration);
assertTrue(instance.getState() == ComponentInstance.VALID);
DependencyHandlerDescription desc = (DependencyHandlerDescription) instance.getInstanceDescription().getHandlerDescription("org.apache.felix" + ".ipojo:requires");
// Only one dependency.
DependencyDescription dependency = desc.getDependencies()[0];
assertEquals("(id=2)", dependency.getFilter());
}
use of org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription in project felix by apache.
the class TestDependencyArchitecture method testOptionalDependency.
@Test
public void testOptionalDependency() {
ServiceReference arch_dep = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), instance2.getInstanceName());
assertNotNull("Check architecture availability", arch_dep);
PrimitiveInstanceDescription id_dep = (PrimitiveInstanceDescription) ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
assertTrue("Check instance invalidity - 1", id_dep.getState() == ComponentInstance.VALID);
// Check dependency handler invalidity
DependencyHandlerDescription dhd = getDependencyDesc(id_dep);
assertTrue("Check dependency handler invalidity", dhd.isValid());
// Check dependency metadata
assertEquals("Check dependency interface", dhd.getDependencies()[0].getInterface(), FooService.class.getName());
assertEquals("Check dependency id", dhd.getDependencies()[0].getId(), "FooService");
assertFalse("Check dependency cardinality", dhd.getDependencies()[0].isMultiple());
assertTrue("Check dependency optionality", dhd.getDependencies()[0].isOptional());
assertNull("Check dependency ref -1", dhd.getDependencies()[0].getServiceReferences());
fooProvider1.start();
ServiceReference arch_ps = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), fooProvider1.getInstanceName());
assertNotNull("Check architecture availability", arch_ps);
PrimitiveInstanceDescription id_ps = (PrimitiveInstanceDescription) ((Architecture) osgiHelper.getRawServiceObject(arch_ps)).getInstanceDescription();
assertTrue("Check instance invalidity - 1", id_ps.getState() == ComponentInstance.VALID);
// id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
assertTrue("Check instance validity", id_dep.getState() == ComponentInstance.VALID);
dhd = getDependencyDesc(id_dep);
assertTrue("Check dependency handler validity", dhd.isValid());
assertEquals("Check dependency ref - 2 ", dhd.getDependencies()[0].getServiceReferences().size(), 1);
ServiceReference cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance2.getInstanceName());
assertNotNull("Check CheckService availability", cs_ref);
CheckService cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
assertTrue("check CheckService invocation", cs.check());
// Check object graph
// id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
dhd = getDependencyDesc(id_dep);
// id_ps = ((Architecture) osgiHelper.getRawServiceObject(arch_ps)).getInstanceDescription();
ProvidedServiceHandlerDescription psh = getPSDesc(id_ps);
assertEquals("Check POJO creation", id_ps.getCreatedObjects().length, 1);
fooProvider1.stop();
// id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
assertTrue("Check instance invalidity - 2", id_dep.getState() == ComponentInstance.VALID);
dhd = getDependencyDesc(id_dep);
assertTrue("Check dependency handler invalidity", dhd.isValid());
fooProvider1.start();
// id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
dhd = getDependencyDesc(id_dep);
arch_ps = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), fooProvider1.getInstanceName());
assertNotNull("Check architecture availability", arch_ps);
// id_ps = ((Architecture) osgiHelper.getRawServiceObject(arch_ps)).getInstanceDescription();
assertTrue("Check instance invalidity - 1", id_ps.getState() == ComponentInstance.VALID);
psh = getPSDesc(id_ps);
assertTrue("Check instance validity", id_dep.getState() == ComponentInstance.VALID);
assertTrue("Check dependency handler validity", dhd.isValid());
assertEquals("Check dependency ref -3", dhd.getDependencies()[0].getServiceReferences().size(), 1);
cs_ref = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance2.getInstanceName());
assertNotNull("Check CheckService availability", cs_ref);
cs = (CheckService) osgiHelper.getRawServiceObject(cs_ref);
assertTrue("check CheckService invocation", cs.check());
// Check object graph
// id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
dhd = getDependencyDesc(id_dep);
// id_ps = ((Architecture) osgiHelper.getRawServiceObject(arch_ps)).getInstanceDescription();
psh = getPSDesc(id_ps);
fooProvider1.stop();
// id_dep = ((Architecture) osgiHelper.getRawServiceObject(arch_dep)).getInstanceDescription();
assertTrue("Check instance invalidity - 2", id_dep.getState() == ComponentInstance.VALID);
dhd = getDependencyDesc(id_dep);
assertTrue("Check dependency handler invalidity", dhd.isValid());
id_dep = null;
cs = null;
getContext().ungetService(arch_dep);
getContext().ungetService(cs_ref);
}
Aggregations