use of javax.management.AttributeChangeNotificationFilter in project geode by apache.
the class MX4JModelMBean method removeAttributeChangeNotificationListener.
// Not in the spec but needed
private void removeAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback) throws MBeanException, RuntimeOperationsException, ListenerNotFoundException {
if (listener == null)
throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_LISTENER_CANNOT_BE_NULL.toLocalizedString()));
AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
if (attributeName != null) {
filter.enableAttribute(attributeName);
} else {
MBeanAttributeInfo[] ai = m_modelMBeanInfo.getAttributes();
for (int i = 0; i < ai.length; i++) {
Descriptor d = ((ModelMBeanAttributeInfo) ai[i]).getDescriptor();
filter.enableAttribute((String) d.getFieldValue("name"));
}
}
getAttributeChangeBroadcaster().removeNotificationListener(listener, filter, handback);
Logger logger = getLogger();
if (logger.isEnabledFor(Logger.DEBUG))
logger.debug("Listener " + listener + " for attribute " + attributeName + " removed successfully, handback is " + handback);
}
use of javax.management.AttributeChangeNotificationFilter in project jdk8u_jdk by JetBrains.
the class RequiredModelMBean method addAttributeChangeNotificationListener.
public void addAttributeChangeNotificationListener(NotificationListener inlistener, String inAttributeName, Object inhandback) throws MBeanException, RuntimeOperationsException, IllegalArgumentException {
final String mth = "addAttributeChangeNotificationListener(" + "NotificationListener, String, Object)";
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Entry");
}
if (inlistener == null)
throw new IllegalArgumentException("Listener to be registered must not be null");
if (attributeBroadcaster == null)
attributeBroadcaster = new NotificationBroadcasterSupport();
AttributeChangeNotificationFilter currFilter = new AttributeChangeNotificationFilter();
MBeanAttributeInfo[] attrInfo = modelMBeanInfo.getAttributes();
boolean found = false;
if (inAttributeName == null) {
if ((attrInfo != null) && (attrInfo.length > 0)) {
for (int i = 0; i < attrInfo.length; i++) {
currFilter.enableAttribute(attrInfo[i].getName());
}
}
} else {
if ((attrInfo != null) && (attrInfo.length > 0)) {
for (int i = 0; i < attrInfo.length; i++) {
if (inAttributeName.equals(attrInfo[i].getName())) {
found = true;
currFilter.enableAttribute(inAttributeName);
break;
}
}
}
if (!found) {
throw new RuntimeOperationsException(new IllegalArgumentException("The attribute name does not exist"), "Exception occurred trying to add an " + "AttributeChangeNotification listener");
}
}
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
Vector<String> enabledAttrs = currFilter.getEnabledAttributes();
String s = (enabledAttrs.size() > 1) ? "[" + enabledAttrs.firstElement() + ", ...]" : enabledAttrs.toString();
MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Set attribute change filter to " + s);
}
attributeBroadcaster.addNotificationListener(inlistener, currFilter, inhandback);
if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Notification listener added for " + inAttributeName);
MODELMBEAN_LOGGER.logp(Level.FINER, RequiredModelMBean.class.getName(), mth, "Exit");
}
}
use of javax.management.AttributeChangeNotificationFilter in project aries by apache.
the class BundleStateMBeanTest method testAttributeChangeNotifications.
@Test
public void testAttributeChangeNotifications() throws Exception {
final List<AttributeChangeNotification> attributeChanges = new ArrayList<AttributeChangeNotification>();
AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
filter.disableAllAttributes();
filter.enableAttribute("BundleIds");
mbeanServer.addNotificationListener(objectName, new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
attributeChanges.add((AttributeChangeNotification) notification);
}
}, filter, null);
long[] idsWithout = mbean.getBundleIds();
assertEquals("Precondition", 0, attributeChanges.size());
Manifest mf = new Manifest();
mf.getMainAttributes().putValue("Bundle-ManifestVersion", "2");
mf.getMainAttributes().putValue("Bundle-SymbolicName", "empty-test-bundle");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JarOutputStream jos = new JarOutputStream(baos, mf);
jos.closeEntry();
jos.close();
InputStream bais = new ByteArrayInputStream(baos.toByteArray());
Bundle bundle = bundleContext.installBundle("http://somelocation", bais);
long[] idsWith = new long[idsWithout.length + 1];
System.arraycopy(idsWithout, 0, idsWith, 0, idsWithout.length);
idsWith[idsWith.length - 1] = bundle.getBundleId();
Arrays.sort(idsWith);
waitForListToReachSize(attributeChanges, 1);
assertEquals(1, attributeChanges.size());
AttributeChangeNotification ac = attributeChanges.get(0);
assertEquals("BundleIds", ac.getAttributeName());
long oldSequence = ac.getSequenceNumber();
assertTrue(Arrays.equals(idsWithout, (long[]) ac.getOldValue()));
assertTrue(Arrays.equals(idsWith, (long[]) ac.getNewValue()));
bundle.uninstall();
waitForListToReachSize(attributeChanges, 2);
AttributeChangeNotification ac2 = attributeChanges.get(1);
assertEquals("BundleIds", ac2.getAttributeName());
assertEquals(oldSequence + 1, ac2.getSequenceNumber());
assertTrue(Arrays.equals(idsWith, (long[]) ac2.getOldValue()));
assertTrue(Arrays.equals(idsWithout, (long[]) ac2.getNewValue()));
}
use of javax.management.AttributeChangeNotificationFilter in project geode by apache.
the class MX4JModelMBean method addAttributeChangeNotificationListener.
public void addAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback) throws MBeanException, RuntimeOperationsException, IllegalArgumentException {
if (listener == null)
throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_LISTENER_CANNOT_BE_NULL.toLocalizedString()));
AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
if (attributeName != null) {
filter.enableAttribute(attributeName);
} else {
MBeanAttributeInfo[] ai = m_modelMBeanInfo.getAttributes();
for (int i = 0; i < ai.length; i++) {
Descriptor d = ((ModelMBeanAttributeInfo) ai[i]).getDescriptor();
filter.enableAttribute((String) d.getFieldValue("name"));
}
}
getAttributeChangeBroadcaster().addNotificationListener(listener, filter, handback);
Logger logger = getLogger();
if (logger.isEnabledFor(Logger.DEBUG))
logger.debug("Listener " + listener + " for attribute " + attributeName + " added successfully, handback is " + handback);
}
Aggregations