0N/A/*
2362N/A * Copyright (c) 2003, 2008, 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 7654321
0N/A * @summary Tests whether a listener receives notifs emitted before the
0N/A * listener is registered.
0N/A * @author Shanliang JIANG
0N/A * @run clean UnexpectedNotifTest
0N/A * @run build UnexpectedNotifTest
0N/A * @run main UnexpectedNotifTest
0N/A */
0N/A
469N/Aimport java.util.ArrayList;
469N/Aimport java.util.Collections;
469N/Aimport java.util.List;
469N/Aimport java.util.Map;
469N/Aimport javax.management.MBeanNotificationInfo;
469N/Aimport javax.management.MBeanServer;
469N/Aimport javax.management.MBeanServerConnection;
469N/Aimport javax.management.MBeanServerFactory;
469N/Aimport javax.management.Notification;
469N/Aimport javax.management.NotificationBroadcasterSupport;
469N/Aimport javax.management.NotificationListener;
469N/Aimport javax.management.ObjectName;
469N/Aimport javax.management.remote.JMXConnector;
469N/Aimport javax.management.remote.JMXConnectorFactory;
469N/Aimport javax.management.remote.JMXConnectorServer;
469N/Aimport javax.management.remote.JMXConnectorServerFactory;
469N/Aimport javax.management.remote.JMXServiceURL;
0N/A//
469N/Aimport javax.management.remote.rmi.RMIConnectorServer;
0N/A
0N/Apublic class UnexpectedNotifTest {
0N/A
0N/A public static void main(String[] args) throws Exception {
469N/A List<String> protos = new ArrayList<String>();
469N/A protos.add("rmi");
0N/A try {
0N/A Class.forName("javax.management.remote.jmxmp.JMXMPConnectorServer");
469N/A protos.add("jmxmp");
0N/A } catch (ClassNotFoundException e) {
469N/A // OK: JMXMP not present so don't test it.
469N/A }
1790N/A for (String proto : protos)
1790N/A test(proto);
469N/A }
0N/A
1790N/A private static void test(String proto) throws Exception {
469N/A System.out.println("Unexpected notifications test for protocol " +
1790N/A proto);
469N/A MBeanServer mbs = null;
469N/A try {
469N/A // Create a MBeanServer
469N/A //
469N/A mbs = MBeanServerFactory.createMBeanServer();
469N/A
469N/A // Create a NotificationEmitter MBean
469N/A //
469N/A mbean = new ObjectName ("Default:name=NotificationEmitter");
469N/A mbs.registerMBean(new NotificationEmitter(), mbean);
0N/A
469N/A // Create a connector server
469N/A //
469N/A url = new JMXServiceURL("service:jmx:" + proto + "://");
0N/A
469N/A server = JMXConnectorServerFactory.newJMXConnectorServer(url,
1790N/A null,
469N/A mbs);
0N/A
469N/A mbs.registerMBean(
469N/A server,
469N/A new ObjectName("Default:name=ConnectorServer"));
0N/A
469N/A server.start();
469N/A
469N/A url = server.getAddress();
0N/A
469N/A for (int j = 0; j < 2; j++) {
469N/A test();
0N/A }
469N/A } finally {
469N/A // Stop server
469N/A //
469N/A server.stop();
469N/A // Release the MBeanServer
469N/A //
469N/A MBeanServerFactory.releaseMBeanServer(mbs);
0N/A }
0N/A }
0N/A
0N/A private static void test() throws Exception {
0N/A // Create client
0N/A //
0N/A JMXConnector connector = JMXConnectorFactory.connect(url);
0N/A MBeanServerConnection client = connector.getMBeanServerConnection();
0N/A
0N/A // Add listener at the client side
0N/A //
0N/A client.addNotificationListener(mbean, listener, null, null);
0N/A
0N/A // Cleanup
0N/A //
0N/A receivedNotifs = 0;
0N/A
0N/A // Ask to send notifs
0N/A //
0N/A Object[] params = new Object[] {new Integer(nb)};
0N/A String[] signatures = new String[] {"java.lang.Integer"};
0N/A
0N/A client.invoke(mbean, "sendNotifications", params, signatures);
0N/A
0N/A // Waiting...
0N/A //
0N/A synchronized (lock) {
0N/A for (int i = 0; i < 10; i++) {
0N/A if (receivedNotifs < nb) {
0N/A lock.wait(1000);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Waiting again to ensure no more notifs
0N/A //
0N/A Thread.sleep(3000);
0N/A
0N/A synchronized (lock) {
0N/A if (receivedNotifs != nb) {
0N/A throw new Exception("The client expected to receive " +
0N/A nb + " notifs, but got " + receivedNotifs);
0N/A }
0N/A }
0N/A
0N/A // Remove listener
0N/A //
0N/A client.removeNotificationListener(mbean, listener);
0N/A
0N/A connector.close();
0N/A }
0N/A
0N/A //--------------------------
0N/A // private classes
0N/A //--------------------------
0N/A
0N/A private static class Listener implements NotificationListener {
0N/A public void handleNotification(Notification notif, Object handback) {
0N/A System.out.println("Received: " + notif + " (" +
0N/A notif.getSequenceNumber() + ")");
0N/A synchronized(lock) {
0N/A if(++receivedNotifs == nb) {
0N/A lock.notifyAll();
0N/A } else if (receivedNotifs > nb) {
0N/A System.out.println("The client expected to receive " +
0N/A nb + " notifs, but got at least " +
0N/A receivedNotifs);
0N/A System.exit(1);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public static class NotificationEmitter
0N/A extends NotificationBroadcasterSupport
0N/A implements NotificationEmitterMBean {
0N/A
0N/A /**
0N/A * Returns a NotificationInfo object containing the name of the Java
0N/A * class of the notification and the notification types sent by this
0N/A * notification broadcaster.
0N/A */
0N/A public MBeanNotificationInfo[] getNotificationInfo() {
0N/A
0N/A MBeanNotificationInfo[] ntfInfoArray = new MBeanNotificationInfo[1];
0N/A
0N/A String[] ntfTypes = new String[1];
0N/A ntfTypes[0] = myType;
0N/A
0N/A ntfInfoArray[0] = new MBeanNotificationInfo(
0N/A ntfTypes,
0N/A "javax.management.Notification",
0N/A "Notifications sent by the NotificationEmitter");
0N/A return ntfInfoArray;
0N/A }
0N/A
0N/A /**
0N/A * Send a Notification object with the specified times.
0N/A * The sequence number will be from zero to times-1.
0N/A *
0N/A * @param nb The number of notifications to send
0N/A */
0N/A public void sendNotifications(Integer nb) {
0N/A System.out.println("NotificationEmitter: asked to send " +
0N/A "notifications: " + nb);
0N/A
0N/A Notification notif;
0N/A for (int i = 1; i <= nb.intValue(); i++) {
0N/A notif = new Notification(myType, this, ++seqno);
0N/A sendNotification(notif);
0N/A }
0N/A }
0N/A
0N/A private String myType = "notification.my_notification";
0N/A }
0N/A
0N/A public interface NotificationEmitterMBean {
0N/A public void sendNotifications(Integer nb);
0N/A }
0N/A
0N/A private static JMXConnectorServer server;
0N/A private static JMXServiceURL url;
0N/A private static ObjectName mbean;
0N/A private static NotificationListener listener = new Listener();
0N/A
0N/A private static int nb = 10;
0N/A private static int receivedNotifs = 0;
0N/A private static int[] lock = new int[0];
0N/A private static volatile long seqno;
0N/A}