use of org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription 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.ProvidedServiceDescription 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.ProvidedServiceDescription 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.ProvidedServiceDescription 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();
}
use of org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription in project felix by apache.
the class Service method getProvidedServiceDescription.
/**
* Gets the provided service description associated with the current service.
* @param instance the instance on which looking for the provided service description
* @return the provided service description or <code>null</code> if not found.
*/
public ProvidedServiceDescription getProvidedServiceDescription(ComponentInstance instance) {
PrimitiveInstanceDescription desc = (PrimitiveInstanceDescription) instance.getInstanceDescription();
ProvidedServiceDescription[] pss = desc.getProvidedServices();
if (pss.length == 0) {
return null;
}
if (pss.length == 1) {
return pss[0];
}
if (m_specifications == null) {
return null;
} else {
for (int j = 0; j < pss.length; j++) {
ProvidedServiceDescription psd = pss[j];
List specs = Arrays.asList(psd.getServiceSpecifications());
if (specs.containsAll(m_specifications)) {
return psd;
}
}
}
return null;
}
Aggregations