001 /*
002 * Copyright 2009-2014 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2009-2014 UnboundID Corp.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021 package com.unboundid.ldap.sdk;
022
023
024
025 import com.unboundid.util.NotExtensible;
026 import com.unboundid.util.ThreadSafety;
027 import com.unboundid.util.ThreadSafetyLevel;
028
029
030
031 /**
032 * This class defines an API that may be implemented by a class that provides
033 * access to a sequence of entries, one entry at a time (e.g., entries read from
034 * an LDIF file, or returned as part of an LDAP search). It provides a
035 * convenient way to operate on a set of entries without regard for the source
036 * of those entries. Implementations currently available include the
037 * {@link LDAPEntrySource} class, which can be used to iterate across entries
038 * returned from a directory server in response to a search request, and the
039 * {@link com.unboundid.ldif.LDIFEntrySource} class, which can be used to
040 * iterate across entries in an LDIF file.
041 * <BR><BR>
042 * Note that the {@link #close} method MUST be called if the entry source is to
043 * be discarded before guaranteeing that all entries have been read. The
044 * {@code close} method may be called after all entries have been read, but it
045 * is not required. All entry source implementations MUST ensure that all
046 * resources are properly released if the caller has read through all entries,
047 * or if an error occurs that prevents the caller from continuing to read
048 * through the entries (i.e., if {@link #nextEntry} throws an
049 * {@link EntrySourceException} and the
050 * {@link EntrySourceException#mayContinueReading()} method returns
051 * {@code false}).
052 * <BR><BR>
053 * <H2>Example</H2>
054 * The following example demonstrates the process that may be used for iterating
055 * across the entries provided by an entry source:
056 * <PRE>
057 * LDIFReader ldifReader = new LDIFReader(ldifFilePath);
058 * EntrySource entrySource = new LDIFEntrySource(ldifReader);
059 *
060 * int entriesRead = 0;
061 * int exceptionsCaught = 0;
062 * try
063 * {
064 * while (true)
065 * {
066 * try
067 * {
068 * Entry entry = entrySource.nextEntry();
069 * if (entry == null)
070 * {
071 * // There are no more entries to be read.
072 * break;
073 * }
074 * else
075 * {
076 * // Do something with the entry here.
077 * entriesRead++;
078 * }
079 * }
080 * catch (EntrySourceException e)
081 * {
082 * // Some kind of problem was encountered (e.g., a malformed entry
083 * // found in an LDIF file, or a referral returned from a directory).
084 * // See if we can continue reading entries.
085 * exceptionsCaught++;
086 * if (! e.mayContinueReading())
087 * {
088 * break;
089 * }
090 * }
091 * }
092 * }
093 * finally
094 * {
095 * entrySource.close();
096 * }
097 * </PRE>
098 */
099 @NotExtensible()
100 @ThreadSafety(level=ThreadSafetyLevel.INTERFACE_NOT_THREADSAFE)
101 public abstract class EntrySource
102 {
103 /**
104 * Retrieves the next entry from the entry source, if there is at least one
105 * remaining entry. This method may block if no entries are immediately
106 * available.
107 *
108 * @return The next entry from the entry source, or {@code null} if there are
109 * no more entries to retrieve..
110 *
111 * @throws EntrySourceException If a problem occurs while attempting to read
112 * the next entry from the entry source.
113 */
114 public abstract Entry nextEntry()
115 throws EntrySourceException;
116
117
118
119 /**
120 * Indicates that this entry source will no longer be needed and any resources
121 * associated with it may be closed. This method MUST be called if the entry
122 * source is no longer needed before all entries have been read. It MAY be
123 * called after all entries have been read with no ill effects, but this is
124 * not necessary as the entry source will have already been closed after all
125 * entries have been read.
126 */
127 public abstract void close();
128 }