0N/A/*
3667N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.jndi.ldap;
0N/A
0N/Aimport java.io.IOException;
3667N/Aimport java.util.concurrent.BlockingQueue;
3667N/Aimport java.util.concurrent.LinkedBlockingQueue;
0N/Aimport javax.naming.CommunicationException;
0N/A
0N/Afinal class LdapRequest {
0N/A
0N/A LdapRequest next; // Set/read in synchronized Connection methods
0N/A int msgId; // read-only
0N/A
0N/A private int gotten = 0;
3667N/A private BlockingQueue<BerDecoder> replies;
3667N/A private int highWatermark = -1;
0N/A private boolean cancelled = false;
0N/A private boolean pauseAfterReceipt = false;
0N/A private boolean completed = false;
0N/A
0N/A LdapRequest(int msgId, boolean pause) {
3667N/A this(msgId, pause, -1);
3667N/A }
3667N/A
3667N/A LdapRequest(int msgId, boolean pause, int replyQueueCapacity) {
0N/A this.msgId = msgId;
0N/A this.pauseAfterReceipt = pause;
3667N/A if (replyQueueCapacity == -1) {
3667N/A this.replies = new LinkedBlockingQueue<BerDecoder>();
3667N/A } else {
3667N/A this.replies =
3667N/A new LinkedBlockingQueue<BerDecoder>(replyQueueCapacity);
3667N/A highWatermark = (replyQueueCapacity * 80) / 100; // 80% capacity
3667N/A }
0N/A }
0N/A
0N/A synchronized void cancel() {
0N/A cancelled = true;
0N/A
0N/A // Unblock reader of pending request
0N/A // Should only ever have atmost one waiter
0N/A notify();
0N/A }
0N/A
0N/A synchronized boolean addReplyBer(BerDecoder ber) {
0N/A if (cancelled) {
0N/A return false;
0N/A }
3667N/A
3667N/A // Add a new reply to the queue of unprocessed replies.
3667N/A try {
3667N/A replies.put(ber);
3667N/A } catch (InterruptedException e) {
3667N/A // ignore
3667N/A }
0N/A
0N/A // peek at the BER buffer to check if it is a SearchResultDone PDU
0N/A try {
0N/A ber.parseSeq(null);
0N/A ber.parseInt();
0N/A completed = (ber.peekByte() == LdapClient.LDAP_REP_RESULT);
0N/A } catch (IOException e) {
0N/A // ignore
0N/A }
0N/A ber.reset();
0N/A
0N/A notify(); // notify anyone waiting for reply
3667N/A /*
3667N/A * If a queue capacity has been set then trigger a pause when the
3667N/A * queue has filled to 80% capacity. Later, when the queue has drained
3667N/A * then the reader gets unpaused.
3667N/A */
3667N/A if (highWatermark != -1 && replies.size() >= highWatermark) {
3667N/A return true; // trigger the pause
3667N/A }
0N/A return pauseAfterReceipt;
0N/A }
0N/A
0N/A synchronized BerDecoder getReplyBer() throws CommunicationException {
0N/A if (cancelled) {
0N/A throw new CommunicationException("Request: " + msgId +
0N/A " cancelled");
0N/A }
0N/A
3667N/A /*
3667N/A * Remove a reply if the queue is not empty.
3667N/A * poll returns null if queue is empty.
3667N/A */
3667N/A BerDecoder reply = replies.poll();
3667N/A return reply;
0N/A }
0N/A
0N/A synchronized boolean hasSearchCompleted() {
0N/A return completed;
0N/A }
0N/A}