1 /*
   2  * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4757273
  27  * @summary Test that registered notification is sent early enough
  28  * @author Eamonn McManus
  29  * @run clean NewMBeanListenerTest
  30  * @run build NewMBeanListenerTest
  31  * @run main NewMBeanListenerTest
  32  */
  33 
  34 import javax.management.*;
  35 
  36 /* Tests that you can write a listener for MBean registrations, that
  37  * registers another listener on every newly-registered MBean (that is
  38  * a NotificationBroadcaster).  Provided the newly-registered MBean
  39  * waits until its postRegister is called, no notifications will be lost.
  40  */
  41 public class NewMBeanListenerTest {
  42     public static void main(String[] args) throws Exception {
  43         final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
  44         final ObjectName delegateName =
  45             new ObjectName("JMImplementation:type=MBeanServerDelegate");
  46         final CountListener countListener = new CountListener();
  47         final NotificationListener addListener = new NotificationListener() {
  48             public void handleNotification(Notification n, Object h) {
  49                 if (!(n instanceof MBeanServerNotification)) {
  50                     System.out.println("Ignoring delegate notif: " +
  51                                        n.getClass().getName());
  52                     return;
  53                 }
  54                 MBeanServerNotification mbsn = (MBeanServerNotification) n;
  55                 if (!(mbsn.getType()
  56                       .equals(MBeanServerNotification
  57                               .REGISTRATION_NOTIFICATION))) {
  58                     System.out.println("Ignoring MBeanServer notif: " +
  59                                        mbsn.getType());
  60                     return;
  61                 }
  62                 System.out.println("Got registration notif for " +
  63                                    mbsn.getMBeanName());
  64                 try {
  65                     mbs.addNotificationListener(mbsn.getMBeanName(),
  66                                                 countListener, null, null);
  67                 } catch (Exception e) {
  68                     System.out.println("TEST INCORRECT: addNL failed:");
  69                     e.printStackTrace(System.out);
  70                     System.exit(1);
  71                 }
  72                 System.out.println("Added notif listener for " +
  73                                    mbsn.getMBeanName());
  74             }
  75         };
  76         System.out.println("Adding registration listener");
  77         mbs.addNotificationListener(delegateName, addListener, null, null);
  78         final ObjectName broadcasterName = new ObjectName(":type=Broadcaster");
  79         System.out.println("Creating Broadcaster MBean");
  80         mbs.createMBean(Broadcaster.class.getName(), broadcasterName);
  81         if (countListener.getCount() == 1)
  82             System.out.println("Got notif as expected");
  83         else {
  84             System.out.println("TEST FAILED: added listener not called");
  85             System.exit(1);
  86         }
  87         mbs.unregisterMBean(broadcasterName);
  88         Broadcaster b = new Broadcaster();
  89         System.out.println("Registering Broadcaster MBean");
  90         mbs.registerMBean(b, broadcasterName);
  91         if (countListener.getCount() == 2)
  92             System.out.println("Got notif as expected");
  93         else {
  94             System.out.println("TEST FAILED: added listener not called");
  95             System.exit(1);
  96         }
  97         System.out.println("Test passed");
  98     }
  99 
 100     public static interface BroadcasterMBean {}
 101 
 102     public static class Broadcaster
 103             extends NotificationBroadcasterSupport
 104             implements BroadcasterMBean, MBeanRegistration {
 105 
 106         public ObjectName preRegister(MBeanServer mbs, ObjectName name) {
 107             return name;
 108         }
 109 
 110         public void postRegister(Boolean registrationDone) {
 111             System.out.println("Broadcaster.postRegister: sending notif");
 112             sendNotification(new Notification("x", this, 0L));
 113         }
 114 
 115         public void preDeregister() {
 116         }
 117 
 118         public void postDeregister() {
 119         }
 120     }
 121 
 122     private static class CountListener implements NotificationListener {
 123         private int count;
 124 
 125         public synchronized void handleNotification(Notification n, Object h) {
 126             if (!n.getType().equals("x")) {
 127                 System.out.println("TEST FAILED: received bogus notif: " + n +
 128                                    " (type=" + n.getType() + ")");
 129                 System.exit(1);
 130             }
 131             count++;
 132         }
 133 
 134         public synchronized int getCount() {
 135             return count;
 136         }
 137     }
 138 }