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