Search in sources :

Example 1 with ProvidedServiceDescription

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());
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ProvidedServiceDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription) ComponentInstance(org.apache.felix.ipojo.ComponentInstance) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) Test(org.junit.Test)

Example 2 with ProvidedServiceDescription

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");
}
Also used : Architecture(org.apache.felix.ipojo.architecture.Architecture) Factory(org.apache.felix.ipojo.Factory) Properties(java.util.Properties) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) ServiceReference(org.osgi.framework.ServiceReference) FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ProvidedServiceDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription) HandlerDescription(org.apache.felix.ipojo.architecture.HandlerDescription) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) Test(org.junit.Test)

Example 3 with ProvidedServiceDescription

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);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ProvidedServiceDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription) Architecture(org.apache.felix.ipojo.architecture.Architecture) HandlerDescription(org.apache.felix.ipojo.architecture.HandlerDescription) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) Factory(org.apache.felix.ipojo.Factory) InstanceDescription(org.apache.felix.ipojo.architecture.InstanceDescription) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) BarService(org.apache.felix.ipojo.runtime.core.services.BarService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 4 with ProvidedServiceDescription

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();
}
Also used : ProvidedServiceDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)

Example 5 with ProvidedServiceDescription

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;
}
Also used : ProvidedServiceDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription) List(java.util.List) ArrayList(java.util.ArrayList) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription)

Aggregations

ProvidedServiceDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription)11 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)9 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)8 Test (org.junit.Test)8 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)4 Factory (org.apache.felix.ipojo.Factory)4 Architecture (org.apache.felix.ipojo.architecture.Architecture)4 HandlerDescription (org.apache.felix.ipojo.architecture.HandlerDescription)4 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)4 ServiceReference (org.osgi.framework.ServiceReference)4 Properties (java.util.Properties)3 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)2 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ProvidedServiceListener (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceListener)1 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)1