Search in sources :

Example 71 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class OSGiInjectionTest method testCollectionOSGiModelField.

@Test
public void testCollectionOSGiModelField() throws Exception {
    ServiceReference ref1 = mock(ServiceReference.class);
    ServiceInterface service1 = mock(ServiceInterface.class);
    when(bundleContext.getService(ref1)).thenReturn(service1);
    ServiceReference ref2 = mock(ServiceReference.class);
    ServiceInterface service2 = mock(ServiceInterface.class);
    when(bundleContext.getService(ref2)).thenReturn(service2);
    when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(new ServiceReference[] { ref1, ref2 });
    Resource res = mock(Resource.class);
    CollectionOSGiModel model = factory.getAdapter(res, CollectionOSGiModel.class);
    assertNotNull(model);
    assertNotNull(model.getServices());
    // the order on those is non deterministic as the ServiceReference.compareTo() is always returning 0
    // the real order is tested in the IT
    assertThat(model.getServices(), Matchers.containsInAnyOrder(service1, service2));
    verifyNoMoreInteractions(res);
}
Also used : ServiceInterface(org.apache.sling.models.testmodels.interfaces.ServiceInterface) Resource(org.apache.sling.api.resource.Resource) CollectionOSGiModel(org.apache.sling.models.testmodels.classes.CollectionOSGiModel) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 72 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class OSGiInjectionTest method testSimpleOSGiModelConstructor.

@Test
public void testSimpleOSGiModelConstructor() throws Exception {
    ServiceReference ref = mock(ServiceReference.class);
    ServiceInterface service = mock(ServiceInterface.class);
    when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(new ServiceReference[] { ref });
    when(bundleContext.getService(ref)).thenReturn(service);
    Resource res = mock(Resource.class);
    org.apache.sling.models.testmodels.classes.constructorinjection.SimpleOSGiModel model = factory.getAdapter(res, org.apache.sling.models.testmodels.classes.constructorinjection.SimpleOSGiModel.class);
    assertNotNull(model);
    assertNotNull(model.getService());
    assertEquals(service, model.getService());
    verifyNoMoreInteractions(res);
}
Also used : ServiceInterface(org.apache.sling.models.testmodels.interfaces.ServiceInterface) Resource(org.apache.sling.api.resource.Resource) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 73 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class ServiceUserMappedBundleFilterTest method testFind.

@Test
public void testFind() {
    List collection = new ArrayList<ServiceReference>();
    ServiceReference serviceReference1 = mock(ServiceReference.class);
    ServiceReference serviceReference2 = mock(ServiceReference.class);
    collection.add(serviceReference1);
    collection.add(serviceReference2);
    when(serviceReference1.getProperty(Mapping.SERVICENAME)).thenReturn(BUNDLE1);
    when(serviceReference1.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[] { ServiceUserMappedImpl.SERVICEUSERMAPPED });
    when(serviceReference2.getProperty(Mapping.SERVICENAME)).thenReturn(BUNDLE2);
    when(serviceReference2.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[] { ServiceUserMappedImpl.SERVICEUSERMAPPED });
    FindHook findHook = new ServiceUserMappedBundleFilter();
    findHook.find(bundleContext1, null, null, false, collection);
    TestCase.assertEquals(1, collection.size());
    TestCase.assertTrue(collection.contains(serviceReference1));
}
Also used : FindHook(org.osgi.framework.hooks.service.FindHook) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 74 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class ServiceUserMappedBundleFilter method find.

@Override
public void find(BundleContext bundleContext, String name, String filter, boolean allServices, Collection references) {
    String bundleServiceName = ServiceUserMapperImpl.getServiceName(bundleContext.getBundle());
    Iterator<ServiceReference> it = references.iterator();
    while (it.hasNext()) {
        ServiceReference serviceReference = it.next();
        if (isServiceMappingReference(serviceReference)) {
            Object serviceName = serviceReference.getProperty(Mapping.SERVICENAME);
            if (serviceName != null && !serviceName.equals(bundleServiceName)) {
                it.remove();
            }
        }
    }
}
Also used : ServiceReference(org.osgi.framework.ServiceReference)

Example 75 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class ServiceUserMappedBundleFilter method event.

@Override
public void event(ServiceEvent serviceEvent, Map map) {
    ServiceReference serviceReference = serviceEvent.getServiceReference();
    if (isServiceMappingReference(serviceReference)) {
        Object serviceName = serviceReference.getProperty(Mapping.SERVICENAME);
        if (serviceName != null && serviceName instanceof String) {
            Iterator<Map.Entry<BundleContext, Collection<ListenerHook.ListenerInfo>>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                BundleContext ctx = it.next().getKey();
                String bundleServiceName = ServiceUserMapperImpl.getServiceName(ctx.getBundle());
                if (!serviceName.equals(bundleServiceName)) {
                    it.remove();
                }
            }
        }
    }
}
Also used : ServiceReference(org.osgi.framework.ServiceReference) BundleContext(org.osgi.framework.BundleContext)

Aggregations

ServiceReference (org.osgi.framework.ServiceReference)1690 Test (org.junit.Test)926 Properties (java.util.Properties)396 Architecture (org.apache.felix.ipojo.architecture.Architecture)263 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)233 BundleContext (org.osgi.framework.BundleContext)231 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)227 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)215 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)183 ArrayList (java.util.ArrayList)167 Bundle (org.osgi.framework.Bundle)144 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)141 Hashtable (java.util.Hashtable)124 IOException (java.io.IOException)107 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)92 Dictionary (java.util.Dictionary)82 Configuration (org.osgi.service.cm.Configuration)74 CheckService (org.apache.felix.ipojo.handler.temporal.services.CheckService)70 FooService (org.apache.felix.ipojo.handler.temporal.services.FooService)70 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65