jdk/src/share/classes/com/sun/jndi/ldap/LdapRequest.java
author prappo
Fri, 01 Aug 2014 22:32:51 +0100
changeset 25808 e113d0a0fde0
parent 8564 d99f879a35ab
permissions -rw-r--r--
8054158: Fix typos in JNDI-related packages Reviewed-by: rriggs, vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jndi.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    29
import java.util.concurrent.BlockingQueue;
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    30
import java.util.concurrent.LinkedBlockingQueue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.naming.CommunicationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
final class LdapRequest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    LdapRequest next;   // Set/read in synchronized Connection methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    int msgId;          // read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private int gotten = 0;
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    39
    private BlockingQueue<BerDecoder> replies;
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    40
    private int highWatermark = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private boolean cancelled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private boolean pauseAfterReceipt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private boolean completed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    LdapRequest(int msgId, boolean pause) {
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    46
        this(msgId, pause, -1);
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    47
    }
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    48
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    49
    LdapRequest(int msgId, boolean pause, int replyQueueCapacity) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        this.msgId = msgId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        this.pauseAfterReceipt = pause;
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    52
        if (replyQueueCapacity == -1) {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    53
            this.replies = new LinkedBlockingQueue<BerDecoder>();
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    54
        } else {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    55
            this.replies =
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    56
                new LinkedBlockingQueue<BerDecoder>(replyQueueCapacity);
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    57
            highWatermark = (replyQueueCapacity * 80) / 100; // 80% capacity
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    58
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    synchronized void cancel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        cancelled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // Unblock reader of pending request
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 8564
diff changeset
    65
        // Should only ever have at most one waiter
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    synchronized boolean addReplyBer(BerDecoder ber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (cancelled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    73
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    74
        // Add a new reply to the queue of unprocessed replies.
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    75
        try {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    76
            replies.put(ber);
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    77
        } catch (InterruptedException e) {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    78
            // ignore
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    79
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // peek at the BER buffer to check if it is a SearchResultDone PDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            ber.parseSeq(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            ber.parseInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            completed = (ber.peekByte() == LdapClient.LDAP_REP_RESULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        ber.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        notify(); // notify anyone waiting for reply
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    92
        /*
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    93
         * If a queue capacity has been set then trigger a pause when the
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    94
         * queue has filled to 80% capacity. Later, when the queue has drained
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    95
         * then the reader gets unpaused.
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    96
         */
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    97
        if (highWatermark != -1 && replies.size() >= highWatermark) {
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    98
            return true; // trigger the pause
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
    99
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return pauseAfterReceipt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    synchronized BerDecoder getReplyBer() throws CommunicationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (cancelled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new CommunicationException("Request: " + msgId +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                " cancelled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
8564
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   109
        /*
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   110
         * Remove a reply if the queue is not empty.
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   111
         * poll returns null if queue is empty.
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   112
         */
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   113
        BerDecoder reply = replies.poll();
d99f879a35ab 6750362: Very large LDAP requests throw a OOM on LDAP servers which aren't aware of Paged Results Controls
coffeys
parents: 5506
diff changeset
   114
        return reply;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    synchronized boolean hasSearchCompleted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return completed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}