org.osgi.framework.hooks.bundle.EventHook Java Examples

The following examples show how to use org.osgi.framework.hooks.bundle.EventHook. 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: BundleCollisionHook.java    From vespa with Apache License 2.0 5 votes vote down vote up
public void start(BundleContext context) {
    if (registration != null) {
        throw new IllegalStateException();
    }
    String[] serviceClasses = {CollisionHook.class.getName(), EventHook.class.getName(), FindHook.class.getName()};
    registration = context.registerService(serviceClasses, this, null);
}
 
Example #2
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));
}
 
Example #3
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * notify all bundle listeners.
 * 
 * @param state
 *            the new state.
 * @param bundle
 *            the bundle.
 */
void notifyBundleListeners(final int state, final Bundle bundle,
		final Bundle origin) {
	if (syncBundleListeners.isEmpty() && bundleListeners.isEmpty()) {
		return;
	}

	final BundleEvent event = new BundleEvent(state, bundle, origin);

	final SynchronousBundleListener[] syncs;
	final BundleListener[] asyncs;

	// call the hooks, if any
	if (!bundleEventHooks.isEmpty()) {
		final ArrayList<SynchronousBundleListener> syncListeners = new ArrayList<SynchronousBundleListener>(
				syncBundleListeners);
		final ArrayList<BundleListener> asyncListeners = new ArrayList<BundleListener>(
				bundleListeners);

		final ConciergeCollections.DeltaTrackingRemoveOnlyList<BundleContext> contexts = new ConciergeCollections.DeltaTrackingRemoveOnlyList<BundleContext>(
				bundleListenerMap.keySet());

		for (final ServiceReferenceImpl<org.osgi.framework.hooks.bundle.EventHook> sref : bundleEventHooks) {
			final org.osgi.framework.hooks.bundle.EventHook eventHook = sref
					.getService(Concierge.this);
			if (eventHook != null) {
				try {
					eventHook.event(event, contexts);
				} catch (final Throwable t) {
					// TODO: to log?
				}
			}
			sref.ungetService(Concierge.this);
		}

		for (final BundleContext removed : contexts.getRemoved()) {
			if(removed != this.context){ // system bundle contexts listeners always gets events
				for (final BundleListener listener : bundleListenerMap
					.get(removed)) {
					syncListeners.remove(listener);
					asyncListeners.remove(listener);
				}
			}
		}

		syncs = syncListeners.toArray(
				new SynchronousBundleListener[syncListeners.size()]);
		asyncs = asyncListeners
				.toArray(new BundleListener[asyncListeners.size()]);
	} else {
		syncs = syncBundleListeners.toArray(
				new SynchronousBundleListener[syncBundleListeners.size()]);
		asyncs = bundleListeners
				.toArray(new BundleListener[bundleListeners.size()]);
	}

	for (int i = 0; i < syncs.length; i++) {
		syncs[i].bundleChanged(event);
	}

	// asynchronous listeners do not get these events
	final int type = event.getType();
	if (bundleListeners.isEmpty() || (type & (BundleEvent.STARTING
			| BundleEvent.STOPPING | BundleEvent.LAZY_ACTIVATION)) > 0) {
		return;
	}

	for (int i = 0; i < asyncs.length; i++) {
		asyncs[i].bundleChanged(event);
	}
}