Search in sources :

Example 1 with NotificationBroadcasterSupport

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

use of javax.management.NotificationBroadcasterSupport in project jdk8u_jdk by JetBrains.

the class RMIConnector method initTransients.

// Initialization of transient variables.
private void initTransients() {
    rmbscMap = new WeakHashMap<Subject, WeakReference<MBeanServerConnection>>();
    connected = false;
    terminated = false;
    connectionBroadcaster = new NotificationBroadcasterSupport();
}
Also used : WeakReference(java.lang.ref.WeakReference) NotificationBroadcasterSupport(javax.management.NotificationBroadcasterSupport) Subject(javax.security.auth.Subject)

Example 3 with NotificationBroadcasterSupport

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

the class DistributedSystemDUnitTest method testNotificationHub.

@Test
public void testNotificationHub() throws Exception {
    this.managementTestRule.createMembers();
    this.managementTestRule.createManagers();
    class NotificationHubTestListener implements NotificationListener {

        @Override
        public synchronized void handleNotification(Notification notification, Object handback) {
            logger.info("Notification received {}", notification);
            notifications.add(notification);
        }
    }
    this.managerVM.invoke("addListenerToMemberMXBean", () -> {
        ManagementService service = this.managementTestRule.getManagementService();
        DistributedSystemMXBean distributedSystemMXBean = service.getDistributedSystemMXBean();
        await().until(() -> assertThat(distributedSystemMXBean.listMemberObjectNames()).hasSize(5));
        for (ObjectName objectName : distributedSystemMXBean.listMemberObjectNames()) {
            NotificationHubTestListener listener = new NotificationHubTestListener();
            getPlatformMBeanServer().addNotificationListener(objectName, listener, null, null);
            notificationListenerMap.put(objectName, listener);
        }
    });
    for (VM memberVM : this.memberVMs) {
        memberVM.invoke("checkNotificationHubListenerCount", () -> {
            SystemManagementService service = this.managementTestRule.getSystemManagementService();
            NotificationHub notificationHub = service.getNotificationHub();
            Map<ObjectName, NotificationHubListener> listenerMap = notificationHub.getListenerObjectMap();
            assertThat(listenerMap.keySet()).hasSize(1);
            ObjectName memberMBeanName = getMemberMBeanName(this.managementTestRule.getDistributedMember());
            NotificationHubListener listener = listenerMap.get(memberMBeanName);
            /*
         * Counter of listener should be 2 . One for default Listener which is added for each member
         * mbean by distributed system mbean One for the added listener in test
         */
            assertThat(listener.getNumCounter()).isEqualTo(2);
            // Raise some notifications
            NotificationBroadcasterSupport notifier = (MemberMBean) service.getMemberMXBean();
            String memberSource = getMemberNameOrId(this.managementTestRule.getDistributedMember());
            // Only a dummy notification , no actual region is created
            Notification notification = new Notification(JMXNotificationType.REGION_CREATED, memberSource, SequenceNumber.next(), System.currentTimeMillis(), ManagementConstants.REGION_CREATED_PREFIX + "/test");
            notifier.sendNotification(notification);
        });
    }
    this.managerVM.invoke("checkNotificationsAndRemoveListeners", () -> {
        await().until(() -> assertThat(notifications).hasSize(3));
        notifications.clear();
        for (ObjectName objectName : notificationListenerMap.keySet()) {
            NotificationListener listener = notificationListenerMap.get(objectName);
            getPlatformMBeanServer().removeNotificationListener(objectName, listener);
        }
    });
    for (VM memberVM : this.memberVMs) {
        memberVM.invoke("checkNotificationHubListenerCountAgain", () -> {
            SystemManagementService service = this.managementTestRule.getSystemManagementService();
            NotificationHub hub = service.getNotificationHub();
            Map<ObjectName, NotificationHubListener> listenerObjectMap = hub.getListenerObjectMap();
            assertThat(listenerObjectMap.keySet().size()).isEqualTo(1);
            ObjectName memberMBeanName = getMemberMBeanName(this.managementTestRule.getDistributedMember());
            NotificationHubListener listener = listenerObjectMap.get(memberMBeanName);
            /*
         * Counter of listener should be 1 for the default Listener which is added for each member
         * mbean by distributed system mbean.
         */
            assertThat(listener.getNumCounter()).isEqualTo(1);
        });
    }
    this.managerVM.invoke("removeListenerFromMemberMXBean", () -> {
        ManagementService service = this.managementTestRule.getManagementService();
        DistributedSystemMXBean distributedSystemMXBean = service.getDistributedSystemMXBean();
        await().until(() -> assertThat(distributedSystemMXBean.listMemberObjectNames()).hasSize(5));
        for (ObjectName objectName : distributedSystemMXBean.listMemberObjectNames()) {
            NotificationHubTestListener listener = new NotificationHubTestListener();
            try {
                // because new
                getPlatformMBeanServer().removeNotificationListener(objectName, listener);
            // instance!!
            } catch (ListenerNotFoundException e) {
            // TODO: [old] apparently there is never a notification listener on any these mbeans at
            // this point [fix this]
            // fix this test so it doesn't hit these unexpected exceptions -- getLogWriter().error(e);
            }
        }
    });
    for (VM memberVM : this.memberVMs) {
        memberVM.invoke("verifyNotificationHubListenersWereRemoved", () -> {
            SystemManagementService service = this.managementTestRule.getSystemManagementService();
            NotificationHub notificationHub = service.getNotificationHub();
            notificationHub.cleanUpListeners();
            assertThat(notificationHub.getListenerObjectMap()).isEmpty();
            for (ObjectName objectName : notificationListenerMap.keySet()) {
                NotificationListener listener = notificationListenerMap.get(objectName);
                assertThatThrownBy(() -> getPlatformMBeanServer().removeNotificationListener(objectName, listener)).isExactlyInstanceOf(ListenerNotFoundException.class);
            }
        });
    }
}
Also used : Notification(javax.management.Notification) SystemManagementService(org.apache.geode.management.internal.SystemManagementService) ObjectName(javax.management.ObjectName) NotificationHubListener(org.apache.geode.management.internal.NotificationHub.NotificationHubListener) SystemManagementService(org.apache.geode.management.internal.SystemManagementService) VM(org.apache.geode.test.dunit.VM) MemberMBean(org.apache.geode.management.internal.beans.MemberMBean) ListenerNotFoundException(javax.management.ListenerNotFoundException) NotificationBroadcasterSupport(javax.management.NotificationBroadcasterSupport) NotificationHub(org.apache.geode.management.internal.NotificationHub) NotificationListener(javax.management.NotificationListener) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 4 with NotificationBroadcasterSupport

use of javax.management.NotificationBroadcasterSupport in project jdk8u_jdk by JetBrains.

the class CommunicatorServer method readObject.

/**
     * Controls the way the CommunicatorServer service is deserialized.
     */
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
    // Call the default deserialization of the object.
    //
    stream.defaultReadObject();
    // Call the specific initialization for the CommunicatorServer service.
    // This is for transient structures to be initialized to specific
    // default values.
    //
    stateLock = new Object();
    state = OFFLINE;
    stopRequested = false;
    servedClientCount = 0;
    clientHandlerVector = new Vector<>();
    mainThread = null;
    notifCount = 0;
    notifInfos = null;
    notifBroadcaster = new NotificationBroadcasterSupport();
    dbgTag = makeDebugTag();
}
Also used : NotificationBroadcasterSupport(javax.management.NotificationBroadcasterSupport)

Example 5 with NotificationBroadcasterSupport

use of javax.management.NotificationBroadcasterSupport in project jdk8u_jdk by JetBrains.

the class StandardMBeanIntrospector method isDefinitelyImmutableInfo.

/* Return true if and only if we can be sure that the given MBean implementation
     * class has immutable MBeanInfo.  A Standard MBean that is a
     * NotificationBroadcaster is allowed to return different values at
     * different times from its getNotificationInfo() method, which is when
     * we might not know if it is immutable.  But if it is a subclass of
     * NotificationBroadcasterSupport and does not override
     * getNotificationInfo(), then we know it won't change.
     */
static boolean isDefinitelyImmutableInfo(Class<?> implClass) {
    if (!NotificationBroadcaster.class.isAssignableFrom(implClass))
        return true;
    synchronized (definitelyImmutable) {
        Boolean immutable = definitelyImmutable.get(implClass);
        if (immutable == null) {
            final Class<NotificationBroadcasterSupport> nbs = NotificationBroadcasterSupport.class;
            if (nbs.isAssignableFrom(implClass)) {
                try {
                    Method m = implClass.getMethod("getNotificationInfo");
                    immutable = (m.getDeclaringClass() == nbs);
                } catch (Exception e) {
                    // Too bad, we'll say no for now.
                    return false;
                }
            } else
                immutable = false;
            definitelyImmutable.put(implClass, immutable);
        }
        return immutable;
    }
}
Also used : NotificationBroadcaster(javax.management.NotificationBroadcaster) Method(java.lang.reflect.Method) NotificationBroadcasterSupport(javax.management.NotificationBroadcasterSupport) IntrospectionException(javax.management.IntrospectionException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MBeanException(javax.management.MBeanException)

Aggregations

NotificationBroadcasterSupport (javax.management.NotificationBroadcasterSupport)5 WeakReference (java.lang.ref.WeakReference)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 AttributeChangeNotificationFilter (javax.management.AttributeChangeNotificationFilter)1 IntrospectionException (javax.management.IntrospectionException)1 ListenerNotFoundException (javax.management.ListenerNotFoundException)1 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)1 MBeanException (javax.management.MBeanException)1 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)1 Notification (javax.management.Notification)1 NotificationBroadcaster (javax.management.NotificationBroadcaster)1 NotificationListener (javax.management.NotificationListener)1 ObjectName (javax.management.ObjectName)1 RuntimeOperationsException (javax.management.RuntimeOperationsException)1 Subject (javax.security.auth.Subject)1 NotificationHub (org.apache.geode.management.internal.NotificationHub)1 NotificationHubListener (org.apache.geode.management.internal.NotificationHub.NotificationHubListener)1 SystemManagementService (org.apache.geode.management.internal.SystemManagementService)1 MemberMBean (org.apache.geode.management.internal.beans.MemberMBean)1