jdk/src/share/classes/javax/management/remote/JMXConnectorServer.java
author emcmanus
Wed, 21 Oct 2009 17:33:18 +0200
changeset 4156 acaa49a2768a
parent 1708 4e1939e6e6b5
child 5506 202f599c92aa
permissions -rw-r--r--
6851617: Remove JSR 255 (JMX API 2.0) from JDK 7 Summary: See http://weblogs.java.net/blog/2009/06/16/jsr-255-jmx-api-20-postponed Reviewed-by: dfuchs
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.MBeanNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.MBeanRegistration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.NotificationBroadcasterSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>Superclass of every connector server.  A connector server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * attached to an MBean server.  It listens for client connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * requests and creates a connection for each one.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>A connector server is associated with an MBean server either by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * registering it in that MBean server, or by passing the MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * to its constructor.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>A connector server is inactive when created.  It only starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * listening for client connections when the {@link #start() start}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * method is called.  A connector server stops listening for client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * connections when the {@link #stop() stop} method is called or when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * the connector server is unregistered from its MBean server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>Stopping a connector server does not unregister it from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * MBean server.  A connector server once stopped cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * restarted.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>Each time a client connection is made or broken, a notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * of class {@link JMXConnectionNotification} is emitted.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public abstract class JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        implements JMXConnectorServerMBean, MBeanRegistration, JMXAddressable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <p>Name of the attribute that specifies the authenticator for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * connector server.  The value associated with this attribute, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * any, must be an object that implements the interface {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * JMXAuthenticator}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static final String AUTHENTICATOR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        "jmx.remote.authenticator";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <p>Constructs a connector server that will be registered as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * MBean in the MBean server it is attached to.  This constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * is typically called by one of the <code>createMBean</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * methods when creating, within an MBean server, a connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * server that makes it available remotely.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public JMXConnectorServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * <p>Constructs a connector server that is attached to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * 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
    92
     * 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
    93
     * in any MBean server.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param mbeanServer the MBean server that this connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * is attached to.  Null if this connector server will be attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * to an MBean server by being registered in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public JMXConnectorServer(MBeanServer mbeanServer) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   100
        this.mbeanServer = mbeanServer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p>Returns the MBean server that this connector server is
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   105
     * attached to.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return the MBean server that this connector server is attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * to, or null if it is not yet attached to an MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public synchronized MBeanServer getMBeanServer() {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   111
        return mbeanServer;
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   112
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   113
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   114
    public synchronized void setMBeanServerForwarder(MBeanServerForwarder mbsf)
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   115
    {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   116
        if (mbsf == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   117
            throw new IllegalArgumentException("Invalid null argument: mbsf");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   118
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   119
        if (mbeanServer !=  null) mbsf.setMBeanServer(mbeanServer);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   120
        mbeanServer = mbsf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public String[] getConnectionIds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        synchronized (connectionIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return connectionIds.toArray(new String[connectionIds.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p>Returns a client stub for this connector server.  A client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * stub is a serializable object whose {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * JMXConnector#connect(Map) connect} method can be used to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * one new connection to this connector server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <p>A given connector need not support the generation of client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * stubs.  However, the connectors specified by the JMX Remote API do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * (JMXMP Connector and RMI Connector).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <p>The default implementation of this method uses {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * #getAddress} and {@link JMXConnectorFactory} to generate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * stub, with code equivalent to the following:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * JMXServiceURL addr = {@link #getAddress() getAddress()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * return {@link JMXConnectorFactory#newJMXConnector(JMXServiceURL, Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *          JMXConnectorFactory.newJMXConnector(addr, env)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p>A connector server for which this is inappropriate must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * override this method so that it either implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * appropriate logic or throws {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * UnsupportedOperationException}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param env client connection parameters of the same sort that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * could be provided to {@link JMXConnector#connect(Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * JMXConnector.connect(Map)}.  Can be null, which is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * to an empty map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return a client stub that can be used to make a new connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * to this connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @exception UnsupportedOperationException if this connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * server does not support the generation of client stubs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @exception IllegalStateException if the JMXConnectorServer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * not started (see {@link JMXConnectorServerMBean#isActive()}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @exception IOException if a communications problem means that a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * stub cannot be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public JMXConnector toJMXConnector(Map<String,?> env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (!isActive()) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            IllegalStateException("Connector is not active");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        JMXServiceURL addr = getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return JMXConnectorFactory.newJMXConnector(addr, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <p>Returns an array indicating the notifications that this MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * sends. The implementation in <code>JMXConnectorServer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * returns an array with one element, indicating that it can emit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * notifications of class {@link JMXConnectionNotification} with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * the types defined in that class.  A subclass that can emit other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * notifications should return an array that contains this element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * plus descriptions of the other notifications.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return the array of possible notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1247
diff changeset
   191
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public MBeanNotificationInfo[] getNotificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        final String[] types = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            JMXConnectionNotification.OPENED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            JMXConnectionNotification.CLOSED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            JMXConnectionNotification.FAILED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        final String className = JMXConnectionNotification.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        final String description =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            "A client connection has been opened or closed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return new MBeanNotificationInfo[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            new MBeanNotificationInfo(types, className, description),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p>Called by a subclass when a new client connection is opened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Adds <code>connectionId</code> to the list returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * #getConnectionIds()}, then emits a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * JMXConnectionNotification} with type {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * JMXConnectionNotification#OPENED}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param connectionId the ID of the new connection.  This must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * different from the ID of any connection previously opened by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * this connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param message the message for the emitted {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Notification#getMessage()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param userData the <code>userData</code> for the emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * {@link JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Notification#getUserData()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @exception NullPointerException if <code>connectionId</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    protected void connectionOpened(String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                    String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                    Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (connectionId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            throw new NullPointerException("Illegal null argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        synchronized (connectionIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            connectionIds.add(connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        sendNotification(JMXConnectionNotification.OPENED, connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                         message, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <p>Called by a subclass when a client connection is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * normally.  Removes <code>connectionId</code> from the list returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * by {@link #getConnectionIds()}, then emits a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * JMXConnectionNotification} with type {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * JMXConnectionNotification#CLOSED}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param connectionId the ID of the closed connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param message the message for the emitted {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Notification#getMessage()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param userData the <code>userData</code> for the emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * {@link JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Notification#getUserData()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @exception NullPointerException if <code>connectionId</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    protected void connectionClosed(String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                    String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                    Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (connectionId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            throw new NullPointerException("Illegal null argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        synchronized (connectionIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            connectionIds.remove(connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        sendNotification(JMXConnectionNotification.CLOSED, connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                         message, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * <p>Called by a subclass when a client connection fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Removes <code>connectionId</code> from the list returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * {@link #getConnectionIds()}, then emits a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * JMXConnectionNotification} with type {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * JMXConnectionNotification#FAILED}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param connectionId the ID of the failed connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param message the message for the emitted {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Notification#getMessage()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param userData the <code>userData</code> for the emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * {@link JMXConnectionNotification}.  Can be null.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Notification#getUserData()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @exception NullPointerException if <code>connectionId</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    protected void connectionFailed(String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                    String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                    Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (connectionId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            throw new NullPointerException("Illegal null argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        synchronized (connectionIds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            connectionIds.remove(connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        sendNotification(JMXConnectionNotification.FAILED, connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                         message, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private void sendNotification(String type, String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                  String message, Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        Notification notif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            new JMXConnectionNotification(type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                          getNotificationSource(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                          connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                          nextSequenceNumber(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                          message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                          userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        sendNotification(notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private synchronized Object getNotificationSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (myName != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return myName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private static long nextSequenceNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        synchronized (sequenceNumberLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return sequenceNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    // implements MBeanRegistration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <p>Called by an MBean server when this connector server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * registered in that MBean server.  This connector server becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * attached to the MBean server and its {@link #getMBeanServer()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * method will return <code>mbs</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <p>If this connector server is already attached to an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * server, this method has no effect.  The MBean server it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * attached to is not necessarily the one it is being registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * in.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param mbs the MBean server in which this connection server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * being registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param name The object name of the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @return The name under which the MBean is to be registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @exception NullPointerException if <code>mbs</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <code>name</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public synchronized ObjectName preRegister(MBeanServer mbs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                               ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (mbs == null || name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            throw new NullPointerException("Null MBeanServer or ObjectName");
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   364
        if (mbeanServer == null) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   365
            mbeanServer = mbs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            myName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public void postRegister(Boolean registrationDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <p>Called by an MBean server when this connector server is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * unregistered from that MBean server.  If this connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * was attached to that MBean server by being registered in it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * and if the connector server is still active,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * then unregistering it will call the {@link #stop stop} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * If the <code>stop</code> method throws an exception, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * unregistration attempt will fail.  It is recommended to call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * the <code>stop</code> method explicitly before unregistering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * the MBean.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @exception IOException if thrown by the {@link #stop stop} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public synchronized void preDeregister() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (myName != null && isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            myName = null; // just in case stop is buggy and doesn't stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public void postDeregister() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        myName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   399
    /**
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   400
     * The MBeanServer used by this server to execute a client request.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   402
    private MBeanServer mbeanServer = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * The name used to registered this server in an MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * It is null if the this server is not registered or has been unregistered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private ObjectName myName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   410
    private final List<String> connectionIds = new ArrayList<String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    private static final int[] sequenceNumberLock = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    private static long sequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
}