jdk/src/share/classes/com/sun/jndi/ldap/Connection.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 14184 5553422ece67
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13157
2141a68fb2a7 7174887: Deadlock in jndi ldap connection cleanup
robm
parents: 10703
diff changeset
     2
 * Copyright (c) 1999, 2012, 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: 4188
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: 4188
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: 4188
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
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
package com.sun.jndi.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InterruptedIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.Socket;
10703
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
    35
import javax.net.ssl.SSLSocket;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.naming.CommunicationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.naming.ServiceUnavailableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.naming.NamingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.naming.InterruptedNamingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.naming.ldap.Control;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.lang.reflect.InvocationTargetException;
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
    47
import java.util.Arrays;
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
    48
import sun.misc.IOUtils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
//import javax.net.SocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  * A thread that creates a connection to an LDAP server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  * After the connection, the thread reads from the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  * A caller can invoke methods on the instance to read LDAP responses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  * and to send LDAP requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  * There is a one-to-one correspondence between an LdapClient and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  * a Connection. Access to Connection and its methods is only via
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  * LdapClient with two exceptions: SASL authentication and StartTLS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  * SASL needs to access Connection's socket IO streams (in order to do encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  * of the security layer). StartTLS needs to do replace IO streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  * and close the IO  streams on nonfatal close. The code for SASL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  * authentication can be treated as being the same as from LdapClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  * because the SASL code is only ever called from LdapClient, from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  * inside LdapClient's synchronized authenticate() method. StartTLS is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
  * directly by the application but should only occur when the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
  * connection is quiet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  * In terms of synchronization, worry about data structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  * used by the Connection thread because that usage might contend
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  * with calls by the main threads (i.e., those that call LdapClient).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  * Main threads need to worry about contention with each other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
  * Fields that Connection thread uses:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
  *     inStream - synced access and update; initialized in constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
  *           referenced outside class unsync'ed (by LdapSasl) only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
  *           when connection is quiet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
  *     traceFile, traceTagIn, traceTagOut - no sync; debugging only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  *     parent - no sync; initialized in constructor; no updates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
  *     pendingRequests - sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
  *     pauseLock - per-instance lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
  *     paused - sync via pauseLock (pauseReader())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
  * Members used by main threads (LdapClient):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
  *     host, port - unsync; read-only access for StartTLS and debug messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
  *     setBound(), setV3() - no sync; called only by LdapClient.authenticate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
  *             which is a sync method called only when connection is "quiet"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
  *     getMsgId() - sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
  *     writeRequest(), removeRequest(),findRequest(), abandonOutstandingReqs() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
  *             access to shared pendingRequests is sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
  *     writeRequest(),  abandonRequest(), ldapUnbind() - access to outStream sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
  *     cleanup() - sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
  *     readReply() - access to sock sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  *     unpauseReader() - (indirectly via writeRequest) sync on pauseLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
  * Members used by SASL auth (main thread):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
  *     inStream, outStream - no sync; used to construct new stream; accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
  *             only when conn is "quiet" and not shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
  *     replaceStreams() - sync method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
  * Members used by StartTLS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
  *     inStream, outStream - no sync; used to record the existing streams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
  *             accessed only when conn is "quiet" and not shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
  *     replaceStreams() - sync method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
  * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
  * Handles anonymous, simple, and SASL bind for v3; anonymous and simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  * for v2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
  * %%% made public for access by LdapSasl %%%
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
  * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
  * @author Jagane Sundar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
public final class Connection implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static final boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static final int dump = 0; // > 0 r, > 1 rw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    final private Thread worker;    // Initialized in constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private boolean v3 = true;       // Set in setV3()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    final public String host;  // used by LdapClient for generating exception messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                         // used by StartTlsResponse when creating an SSL socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    final public int port;     // used by LdapClient for generating exception messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                         // used by StartTlsResponse when creating an SSL socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private boolean bound = false;   // Set in setBound()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // All three are initialized in constructor and read-only afterwards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private OutputStream traceFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private String traceTagIn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private String traceTagOut = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // Initialized in constructor; read and used externally (LdapSasl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // Updated in replaceStreams() during "quiet", unshared, period
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public InputStream inStream;   // must be public; used by LdapSasl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // Initialized in constructor; read and used externally (LdapSasl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // Updated in replaceOutputStream() during "quiet", unshared, period
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public OutputStream outStream; // must be public; used by LdapSasl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // Initialized in constructor; read and used externally (TLS) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // get new IO streams; closed during cleanup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public Socket sock;            // for TLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    // For processing "disconnect" unsolicited notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    // Initialized in constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    final private LdapClient parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // Incremented and returned in sync getMsgId()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private int outMsgId = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    // The list of ldapRequests pending on this binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // Accessed only within sync methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private LdapRequest pendingRequests = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    volatile IOException closureReason = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    volatile boolean useable = true;  // is Connection still useable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
14184
5553422ece67 8000487: Java JNDI connection library on ldap conn is not honoring configured timeout
robm
parents: 13157
diff changeset
   160
    int readTimeout;
5553422ece67 8000487: Java JNDI connection library on ldap conn is not honoring configured timeout
robm
parents: 13157
diff changeset
   161
    int connectTimeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // true means v3; false means v2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // Called in LdapClient.authenticate() (which is synchronized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // when connection is "quiet" and not shared; no need to synchronize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    void setV3(boolean v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        v3 = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // A BIND request has been successfully made on this connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // When cleaning up, remember to do an UNBIND
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    // Called in LdapClient.authenticate() (which is synchronized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    // when connection is "quiet" and not shared; no need to synchronize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    void setBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // Create an LDAP Binding object and bind to a particular server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    Connection(LdapClient parent, String host, int port, String socketFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int connectTimeout, int readTimeout, OutputStream trace) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        this.readTimeout = readTimeout;
14184
5553422ece67 8000487: Java JNDI connection library on ldap conn is not honoring configured timeout
robm
parents: 13157
diff changeset
   191
        this.connectTimeout = connectTimeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (trace != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            traceFile = trace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            traceTagIn = "<- " + host + ":" + port + "\n\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            traceTagOut = "-> " + host + ":" + port + "\n\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // Connect to server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            sock = createSocket(host, port, socketFactory, connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                System.err.println("Connection: opening socket: " + host + "," + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            inStream = new BufferedInputStream(sock.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            outStream = new BufferedOutputStream(sock.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            Throwable realException = e.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            // realException.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            CommunicationException ce =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                new CommunicationException(host + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            ce.setRootCause(realException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw ce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            // Class.forName() seems to do more error checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            // and will throw IllegalArgumentException and such.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // That's why we need to have a catch all here and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // ignore generic exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // Also catches all IO errors generated by socket creation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            CommunicationException ce =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                new CommunicationException(host + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            ce.setRootCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw ce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        worker = Obj.helper.createThread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        worker.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        worker.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Create an InetSocketAddress using the specified hostname and port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    private Object createInetSocketAddress(String host, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throws NoSuchMethodException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        try {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   244
            Class<?> inetSocketAddressClass =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                Class.forName("java.net.InetSocketAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   247
            Constructor<?> inetSocketAddressCons =
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   248
                inetSocketAddressClass.getConstructor(new Class<?>[]{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                String.class, int.class});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return inetSocketAddressCons.newInstance(new Object[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                host, new Integer(port)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   254
        } catch (ClassNotFoundException |
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   255
                 InstantiationException |
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   256
                 InvocationTargetException |
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   257
                 IllegalAccessException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            throw new NoSuchMethodException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Create a Socket object using the specified socket factory and time limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * If a timeout is supplied and unconnected sockets are supported then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * an unconnected socket is created and the timeout is applied when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * connecting the socket. If a timeout is supplied but unconnected sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * are not supported then the timeout is ignored and a connected socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private Socket createSocket(String host, int port, String socketFactory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            int connectTimeout) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        Socket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (socketFactory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // create the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   281
            Class<?> socketFactoryClass = Obj.helper.loadClass(socketFactory);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            Method getDefault =
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   283
                socketFactoryClass.getMethod("getDefault", new Class<?>[]{});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            Object factory = getDefault.invoke(null, new Object[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // create the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            Method createSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (connectTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    createSocket = socketFactoryClass.getMethod("createSocket",
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   294
                        new Class<?>[]{});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    Method connect = Socket.class.getMethod("connect",
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   297
                        new Class<?>[]{Class.forName("java.net.SocketAddress"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        int.class});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    Object endpoint = createInetSocketAddress(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    // unconnected socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    socket =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        (Socket)createSocket.invoke(factory, new Object[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        System.err.println("Connection: creating socket with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                            "a timeout using supplied socket factory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    // connected socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    connect.invoke(socket, new Object[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        endpoint, new Integer(connectTimeout)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    // continue (but ignore connectTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (socket == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                createSocket = socketFactoryClass.getMethod("createSocket",
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   321
                    new Class<?>[]{String.class, int.class});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    System.err.println("Connection: creating socket using " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        "supplied socket factory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                // connected socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                socket = (Socket) createSocket.invoke(factory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    new Object[]{host, new Integer(port)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (connectTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                try {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   336
                    Constructor<Socket> socketCons =
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   337
                        Socket.class.getConstructor(new Class<?>[]{});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    Method connect = Socket.class.getMethod("connect",
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   340
                        new Class<?>[]{Class.forName("java.net.SocketAddress"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        int.class});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    Object endpoint = createInetSocketAddress(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 8564
diff changeset
   344
                    socket = socketCons.newInstance(new Object[]{});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        System.err.println("Connection: creating socket with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                            "a timeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    connect.invoke(socket, new Object[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        endpoint, new Integer(connectTimeout)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    // continue (but ignore connectTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (socket == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    System.err.println("Connection: creating socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                // connected socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                socket = new Socket(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
10703
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   367
        // For LDAP connect timeouts on LDAP over SSL connections must treat
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   368
        // the SSL handshake following socket connection as part of the timeout.
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   369
        // So explicitly set a socket read timeout, trigger the SSL handshake,
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   370
        // then reset the timeout.
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   371
        if (connectTimeout > 0 && socket instanceof SSLSocket) {
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   372
            SSLSocket sslSocket = (SSLSocket) socket;
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   373
            int socketTimeout = sslSocket.getSoTimeout();
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   374
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   375
            sslSocket.setSoTimeout(connectTimeout); // reuse full timeout value
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   376
            sslSocket.startHandshake();
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   377
            sslSocket.setSoTimeout(socketTimeout);
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   378
        }
5e4b04df89ec 7094377: Com.sun.jndi.ldap.read.timeout doesn't work with ldaps.
vinnie
parents: 10324
diff changeset
   379
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    // Methods to IO to the LDAP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    synchronized int getMsgId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return ++outMsgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    LdapRequest writeRequest(BerEncoder ber, int msgId) throws IOException {
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 8182
diff changeset
   394
        return writeRequest(ber, msgId, false /* pauseAfterReceipt */, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 8182
diff changeset
   397
    LdapRequest writeRequest(BerEncoder ber, int msgId,
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 8182
diff changeset
   398
        boolean pauseAfterReceipt) throws IOException {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 8182
diff changeset
   399
        return writeRequest(ber, msgId, pauseAfterReceipt, -1);
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 8182
diff changeset
   400
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 8182
diff changeset
   402
    LdapRequest writeRequest(BerEncoder ber, int msgId,
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 8182
diff changeset
   403
        boolean pauseAfterReceipt, int replyQueueCapacity) throws IOException {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 8182
diff changeset
   404
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 8182
diff changeset
   405
        LdapRequest req =
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 8182
diff changeset
   406
            new LdapRequest(msgId, pauseAfterReceipt, replyQueueCapacity);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        addRequest(req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (traceFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            Ber.dumpBER(traceFile, traceTagOut, ber.getBuf(), 0, ber.getDataLen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // unpause reader so that it can get response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // NOTE: Must do this before writing request, otherwise might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        // create a race condition where the writer unblocks its own response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        unpauseReader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            System.err.println("Writing request to: " + outStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                outStream.write(ber.getBuf(), 0, ber.getDataLen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                outStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            cleanup(null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            throw (closureReason = e); // rethrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        return req;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Reads a reply; waits until one is ready.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    BerDecoder readReply(LdapRequest ldr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            throws IOException, NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        BerDecoder rber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        boolean waited = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        while (((rber = ldr.getReplyBer()) == null) && !waited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                // If socket closed, don't even try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    if (sock == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        throw new ServiceUnavailableException(host + ":" + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                            "; socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                synchronized (ldr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    // check if condition has changed since our last check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    rber = ldr.getReplyBer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    if (rber == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        if (readTimeout > 0) {  // Socket read timeout is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                            // will be woken up before readTimeout only if reply is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                            // available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                            ldr.wait(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                            waited = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                            ldr.wait(15 * 1000); // 15 second timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                throw new InterruptedNamingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    "Interrupted during LDAP operation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if ((rber == null) && waited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            removeRequest(ldr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            throw new NamingException("LDAP response read timed out, timeout used:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                            + readTimeout + "ms." );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return rber;
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
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    // Methods to add, find, delete, and abandon requests made to server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    private synchronized void addRequest(LdapRequest ldapRequest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        LdapRequest ldr = pendingRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (ldr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            pendingRequests = ldapRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            ldapRequest.next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            ldapRequest.next = pendingRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            pendingRequests = ldapRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    synchronized LdapRequest findRequest(int msgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        LdapRequest ldr = pendingRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        while (ldr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            if (ldr.msgId == msgId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                return ldr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            ldr = ldr.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    synchronized void removeRequest(LdapRequest req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        LdapRequest ldr = pendingRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        LdapRequest ldrprev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        while (ldr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            if (ldr == req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                ldr.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                if (ldrprev != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    ldrprev.next = ldr.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    pendingRequests = ldr.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                ldr.next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            ldrprev = ldr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            ldr = ldr.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    void abandonRequest(LdapRequest ldr, Control[] reqCtls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        // Remove from queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        removeRequest(ldr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        BerEncoder ber = new BerEncoder(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        int abandonMsgId = getMsgId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        // build the abandon request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                ber.encodeInt(abandonMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                ber.encodeInt(ldr.msgId, LdapClient.LDAP_REQ_ABANDON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                if (v3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    LdapClient.encodeControls(ber, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (traceFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                Ber.dumpBER(traceFile, traceTagOut, ber.getBuf(), 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    ber.getDataLen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                outStream.write(ber.getBuf(), 0, ber.getDataLen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                outStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            //System.err.println("ldap.abandon: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14184
diff changeset
   571
        // Don't expect any response for the abandon request.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    synchronized void abandonOutstandingReqs(Control[] reqCtls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        LdapRequest ldr = pendingRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        while (ldr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            abandonRequest(ldr, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            pendingRequests = ldr = ldr.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    // Methods to unbind from server and clear up resources when object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    // destroyed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    private void ldapUnbind(Control[] reqCtls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        BerEncoder ber = new BerEncoder(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        int unbindMsgId = getMsgId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        // build the unbind request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                ber.encodeInt(unbindMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                // IMPLICIT TAGS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                ber.encodeByte(LdapClient.LDAP_REQ_UNBIND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                ber.encodeByte(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                if (v3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    LdapClient.encodeControls(ber, reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (traceFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                Ber.dumpBER(traceFile, traceTagOut, ber.getBuf(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    0, ber.getDataLen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                outStream.write(ber.getBuf(), 0, ber.getDataLen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                outStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            //System.err.println("ldap.unbind: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14184
diff changeset
   626
        // Don't expect any response for the unbind request.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @param reqCtls Possibly null request controls that accompanies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *    abandon and unbind LDAP request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @param notifyParent true means to call parent LdapClient back, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *    it that the connection has been closed; false means not to notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *    parent. If LdapClient invokes cleanup(), notifyParent should be set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *    false because LdapClient already knows that it is closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *    the connection. If Connection invokes cleanup(), notifyParent should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *    set to true because LdapClient needs to know about the closure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    void cleanup(Control[] reqCtls, boolean notifyParent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        boolean nparent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            useable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            if (sock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    System.err.println("Connection: closing socket: " + host + "," + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    if (!notifyParent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        abandonOutstandingReqs(reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    if (bound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                        ldapUnbind(reqCtls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                        outStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                        sock.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        unpauseReader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    } catch (IOException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                            System.err.println("Connection: problem closing socket: " + ie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    if (!notifyParent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        LdapRequest ldr = pendingRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        while (ldr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                            ldr.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                            ldr = ldr.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    sock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                nparent = notifyParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            }
8182
1afdfa9621b6 7017486: Need synchronized access when flushing the LDAP request queue
vinnie
parents: 6312
diff changeset
   676
            if (nparent) {
1afdfa9621b6 7017486: Need synchronized access when flushing the LDAP request queue
vinnie
parents: 6312
diff changeset
   677
                LdapRequest ldr = pendingRequests;
1afdfa9621b6 7017486: Need synchronized access when flushing the LDAP request queue
vinnie
parents: 6312
diff changeset
   678
                while (ldr != null) {
1afdfa9621b6 7017486: Need synchronized access when flushing the LDAP request queue
vinnie
parents: 6312
diff changeset
   679
1afdfa9621b6 7017486: Need synchronized access when flushing the LDAP request queue
vinnie
parents: 6312
diff changeset
   680
                    synchronized (ldr) {
1afdfa9621b6 7017486: Need synchronized access when flushing the LDAP request queue
vinnie
parents: 6312
diff changeset
   681
                        ldr.notify();
1afdfa9621b6 7017486: Need synchronized access when flushing the LDAP request queue
vinnie
parents: 6312
diff changeset
   682
                        ldr = ldr.next;
1afdfa9621b6 7017486: Need synchronized access when flushing the LDAP request queue
vinnie
parents: 6312
diff changeset
   683
                    }
1afdfa9621b6 7017486: Need synchronized access when flushing the LDAP request queue
vinnie
parents: 6312
diff changeset
   684
                }
6126
bf66999384ac 6870947: 15 sec delay detecting "socket closed" condition when a TCP connection is reset by an LDAP server
xuelei
parents: 5506
diff changeset
   685
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
13157
2141a68fb2a7 7174887: Deadlock in jndi ldap connection cleanup
robm
parents: 10703
diff changeset
   687
        if (nparent) {
2141a68fb2a7 7174887: Deadlock in jndi ldap connection cleanup
robm
parents: 10703
diff changeset
   688
            parent.processConnectionClosure();
2141a68fb2a7 7174887: Deadlock in jndi ldap connection cleanup
robm
parents: 10703
diff changeset
   689
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    // Assume everything is "quiet"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    // "synchronize" might lead to deadlock so don't synchronize method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    // Use streamLock instead for synchronizing update to stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    synchronized public void replaceStreams(InputStream newIn, OutputStream newOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            System.err.println("Replacing " + inStream + " with: " + newIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            System.err.println("Replacing " + outStream + " with: " + newOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        inStream = newIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        // Cleanup old stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            outStream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        } catch (IOException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                System.err.println("Connection: cannot flush outstream: " + ie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        // Replace stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        outStream = newOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * Used by Connection thread to read inStream into a local variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * This ensures that there is no contention between the main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * and the Connection thread when the main thread updates inStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    synchronized private InputStream getInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return inStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    // Code for pausing/unpausing the reader thread ('worker')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * The main idea is to mark requests that need the reader thread to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * pause after getting the response. When the reader thread gets the response,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * it waits on a lock instead of returning to the read(). The next time a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * request is sent, the reader is automatically unblocked if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Note that the reader must be unblocked BEFORE the request is sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * Otherwise, there is a race condition where the request is sent and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * the reader thread might read the response and be unblocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * by writeRequest().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * This pause gives the main thread (StartTLS or SASL) an opportunity to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * update the reader's state (e.g., its streams) if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * The assumption is that the connection will remain quiet during this pause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * (i.e., no intervening requests being sent).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * For dealing with StartTLS close,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * when the read() exits either due to EOF or an exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * the reader thread checks whether there is a new stream to read from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * If so, then it reattempts the read. Otherwise, the EOF or exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * is processed and the reader thread terminates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * In a StartTLS close, the client first replaces the SSL IO streams with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * plain ones and then closes the SSL socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * If the reader thread attempts to read, or was reading, from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * the SSL socket (that is, it got to the read BEFORE replaceStreams()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * the SSL socket close will cause the reader thread to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * get an EOF/exception and reexamine the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * If the reader thread sees a new stream, it reattempts the read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * If the underlying socket is still alive, then the new read will succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * If the underlying socket has been closed also, then the new read will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * fail and the reader thread exits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * If the reader thread attempts to read, or was reading, from the plain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * socket (that is, it got to the read AFTER replaceStreams()), the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * SSL socket close will have no effect on the reader thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * The check for new stream is made only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * in the first attempt at reading a BER buffer; the reader should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * never be in midst of reading a buffer when a nonfatal close occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * If this occurs, then the connection is in an inconsistent state and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * the safest thing to do is to shut it down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    private Object pauseLock = new Object();  // lock for reader to wait on while paused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    private boolean paused = false;           // paused state of reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * Unpauses reader thread if it was paused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    private void unpauseReader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        synchronized (pauseLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            if (paused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    System.err.println("Unpausing reader; read from: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                                        inStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                paused = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                pauseLock.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * Pauses reader so that it stops reading from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * Reader blocks on pauseLock instead of read().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * MUST be called from within synchronized (pauseLock) clause.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    private void pauseReader() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            System.err.println("Pausing reader;  was reading from: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                                inStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        paused = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            while (paused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                pauseLock.wait(); // notified by unpauseReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            throw new InterruptedIOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    "Pause/unpause reader has problems.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    // The LDAP Binding thread. It does the mux/demux of multiple requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    // on the same TCP connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    ////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        byte inbuf[];   // Buffer for reading incoming bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        int inMsgId;    // Message id of incoming response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        int bytesread;  // Number of bytes in inbuf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        int br;         // Temp; number of bytes read from stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        int offset;     // Offset of where to store bytes in inbuf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        int seqlen;     // Length of ASN sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        int seqlenlen;  // Number of sequence length bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        boolean eos;    // End of stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        BerDecoder retBer;    // Decoder for ASN.1 BER data from inbuf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                try {
6312
ec857430700d 6921610: 1.6 update 17 and 18 throw java.lang.IndexOutOfBoundsException
weijun
parents: 6126
diff changeset
   838
                    // type and length (at most 128 octets for long form)
ec857430700d 6921610: 1.6 update 17 and 18 throw java.lang.IndexOutOfBoundsException
weijun
parents: 6126
diff changeset
   839
                    inbuf = new byte[129];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    seqlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    seqlenlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    in = getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    // check that it is the beginning of a sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    bytesread = in.read(inbuf, offset, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    if (bytesread < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                        if (in != getInputStream()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                            continue;   // a new stream to try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                            break; // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                    if (inbuf[offset++] != (Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    // get length of sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    bytesread = in.read(inbuf, offset, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                    if (bytesread < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                        break; // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    seqlen = inbuf[offset++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    // if high bit is on, length is encoded in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    // subsequent length bytes and the number of length bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                    // is equal to & 0x80 (i.e. length byte with high bit off).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                    if ((seqlen & 0x80) == 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                        seqlenlen = seqlen & 0x7f;  // number of length bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                        bytesread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                        eos = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                        // Read all length bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                        while (bytesread < seqlenlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                            br = in.read(inbuf, offset+bytesread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                                seqlenlen-bytesread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                            if (br < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                                eos = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                                break; // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                            bytesread += br;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                        // end-of-stream reached before length bytes are read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                        if (eos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                            break;  // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                        // Add contents of length bytes to determine length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                        seqlen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                        for( int i = 0; i < seqlenlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                            seqlen = (seqlen << 8) + (inbuf[offset+i] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                        offset += bytesread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                    // read in seqlen bytes
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   899
                    byte[] left = IOUtils.readFully(in, seqlen, false);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   900
                    inbuf = Arrays.copyOf(inbuf, offset + left.length);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   901
                    System.arraycopy(left, 0, inbuf, offset, left.length);
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2
diff changeset
   902
                    offset += left.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
if (dump > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
System.err.println("seqlen: " + seqlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
System.err.println("bufsize: " + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
System.err.println("bytesleft: " + bytesleft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
System.err.println("bytesread: " + bytesread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                        retBer = new BerDecoder(inbuf, 0, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                        if (traceFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                            Ber.dumpBER(traceFile, traceTagIn, inbuf, 0, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                        retBer.parseSeq(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                        inMsgId = retBer.parseInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                        retBer.reset(); // reset offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                        boolean needPause = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                        if (inMsgId == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                            // Unsolicited Notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                            parent.processUnsolicited(retBer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                            LdapRequest ldr = findRequest(inMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                            if (ldr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                                 * Grab pauseLock before making reply available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                                 * to ensure that reader goes into paused state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                                 * before writer can attempt to unpause reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                                synchronized (pauseLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                                    needPause = ldr.addReplyBer(retBer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                                    if (needPause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                                         * Go into paused state; release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                                         * pauseLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                                        pauseReader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                                    // else release pauseLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                                // System.err.println("Cannot find" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                                //              "LdapRequest for " + inMsgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                    } catch (Ber.DecodeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                        //System.err.println("Cannot parse Ber");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                } catch (IOException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                    if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                        System.err.println("Connection: Inside Caught " + ie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                        ie.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                    if (in != getInputStream()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                        // A new stream to try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                        // Go to top of loop and continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                            System.err.println("Connection: rethrowing " + ie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                        throw ie;  // rethrow exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                System.err.println("Connection: end-of-stream detected: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    + in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                System.err.println("Connection: Caught " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            closureReason = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            cleanup(null, true); // cleanup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            System.err.println("Connection: Thread Exiting");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    // This code must be uncommented to run the LdapAbandonTest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    /*public void sendSearchReqs(String dn, int numReqs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        String attrs[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        for(i = 1; i <= numReqs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            BerEncoder ber = new BerEncoder(2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                ber.encodeInt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                ber.beginSeq(LdapClient.LDAP_REQ_SEARCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                    ber.encodeString(dn == null ? "" : dn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                    ber.encodeInt(0, LdapClient.LBER_ENUMERATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                    ber.encodeInt(3, LdapClient.LBER_ENUMERATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                    ber.encodeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    ber.encodeInt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                    ber.encodeBoolean(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                    LdapClient.encodeFilter(ber, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                    ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                        ber.encodeStringArray(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                    ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            writeRequest(ber, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            //System.err.println("wrote request " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            //System.err.println("ldap.search: Caught " + ex + " building req");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    } */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
}