test/jdk/com/sun/jndi/ldap/lib/BaseLdapServer.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58126 1def54255e93
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57739
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     1
/*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     4
 *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     8
 *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    13
 * accompanied this code).
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    14
 *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    18
 *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    21
 * questions.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    22
 */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    23
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    24
import java.io.ByteArrayOutputStream;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    25
import java.io.Closeable;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    26
import java.io.IOException;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    27
import java.io.InputStream;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    28
import java.io.OutputStream;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    29
import java.net.InetAddress;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    30
import java.net.ServerSocket;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    31
import java.net.Socket;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    32
import java.util.ArrayList;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    33
import java.util.Arrays;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    34
import java.util.List;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    35
import java.util.Objects;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    36
import java.util.concurrent.ExecutorService;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    37
import java.util.concurrent.Executors;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    38
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    39
import static java.lang.System.Logger.Level.INFO;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    40
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    41
/*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    42
 * A bare-bones (testing aid) server for LDAP scenarios.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    43
 *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    44
 * Override the following methods to provide customized behavior
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    45
 *
58126
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
    46
 *     * beforeAcceptingConnections
57739
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    47
 *     * beforeConnectionHandled
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    48
 *     * handleRequest
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    49
 *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    50
 * Instances of this class are safe for use by multiple threads.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    51
 */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    52
public class BaseLdapServer implements Closeable {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    53
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    54
    private static final System.Logger logger = System.getLogger("BaseLdapServer");
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    55
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    56
    private final Thread acceptingThread = new Thread(this::acceptConnections);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    57
    private final ServerSocket serverSocket;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    58
    private final List<Socket> socketList = new ArrayList<>();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    59
    private final ExecutorService connectionsPool;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    60
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    61
    private final Object lock = new Object();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    62
    /*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    63
     * 3-valued state to detect restarts and other programming errors.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    64
     */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    65
    private State state = State.NEW;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    66
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    67
    private enum State {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    68
        NEW,
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    69
        STARTED,
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    70
        STOPPED
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    71
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    72
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    73
    public BaseLdapServer() throws IOException {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    74
        this(new ServerSocket(0, 0, InetAddress.getLoopbackAddress()));
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    75
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    76
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    77
    public BaseLdapServer(ServerSocket serverSocket) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    78
        this.serverSocket = Objects.requireNonNull(serverSocket);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    79
        this.connectionsPool = Executors.newCachedThreadPool();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    80
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    81
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    82
    private void acceptConnections() {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    83
        logger().log(INFO, "Server is accepting connections at port {0}",
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    84
                     getPort());
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    85
        try {
58126
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
    86
            beforeAcceptingConnections();
57739
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    87
            while (isRunning()) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    88
                Socket socket = serverSocket.accept();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    89
                logger().log(INFO, "Accepted new connection at {0}", socket);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    90
                synchronized (lock) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    91
                    // Recheck if the server is still running
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    92
                    // as someone has to close the `socket`
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    93
                    if (isRunning()) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    94
                        socketList.add(socket);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    95
                    } else {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    96
                        closeSilently(socket);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    97
                    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    98
                }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
    99
                connectionsPool.submit(() -> handleConnection(socket));
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   100
            }
58126
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   101
        } catch (Throwable t) {
57739
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   102
            if (isRunning()) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   103
                throw new RuntimeException(
58126
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   104
                        "Unexpected exception while accepting connections", t);
57739
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   105
            }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   106
        } finally {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   107
            logger().log(INFO, "Server stopped accepting connections at port {0}",
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   108
                                getPort());
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   109
        }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   110
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   111
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   112
    /*
58126
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   113
     * Called once immediately preceding the server accepting connections.
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   114
     *
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   115
     * Override to customize the behavior.
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   116
     */
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   117
    protected void beforeAcceptingConnections() { }
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   118
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   119
    /*
57739
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   120
     * A "Template Method" describing how a connection (represented by a socket)
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   121
     * is handled.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   122
     *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   123
     * The socket is closed immediately before the method returns (normally or
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   124
     * abruptly).
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   125
     */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   126
    private void handleConnection(Socket socket) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   127
        // No need to close socket's streams separately, they will be closed
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   128
        // automatically when `socket.close()` is called
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   129
        beforeConnectionHandled(socket);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   130
        try (socket) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   131
            OutputStream out = socket.getOutputStream();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   132
            InputStream in = socket.getInputStream();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   133
            byte[] inBuffer = new byte[1024];
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   134
            int count;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   135
            byte[] request;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   136
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   137
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   138
            int msgLen = -1;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   139
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   140
            // As inBuffer.length > 0, at least 1 byte is read
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   141
            while ((count = in.read(inBuffer)) > 0) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   142
                buffer.write(inBuffer, 0, count);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   143
                if (msgLen <= 0) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   144
                    msgLen = LdapMessage.getMessageLength(buffer.toByteArray());
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   145
                }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   146
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   147
                if (msgLen > 0 && buffer.size() >= msgLen) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   148
                    if (buffer.size() > msgLen) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   149
                        byte[] tmpBuffer = buffer.toByteArray();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   150
                        request = Arrays.copyOf(tmpBuffer, msgLen);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   151
                        buffer.reset();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   152
                        buffer.write(tmpBuffer, msgLen, tmpBuffer.length - msgLen);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   153
                    } else {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   154
                        request = buffer.toByteArray();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   155
                        buffer.reset();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   156
                    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   157
                    msgLen = -1;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   158
                } else {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   159
                    logger.log(INFO, "Request message incomplete, " +
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   160
                            "bytes received {0}, expected {1}", buffer.size(), msgLen);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   161
                    continue;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   162
                }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   163
                handleRequest(socket, new LdapMessage(request), out);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   164
            }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   165
        } catch (Throwable t) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   166
            if (!isRunning()) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   167
                logger.log(INFO, "Connection Handler exit {0}", t.getMessage());
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   168
            } else {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   169
                t.printStackTrace();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   170
            }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   171
        }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   172
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   173
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   174
    /*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   175
     * Called first thing in `handleConnection()`.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   176
     *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   177
     * Override to customize the behavior.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   178
     */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   179
    protected void beforeConnectionHandled(Socket socket) { /* empty */ }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   180
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   181
    /*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   182
     * Called after an LDAP request has been read in `handleConnection()`.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   183
     *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   184
     * Override to customize the behavior.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   185
     */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   186
    protected void handleRequest(Socket socket,
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   187
                                 LdapMessage request,
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   188
                                 OutputStream out)
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   189
            throws IOException
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   190
    {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   191
        logger().log(INFO, "Discarding message {0} from {1}. "
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   192
                             + "Override {2}.handleRequest to change this behavior.",
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   193
                     request, socket, getClass().getName());
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   194
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   195
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   196
    /*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   197
     * To be used by subclasses.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   198
     */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   199
    protected final System.Logger logger() {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   200
        return logger;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   201
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   202
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   203
    /*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   204
     * Starts this server. May be called only once.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   205
     */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   206
    public BaseLdapServer start() {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   207
        synchronized (lock) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   208
            if (state != State.NEW) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   209
                throw new IllegalStateException(state.toString());
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   210
            }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   211
            state = State.STARTED;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   212
            logger().log(INFO, "Starting server at port {0}", getPort());
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   213
            acceptingThread.start();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   214
            return this;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   215
        }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   216
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   217
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   218
    /*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   219
     * Stops this server.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   220
     *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   221
     * May be called at any time, even before a call to `start()`. In the latter
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   222
     * case the subsequent call to `start()` will throw an exception. Repeated
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   223
     * calls to this method have no effect.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   224
     *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   225
     * Stops accepting new connections, interrupts the threads serving already
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   226
     * accepted connections and closes all the sockets.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   227
     */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   228
    @Override
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   229
    public void close() {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   230
        synchronized (lock) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   231
            if (state == State.STOPPED) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   232
                return;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   233
            }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   234
            state = State.STOPPED;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   235
            logger().log(INFO, "Stopping server at port {0}", getPort());
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   236
            acceptingThread.interrupt();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   237
            closeSilently(serverSocket);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   238
            // It's important to signal an interruption so that overridden
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   239
            // methods have a chance to return if they use
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   240
            // interruption-sensitive blocking operations. However, blocked I/O
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   241
            // operations on the socket will NOT react on that, hence the socket
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   242
            // also has to be closed to propagate shutting down.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   243
            connectionsPool.shutdownNow();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   244
            socketList.forEach(BaseLdapServer.this::closeSilently);
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   245
        }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   246
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   247
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   248
    /**
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   249
     * Returns the local port this server is listening at.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   250
     *
58126
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   251
     * This method can be called at any time.
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   252
     *
57739
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   253
     * @return the port this server is listening at
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   254
     */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   255
    public int getPort() {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   256
        return serverSocket.getLocalPort();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   257
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   258
58126
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   259
    /**
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   260
     * Returns the address this server is listening at.
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   261
     *
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   262
     * This method can be called at any time.
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   263
     *
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   264
     * @return the address
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   265
     */
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   266
    public InetAddress getInetAddress() {
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   267
        return serverSocket.getInetAddress();
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   268
    }
1def54255e93 8151678: com/sun/jndi/ldap/LdapTimeoutTest.java failed due to timeout on DeadServerNoTimeoutTest is incorrect
prappo
parents: 57739
diff changeset
   269
57739
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   270
    /*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   271
     * Returns a flag to indicate whether this server is running or not.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   272
     *
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   273
     * @return {@code true} if this server is running, {@code false} otherwise.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   274
     */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   275
    public boolean isRunning() {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   276
        synchronized (lock) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   277
            return state == State.STARTED;
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   278
        }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   279
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   280
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   281
    /*
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   282
     * To be used by subclasses.
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   283
     */
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   284
    protected final void closeSilently(Closeable resource) {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   285
        try {
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   286
            resource.close();
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   287
        } catch (IOException ignored) { }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   288
    }
6717d7e59db4 8217606: LdapContext#reconnect always opens a new connection
prappo
parents:
diff changeset
   289
}