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