@ThreadSafety(level=NOT_THREADSAFE) public final class LDAPEntrySource extends EntrySource implements AsyncSearchResultListener
EntrySource
that will read entries matching a
given set of search criteria from an LDAP directory server. It may
optionally close the associated connection after all entries have been read.
close()
method) and
the caller intends to stop iterating through the results.person
object class using the LDAP
entry source API:
SearchRequest searchRequest = new SearchRequest("dc=example,dc=com", SearchScope.SUB, Filter.createEqualityFilter("objectClass", "person")); LDAPEntrySource entrySource = new LDAPEntrySource(connection, searchRequest, false); int entriesRead = 0; int referencesRead = 0; int exceptionsCaught = 0; try { while (true) { try { Entry entry = entrySource.nextEntry(); if (entry == null) { // There are no more entries to be read. break; } else { // Do something with the entry here. entriesRead++; } } catch (SearchResultReferenceEntrySourceException e) { // The directory server returned a search result reference. SearchResultReference searchReference = e.getSearchReference(); referencesRead++; } catch (EntrySourceException e) { // Some kind of problem was encountered (e.g., the connection is no // longer valid). See if we can continue reading entries. exceptionsCaught++; if (! e.mayContinueReading()) { break; } } } } finally { entrySource.close(); }
Constructor and Description |
---|
LDAPEntrySource(LDAPConnection connection,
SearchRequest searchRequest,
boolean closeConnection)
Creates a new LDAP entry source with the provided information.
|
LDAPEntrySource(LDAPConnection connection,
SearchRequest searchRequest,
boolean closeConnection,
int queueSize)
Creates a new LDAP entry source with the provided information.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Indicates that this entry source will no longer be needed and any resources
associated with it may be closed.
|
SearchResult |
getSearchResult()
Retrieves the search result for the search operation, if available.
|
Entry |
nextEntry()
Retrieves the next entry from the entry source, if there is at least one
remaining entry.
|
void |
searchEntryReturned(SearchResultEntry searchEntry)
Indicates that the provided search result entry has been returned by the
server and may be processed by this search result listener.
|
void |
searchReferenceReturned(SearchResultReference searchReference)
Indicates that the provided search result reference has been returned by
the server and may be processed by this search result listener.
|
void |
searchResultReceived(AsyncRequestID requestID,
SearchResult searchResult)
Indicates that the provided search result has been received in response to
an asynchronous search operation.
|
public LDAPEntrySource(@NotNull LDAPConnection connection, @NotNull SearchRequest searchRequest, boolean closeConnection) throws LDAPException
connection
- The connection to the directory server from which
the entries will be read. It must not be
null
.searchRequest
- The search request that will be used to identify
which entries should be returned. It must not be
null
, and it must not be configured with a
SearchResultListener
.closeConnection
- Indicates whether the provided connection should
be closed whenever all of the entries have been
read, or if the close()
method is called.LDAPException
- If there is a problem with the provided search
request or when trying to communicate with the
directory server over the provided connection.public LDAPEntrySource(@NotNull LDAPConnection connection, @NotNull SearchRequest searchRequest, boolean closeConnection, int queueSize) throws LDAPException
connection
- The connection to the directory server from which
the entries will be read. It must not be
null
.searchRequest
- The search request that will be used to identify
which entries should be returned. It must not be
null
, and it must not be configured with a
SearchResultListener
.closeConnection
- Indicates whether the provided connection should
be closed whenever all of the entries have been
read, or if the close()
method is called.queueSize
- The size of the internal queue used to hold search
result entries until they can be consumed by the
nextEntry()
method. The value must be
greater than zero.LDAPException
- If there is a problem with the provided search
request or when trying to communicate with the
directory server over the provided connection.@Nullable public Entry nextEntry() throws EntrySourceException
nextEntry
in class EntrySource
null
if there are
no more entries to retrieve.EntrySourceException
- If a problem occurs while attempting to read
the next entry from the entry source.public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class EntrySource
@Nullable public SearchResult getSearchResult()
null
return value from the nextEntry()
method).null
if it
is not available (e.g., because the search has not yet completed).@InternalUseOnly public void searchEntryReturned(@NotNull SearchResultEntry searchEntry)
searchEntryReturned
in interface SearchResultListener
searchEntry
- The search result entry that has been returned by the
server.@InternalUseOnly public void searchReferenceReturned(@NotNull SearchResultReference searchReference)
searchReferenceReturned
in interface SearchResultListener
searchReference
- The search result reference that has been returned
by the server.@InternalUseOnly public void searchResultReceived(@NotNull AsyncRequestID requestID, @NotNull SearchResult searchResult)
searchResultReceived
in interface AsyncSearchResultListener
requestID
- The async request ID of the request for which the
response was received.searchResult
- The search result that has been received.