Search in sources :

Example 66 with ServiceReference

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

the class InjectorSpecificAnnotationTest method testOSGiServiceField.

@Test
public void testOSGiServiceField() throws InvalidSyntaxException {
    ServiceReference ref = mock(ServiceReference.class);
    Logger log = mock(Logger.class);
    when(bundleContext.getServiceReferences(Logger.class.getName(), null)).thenReturn(new ServiceReference[] { ref });
    when(bundleContext.getService(ref)).thenReturn(log);
    InjectorSpecificAnnotationModel model = factory.getAdapter(request, InjectorSpecificAnnotationModel.class);
    assertNotNull("Could not instanciate model", model);
    assertEquals(log, model.getService());
}
Also used : InjectorSpecificAnnotationModel(org.apache.sling.models.testmodels.classes.InjectorSpecificAnnotationModel) Logger(org.slf4j.Logger) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 67 with ServiceReference

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

the class OSGiInjectionTest method testArrayOSGiModelField.

@Test
public void testArrayOSGiModelField() 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);
    ArrayOSGiModel model = factory.getAdapter(res, ArrayOSGiModel.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(Arrays.asList(model.getServices()), Matchers.containsInAnyOrder(service1, service2));
    verifyNoMoreInteractions(res);
}
Also used : ArrayOSGiModel(org.apache.sling.models.testmodels.classes.ArrayOSGiModel) OptionalArrayOSGiModel(org.apache.sling.models.testmodels.classes.OptionalArrayOSGiModel) 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 68 with ServiceReference

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

the class OSGiServiceInjector method getService.

@SuppressWarnings("unchecked")
private <T> Object getService(Object adaptable, Class<T> type, String filter, DisposalCallbackRegistry callbackRegistry) {
    // cannot use SlingScriptHelper since it does not support ordering by service ranking due to https://issues.apache.org/jira/browse/SLING-5665
    try {
        ServiceReference[] refs = bundleContext.getServiceReferences(type.getName(), filter);
        if (refs == null || refs.length == 0) {
            return null;
        } else {
            // sort by service ranking (lowest first) (see ServiceReference.compareTo)
            List<ServiceReference> references = Arrays.asList(refs);
            Collections.sort(references);
            callbackRegistry.addDisposalCallback(new Callback(refs, bundleContext));
            return bundleContext.getService(references.get(references.size() - 1));
        }
    } catch (InvalidSyntaxException e) {
        log.error("invalid filter expression", e);
        return null;
    }
}
Also used : DisposalCallback(org.apache.sling.models.spi.DisposalCallback) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 69 with ServiceReference

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

the class OSGiInjectionTest method testListOSGiModelField.

@Test
public void testListOSGiModelField() 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);
    ListOSGiModel model = factory.getAdapter(res, ListOSGiModel.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) OptionalListOSGiModel(org.apache.sling.models.testmodels.classes.OptionalListOSGiModel) ListOSGiModel(org.apache.sling.models.testmodels.classes.ListOSGiModel) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 70 with ServiceReference

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

the class OSGiInjectionTest method testSetOSGiModelField.

@Test
public void testSetOSGiModelField() 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);
    SetOSGiModel model = factory.getAdapter(res, SetOSGiModel.class);
    assertNull(model);
    verify(bundleContext).registerService(eq(Runnable.class.getName()), eq(factory), any(Dictionary.class));
    verify(bundleContext).addBundleListener(any(BundleListener.class));
    verify(bundleContext).registerService(eq(Object.class.getName()), any(Object.class), any(Dictionary.class));
    verify(bundleContext).getBundles();
    verify(bundleContext).getBundle();
    verifyNoMoreInteractions(res, bundleContext);
}
Also used : Dictionary(java.util.Dictionary) SetOSGiModel(org.apache.sling.models.testmodels.classes.SetOSGiModel) ServiceInterface(org.apache.sling.models.testmodels.interfaces.ServiceInterface) Resource(org.apache.sling.api.resource.Resource) BundleListener(org.osgi.framework.BundleListener) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

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