| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 2001 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap; |
| 39 | |
| 40 | /** |
| 41 | * Manages search results, references and responses returned on one or |
| 42 | * more search requests |
| 43 | * |
| 44 | */ |
| 45 | public class LDAPSearchQueue extends LDAPMessageQueueImpl { |
| 46 | |
| 47 | static final long serialVersionUID = -7163312406176592277L; |
| 48 | |
| 49 | /** |
| 50 | * Constructs a search message queue |
| 51 | * |
| 52 | * @param asynchOp a boolean flag indicating whether the object is used |
| 53 | * for asynchronous LDAP operations |
| 54 | * @param cons LDAP search constraints |
| 55 | * @see org.ietf.ldap.LDAPAsynchronousConnection |
| 56 | */ |
| 57 | LDAPSearchQueue( boolean asynchOp, |
| 58 | LDAPSearchConstraints cons ) { |
| 59 | super( asynchOp ); |
| 60 | _constraints = cons; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Blocks until a search result, reference or response is available, |
| 65 | * or until all operations associated with the object have completed |
| 66 | * or been canceled. |
| 67 | * Wakes up the LDAPConnThread if the backlog limit has been reached. |
| 68 | * |
| 69 | * @return a search result, search reference, search response message, |
| 70 | * or null if there are no more outstanding requests. |
| 71 | * @exception LDAPException Network error exception |
| 72 | * @exception LDAPInterruptedException The invoking thread was interrupted |
| 73 | * @see LDAPResponse |
| 74 | * @see LDAPSearchResult |
| 75 | * @see LDAPSearchResultReference |
| 76 | */ |
| 77 | public LDAPMessage getResponse() throws LDAPException{ |
| 78 | LDAPMessage result = super.getResponse(); |
| 79 | |
| 80 | // Notify LDAPConnThread to wake up if backlog limit has been reached |
| 81 | if ( result instanceof LDAPSearchResult || |
| 82 | result instanceof LDAPSearchResultReference ) { |
| 83 | LDAPConnThread connThread = |
| 84 | getConnThread( result.getMessageID() ); |
| 85 | if ( connThread != null ) { |
| 86 | connThread.resultRetrieved(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Blocks until a response is available for a particular message ID, or |
| 95 | * until all operations associated with the message ID have completed or |
| 96 | * been canceled, and returns the response. If there is no outstanding |
| 97 | * operation for the message ID (or if it is zero or a negative number), |
| 98 | * IllegalArgumentException is thrown. |
| 99 | * |
| 100 | * @param msgid A particular message to query for responses available |
| 101 | * @return a response for an LDAP operation or null if there are no |
| 102 | * more outstanding requests. |
| 103 | * @exception LDAPException Network error exception |
| 104 | * @exception LDAPInterruptedException The invoking thread was interrupted |
| 105 | */ |
| 106 | |
| 107 | public LDAPMessage getResponse( int msgid ) |
| 108 | throws LDAPException { |
| 109 | LDAPMessage result = super.getResponse( msgid ); |
| 110 | |
| 111 | // Notify LDAPConnThread to wake up if backlog limit has been reached |
| 112 | if ( result instanceof LDAPSearchResult || |
| 113 | result instanceof LDAPSearchResultReference ) { |
| 114 | LDAPConnThread connThread = |
| 115 | getConnThread( result.getMessageID() ); |
| 116 | if ( connThread != null ) { |
| 117 | connThread.resultRetrieved(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return result; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Reports true if all results for a particular message ID have been |
| 126 | * received by the API implementation. That is the case if a |
| 127 | * searchResultDone response has been received by the SDK. There may |
| 128 | * still be messages queued in the object for retrieval by the |
| 129 | * application. If there is no outstanding operation for the message ID |
| 130 | * (or if it is zero or a negative number), IllegalArgumentException is |
| 131 | * thrown. |
| 132 | * |
| 133 | * @param msgid A particular message to query for completion |
| 134 | * @return true if all results for a particular message ID have been |
| 135 | * received |
| 136 | */ |
| 137 | public synchronized boolean isComplete( int msgid ) throws LDAPException { |
| 138 | |
| 139 | boolean OK = false; |
| 140 | for ( int i = (_messageQueue.size()-1); i >= 0; i-- ) { |
| 141 | LDAPMessage msg = _messageQueue.get(i); |
| 142 | if ( msg.getMessageID() == msgid ) { |
| 143 | OK = true; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | if ( !OK ) { |
| 148 | throw new IllegalArgumentException( "Invalid msg ID: " + msgid ); |
| 149 | } |
| 150 | |
| 151 | // Search an instance of LDAPResponse |
| 152 | for ( int i = _messageQueue.size()-1; i >= 0; i-- ) { |
| 153 | LDAPMessage msg = _messageQueue.get(i); |
| 154 | if ( msg instanceof LDAPResponse ) { |
| 155 | return true; |
| 156 | } |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Gets the key of the cache entry |
| 163 | * |
| 164 | * @return the key of the cache entry |
| 165 | */ |
| 166 | Long getKey() { |
| 167 | return _key; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the search constraints used to create this object. |
| 172 | * |
| 173 | * @return the search constraints used to create this object. |
| 174 | */ |
| 175 | LDAPSearchConstraints getSearchConstraints() { |
| 176 | return _constraints; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Resets the state of this object so it can be recycled |
| 181 | * Used by LDAPConnection synchronous operations. |
| 182 | */ |
| 183 | void reset() { |
| 184 | super.reset(); |
| 185 | _constraints = null; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Sets the key of the cache entry. The queue needs to know this value |
| 190 | * when the results get processed. After the results have been |
| 191 | * saved in the vector, then the key and a vector of results are put in |
| 192 | * the cache. |
| 193 | * |
| 194 | * @param key the key of the cache entry |
| 195 | */ |
| 196 | void setKey( Long key ) { |
| 197 | _key = key; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Sets search constraints |
| 202 | * |
| 203 | * @param cons LDAP search constraints |
| 204 | */ |
| 205 | void setSearchConstraints( LDAPSearchConstraints cons ) { |
| 206 | _constraints = cons; |
| 207 | } |
| 208 | |
| 209 | // this instance variable is only for cache purposes |
| 210 | private Long _key = null; |
| 211 | private LDAPSearchConstraints _constraints; |
| 212 | } |
| 213 |