Search in sources :

Example 16 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project aries by apache.

the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testMultipleServiceInstancesAndTypes.

@Test
public void testMultipleServiceInstancesAndTypes() throws Exception {
    Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
    EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
    EasyMock.replay(mediatorBundle);
    BaseActivator activator = new BaseActivator() {

        @Override
        public void start(BundleContext context) throws Exception {
        }
    };
    ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
    BundleContext implBC = mockSPIBundleContext4();
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.PROVIDER_REQUIREMENT);
    headers.put(SpiFlyConstants.PROVIDE_CAPABILITY, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI," + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI2");
    Bundle implBundle = mockSPIBundle4(implBC, headers);
    List<ServiceRegistration> registrations = customizer.addingBundle(implBundle, null);
    assertEquals(3, registrations.size());
    boolean foundA = false, foundB = false, foundC = false;
    for (ServiceRegistration sreg : registrations) {
        ServiceReference sref = sreg.getReference();
        String objectClassName = ((String[]) sref.getProperty(Constants.OBJECTCLASS))[0];
        String serviceImplClassName = (String) sref.getProperty(SpiFlyConstants.PROVIDER_IMPLCLASS_PROPERTY);
        if (MySPIImpl4a.class.getName().equals(serviceImplClassName)) {
            assertEquals("org.apache.aries.mytest.MySPI", objectClassName);
            MySPI svc = (MySPI) implBC.getService(sreg.getReference());
            assertEquals("impl4a", svc.someMethod(""));
            foundA = true;
        } else if (MySPIImpl4b.class.getName().equals(serviceImplClassName)) {
            assertEquals("org.apache.aries.mytest.MySPI2", objectClassName);
            MySPI2 svc = (MySPI2) implBC.getService(sreg.getReference());
            assertEquals("impl4b", svc.someMethod(""));
            foundB = true;
        } else if (MySPIImpl4c.class.getName().equals(serviceImplClassName)) {
            assertEquals("org.apache.aries.mytest.MySPI2", objectClassName);
            MySPI2 svc = (MySPI2) implBC.getService(sreg.getReference());
            assertEquals("impl4c", svc.someMethod(""));
            foundC = true;
        }
    }
    assertTrue(foundA);
    assertTrue(foundB);
    assertTrue(foundC);
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) MySPI2(org.apache.aries.mytest.MySPI2) ServiceReference(org.osgi.framework.ServiceReference) MySPIImpl4c(org.apache.aries.spifly.impl4.MySPIImpl4c) MySPI(org.apache.aries.mytest.MySPI) MySPIImpl4a(org.apache.aries.spifly.impl4.MySPIImpl4a) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 17 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project aries by apache.

the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testNonServiceRegistryBundle.

@Test
public void testNonServiceRegistryBundle() throws Exception {
    Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
    EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
    EasyMock.replay(mediatorBundle);
    BaseActivator activator = new BaseActivator() {

        @Override
        public void start(BundleContext context) throws Exception {
        }
    };
    ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
    ServiceRegistration sreg = EasyMock.createMock(ServiceRegistration.class);
    EasyMock.replay(sreg);
    BundleContext implBC = mockSPIBundleContext(sreg);
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.PROVIDER_REQUIREMENT);
    headers.put(SpiFlyConstants.PROVIDE_CAPABILITY, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI; approval=yeah;" + SpiFlyConstants.REGISTER_DIRECTIVE + ":=\"\"");
    Bundle implBundle = mockSPIBundle(implBC, headers);
    List<ServiceRegistration> registrations = customizer.addingBundle(implBundle, null);
    assertEquals(0, registrations.size());
    Collection<Bundle> bundles = activator.findProviderBundles("org.apache.aries.mytest.MySPI");
    assertEquals(1, bundles.size());
    assertSame(implBundle, bundles.iterator().next());
    Map<String, Object> attrs = activator.getCustomBundleAttributes("org.apache.aries.mytest.MySPI", implBundle);
    assertEquals(1, attrs.size());
    assertEquals("yeah", attrs.get("approval"));
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 18 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project aries by apache.

the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testServiceSubsetSelectionAndRegistrationProperties.

@Test
public void testServiceSubsetSelectionAndRegistrationProperties() throws Exception {
    Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
    EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
    EasyMock.replay(mediatorBundle);
    BaseActivator activator = new BaseActivator() {

        @Override
        public void start(BundleContext context) throws Exception {
        }
    };
    ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
    BundleContext implBC = mockSPIBundleContext4();
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.PROVIDER_REQUIREMENT);
    headers.put(SpiFlyConstants.PROVIDE_CAPABILITY, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI2; approval=yeah; " + SpiFlyConstants.REGISTER_DIRECTIVE + ":=\"org.apache.aries.spifly.impl4.MySPIImpl4b\"");
    Bundle implBundle = mockSPIBundle4(implBC, headers);
    List<ServiceRegistration> registrations = customizer.addingBundle(implBundle, null);
    assertEquals(1, registrations.size());
    String[] objectClassProp = (String[]) registrations.iterator().next().getReference().getProperty(Constants.OBJECTCLASS);
    assertEquals(1, objectClassProp.length);
    assertEquals("org.apache.aries.mytest.MySPI2", objectClassProp[0]);
    assertNotNull(registrations.iterator().next().getReference().getProperty(SpiFlyConstants.SERVICELOADER_MEDIATOR_PROPERTY));
    assertEquals("yeah", registrations.iterator().next().getReference().getProperty("approval"));
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 19 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project aries by apache.

the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testNoServiceRegistration.

@Test
public void testNoServiceRegistration() throws Exception {
    Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
    EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
    EasyMock.replay(mediatorBundle);
    BaseActivator activator = new BaseActivator() {

        @Override
        public void start(BundleContext context) throws Exception {
        }
    };
    ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
    ServiceRegistration sreg = EasyMock.createMock(ServiceRegistration.class);
    EasyMock.replay(sreg);
    BundleContext implBC = mockSPIBundleContext(sreg);
    Bundle implBundle = mockSPIBundle(implBC, SpiFlyConstants.PROVIDER_REQUIREMENT);
    List<ServiceRegistration> registrations = customizer.addingBundle(implBundle, null);
    assertEquals(0, registrations.size());
    Collection<Bundle> bundles = activator.findProviderBundles("org.apache.aries.mytest.MySPI");
    assertEquals(1, bundles.size());
    assertSame(implBundle, bundles.iterator().next());
}
Also used : Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 20 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project aries by apache.

the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testProvidedSPIDirective.

@Test
public void testProvidedSPIDirective() throws Exception {
    Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
    EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
    EasyMock.replay(mediatorBundle);
    BaseActivator activator = new BaseActivator() {

        @Override
        public void start(BundleContext context) throws Exception {
        }
    };
    ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
    BundleContext implBC = mockSPIBundleContext4();
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.PROVIDER_REQUIREMENT);
    headers.put(SpiFlyConstants.PROVIDE_CAPABILITY, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI; approval=yeah, " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI2");
    Bundle implBundle = mockSPIBundle4(implBC, headers);
    List<ServiceRegistration> registrations = customizer.addingBundle(implBundle, null);
    assertEquals("Expected 3 registrations, one for MySPI and 2 for MySPI2", 3, registrations.size());
    Set<String> expectedObjectClasses = new HashSet<String>(Arrays.asList("org.apache.aries.mytest.MySPI", "org.apache.aries.mytest.MySPI2"));
    Set<String> actualObjectClasses = new HashSet<String>();
    boolean foundMySPI = false;
    boolean foundMySPI2 = false;
    for (ServiceRegistration sr : registrations) {
        List<String> objectClasses = Arrays.asList((String[]) sr.getReference().getProperty(Constants.OBJECTCLASS));
        actualObjectClasses.addAll(objectClasses);
        assertNotNull(sr.getReference().getProperty(SpiFlyConstants.SERVICELOADER_MEDIATOR_PROPERTY));
        if (objectClasses.contains("org.apache.aries.mytest.MySPI")) {
            assertEquals("yeah", sr.getReference().getProperty("approval"));
            foundMySPI = true;
        } else if (objectClasses.contains("org.apache.aries.mytest.MySPI2")) {
            assertNull(sr.getReference().getProperty("approval"));
            foundMySPI2 = true;
        }
    }
    assertTrue(foundMySPI);
    assertTrue(foundMySPI2);
    assertEquals(expectedObjectClasses, actualObjectClasses);
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ServiceRegistration (org.osgi.framework.ServiceRegistration)188 Test (org.junit.Test)80 Hashtable (java.util.Hashtable)61 BundleContext (org.osgi.framework.BundleContext)35 Bundle (org.osgi.framework.Bundle)26 Dictionary (java.util.Dictionary)23 Properties (java.util.Properties)22 HashMap (java.util.HashMap)21 ServiceReference (org.osgi.framework.ServiceReference)21 ArrayList (java.util.ArrayList)18 Map (java.util.Map)13 IOException (java.io.IOException)8 ServiceFactory (org.osgi.framework.ServiceFactory)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 URL (java.net.URL)6 HashSet (java.util.HashSet)6 List (java.util.List)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ObjectName (javax.management.ObjectName)5 InitialContext (javax.naming.InitialContext)5