Search in sources :

Example 1 with AttributeChangeNotificationFilter

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);
}
Also used : ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) AttributeChangeNotificationFilter(javax.management.AttributeChangeNotificationFilter) Descriptor(javax.management.Descriptor) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 2 with AttributeChangeNotificationFilter

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");
    }
}
Also used : AttributeChangeNotificationFilter(javax.management.AttributeChangeNotificationFilter) NotificationBroadcasterSupport(javax.management.NotificationBroadcasterSupport) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 3 with AttributeChangeNotificationFilter

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()));
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) AttributeChangeNotificationFilter(javax.management.AttributeChangeNotificationFilter) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) ByteArrayInputStream(java.io.ByteArrayInputStream) NotificationListener(javax.management.NotificationListener) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Example 4 with AttributeChangeNotificationFilter

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);
}
Also used : ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) AttributeChangeNotificationFilter(javax.management.AttributeChangeNotificationFilter) Descriptor(javax.management.Descriptor) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Aggregations

AttributeChangeNotificationFilter (javax.management.AttributeChangeNotificationFilter)4 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)3 RuntimeOperationsException (javax.management.RuntimeOperationsException)3 Descriptor (javax.management.Descriptor)2 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)2 FileLogger (mx4j.log.FileLogger)2 Logger (mx4j.log.Logger)2 MBeanLogger (mx4j.log.MBeanLogger)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 JarOutputStream (java.util.jar.JarOutputStream)1 Manifest (java.util.jar.Manifest)1 AttributeChangeNotification (javax.management.AttributeChangeNotification)1 Notification (javax.management.Notification)1 NotificationBroadcasterSupport (javax.management.NotificationBroadcasterSupport)1 NotificationListener (javax.management.NotificationListener)1 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)1 Test (org.junit.Test)1