jdk/test/javax/management/proxy/NotificationEmitterProxy.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test that we can create proxies which are NotificationEmitters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 6411747
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Daniel Fuchs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean NotificationEmitterProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build NotificationEmitterProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main NotificationEmitterProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.naming.NoPermissionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class NotificationEmitterProxy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static class Counter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        public synchronized int count() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        public synchronized int peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        public synchronized int waitfor(int max, long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            final long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            while (count < max && timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                final long rest = timeout -
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                        (System.currentTimeMillis() - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                if (rest <= 0) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                wait(rest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static class CounterListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        final private Counter counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        public CounterListener(Counter counter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            this.counter = counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        public void handleNotification(Notification notification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                        Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
               System.out.println("Received notif from " + handback +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                  ":\n\t" + notification);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
               counter.count();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.out.println("<<< Register for notification from a proxy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        final ObjectName name = new ObjectName(":class=Simple");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        final JMXConnectorServer server =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        url = server.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        final JMXConnector client = JMXConnectorFactory.connect(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        final Counter counter = new Counter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        final CounterListener listener = new CounterListener(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        final Counter mxcounter = new Counter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        final CounterListener mxlistener = new CounterListener(mxcounter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        final NotificationFilterSupport filter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                new NotificationFilterSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        filter.enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        filter.enableType(MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int registered = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            final MBeanServerDelegateMBean delegate =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                JMX.newMBeanProxy(client.getMBeanServerConnection(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                       MBeanServerDelegate.DELEGATE_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                       MBeanServerDelegateMBean.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                       true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            NotificationEmitter emitter = (NotificationEmitter)delegate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            emitter.addNotificationListener(listener,filter,"JMX.newMBeanProxy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new RuntimeException("Failed to register listener with "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    " JMX.newMBeanProxy: " + x, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            final MBeanServerDelegateMBean delegate =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                MBeanServerInvocationHandler.newProxyInstance(mbs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                       MBeanServerDelegate.DELEGATE_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                       MBeanServerDelegateMBean.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                       true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            NotificationEmitter emitter = (NotificationEmitter)delegate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            emitter.addNotificationListener(listener,filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    "MBeanServerInvocationHandler.newProxyInstance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new RuntimeException("Failed to register listener with "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    " MBeanServerInvocationHandler.newProxyInstance: " + x, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        System.out.println("<<< Register an MBean.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        final Simple simple = new Simple();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        mbs.registerMBean(simple, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        registered++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        SimpleMXBean simple0 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
           JMX.newMXBeanProxy(client.getMBeanServerConnection(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                              name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                              SimpleMXBean.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                              true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        SimpleMXBean simple1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            JMX.newMXBeanProxy(mbs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                               name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                               SimpleMXBean.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                               false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        final int expected = 2*registered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        final int reg = counter.waitfor(expected,3000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (reg != expected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new RuntimeException("Bad notification count: " + reg +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    ", expected " +expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        System.out.println("Received expected "+reg+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                " notifs after registerMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        ((NotificationEmitter)simple0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            .addNotificationListener(mxlistener,null,name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        simple1.equals("Are you a Wombat?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        final int mxnotifs = mxcounter.waitfor(1,3000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (mxnotifs != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
             throw new RuntimeException("Bad MXBean notification count: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                     mxnotifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        System.out.println("Received expected "+mxnotifs+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                " notifs from MXBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        mbs.unregisterMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        final int unreg = counter.waitfor(expected+reg,3000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (unreg != (expected+reg))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new RuntimeException("Bad notification count: " + unreg +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    ", expected " +expected+reg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        System.out.println("Received expected "+(unreg-reg)+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                " notifs after unregisterMBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        System.out.println("Total notifs received: " + unreg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public static interface Simplest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public static interface SimpleMXBean extends Simplest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        public String equals(String x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private static class Simple extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            implements SimpleMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        public static final String NOTIF_TYPE = "simple.equals";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        private static long seq=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        private static synchronized long seq() { return ++seq; };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        public String equals(String x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            sendNotification(new Notification(NOTIF_TYPE,this,seq(),x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 }