use of org.osgi.service.startlevel.StartLevel in project aries by apache.
the class BundleStateMBeanHandlerTest method testOpenAndClose.
@Test
public void testOpenAndClose() throws Exception {
BundleContext context = mock(BundleContext.class);
when(context.getProperty(Constants.FRAMEWORK_UUID)).thenReturn("some-uuid");
Logger logger = mock(Logger.class);
Bundle mockSystemBundle = mock(Bundle.class);
when(mockSystemBundle.getSymbolicName()).thenReturn("the.sytem.bundle");
when(context.getBundle(0)).thenReturn(mockSystemBundle);
ServiceReference packageAdminRef = mock(ServiceReference.class);
PackageAdmin packageAdmin = mock(PackageAdmin.class);
when(context.getServiceReference(PackageAdmin.class.getName())).thenReturn(packageAdminRef);
when(context.getService(packageAdminRef)).thenReturn(packageAdmin);
ServiceReference startLevelRef = mock(ServiceReference.class);
StartLevel startLevel = mock(StartLevel.class);
when(context.getServiceReference(StartLevel.class.getName())).thenReturn(startLevelRef);
when(context.getService(startLevelRef)).thenReturn(startLevel);
JMXAgent agent = mock(JMXAgent.class);
JMXAgentContext agentContext = new JMXAgentContext(context, agent, logger);
BundleStateMBeanHandler handler = new BundleStateMBeanHandler(agentContext, new StateConfig());
handler.open();
assertNotNull(handler.getMbean());
handler.close();
verify(context).ungetService(packageAdminRef);
verify(context).ungetService(startLevelRef);
}
use of org.osgi.service.startlevel.StartLevel in project aries by apache.
the class BundleStateTest method createBundle.
private void createBundle(StateConfig stateConfig, final List<Notification> received, final List<AttributeChangeNotification> attributeChanges) throws Exception {
BundleContext context = mock(BundleContext.class);
when(context.getBundles()).thenReturn(new Bundle[] {});
PackageAdmin admin = mock(PackageAdmin.class);
StartLevel startLevel = mock(StartLevel.class);
Logger logger = mock(Logger.class);
BundleState bundleState = new BundleState(context, admin, startLevel, stateConfig, logger);
Bundle b1 = mock(Bundle.class);
when(b1.getBundleId()).thenReturn(new Long(9));
when(b1.getSymbolicName()).thenReturn("bundle");
when(b1.getLocation()).thenReturn("file:/location");
BundleEvent installedEvent = mock(BundleEvent.class);
when(installedEvent.getBundle()).thenReturn(b1);
when(installedEvent.getType()).thenReturn(BundleEvent.INSTALLED);
BundleEvent resolvedEvent = mock(BundleEvent.class);
when(resolvedEvent.getBundle()).thenReturn(b1);
when(resolvedEvent.getType()).thenReturn(BundleEvent.RESOLVED);
MBeanServer server = mock(MBeanServer.class);
//setup for notification
ObjectName objectName = new ObjectName(OBJECTNAME);
bundleState.preRegister(server, objectName);
bundleState.postRegister(true);
//add NotificationListener to receive the events
bundleState.addNotificationListener(new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
if (notification instanceof AttributeChangeNotification) {
attributeChanges.add((AttributeChangeNotification) notification);
} else {
received.add(notification);
}
}
}, null, null);
// capture the BundleListener registered with BundleContext to issue BundleEvents
ArgumentCaptor<BundleListener> argument = ArgumentCaptor.forClass(BundleListener.class);
verify(context).addBundleListener(argument.capture());
//send events
BundleListener listener = argument.getValue();
listener.bundleChanged(installedEvent);
listener.bundleChanged(resolvedEvent);
//shutdown dispatcher via unregister callback
bundleState.postDeregister();
//check the BundleListener is cleaned up
verify(context).removeBundleListener(listener);
ExecutorService dispatcher = bundleState.getEventDispatcher();
assertTrue(dispatcher.isShutdown());
dispatcher.awaitTermination(2, TimeUnit.SECONDS);
assertTrue(dispatcher.isTerminated());
}
use of org.osgi.service.startlevel.StartLevel in project aries by apache.
the class BundleStateTest method testLifeCycleOfNotificationSupport.
@Test
public void testLifeCycleOfNotificationSupport() throws Exception {
BundleContext context = mock(BundleContext.class);
PackageAdmin admin = mock(PackageAdmin.class);
StartLevel startLevel = mock(StartLevel.class);
Logger logger = mock(Logger.class);
BundleState bundleState = new BundleState(context, admin, startLevel, new StateConfig(), logger);
MBeanServer server1 = mock(MBeanServer.class);
MBeanServer server2 = mock(MBeanServer.class);
ObjectName objectName = new ObjectName(OBJECTNAME);
bundleState.preRegister(server1, objectName);
bundleState.postRegister(true);
// capture the BundleListener registered with BundleContext
ArgumentCaptor<BundleListener> argument = ArgumentCaptor.forClass(BundleListener.class);
verify(context).addBundleListener(argument.capture());
assertEquals(1, argument.getAllValues().size());
BundleListener listener = argument.getValue();
assertNotNull(listener);
ExecutorService dispatcher = bundleState.getEventDispatcher();
//do registration with another server
bundleState.preRegister(server2, objectName);
bundleState.postRegister(true);
// check no more actions on BundleContext
argument = ArgumentCaptor.forClass(BundleListener.class);
verify(context, atMost(1)).addBundleListener(argument.capture());
assertEquals(1, argument.getAllValues().size());
//do one unregister
bundleState.postDeregister();
//verify bundleListener not invoked
verify(context, never()).removeBundleListener(listener);
assertFalse(dispatcher.isShutdown());
//do second unregister and check cleanup
bundleState.postDeregister();
verify(context).removeBundleListener(listener);
assertTrue(dispatcher.isShutdown());
dispatcher.awaitTermination(2, TimeUnit.SECONDS);
assertTrue(dispatcher.isTerminated());
}
use of org.osgi.service.startlevel.StartLevel in project sling by apache.
the class LogSupportTest method prepare.
@Before
@SuppressWarnings("unchecked")
public void prepare() throws Exception {
bundle = Mockito.mock(Bundle.class);
Mockito.when(bundle.getSymbolicName()).thenReturn("foo.bundle");
Mockito.when(bundle.getBundleId()).thenReturn(42L);
StartLevel startLevel = Mockito.mock(StartLevel.class);
logSupport = new LogSupport(startLevel);
Field loggerField = LogSupport.class.getDeclaredField("loggers");
loggerField.setAccessible(true);
Map<Long, Logger> loggers = (Map<Long, Logger>) loggerField.get(logSupport);
testLogger = getMockInfoLogger();
loggers.put(bundle.getBundleId(), testLogger);
}
use of org.osgi.service.startlevel.StartLevel in project sling by apache.
the class OsgiInstallerImpl method executeTasks.
/**
* Execute all tasks
* @param tasks The tasks to executed.
* @return The action to perform after the execution.
*/
private ACTION executeTasks(final SortedSet<InstallTask> tasks) {
if (this.switchStartLevel && this.hasBundleUpdateTask(tasks)) {
// StartLevel service is always available
final ServiceReference ref = ctx.getServiceReference(StartLevel.class.getName());
final StartLevel startLevel = (StartLevel) ctx.getService(ref);
try {
final int targetStartLevel = this.getLowestStartLevel(tasks, startLevel);
final int currentStartLevel = startLevel.getStartLevel();
if (targetStartLevel < currentStartLevel) {
auditLogger.info("Switching to start level {}", targetStartLevel);
try {
startLevel.setStartLevel(targetStartLevel);
// now we have to wait until the start level is reached
while (startLevel.getStartLevel() > targetStartLevel) {
try {
Thread.sleep(300);
} catch (final InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
return doExecuteTasks(tasks);
} finally {
// restore old start level in any case
startLevel.setStartLevel(currentStartLevel);
auditLogger.info("Switching back to start level {} after performing the required " + "installation tasks", currentStartLevel);
}
}
} finally {
ctx.ungetService(ref);
}
}
return doExecuteTasks(tasks);
}
Aggregations