001    /*
002     * Copyright 2009-2013 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2009-2013 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     *   try
058     *   {
059     *     while (true)
060     *     {
061     *       try
062     *       {
063     *         Entry entry = entrySource.nextEntry();
064     *         if (entry == null)
065     *         {
066     *           // There are no more entries to be read.
067     *           break;
068     *         }
069     *         else
070     *         {
071     *           // Do something with the entry here.
072     *         }
073     *       }
074     *       catch (EntrySourceException e)
075     *       {
076     *         // Some kind of problem was encountered (e.g., a malformed entry
077     *         // found in an LDIF file, or a referral returned from a directory).
078     *         // See if we can continue reading entries.
079     *         if (! e.mayContinueReading())
080     *         {
081     *           break;
082     *         }
083     *       }
084     *     }
085     *   }
086     *   finally
087     *   {
088     *     entrySource.close();
089     *   }
090     * </PRE>
091     */
092    @NotExtensible()
093    @ThreadSafety(level=ThreadSafetyLevel.INTERFACE_NOT_THREADSAFE)
094    public abstract class EntrySource
095    {
096      /**
097       * Retrieves the next entry from the entry source, if there is at least one
098       * remaining entry.  This method may block if no entries are immediately
099       * available.
100       *
101       * @return  The next entry from the entry source, or {@code null} if there are
102       *          no more entries to retrieve..
103       *
104       * @throws  EntrySourceException  If a problem occurs while attempting to read
105       *                                the next entry from the entry source.
106       */
107      public abstract Entry nextEntry()
108             throws EntrySourceException;
109    
110    
111    
112      /**
113       * Indicates that this entry source will no longer be needed and any resources
114       * associated with it may be closed.  This method MUST be called if the entry
115       * source is no longer needed before all entries have been read.  It MAY be
116       * called after all entries have been read with no ill effects, but this is
117       * not necessary as the entry source will have already been closed after all
118       * entries have been read.
119       */
120      public abstract void close();
121    }