0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4757273
0N/A * @summary Test that registered notification is sent early enough
0N/A * @author Eamonn McManus
0N/A * @run clean NewMBeanListenerTest
0N/A * @run build NewMBeanListenerTest
0N/A * @run main NewMBeanListenerTest
0N/A */
0N/A
0N/Aimport javax.management.*;
0N/A
0N/A/* Tests that you can write a listener for MBean registrations, that
0N/A * registers another listener on every newly-registered MBean (that is
0N/A * a NotificationBroadcaster). Provided the newly-registered MBean
0N/A * waits until its postRegister is called, no notifications will be lost.
0N/A */
0N/Apublic class NewMBeanListenerTest {
0N/A public static void main(String[] args) throws Exception {
0N/A final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A final ObjectName delegateName =
0N/A new ObjectName("JMImplementation:type=MBeanServerDelegate");
0N/A final CountListener countListener = new CountListener();
0N/A final NotificationListener addListener = new NotificationListener() {
0N/A public void handleNotification(Notification n, Object h) {
0N/A if (!(n instanceof MBeanServerNotification)) {
0N/A System.out.println("Ignoring delegate notif: " +
0N/A n.getClass().getName());
0N/A return;
0N/A }
0N/A MBeanServerNotification mbsn = (MBeanServerNotification) n;
0N/A if (!(mbsn.getType()
0N/A .equals(MBeanServerNotification
0N/A .REGISTRATION_NOTIFICATION))) {
0N/A System.out.println("Ignoring MBeanServer notif: " +
0N/A mbsn.getType());
0N/A return;
0N/A }
0N/A System.out.println("Got registration notif for " +
0N/A mbsn.getMBeanName());
0N/A try {
0N/A mbs.addNotificationListener(mbsn.getMBeanName(),
0N/A countListener, null, null);
0N/A } catch (Exception e) {
0N/A System.out.println("TEST INCORRECT: addNL failed:");
0N/A e.printStackTrace(System.out);
0N/A System.exit(1);
0N/A }
0N/A System.out.println("Added notif listener for " +
0N/A mbsn.getMBeanName());
0N/A }
0N/A };
0N/A System.out.println("Adding registration listener");
0N/A mbs.addNotificationListener(delegateName, addListener, null, null);
0N/A final ObjectName broadcasterName = new ObjectName(":type=Broadcaster");
0N/A System.out.println("Creating Broadcaster MBean");
0N/A mbs.createMBean(Broadcaster.class.getName(), broadcasterName);
0N/A if (countListener.getCount() == 1)
0N/A System.out.println("Got notif as expected");
0N/A else {
0N/A System.out.println("TEST FAILED: added listener not called");
0N/A System.exit(1);
0N/A }
0N/A mbs.unregisterMBean(broadcasterName);
0N/A Broadcaster b = new Broadcaster();
0N/A System.out.println("Registering Broadcaster MBean");
0N/A mbs.registerMBean(b, broadcasterName);
0N/A if (countListener.getCount() == 2)
0N/A System.out.println("Got notif as expected");
0N/A else {
0N/A System.out.println("TEST FAILED: added listener not called");
0N/A System.exit(1);
0N/A }
0N/A System.out.println("Test passed");
0N/A }
0N/A
0N/A public static interface BroadcasterMBean {}
0N/A
0N/A public static class Broadcaster
0N/A extends NotificationBroadcasterSupport
0N/A implements BroadcasterMBean, MBeanRegistration {
0N/A
0N/A public ObjectName preRegister(MBeanServer mbs, ObjectName name) {
0N/A return name;
0N/A }
0N/A
0N/A public void postRegister(Boolean registrationDone) {
0N/A System.out.println("Broadcaster.postRegister: sending notif");
0N/A sendNotification(new Notification("x", this, 0L));
0N/A }
0N/A
0N/A public void preDeregister() {
0N/A }
0N/A
0N/A public void postDeregister() {
0N/A }
0N/A }
0N/A
0N/A private static class CountListener implements NotificationListener {
0N/A private int count;
0N/A
0N/A public synchronized void handleNotification(Notification n, Object h) {
0N/A if (!n.getType().equals("x")) {
0N/A System.out.println("TEST FAILED: received bogus notif: " + n +
0N/A " (type=" + n.getType() + ")");
0N/A System.exit(1);
0N/A }
0N/A count++;
0N/A }
0N/A
0N/A public synchronized int getCount() {
0N/A return count;
0N/A }
0N/A }
0N/A}