use of org.osgi.framework.Filter in project aries by apache.
the class ConfigurationTests method testOptionalConfiguration.
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testOptionalConfiguration() throws Exception {
Bundle tb5Bundle = installBundle("tb5.jar");
Configuration configurationC = null;
try {
Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb5Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=CREATED))");
ServiceTracker<CdiContainer, CdiContainer> containerTracker = new ServiceTracker<>(bundleContext, filter, null);
containerTracker.open();
containerTracker.waitForService(timeout);
ServiceTracker<BeanService, BeanService> stC = new ServiceTracker<BeanService, BeanService>(bundleContext, bundleContext.createFilter("(&(objectClass=org.apache.aries.cdi.test.interfaces.BeanService)(bean=C))"), null);
stC.open(true);
BeanService<Callable<int[]>> beanService = stC.waitForService(timeout);
int t = stC.getTrackingCount();
assertNotNull(beanService);
assertEquals("blue", beanService.doSomething());
assertArrayEquals(new int[] { 35777 }, beanService.get().call());
configurationC = configurationAdmin.getConfiguration("foo.bar", "?");
Dictionary<String, Object> properties = new Hashtable<>();
properties.put("ports", new int[] { 12, 4567 });
configurationC.update(properties);
while (t == stC.getTrackingCount()) {
Thread.sleep(10);
}
t = stC.getTrackingCount();
while (t == stC.getTrackingCount()) {
Thread.sleep(10);
}
t = stC.getTrackingCount();
beanService = stC.waitForService(timeout);
assertNotNull(beanService);
assertEquals("blue", beanService.doSomething());
assertArrayEquals(new int[] { 12, 4567 }, beanService.get().call());
configurationC.delete();
while (t == stC.getTrackingCount()) {
Thread.sleep(10);
}
beanService = stC.waitForService(timeout);
assertNotNull(beanService);
assertEquals("blue", beanService.doSomething());
assertArrayEquals(new int[] { 35777 }, beanService.get().call());
} finally {
if (configurationC != null) {
try {
configurationC.delete();
} catch (Exception e) {
// ignore
}
}
tb5Bundle.uninstall();
}
}
use of org.osgi.framework.Filter in project karaf by apache.
the class EncryptablePropertyPlaceholderTest method getOsgiService.
protected <T> T getOsgiService(Class<T> type, String filter, long timeout) {
ServiceTracker tracker = null;
try {
String flt;
if (filter != null) {
if (filter.startsWith("(")) {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
} else {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
}
} else {
flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
}
Filter osgiFilter = FrameworkUtil.createFilter(flt);
tracker = new ServiceTracker(bundleContext, osgiFilter, null);
tracker.open(true);
// Note that the tracker is not closed to keep the reference
// This is buggy, as the service reference may change i think
Object svc = type.cast(tracker.waitForService(timeout));
if (svc == null) {
Dictionary dic = bundleContext.getBundle().getHeaders();
System.err.println("Test bundle headers: " + explode(dic));
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
System.err.println("ServiceReference: " + ref);
}
for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
System.err.println("Filtered ServiceReference: " + ref);
}
throw new RuntimeException("Gave up waiting for service " + flt);
}
return type.cast(svc);
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException("Invalid filter", e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
use of org.osgi.framework.Filter in project karaf by apache.
the class Wait method execute.
@Override
public Object execute() throws Exception {
ServiceTracker<?, ?> tracker = null;
try {
String filter = service;
if (!filter.startsWith("(")) {
if (!filter.contains("=")) {
filter = Constants.OBJECTCLASS + "=" + filter;
}
filter = "(" + filter + ")";
}
Filter osgiFilter = FrameworkUtil.createFilter(filter);
tracker = new ServiceTracker<>(bundleContext, osgiFilter, null);
tracker.open(true);
Object svc = tracker.getService();
if (timeout >= 0) {
svc = tracker.waitForService(timeout);
}
if (exception && svc == null) {
throw new TimeoutException("Can not find service '" + service + "' in the OSGi registry");
}
return svc != null;
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException("Invalid filter", e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if (tracker != null) {
tracker.close();
}
}
}
use of org.osgi.framework.Filter in project karaf by apache.
the class GuardingFindHookTest method testFindHook.
@SuppressWarnings("unchecked")
@Test
public void testFindHook() throws Exception {
Dictionary<String, Object> config = new Hashtable<>();
config.put("service.guard", "(|(moo=foo)(foo=*))");
BundleContext hookBC = mockConfigAdminBundleContext(config);
GuardProxyCatalog gpc = new GuardProxyCatalog(hookBC);
Filter serviceFilter = FrameworkUtil.createFilter("(foo=*)");
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("moo", "foo");
ServiceReference<?> sref = mockServiceReference(props);
Collection<ServiceReference<?>> refs = new ArrayList<>();
refs.add(sref);
assertEquals("Precondition", 0, gpc.proxyMap.size());
gfh.find(clientBC, null, null, true, refs);
assertEquals("The service doesn't match the filter so should have no effect", 0, gpc.proxyMap.size());
assertEquals("The service doesn't match the filter so should be presented to the client", Collections.singletonList(sref), refs);
long service2ID = 17L;
Dictionary<String, Object> props2 = new Hashtable<>();
props2.put(Constants.SERVICE_ID, service2ID);
props2.put("foo", new Object());
ServiceReference<?> sref2 = mockServiceReference(props2);
Collection<ServiceReference<?>> refs2 = new ArrayList<>();
refs2.add(sref2);
gfh.find(clientBC, null, null, true, refs2);
assertEquals("The service should be hidden from the client", 0, refs2.size());
assertEquals("The service should have caused a proxy creation", 1, gpc.proxyMap.size());
assertEquals("A proxy creation job should have been created", 1, gpc.createProxyQueue.size());
assertEquals(sref2.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next());
Collection<ServiceReference<?>> refs3 = new ArrayList<>();
refs3.add(sref2);
// Ensure that the hook bundle has nothing hidden
gfh.find(hookBC, null, null, true, refs3);
assertEquals("The service should not be hidden from the hook bundle", Collections.singletonList(sref2), refs3);
assertEquals("No proxy creation caused in this case", 1, gpc.proxyMap.size());
assertEquals("No change expected", sref2.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next());
// Ensure that the system bundle has nothing hidden
gfh.find(mockBundleContext(0L), null, null, true, refs3);
assertEquals("The service should not be hidden from the framework bundle", Collections.singletonList(sref2), refs3);
assertEquals("No proxy creation caused in this case", 1, gpc.proxyMap.size());
assertEquals("No change expected", sref2.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next());
// Ensure that if we ask for the same client again, it will not create another proxy
// Manually empty the queue
gpc.createProxyQueue.clear();
gfh.find(clientBC, null, null, true, refs3);
assertEquals("The service should be hidden from the client", 0, refs3.size());
assertEquals("There is already a proxy for this client, no need for an additional one", 1, gpc.proxyMap.size());
assertEquals("No additional jobs should have been scheduled", 0, gpc.createProxyQueue.size());
assertEquals("No change expected", sref2.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next());
Collection<ServiceReference<?>> refs4 = new ArrayList<>();
refs4.add(sref2);
// another client should not get another proxy
BundleContext client2BC = mockBundleContext(32768L);
gfh.find(client2BC, null, null, true, refs4);
assertEquals("The service should be hidden for this new client", 0, refs4.size());
assertEquals("No proxy creation job should have been created", 0, gpc.createProxyQueue.size());
assertEquals("No proxy creation caused in this case", 1, gpc.proxyMap.size());
assertEquals("No change expected", sref2.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next());
}
use of org.osgi.framework.Filter in project sling by apache.
the class AdapterManagerTest method createComponentContext.
/**
* Helper method to create a mock component context
*/
protected ComponentContext createComponentContext() throws Exception {
final BundleContext bundleCtx = this.context.mock(BundleContext.class);
final Filter filter = this.context.mock(Filter.class);
final ComponentContext ctx = this.context.mock(ComponentContext.class);
this.context.checking(new Expectations() {
{
allowing(ctx).locateService(with(any(String.class)), with(any(ServiceReference.class)));
will(returnValue(new MockAdapterFactory()));
allowing(ctx).getBundleContext();
will(returnValue(bundleCtx));
allowing(bundleCtx).createFilter(with(any(String.class)));
will(returnValue(filter));
allowing(bundleCtx).addServiceListener(with(any(ServiceListener.class)), with(any(String.class)));
allowing(bundleCtx).getServiceReferences(with(any(String.class)), with(any(String.class)));
will(returnValue(null));
allowing(bundleCtx).removeServiceListener(with(any(ServiceListener.class)));
allowing(bundleCtx).registerService(with(Adaption.class), with(AdaptionImpl.INSTANCE), with(any(Dictionary.class)));
will(returnValue(null));
}
});
return ctx;
}
Aggregations