jdk/src/share/classes/com/sun/jmx/snmp/daemon/CommunicatorServer.java
author prr
Tue, 15 Jul 2014 11:22:14 -0700
changeset 25522 10d789df41bb
parent 23010 6dadb192ad81
permissions -rw-r--r--
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes Reviewed-by: psandoz, prr Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21291
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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 com.sun.jmx.snmp.daemon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
// java import
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
// jmx import
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.MBeanRegistration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.NotificationFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.NotificationBroadcaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.NotificationBroadcasterSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.MBeanNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.AttributeChangeNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.ListenerNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import static com.sun.jmx.defaults.JmxProperties.SNMP_ADAPTOR_LOGGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
// JSR 160 import
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
// XXX Revisit:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
//   used to import com.sun.jmx.snmp.MBeanServerForwarder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
// Now using JSR 160 instead. => this is an additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
// dependency to JSR 160.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.management.remote.MBeanServerForwarder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Defines generic behavior for the server part of a connector or an adaptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Most connectors or adaptors extend <CODE>CommunicatorServer</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * and inherit this behavior. Connectors or adaptors that do not fit into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * this model do not extend <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * A <CODE>CommunicatorServer</CODE> is an active object, it listens for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * client requests  and processes them in its own thread. When necessary, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <CODE>CommunicatorServer</CODE> creates other threads to process multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * requests concurrently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * A <CODE>CommunicatorServer</CODE> object can be stopped by calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <CODE>stop</CODE> method. When it is stopped, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <CODE>CommunicatorServer</CODE> no longer listens to client requests and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * no longer holds any thread or communication resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * It can be started again by calling the <CODE>start</CODE> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * A <CODE>CommunicatorServer</CODE> has a <CODE>State</CODE> attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * which reflects its  activity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <TABLE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <TR><TH>CommunicatorServer</TH>      <TH>State</TH></TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <TR><TD><CODE>stopped</CODE></TD>    <TD><CODE>OFFLINE</CODE></TD></TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <TR><TD><CODE>starting</CODE></TD>    <TD><CODE>STARTING</CODE></TD></TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <TR><TD><CODE>running</CODE></TD>     <TD><CODE>ONLINE</CODE></TD></TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <TR><TD><CODE>stopping</CODE></TD>     <TD><CODE>STOPPING</CODE></TD></TR>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * </TABLE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * The <CODE>STARTING</CODE> state marks the transition
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * from <CODE>OFFLINE</CODE> to <CODE>ONLINE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * The <CODE>STOPPING</CODE> state marks the transition from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <CODE>ONLINE</CODE> to <CODE>OFFLINE</CODE>. This occurs when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <CODE>CommunicatorServer</CODE> is finishing or interrupting active
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * When a <CODE>CommunicatorServer</CODE> is unregistered from the MBeanServer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * it is stopped automatically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * When the value of the <CODE>State</CODE> attribute changes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <CODE>CommunicatorServer</CODE> sends a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <tt>{@link javax.management.AttributeChangeNotification}</tt> to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * registered listeners, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <p><b>This API is a Sun Microsystems internal API  and is subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * to change without notice.</b></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
public abstract class CommunicatorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    implements Runnable, MBeanRegistration, NotificationBroadcaster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
               CommunicatorServerMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // States of a CommunicatorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Represents an <CODE>ONLINE</CODE> state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static final int ONLINE = 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Represents an <CODE>OFFLINE</CODE> state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static final int OFFLINE = 1 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Represents a <CODE>STOPPING</CODE> state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public static final int STOPPING = 2 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Represents a <CODE>STARTING</CODE> state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public static final int STARTING = 3 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // Types of connectors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Indicates that it is an RMI connector type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    //public static final int RMI_TYPE = 1 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Indicates that it is an HTTP connector type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    //public static final int HTTP_TYPE = 2 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Indicates that it is an HTML connector type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    //public static final int HTML_TYPE = 3 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Indicates that it is an SNMP connector type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public static final int SNMP_TYPE = 4 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Indicates that it is an HTTPS connector type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    //public static final int HTTPS_TYPE = 5 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // Package variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * The state of the connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     transient volatile int state = OFFLINE ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * The object name of the connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    ObjectName objectName ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    MBeanServer topMBS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    MBeanServer bottomMBS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    transient String dbgTag = null ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * The maximum number of clients that the CommunicatorServer can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * process concurrently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    int maxActiveClientCount = 1 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    transient int servedClientCount = 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * The host name used by this CommunicatorServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    String host = null ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * The port number used by this CommunicatorServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    int port = -1 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // Private fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /* This object controls access to the "state" and "interrupted" variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
       If held at the same time as the lock on "this", the "this" lock must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
       be taken first.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private transient Object stateLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private transient Vector<ClientHandler>
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   225
            clientHandlerVector = new Vector<>() ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private transient Thread mainThread = null ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    private volatile boolean stopRequested = false ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    private boolean interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private transient Exception startException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    // Notifs count, broadcaster and info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private transient long notifCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private transient NotificationBroadcasterSupport notifBroadcaster =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        new NotificationBroadcasterSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private transient MBeanNotificationInfo[] notifInfos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Instantiates a <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param connectorType Indicates the connector type. Possible values are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * SNMP_TYPE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @exception <CODE>java.lang.IllegalArgumentException</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *            This connector type is not correct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public CommunicatorServer(int connectorType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        switch (connectorType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        case SNMP_TYPE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            //No op. int Type deciding debugging removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new IllegalArgumentException("Invalid connector Type") ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        dbgTag = makeDebugTag() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    protected Thread createMainThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return new Thread (this, makeThreadName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Starts this <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Has no effect if this <CODE>CommunicatorServer</CODE> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <CODE>ONLINE</CODE> or <CODE>STOPPING</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param timeout Time in ms to wait for the connector to start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *        If <code>timeout</code> is positive, wait for at most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *        the specified time. An infinite timeout can be specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *        by passing a <code>timeout</code> value equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *        <code>Long.MAX_VALUE</code>. In that case the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *        will wait until the connector starts or fails to start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *        If timeout is negative or zero, returns as soon as possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *        without waiting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @exception CommunicationException if the connectors fails to start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @exception InterruptedException if the thread is interrupted or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *            timeout expires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public void start(long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        throws CommunicationException, InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        boolean start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (state == STOPPING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                // Fix for bug 4352451:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                //     "java.net.BindException: Address in use".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                waitState(OFFLINE, 60000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            start = (state == OFFLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                changeState(STARTING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                stopRequested = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                startException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (!start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    "start","Connector is not OFFLINE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                "start","--> Start connector ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        mainThread = createMainThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        mainThread.start() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (timeout > 0) waitForStart(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Starts this <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Has no effect if this <CODE>CommunicatorServer</CODE> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <CODE>ONLINE</CODE> or <CODE>STOPPING</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   327
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            start(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } catch (InterruptedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            // cannot happen because of `0'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    "start","interrupted", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Stops this <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Has no effect if this <CODE>CommunicatorServer</CODE> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * <CODE>OFFLINE</CODE> or  <CODE>STOPPING</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   346
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (state == OFFLINE || state == STOPPING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        "stop","Connector is not ONLINE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            changeState(STOPPING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            // Stop the connector thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    "stop","Interrupt main thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            stopRequested = true ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            if (!interrupted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                mainThread.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // Call terminate on each active client handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                "stop","terminateAllClient");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        terminateAllClient() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // ----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // changeState
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        // ----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (state == STARTING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                changeState(OFFLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Tests whether the <CODE>CommunicatorServer</CODE> is active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @return True if connector is <CODE>ONLINE</CODE>; false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   394
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public boolean isActive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            return (state == ONLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <p>Waits until either the State attribute of this MBean equals the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * specified <VAR>wantedState</VAR> parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * or the specified  <VAR>timeOut</VAR> has elapsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * The method <CODE>waitState</CODE> returns with a boolean value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * indicating whether the specified <VAR>wantedState</VAR> parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * equals the value of this MBean's State attribute at the time the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * terminates.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * <p>Two special cases for the <VAR>timeOut</VAR> parameter value are:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * <UL><LI> if <VAR>timeOut</VAR> is negative then <CODE>waitState</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *     returns immediately (i.e. does not wait at all),</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * <LI> if <VAR>timeOut</VAR> equals zero then <CODE>waitState</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *     waits untill the value of this MBean's State attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *     is the same as the <VAR>wantedState</VAR> parameter (i.e. will wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *     indefinitely if this condition is never met).</LI></UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @param wantedState The value of this MBean's State attribute to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *        for. <VAR>wantedState</VAR> can be one of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * <li><CODE>CommunicatorServer.OFFLINE</CODE>,</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <li><CODE>CommunicatorServer.ONLINE</CODE>,</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * <li><CODE>CommunicatorServer.STARTING</CODE>,</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <li><CODE>CommunicatorServer.STOPPING</CODE>.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @param timeOut The maximum time to wait for, in milliseconds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *        if positive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Infinite time out if 0, or no waiting at all if negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @return true if the value of this MBean's State attribute is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *      same as the <VAR>wantedState</VAR> parameter; false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   433
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public boolean waitState(int wantedState, long timeOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                "waitState", wantedState + "(0on,1off,2st) TO=" + timeOut +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                  " ; current state = " + getStateString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        long endTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (timeOut > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            endTime = System.currentTimeMillis() + timeOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            while (state != wantedState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                if (timeOut < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                            "waitState", "timeOut < 0, return without wait");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        if (timeOut > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                            long toWait = endTime - System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            if (toWait <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                    SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                        "waitState", "timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                            stateLock.wait(toWait);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        } else {  // timeOut == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                            stateLock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                            SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                                "waitState", "wait interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        return (state == wantedState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    "waitState","returning in desired state");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * <p>Waits until the communicator is started or timeout expires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @param timeout Time in ms to wait for the connector to start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *        If <code>timeout</code> is positive, wait for at most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *        the specified time. An infinite timeout can be specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *        by passing a <code>timeout</code> value equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *        <code>Long.MAX_VALUE</code>. In that case the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *        will wait until the connector starts or fails to start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *        If timeout is negative or zero, returns as soon as possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *        without waiting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @exception CommunicationException if the connectors fails to start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @exception InterruptedException if the thread is interrupted or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *            timeout expires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    private void waitForStart(long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        throws CommunicationException, InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                "waitForStart", "Timeout=" + timeout +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                 " ; current state = " + getStateString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        final long startTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            while (state == STARTING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                // Time elapsed since startTime...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                final long elapsed = System.currentTimeMillis() - startTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                // wait for timeout - elapsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                // A timeout of Long.MAX_VALUE is equivalent to something
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                // like 292271023 years - which is pretty close to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                // forever as far as we are concerned ;-)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                final long remainingTime = timeout-elapsed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                // If remainingTime is negative, the timeout has elapsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                if (remainingTime < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                            "waitForStart", "timeout < 0, return without wait");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    throw new InterruptedException("Timeout expired");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                // We're going to wait until someone notifies on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                // the stateLock object, or until the timeout expires,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                // or until the thread is interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    stateLock.wait(remainingTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                            "waitForStart", "wait interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    // If we are now ONLINE, then no need to rethrow the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    // exception... we're simply going to exit the while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    // loop. Otherwise, throw the InterruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    if (state != ONLINE) throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            // We're no longer in STARTING state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (state == ONLINE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                // OK, we're started, everything went fine, just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        "waitForStart", "started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            } else if (startException instanceof CommunicationException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                // There was some exception during the starting phase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                // Cast and throw...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                throw (CommunicationException)startException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            } else if (startException instanceof InterruptedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                // There was some exception during the starting phase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                // Cast and throw...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                throw (InterruptedException)startException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            } else if (startException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                // There was some exception during the starting phase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                // Wrap and throw...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                throw new CommunicationException(startException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                                 "Failed to start: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                                                 startException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                // We're not ONLINE, and there's no exception...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                // Something went wrong but we don't know what...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                throw new CommunicationException("Failed to start: state is "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                                                 getStringForState(state));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Gets the state of this <CODE>CommunicatorServer</CODE> as an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @return <CODE>ONLINE</CODE>, <CODE>OFFLINE</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *         <CODE>STARTING</CODE> or <CODE>STOPPING</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   598
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public int getState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            return state ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * Gets the state of this <CODE>CommunicatorServer</CODE> as a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @return One of the strings "ONLINE", "OFFLINE", "STARTING" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *         "STOPPING".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   611
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    public String getStateString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        return getStringForState(state) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * Gets the host name used by this <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @return The host name used by this <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   621
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    public String getHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            host = InetAddress.getLocalHost().getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            host = "Unknown host";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        return host ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * Gets the port number used by this <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @return The port number used by this <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   636
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            return port ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * Sets the port number used by this <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @param port The port number used by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *             <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @exception java.lang.IllegalStateException This method has been invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * while the communicator was ONLINE or STARTING.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   652
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public void setPort(int port) throws java.lang.IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            if ((state == ONLINE) || (state == STARTING))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                throw new IllegalStateException("Stop server before " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                                "carrying out this operation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            dbgTag = makeDebugTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * Gets the protocol being used by this <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @return The protocol as a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   667
    @Override
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   668
    public abstract String getProtocol();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * Gets the number of clients that have been processed by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * <CODE>CommunicatorServer</CODE>  since its creation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @return The number of clients handled by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *         <CODE>CommunicatorServer</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *         since its creation. This counter is not reset by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *         <CODE>stop</CODE> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    int getServedClientCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        return servedClientCount ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Gets the number of clients currently being processed by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @return The number of clients currently being processed by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *         <CODE>CommunicatorServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    int getActiveClientCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        int result = clientHandlerVector.size() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        return result ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * Gets the maximum number of clients that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * <CODE>CommunicatorServer</CODE> can  process concurrently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @return The maximum number of clients that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *         <CODE>CommunicatorServer</CODE> can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *         process concurrently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    int getMaxActiveClientCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        return maxActiveClientCount ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * Sets the maximum number of clients this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * <CODE>CommunicatorServer</CODE> can process concurrently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @param c The number of clients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @exception java.lang.IllegalStateException This method has been invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * while the communicator was ONLINE or STARTING.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    void setMaxActiveClientCount(int c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        throws java.lang.IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            if ((state == ONLINE) || (state == STARTING)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                          "Stop server before carrying out this operation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            maxActiveClientCount = c ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * For SNMP Runtime internal use only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    void notifyClientHandlerCreated(ClientHandler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        clientHandlerVector.addElement(h) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * For SNMP Runtime internal use only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    synchronized void notifyClientHandlerDeleted(ClientHandler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        clientHandlerVector.removeElement(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * The number of times the communicator server will attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * to bind before giving up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    protected int getBindTries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        return 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * The delay, in ms, during which the communicator server will sleep before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * attempting to bind again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    protected long getBindSleepTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        return 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * For SNMP Runtime internal use only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * The <CODE>run</CODE> method executed by this connector's main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   763
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        // Fix jaw.00667.B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        // It seems that the init of "i" and "success"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        // need to be done outside the "try" clause...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        // A bug in Java 2 production release ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        boolean success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        // ----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        // Bind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        // ----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            // Fix for bug 4352451: "java.net.BindException: Address in use".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            final int  bindRetries = getBindTries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            final long sleepTime   = getBindSleepTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            while (i < bindRetries && !success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    // Try socket connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    doBind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    success = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                } catch (CommunicationException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                        Thread.sleep(sleepTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                        throw ie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            // Retry last time to get correct exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            if (!success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                // Try socket connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                doBind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        } catch(Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    "run", "Got unexpected exception", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            synchronized(stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                startException = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                changeState(OFFLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                    "run","State is OFFLINE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            doError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            // ----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            // State change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            // ----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            changeState(ONLINE) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                    "run","State is ONLINE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            // ----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            // Main loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            // ----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            while (!stopRequested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                servedClientCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                doReceive() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                waitIfTooManyClients() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                doProcess() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    "run","Stop has been requested");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        } catch(InterruptedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    "run","Interrupt caught");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            changeState(STOPPING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        } catch(Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    "run","Got unexpected exception", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            changeState(STOPPING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                interrupted = true;
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   861
                Thread.interrupted();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            // ----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            // unBind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            // ----------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                doUnbind() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                waitClientTermination() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                changeState(OFFLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                        "run","State is OFFLINE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            } catch(Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                        "run","Got unexpected exception", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                changeState(OFFLINE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    protected abstract void doError(Exception e) throws CommunicationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    // To be defined by the subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    // Each method below is called by run() and must be subclassed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    // If the method sends an exception (Communication or Interrupt), this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    // will end up the run() method and switch the connector offline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    // If it is a CommunicationException, run() will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    //       Debug.printException().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    // All these methods should propagate the InterruptedException to inform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    // run() that the connector must be switch OFFLINE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    // doBind() should do all what is needed before calling doReceive().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    // If doBind() throws an exception, doUnbind() is not to be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    // and run() ends up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    protected abstract void doBind()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        throws CommunicationException, InterruptedException ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * <CODE>doReceive()</CODE> should block until a client is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * If this method throws an exception, <CODE>doProcess()</CODE> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * called but <CODE>doUnbind()</CODE> is called then <CODE>run()</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * stops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    protected abstract void doReceive()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        throws CommunicationException, InterruptedException ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * <CODE>doProcess()</CODE> is called after <CODE>doReceive()</CODE>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * it should process the requests of the incoming client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * If it throws an exception, <CODE>doUnbind()</CODE> is called and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * <CODE>run()</CODE> stops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    protected abstract void doProcess()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        throws CommunicationException, InterruptedException ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * <CODE>doUnbind()</CODE> is called whenever the connector goes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * <CODE>OFFLINE</CODE>, except if <CODE>doBind()</CODE> has thrown an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    protected abstract void doUnbind()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        throws CommunicationException, InterruptedException ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * Get the <code>MBeanServer</code> object to which incoming requests are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * sent.  This is either the MBean server in which this connector is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * registered, or an <code>MBeanServerForwarder</code> leading to that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    public synchronized MBeanServer getMBeanServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        return topMBS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * Set the <code>MBeanServer</code> object to which incoming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * requests are sent.  This must be either the MBean server in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * which this connector is registered, or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * <code>MBeanServerForwarder</code> leading to that server.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * <code>MBeanServerForwarder</code> <code>mbsf</code> leads to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * MBean server <code>mbs</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * <code>mbsf.getMBeanServer()</code> is either <code>mbs</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * or an <code>MBeanServerForwarder</code> leading to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * <code>mbs</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * @exception IllegalArgumentException if <code>newMBS</code> is neither
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * the MBean server in which this connector is registered nor an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * <code>MBeanServerForwarder</code> leading to that server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * @exception IllegalStateException This method has been invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * while the communicator was ONLINE or STARTING.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    public synchronized void setMBeanServer(MBeanServer newMBS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            throws IllegalArgumentException, IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            if (state == ONLINE || state == STARTING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                throw new IllegalStateException("Stop server before " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                                                "carrying out this operation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        final String error =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            "MBeanServer argument must be MBean server where this " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            "server is registered, or an MBeanServerForwarder " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            "leading to that server";
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
   980
        Vector<MBeanServer> seenMBS = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        for (MBeanServer mbs = newMBS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
             mbs != bottomMBS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
             mbs = ((MBeanServerForwarder) mbs).getMBeanServer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            if (!(mbs instanceof MBeanServerForwarder))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                throw new IllegalArgumentException(error);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            if (seenMBS.contains(mbs))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                throw new IllegalArgumentException("MBeanServerForwarder " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                                                   "loop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            seenMBS.addElement(mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        topMBS = newMBS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    // To be called by the subclass if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * For internal use only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    ObjectName getObjectName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        return objectName ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * For internal use only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    void changeState(int newState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        int oldState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            if (state == newState)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            oldState = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            stateLock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        sendStateChangeNotification(oldState, newState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * Returns the string used in debug traces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    String makeDebugTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        return "CommunicatorServer["+ getProtocol() + ":" + getPort() + "]" ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * Returns the string used to name the connector thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    String makeThreadName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        String result ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        if (objectName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            result = "CommunicatorServer" ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            result = objectName.toString() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        return result ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * This method blocks if there are too many active clients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * Call to <CODE>wait()</CODE> is terminated when a client handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * thread calls <CODE>notifyClientHandlerDeleted(this)</CODE> ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    private synchronized void waitIfTooManyClients()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        while (getActiveClientCount() >= maxActiveClientCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                    "waitIfTooManyClients","Waiting for a client to terminate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * This method blocks until there is no more active client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    private void waitClientTermination() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        int s = clientHandlerVector.size() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            if (s >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                "waitClientTermination","waiting for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                      s + " clients to terminate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        // The ClientHandler will remove themselves from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        // clientHandlerVector at the end of their run() method, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        // calling notifyClientHandlerDeleted().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        // Since the clientHandlerVector is modified by the ClientHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        // threads we must avoid using Enumeration or Iterator to loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        // over this array. We must also take care of NoSuchElementException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        // which could be thrown if the last ClientHandler removes itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        // between the call to clientHandlerVector.isEmpty() and the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        // to clientHandlerVector.firstElement().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        // What we *MUST NOT DO* is locking the clientHandlerVector, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        // this would most probably cause a deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        while (! clientHandlerVector.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                clientHandlerVector.firstElement().join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            } catch (NoSuchElementException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                        "waitClientTermination","No elements left",  x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            if (s >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                    "waitClientTermination","Ok, let's go...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * Call <CODE>interrupt()</CODE> on each pending client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    private void terminateAllClient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        final int s = clientHandlerVector.size() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            if (s >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    "terminateAllClient","Interrupting " + s + " clients");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        // The ClientHandler will remove themselves from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        // clientHandlerVector at the end of their run() method, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        // calling notifyClientHandlerDeleted().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        // Since the clientHandlerVector is modified by the ClientHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        // threads we must avoid using Enumeration or Iterator to loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        // over this array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        // We cannot use the same logic here than in waitClientTermination()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        // because there is no guarantee that calling interrupt() on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        // ClientHandler will actually terminate the ClientHandler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        // Since we do not want to wait for the actual ClientHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        // termination, we cannot simply loop over the array until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        // empty (this might result in calling interrupt() endlessly on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        // the same client handler. So what we do is simply take a snapshot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        // copy of the vector and loop over the copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        // What we *MUST NOT DO* is locking the clientHandlerVector, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        // this would most probably cause a deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        final  ClientHandler[] handlers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                clientHandlerVector.toArray(new ClientHandler[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
         for (ClientHandler h : handlers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
             try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                 h.interrupt() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
             } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                 if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                     SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                             "terminateAllClient",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                             "Failed to interrupt pending request. " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                             "Ignore the exception.", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * Controls the way the CommunicatorServer service is deserialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    private void readObject(ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        // Call the default deserialization of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        stream.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        // Call the specific initialization for the CommunicatorServer service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        // This is for transient structures to be initialized to specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        // default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        stateLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        state = OFFLINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        stopRequested = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        servedClientCount = 0;
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
  1163
        clientHandlerVector = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        mainThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        notifCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        notifInfos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        notifBroadcaster = new NotificationBroadcasterSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        dbgTag = makeDebugTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    // NotificationBroadcaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * Adds a listener for the notifications emitted by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * CommunicatorServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * There is only one type of notifications sent by the CommunicatorServer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * they are <tt>{@link javax.management.AttributeChangeNotification}</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * sent when the <tt>State</tt> attribute of this CommunicatorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * @param listener The listener object which will handle the emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     *        notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * @param filter The filter object. If filter is null, no filtering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     *        will be performed before handling notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * @param handback An object which will be sent back unchanged to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     *        listener when a notification is emitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @exception IllegalArgumentException Listener parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
  1193
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    public void addNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                                        NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                                        Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        throws java.lang.IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                "addNotificationListener","Adding listener "+ listener +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                  " with filter "+ filter + " and handback "+ handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        notifBroadcaster.addNotificationListener(listener, filter, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * Removes the specified listener from this CommunicatorServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * Note that if the listener has been registered with different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * handback objects or notification filters, all entries corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * to the listener will be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * @param listener The listener object to be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * @exception ListenerNotFoundException The listener is not registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
  1217
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    public void removeNotificationListener(NotificationListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                "removeNotificationListener","Removing listener "+ listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        notifBroadcaster.removeNotificationListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * Returns an array of MBeanNotificationInfo objects describing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * the notification types sent by this CommunicatorServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * There is only one type of notifications sent by the CommunicatorServer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * it is <tt>{@link javax.management.AttributeChangeNotification}</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * sent when the <tt>State</tt> attribute of this CommunicatorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
  1236
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    public MBeanNotificationInfo[] getNotificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        // Initialize notifInfos on first call to getNotificationInfo()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        if (notifInfos == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            notifInfos = new MBeanNotificationInfo[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            String[] notifTypes = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                AttributeChangeNotification.ATTRIBUTE_CHANGE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            notifInfos[0] = new MBeanNotificationInfo( notifTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                     AttributeChangeNotification.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                     "Sent to notify that the value of the State attribute "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                     "of this CommunicatorServer instance has changed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
21291
64a9ea14762c 8026028: [findbugs] findbugs report some issue in com.sun.jmx.snmp package
sjiang
parents: 14677
diff changeset
  1251
        return notifInfos.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    private void sendStateChangeNotification(int oldState, int newState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        String oldStateString = getStringForState(oldState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        String newStateString = getStringForState(newState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        String message = new StringBuffer().append(dbgTag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            .append(" The value of attribute State has changed from ")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            .append(oldState).append(" (").append(oldStateString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            .append(") to ").append(newState).append(" (")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            .append(newStateString).append(").").toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        notifCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        AttributeChangeNotification notif =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            new AttributeChangeNotification(this,    // source
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                         notifCount,                 // sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                         System.currentTimeMillis(), // time stamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                         message,                    // message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                         "State",                    // attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                         "int",                      // attribute type
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
  1275
                         oldState,                   // old value
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
  1276
                         newState );                 // new value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                "sendStateChangeNotification","Sending AttributeChangeNotification #"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                    + notifCount + " with message: "+ message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        notifBroadcaster.sendNotification(notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    private static String getStringForState(int s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        switch (s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        case ONLINE:   return "ONLINE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        case STARTING: return "STARTING";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        case OFFLINE:  return "OFFLINE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        case STOPPING: return "STOPPING";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        default:       return "UNDEFINED";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    // MBeanRegistration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * Preregister method of connector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     *@param server The <CODE>MBeanServer</CODE> in which the MBean will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     *       be registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     *@param name The object name of the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     *@return  The name of the MBean registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     *@exception java.langException This exception should be caught by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     *           the <CODE>MBeanServer</CODE> and re-thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *           as an <CODE>MBeanRegistrationException</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
  1316
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    public ObjectName preRegister(MBeanServer server, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            throws java.lang.Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        objectName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            if (bottomMBS != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                throw new IllegalArgumentException("connector already " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                                                   "registered in an MBean " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                                                   "server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            topMBS = bottomMBS = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        dbgTag = makeDebugTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     *@param registrationDone Indicates whether or not the MBean has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     *       successfully registered in the <CODE>MBeanServer</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     *       The value false means that the registration phase has failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
  1338
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    public void postRegister(Boolean registrationDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        if (!registrationDone.booleanValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                topMBS = bottomMBS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * Stop the connector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * @exception java.langException This exception should be caught by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     *            the <CODE>MBeanServer</CODE> and re-thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     *            as an <CODE>MBeanRegistrationException</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
  1354
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    public void preDeregister() throws java.lang.Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            topMBS = bottomMBS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        objectName = null ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        final int cstate = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        if ((cstate == ONLINE) || ( cstate == STARTING)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            stop() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * Do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     */
14677
1607f4cfc506 8003476: Cleanup warnings in com.sun.jmx.snmp code
dfuchs
parents: 5506
diff changeset
  1369
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    public void postDeregister(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
}