下面列出了org.osgi.framework.hooks.bundle.EventHook#org.osgi.framework.hooks.service.EventListenerHook 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Workaround https://issues.apache.org/jira/browse/KARAF-4899 by
* re-adding probe bundle to root region before any service lookup
* (avoids spurious service lookup issues due to region filtering)
*/
private void installRegionWorkaround(final BundleContext context) {
Map<String, ?> maxRanking = singletonMap(SERVICE_RANKING, Integer.MAX_VALUE);
// add probe to root region on install
context.registerService(EventHook.class,
(event, contexts) -> {
if (event.getType() == BundleEvent.INSTALLED) {
fixProbeRegion(event.getBundle());
}
}, new Hashtable<>(maxRanking));
// add probe to root region whenever a service it's listening to changes
context.registerService(EventListenerHook.class,
(event, listeners) -> {
if (((String[]) event.getServiceReference().getProperty(Constants.OBJECTCLASS))[0].contains("pax.exam")) {
listeners.keySet().stream().map(BundleContext::getBundle).forEach(this::fixProbeRegion);
}
}, new Hashtable<>(maxRanking));
// add probe to root region whenever it's directly looking up a service
context.registerService(FindHook.class,
(findContext, name, filter, allServices, references) -> {
if ((name != null && name.contains("pax.exam")) || (filter != null && filter.contains("pax.exam"))) {
fixProbeRegion(findContext.getBundle());
}
}, new Hashtable<>(maxRanking));
}