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));
}
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;
}
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());
}
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());
}
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();
}
Aggregations