0N/A/*
2362N/A * Copyright (c) 2005, 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 6238731
0N/A * @summary Check that the expected notification is received by the JMX
0N/A * client even when the domain in the ObjectName is not specified
0N/A * @author Shanliang JIANG
0N/A * @run clean EmptyDomainNotificationTest
0N/A * @run build EmptyDomainNotificationTest
1790N/A * @run main EmptyDomainNotificationTest
0N/A */
0N/A
1790N/Aimport java.util.ArrayList;
1790N/Aimport java.util.List;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerConnection;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.Notification;
0N/Aimport javax.management.NotificationBroadcasterSupport;
0N/Aimport javax.management.NotificationListener;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.remote.JMXConnector;
0N/Aimport javax.management.remote.JMXConnectorFactory;
0N/Aimport javax.management.remote.JMXConnectorServer;
0N/Aimport javax.management.remote.JMXConnectorServerFactory;
0N/Aimport javax.management.remote.JMXServiceURL;
0N/A
0N/Apublic class EmptyDomainNotificationTest {
0N/A
0N/A public static interface SimpleMBean {
0N/A public void emitNotification();
0N/A }
0N/A
0N/A public static class Simple
0N/A extends NotificationBroadcasterSupport
0N/A implements SimpleMBean {
0N/A public void emitNotification() {
0N/A sendNotification(new Notification("simple", this, 0));
0N/A }
0N/A }
0N/A
0N/A public static class Listener implements NotificationListener {
0N/A public void handleNotification(Notification n, Object h) {
0N/A System.out.println(
0N/A "EmptyDomainNotificationTest-Listener-handleNotification: receives:" + n);
0N/A
0N/A if (n.getType().equals("simple")) {
0N/A synchronized(this) {
0N/A received++;
0N/A
0N/A this.notifyAll();
0N/A }
0N/A }
0N/A }
0N/A
0N/A public int received;
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A
0N/A final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
0N/A
1790N/A JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
0N/A server.start();
0N/A
0N/A JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);
0N/A
0N/A final MBeanServerConnection mbsc = client.getMBeanServerConnection();
0N/A
0N/A final ObjectName mbean = ObjectName.getInstance(":type=Simple");
0N/A mbsc.createMBean(Simple.class.getName(), mbean);
0N/A
0N/A System.out.println("EmptyDomainNotificationTest-main: add a listener ...");
0N/A final Listener li = new Listener();
0N/A mbsc.addNotificationListener(mbean, li, null, null);
0N/A
0N/A System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");
0N/A mbsc.invoke(mbean, "emitNotification", null, null);
0N/A
0N/A System.out.println("EmptyDomainNotificationTest-main: waiting notif...");
0N/A final long stopTime = System.currentTimeMillis() + 2000;
0N/A synchronized(li) {
0N/A long toWait = stopTime - System.currentTimeMillis();
0N/A
0N/A while (li.received < 1 && toWait > 0) {
0N/A li.wait(toWait);
0N/A
0N/A toWait = stopTime - System.currentTimeMillis();
0N/A }
0N/A }
0N/A
0N/A if (li.received < 1) {
0N/A throw new RuntimeException("No notif received!");
0N/A } else if (li.received > 1) {
0N/A throw new RuntimeException("Wait one notif but got: "+li.received);
0N/A }
0N/A
0N/A System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");
0N/A
0N/A System.out.println("EmptyDomainNotificationTest-main: remove the listener.");
0N/A mbsc.removeNotificationListener(mbean, li);
0N/A
0N/A // clean
0N/A client.close();
0N/A server.stop();
0N/A
0N/A System.out.println("EmptyDomainNotificationTest-main: Bye.");
0N/A }
0N/A}