use of org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription in project felix by apache.
the class TestListeners method testRemoveNonexistentProvidedServiceListener.
@Test(expected = NoSuchElementException.class)
public void testRemoveNonexistentProvidedServiceListener() {
ComponentInstance ci = ipojoHelper.createComponentInstance("PS-Controller-1-default");
ProvidedServiceHandlerDescription pshd = (ProvidedServiceHandlerDescription) ci.getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:provides");
ProvidedServiceDescription ps = getPS(FooService.class.getName(), pshd.getProvidedServices());
// Should fail!
ps.removeListener(new ThrowingListener());
}
use of org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription in project felix by apache.
the class TestProvidedServiceArchitecture method testProps.
@Test
public void testProps() {
String factName = "PS-FooProviderType-3";
String compName = "FooProvider";
// Get the factory to create a component instance
Factory fact = ipojoHelper.getFactory(factName);
assertNotNull("Cannot find the factory FooProvider", fact);
Properties props = new Properties();
props.put("instance.name", compName);
props.put("foo", "foo");
props.put("bar", "2");
props.put("baz", "baz");
ipojoHelper.createComponentInstance(factName, props);
ServiceReference arch_ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), compName);
assertNotNull("Architecture not available", arch_ref);
Architecture arch = (Architecture) osgiHelper.getRawServiceObject(arch_ref);
InstanceDescription id = arch.getInstanceDescription();
assertEquals("Check component instance name (" + id.getName() + ")", id.getName(), compName);
assertEquals("Check component type implementation class", id.getComponentDescription().getClassName(), "org.apache.felix.ipojo.runtime.core.components.FooProviderType1");
HandlerDescription[] handlers = id.getHandlers();
assertEquals("Number of handlers", handlers.length, 3);
// Look for the ProvidedService Handler
ProvidedServiceHandlerDescription pshd = null;
pshd = (ProvidedServiceHandlerDescription) id.getHandlerDescription("org.apache.felix.ipojo:provides");
assertNotNull("Check ProvidedServiceHandlerDescription", pshd);
ProvidedServiceDescription[] ps = pshd.getProvidedServices();
assertEquals("Check ProvidedService number", ps.length, 1);
assertEquals("Check Provided Service Specs - 1", ps[0].getServiceSpecifications().length, 1);
assertEquals("Check Provided Service Specs - 2", ps[0].getServiceSpecifications()[0], FooService.class.getName());
assertEquals("Check Provided Service availability", ps[0].getState(), ProvidedServiceDescription.REGISTERED);
Properties prop = ps[0].getProperties();
assertNotNull("Check Props", prop);
assertEquals("Check service properties number (#" + prop + "?=5)", prop.size(), 5);
assertEquals("Check instance.name property", prop.getProperty("instance.name"), compName);
assertEquals("Check factory.name property", prop.getProperty("factory.name"), factName);
assertEquals("Check foo property", prop.get("foo"), "foo");
assertEquals("Check bar property", prop.get("bar"), new Integer(2));
assertEquals("Check baz property", prop.get("baz"), "baz");
}
use of org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription in project felix by apache.
the class TestProvidedServiceArchitecture method testDoubleProviding.
@Test
public void testDoubleProviding() {
String factName = "PS-FooBarProviderType-1";
String compName = "FooProvider";
// Get the factory to create a component instance
Factory fact = ipojoHelper.getFactory(factName);
assertNotNull("Cannot find the factory FooProvider", fact);
ipojoHelper.createComponentInstance(factName, compName);
ServiceReference arch_ref = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), compName);
assertNotNull("Architecture not available", arch_ref);
Architecture arch = (Architecture) osgiHelper.getRawServiceObject(arch_ref);
InstanceDescription id = arch.getInstanceDescription();
assertEquals("Check component instance name (" + id.getName() + ")", id.getName(), compName);
assertEquals("Check component type implementation class", id.getComponentDescription().getClassName(), "org.apache.felix.ipojo.runtime.core.components.FooBarProviderType1");
HandlerDescription[] handlers = id.getHandlers();
assertEquals("Number of handlers", handlers.length, 2);
// Look for the ProvidedService Handler
ProvidedServiceHandlerDescription pshd = null;
pshd = (ProvidedServiceHandlerDescription) id.getHandlerDescription("org.apache.felix.ipojo:provides");
// for(int i = 0; i < handlers.length; i++) {
// if(handlers[i].getHandlerName().equals("org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandler")) {
// pshd = (ProvidedServiceHandlerDescription) handlers[i];
// }
// }
assertNotNull("Check ProvidedServiceHandlerDescription", pshd);
ProvidedServiceDescription[] ps = pshd.getProvidedServices();
assertEquals("Check ProvidedService number", ps.length, 1);
assertEquals("Check Provided Service Specs - 1", ps[0].getServiceSpecifications().length, 2);
assertContains("Check provided service specs - 2", ps[0].getServiceSpecifications(), FooService.class.getName());
assertContains("Check provided service specs - 2", ps[0].getServiceSpecifications(), BarService.class.getName());
assertEquals("Check Provided Service availability", ps[0].getState(), ProvidedServiceDescription.REGISTERED);
}
use of org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription in project felix by apache.
the class TestDependencyArchitecture method testProxiedSimpleDependency.
@Test
public void testProxiedSimpleDependency() {
ServiceReference arch_dep = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), instance5.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.INVALID);
// Check dependency handler invalidity
DependencyHandlerDescription dhd = getDependencyDesc(id_dep);
assertFalse("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.class.getName());
assertFalse("Check dependency cardinality", dhd.getDependencies()[0].isMultiple());
assertFalse("Check dependency optionality", dhd.getDependencies()[0].isOptional());
assertNull("Check dependency ref -1", dhd.getDependencies()[0].getServiceReferences());
assertTrue("Check dependency proxy", dhd.getDependencies()[0].isProxy());
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(), instance5.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.INVALID);
dhd = getDependencyDesc(id_dep);
assertFalse("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(), instance5.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.INVALID);
dhd = getDependencyDesc(id_dep);
assertFalse("Check dependency handler invalidity", dhd.isValid());
id_dep = null;
cs = null;
getContext().ungetService(arch_dep);
getContext().ungetService(cs_ref);
}
use of org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription in project felix by apache.
the class IPOJOPlugin method getProvidedServiceDetail.
/**
* Creates the JSON Array describing the provided services.
* @param hd the provided service handler
* @return the JSON Array or null if no provided service
* @throws JSONException if the array cannot be created.
*/
private void getProvidedServiceDetail(final JSONWriter pw, HandlerDescription hd) throws IOException {
if (hd == null) {
return;
}
pw.key("services");
pw.array();
ProvidedServiceHandlerDescription desc = (ProvidedServiceHandlerDescription) hd;
for (ProvidedServiceDescription ps : desc.getProvidedServices()) {
pw.object();
String spec = Arrays.toString(ps.getServiceSpecifications());
if (spec.startsWith("[")) {
spec = spec.substring(1, spec.length() - 1);
}
pw.key("specification");
pw.value(spec);
pw.key("state");
pw.value(StateUtils.getProvidedServiceState(ps.getState()));
if (ps.getServiceReference() != null) {
pw.key("id");
pw.value(ps.getServiceReference().getProperty(Constants.SERVICE_ID));
}
if (ps.getProperties() != null && !ps.getProperties().isEmpty()) {
pw.key("properties");
getServiceProperties(pw, ps.getProperties());
}
pw.endObject();
}
pw.endArray();
}
Aggregations