Search in sources :

Example 6 with AttributeChangeNotification

use of javax.management.AttributeChangeNotification 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 7 with AttributeChangeNotification

use of javax.management.AttributeChangeNotification in project aries by apache.

the class ServiceStateTest method testNotificationsForServiceEvents.

@Test
public void testNotificationsForServiceEvents() throws Exception {
    StateConfig stateConfig = new StateConfig();
    //holders for Notifications captured
    List<Notification> received = new LinkedList<Notification>();
    List<AttributeChangeNotification> attributeChanges = new LinkedList<AttributeChangeNotification>();
    createService(stateConfig, received, attributeChanges);
    assertEquals(2, received.size());
    Notification registered = received.get(0);
    assertEquals(1, registered.getSequenceNumber());
    CompositeData data = (CompositeData) registered.getUserData();
    assertEquals(new Long(44), data.get(IDENTIFIER));
    assertEquals(new Long(9), data.get(BUNDLE_IDENTIFIER));
    assertEquals("file:/location", data.get(BUNDLE_LOCATION));
    assertEquals("bundle", data.get(BUNDLE_SYMBOLIC_NAME));
    assertArrayEquals(new String[] { "org.apache.aries.jmx.Mock" }, (String[]) data.get(OBJECT_CLASS));
    assertEquals(ServiceEvent.REGISTERED, data.get(EVENT));
    Notification modified = received.get(1);
    assertEquals(2, modified.getSequenceNumber());
    data = (CompositeData) modified.getUserData();
    assertEquals(new Long(44), data.get(IDENTIFIER));
    assertEquals(new Long(9), data.get(BUNDLE_IDENTIFIER));
    assertEquals("file:/location", data.get(BUNDLE_LOCATION));
    assertEquals("bundle", data.get(BUNDLE_SYMBOLIC_NAME));
    assertArrayEquals(new String[] { "org.apache.aries.jmx.Mock" }, (String[]) data.get(OBJECT_CLASS));
    assertEquals(ServiceEvent.MODIFIED, data.get(EVENT));
    assertEquals(1, attributeChanges.size());
    AttributeChangeNotification ac = attributeChanges.get(0);
    assertEquals("ServiceIds", ac.getAttributeName());
    assertEquals(0, ((long[]) ac.getOldValue()).length);
    assertEquals(1, ((long[]) ac.getNewValue()).length);
    assertEquals(44L, ((long[]) ac.getNewValue())[0]);
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) CompositeData(javax.management.openmbean.CompositeData) LinkedList(java.util.LinkedList) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) Test(org.junit.Test)

Example 8 with AttributeChangeNotification

use of javax.management.AttributeChangeNotification in project geode by apache.

the class MX4JModelMBean method sendAttributeChangeNotification.

public void sendAttributeChangeNotification(Attribute oldAttribute, Attribute newAttribute) throws MBeanException, RuntimeOperationsException {
    if (oldAttribute == null || newAttribute == null)
        throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_CANNOT_BE_NULL.toLocalizedString()));
    if (!oldAttribute.getName().equals(newAttribute.getName()))
        throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_NAMES_CANNOT_BE_DIFFERENT.toLocalizedString()));
    // TODO: the source must be the object name of the MBean if the listener was registered through
    // MBeanServer
    Object oldValue = oldAttribute.getValue();
    AttributeChangeNotification n = new AttributeChangeNotification(this, 1, System.currentTimeMillis(), "Attribute value changed", oldAttribute.getName(), oldValue == null ? null : oldValue.getClass().getName(), oldValue, newAttribute.getValue());
    sendAttributeChangeNotification(n);
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 9 with AttributeChangeNotification

use of javax.management.AttributeChangeNotification in project sling by apache.

the class QueuesMBeanImpl method removeAndNotify.

private void removeAndNotify(QueueMBeanHolder queueMBeanHolder) {
    String[] oldQueue = getQueueNames();
    remove(queueMBeanHolder);
    names = null;
    this.sendNotification(new AttributeChangeNotification(this, sequence.incrementAndGet(), System.currentTimeMillis(), "Queue " + queueMBeanHolder.name + " removed ", "queueNames", "String[]", oldQueue, getQueueNames()));
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification)

Example 10 with AttributeChangeNotification

use of javax.management.AttributeChangeNotification in project sling by apache.

the class QueuesMBeanImpl method addAndNotify.

private void addAndNotify(Queue queue) {
    String[] oldQueue = getQueueNames();
    QueueMBeanHolder queueMBeanHolder = add(queue);
    names = null;
    this.sendNotification(new AttributeChangeNotification(this, sequence.incrementAndGet(), System.currentTimeMillis(), "Queue " + queueMBeanHolder.name + " added ", "queueNames", "String[]", oldQueue, getQueueNames()));
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification)

Aggregations

AttributeChangeNotification (javax.management.AttributeChangeNotification)28 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)8 Test (org.junit.Test)6 ObjectName (javax.management.ObjectName)5 Bundle (org.osgi.framework.Bundle)4 ScanState (com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 NotificationFilter (javax.management.NotificationFilter)3 LinkedList (java.util.LinkedList)2 ExecutorService (java.util.concurrent.ExecutorService)2 MBeanServer (javax.management.MBeanServer)2 NotificationEmitter (javax.management.NotificationEmitter)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 CompositeData (javax.management.openmbean.CompositeData)2 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)2 Call (com.sun.jmx.examples.scandir.ScanManagerTest.Call)1 DirectoryScannerConfig (com.sun.jmx.examples.scandir.config.DirectoryScannerConfig)1