use of org.osgi.framework.Filter in project karaf by apache.
the class GuardingEventHookTest method testEventHookProxyEvents.
@SuppressWarnings("unchecked")
@Test
public void testEventHookProxyEvents() throws Exception {
BundleContext frameworkBC = mockBundleContext(0L);
Dictionary<String, Object> config = new Hashtable<>();
config.put("service.guard", "(service.id=*)");
BundleContext hookBC = mockConfigAdminBundleContext(frameworkBC, config);
GuardProxyCatalog gpc = new GuardProxyCatalog(hookBC);
// any service will match
Filter serviceFilter = FrameworkUtil.createFilter("(service.id=*)");
GuardingEventHook geh = new GuardingEventHook(hookBC, gpc, serviceFilter);
BundleContext client1BC = mockBundleContext(123L);
// Create a proxy service mock
Dictionary<String, Object> props = new Hashtable<>();
props.put(Constants.SERVICE_ID, 13L);
props.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
ServiceReference<?> sref = mockServiceReference(props);
Map<BundleContext, Collection<ListenerInfo>> listeners = new HashMap<>();
listeners.put(client1BC, Collections.emptyList());
// Send the event. It should have no effect because the service is already a proxy for the client
assertEquals("Precondition", 0, gpc.proxyMap.size());
geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref), listeners);
assertEquals("No changes expected for the proxy map.", 0, gpc.proxyMap.size());
assertEquals("The event should be delivered to the client", 1, listeners.size());
// send the event to a different client, this client should also see the event as the proxy Service Factory is shared
Map<BundleContext, Collection<ListenerInfo>> listeners2 = new HashMap<>();
listeners2.put(mockBundleContext(51L), Collections.emptyList());
geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref), listeners2);
assertEquals("No changes expected for the proxy map.", 0, gpc.proxyMap.size());
assertEquals("The event should be delivered to the client", 1, listeners2.size());
}
use of org.osgi.framework.Filter in project karaf by apache.
the class GuardingFindHookTest method testFindHookProxyServices.
@SuppressWarnings("unchecked")
@Test
public void testFindHookProxyServices() throws Exception {
Dictionary<String, Object> config = new Hashtable<>();
config.put("service.guard", "(service.id=*)");
BundleContext hookBC = mockConfigAdminBundleContext(config);
GuardProxyCatalog gpc = new GuardProxyCatalog(hookBC);
// any service
Filter serviceFilter = FrameworkUtil.createFilter("(service.id=*)");
GuardingFindHook gfh = new GuardingFindHook(hookBC, gpc, serviceFilter);
BundleContext clientBC = mockBundleContext(31L);
Dictionary<String, Object> props = new Hashtable<>();
props.put(Constants.SERVICE_ID, 16L);
props.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
ServiceReference<?> sref = mockServiceReference(props);
Collection<ServiceReference<?>> refs = new ArrayList<>();
refs.add(sref);
gfh.find(clientBC, null, null, false, refs);
assertEquals("No proxy should have been created for the proxy find", 0, gpc.proxyMap.size());
assertEquals("As the proxy is for this bundle is should be visible and remain on the list", Collections.singletonList(sref), refs);
}
use of org.osgi.framework.Filter in project karaf by apache.
the class GuardingEventHookTest method testEventHookEvents.
@SuppressWarnings("unchecked")
@Test
public void testEventHookEvents() throws Exception {
BundleContext frameworkBC = mockBundleContext(0L);
Dictionary<String, Object> config = new Hashtable<>();
config.put("service.guard", "(service.id=*)");
BundleContext hookBC = mockConfigAdminBundleContext(frameworkBC, config);
GuardProxyCatalog gpc = new GuardProxyCatalog(hookBC);
Filter serviceFilter = FrameworkUtil.createFilter("(foo=bar)");
GuardingEventHook geh = new GuardingEventHook(hookBC, gpc, serviceFilter);
Dictionary<String, Object> props = new Hashtable<>();
props.put(Constants.SERVICE_ID, 13L);
ServiceReference<?> sref = mockServiceReference(props);
BundleContext client1BC = mockBundleContext(123L);
Map<BundleContext, Collection<ListenerInfo>> listeners = new HashMap<>();
listeners.put(client1BC, Collections.emptyList());
// Send the event. It should have no effect because the service doens't match the filter
assertEquals("Precondition", 0, gpc.proxyMap.size());
geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref), listeners);
assertEquals("No proxy should have been created because the service doesn't match the filter", 0, gpc.proxyMap.size());
assertEquals("Nothing should have been removed from the listeners", 1, listeners.size());
long service2ID = 887L;
Dictionary<String, Object> props2 = new Hashtable<>();
props2.put(Constants.SERVICE_ID, service2ID);
props2.put("a", "b");
props2.put("foo", "bar");
ServiceReference<?> sref2 = mockServiceReference(props2);
ServiceEvent se2 = new ServiceEvent(ServiceEvent.REGISTERED, sref2);
// Send the event to the system bundle and the bundle that contains the hook, should have no effect
Map<BundleContext, Collection<ListenerInfo>> listeners2 = new Hashtable<>();
listeners2.put(frameworkBC, Collections.emptyList());
listeners2.put(hookBC, Collections.emptyList());
geh.event(se2, listeners2);
assertEquals("No proxies to be created for the hook bundle or the system bundle", 0, gpc.proxyMap.size());
assertEquals("Nothing should have been removed from the listeners", 2, listeners2.size());
// This is the first time that a proxy actually does get created
Map<BundleContext, Collection<ListenerInfo>> listeners3 = new HashMap<>();
listeners3.put(client1BC, Collections.emptyList());
geh.event(se2, listeners3);
assertEquals("The service should be hidden from these listeners", 0, listeners3.size());
assertEquals("Proxy should have been created for this client", 1, gpc.proxyMap.size());
assertEquals(new Long(service2ID), gpc.proxyMap.keySet().iterator().next());
// Update the service, now an additional client is interested
// Will change the properties of sref
props2.put("a", "c");
Map<BundleContext, Collection<ListenerInfo>> listeners4 = new HashMap<>();
BundleContext client2BC = mockBundleContext(11);
listeners4.put(client2BC, Collections.emptyList());
listeners4.put(client1BC, Collections.emptyList());
geh.event(new ServiceEvent(ServiceEvent.MODIFIED, sref2), listeners4);
assertEquals("The service should be hidden from these listeners", 0, listeners4.size());
assertEquals("There should not be an additional proxy for client 2", 1, gpc.proxyMap.size());
assertNotNull(gpc.proxyMap.get(service2ID));
long service3ID = 1L;
Dictionary<String, Object> props3 = new Hashtable<>();
props3.put(Constants.SERVICE_ID, service3ID);
props3.put("foo", "bar");
ServiceReference<?> sref3 = mockServiceReference(props3);
// An event for a new service
Map<BundleContext, Collection<ListenerInfo>> listeners5 = new HashMap<>();
listeners5.put(client1BC, Collections.emptyList());
// Should be ignored
listeners5.put(client1BC, Collections.emptyList());
geh.event(new ServiceEvent(ServiceEvent.REGISTERED, sref3), listeners5);
assertEquals("There should be an additional procy for client1 to the new service", 2, gpc.proxyMap.size());
assertEquals("The service should be hidden from these listeners", 0, listeners5.size());
assertNotNull(gpc.proxyMap.get(service2ID));
assertNotNull(gpc.proxyMap.get(service3ID));
}
use of org.osgi.framework.Filter in project karaf by apache.
the class Activator method start.
@Override
public void start(BundleContext bundleContext) throws Exception {
String f = System.getProperty(GuardProxyCatalog.KARAF_SECURED_SERVICES_SYSPROP);
Filter securedServicesFilter;
if (f == null) {
// no services need to be secured
logger.info("No role-based security for services as its system property is not set: {}", GuardProxyCatalog.KARAF_SECURED_SERVICES_SYSPROP);
return;
} else {
securedServicesFilter = bundleContext.createFilter(f);
logger.info("Adding role-based security to services with filter: {}", f);
}
guardProxyCatalog = new GuardProxyCatalog(bundleContext);
guardingEventHook = new GuardingEventHook(bundleContext, guardProxyCatalog, securedServicesFilter);
bundleContext.registerService(EventListenerHook.class, guardingEventHook, null);
guardingFindHook = new GuardingFindHook(bundleContext, guardProxyCatalog, securedServicesFilter);
bundleContext.registerService(FindHook.class, guardingFindHook, null);
}
use of org.osgi.framework.Filter in project karaf by apache.
the class GuardProxyCatalog method getFilter.
private Filter getFilter(String string) throws InvalidSyntaxException {
Filter filter = filters.get(string);
if (filter == null) {
filter = myBundleContext.createFilter(string);
filters.put(string, filter);
}
return filter;
}
Aggregations