1 /*
   2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.jndi.ldap;
  27 
  28 import java.io.IOException;
  29 import java.util.concurrent.BlockingQueue;
  30 import java.util.concurrent.LinkedBlockingQueue;
  31 import javax.naming.CommunicationException;
  32 import java.util.concurrent.TimeUnit;
  33 
  34 final class LdapRequest {
  35 
  36     private final static BerDecoder EOF = new BerDecoder(new byte[]{}, -1, 0);
  37 
  38     LdapRequest next;   // Set/read in synchronized Connection methods
  39     final int msgId;    // read-only
  40 
  41     private final BlockingQueue<BerDecoder> replies;
  42     private volatile boolean cancelled;
  43     private volatile boolean closed;
  44     private volatile boolean completed;
  45     private final boolean pauseAfterReceipt;
  46     
  47     LdapRequest(int msgId, boolean pause, int replyQueueCapacity) {
  48         this.msgId = msgId;
  49         this.pauseAfterReceipt = pause;
  50         if (replyQueueCapacity == -1) {
  51             this.replies = new LinkedBlockingQueue<>();
  52         } else {
  53             this.replies = new LinkedBlockingQueue<>(8 * replyQueueCapacity / 10);
  54         }
  55     }
  56 
  57     void cancel() {
  58         cancelled = true;
  59         replies.offer(EOF);
  60     }
  61 
  62     synchronized void close() {
  63         closed = true;
  64         replies.offer(EOF);
  65     }
  66 
  67     private boolean isClosed() {
  68         return closed && (replies.size() == 0 || replies.peek() == EOF);
  69      }
  70     
  71     synchronized boolean addReplyBer(BerDecoder ber) {
  72         // check the closed boolean value here as we don't want anything
  73         // to be added to the queue after close() has been called.
  74         if (cancelled || closed) {
  75             return false;
  76         }
  77 
  78         // peek at the BER buffer to check if it is a SearchResultDone PDU
  79         try {
  80             ber.parseSeq(null);
  81             ber.parseInt();
  82             completed = (ber.peekByte() == LdapClient.LDAP_REP_RESULT);
  83         } catch (IOException e) {
  84             // ignore
  85         }
  86         ber.reset();
  87 
  88         // Add a new reply to the queue of unprocessed replies.
  89         try {
  90             replies.put(ber);
  91         } catch (InterruptedException e) {
  92             // ignore
  93         }
  94 
  95         return pauseAfterReceipt;
  96     }
  97 
  98     BerDecoder getReplyBer(long millis) throws CommunicationException,
  99                                                InterruptedException {
 100         if (cancelled) {
 101             throw new CommunicationException("Request: " + msgId +
 102                 " cancelled");
 103         }
 104         if (isClosed()) {
 105             return null;
 106         }
 107 
 108         BerDecoder result = millis > 0 ?
 109                 replies.poll(millis, TimeUnit.MILLISECONDS) : replies.take();
 110 
 111         if (cancelled) {
 112             throw new CommunicationException("Request: " + msgId +
 113                 " cancelled");
 114         }
 115 
 116         return result == EOF ? null : result;
 117     }
 118 
 119     boolean hasSearchCompleted() {
 120         return completed;
 121     }
 122 }