jdk/src/share/classes/javax/management/remote/JMXConnectorServer.java
author sjiang
Thu, 31 Jul 2008 15:31:13 +0200
changeset 1004 5ba8217eb504
parent 2 90ce3da70b43
child 1247 b4c26443dee5
permissions -rw-r--r--
5108776: Add reliable event handling to the JMX API 6218920: API bug - impossible to delete last MBeanServerForwarder on a connector Reviewed-by: emcmanus
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package javax.management.remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    29
import com.sun.jmx.remote.util.EnvHelp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    35
import java.util.NoSuchElementException;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    36
import javax.management.MBeanInfo;  // for javadoc
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.MBeanNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.MBeanRegistration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.NotificationBroadcasterSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.ObjectName;
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    43
import javax.management.event.EventClientDelegate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>Superclass of every connector server.  A connector server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * attached to an MBean server.  It listens for client connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * requests and creates a connection for each one.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>A connector server is associated with an MBean server either by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * registering it in that MBean server, or by passing the MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * to its constructor.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>A connector server is inactive when created.  It only starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * listening for client connections when the {@link #start() start}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * method is called.  A connector server stops listening for client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * connections when the {@link #stop() stop} method is called or when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the connector server is unregistered from its MBean server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>Stopping a connector server does not unregister it from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * MBean server.  A connector server once stopped cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * restarted.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>Each time a client connection is made or broken, a notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * of class {@link JMXConnectionNotification} is emitted.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
public abstract class JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        implements JMXConnectorServerMBean, MBeanRegistration, JMXAddressable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <p>Name of the attribute that specifies the authenticator for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * connector server.  The value associated with this attribute, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * any, must be an object that implements the interface {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * JMXAuthenticator}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final String AUTHENTICATOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        "jmx.remote.authenticator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    82
     /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    83
      * <p>Name of the attribute that specifies whether this connector
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    84
      * server can delegate notification handling to the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    85
      * {@linkplain javax.management.event Event Service}.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    86
      * The value associated with
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    87
      * this attribute, if any, is a String, which must be equal,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    88
      * ignoring case, to {@code "true"} or {@code "false"}.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    89
      *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    90
      * <p>Not all connector servers will understand this attribute, but the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    91
      * standard {@linkplain javax.management.remote.rmi.RMIConnectorServer
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    92
      * RMI Connector Server} does.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    93
      *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    94
      * <p>If this attribute is not present, then the system property of the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    95
      * same name (<code>{@value}</code>) is consulted. If that is not set
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    96
      * either, then the Event Service is used if the connector server
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    97
      * supports it.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    98
      *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    99
      * @since 1.7
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   100
      */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   101
     public static final String DELEGATE_TO_EVENT_SERVICE =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   102
         "jmx.remote.delegate.event.service";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   103
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   104
     /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   105
      * <p>Name of the attribute that specifies whether this connector
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   106
      * server simulates the existence of the {@link EventClientDelegate}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   107
      * MBean. The value associated with this attribute, if any, must
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   108
      * be a string that is equal to {@code "true"} or {@code "false"},
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   109
      * ignoring case. If it is {@code "true"}, then the connector server
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   110
      * will simulate an EventClientDelegate MBean, as described in {@link
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   111
      * EventClientDelegate#newForwarder}. This MBean is needed for {@link
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   112
      * javax.management.event.EventClient EventClient} to function correctly.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   113
      *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   114
      * <p>Not all connector servers will understand this attribute, but the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   115
      * standard {@linkplain javax.management.remote.rmi.RMIConnectorServer
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   116
      * RMI Connector Server} does.  For a connector server that understands
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   117
      * this attribute, the default value is {@code "true"}.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   118
      *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   119
      * @since 1.7
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   120
      */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   121
     public static final String EVENT_CLIENT_DELEGATE_FORWARDER =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   122
         "jmx.remote.event.client.delegate.forwarder";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   123
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p>Constructs a connector server that will be registered as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * MBean in the MBean server it is attached to.  This constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * is typically called by one of the <code>createMBean</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * methods when creating, within an MBean server, a connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * server that makes it available remotely.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public JMXConnectorServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p>Constructs a connector server that is attached to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * MBean server.  A connector server that is created in this way
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   138
     * can be registered in a different MBean server, or not registered
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   139
     * in any MBean server.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param mbeanServer the MBean server that this connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * is attached to.  Null if this connector server will be attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * to an MBean server by being registered in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public JMXConnectorServer(MBeanServer mbeanServer) {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   146
        insertUserMBeanServer(mbeanServer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <p>Returns the MBean server that this connector server is
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   151
     * attached to, or the first in a chain of user-added
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   152
     * {@link MBeanServerForwarder}s, if any.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @return the MBean server that this connector server is attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * to, or null if it is not yet attached to an MBean server.
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   156
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   157
     * @see #setMBeanServerForwarder
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   158
     * @see #getSystemMBeanServer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public synchronized MBeanServer getMBeanServer() {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   161
        return userMBeanServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   162
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   163
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   164
    public synchronized void setMBeanServerForwarder(MBeanServerForwarder mbsf) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   165
        if (mbsf == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   166
            throw new IllegalArgumentException("Invalid null argument: mbsf");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   167
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   168
        if (userMBeanServer != null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   169
            mbsf.setMBeanServer(userMBeanServer);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   170
        insertUserMBeanServer(mbsf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   173
    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   174
     * <p>Remove a forwarder from the chain of forwarders.  The forwarder can
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   175
     * be in the system chain or the user chain.  On successful return from
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   176
     * this method, the first occurrence in the chain of an object that is
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   177
     * {@linkplain Object#equals equal} to {@code mbsf} will have been
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   178
     * removed.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   179
     * @param mbsf the forwarder to remove
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   180
     * @throws NoSuchElementException if there is no occurrence of {@code mbsf}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   181
     * in the chain.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   182
     * @throws IllegalArgumentException if {@code mbsf} is null.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   183
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   184
    public synchronized void removeMBeanServerForwarder(MBeanServerForwarder mbsf) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (mbsf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new IllegalArgumentException("Invalid null argument: mbsf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   188
        MBeanServerForwarder prev = null;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   189
        MBeanServer curr = systemMBeanServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   190
        while (curr instanceof MBeanServerForwarder && !mbsf.equals(curr)) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   191
            prev = (MBeanServerForwarder) curr;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   192
            curr = prev.getMBeanServer();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   193
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   194
        if (!(curr instanceof MBeanServerForwarder))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   195
            throw new NoSuchElementException("MBeanServerForwarder not in chain");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   196
        MBeanServerForwarder deleted = (MBeanServerForwarder) curr;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   197
        MBeanServer next = deleted.getMBeanServer();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   198
        if (prev != null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   199
            prev.setMBeanServer(next);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   200
        if (systemMBeanServer == deleted)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   201
            systemMBeanServer = next;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   202
        if (userMBeanServer == deleted)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   203
            userMBeanServer = next;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   204
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   205
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   206
    /*
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   207
     * Set userMBeanServer to mbs and arrange for the end of the chain of
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   208
     * system MBeanServerForwarders to point to it.  See the comment before
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   209
     * the systemMBeanServer and userMBeanServer field declarations.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   210
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   211
    private void insertUserMBeanServer(MBeanServer mbs) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   212
        MBeanServerForwarder lastSystemMBSF = null;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   213
        for (MBeanServer mbsi = systemMBeanServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   214
             mbsi != userMBeanServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   215
             mbsi = lastSystemMBSF.getMBeanServer()) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   216
            lastSystemMBSF = (MBeanServerForwarder) mbsi;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   217
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   218
        userMBeanServer = mbs;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   219
        if (lastSystemMBSF == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   220
            systemMBeanServer = mbs;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   221
        else
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   222
            lastSystemMBSF.setMBeanServer(mbs);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   223
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   224
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   225
    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   226
     * <p>Returns the first item in the chain of system and then user
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   227
     * forwarders.  In the simplest case, a {@code JMXConnectorServer}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   228
     * is connected directly to an {@code MBeanServer}.  But there can
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   229
     * also be a chain of {@link MBeanServerForwarder}s between the two.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   230
     * This chain consists of two sub-chains: first the <em>system chain</em>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   231
     * and then the <em>user chain</em>.  Incoming requests are given to the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   232
     * first forwarder in the system chain.  Each forwarder can handle
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   233
     * a request itself, or more usually forward it to the next forwarder,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   234
     * perhaps with some extra behavior such as logging or security
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   235
     * checking before or after the forwarding.  The last forwarder in
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   236
     * the system chain is followed by the first forwarder in the user
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   237
     * chain.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   238
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   239
     * <p>The <em>system chain</em> is usually
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   240
     * defined by a connector server based on the environment Map;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   241
     * see {@link JMXConnectorServerFactory#newJMXConnectorServer}.  Allowing the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   242
     * connector server to define its forwarders in this way ensures that
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   243
     * they are in the correct order - some forwarders need to be inserted
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   244
     * before others for correct behavior.  It is possible to modify the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   245
     * system chain, for example using {@link #setSystemMBeanServerForwarder} or
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   246
     * {@link #removeMBeanServerForwarder}, but in that case the system
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   247
     * chain is no longer guaranteed to be correct.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   248
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   249
     * <p>The <em>user chain</em> is defined by calling {@link
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   250
     * #setMBeanServerForwarder} to insert forwarders at the head of the user
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   251
     * chain.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   252
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   253
     * <p>If there are no forwarders in either chain, then both
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   254
     * {@link #getMBeanServer()} and {@code getSystemMBeanServer()} will
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   255
     * return the {@code MBeanServer} for this connector server.  If there
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   256
     * are forwarders in the user chain but not the system chain, then
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   257
     * both methods will return the first forwarder in the user chain.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   258
     * If there are forwarders in the system chain but not the user chain,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   259
     * then {@code getSystemMBeanServer()} will return the first forwarder
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   260
     * in the system chain, and {@code getMBeanServer()} will return the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   261
     * {@code MBeanServer} for this connector server.  Finally, if there
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   262
     * are forwarders in each chain then {@code getSystemMBeanServer()}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   263
     * will return the first forwarder in the system chain, and {@code
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   264
     * getMBeanServer()} will return the first forwarder in the user chain.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   265
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   266
     * <p>This code illustrates how the chains can be traversed:</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   267
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   268
     * <pre>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   269
     * JMXConnectorServer cs;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   270
     * System.out.println("system chain:");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   271
     * MBeanServer mbs = cs.getSystemMBeanServer();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   272
     * while (true) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   273
     *     if (mbs == cs.getMBeanServer())
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   274
     *         System.out.println("user chain:");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   275
     *     if (!(mbs instanceof MBeanServerForwarder))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   276
     *         break;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   277
     *     MBeanServerForwarder mbsf = (MBeanServerForwarder) mbs;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   278
     *     System.out.println("--forwarder: " + mbsf);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   279
     *     mbs = mbsf.getMBeanServer();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   280
     * }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   281
     * System.out.println("--MBean Server");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   282
     * </pre>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   283
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   284
     * @return the first item in the system chain of forwarders.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   285
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   286
     * @see #setSystemMBeanServerForwarder
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   287
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   288
    public synchronized MBeanServer getSystemMBeanServer() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   289
        return systemMBeanServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   290
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   291
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   292
    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   293
     * <p>Inserts an object that intercepts requests for the MBean server
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   294
     * that arrive through this connector server.  This object will be
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   295
     * supplied as the <code>MBeanServer</code> for any new connection
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   296
     * created by this connector server.  Existing connections are
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   297
     * unaffected.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   298
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   299
     * <p>This method can be called more than once with different
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   300
     * {@link MBeanServerForwarder} objects.  The result is a chain
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   301
     * of forwarders.  The last forwarder added is the first in the chain.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   302
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   303
     * <p>This method modifies the system chain of {@link MBeanServerForwarder}s.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   304
     * Usually user code should change the user chain instead, via
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   305
     * {@link #setMBeanServerForwarder}.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   306
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   307
     * <p>Not all connector servers support a system chain of forwarders.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   308
     * Calling this method on a connector server that does not will produce an
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   309
     * {@link UnsupportedOperationException}.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   310
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   311
     * <p>Suppose {@code mbs} is the result of {@link #getSystemMBeanServer()}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   312
     * before calling this method.  If {@code mbs} is not null, then
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   313
     * {@code mbsf.setMBeanServer(mbs)} will be called.  If doing so
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   314
     * produces an exception, this method throws the same exception without
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   315
     * any other effect.  If {@code mbs} is null, or if the call to
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   316
     * {@code mbsf.setMBeanServer(mbs)} succeeds, then this method will
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   317
     * return normally and {@code getSystemMBeanServer()} will then return
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   318
     * {@code mbsf}.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   319
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   320
     * <p>The result of {@link #getMBeanServer()} is unchanged by this method.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   321
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   322
     * @param mbsf the new <code>MBeanServerForwarder</code>.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   323
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   324
     * @throws IllegalArgumentException if the call to {@link
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   325
     * MBeanServerForwarder#setMBeanServer mbsf.setMBeanServer} fails
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   326
     * with <code>IllegalArgumentException</code>, or if
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   327
     * <code>mbsf</code> is null.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   328
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   329
     * @throws UnsupportedOperationException if
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   330
     * {@link #supportsSystemMBeanServerForwarder} returns false.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   331
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   332
     * @see #getSystemMBeanServer()
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   333
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   334
    public synchronized void setSystemMBeanServerForwarder(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   335
            MBeanServerForwarder mbsf) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   336
        if (mbsf == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   337
            throw new IllegalArgumentException("Invalid null argument: mbsf");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   338
        mustSupportSystemMBSF();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   339
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   340
        if (systemMBeanServer != null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   341
            mbsf.setMBeanServer(systemMBeanServer);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   342
        systemMBeanServer = mbsf;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   343
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   344
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   345
    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   346
     * <p>Returns true if this connector server supports a system chain of
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   347
     * {@link MBeanServerForwarder}s.  The default implementation of this
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   348
     * method returns false.  Connector servers that do support the system
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   349
     * chain must override this method to return true.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   350
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   351
     * @return true if this connector server supports the system chain of
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   352
     * forwarders.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   353
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   354
    public boolean supportsSystemMBeanServerForwarder() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   355
        return false;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   356
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   357
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   358
    private void mustSupportSystemMBSF() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   359
        if (!supportsSystemMBeanServerForwarder()) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   360
            throw new UnsupportedOperationException(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   361
                    "System MBeanServerForwarder not supported by this " +
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   362
                    "connector server");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   363
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   364
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   365
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   366
    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   367
     * <p>Install {@link MBeanServerForwarder}s in the system chain
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   368
     * based on the attributes in the given {@code Map}.  A connector
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   369
     * server that {@linkplain #supportsSystemMBeanServerForwarder supports}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   370
     * a system chain of {@code MBeanServerForwarder}s can call this method
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   371
     * to add forwarders to that chain based on the contents of {@code env}.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   372
     * In order:</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   373
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   374
     * <ul>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   375
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   376
     * <li>If {@link #EVENT_CLIENT_DELEGATE_FORWARDER} is absent, or is
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   377
     * present with the value {@code "true"}, then a forwarder with the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   378
     * functionality of {@link EventClientDelegate#newForwarder} is inserted
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   379
     * at the start of the system chain.</li>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   380
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   381
     * </ul>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   382
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   383
     * <p>For {@code EVENT_CLIENT_DELEGATE_FORWARDER}, if the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   384
     * attribute is absent from the {@code Map} and a system property
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   385
     * of the same name is defined, then the value of the system
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   386
     * property is used as if it were in the {@code Map}.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   387
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   388
     * <p>Attributes in {@code env} that are not listed above are ignored
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   389
     * by this method.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   390
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   391
     * @throws UnsupportedOperationException if {@link
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   392
     * #supportsSystemMBeanServerForwarder} is false.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   393
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   394
    protected void installStandardForwarders(Map<String, ?> env) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   395
        mustSupportSystemMBSF();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   396
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   397
        // Remember that forwarders must be added in reverse order!
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   398
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   399
        boolean ecd = EnvHelp.computeBooleanFromString(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   400
                env, EVENT_CLIENT_DELEGATE_FORWARDER, false, true);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   401
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   402
        if (ecd) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   403
            MBeanServerForwarder mbsf = EventClientDelegate.newForwarder();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   404
            setSystemMBeanServerForwarder(mbsf);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   405
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public String[] getConnectionIds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        synchronized (connectionIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            return connectionIds.toArray(new String[connectionIds.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <p>Returns a client stub for this connector server.  A client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * stub is a serializable object whose {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * JMXConnector#connect(Map) connect} method can be used to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * one new connection to this connector server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <p>A given connector need not support the generation of client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * stubs.  However, the connectors specified by the JMX Remote API do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * (JMXMP Connector and RMI Connector).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <p>The default implementation of this method uses {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * #getAddress} and {@link JMXConnectorFactory} to generate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * stub, with code equivalent to the following:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * JMXServiceURL addr = {@link #getAddress() getAddress()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * return {@link JMXConnectorFactory#newJMXConnector(JMXServiceURL, Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *          JMXConnectorFactory.newJMXConnector(addr, env)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <p>A connector server for which this is inappropriate must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * override this method so that it either implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * appropriate logic or throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * UnsupportedOperationException}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param env client connection parameters of the same sort that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * could be provided to {@link JMXConnector#connect(Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * JMXConnector.connect(Map)}.  Can be null, which is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * to an empty map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @return a client stub that can be used to make a new connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * to this connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @exception UnsupportedOperationException if this connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * server does not support the generation of client stubs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @exception IllegalStateException if the JMXConnectorServer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * not started (see {@link JMXConnectorServerMBean#isActive()}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @exception IOException if a communications problem means that a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * stub cannot be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public JMXConnector toJMXConnector(Map<String,?> env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (!isActive()) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            IllegalStateException("Connector is not active");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        JMXServiceURL addr = getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return JMXConnectorFactory.newJMXConnector(addr, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <p>Returns an array indicating the notifications that this MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * sends. The implementation in <code>JMXConnectorServer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * returns an array with one element, indicating that it can emit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * notifications of class {@link JMXConnectionNotification} with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * the types defined in that class.  A subclass that can emit other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * notifications should return an array that contains this element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * plus descriptions of the other notifications.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @return the array of possible notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public MBeanNotificationInfo[] getNotificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        final String[] types = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            JMXConnectionNotification.OPENED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            JMXConnectionNotification.CLOSED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            JMXConnectionNotification.FAILED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        final String className = JMXConnectionNotification.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        final String description =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            "A client connection has been opened or closed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return new MBeanNotificationInfo[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            new MBeanNotificationInfo(types, className, description),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * <p>Called by a subclass when a new client connection is opened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Adds <code>connectionId</code> to the list returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * #getConnectionIds()}, then emits a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * JMXConnectionNotification} with type {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * JMXConnectionNotification#OPENED}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @param connectionId the ID of the new connection.  This must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * different from the ID of any connection previously opened by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * this connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @param message the message for the emitted {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * Notification#getMessage()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @param userData the <code>userData</code> for the emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * {@link JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Notification#getUserData()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @exception NullPointerException if <code>connectionId</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    protected void connectionOpened(String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                    String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                    Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (connectionId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            throw new NullPointerException("Illegal null argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        synchronized (connectionIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            connectionIds.add(connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        sendNotification(JMXConnectionNotification.OPENED, connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                         message, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * <p>Called by a subclass when a client connection is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * normally.  Removes <code>connectionId</code> from the list returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * by {@link #getConnectionIds()}, then emits a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * JMXConnectionNotification} with type {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * JMXConnectionNotification#CLOSED}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @param connectionId the ID of the closed connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @param message the message for the emitted {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Notification#getMessage()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @param userData the <code>userData</code> for the emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * {@link JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Notification#getUserData()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @exception NullPointerException if <code>connectionId</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    protected void connectionClosed(String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                                    String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                                    Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (connectionId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            throw new NullPointerException("Illegal null argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        synchronized (connectionIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            connectionIds.remove(connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        sendNotification(JMXConnectionNotification.CLOSED, connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                         message, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <p>Called by a subclass when a client connection fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * Removes <code>connectionId</code> from the list returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * {@link #getConnectionIds()}, then emits a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * JMXConnectionNotification} with type {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * JMXConnectionNotification#FAILED}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @param connectionId the ID of the failed connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @param message the message for the emitted {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * Notification#getMessage()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @param userData the <code>userData</code> for the emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * {@link JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Notification#getUserData()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @exception NullPointerException if <code>connectionId</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    protected void connectionFailed(String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                                    String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                                    Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (connectionId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            throw new NullPointerException("Illegal null argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        synchronized (connectionIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            connectionIds.remove(connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        sendNotification(JMXConnectionNotification.FAILED, connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                         message, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    private void sendNotification(String type, String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                                  String message, Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        Notification notif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            new JMXConnectionNotification(type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                                          getNotificationSource(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                                          connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                                          nextSequenceNumber(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                                          message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                          userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        sendNotification(notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    private synchronized Object getNotificationSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (myName != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            return myName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    private static long nextSequenceNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        synchronized (sequenceNumberLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            return sequenceNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    // implements MBeanRegistration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <p>Called by an MBean server when this connector server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * registered in that MBean server.  This connector server becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * attached to the MBean server and its {@link #getMBeanServer()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * method will return <code>mbs</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <p>If this connector server is already attached to an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * server, this method has no effect.  The MBean server it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * attached to is not necessarily the one it is being registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * in.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @param mbs the MBean server in which this connection server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * being registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @param name The object name of the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @return The name under which the MBean is to be registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @exception NullPointerException if <code>mbs</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * <code>name</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    public synchronized ObjectName preRegister(MBeanServer mbs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                               ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if (mbs == null || name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            throw new NullPointerException("Null MBeanServer or ObjectName");
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   648
        if (userMBeanServer == null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   649
            insertUserMBeanServer(mbs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            myName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    public void postRegister(Boolean registrationDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * <p>Called by an MBean server when this connector server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * unregistered from that MBean server.  If this connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * was attached to that MBean server by being registered in it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * and if the connector server is still active,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * then unregistering it will call the {@link #stop stop} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * If the <code>stop</code> method throws an exception, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * unregistration attempt will fail.  It is recommended to call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * the <code>stop</code> method explicitly before unregistering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * the MBean.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @exception IOException if thrown by the {@link #stop stop} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    public synchronized void preDeregister() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        if (myName != null && isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            myName = null; // just in case stop is buggy and doesn't stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    public void postDeregister() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        myName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   683
    /*
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   684
     * Fields describing the chains of forwarders (MBeanServerForwarders).
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   685
     * In the general case, the forwarders look something like this:
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   686
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   687
     * systemMBeanServer          userMBeanServer
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   688
     * |                          |
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   689
     * v                          v
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   690
     * mbsf1 -> mbsf2 -> mbsf3 -> mbsf4 -> mbsf5 -> mbs
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   691
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   692
     * Here, each mbsfi is an MBeanServerForwarder, and the arrows
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   693
     * illustrate its getMBeanServer() method.  The last MBeanServerForwarder
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   694
     * can point to an MBeanServer that is not instanceof MBeanServerForwarder,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   695
     * here mbs.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   696
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   697
     * Initially, the chain can be empty if this JMXConnectorServer was
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   698
     * constructed without an MBeanServer.  In this case, both systemMBS
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   699
     * and userMBS will be null.  If there is initially an MBeanServer,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   700
     * then both systemMBS and userMBS will point to it.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   701
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   702
     * Whenever userMBS is changed, the system chain must be updated. If there
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   703
     * are forwarders in the system chain (between systemMBS and userMBS in the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   704
     * picture above), then the last one must point to the old value of userMBS
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   705
     * (possibly null). It must be updated to point to the new value. If there
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   706
     * are no forwarders in the system chain, then systemMBS must be updated to
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   707
     * the new value of userMBS. The invariant is that starting from systemMBS
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   708
     * and repeatedly calling MBSF.getMBeanServer() you will end up at
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   709
     * userMBS.  The implication is that you will not see any MBeanServer
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   710
     * object on the way that is not also an MBeanServerForwarder.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   711
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   712
     * The method insertUserMBeanServer contains the logic to change userMBS
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   713
     * and adjust the system chain appropriately.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   714
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   715
     * If userMBS is null and this JMXConnectorServer is registered in an
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   716
     * MBeanServer, then userMBS becomes that MBeanServer, and the system
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   717
     * chain must be updated as just described.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   718
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   719
     * When systemMBS is updated, there is no effect on userMBS. The system
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   720
     * chain may contain forwarders even though the user chain is empty
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   721
     * (there is no MBeanServer). In that case an attempt to forward an
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   722
     * incoming request through the chain will fall off the end and fail with a
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   723
     * NullPointerException. Usually a connector server will refuse to start()
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   724
     * if it is not attached to an MBS, so this situation should not arise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   726
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   727
    private MBeanServer userMBeanServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   728
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   729
    private MBeanServer systemMBeanServer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * The name used to registered this server in an MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * It is null if the this server is not registered or has been unregistered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    private ObjectName myName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    private List<String> connectionIds = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    private static final int[] sequenceNumberLock = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    private static long sequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
}