Search in sources :

Example 1 with ProvidedServiceHandlerDescription

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());
}
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 ProvidedServiceHandlerDescription

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");
}
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 ProvidedServiceHandlerDescription

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);
}
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 ProvidedServiceHandlerDescription

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);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.test.services.FooService) DependencyHandlerDescription(org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription) Architecture(org.apache.felix.ipojo.architecture.Architecture) CheckService(org.apache.felix.ipojo.runtime.core.test.services.CheckService) ProvidedServiceHandlerDescription(org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription) PrimitiveInstanceDescription(org.apache.felix.ipojo.PrimitiveInstanceDescription) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 5 with ProvidedServiceHandlerDescription

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

Aggregations

ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)14 Test (org.junit.Test)13 Architecture (org.apache.felix.ipojo.architecture.Architecture)9 ProvidedServiceDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription)9 ServiceReference (org.osgi.framework.ServiceReference)9 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)8 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)5 DependencyHandlerDescription (org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription)5 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)5 FooService (org.apache.felix.ipojo.runtime.core.test.services.FooService)5 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)4 Factory (org.apache.felix.ipojo.Factory)4 HandlerDescription (org.apache.felix.ipojo.architecture.HandlerDescription)4 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)4 Properties (java.util.Properties)3 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)2 DependencyDescription (org.apache.felix.ipojo.handlers.dependency.DependencyDescription)1 ProvidedServiceListener (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceListener)1 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)1