src/share/classes/com/sun/jndi/ldap/LdapRequest.java

changeset 14206
ab2e99db6702
parent 13707
b4522ab88a12
child 14222
5a272e10d7e7
equal deleted inserted replaced
14205:9deaed6c4a0f 14206:ab2e99db6702
1 /* 1 /*
2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
27 27
28 import java.io.IOException; 28 import java.io.IOException;
29 import java.util.concurrent.BlockingQueue; 29 import java.util.concurrent.BlockingQueue;
30 import java.util.concurrent.LinkedBlockingQueue; 30 import java.util.concurrent.LinkedBlockingQueue;
31 import javax.naming.CommunicationException; 31 import javax.naming.CommunicationException;
32 import javax.naming.NamingException;
32 import java.util.concurrent.TimeUnit; 33 import java.util.concurrent.TimeUnit;
33 34
34 final class LdapRequest { 35 final class LdapRequest {
35 36
36 private final static BerDecoder EOF = new BerDecoder(new byte[]{}, -1, 0); 37 private final static BerDecoder EOF = new BerDecoder(new byte[]{}, -1, 0);
38 private final static String CLOSE_MSG = "LDAP connection has been closed";
39 private final static String TIMEOUT_MSG_FMT = "LDAP response read timed out, timeout used: %d ms.";
37 40
38 LdapRequest next; // Set/read in synchronized Connection methods 41 LdapRequest next; // Set/read in synchronized Connection methods
39 final int msgId; // read-only 42 final int msgId; // read-only
40 43
41 private final BlockingQueue<BerDecoder> replies; 44 private final BlockingQueue<BerDecoder> replies;
93 } 96 }
94 97
95 return pauseAfterReceipt; 98 return pauseAfterReceipt;
96 } 99 }
97 100
98 BerDecoder getReplyBer(long millis) throws CommunicationException, 101 /**
102 * Read reply BER
103 * @param millis timeout, infinite if the value is negative
104 * @return BerDecoder if reply was read successfully
105 * @throws CommunicationException request has been canceled and request does not need to be abandoned
106 * @throws NamingException request has been closed or timed out. Request does need to be abandoned
107 * @throws InterruptedException LDAP operation has been interrupted
108 */
109 BerDecoder getReplyBer(long millis) throws NamingException,
99 InterruptedException { 110 InterruptedException {
100 if (cancelled) { 111 if (cancelled) {
101 throw new CommunicationException("Request: " + msgId + 112 throw new CommunicationException("Request: " + msgId +
102 " cancelled"); 113 " cancelled");
103 } 114 }
104 if (isClosed()) { 115 if (isClosed()) {
105 return null; 116 throw new NamingException(CLOSE_MSG);
106 } 117 }
107 118
108 BerDecoder result = millis > 0 ? 119 BerDecoder result = millis > 0 ?
109 replies.poll(millis, TimeUnit.MILLISECONDS) : replies.take(); 120 replies.poll(millis, TimeUnit.MILLISECONDS) : replies.take();
110 121
111 if (cancelled) { 122 if (cancelled) {
112 throw new CommunicationException("Request: " + msgId + 123 throw new CommunicationException("Request: " + msgId +
113 " cancelled"); 124 " cancelled");
114 } 125 }
115 126
116 return result == EOF ? null : result; 127 // poll from 'replies' blocking queue ended-up with timeout
128 if (result == null) {
129 throw new NamingException(String.format(TIMEOUT_MSG_FMT, millis));
130 }
131 // Unexpected EOF can be caused by connection closure or cancellation
132 if (result == EOF) {
133 throw new NamingException(CLOSE_MSG);
134 }
135 return result;
117 } 136 }
118 137
119 boolean hasSearchCompleted() { 138 boolean hasSearchCompleted() {
120 return completed; 139 return completed;
121 } 140 }

mercurial