jdk/src/share/classes/javax/management/remote/JMXConnectorServer.java
author sjiang
Tue, 09 Dec 2008 19:44:22 +0100
changeset 1708 4e1939e6e6b5
parent 1570 4165709c91e3
child 4156 acaa49a2768a
permissions -rw-r--r--
6332907: Add ability for connector server to close individual connections Reviewed-by: emcmanus
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1247
b4c26443dee5 6754988: Update copyright year
xdono
parents: 1004
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
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;
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
    36
import javax.management.ClientContext;
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    37
import javax.management.MBeanInfo;  // for javadoc
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.MBeanNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanRegistration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.NotificationBroadcasterSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.ObjectName;
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    44
import javax.management.event.EventClientDelegate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>Superclass of every connector server.  A connector server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * attached to an MBean server.  It listens for client connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * requests and creates a connection for each one.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>A connector server is associated with an MBean server either by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * registering it in that MBean server, or by passing the MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * to its constructor.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>A connector server is inactive when created.  It only starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * listening for client connections when the {@link #start() start}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * method is called.  A connector server stops listening for client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * connections when the {@link #stop() stop} method is called or when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * the connector server is unregistered from its MBean server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>Stopping a connector server does not unregister it from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * MBean server.  A connector server once stopped cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * restarted.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>Each time a client connection is made or broken, a notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * of class {@link JMXConnectionNotification} is emitted.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public abstract class JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        implements JMXConnectorServerMBean, MBeanRegistration, JMXAddressable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <p>Name of the attribute that specifies the authenticator for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * connector server.  The value associated with this attribute, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * any, must be an object that implements the interface {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * JMXAuthenticator}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static final String AUTHENTICATOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        "jmx.remote.authenticator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    83
     /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    84
      * <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
    85
      * server can delegate notification handling to the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    86
      * {@linkplain javax.management.event Event Service}.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    87
      * The value associated with
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    88
      * 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
    89
      * ignoring case, to {@code "true"} or {@code "false"}.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    90
      *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    91
      * <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
    92
      * standard {@linkplain javax.management.remote.rmi.RMIConnectorServer
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    93
      * RMI Connector Server} does.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    94
      *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    95
      * <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
    96
      * 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
    97
      * 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
    98
      * supports it.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    99
      *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   100
      * @since 1.7
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   101
      */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   102
     public static final String DELEGATE_TO_EVENT_SERVICE =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   103
         "jmx.remote.delegate.event.service";
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
     /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   106
      * <p>Name of the attribute that specifies whether this connector
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   107
      * server allows clients to communicate a context with each request.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   108
      * The value associated with this attribute, if any, must be a string
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   109
      * that is equal to {@code "true"} or {@code "false"}, ignoring case.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   110
      * If it is {@code "true"}, then the connector server will simulate
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   111
      * a namespace {@code jmx.context//}, as described in
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   112
      * {@link ClientContext#newContextForwarder}.  This namespace is needed
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   113
      * for {@link ClientContext#withContext ClientContext.withContext} to
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   114
      * function correctly.</p>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   115
      *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   116
      * <p>Not all connector servers will understand this attribute, but the
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   117
      * standard {@linkplain javax.management.remote.rmi.RMIConnectorServer
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   118
      * RMI Connector Server} does.  For a connector server that understands
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   119
      * this attribute, the default value is {@code "true"}.</p>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   120
      *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   121
      * @since 1.7
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   122
      */
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   123
     public static final String CONTEXT_FORWARDER =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   124
         "jmx.remote.context.forwarder";
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   125
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   126
     /**
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   127
      * <p>Name of the attribute that specifies whether this connector server
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   128
      * localizes the descriptions in the {@link MBeanInfo} object returned by
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   129
      * {@link MBeanServer#getMBeanInfo MBeanServer.getMBeanInfo}, based on the
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   130
      * locale communicated by the client.</p>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   131
      *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   132
      * <p>The value associated with this attribute, if any, must be a string
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   133
      * that is equal to {@code "true"} or {@code "false"}, ignoring case.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   134
      * If it is {@code "true"}, then the connector server will localize
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   135
      * {@code MBeanInfo} descriptions as specified in {@link
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   136
      * ClientContext#newLocalizeMBeanInfoForwarder}.</p>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   137
      *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   138
      * <p>Not all connector servers will understand this attribute, but the
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   139
      * standard {@linkplain javax.management.remote.rmi.RMIConnectorServer
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   140
      * RMI Connector Server} does.  For a connector server that understands
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   141
      * this attribute, the default value is {@code "false"}.</p>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   142
      *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   143
      * <p>Because localization requires the client to be able to communicate
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   144
      * its locale, it does not make sense to specify this attribute as
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   145
      * {@code "true"} if {@link #CONTEXT_FORWARDER} is not also {@code "true"}.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   146
      * For a connector server that understands these attributes, specifying
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   147
      * this inconsistent combination will result in an {@link
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   148
      * IllegalArgumentException}.</p>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   149
      *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   150
      * @since 1.7
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   151
      */
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   152
     public static final String LOCALIZE_MBEAN_INFO_FORWARDER =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   153
        "jmx.remote.localize.mbean.info";
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   154
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   155
     /**
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   156
      * <p>Name of the attribute that specifies whether this connector
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   157
      * server simulates the existence of the {@link EventClientDelegate}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   158
      * 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
   159
      * 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
   160
      * 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
   161
      * will simulate an EventClientDelegate MBean, as described in {@link
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   162
      * EventClientDelegate#newForwarder}. This MBean is needed for {@link
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   163
      * javax.management.event.EventClient EventClient} to function correctly.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   164
      *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   165
      * <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
   166
      * standard {@linkplain javax.management.remote.rmi.RMIConnectorServer
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   167
      * 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
   168
      * this attribute, the default value is {@code "true"}.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   169
      *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   170
      * @since 1.7
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   171
      */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   172
     public static final String EVENT_CLIENT_DELEGATE_FORWARDER =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   173
         "jmx.remote.event.client.delegate.forwarder";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   174
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <p>Constructs a connector server that will be registered as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * MBean in the MBean server it is attached to.  This constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * is typically called by one of the <code>createMBean</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * methods when creating, within an MBean server, a connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * server that makes it available remotely.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public JMXConnectorServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <p>Constructs a connector server that is attached to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * 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
   189
     * 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
   190
     * in any MBean server.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param mbeanServer the MBean server that this connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * is attached to.  Null if this connector server will be attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * to an MBean server by being registered in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public JMXConnectorServer(MBeanServer mbeanServer) {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   197
        insertUserMBeanServer(mbeanServer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <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
   202
     * 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
   203
     * {@link MBeanServerForwarder}s, if any.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return the MBean server that this connector server is attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * 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
   207
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   208
     * @see #setMBeanServerForwarder
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   209
     * @see #getSystemMBeanServerForwarder
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public synchronized MBeanServer getMBeanServer() {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   212
        return userMBeanServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   213
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   214
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   215
    public synchronized void setMBeanServerForwarder(MBeanServerForwarder mbsf) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   216
        if (mbsf == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   217
            throw new IllegalArgumentException("Invalid null argument: mbsf");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   218
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   219
        if (userMBeanServer != null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   220
            mbsf.setMBeanServer(userMBeanServer);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   221
        insertUserMBeanServer(mbsf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
1004
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
     * <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
   226
     * 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
   227
     * 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
   228
     * {@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
   229
     * removed.</p>
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   230
     *
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   231
     * @param mbsf the forwarder to remove
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   232
     *
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   233
     * @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
   234
     * in the chain.
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   235
     * @throws IllegalArgumentException if {@code mbsf} is null or is the
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   236
     * {@linkplain #getSystemMBeanServerForwarder() system forwarder}.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   237
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   238
     * @since 1.7
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   239
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   240
    public synchronized void removeMBeanServerForwarder(MBeanServerForwarder mbsf) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (mbsf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new IllegalArgumentException("Invalid null argument: mbsf");
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   243
        if (systemMBeanServerForwarder.equals(mbsf))
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   244
            throw new IllegalArgumentException("Cannot remove system forwarder");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   246
        MBeanServerForwarder prev = systemMBeanServerForwarder;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   247
        MBeanServer curr;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   248
        while (true) {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   249
            curr = prev.getMBeanServer();
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   250
            if (mbsf.equals(curr))
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   251
                break;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   252
            if (curr instanceof MBeanServerForwarder)
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   253
                prev = (MBeanServerForwarder) curr;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   254
            else
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   255
                throw new NoSuchElementException("MBeanServerForwarder not in chain");
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   256
        }
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   257
        MBeanServer next = mbsf.getMBeanServer();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   258
        prev.setMBeanServer(next);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   259
        if (userMBeanServer == mbsf)
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   260
            userMBeanServer = next;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   261
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   262
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   263
    /*
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   264
     * 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
   265
     * 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
   266
     * the systemMBeanServer and userMBeanServer field declarations.
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
    private void insertUserMBeanServer(MBeanServer mbs) {
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   269
        MBeanServerForwarder lastSystemMBSF = systemMBeanServerForwarder;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   270
        while (true) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   271
            MBeanServer mbsi = lastSystemMBSF.getMBeanServer();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   272
            if (mbsi == userMBeanServer)
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   273
                break;
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   274
            lastSystemMBSF = (MBeanServerForwarder) mbsi;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   275
        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   276
        userMBeanServer = mbs;
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   277
        lastSystemMBSF.setMBeanServer(mbs);
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   278
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   279
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
     * <p>Returns the first item in the chain of system and then user
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   282
     * forwarders.  There is a chain of {@link MBeanServerForwarder}s between
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   283
     * a {@code JMXConnectorServer} and its {@code MBeanServer}.  This chain
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   284
     * consists of two sub-chains: first the <em>system chain</em> and then
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   285
     * the <em>user chain</em>.  Incoming requests are given to the first
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   286
     * forwarder in the system chain.  Each forwarder can handle a request
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   287
     * itself, or more usually forward it to the next forwarder, perhaps with
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   288
     * some extra behavior such as logging or security checking before or after
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   289
     * the forwarding.  The last forwarder in the system chain is followed by
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   290
     * the first forwarder in the user chain.</p>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   291
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   292
     * <p>The object returned by this method is the first forwarder in the
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   293
     * system chain.  For a given {@code JMXConnectorServer}, this method
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   294
     * always returns the same object, which simply forwards every request
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   295
     * to the next object in the chain.</p>
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   296
     *
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   297
     * <p>Not all connector servers support a system chain of forwarders,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   298
     * although the standard {@linkplain
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   299
     * javax.management.remote.rmi.RMIConnectorServer RMI connector
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   300
     * server} does.  For those that do not, this method will throw {@code
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   301
     * UnsupportedOperationException}.  All
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   302
     * connector servers do support a user chain of forwarders.</p>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   303
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   304
     * <p>The <em>system chain</em> is usually defined by a
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   305
     * connector server based on the environment Map; see {@link
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   306
     * JMXConnectorServerFactory#newJMXConnectorServer
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   307
     * JMXConnectorServerFactory.newJMXConnectorServer}.  Allowing
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   308
     * the connector server to define its forwarders in this way
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   309
     * ensures that they are in the correct order - some forwarders
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   310
     * need to be inserted before others for correct behavior.  It is
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   311
     * possible to modify the system chain, for example using {@code
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   312
     * connectorServer.getSystemMBeanServerForwarder().setMBeanServer(mbsf)} or
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   313
     * {@link #removeMBeanServerForwarder removeMBeanServerForwarder}, but in
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   314
     * that case the system chain is no longer guaranteed to be correct.</p>
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   315
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   316
     * <p>The <em>user chain</em> is defined by calling {@link
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   317
     * #setMBeanServerForwarder setMBeanServerForwarder} to insert forwarders
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   318
     * at the head of the user chain.</p>
1004
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>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
   321
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   322
     * <pre>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   323
     * JMXConnectorServer cs;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   324
     * System.out.println("system chain:");
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   325
     * MBeanServer mbs = cs.getSystemMBeanServerForwarder();
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   326
     * while (true) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   327
     *     if (mbs == cs.getMBeanServer())
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   328
     *         System.out.println("user chain:");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   329
     *     if (!(mbs instanceof MBeanServerForwarder))
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   330
     *         break;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   331
     *     MBeanServerForwarder mbsf = (MBeanServerForwarder) mbs;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   332
     *     System.out.println("--forwarder: " + mbsf);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   333
     *     mbs = mbsf.getMBeanServer();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   334
     * }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   335
     * System.out.println("--MBean Server");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   336
     * </pre>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   337
     *
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   338
     * <h4>Note for connector server implementors</h4>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   339
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   340
     * <p>Existing connector server implementations can be updated to support
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   341
     * a system chain of forwarders as follows:</p>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   342
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   343
     * <ul>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   344
     * <li><p>Override the {@link #supportsSystemMBeanServerForwarder()}
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   345
     * method so that it returns true.</p>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   346
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   347
     * <li><p>Call {@link #installStandardForwarders} from the constructor of
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   348
     * the connector server.</p>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   349
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   350
     * <li><p>Direct incoming requests to the result of {@link
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   351
     * #getSystemMBeanServerForwarder()} instead of the result of {@link
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   352
     * #getMBeanServer()}.</p>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   353
     * </ul>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   354
     *
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   355
     * @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
   356
     *
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   357
     * @throws UnsupportedOperationException if {@link
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   358
     * #supportsSystemMBeanServerForwarder} returns false.
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   359
     *
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   360
     * @see #supportsSystemMBeanServerForwarder
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   361
     * @see #setMBeanServerForwarder
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   362
     *
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   363
     * @since 1.7
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   364
     */
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   365
    public MBeanServerForwarder getSystemMBeanServerForwarder() {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   366
        if (!supportsSystemMBeanServerForwarder()) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   367
            throw new UnsupportedOperationException(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   368
                    "System MBeanServerForwarder not supported by this " +
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   369
                    "connector server");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   370
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   371
        return systemMBeanServerForwarder;
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   372
    }
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
    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   375
     * <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
   376
     * {@link MBeanServerForwarder}s.  The default implementation of this
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   377
     * 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
   378
     * chain must override this method to return true.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   379
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   380
     * @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
   381
     * forwarders.
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   382
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   383
     * @since 1.7
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   384
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   385
    public boolean supportsSystemMBeanServerForwarder() {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   386
        return false;
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
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   389
    /**
1708
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   390
     * Closes a client connection. If the connection is successfully closed,
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   391
     * the method {@link #connectionClosed} is called to notify interested parties.
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   392
     * <P>Not all connector servers support this method. For those that do, it
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   393
     * should be possible to cause a new client connection to fail before it
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   394
     * can be used, by calling this method from within a
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   395
     * {@link javax.management.NotificationListener}
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   396
     * when it receives a {@link JMXConnectionNotification#OPENED} notification.
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   397
     * This allows the owner of a connector server to deny certain connections,
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   398
     * typically based on the information in the connection id.
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   399
     * <P>The implementation of this method in {@code JMXConnectorServer} throws
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   400
     * {@code UnsupportedOperationException}. Subclasses can override this
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   401
     * method to support closing a specified client connection.
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   402
     *
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   403
     * @param connectionId the id of the client connection to be closed.
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   404
     * @throws IllegalStateException if the server is not started or is closed.
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   405
     * @throws IllegalArgumentException if {@code connectionId} is null or is
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   406
     * not the id of any open connection.
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   407
     * @throws java.io.IOException if an I/O error appears when closing the
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   408
     * connection.
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   409
     *
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   410
     * @since 1.7
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   411
     */
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   412
    public void closeConnection(String connectionId)
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   413
            throws IOException {
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   414
        throw new UnsupportedOperationException();
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   415
    }
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   416
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   417
    /**
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   418
     * <p>Install {@link MBeanServerForwarder}s in the system chain
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   419
     * 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
   420
     * server that {@linkplain #supportsSystemMBeanServerForwarder supports}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   421
     * 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
   422
     * 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
   423
     * In order:</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   424
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   425
     * <ul>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   426
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   427
     * <li>If {@link #EVENT_CLIENT_DELEGATE_FORWARDER} is absent, or is
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   428
     * present with the value {@code "true"}, then a forwarder
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   429
     * equivalent to {@link EventClientDelegate#newForwarder
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   430
     * EventClientDelegate.newForwarder}{@code (sysMBSF.getMBeanServer(),
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   431
     * sysMBSF)} is inserted at the start of the system chain,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   432
     * where {@code sysMBSF} is the object returned by {@link
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   433
     * #getSystemMBeanServerForwarder()}. </li>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   434
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   435
     * <li>If {@link #LOCALIZE_MBEAN_INFO_FORWARDER} is present with the
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   436
     * value {@code "true"}, then a forwarder equivalent to
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   437
     * {@link ClientContext#newLocalizeMBeanInfoForwarder
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   438
     * ClientContext.newLocalizeMBeanInfoForwarder}{@code
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   439
     * (sysMBSF.getMBeanServer())} is inserted at the start of the system
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   440
     * chain.</li>
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   441
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   442
     * <li>If {@link #CONTEXT_FORWARDER} is absent, or is present with
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   443
     * the value {@code "true"}, then a forwarder equivalent to
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   444
     * {@link ClientContext#newContextForwarder
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   445
     * ClientContext.newContextForwarder}{@code (sysMSBF.getMBeanServer(),
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   446
     * sysMBSF)} is inserted at the tart of the system chain.</li>
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   447
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   448
     * </ul>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   449
     *
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   450
     * <p>For {@code EVENT_CLIENT_DELEGATE_FORWARDER} and {@code
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   451
     * CONTEXT_FORWARDER}, if the attribute is absent from the {@code
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   452
     * Map} and a system property of the same name is defined, then
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   453
     * the value of the system property is used as if it were in the
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   454
     * {@code Map}.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   455
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   456
     * <p>Since each forwarder is inserted at the start of the chain,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   457
     * the final order of the forwarders is the <b>reverse</b> of the order
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   458
     * above.  This is important, because the {@code
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   459
     * LOCALIZE_MBEAN_INFO_FORWARDER} can only work if the {@code
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   460
     * CONTEXT_FORWARDER} has already installed the remote client's locale
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   461
     * in the {@linkplain ClientContext#getContext context} of the current
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   462
     * thread.</p>
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   463
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   464
     * <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
   465
     * by this method.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   466
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   467
     * @throws UnsupportedOperationException if {@link
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   468
     * #supportsSystemMBeanServerForwarder} is false.
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   469
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   470
     * @throws IllegalArgumentException if the relevant attributes in {@code env} are
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   471
     * inconsistent, for example if {@link #LOCALIZE_MBEAN_INFO_FORWARDER} is
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   472
     * {@code "true"} but {@link #CONTEXT_FORWARDER} is {@code "false"}; or
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   473
     * if one of the attributes has an illegal value.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   474
     *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   475
     * @since 1.7
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   476
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   477
    protected void installStandardForwarders(Map<String, ?> env) {
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   478
        MBeanServerForwarder sysMBSF = getSystemMBeanServerForwarder();
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   479
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   480
        // Remember that forwarders must be added in reverse order!
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   481
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   482
        boolean ecd = EnvHelp.computeBooleanFromString(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   483
                env, EVENT_CLIENT_DELEGATE_FORWARDER, false, true);
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   484
        boolean localize = EnvHelp.computeBooleanFromString(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   485
                env, LOCALIZE_MBEAN_INFO_FORWARDER, false, false);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   486
        boolean context = EnvHelp.computeBooleanFromString(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   487
                env, CONTEXT_FORWARDER, false, true);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   488
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   489
        if (localize && !context) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   490
            throw new IllegalArgumentException(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   491
                    "Inconsistent environment parameters: " +
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   492
                    LOCALIZE_MBEAN_INFO_FORWARDER + "=\"true\" requires " +
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   493
                    CONTEXT_FORWARDER + "=\"true\"");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   494
        }
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   495
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   496
        if (ecd) {
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   497
            MBeanServerForwarder mbsf = EventClientDelegate.newForwarder(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   498
                    sysMBSF.getMBeanServer(), sysMBSF);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   499
            sysMBSF.setMBeanServer(mbsf);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   500
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   501
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   502
        if (localize) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   503
            MBeanServerForwarder mbsf = ClientContext.newLocalizeMBeanInfoForwarder(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   504
                    sysMBSF.getMBeanServer());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   505
            sysMBSF.setMBeanServer(mbsf);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   506
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   507
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   508
        if (context) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   509
            MBeanServerForwarder mbsf = ClientContext.newContextForwarder(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   510
                    sysMBSF.getMBeanServer(), sysMBSF);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   511
            sysMBSF.setMBeanServer(mbsf);
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   512
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public String[] getConnectionIds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        synchronized (connectionIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            return connectionIds.toArray(new String[connectionIds.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * <p>Returns a client stub for this connector server.  A client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * stub is a serializable object whose {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * JMXConnector#connect(Map) connect} method can be used to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * one new connection to this connector server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * <p>A given connector need not support the generation of client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * stubs.  However, the connectors specified by the JMX Remote API do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * (JMXMP Connector and RMI Connector).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <p>The default implementation of this method uses {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * #getAddress} and {@link JMXConnectorFactory} to generate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * stub, with code equivalent to the following:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * JMXServiceURL addr = {@link #getAddress() getAddress()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * return {@link JMXConnectorFactory#newJMXConnector(JMXServiceURL, Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *          JMXConnectorFactory.newJMXConnector(addr, env)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * <p>A connector server for which this is inappropriate must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * override this method so that it either implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * appropriate logic or throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * UnsupportedOperationException}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @param env client connection parameters of the same sort that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * could be provided to {@link JMXConnector#connect(Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * JMXConnector.connect(Map)}.  Can be null, which is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * to an empty map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @return a client stub that can be used to make a new connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * to this connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @exception UnsupportedOperationException if this connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * server does not support the generation of client stubs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @exception IllegalStateException if the JMXConnectorServer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * not started (see {@link JMXConnectorServerMBean#isActive()}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @exception IOException if a communications problem means that a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * stub cannot be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    public JMXConnector toJMXConnector(Map<String,?> env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if (!isActive()) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            IllegalStateException("Connector is not active");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        JMXServiceURL addr = getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        return JMXConnectorFactory.newJMXConnector(addr, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <p>Returns an array indicating the notifications that this MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * sends. The implementation in <code>JMXConnectorServer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * returns an array with one element, indicating that it can emit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * notifications of class {@link JMXConnectionNotification} with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * the types defined in that class.  A subclass that can emit other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * notifications should return an array that contains this element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * plus descriptions of the other notifications.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @return the array of possible notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   583
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    public MBeanNotificationInfo[] getNotificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        final String[] types = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            JMXConnectionNotification.OPENED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            JMXConnectionNotification.CLOSED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            JMXConnectionNotification.FAILED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        final String className = JMXConnectionNotification.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        final String description =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            "A client connection has been opened or closed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return new MBeanNotificationInfo[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            new MBeanNotificationInfo(types, className, description),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * <p>Called by a subclass when a new client connection is opened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * Adds <code>connectionId</code> to the list returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * #getConnectionIds()}, then emits a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * JMXConnectionNotification} with type {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * JMXConnectionNotification#OPENED}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @param connectionId the ID of the new connection.  This must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * different from the ID of any connection previously opened by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * this connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @param message the message for the emitted {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Notification#getMessage()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @param userData the <code>userData</code> for the emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * {@link JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * Notification#getUserData()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @exception NullPointerException if <code>connectionId</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    protected void connectionOpened(String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                    String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                    Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (connectionId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            throw new NullPointerException("Illegal null argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        synchronized (connectionIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            connectionIds.add(connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        sendNotification(JMXConnectionNotification.OPENED, connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                         message, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <p>Called by a subclass when a client connection is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * normally.  Removes <code>connectionId</code> from the list returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * by {@link #getConnectionIds()}, then emits a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * JMXConnectionNotification} with type {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * JMXConnectionNotification#CLOSED}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param connectionId the ID of the closed connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @param message the message for the emitted {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * Notification#getMessage()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @param userData the <code>userData</code> for the emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * {@link JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * Notification#getUserData()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @exception NullPointerException if <code>connectionId</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    protected void connectionClosed(String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                    String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                    Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (connectionId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            throw new NullPointerException("Illegal null argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        synchronized (connectionIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            connectionIds.remove(connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        sendNotification(JMXConnectionNotification.CLOSED, connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                         message, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <p>Called by a subclass when a client connection fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * Removes <code>connectionId</code> from the list returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * {@link #getConnectionIds()}, then emits a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * JMXConnectionNotification} with type {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * JMXConnectionNotification#FAILED}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @param connectionId the ID of the failed connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @param message the message for the emitted {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Notification#getMessage()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @param userData the <code>userData</code> for the emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * {@link JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * Notification#getUserData()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @exception NullPointerException if <code>connectionId</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    protected void connectionFailed(String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                                    String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                                    Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        if (connectionId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            throw new NullPointerException("Illegal null argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        synchronized (connectionIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            connectionIds.remove(connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        sendNotification(JMXConnectionNotification.FAILED, connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                         message, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    private void sendNotification(String type, String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                                  String message, Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        Notification notif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            new JMXConnectionNotification(type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                                          getNotificationSource(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                                          connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                                          nextSequenceNumber(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                                          message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                                          userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        sendNotification(notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    private synchronized Object getNotificationSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if (myName != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            return myName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    private static long nextSequenceNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        synchronized (sequenceNumberLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            return sequenceNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    // implements MBeanRegistration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * <p>Called by an MBean server when this connector server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * registered in that MBean server.  This connector server becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * attached to the MBean server and its {@link #getMBeanServer()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * method will return <code>mbs</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * <p>If this connector server is already attached to an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * server, this method has no effect.  The MBean server it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * attached to is not necessarily the one it is being registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * in.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @param mbs the MBean server in which this connection server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * being registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @param name The object name of the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @return The name under which the MBean is to be registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * @exception NullPointerException if <code>mbs</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * <code>name</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    public synchronized ObjectName preRegister(MBeanServer mbs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                                               ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        if (mbs == null || name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            throw new NullPointerException("Null MBeanServer or ObjectName");
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   756
        if (userMBeanServer == null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   757
            insertUserMBeanServer(mbs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            myName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    public void postRegister(Boolean registrationDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * <p>Called by an MBean server when this connector server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * unregistered from that MBean server.  If this connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * was attached to that MBean server by being registered in it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * and if the connector server is still active,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * then unregistering it will call the {@link #stop stop} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * If the <code>stop</code> method throws an exception, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * unregistration attempt will fail.  It is recommended to call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * the <code>stop</code> method explicitly before unregistering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * the MBean.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @exception IOException if thrown by the {@link #stop stop} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    public synchronized void preDeregister() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if (myName != null && isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            myName = null; // just in case stop is buggy and doesn't stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    public void postDeregister() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        myName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   791
    /*
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   792
     * Fields describing the chains of forwarders (MBeanServerForwarders).
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   793
     * 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
   794
     *
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   795
     *                                        userMBeanServer
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   796
     *                                        |
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   797
     *                                        v
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   798
     * systemMBeanServerForwarder -> mbsf2 -> mbsf3 -> mbsf4 -> mbsf5 -> mbs
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   799
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   800
     * Here, each mbsfi is an MBeanServerForwarder, and the arrows
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   801
     * illustrate its getMBeanServer() method.  The last MBeanServerForwarder
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   802
     * 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
   803
     * here mbs.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   804
     *
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   805
     * The system chain is never empty because it always has at least
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   806
     * systemMBeanServerForwarder.  Initially, the user chain can be empty if
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   807
     * this JMXConnectorServer was constructed without an MBeanServer.  In
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   808
     * this case, userMBS will be null.  If there is initially an MBeanServer,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   809
     * userMBS will point to it.
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   810
     *
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   811
     * Whenever userMBS is changed, the system chain must be updated.  Before
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   812
     * the update, the last forwarder in the system chain points to the old
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   813
     * value of userMBS (possibly null).  It must be updated to point to
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   814
     * the new value. The invariant is that starting from systemMBSF and
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   815
     * repeatedly calling MBSF.getMBeanServer() you will end up at userMBS.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   816
     * The implication is that you will not see any MBeanServer object on the
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   817
     * way that is not also an MBeanServerForwarder.
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   818
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   819
     * The method insertUserMBeanServer contains the logic to change userMBS
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   820
     * and adjust the system chain appropriately.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   821
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   822
     * 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
   823
     * MBeanServer, then userMBS becomes that MBeanServer, and the system
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   824
     * chain must be updated as just described.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   825
     *
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   826
     * When systemMBSF is updated, there is no effect on userMBS. The system
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   827
     * 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
   828
     * (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
   829
     * 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
   830
     * NullPointerException. Usually a connector server will refuse to start()
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   831
     * if it is not attached to an MBS, so this situation should not arise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     */
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   833
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   834
    private MBeanServer userMBeanServer;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   835
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   836
    private final MBeanServerForwarder systemMBeanServerForwarder =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   837
            new IdentityMBeanServerForwarder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * The name used to registered this server in an MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * It is null if the this server is not registered or has been unregistered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    private ObjectName myName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    private List<String> connectionIds = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    private static final int[] sequenceNumberLock = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    private static long sequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
}