Search in sources :

Example 1 with HandlerDescription

use of org.apache.felix.ipojo.architecture.HandlerDescription 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 2 with HandlerDescription

use of org.apache.felix.ipojo.architecture.HandlerDescription 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 3 with HandlerDescription

use of org.apache.felix.ipojo.architecture.HandlerDescription in project felix by apache.

the class AutoHandlerTest method testInstanceCreation.

@Test
public void testInstanceCreation() throws Exception {
    String v = "org.apache.felix.ipojo.test.handler.checkservice:check";
    System.setProperty(ORG_APACHE_FELIX_IPOJO_HANDLER_AUTO_PRIMITIVE, v);
    factory.stop();
    factory.restart();
    factory.start();
    instance = factory.createComponentInstance(new Properties());
    assertEquals(ComponentInstance.VALID, instance.getState());
    HandlerDescription hd = instance.getInstanceDescription().getHandlerDescription(v);
    assertNotNull(hd);
    assertTrue(hd instanceof CheckServiceHandlerDescription);
    System.setProperty(ORG_APACHE_FELIX_IPOJO_HANDLER_AUTO_PRIMITIVE, "");
    factory.stop();
    factory.restart();
    factory.start();
}
Also used : CheckServiceHandlerDescription(org.apache.felix.ipojo.runtime.externalhandlers.services.CheckServiceHandlerDescription) HandlerDescription(org.apache.felix.ipojo.architecture.HandlerDescription) CheckServiceHandlerDescription(org.apache.felix.ipojo.runtime.externalhandlers.services.CheckServiceHandlerDescription) Properties(java.util.Properties) Test(org.junit.Test)

Example 4 with HandlerDescription

use of org.apache.felix.ipojo.architecture.HandlerDescription in project felix by apache.

the class VersionConflictTest method deployBundlesAtRuntime.

@Test
public void deployBundlesAtRuntime() throws MalformedURLException, BundleException, InvalidSyntaxException {
    if (isKF()) {
        System.out.println("Test disabled on knopflerfish");
        return;
    }
    Bundle b1 = context.installBundle(context.getProperty("url1"));
    b1.start();
    Bundle b3 = context.installBundle(context.getProperty("c1"));
    b3.start();
    Bundle b2 = context.installBundle(context.getProperty("url2"));
    b2.start();
    Bundle b4 = context.installBundle(context.getProperty("c2"));
    b4.start();
    Bundle b5 = context.installBundle(context.getProperty("cons"));
    b5.start();
    Stability.waitForStability(bc);
    Bundle[] bundles = context.getBundles();
    for (Bundle bundle : bundles) {
        System.out.println("bundle " + bundle.getSymbolicName() + " : " + (bundle.getState() == Bundle.ACTIVE));
    // Assert.assertEquals(bundles[i].getSymbolicName() + " is not active", Bundle.ACTIVE, bundles[i].getState());
    }
    // Bundle consBundle = osgiHelper.getBundle("MyCons");
    // BundleWiring wiring = consBundle.adapt(BundleWiring.class);
    // System.out.println("Bundle Wiring req: ");
    // for (BundleWire wire : wiring.getRequiredWires(null)) {
    // System.out.println(wire.getCapability().getAttributes() + " - " + wire.getCapability().getDirectives());
    // }
    osgiHelper.waitForService(Architecture.class.getName(), "(architecture.instance=mycons)", 2000);
    // Check that the two services are provided.
    ServiceReference[] refs = context.getAllServiceReferences("org.apache.felix.ipojo.core.tests.services.MyService", null);
    Assert.assertNotNull(refs);
    Assert.assertEquals(2, refs.length);
    ServiceReference refv1 = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "mycons");
    Assert.assertNotNull(refv1);
    Architecture arch = (Architecture) osgiHelper.getRawServiceObject(refv1);
    HandlerDescription desc = arch.getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:requires");
    Assert.assertNotNull(desc);
    DependencyHandlerDescription d = (DependencyHandlerDescription) desc;
    Assert.assertNotNull(d.getDependencies());
    Assert.assertEquals(1, d.getDependencies().length);
    DependencyDescription dep = d.getDependencies()[0];
    Assert.assertEquals(Dependency.RESOLVED, dep.getState());
    Assert.assertEquals(1, dep.getServiceReferences().size());
    ServiceReference r = dep.getServiceReferences().get(0);
    Assert.assertEquals("provider", r.getProperty("factory.name"));
    Assert.assertEquals("2.0", r.getProperty("factory.version"));
}
Also used : DependencyHandlerDescription(org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription) Architecture(org.apache.felix.ipojo.architecture.Architecture) HandlerDescription(org.apache.felix.ipojo.architecture.HandlerDescription) DependencyHandlerDescription(org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription) DependencyDescription(org.apache.felix.ipojo.handlers.dependency.DependencyDescription) Test(org.junit.Test)

Example 5 with HandlerDescription

use of org.apache.felix.ipojo.architecture.HandlerDescription in project felix by apache.

the class VersionConflictTest method deployBundlesAtRuntimeV1.

@Test
@Ignore("Does not work anymore, but the scenario runs as expected in a regular framework. The check find the " + "version 2.0 of the service instead of the 1.0")
public void deployBundlesAtRuntimeV1() throws MalformedURLException, BundleException, InvalidSyntaxException {
    Bundle b1 = context.installBundle(context.getProperty("url1"));
    b1.start();
    Bundle b3 = context.installBundle(context.getProperty("c1"));
    b3.start();
    Bundle b2 = context.installBundle(context.getProperty("url2"));
    b2.start();
    Bundle b4 = context.installBundle(context.getProperty("c2"));
    b4.start();
    Bundle b5 = context.installBundle(context.getProperty("consV1"));
    b5.start();
    Bundle[] bundles = context.getBundles();
    for (int i = 0; i < bundles.length; i++) {
        System.out.println("bundle " + bundles[i].getSymbolicName() + " : " + (bundles[i].getState() == Bundle.ACTIVE));
    }
    osgiHelper.waitForService(Architecture.class.getName(), "(architecture.instance=mycons)", 2000);
    // Check that the two services are provided.
    ServiceReference[] refs = context.getAllServiceReferences("org.apache.felix.ipojo.core.tests.services.MyService", null);
    Assert.assertNotNull(refs);
    Assert.assertEquals(2, refs.length);
    ServiceReference refv1 = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "mycons");
    Assert.assertNotNull(refv1);
    Architecture arch = (Architecture) osgiHelper.getRawServiceObject(refv1);
    HandlerDescription desc = arch.getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:requires");
    Assert.assertNotNull(desc);
    DependencyHandlerDescription d = (DependencyHandlerDescription) desc;
    Assert.assertNotNull(d.getDependencies());
    Assert.assertEquals(1, d.getDependencies().length);
    DependencyDescription dep = d.getDependencies()[0];
    Assert.assertEquals(Dependency.RESOLVED, dep.getState());
    Assert.assertEquals(1, dep.getServiceReferences().size());
    ServiceReference r = dep.getServiceReferences().get(0);
    Assert.assertEquals("provider", r.getProperty("factory.name"));
    Assert.assertEquals("1.0", r.getProperty("factory.version"));
}
Also used : DependencyHandlerDescription(org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription) Architecture(org.apache.felix.ipojo.architecture.Architecture) HandlerDescription(org.apache.felix.ipojo.architecture.HandlerDescription) DependencyHandlerDescription(org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription) DependencyDescription(org.apache.felix.ipojo.handlers.dependency.DependencyDescription) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

HandlerDescription (org.apache.felix.ipojo.architecture.HandlerDescription)11 Test (org.junit.Test)11 Architecture (org.apache.felix.ipojo.architecture.Architecture)8 ServiceReference (org.osgi.framework.ServiceReference)6 Properties (java.util.Properties)4 Factory (org.apache.felix.ipojo.Factory)4 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)4 ProvidedServiceDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceDescription)4 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)4 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)4 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)2 PrimitiveComponentType (org.apache.felix.ipojo.api.PrimitiveComponentType)2 DependencyDescription (org.apache.felix.ipojo.handlers.dependency.DependencyDescription)2 DependencyHandlerDescription (org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription)2 BarService (org.apache.felix.ipojo.runtime.core.services.BarService)1 CheckServiceHandlerDescription (org.apache.felix.ipojo.runtime.externalhandlers.services.CheckServiceHandlerDescription)1 Ignore (org.junit.Ignore)1