jdk/test/javax/management/MBeanServer/OldMBeanServerTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 1247 b4c26443dee5
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     2
 * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     4
 *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     8
 *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    13
 * accompanied this code).
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    14
 *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    21
 * questions.
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    22
 */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    23
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    24
import java.io.IOException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    25
import java.io.ObjectInputStream;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    26
import java.io.Serializable;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    27
import java.lang.annotation.Retention;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    28
import java.lang.annotation.RetentionPolicy;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    29
import java.lang.management.ManagementFactory;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    30
import java.lang.ref.WeakReference;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    31
import java.lang.reflect.AccessibleObject;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    32
import java.lang.reflect.Constructor;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    33
import java.lang.reflect.InvocationTargetException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    34
import java.lang.reflect.Method;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    35
import java.lang.reflect.Modifier;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    36
import java.util.ArrayList;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    37
import java.util.Arrays;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    38
import java.util.HashMap;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    39
import java.util.HashSet;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    40
import java.util.Iterator;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    41
import java.util.List;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    42
import java.util.Map;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    43
import java.util.Set;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    44
import java.util.WeakHashMap;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    45
import java.util.concurrent.Callable;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    46
import java.util.concurrent.ConcurrentHashMap;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    47
import java.util.concurrent.ConcurrentMap;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    48
import javax.management.Attribute;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    49
import javax.management.AttributeList;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    50
import javax.management.AttributeNotFoundException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    51
import javax.management.DynamicMBean;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    52
import javax.management.InstanceAlreadyExistsException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    53
import javax.management.InstanceNotFoundException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    54
import javax.management.IntrospectionException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    55
import javax.management.InvalidAttributeValueException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    56
import javax.management.ListenerNotFoundException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    57
import javax.management.MBeanAttributeInfo;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    58
import javax.management.MBeanConstructorInfo;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    59
import javax.management.MBeanException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    60
import javax.management.MBeanInfo;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    61
import javax.management.MBeanNotificationInfo;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    62
import javax.management.MBeanOperationInfo;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    63
import javax.management.MBeanRegistration;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    64
import javax.management.MBeanRegistrationException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    65
import javax.management.MBeanServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    66
import javax.management.MBeanServerBuilder;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    67
import javax.management.MBeanServerConnection;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    68
import javax.management.MBeanServerDelegate;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    69
import javax.management.MBeanServerFactory;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    70
import javax.management.MBeanServerNotification;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    71
import javax.management.MalformedObjectNameException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    72
import javax.management.NotCompliantMBeanException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    73
import javax.management.Notification;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    74
import javax.management.NotificationBroadcaster;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    75
import javax.management.NotificationBroadcasterSupport;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    76
import javax.management.NotificationEmitter;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    77
import javax.management.NotificationFilter;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    78
import javax.management.NotificationListener;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    79
import javax.management.ObjectInstance;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    80
import javax.management.ObjectName;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    81
import javax.management.OperationsException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    82
import javax.management.QueryEval;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    83
import javax.management.QueryExp;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    84
import javax.management.ReflectionException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    85
import javax.management.RuntimeErrorException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    86
import javax.management.RuntimeMBeanException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    87
import javax.management.StandardMBean;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    88
import javax.management.loading.ClassLoaderRepository;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    89
import javax.management.remote.JMXConnector;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    90
import javax.management.remote.JMXConnectorFactory;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    91
import javax.management.remote.JMXConnectorServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    92
import javax.management.remote.JMXConnectorServerFactory;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    93
import javax.management.remote.JMXServiceURL;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    94
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    95
/*
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    96
 * @test OldMBeanServerTest.java
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    97
 * @bug 5072268
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    98
 * @summary Test that nothing assumes a post-1.2 MBeanServer
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
    99
 * @author Eamonn McManus
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   100
 * @run main/othervm -ea OldMBeanServerTest
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   101
 */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   102
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   103
/*
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   104
 * We defined the MBeanServerBuilder class and the associated system
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   105
 * property javax.management.builder.initial in version 1.2 of the JMX
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   106
 * spec.  That amounts to a guarantee that someone can set the property
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   107
 * to an MBeanServer that only knows about JMX 1.2 semantics, and if they
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   108
 * only do JMX 1.2 operations, everything should work.  This test is a
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   109
 * sanity check that ensures we don't inadvertently make any API changes
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   110
 * that stop that from being true.  It includes a complete (if slow)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   111
 * MBeanServer implementation.  That implementation doesn't replicate the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   112
 * mandated exception behaviour everywhere, though, since there's lots of
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   113
 * arbitrary cruft in that.  Also, the behaviour of concurrent unregisterMBean
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   114
 * calls is incorrect in detail.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   115
 */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   116
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   117
public class OldMBeanServerTest {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   118
    private static MBeanServerConnection mbsc;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   119
    private static String failure;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   120
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   121
    public static void main(String[] args) throws Exception {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   122
        if (!OldMBeanServerTest.class.desiredAssertionStatus())
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   123
            throw new Exception("Test must be run with -ea");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   124
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   125
        System.setProperty("javax.management.builder.initial",
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   126
                OldMBeanServerBuilder.class.getName());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   127
        assert MBeanServerFactory.newMBeanServer() instanceof OldMBeanServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   128
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   129
        System.out.println("=== RUNNING TESTS WITH LOCAL MBEANSERVER ===");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   130
        runTests(new Callable<MBeanServerConnection>() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   131
            public MBeanServerConnection call() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   132
                return MBeanServerFactory.newMBeanServer();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   133
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   134
        }, null);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   135
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   136
        System.out.println("=== RUNNING TESTS THROUGH CONNECTOR ===");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   137
        ConnectionBuilder builder = new ConnectionBuilder();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   138
        runTests(builder, builder);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   139
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   140
        if (failure == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   141
            System.out.println("TEST PASSED");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   142
        else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   143
            throw new Exception("TEST FAILED: " + failure);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   144
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   145
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   146
    private static class ConnectionBuilder
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   147
            implements Callable<MBeanServerConnection>, Runnable {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   148
        private JMXConnector connector;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   149
        public MBeanServerConnection call() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   150
            MBeanServer mbs = MBeanServerFactory.newMBeanServer();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   151
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   152
                JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   153
                JMXConnectorServer cs =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   154
                    JMXConnectorServerFactory.newJMXConnectorServer(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   155
                        url, null, mbs);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   156
                cs.start();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   157
                JMXServiceURL addr = cs.getAddress();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   158
                connector = JMXConnectorFactory.connect(addr);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   159
                return connector.getMBeanServerConnection();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   160
            } catch (IOException e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   161
                throw new RuntimeException(e);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   162
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   163
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   164
        public void run() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   165
            if (connector != null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   166
                try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   167
                    connector.close();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   168
                } catch (IOException e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   169
                    throw new RuntimeException(e);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   170
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   171
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   172
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   173
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   174
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   175
    private static void runTests(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   176
            Callable<MBeanServerConnection> maker, Runnable breaker)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   177
    throws Exception {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   178
        for (Method m : OldMBeanServerTest.class.getDeclaredMethods()) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   179
            if (Modifier.isStatic(m.getModifiers()) &&
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   180
                    m.getName().startsWith("test") &&
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   181
                    m.getParameterTypes().length == 0) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   182
                ExpectException expexc = m.getAnnotation(ExpectException.class);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   183
                mbsc = maker.call();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   184
                try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   185
                    m.invoke(null);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   186
                    if (expexc != null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   187
                        failure =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   188
                                m.getName() + " did not got expected exception " +
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   189
                                expexc.value().getName();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   190
                        System.out.println(failure);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   191
                    } else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   192
                        System.out.println(m.getName() + " OK");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   193
                } catch (InvocationTargetException ite) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   194
                    Throwable t = ite.getCause();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   195
                    String prob = null;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   196
                    if (expexc != null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   197
                        if (expexc.value().isInstance(t)) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   198
                            System.out.println(m.getName() + " OK (got expected " +
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   199
                                    expexc.value().getName() + ")");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   200
                        } else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   201
                            prob = "got wrong exception";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   202
                    } else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   203
                        prob = "got exception";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   204
                    if (prob != null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   205
                        failure = m.getName() + ": " + prob + " " +
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   206
                                t.getClass().getName();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   207
                        System.out.println(failure);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   208
                        t.printStackTrace(System.out);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   209
                    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   210
                } finally {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   211
                    if (breaker != null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   212
                        breaker.run();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   213
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   214
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   215
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   216
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   217
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   218
    @Retention(RetentionPolicy.RUNTIME)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   219
    private static @interface ExpectException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   220
        Class<? extends Exception> value();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   221
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   222
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   223
    public static interface BoringMBean {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   224
        public String getName();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   225
        public int add(int x, int y);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   226
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   227
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   228
    // This class is Serializable so we can createMBean a StandardMBean
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   229
    // that contains it.  Not recommended practice in general --
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   230
    // should we have a StandardMBean constructor that takes a class
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   231
    // name and constructor parameters?
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   232
    public static class Boring implements BoringMBean, Serializable {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   233
        public String getName() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   234
            return "Jessica";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   235
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   236
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   237
        public int add(int x, int y) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   238
            return x + y;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   239
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   240
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   241
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   242
    public static interface BoringNotifierMBean extends BoringMBean {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   243
        public void send();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   244
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   245
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   246
    public static class BoringNotifier
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   247
            extends Boring implements BoringNotifierMBean, NotificationBroadcaster {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   248
        private final NotificationBroadcasterSupport nbs =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   249
                new NotificationBroadcasterSupport();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   250
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   251
        public void addNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   252
                NotificationListener listener, NotificationFilter filter, Object handback)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   253
        throws IllegalArgumentException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   254
            nbs.addNotificationListener(listener, filter, handback);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   255
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   256
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   257
        public void removeNotificationListener(NotificationListener listener)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   258
        throws ListenerNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   259
            nbs.removeNotificationListener(listener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   260
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   261
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   262
        public MBeanNotificationInfo[] getNotificationInfo() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   263
            return null;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   264
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   265
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   266
        public void send() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   267
            Notification n = new Notification("type.type", this, 0L);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   268
            nbs.sendNotification(n);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   269
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   270
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   271
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   272
    private static class CountListener implements NotificationListener {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   273
        volatile int count;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   274
        public void handleNotification(Notification n, Object h) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   275
            if (h == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   276
                h = 1;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   277
            count += (Integer) h;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   278
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   279
        void waitForCount(int expect) throws InterruptedException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   280
            long deadline = System.currentTimeMillis() + 2000L;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   281
            while (count < expect && System.currentTimeMillis() < deadline)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   282
                Thread.sleep(1);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   283
            assert count == expect;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   284
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   285
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   286
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   287
    private static void testBasic() throws Exception {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   288
        CountListener countListener = new CountListener();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   289
        mbsc.addNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   290
                MBeanServerDelegate.DELEGATE_NAME, countListener, null, null);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   291
        assert countListener.count == 0;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   292
        ObjectName name = new ObjectName("a:b=c");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   293
        if (mbsc instanceof MBeanServer)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   294
            ((MBeanServer) mbsc).registerMBean(new Boring(), name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   295
        else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   296
            mbsc.createMBean(Boring.class.getName(), name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   297
        countListener.waitForCount(1);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   298
        assert mbsc.isRegistered(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   299
        assert mbsc.queryNames(null, null).contains(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   300
        assert mbsc.getAttribute(name, "Name").equals("Jessica");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   301
        assert mbsc.invoke(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   302
                name, "add", new Object[] {2, 3}, new String[] {"int", "int"})
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   303
                .equals(5);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   304
        mbsc.unregisterMBean(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   305
        countListener.waitForCount(2);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   306
        assert !mbsc.isRegistered(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   307
        assert !mbsc.queryNames(null, null).contains(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   308
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   309
        mbsc.createMBean(BoringNotifier.class.getName(), name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   310
        countListener.waitForCount(3);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   311
        CountListener boringListener = new CountListener();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   312
        class AlwaysNotificationFilter implements NotificationFilter {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   313
            public boolean isNotificationEnabled(Notification notification) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   314
                return true;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   315
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   316
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   317
        mbsc.addNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   318
                name, boringListener, new AlwaysNotificationFilter(), 5);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   319
        mbsc.invoke(name, "send", null, null);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   320
        boringListener.waitForCount(5);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   321
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   322
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   323
    private static void testPrintAttrs() throws Exception {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   324
        printAttrs(mbsc, null);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   325
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   326
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   327
    private static void testPlatformMBeanServer() throws Exception {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   328
        MBeanServer pmbs = ManagementFactory.getPlatformMBeanServer();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   329
        assert pmbs instanceof OldMBeanServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   330
        // Preceding assertion could be violated if at some stage we wrap
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   331
        // the Platform MBeanServer.  In that case we can still check that
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   332
        // it is ultimately an OldMBeanServer for example by adding a
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   333
        // counter to getAttribute and checking that it is incremented
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   334
        // when we call pmbs.getAttribute.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   335
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   336
        printAttrs(pmbs, UnsupportedOperationException.class);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   337
        ObjectName memoryMXBeanName =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   338
                new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   339
        pmbs.invoke(memoryMXBeanName, "gc", null, null);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   340
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   341
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   342
    private static void printAttrs(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   343
            MBeanServerConnection mbsc1, Class<? extends Exception> expectX)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   344
    throws Exception {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   345
        Set<ObjectName> names = mbsc1.queryNames(null, null);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   346
        for (ObjectName name : names) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   347
            System.out.println(name + ":");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   348
            MBeanInfo mbi = mbsc1.getMBeanInfo(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   349
            MBeanAttributeInfo[] mbais = mbi.getAttributes();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   350
            for (MBeanAttributeInfo mbai : mbais) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   351
                String attr = mbai.getName();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   352
                Object value;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   353
                try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   354
                    value = mbsc1.getAttribute(name, attr);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   355
                } catch (Exception e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   356
                    if (expectX != null && expectX.isInstance(e))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   357
                        value = "<" + e + ">";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   358
                    else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   359
                        throw e;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   360
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   361
                String s = "  " + attr + " = " + value;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   362
                if (s.length() > 80)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   363
                    s = s.substring(0, 77) + "...";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   364
                System.out.println(s);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   365
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   366
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   367
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   368
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   369
    private static void testJavaxManagementStandardMBean() throws Exception {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   370
        ObjectName name = new ObjectName("a:b=c");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   371
        Object mbean = new StandardMBean(new Boring(), BoringMBean.class);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   372
        mbsc.createMBean(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   373
                StandardMBean.class.getName(), name,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   374
                new Object[] {new Boring(), BoringMBean.class},
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   375
                new String[] {Object.class.getName(), Class.class.getName()});
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   376
        assert mbsc.getAttribute(name, "Name").equals("Jessica");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   377
        assert mbsc.invoke(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   378
                name, "add", new Object[] {2, 3}, new String[] {"int", "int"})
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   379
                .equals(5);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   380
        mbsc.unregisterMBean(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   381
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   382
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   383
    private static void testConnector() throws Exception {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   384
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   385
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   386
    public static class OldMBeanServerBuilder extends MBeanServerBuilder {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   387
        public MBeanServer newMBeanServer(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   388
                String defaultDomain, MBeanServer outer, MBeanServerDelegate delegate) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   389
            return new OldMBeanServer(defaultDomain, delegate);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   390
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   391
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   392
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   393
    public static class OldMBeanServer implements MBeanServer {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   394
        // We pretend there's a ClassLoader MBean representing the Class Loader
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   395
        // Repository and intercept references to it where necessary to keep up
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   396
        // the pretence.  This allows us to fake the right behaviour for
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   397
        // the omitted-ClassLoader versions of createMBean and instantiate
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   398
        // (which are not the same as passing a null for the ClassLoader parameter
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   399
        // of the versions that have one).
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   400
        private static final ObjectName clrName;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   401
        static {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   402
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   403
                clrName =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   404
                        new ObjectName("JMImplementation:type=ClassLoaderRepository");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   405
            } catch (MalformedObjectNameException e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   406
                throw new RuntimeException(e);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   407
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   408
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   409
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   410
        private final ConcurrentMap<ObjectName, DynamicMBean> mbeans =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   411
                new ConcurrentHashMap<ObjectName, DynamicMBean>();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   412
        private final ConcurrentMap<ObjectName, ListenerTable> listenerMap =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   413
                new ConcurrentHashMap<ObjectName, ListenerTable>();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   414
        private final String defaultDomain;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   415
        private final MBeanServerDelegate delegate;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   416
        private final ClassLoaderRepositoryImpl clr =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   417
                new ClassLoaderRepositoryImpl();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   418
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   419
        OldMBeanServer(String defaultDomain, MBeanServerDelegate delegate) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   420
            this.defaultDomain = defaultDomain;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   421
            this.delegate = delegate;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   422
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   423
                registerMBean(delegate, MBeanServerDelegate.DELEGATE_NAME);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   424
            } catch (Exception e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   425
                throw new RuntimeException(e);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   426
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   427
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   428
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   429
        public ObjectInstance createMBean(String className, ObjectName name)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   430
        throws ReflectionException, InstanceAlreadyExistsException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   431
                MBeanRegistrationException, MBeanException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   432
                NotCompliantMBeanException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   433
            return createMBean(className, name, null, null);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   434
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   435
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   436
        public ObjectInstance createMBean(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   437
                String className, ObjectName name, ObjectName loaderName)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   438
        throws ReflectionException, InstanceAlreadyExistsException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   439
                MBeanRegistrationException, MBeanException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   440
                NotCompliantMBeanException, InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   441
            return createMBean(className, name, loaderName, null, null);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   442
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   443
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   444
        public ObjectInstance createMBean(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   445
                String className, ObjectName name, Object[] params, String[] signature)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   446
        throws ReflectionException, InstanceAlreadyExistsException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   447
                MBeanRegistrationException, MBeanException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   448
                NotCompliantMBeanException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   449
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   450
                return createMBean(className, name, clrName, params, signature);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   451
            } catch (InstanceNotFoundException ex) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   452
                throw new RuntimeException(ex);  // can't happen
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   453
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   454
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   455
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   456
        public ObjectInstance createMBean(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   457
                String className, ObjectName name, ObjectName loaderName,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   458
                Object[] params, String[] signature)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   459
        throws ReflectionException, InstanceAlreadyExistsException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   460
                MBeanRegistrationException, MBeanException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   461
                NotCompliantMBeanException, InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   462
            Object mbean = instantiate(className, loaderName, params, signature);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   463
            return registerMBean(mbean, name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   464
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   465
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   466
        private void forbidJMImpl(ObjectName name) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   467
            if (name.getDomain().equals("JMImplementation") &&
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   468
                    mbeans.containsKey(MBeanServerDelegate.DELEGATE_NAME))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   469
                throw new IllegalArgumentException("JMImplementation reserved");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   470
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   471
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   472
        public ObjectInstance registerMBean(Object object, ObjectName name)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   473
        throws InstanceAlreadyExistsException, MBeanRegistrationException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   474
                NotCompliantMBeanException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   475
            forbidJMImpl(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   476
            if (name.isPattern())
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   477
                throw new IllegalArgumentException(name.toString());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   478
            // This is the only place we check for wildcards.  Since you
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   479
            // can't register a wildcard name, other operations that supply
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   480
            // one will get InstanceNotFoundException when they look it up.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   481
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   482
            DynamicMBean mbean;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   483
            if (object instanceof DynamicMBean)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   484
                mbean = (DynamicMBean) object;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   485
            else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   486
                mbean = standardToDynamic(object);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   487
            MBeanRegistration reg = mbeanRegistration(object);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   488
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   489
                name = reg.preRegister(this, name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   490
            } catch (Exception e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   491
                throw new MBeanRegistrationException(e);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   492
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   493
            DynamicMBean put = mbeans.putIfAbsent(name, mbean);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   494
            if (put != null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   495
                reg.postRegister(false);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   496
                throw new InstanceAlreadyExistsException(name.toString());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   497
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   498
            reg.postRegister(true);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   499
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   500
            if (object instanceof ClassLoader)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   501
                clr.addLoader((ClassLoader) object);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   502
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   503
            Notification n = new MBeanServerNotification(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   504
                    MBeanServerNotification.REGISTRATION_NOTIFICATION,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   505
                    MBeanServerDelegate.DELEGATE_NAME,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   506
                    0,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   507
                    name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   508
            delegate.sendNotification(n);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   509
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   510
            String className = mbean.getMBeanInfo().getClassName();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   511
            return new ObjectInstance(name, className);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   512
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   513
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   514
        public void unregisterMBean(ObjectName name)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   515
        throws InstanceNotFoundException, MBeanRegistrationException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   516
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   517
            forbidJMImpl(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   518
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   519
            DynamicMBean mbean = getMBean(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   520
            if (mbean == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   521
                throw new InstanceNotFoundException(name.toString());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   522
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   523
            MBeanRegistration reg = mbeanRegistration(mbean);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   524
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   525
                reg.preDeregister();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   526
            } catch (Exception e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   527
                throw new MBeanRegistrationException(e);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   528
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   529
            if (!mbeans.remove(name, mbean))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   530
                throw new InstanceNotFoundException(name.toString());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   531
                // This is incorrect because we've invoked preDeregister
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   532
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   533
            Object userMBean = getUserMBean(mbean);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   534
            if (userMBean instanceof ClassLoader)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   535
                clr.removeLoader((ClassLoader) userMBean);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   536
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   537
            Notification n = new MBeanServerNotification(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   538
                    MBeanServerNotification.REGISTRATION_NOTIFICATION,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   539
                    MBeanServerDelegate.DELEGATE_NAME,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   540
                    0,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   541
                    name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   542
            delegate.sendNotification(n);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   543
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   544
            reg.postDeregister();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   545
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   546
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   547
        public ObjectInstance getObjectInstance(ObjectName name)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   548
        throws InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   549
            DynamicMBean mbean = getMBean(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   550
            return new ObjectInstance(name, mbean.getMBeanInfo().getClassName());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   551
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   552
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   553
        private static class TrueQueryExp implements QueryExp {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   554
            public boolean apply(ObjectName name) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   555
                return true;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   556
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   557
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   558
            public void setMBeanServer(MBeanServer s) {}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   559
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   560
        private static final QueryExp trueQuery = new TrueQueryExp();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   561
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   562
        public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   563
            Set<ObjectInstance> instances = newSet();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   564
            if (name == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   565
                name = ObjectName.WILDCARD;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   566
            if (query == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   567
                query = trueQuery;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   568
            MBeanServer oldMBS = QueryEval.getMBeanServer();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   569
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   570
                query.setMBeanServer(this);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   571
                for (ObjectName n : mbeans.keySet()) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   572
                    if (name.apply(n)) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   573
                        try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   574
                            if (query.apply(n))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   575
                                instances.add(getObjectInstance(n));
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   576
                        } catch (Exception e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   577
                            // OK: Ignore this MBean in the result
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   578
                        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   579
                    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   580
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   581
            } finally {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   582
                query.setMBeanServer(oldMBS);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   583
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   584
            return instances;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   585
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   586
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   587
        public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   588
            Set<ObjectInstance> instances = queryMBeans(name, query);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   589
            Set<ObjectName> names = newSet();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   590
            for (ObjectInstance instance : instances)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   591
                names.add(instance.getObjectName());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   592
            return names;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   593
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   594
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   595
        public boolean isRegistered(ObjectName name) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   596
            return mbeans.containsKey(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   597
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   598
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   599
        public Integer getMBeanCount() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   600
            return mbeans.size();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   601
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   602
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   603
        public Object getAttribute(ObjectName name, String attribute)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   604
        throws MBeanException, AttributeNotFoundException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   605
                InstanceNotFoundException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   606
            return getMBean(name).getAttribute(attribute);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   607
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   608
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   609
        public AttributeList getAttributes(ObjectName name, String[] attributes)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   610
        throws InstanceNotFoundException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   611
            return getMBean(name).getAttributes(attributes);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   612
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   613
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   614
        public void setAttribute(ObjectName name, Attribute attribute)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   615
        throws InstanceNotFoundException, AttributeNotFoundException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   616
                InvalidAttributeValueException, MBeanException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   617
                ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   618
            getMBean(name).setAttribute(attribute);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   619
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   620
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   621
        public AttributeList setAttributes(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   622
                ObjectName name, AttributeList attributes)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   623
        throws InstanceNotFoundException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   624
            return getMBean(name).setAttributes(attributes);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   625
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   626
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   627
        public Object invoke(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   628
                ObjectName name, String operationName, Object[] params,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   629
                String[] signature)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   630
        throws InstanceNotFoundException, MBeanException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   631
            return getMBean(name).invoke(operationName, params, signature);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   632
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   633
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   634
        public String getDefaultDomain() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   635
            return defaultDomain;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   636
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   637
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   638
        public String[] getDomains() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   639
            Set<String> domains = newSet();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   640
            for (ObjectName name : mbeans.keySet())
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   641
                domains.add(name.getDomain());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   642
            return domains.toArray(new String[0]);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   643
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   644
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   645
        // ClassCastException if MBean is not a NotificationBroadcaster
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   646
        public void addNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   647
                ObjectName name, NotificationListener listener,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   648
                NotificationFilter filter, Object handback)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   649
                throws InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   650
            NotificationBroadcaster userMBean =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   651
                    (NotificationBroadcaster) getUserMBean(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   652
            NotificationListener wrappedListener =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   653
                  wrappedListener(name, userMBean, listener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   654
            userMBean.addNotificationListener(wrappedListener, filter, handback);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   655
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   656
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   657
        public void addNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   658
                ObjectName name, ObjectName listener,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   659
                NotificationFilter filter, Object handback)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   660
                throws InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   661
            NotificationListener nl =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   662
                    (NotificationListener) getUserMBean(listener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   663
            addNotificationListener(name, nl, filter, handback);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   664
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   665
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   666
        public void removeNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   667
                ObjectName name, ObjectName listener)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   668
                throws InstanceNotFoundException, ListenerNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   669
            NotificationListener nl =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   670
                    (NotificationListener) getUserMBean(listener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   671
            removeNotificationListener(name, nl);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   672
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   673
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   674
        public void removeNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   675
                ObjectName name, ObjectName listener,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   676
                NotificationFilter filter, Object handback)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   677
                throws InstanceNotFoundException, ListenerNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   678
            NotificationListener nl =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   679
                    (NotificationListener) getUserMBean(listener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   680
            removeNotificationListener(name, nl, filter, handback);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   681
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   682
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   683
        public void removeNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   684
                ObjectName name, NotificationListener listener)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   685
                throws InstanceNotFoundException, ListenerNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   686
            NotificationBroadcaster userMBean =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   687
                    (NotificationBroadcaster) getUserMBean(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   688
            NotificationListener wrappedListener =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   689
                  wrappedListener(name, userMBean, listener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   690
            userMBean.removeNotificationListener(wrappedListener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   691
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   692
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   693
        public void removeNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   694
                ObjectName name, NotificationListener listener,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   695
                NotificationFilter filter, Object handback)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   696
                throws InstanceNotFoundException, ListenerNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   697
            NotificationEmitter userMBean =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   698
                    (NotificationEmitter) getMBean(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   699
            NotificationListener wrappedListener =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   700
                  wrappedListener(name, userMBean, listener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   701
            userMBean.removeNotificationListener(wrappedListener, filter, handback);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   702
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   703
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   704
        public MBeanInfo getMBeanInfo(ObjectName name)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   705
        throws InstanceNotFoundException, IntrospectionException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   706
                ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   707
            return getMBean(name).getMBeanInfo();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   708
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   709
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   710
        public boolean isInstanceOf(ObjectName name, String className)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   711
        throws InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   712
            DynamicMBean mbean = getMBean(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   713
            String mbeanClassName = mbean.getMBeanInfo().getClassName();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   714
            if (className.equals(mbeanClassName))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   715
                return true;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   716
            ClassLoader loader = getUserMBean(mbean).getClass().getClassLoader();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   717
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   718
                Class<?> mbeanClass = Class.forName(mbeanClassName, false, loader);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   719
                Class<?> isInstClass = Class.forName(className, false, loader);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   720
                return isInstClass.isAssignableFrom(mbeanClass);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   721
            } catch (ClassNotFoundException e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   722
                return false;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   723
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   724
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   725
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   726
        public Object instantiate(String className)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   727
        throws ReflectionException, MBeanException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   728
            return instantiate(className, null, null);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   729
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   730
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   731
        public Object instantiate(String className, ObjectName loaderName)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   732
        throws ReflectionException, MBeanException, InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   733
            return instantiate(className, loaderName, null, null);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   734
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   735
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   736
        public Object instantiate(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   737
                String className, Object[] params, String[] signature)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   738
        throws ReflectionException, MBeanException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   739
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   740
                return instantiate(className, clrName, params, signature);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   741
            } catch (InstanceNotFoundException e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   742
                throw new RuntimeException(e);  // can't happen
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   743
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   744
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   745
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   746
        public Object instantiate(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   747
                String className, ObjectName loaderName,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   748
                Object[] params, String[] signature)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   749
        throws ReflectionException, MBeanException, InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   750
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   751
            if (params == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   752
                params = new Object[0];
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   753
            if (signature == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   754
                signature = new String[0];
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   755
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   756
            ClassLoader loader;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   757
            if (loaderName == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   758
                loader = this.getClass().getClassLoader();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   759
            else if (loaderName.equals(clrName))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   760
                loader = clr;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   761
            else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   762
                loader = (ClassLoader) getMBean(loaderName);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   763
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   764
            Class<?> c;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   765
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   766
                c = Class.forName(className, false, loader);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   767
            } catch (ClassNotFoundException e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   768
                throw new ReflectionException(e);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   769
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   770
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   771
            Constructor[] constrs = c.getConstructors();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   772
            Constructor found = null;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   773
            findconstr:
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   774
            for (Constructor constr : constrs) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   775
                Class<?>[] cTypes = constr.getParameterTypes();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   776
                if (cTypes.length == signature.length) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   777
                    for (int i = 0; i < cTypes.length; i++) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   778
                        if (!cTypes[i].getName().equals(signature[i]))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   779
                            continue findconstr;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   780
                    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   781
                    found = constr;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   782
                    break findconstr;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   783
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   784
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   785
            if (found == null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   786
                Exception x = new NoSuchMethodException(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   787
                        className + Arrays.toString(signature));
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   788
                throw new ReflectionException(x);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   789
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   790
            return invokeSomething(found, null, params);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   791
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   792
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   793
        @Deprecated
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   794
        public ObjectInputStream deserialize(ObjectName name, byte[] data)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   795
        throws InstanceNotFoundException, OperationsException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   796
            throw new UnsupportedOperationException();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   797
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   798
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   799
        @Deprecated
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   800
        public ObjectInputStream deserialize(String className, byte[] data)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   801
        throws OperationsException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   802
            throw new UnsupportedOperationException();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   803
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   804
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   805
        @Deprecated
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   806
        public ObjectInputStream deserialize(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   807
                String className, ObjectName loaderName, byte[] data)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   808
        throws InstanceNotFoundException, OperationsException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   809
            throw new UnsupportedOperationException();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   810
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   811
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   812
        public ClassLoader getClassLoaderFor(ObjectName mbeanName)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   813
        throws InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   814
            DynamicMBean mbean = getMBean(mbeanName);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   815
            Object userMBean = getUserMBean(mbean);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   816
            return userMBean.getClass().getClassLoader();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   817
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   818
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   819
        public ClassLoader getClassLoader(ObjectName loaderName)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   820
        throws InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   821
            return (ClassLoader) getMBean(loaderName);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   822
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   823
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   824
        public ClassLoaderRepository getClassLoaderRepository() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   825
            return new ClassLoaderRepository() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   826
                public Class<?> loadClass(String className)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   827
                throws ClassNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   828
                    return clr.loadClass(className);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   829
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   830
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   831
                public Class<?> loadClassWithout(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   832
                        ClassLoader exclude, String className)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   833
                throws ClassNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   834
                    return clr.loadClassWithout(exclude, className);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   835
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   836
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   837
                public Class<?> loadClassBefore(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   838
                        ClassLoader stop, String className)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   839
                throws ClassNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   840
                    return clr.loadClassBefore(stop, className);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   841
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   842
            };
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   843
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   844
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   845
        private static class ClassLoaderRepositoryImpl
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   846
                extends ClassLoader implements ClassLoaderRepository {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   847
            private List<ClassLoader> loaders = newList();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   848
            {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   849
                loaders.add(this.getClass().getClassLoader());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   850
                // We also behave as if the system class loader were in
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   851
                // the repository, since we do nothing to stop delegation
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   852
                // to the parent, which is the system class loader, and
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   853
                // that delegation happens before our findClass is called.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   854
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   855
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   856
            void addLoader(ClassLoader loader) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   857
                loaders.add(loader);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   858
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   859
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   860
            void removeLoader(ClassLoader loader) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   861
                if (!loaders.remove(loader))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   862
                    throw new RuntimeException("Loader was not in CLR!");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   863
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   864
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   865
            public Class<?> loadClassWithout(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   866
                    ClassLoader exclude, String className)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   867
                    throws ClassNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   868
                return loadClassWithoutBefore(exclude, null, className);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   869
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   870
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   871
            public Class<?> loadClassBefore(ClassLoader stop, String className)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   872
            throws ClassNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   873
                return loadClassWithoutBefore(null, stop, className);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   874
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   875
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   876
            private Class<?> loadClassWithoutBefore(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   877
                    ClassLoader exclude, ClassLoader stop, String className)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   878
                    throws ClassNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   879
                for (ClassLoader loader : loaders) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   880
                    if (loader == exclude)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   881
                        continue;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   882
                    if (loader == stop)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   883
                        break;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   884
                    try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   885
                        return Class.forName(className, false, loader);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   886
                    } catch (ClassNotFoundException e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   887
                        // OK: try others
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   888
                    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   889
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   890
                throw new ClassNotFoundException(className);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   891
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   892
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   893
            @Override
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   894
            protected Class<?> findClass(String className)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   895
            throws ClassNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   896
                return loadClassWithout(null, className);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   897
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   898
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   899
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   900
        /* There is zero or one ListenerTable per MBean.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   901
         * The ListenerTable stuff is complicated.  We want to rewrite the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   902
         * source of notifications so that if the source of a notification
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   903
         * from the MBean X is a reference to X itself, it gets replaced
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   904
         * by X's ObjectName.  To do this, we wrap the user's listener in
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   905
         * a RewriteListener.  But if the same listener is added a second
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   906
         * time (perhaps with a different filter or handback) we must
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   907
         * reuse the same RewriteListener so that the two-argument
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   908
         * removeNotificationListener(ObjectName,NotificationListener) will
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   909
         * correctly remove both listeners. This means we must remember the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   910
         * mapping from listener to WrappedListener.  But if the MBean
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   911
         * discards its listeners (as a result of removeNL or spontaneously)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   912
         * then we don't want to keep a reference to the WrappedListener.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   913
         * So we have tons of WeakReferences.  The key in the ListenerTable
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   914
         * is an IdentityListener, which wraps the user's listener to ensure
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   915
         * that identity and not equality is used during the lookup, even if
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   916
         * the user's listener has an equals method.  The value in the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   917
         * ListenerTable is a WeakReference wrapping a RewriteListener wrapping
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   918
         * the same IdentityListener.  Since the RewriteListener is what is
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   919
         * added to the user's MBean, the WeakReference won't disappear as long
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   920
         * as the MBean still has this listener.  And since it references the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   921
         * IdentityListener, that won't disappear either.  But once the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   922
         * RewriteListener is no longer referenced by the user's MBean,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   923
         * there's nothing to stop its WeakReference from being cleared,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   924
         * and then corresponding IdentityListener that is now only weakly
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   925
         * referenced from the key in the table.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   926
         */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   927
        private static class ListenerTable
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   928
                extends WeakHashMap<NotificationListener,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   929
                                    WeakReference<NotificationListener>> {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   930
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   931
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   932
        private static class IdentityListener implements NotificationListener {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   933
            private final NotificationListener userListener;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   934
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   935
            IdentityListener(NotificationListener userListener) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   936
                this.userListener = userListener;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   937
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   938
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   939
            public void handleNotification(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   940
                    Notification notification, Object handback) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   941
                userListener.handleNotification(notification, handback);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   942
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   943
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   944
            @Override
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   945
            public boolean equals(Object o) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   946
                return (this == o);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   947
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   948
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   949
            @Override
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   950
            public int hashCode() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   951
                return System.identityHashCode(this);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   952
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   953
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   954
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   955
        private static class RewriteListener implements NotificationListener {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   956
            private final ObjectName name;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   957
            private final Object userMBean;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   958
            private final NotificationListener userListener;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   959
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   960
            RewriteListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   961
                    ObjectName name, Object userMBean,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   962
                    NotificationListener userListener) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   963
                this.name = name;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   964
                this.userMBean = userMBean;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   965
                this.userListener = userListener;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   966
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   967
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   968
            public void handleNotification(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   969
                    Notification notification, Object handback) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   970
                if (notification.getSource() == userMBean)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   971
                    notification.setSource(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   972
                userListener.handleNotification(notification, handback);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   973
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   974
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   975
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   976
        private NotificationListener wrappedListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   977
                ObjectName name, Object userMBean, NotificationListener userListener)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   978
        throws InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   979
            ListenerTable table = new ListenerTable();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   980
            ListenerTable oldTable = listenerMap.putIfAbsent(name, table);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   981
            if (oldTable != null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   982
                table = oldTable;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   983
            NotificationListener identityListener =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   984
                    new IdentityListener(userListener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   985
            synchronized (table) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   986
                NotificationListener rewriteListener = null;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   987
                WeakReference<NotificationListener> wr =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   988
                        table.get(identityListener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   989
                if (wr != null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   990
                    rewriteListener = wr.get();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   991
                if (rewriteListener == null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   992
                    rewriteListener = new RewriteListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   993
                            name, userMBean, identityListener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   994
                    wr = new WeakReference<NotificationListener>(rewriteListener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   995
                    table.put(identityListener, wr);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   996
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   997
                return rewriteListener;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   998
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
   999
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1000
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1001
        private DynamicMBean getMBean(ObjectName name)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1002
        throws InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1003
            DynamicMBean mbean = mbeans.get(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1004
            if (mbean == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1005
                throw new InstanceNotFoundException(name.toString());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1006
            return mbean;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1007
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1008
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1009
        private static interface WrapDynamicMBean extends DynamicMBean {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1010
            public Object getWrappedMBean();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1011
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1012
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1013
        private static class StandardWrapper
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1014
                implements WrapDynamicMBean, MBeanRegistration {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1015
            private final Map<String, AttrMethods> attrMap = newMap();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1016
            private final Map<String, List<Method>> opMap = newMap();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1017
            private static class AttrMethods {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1018
                Method getter, setter;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1019
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1020
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1021
            private final Object std;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1022
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1023
            StandardWrapper(Object std) throws NotCompliantMBeanException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1024
                this.std = std;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1025
                Class<?> intf = mbeanInterface(std.getClass());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1026
                try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1027
                    initMaps(intf);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1028
                } catch (NotCompliantMBeanException e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1029
                    throw e;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1030
                } catch (Exception e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1031
                    NotCompliantMBeanException x =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1032
                            new NotCompliantMBeanException(e.getMessage());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1033
                    x.initCause(e);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1034
                    throw x;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1035
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1036
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1037
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1038
            private static Class<?> mbeanInterface(Class<?> c)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1039
            throws NotCompliantMBeanException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1040
                do {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1041
                    Class<?>[] intfs = c.getInterfaces();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1042
                    String intfName = c.getName() + "MBean";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1043
                    for (Class<?> intf : intfs) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1044
                        if (intf.getName().equals(intfName))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1045
                            return intf;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1046
                    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1047
                    c = c.getSuperclass();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1048
                } while (c != null);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1049
                throw new NotCompliantMBeanException(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1050
                        "Does not match Standard or Dynamic MBean patterns: " +
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1051
                        c.getName());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1052
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1053
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1054
            private void initMaps(Class<?> intf) throws NotCompliantMBeanException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1055
                Method[] methods = intf.getMethods();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1056
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1057
                for (Method m : methods) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1058
                    final String name = m.getName();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1059
                    final int nParams = m.getParameterTypes().length;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1060
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1061
                    String attrName = "";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1062
                    if (name.startsWith("get"))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1063
                        attrName = name.substring(3);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1064
                    else if (name.startsWith("is")
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1065
                    && m.getReturnType() == boolean.class)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1066
                        attrName = name.substring(2);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1067
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1068
                    if (attrName.length() != 0 && m.getParameterTypes().length == 0
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1069
                            && m.getReturnType() != void.class) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1070
                        // It's a getter
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1071
                        // Check we don't have both isX and getX
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1072
                        AttrMethods am = attrMap.get(attrName);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1073
                        if (am == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1074
                            am = new AttrMethods();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1075
                        else {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1076
                            if (am.getter != null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1077
                                final String msg = "Attribute " + attrName +
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1078
                                        " has more than one getter";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1079
                                throw new NotCompliantMBeanException(msg);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1080
                            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1081
                        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1082
                        am.getter = m;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1083
                        attrMap.put(attrName, am);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1084
                    } else if (name.startsWith("set") && name.length() > 3
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1085
                            && m.getParameterTypes().length == 1 &&
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1086
                            m.getReturnType() == void.class) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1087
                        // It's a setter
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1088
                        attrName = name.substring(3);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1089
                        AttrMethods am = attrMap.get(attrName);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1090
                        if (am == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1091
                            am = new AttrMethods();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1092
                        else if (am.setter != null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1093
                            final String msg = "Attribute " + attrName +
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1094
                                    " has more than one setter";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1095
                            throw new NotCompliantMBeanException(msg);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1096
                        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1097
                        am.setter = m;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1098
                        attrMap.put(attrName, am);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1099
                    } else {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1100
                        // It's an operation
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1101
                        List<Method> ops = opMap.get(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1102
                        if (ops == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1103
                            ops = newList();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1104
                        ops.add(m);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1105
                        opMap.put(name, ops);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1106
                    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1107
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1108
                /* Check that getters and setters are consistent. */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1109
                for (Map.Entry<String, AttrMethods> entry : attrMap.entrySet()) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1110
                    AttrMethods am = entry.getValue();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1111
                    if (am.getter != null && am.setter != null &&
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1112
                            am.getter.getReturnType() != am.setter.getParameterTypes()[0]) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1113
                        final String msg = "Getter and setter for " + entry.getKey() +
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1114
                                " have inconsistent types";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1115
                        throw new NotCompliantMBeanException(msg);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1116
                    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1117
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1118
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1119
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1120
            public Object getAttribute(String attribute)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1121
            throws AttributeNotFoundException, MBeanException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1122
                AttrMethods am = attrMap.get(attribute);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1123
                if (am == null || am.getter == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1124
                    throw new AttributeNotFoundException(attribute);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1125
                return invokeMethod(am.getter);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1126
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1127
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1128
            public void setAttribute(Attribute attribute)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1129
            throws AttributeNotFoundException, InvalidAttributeValueException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1130
                    MBeanException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1131
                String name = attribute.getName();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1132
                AttrMethods am = attrMap.get(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1133
                if (am == null || am.setter == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1134
                    throw new AttributeNotFoundException(name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1135
                invokeMethod(am.setter, attribute.getValue());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1136
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1137
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1138
            public AttributeList getAttributes(String[] attributes) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1139
                AttributeList list = new AttributeList();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1140
                for (String attr : attributes) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1141
                    try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1142
                        list.add(new Attribute(attr, getAttribute(attr)));
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1143
                    } catch (Exception e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1144
                        // OK: ignore per spec
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1145
                    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1146
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1147
                return list;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1148
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1149
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1150
            public AttributeList setAttributes(AttributeList attributes) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1151
                AttributeList list = new AttributeList();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1152
                // We carefully avoid using any new stuff from AttributeList here!
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1153
                for (Iterator<?> it = attributes.iterator(); it.hasNext(); ) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1154
                    Attribute attr = (Attribute) it.next();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1155
                    try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1156
                        setAttribute(attr);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1157
                        list.add(attr);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1158
                    } catch (Exception e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1159
                        // OK: ignore per spec
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1160
                    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1161
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1162
                return list;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1163
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1164
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1165
            public Object invoke(String actionName, Object[] params, String[] signature)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1166
            throws MBeanException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1167
                if (params == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1168
                    params = new Object[0];
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1169
                if (signature == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1170
                    signature = new String[0];
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1171
                List<Method> methods = opMap.get(actionName);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1172
                if (methods == null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1173
                    Exception x = new NoSuchMethodException(actionName);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1174
                    throw new MBeanException(x);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1175
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1176
                Method found = null;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1177
                methodloop:
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1178
                for (Method m : methods) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1179
                    Class<?>[] msig = m.getParameterTypes();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1180
                    if (msig.length != signature.length)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1181
                        continue methodloop;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1182
                    for (int i = 0; i < msig.length; i++) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1183
                        if (!msig[i].getName().equals(signature[i]))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1184
                            continue methodloop;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1185
                    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1186
                    found = m;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1187
                    break methodloop;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1188
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1189
                if (found == null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1190
                    Exception x = new NoSuchMethodException(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1191
                            actionName + Arrays.toString(signature));
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1192
                    throw new MBeanException(x);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1193
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1194
                return invokeMethod(found, params);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1195
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1196
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1197
            public MBeanInfo getMBeanInfo() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1198
                // Attributes
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1199
                List<MBeanAttributeInfo> attrs = newList();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1200
                for (Map.Entry<String, AttrMethods> attr : attrMap.entrySet()) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1201
                    String name = attr.getKey();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1202
                    AttrMethods am = attr.getValue();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1203
                    try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1204
                        attrs.add(new MBeanAttributeInfo(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1205
                                name, name, am.getter, am.setter));
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1206
                    } catch (IntrospectionException e) { // grrr
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1207
                        throw new RuntimeException(e);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1208
                    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1209
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1210
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1211
                // Operations
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1212
                List<MBeanOperationInfo> ops = newList();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1213
                for (Map.Entry<String, List<Method>> op : opMap.entrySet()) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1214
                    String name = op.getKey();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1215
                    List<Method> methods = op.getValue();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1216
                    for (Method m : methods)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1217
                        ops.add(new MBeanOperationInfo(name, m));
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1218
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1219
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1220
                // Constructors
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1221
                List<MBeanConstructorInfo> constrs = newList();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1222
                for (Constructor constr : std.getClass().getConstructors())
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1223
                    constrs.add(new MBeanConstructorInfo("Constructor", constr));
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1224
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1225
                // Notifications
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1226
                MBeanNotificationInfo[] notifs;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1227
                if (std instanceof NotificationBroadcaster)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1228
                    notifs = ((NotificationBroadcaster) std).getNotificationInfo();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1229
                else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1230
                    notifs = null;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1231
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1232
                String className = std.getClass().getName();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1233
                return new MBeanInfo(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1234
                        className, className,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1235
                        attrs.toArray(new MBeanAttributeInfo[0]),
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1236
                        constrs.toArray(new MBeanConstructorInfo[0]),
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1237
                        ops.toArray(new MBeanOperationInfo[0]),
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1238
                        notifs);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1239
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1240
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1241
            private Object invokeMethod(Method m, Object... args)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1242
            throws MBeanException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1243
                return invokeSomething(m, std,args);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1244
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1245
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1246
            public ObjectName preRegister(MBeanServer server, ObjectName name)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1247
            throws Exception {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1248
                return mbeanRegistration(std).preRegister(server, name);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1249
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1250
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1251
            public void postRegister(Boolean registrationDone) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1252
                mbeanRegistration(std).postRegister(registrationDone);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1253
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1254
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1255
            public void preDeregister() throws Exception {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1256
                mbeanRegistration(std).preDeregister();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1257
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1258
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1259
            public void postDeregister() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1260
                mbeanRegistration(std).postDeregister();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1261
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1262
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1263
            public Object getWrappedMBean() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1264
                return std;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1265
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1266
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1267
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1268
        private DynamicMBean standardToDynamic(Object std)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1269
        throws NotCompliantMBeanException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1270
            return new StandardWrapper(std);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1271
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1272
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1273
//        private static class NotifWrapper
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1274
//                implements WrapDynamicMBean, NotificationEmitter {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1275
//            private final DynamicMBean mbean;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1276
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1277
//            NotifWrapper(DynamicMBean mbean) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1278
//                this.mbean = mbean;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1279
//            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1280
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1281
//            public Object getAttribute(String attribute)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1282
//            throws AttributeNotFoundException, MBeanException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1283
//                return mbean.getAttribute(attribute);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1284
//            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1285
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1286
//            public void setAttribute(Attribute attribute)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1287
//            throws AttributeNotFoundException, InvalidAttributeValueException,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1288
//                    MBeanException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1289
//                mbean.setAttribute(attribute);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1290
//            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1291
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1292
//            public AttributeList getAttributes(String[] attributes) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1293
//                return mbean.getAttributes(attributes);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1294
//            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1295
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1296
//            public AttributeList setAttributes(AttributeList attributes) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1297
//                return mbean.setAttributes(attributes);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1298
//            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1299
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1300
//            public Object invoke(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1301
//                    String actionName, Object[] params, String[] signature)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1302
//                    throws MBeanException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1303
//                return mbean.invoke(actionName, params, signature);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1304
//            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1305
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1306
//            public MBeanInfo getMBeanInfo() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1307
//                return mbean.getMBeanInfo();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1308
//            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1309
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1310
//            public void removeNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1311
//                    NotificationListener listener, NotificationFilter filter, Object handback)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1312
//            throws ListenerNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1313
//                ((NotificationEmitter) mbean).removeNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1314
//                        listener, filter, handback);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1315
//                // ClassCastException if MBean is not an emitter
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1316
//            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1317
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1318
//            public void addNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1319
//                    NotificationListener listener, NotificationFilter filter, Object handback)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1320
//            throws IllegalArgumentException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1321
//                ((NotificationBroadcaster) mbean).addNotificationListener(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1322
//                        listener, filter, handback);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1323
//            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1324
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1325
//            public void removeNotificationListener(NotificationListener listener)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1326
//            throws ListenerNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1327
//                ((NotificationBroadcaster) mbean).removeNotificationListener(listener);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1328
//            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1329
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1330
//            public MBeanNotificationInfo[] getNotificationInfo() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1331
//                return ((NotificationBroadcaster) mbean).getNotificationInfo();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1332
//            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1333
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1334
//            public Object getWrappedMBean() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1335
//                return getUserMBean(mbean);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1336
//            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1337
//        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1338
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1339
        private static Object invokeSomething(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1340
                AccessibleObject ao, Object target, Object[] args)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1341
        throws MBeanException, ReflectionException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1342
            try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1343
                if (ao instanceof Method)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1344
                    return ((Method) ao).invoke(target, args);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1345
                else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1346
                    return ((Constructor) ao).newInstance(args);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1347
            } catch (InvocationTargetException e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1348
                try {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1349
                    throw e.getCause();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1350
                } catch (RuntimeException x) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1351
                    throw new RuntimeMBeanException(x);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1352
                } catch (Error x) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1353
                    throw new RuntimeErrorException(x);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1354
                } catch (Exception x) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1355
                    throw new MBeanException(x);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1356
                } catch (Throwable x) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1357
                    throw new RuntimeException(x); // neither Error nor Exception!
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1358
                }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1359
            } catch (Exception e) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1360
                throw new ReflectionException(e);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1361
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1362
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1363
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1364
        private static Object getUserMBean(DynamicMBean mbean) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1365
            if (mbean instanceof WrapDynamicMBean)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1366
                return ((WrapDynamicMBean) mbean).getWrappedMBean();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1367
            return mbean;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1368
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1369
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1370
        private Object getUserMBean(ObjectName name)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1371
        throws InstanceNotFoundException {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1372
            return getUserMBean(getMBean(name));
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1373
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1374
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1375
        private static final MBeanRegistration noRegistration =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1376
                new MBeanRegistration() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1377
            public ObjectName preRegister(MBeanServer server, ObjectName name) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1378
                return name;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1379
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1380
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1381
            public void postRegister(Boolean registrationDone) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1382
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1383
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1384
            public void preDeregister() throws Exception {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1385
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1386
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1387
            public void postDeregister() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1388
            }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1389
        };
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1390
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1391
        private static MBeanRegistration mbeanRegistration(Object object) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1392
            if (object instanceof MBeanRegistration)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1393
                return (MBeanRegistration) object;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1394
            else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1395
                return noRegistration;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1396
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1397
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1398
        private static <E> List<E> newList() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1399
            return new ArrayList<E>();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1400
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1401
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1402
        private static <K, V> Map<K, V> newMap() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1403
            return new HashMap<K, V>();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1404
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1405
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1406
        private static <E> Set<E> newSet() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1407
            return new HashSet<E>();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1408
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1409
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents:
diff changeset
  1410
}