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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations