changeset 714:098e456e860e

6772779: @NotificationInfo does not create MBeanNotificationInfo in the MBean's MBeanInfo 6773593: CompositeDataSupport constructor javadoc is not in sync with the implementation Reviewed-by: sjiang
author emcmanus
date Thu, 20 Nov 2008 10:10:48 +0100
parents 4f985ba72055
children 9df22bc448a3
files src/share/classes/com/sun/jmx/interceptor/DefaultMBeanServerInterceptor.java src/share/classes/com/sun/jmx/mbeanserver/MBeanIntrospector.java src/share/classes/javax/management/openmbean/CompositeDataSupport.java test/javax/management/Introspector/AnnotatedNotificationInfoTest.java
diffstat 4 files changed, 82 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/jmx/interceptor/DefaultMBeanServerInterceptor.java	Wed Nov 19 14:29:12 2008 -0800
+++ b/src/share/classes/com/sun/jmx/interceptor/DefaultMBeanServerInterceptor.java	Thu Nov 20 10:10:48 2008 +0100
@@ -70,6 +70,7 @@
 import javax.management.ListenerNotFoundException;
 import javax.management.MBeanException;
 import javax.management.MBeanInfo;
+import javax.management.MBeanNotificationInfo;
 import javax.management.MBeanPermission;
 import javax.management.MBeanRegistration;
 import javax.management.MBeanRegistrationException;
@@ -1045,8 +1046,10 @@
             Object resource = getResource(mbean);
             MBeanInjector.inject(resource, mbs, name);
             if (MBeanInjector.injectsSendNotification(resource)) {
+                MBeanNotificationInfo[] mbnis =
+                        mbean.getMBeanInfo().getNotifications();
                 NotificationBroadcasterSupport nbs =
-                        new NotificationBroadcasterSupport();
+                        new NotificationBroadcasterSupport(mbnis);
                 MBeanInjector.injectSendNotification(resource, nbs);
                 mbean = NotifySupport.wrap(mbean, nbs);
             }
--- a/src/share/classes/com/sun/jmx/mbeanserver/MBeanIntrospector.java	Wed Nov 19 14:29:12 2008 -0800
+++ b/src/share/classes/com/sun/jmx/mbeanserver/MBeanIntrospector.java	Thu Nov 20 10:10:48 2008 +0100
@@ -44,6 +44,7 @@
 import javax.management.ImmutableDescriptor;
 import javax.management.IntrospectionException;
 import javax.management.InvalidAttributeValueException;
+import javax.management.JMX;
 import javax.management.MBean;
 import javax.management.MBeanAttributeInfo;
 import javax.management.MBeanConstructorInfo;
@@ -538,21 +539,22 @@
     }
 
     static MBeanNotificationInfo[] findNotifications(Object moi) {
-        if (!(moi instanceof NotificationBroadcaster))
-            return null;
-        MBeanNotificationInfo[] mbn =
-                ((NotificationBroadcaster) moi).getNotificationInfo();
-        if (mbn == null || mbn.length == 0)
-            return findNotificationsFromAnnotations(moi.getClass());
-        MBeanNotificationInfo[] result =
-                new MBeanNotificationInfo[mbn.length];
-        for (int i = 0; i < mbn.length; i++) {
-            MBeanNotificationInfo ni = mbn[i];
-            if (ni.getClass() != MBeanNotificationInfo.class)
-                ni = (MBeanNotificationInfo) ni.clone();
-            result[i] = ni;
+        if (moi instanceof NotificationBroadcaster) {
+            MBeanNotificationInfo[] mbn =
+                    ((NotificationBroadcaster) moi).getNotificationInfo();
+            if (mbn != null && mbn.length > 0) {
+                MBeanNotificationInfo[] result =
+                        new MBeanNotificationInfo[mbn.length];
+                for (int i = 0; i < mbn.length; i++) {
+                    MBeanNotificationInfo ni = mbn[i];
+                    if (ni.getClass() != MBeanNotificationInfo.class)
+                        ni = (MBeanNotificationInfo) ni.clone();
+                    result[i] = ni;
+                }
+                return result;
+            }
         }
-        return result;
+        return findNotificationsFromAnnotations(moi.getClass());
     }
 
     private static MBeanNotificationInfo[] findNotificationsFromAnnotations(
--- a/src/share/classes/javax/management/openmbean/CompositeDataSupport.java	Wed Nov 19 14:29:12 2008 -0800
+++ b/src/share/classes/javax/management/openmbean/CompositeDataSupport.java	Thu Nov 20 10:10:48 2008 +0100
@@ -101,7 +101,7 @@
      * the same size as <tt>itemNames</tt>; must not be null.
      *
      * @throws IllegalArgumentException <tt>compositeType</tt> is null, or
-     * <tt>itemNames[]</tt> or <tt>itemValues[]</tt> is null or empty, or one
+     * <tt>itemNames[]</tt> or <tt>itemValues[]</tt> is null, or one
      * of the elements in <tt>itemNames[]</tt> is a null or empty string, or
      * <tt>itemNames[]</tt> and <tt>itemValues[]</tt> are not of the same size.
      *
--- a/test/javax/management/Introspector/AnnotatedNotificationInfoTest.java	Wed Nov 19 14:29:12 2008 -0800
+++ b/test/javax/management/Introspector/AnnotatedNotificationInfoTest.java	Thu Nov 20 10:10:48 2008 +0100
@@ -22,8 +22,8 @@
  */
 
 /*
- * @test %M% %I%
- * @bug 6323980
+ * @test
+ * @bug 6323980 6772779
  * @summary Test &#64;NotificationInfo annotation
  * @author Eamonn McManus
  * @run main/othervm -ea AnnotatedNotificationInfoTest
@@ -32,6 +32,7 @@
 import java.io.Serializable;
 import java.lang.management.ManagementFactory;
 import java.lang.reflect.Field;
+import java.util.Arrays;
 import javax.annotation.Resource;
 import javax.management.AttributeChangeNotification;
 import javax.management.Description;
@@ -134,6 +135,23 @@
 
     private static Object mbeanIntf5 = new Intf5Impl();
 
+    @NotificationInfo(
+            types = {"foo", "bar"},
+            notificationClass = AttributeChangeNotification.class,
+            description = @Description(
+                value = "description",
+                bundleBaseName = "bundle",
+                key = "key"),
+            descriptorFields = {"foo=bar"})
+    public static interface Intf6MBean {}
+
+    public static class Intf6 implements Intf6MBean {
+        @Resource
+        private volatile SendNotification send;
+    }
+
+    private static Object mbeanIntf6 = new Intf6();
+
     public static interface Impl1MBean {}
 
     @NotificationInfo(
@@ -202,22 +220,21 @@
 
     private static Object mbeanMBean2 = new MBean2();
 
-    // Following disabled until we support it
-//    @MBean
-//    @NotificationInfo(
-//            types = {"foo", "bar"},
-//            notificationClass = AttributeChangeNotification.class,
-//            description = @Description(
-//                value = "description",
-//                bundleBaseName = "bundle",
-//                key = "key"),
-//            descriptorFields = {"foo=bar"})
-//    public static class MBean3 {
-//        @Resource
-//        private volatile SendNotification send;
-//    }
-//
-//    private static Object mbeanMBean3 = new MBean3();
+    @MBean
+    @NotificationInfo(
+            types = {"foo", "bar"},
+            notificationClass = AttributeChangeNotification.class,
+            description = @Description(
+                value = "description",
+                bundleBaseName = "bundle",
+                key = "key"),
+            descriptorFields = {"foo=bar"})
+    public static class MBean3 {
+        @Resource
+        private volatile SendNotification send;
+    }
+
+    private static Object mbeanMBean3 = new MBean3();
 
     @MXBean
     @NotificationInfo(
@@ -237,6 +254,23 @@
 
     private static Object mbeanMXBean2 = new MXBean2();
 
+    // Classes for the second test.  This tests the simplest case, which is
+    // the first example in the javadoc for @NotificationInfo.  Notice that
+    // this MBean is not a NotificationBroadcaster and does not inject a
+    // SendNotification!  That should possibly be an error, but it's currently
+    // allowed by the spec.
+    @NotificationInfo(types={"com.example.notifs.create",
+                             "com.example.notifs.destroy"})
+    public static interface CacheMBean {
+        public int getCachedNum();
+    }
+
+    public static class Cache implements CacheMBean {
+        public int getCachedNum() {
+            return 0;
+        }
+    }
+
     public static void main(String[] args) throws Exception {
         if (!AnnotatedNotificationInfoTest.class.desiredAssertionStatus())
             throw new Exception("Test must be run with -ea");
@@ -267,5 +301,14 @@
             assert mbnis[0].equals(expected) : mbnis[0];
             mbs.unregisterMBean(on);
         }
+
+        mbs.registerMBean(new Cache(), on);
+        MBeanInfo mbi = mbs.getMBeanInfo(on);
+        MBeanNotificationInfo[] mbnis = mbi.getNotifications();
+        assert mbnis.length == 1 : mbnis.length;
+        String[] types = mbnis[0].getNotifTypes();
+        String[] expectedTypes =
+                CacheMBean.class.getAnnotation(NotificationInfo.class).types();
+        assert Arrays.equals(types, expectedTypes) : Arrays.toString(types);
     }
 }