use of org.apache.felix.ipojo.ServiceContext in project felix by apache.
the class TestCompositeAPI method testAPI2.
@Test
public void testAPI2() {
Factory fact1 = ipojoHelper.getFactory("composite.empty");
Properties p = new Properties();
p.put("instance.name", "empty-2");
ComponentInstance empty = null;
try {
empty = fact1.createComponentInstance(p);
} catch (Exception e) {
e.printStackTrace();
}
ServiceContext sc = getServiceContext(empty);
Factory fact2 = ipojoHelper.getFactory(sc, "composite.test.2");
Properties props2 = new Properties();
props2.put("instance.name", "4");
Factory fact3 = ipojoHelper.getFactory(sc, "composite.test.3");
Properties props3 = new Properties();
props3.put("instance.name", "5");
ComponentInstance comp2 = null;
ComponentInstance comp3 = null;
try {
comp2 = fact2.createComponentInstance(props2, sc);
comp3 = fact3.createComponentInstance(props3, sc);
} catch (Exception e) {
e.printStackTrace();
}
assertTrue("Test comp3", comp3.getState() == ComponentInstance.VALID);
assertTrue("Test comp2", comp2.getState() == ComponentInstance.VALID);
ServiceReference ref = null;
ref = ipojoHelper.getServiceReferenceByName(sc, CheckService.class.getName(), "4");
assertNotNull("Check ref", ref);
CheckService cs = (CheckService) sc.getService(ref);
assertTrue("Check invoke", cs.check());
comp3.dispose();
comp2.dispose();
empty.dispose();
}
use of org.apache.felix.ipojo.ServiceContext in project felix by apache.
the class TestCompositeAPI method testAPI.
@Test
public void testAPI() {
Factory fact1 = ipojoHelper.getFactory("composite.empty");
Properties p = new Properties();
p.put("instance.name", "empty-1");
ComponentInstance empty = null;
try {
empty = fact1.createComponentInstance(p);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
ServiceContext sc = getServiceContext(empty);
Factory fact2 = ipojoHelper.getFactory("composite.test.2");
Properties props2 = new Properties();
// 2
props2.put("instance.name", "2");
Factory fact3 = ipojoHelper.getFactory("composite.test.3");
Properties props3 = new Properties();
props3.put("instance.name", "3");
ComponentInstance comp2 = null;
ComponentInstance comp3 = null;
try {
comp2 = fact2.createComponentInstance(props2, sc);
comp3 = fact3.createComponentInstance(props3, sc);
} catch (Throwable e) {
e.printStackTrace();
fail(e.getMessage());
}
assertTrue("Test comp3", comp3.getState() == ComponentInstance.VALID);
assertTrue("Test comp2", comp2.getState() == ComponentInstance.VALID);
ServiceReference ref = null;
// 2
ref = ipojoHelper.getServiceReferenceByName(sc, CheckService.class.getName(), "2");
assertNotNull("Check ref", ref);
CheckService cs = (CheckService) sc.getService(ref);
assertTrue("Check invoke", cs.check());
comp3.dispose();
comp2.dispose();
empty.dispose();
}
use of org.apache.felix.ipojo.ServiceContext in project felix by apache.
the class CompositeInstanceDescription method getContainedInstances.
/**
* Gets the list of contained instance in the described instance.
* This list contains only instances who exposed their architecture.
* @return the list of contained instances.
*/
public InstanceDescription[] getContainedInstances() {
// Get instances description of internal instance
ServiceContext internal = ((CompositeManager) m_instance).getServiceContext();
try {
ServiceReference[] refs = internal.getServiceReferences(Architecture.class.getName(), null);
if (refs != null) {
InstanceDescription[] desc = new InstanceDescription[refs.length];
for (int i = 0; i < refs.length; i++) {
Architecture arch = (Architecture) internal.getService(refs[i]);
desc[i] = arch.getInstanceDescription();
internal.ungetService(refs[i]);
}
return desc;
}
} catch (InvalidSyntaxException e) {
// Cannot happen
}
return new InstanceDescription[0];
}
use of org.apache.felix.ipojo.ServiceContext in project felix by apache.
the class CompositeInstanceDescription method getInternalServices.
/**
* Gets the list of internally published services.
* @return the list of published services.
*/
public Element getInternalServices() {
Element services = new Element("services", "");
ServiceContext internal = ((CompositeManager) m_instance).getServiceContext();
try {
ServiceReference[] refs = internal.getServiceReferences((String) null, "(!(objectclass=" + Factory.class.getName() + "))");
for (int i = 0; refs != null && i < refs.length; i++) {
Element svc = new Element("service", "");
String[] keys = refs[i].getPropertyKeys();
for (int j = 0; j < keys.length; j++) {
Object v = refs[i].getProperty(keys[j]);
if (v instanceof String[]) {
List l = Arrays.asList((String[]) v);
svc.addAttribute(new Attribute(keys[j], l.toString()));
} else {
svc.addAttribute(new Attribute(keys[j], v.toString()));
}
}
services.addElement(svc);
}
} catch (InvalidSyntaxException e) {
// Cannot happen
}
return services;
}
use of org.apache.felix.ipojo.ServiceContext in project felix by apache.
the class TestDelayedFilteredImport method testSimple.
@Test
public void testSimple() {
import1.start();
// Two providers
assertTrue("Test component validity", import1.getState() == ComponentInstance.VALID);
ServiceContext sc = getServiceContext(import1);
ServiceReference[] refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
assertNotNull("Test foo availability inside the composite - 1", refs);
assertEquals("Test foo availability inside the composite - 1.2", refs.length, 1);
FooService fs = (FooService) sc.getService(refs[0]);
assertTrue("Test foo invocation", fs.foo());
sc.ungetService(refs[0]);
foo1.stop();
assertTrue("Test component validity", import1.getState() == ComponentInstance.VALID);
sc = getServiceContext(import1);
refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
assertNotNull("Test foo availability inside the composite - 1", refs);
assertEquals("Test foo availability inside the composite - 1.2", refs.length, 1);
fs = (FooService) sc.getService(refs[0]);
assertTrue("Test foo invocation", fs.foo());
sc.ungetService(refs[0]);
// Stop the second provider
foo2.stop();
assertTrue("Test component invalidity - 2", import1.getState() == ComponentInstance.INVALID);
foo2.start();
assertTrue("Test component validity", import1.getState() == ComponentInstance.VALID);
sc = getServiceContext(import1);
refs = ipojoHelper.getServiceReferences(sc, FooService.class.getName(), null);
assertNotNull("Test foo availability inside the composite - 3", refs);
assertEquals("Test foo availability inside the composite - 3.1", refs.length, 1);
fs = (FooService) sc.getService(refs[0]);
assertTrue("Test foo invocation", fs.foo());
sc.ungetService(refs[0]);
}
Aggregations