org.osgi.framework.hooks.service.EventListenerHook Java Examples

The following examples show how to use org.osgi.framework.hooks.service.EventListenerHook. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DelayedProbeInvokerFactory.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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));
}