Search in sources :

Example 51 with BundleEvent

use of org.osgi.framework.BundleEvent in project sling by apache.

the class MockBundleContextTest method testBundleListener.

@Test
public void testBundleListener() throws Exception {
    BundleListener bundleListener = mock(BundleListener.class);
    BundleEvent bundleEvent = mock(BundleEvent.class);
    bundleContext.addBundleListener(bundleListener);
    MockOsgi.sendBundleEvent(bundleContext, bundleEvent);
    verify(bundleListener).bundleChanged(bundleEvent);
    bundleContext.removeBundleListener(bundleListener);
}
Also used : BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) Test(org.junit.Test)

Example 52 with BundleEvent

use of org.osgi.framework.BundleEvent in project sling by apache.

the class ModelAdapterFactoryUtil method addModelsForPackages.

/**
     * Search classpath for given java package names (and sub packages) to scan for and
     * register all classes with @Model annotation.
     * @param bundleContext Bundle context
     * @param packageNames Java package names
     */
public static void addModelsForPackages(BundleContext bundleContext, String... packageNames) {
    Bundle bundle = new RegisterModelsBundle(bundleContext, Bundle.ACTIVE, packageNames, null);
    BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle);
    MockOsgi.sendBundleEvent(bundleContext, event);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent)

Example 53 with BundleEvent

use of org.osgi.framework.BundleEvent in project sling by apache.

the class LogSupportTest method testBundleChanges.

@Test
public void testBundleChanges() throws Exception {
    logSupport.bundleChanged(new BundleEvent(BundleEvent.INSTALLED, bundle));
    Mockito.verify(testLogger).info("BundleEvent INSTALLED", (Throwable) null);
}
Also used : BundleEvent(org.osgi.framework.BundleEvent) Test(org.junit.Test)

Example 54 with BundleEvent

use of org.osgi.framework.BundleEvent in project sling by apache.

the class ModelAdapterFactoryUtil method addModelsForPackage.

/**
     * Scan classpaths for given package name (and sub packages) to scan for and
     * register all classes with @Model annotation.
     * @param packageName Java package name
     */
public static void addModelsForPackage(BundleContext bundleContext, Class... classes) {
    Bundle bundle = new ModelsPackageBundle(classes, Bundle.ACTIVE, bundleContext);
    BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle);
    MockOsgi.sendBundleEvent(bundleContext, event);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleEvent(org.osgi.framework.BundleEvent)

Example 55 with BundleEvent

use of org.osgi.framework.BundleEvent in project felix by apache.

the class EventDispatcher method createWhitelistFromHooks.

private <T> Set<BundleContext> createWhitelistFromHooks(EventObject event, Felix felix, Map<BundleContext, List<ListenerInfo>> listeners1, Map<BundleContext, List<ListenerInfo>> listeners2, Class<T> hookClass) {
    // Create a whitelist of bundle context, if we have hooks.
    Set<BundleContext> whitelist = null;
    Set<ServiceReference<T>> hooks = m_registry.getHookRegistry().getHooks(hookClass);
    if (!hooks.isEmpty()) {
        boolean systemBundleListener = false;
        BundleContext systemBundleContext = felix._getBundleContext();
        whitelist = new HashSet<BundleContext>();
        for (Entry<BundleContext, List<ListenerInfo>> entry : listeners1.entrySet()) {
            whitelist.add(entry.getKey());
            if (entry.getKey() == systemBundleContext)
                systemBundleListener = true;
        }
        if (listeners2 != null) {
            for (Entry<BundleContext, List<ListenerInfo>> entry : listeners2.entrySet()) {
                whitelist.add(entry.getKey());
                if (entry.getKey() == systemBundleContext)
                    systemBundleListener = true;
            }
        }
        int originalSize = whitelist.size();
        ShrinkableCollection<BundleContext> shrinkable = new ShrinkableCollection<BundleContext>(whitelist);
        for (ServiceReference<T> sr : hooks) {
            if (felix != null) {
                T eh = null;
                try {
                    eh = m_registry.getService(felix, sr, false);
                } catch (Exception ex) {
                // If we can't get the hook, then ignore it.
                }
                if (eh != null) {
                    try {
                        if (eh instanceof org.osgi.framework.hooks.service.EventHook) {
                            m_secureAction.invokeServiceEventHook((org.osgi.framework.hooks.service.EventHook) eh, (ServiceEvent) event, shrinkable);
                        } else if (eh instanceof org.osgi.framework.hooks.bundle.EventHook) {
                            m_secureAction.invokeBundleEventHook((org.osgi.framework.hooks.bundle.EventHook) eh, (BundleEvent) event, shrinkable);
                        }
                    } catch (Throwable th) {
                        m_logger.log(sr, Logger.LOG_WARNING, "Problem invoking event hook", th);
                    } finally {
                        m_registry.ungetService(felix, sr, null);
                    }
                }
            }
        }
        if (systemBundleListener && !whitelist.contains(systemBundleContext)) {
            // The system bundle cannot be removed from the listeners, so if it was
            // removed, add it back in. Note that this cannot be prevented by the shrinkable
            // since the effect of removing the system bundle from the listeners must be
            // visible between the event hooks.
            whitelist.add(systemBundleContext);
        }
        // to do whitelist lookups during event delivery.
        if (originalSize == whitelist.size()) {
            whitelist = null;
        }
    }
    return whitelist;
}
Also used : ServiceReference(org.osgi.framework.ServiceReference) BundleEvent(org.osgi.framework.BundleEvent) ArrayList(java.util.ArrayList) List(java.util.List) BundleContext(org.osgi.framework.BundleContext)

Aggregations

BundleEvent (org.osgi.framework.BundleEvent)83 Bundle (org.osgi.framework.Bundle)49 Test (org.junit.Test)24 SynchronousBundleListener (org.osgi.framework.SynchronousBundleListener)17 BundleListener (org.osgi.framework.BundleListener)15 FeatureDTO (com.amitinside.featureflags.dto.FeatureDTO)11 File (java.io.File)10 IOException (java.io.IOException)10 BundleException (org.osgi.framework.BundleException)10 BundleContext (org.osgi.framework.BundleContext)9 FrameworkEvent (org.osgi.framework.FrameworkEvent)7 StartLevel (org.osgi.service.startlevel.StartLevel)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 BundleTracker (org.osgi.util.tracker.BundleTracker)5 Map (java.util.Map)4 ServiceReference (org.osgi.framework.ServiceReference)4 BundleTrackerCustomizer (org.osgi.util.tracker.BundleTrackerCustomizer)4 FileInputStream (java.io.FileInputStream)3