Search in sources :

Example 1 with EventHook

use of org.osgi.framework.hooks.bundle.EventHook in project aries by apache.

the class BlueprintContainerUseSystemContextTest method regiserHook.

@Before
public void regiserHook() {
    final BundleContext systemContext = context().getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getBundleContext();
    eventHook = context().registerService(EventHook.class, new EventHook() {

        @Override
        public void event(BundleEvent event, Collection contexts) {
            if ("org.apache.aries.blueprint.sample".equals(event.getBundle().getSymbolicName())) {
                // hide sample from everything but the system bundle
                // TODO on R6 we should be able to even try hiding from the system bundle
                // R5 it was not clear if hooks could hide from the system bundle
                // equinox R5 does allow hiding from system bundle
                contexts.retainAll(Collections.singleton(systemContext));
            }
        }
    }, null);
    findHook = context().registerService(FindHook.class, new FindHook() {

        @Override
        public void find(BundleContext context, Collection bundles) {
            if (context.equals(systemContext)) {
                // equinox R5 does allow hiding from system bundle
                return;
            }
            for (Iterator iBundles = bundles.iterator(); iBundles.hasNext(); ) {
                if ("org.apache.aries.blueprint.sample".equals(((Bundle) iBundles.next()).getSymbolicName())) {
                    // hide sample from everything
                    iBundles.remove();
                }
            }
        }
    }, null);
}
Also used : EventHook(org.osgi.framework.hooks.bundle.EventHook) FindHook(org.osgi.framework.hooks.bundle.FindHook) Iterator(java.util.Iterator) BundleEvent(org.osgi.framework.BundleEvent) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Example 2 with EventHook

use of org.osgi.framework.hooks.bundle.EventHook in project aries by apache.

the class BundleHookBundleTracker method open.

/**
     * Open this {@code BundleTracker} and begin tracking bundles.
     *
     * <p>
     * Bundle which match the state criteria specified when this
     * {@code BundleTracker} was created are now tracked by this
     * {@code BundleTracker}.
     *
     * @throws java.lang.IllegalStateException If the {@code BundleContext} with
     *         which this {@code BundleTracker} was created is no longer valid.
     * @throws java.lang.SecurityException If the caller and this class do not
     *         have the appropriate
     *         {@code AdminPermission[context bundle,LISTENER]}, and the Java
     *         Runtime Environment supports permissions.
     */
@Override
public void open() {
    final Tracked t;
    synchronized (this) {
        if (tracked != null) {
            return;
        }
        t = new Tracked();
        synchronized (t) {
            EventHook hook = new BundleEventHook(t);
            sr = context.registerService(EventHook.class.getName(), hook, null);
            Bundle[] bundles = context.getBundles();
            if (bundles != null) {
                int length = bundles.length;
                for (int i = 0; i < length; i++) {
                    int state = bundles[i].getState();
                    if ((state & mask) == 0) {
                        /* null out bundles whose states are not interesting */
                        bundles[i] = null;
                    }
                }
                /* set tracked with the initial bundles */
                t.setInitial(bundles);
            }
        }
        tracked = t;
    }
    /* Call tracked outside of synchronized region */
    tracked.trackInitial();
/* process the initial references */
}
Also used : EventHook(org.osgi.framework.hooks.bundle.EventHook) Bundle(org.osgi.framework.Bundle)

Aggregations

EventHook (org.osgi.framework.hooks.bundle.EventHook)2 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Before (org.junit.Before)1 Bundle (org.osgi.framework.Bundle)1 BundleContext (org.osgi.framework.BundleContext)1 BundleEvent (org.osgi.framework.BundleEvent)1 FindHook (org.osgi.framework.hooks.bundle.FindHook)1