Search in sources :

Example 1 with StartLevel

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);
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) JMXAgent(org.apache.aries.jmx.agent.JMXAgent) Bundle(org.osgi.framework.Bundle) StartLevel(org.osgi.service.startlevel.StartLevel) JMXAgentContext(org.apache.aries.jmx.agent.JMXAgentContext) Logger(org.apache.aries.jmx.Logger) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 2 with StartLevel

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());
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) Bundle(org.osgi.framework.Bundle) Logger(org.apache.aries.jmx.Logger) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) ExecutorService(java.util.concurrent.ExecutorService) StartLevel(org.osgi.service.startlevel.StartLevel) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) BundleContext(org.osgi.framework.BundleContext) MBeanServer(javax.management.MBeanServer) NotificationListener(javax.management.NotificationListener)

Example 3 with StartLevel

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());
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) ExecutorService(java.util.concurrent.ExecutorService) StartLevel(org.osgi.service.startlevel.StartLevel) BundleListener(org.osgi.framework.BundleListener) Logger(org.apache.aries.jmx.Logger) BundleContext(org.osgi.framework.BundleContext) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 4 with StartLevel

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);
}
Also used : Field(java.lang.reflect.Field) Bundle(org.osgi.framework.Bundle) StartLevel(org.osgi.service.startlevel.StartLevel) Logger(org.slf4j.Logger) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 5 with StartLevel

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);
}
Also used : StartLevel(org.osgi.service.startlevel.StartLevel) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

StartLevel (org.osgi.service.startlevel.StartLevel)10 ServiceReference (org.osgi.framework.ServiceReference)6 PackageAdmin (org.osgi.service.packageadmin.PackageAdmin)6 Bundle (org.osgi.framework.Bundle)4 BundleContext (org.osgi.framework.BundleContext)4 Logger (org.apache.aries.jmx.Logger)3 Test (org.junit.Test)3 ExecutorService (java.util.concurrent.ExecutorService)2 MBeanServer (javax.management.MBeanServer)2 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)2 ObjectName (javax.management.ObjectName)2 BundleListener (org.osgi.framework.BundleListener)2 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Map (java.util.Map)1 Semaphore (java.util.concurrent.Semaphore)1 AttributeChangeNotification (javax.management.AttributeChangeNotification)1 Notification (javax.management.Notification)1 NotificationListener (javax.management.NotificationListener)1