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 4911721
0N/A * @summary test on add/remove NotificationListener
0N/A * @author Shanliang JIANG
0N/A * @run clean DiffHBTest
0N/A * @run build DiffHBTest
0N/A * @run main DiffHBTest
0N/A */
0N/A
0N/A
0N/Aimport javax.management.*;
0N/Aimport javax.management.remote.*;
0N/A
0N/A/**
0N/A * This test registeres an unique listener with two different handbacks,
0N/A * it expects to receive a same notification two times.
0N/A */
0N/Apublic class DiffHBTest {
0N/A private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
0N/A
0N/A private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A private static ObjectName delegateName;
0N/A private static ObjectName timerName;
0N/A
0N/A public static final String[] hbs = new String[] {"0", "1"};
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A System.out.println(">>> test on one listener with two different handbacks.");
0N/A
0N/A delegateName = new ObjectName("JMImplementation:type=MBeanServerDelegate");
0N/A timerName = new ObjectName("MBean:name=Timer");
0N/A
469N/A String errors = "";
469N/A
0N/A for (int i = 0; i < protocols.length; i++) {
469N/A final String s = test(protocols[i]);
469N/A if (s != null) {
469N/A if ("".equals(errors)) {
469N/A errors = "Failed to " + protocols[i] + ": "+s;
0N/A } else {
469N/A errors = "\tFailed to " + protocols[i] + ": "+s;
0N/A }
0N/A }
0N/A }
0N/A
469N/A if ("".equals(errors)) {
469N/A System.out.println(">>> Passed!");
0N/A } else {
469N/A System.out.println(">>> Failed!");
469N/A
469N/A throw new RuntimeException(errors);
0N/A }
0N/A }
0N/A
469N/A private static String test(String proto) throws Exception {
1790N/A System.out.println(">>> Test for protocol " + proto);
0N/A JMXServiceURL u = new JMXServiceURL(proto, null, 0);
0N/A JMXConnectorServer server;
0N/A JMXConnector client;
0N/A
0N/A try {
469N/A server =
1790N/A JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
0N/A server.start();
469N/A JMXServiceURL addr = server.getAddress();
469N/A client = JMXConnectorFactory.connect(addr, null);
469N/A } catch (Exception e) {
469N/A // not support
469N/A System.out.println(">>> not support: " + proto);
469N/A return null;
469N/A }
0N/A
469N/A MBeanServerConnection mserver = client.getMBeanServerConnection();
0N/A
469N/A System.out.print(">>>\t");
469N/A for (int i=0; i<5; i++) {
469N/A System.out.print(i + "\t");
469N/A final MyListener dummyListener = new MyListener();
469N/A mserver.addNotificationListener(
469N/A delegateName, dummyListener, null, hbs[0]);
469N/A mserver.addNotificationListener(
469N/A delegateName, dummyListener, null, hbs[1]);
0N/A
469N/A mserver.createMBean("javax.management.timer.Timer", timerName);
469N/A
469N/A long remainingTime = waitingTime;
469N/A final long startTime = System.currentTimeMillis();
0N/A
469N/A try {
469N/A synchronized(dummyListener) {
469N/A while (!dummyListener.done && remainingTime > 0) {
469N/A dummyListener.wait(remainingTime);
469N/A remainingTime = waitingTime -
0N/A (System.currentTimeMillis() - startTime);
0N/A }
0N/A
469N/A if (dummyListener.errorInfo != null) {
469N/A return dummyListener.errorInfo;
469N/A }
469N/A }
469N/A } finally {
469N/A //System.out.println("Unregister: "+i);
469N/A mserver.unregisterMBean(timerName);
469N/A mserver.removeNotificationListener(delegateName, dummyListener);
469N/A }
469N/A }
469N/A
469N/A System.out.println("");
469N/A client.close();
469N/A server.stop();
469N/A
469N/A return null;
469N/A }
469N/A
469N/A private static class MyListener implements NotificationListener {
469N/A public boolean done = false;
469N/A public String errorInfo = null;
0N/A
469N/A private int received = 0;
469N/A private MBeanServerNotification receivedNotif = null;
469N/A private Object receivedHB = null;
469N/A public void handleNotification(Notification n, Object o) {
469N/A if (!(n instanceof MBeanServerNotification)) {
469N/A failed("Received an unexpected notification: "+n);
469N/A return;
469N/A }
469N/A
469N/A if (!hbs[0].equals(o) && !hbs[1].equals(o)) {
469N/A failed("Unkown handback: "+o);
469N/A return;
469N/A }
469N/A
469N/A // what we need
469N/A final MBeanServerNotification msn = (MBeanServerNotification)n;
469N/A if (!(MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(
469N/A msn.getType())) ||
469N/A !msn.getMBeanName().equals(timerName)) {
469N/A return;
469N/A }
469N/A
469N/A synchronized(this) {
469N/A received++;
469N/A
469N/A if (received == 1) { // first time
469N/A receivedNotif = msn;
469N/A receivedHB = o;
469N/A
469N/A return;
0N/A }
0N/A
469N/A if (received > 2) {
469N/A failed("Expect to receive 2 notifs, but get "+received);
0N/A
469N/A return;
0N/A }
0N/A
469N/A // second time
469N/A if (receivedHB.equals(o)) {
469N/A failed("Got same handback twice: "+o);
469N/A } else if(!hbs[0].equals(o) && !hbs[1].equals(o)) {
469N/A failed("Unknown handback: "+o);
469N/A } else if (receivedNotif.getSequenceNumber() !=
469N/A msn.getSequenceNumber()) {
469N/A failed("expected to receive:\n"
469N/A +receivedNotif
469N/A +"\n but got\n"+msn);
469N/A }
0N/A
469N/A // passed
469N/A done = true;
469N/A this.notify();
0N/A }
0N/A }
0N/A
469N/A private void failed(String errorInfo) {
469N/A this.errorInfo = errorInfo;
469N/A done = true;
469N/A
469N/A this.notify();
469N/A }
0N/A }
0N/A
469N/A private final static long waitingTime = 2000;
0N/A}