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