Search in sources :

Example 1 with FindHook

use of org.osgi.framework.hooks.service.FindHook 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 2 with FindHook

use of org.osgi.framework.hooks.service.FindHook in project felix by apache.

the class PojoSRBundleContext method getServiceReferences.

/**
 * Retrieves an array of {@link ServiceReference} objects based on calling bundle,
 * service class name, and filter expression.  Optionally checks for isAssignable to
 * make sure that the service can be cast to the
 * @param className Service Classname or <code>null</code> for all
 * @param expr Filter Criteria or <code>null</code>
 * @return Array of ServiceReference objects that meet the criteria
 * @throws InvalidSyntaxException
 */
ServiceReference[] getServiceReferences(final String className, final String expr, final boolean checkAssignable) throws InvalidSyntaxException {
    // Define filter if expression is not null.
    SimpleFilter filter = null;
    if (expr != null) {
        try {
            filter = SimpleFilter.parse(expr);
        } catch (Exception ex) {
            throw new InvalidSyntaxException(ex.getMessage(), expr);
        }
    }
    // Ask the service registry for all matching service references.
    final Collection<ServiceReference<?>> refList = m_reg.getServiceReferences(className, filter);
    // Filter on assignable references
    if (checkAssignable) {
        for (Iterator<ServiceReference<?>> it = refList.iterator(); it.hasNext(); ) {
            // Get the current service reference.
            ServiceReference ref = it.next();
            // Now check for castability.
            if (!Util.isServiceAssignable(m_bundle, ref)) {
                it.remove();
            }
        }
    }
    // activate findhooks
    Set<ServiceReference<FindHook>> findHooks = m_reg.getHooks(org.osgi.framework.hooks.service.FindHook.class);
    for (ServiceReference<org.osgi.framework.hooks.service.FindHook> sr : findHooks) {
        org.osgi.framework.hooks.service.FindHook fh = m_reg.getService(getBundle(0), sr);
        if (fh != null) {
            try {
                fh.find(this, className, expr, !checkAssignable, new ShrinkableCollection<ServiceReference<?>>(refList));
            } catch (Throwable th) {
                System.err.println("Problem invoking service registry hook");
                th.printStackTrace();
            } finally {
                m_reg.ungetService(getBundle(0), sr);
            }
        }
    }
    if (refList.size() > 0) {
        return refList.toArray(new ServiceReference[refList.size()]);
    }
    return null;
}
Also used : FindHook(org.osgi.framework.hooks.service.FindHook) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) SimpleFilter(org.apache.felix.connect.felix.framework.capabilityset.SimpleFilter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) FindHook(org.osgi.framework.hooks.service.FindHook)

Example 3 with FindHook

use of org.osgi.framework.hooks.service.FindHook in project felix by apache.

the class ServiceRegistryTest method testRegisterFindHookService.

public void testRegisterFindHookService() {
    MockControl control = MockControl.createNiceControl(Bundle.class);
    Bundle b = (Bundle) control.getMock();
    control.replay();
    MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
    BundleContext c = (BundleContext) controlContext.getMock();
    controlContext.expectAndReturn(c.getBundle(), b);
    controlContext.replay();
    ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
    FindHook hook = new FindHook() {

        @Override
        public void find(BundleContext context, String name, String filter, boolean allServices, Collection references) {
        }
    };
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { FindHook.class.getName() }, hook, new Hashtable());
    assertEquals(1, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(FindHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    sr.unregisterService(b, reg);
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
Also used : MockControl(org.easymock.MockControl) FindHook(org.osgi.framework.hooks.service.FindHook) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 4 with FindHook

use of org.osgi.framework.hooks.service.FindHook in project felix by apache.

the class ServiceRegistryTest method testRegisterCombinedService.

public void testRegisterCombinedService() {
    MockControl control = MockControl.createNiceControl(Bundle.class);
    Bundle b = (Bundle) control.getMock();
    control.replay();
    MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
    BundleContext c = (BundleContext) controlContext.getMock();
    controlContext.expectAndReturn(c.getBundle(), b);
    controlContext.replay();
    ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
    class CombinedService implements ListenerHook, FindHook, EventHook, Runnable {

        @Override
        public void added(Collection listeners) {
        }

        @Override
        public void removed(Collection listener) {
        }

        @Override
        public void find(BundleContext context, String name, String filter, boolean allServices, Collection references) {
        }

        @Override
        public void event(ServiceEvent event, Collection contexts) {
        }

        @Override
        public void run() {
        }
    }
    CombinedService hook = new CombinedService();
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { Runnable.class.getName(), ListenerHook.class.getName(), FindHook.class.getName(), EventHook.class.getName() }, hook, new Hashtable());
    assertEquals(1, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(ListenerHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals(1, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(EventHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals(1, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(FindHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    sr.unregisterService(b, reg);
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
Also used : FindHook(org.osgi.framework.hooks.service.FindHook) Bundle(org.osgi.framework.Bundle) ListenerHook(org.osgi.framework.hooks.service.ListenerHook) Hashtable(java.util.Hashtable) EventHook(org.osgi.framework.hooks.service.EventHook) MockControl(org.easymock.MockControl) ServiceEvent(org.osgi.framework.ServiceEvent) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 5 with FindHook

use of org.osgi.framework.hooks.service.FindHook in project aries by apache.

the class ManagedServiceFactoryUseSystemBundleTest method regiserHook.

@Before
public void regiserHook() throws BundleException {
    context().getBundleByName(CM_BUNDLE).stop();
    final BundleContext systemContext = context().getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getBundleContext();
    eventHook = context().registerService(EventListenerHook.class, new EventListenerHook() {

        public void event(ServiceEvent event, Map contexts) {
            if (CM_BUNDLE.equals(event.getServiceReference().getBundle().getSymbolicName())) {
                // hide from everything but the system bundle
                // TODO on R6 we should be able to even try hiding from the system bundle
                // R5 it was not clear if hooks could hide from the system bundle
                // equinox R5 does allow hiding from system bundle
                contexts.keySet().retainAll(Collections.singleton(systemContext));
            }
        }
    }, null);
    findHook = context().registerService(FindHook.class, new FindHook() {

        public void find(BundleContext context, String arg1, String arg2, boolean arg3, Collection references) {
            // equinox R5 does allow hiding from system bundle
            if (!context.equals(systemContext)) {
                for (Iterator<ServiceReference> iReferences = references.iterator(); iReferences.hasNext(); ) {
                    if (CM_BUNDLE.equals(iReferences.next().getBundle().getSymbolicName())) {
                        iReferences.remove();
                    }
                }
            }
        }
    }, null);
    context().getBundleByName(CM_BUNDLE).start();
}
Also used : EventListenerHook(org.osgi.framework.hooks.service.EventListenerHook) FindHook(org.osgi.framework.hooks.service.FindHook) ServiceEvent(org.osgi.framework.ServiceEvent) Collection(java.util.Collection) Map(java.util.Map) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Before(org.junit.Before)

Aggregations

FindHook (org.osgi.framework.hooks.service.FindHook)5 Collection (java.util.Collection)3 BundleContext (org.osgi.framework.BundleContext)3 ServiceReference (org.osgi.framework.ServiceReference)3 Hashtable (java.util.Hashtable)2 MockControl (org.easymock.MockControl)2 Bundle (org.osgi.framework.Bundle)2 ServiceEvent (org.osgi.framework.ServiceEvent)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 SimpleFilter (org.apache.felix.connect.felix.framework.capabilityset.SimpleFilter)1 Before (org.junit.Before)1 Test (org.junit.Test)1 BundleException (org.osgi.framework.BundleException)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 EventHook (org.osgi.framework.hooks.service.EventHook)1 EventListenerHook (org.osgi.framework.hooks.service.EventListenerHook)1 ListenerHook (org.osgi.framework.hooks.service.ListenerHook)1