001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2024 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2009-2024 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldif;
037
038
039
040import java.util.concurrent.atomic.AtomicBoolean;
041
042import com.unboundid.ldap.sdk.Entry;
043import com.unboundid.ldap.sdk.EntrySource;
044import com.unboundid.ldap.sdk.EntrySourceException;
045import com.unboundid.util.Debug;
046import com.unboundid.util.NotNull;
047import com.unboundid.util.Nullable;
048import com.unboundid.util.ThreadSafety;
049import com.unboundid.util.ThreadSafetyLevel;
050import com.unboundid.util.Validator;
051
052
053
054/**
055 * This class provides an {@link EntrySource} that will read entries from an
056 * LDIF file.
057 * <BR><BR>
058 * <H2>Example</H2>
059 * The following example demonstrates the process that may be used for iterating
060 * through all entries in an LDIF file using the entry source API:
061 * <PRE>
062 * LDIFEntrySource entrySource =
063 *      new LDIFEntrySource(new LDIFReader(pathToLDIFFile));
064 *
065 * int entriesRead = 0;
066 * int errorsEncountered = 0;
067 * try
068 * {
069 *   while (true)
070 *   {
071 *     try
072 *     {
073 *       Entry entry = entrySource.nextEntry();
074 *       if (entry == null)
075 *       {
076 *         // There are no more entries to be read.
077 *         break;
078 *       }
079 *       else
080 *       {
081 *         // Do something with the entry here.
082 *         entriesRead++;
083 *       }
084 *     }
085 *     catch (EntrySourceException e)
086 *     {
087 *       // Some kind of problem was encountered (e.g., a malformed entry
088 *       // found in the LDIF file, or an I/O error when trying to read).  See
089 *       // if we can continue reading entries.
090 *       errorsEncountered++;
091 *       if (! e.mayContinueReading())
092 *       {
093 *         break;
094 *       }
095 *     }
096 *   }
097 * }
098 * finally
099 * {
100 *   entrySource.close();
101 * }
102 * </PRE>
103 */
104@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
105public final class LDIFEntrySource
106       extends EntrySource
107{
108  // Indicates whether this entry source has been closed.
109  @NotNull private final AtomicBoolean closed;
110
111  // The LDIF reader from which entries will be read.
112  @NotNull private final LDIFReader ldifReader;
113
114
115
116  /**
117   * Creates a new LDAP entry source that will obtain entries from the provided
118   * LDIF reader.
119   *
120   * @param  ldifReader  The LDIF reader from which to read entries.  It must
121   *                     not be {@code null}.
122   */
123  public LDIFEntrySource(@NotNull final LDIFReader ldifReader)
124  {
125    Validator.ensureNotNull(ldifReader);
126
127    this.ldifReader = ldifReader;
128
129    closed = new AtomicBoolean(false);
130  }
131
132
133
134  /**
135   * {@inheritDoc}
136   */
137  @Override()
138  @Nullable()
139  public Entry nextEntry()
140         throws EntrySourceException
141  {
142    if (closed.get())
143    {
144      return null;
145    }
146
147    try
148    {
149      final Entry e = ldifReader.readEntry();
150      if (e == null)
151      {
152        close();
153      }
154
155      return e;
156    }
157    catch (final LDIFException le)
158    {
159      Debug.debugException(le);
160      if (le.mayContinueReading())
161      {
162        throw new EntrySourceException(true, le);
163      }
164      else
165      {
166        close();
167        throw new EntrySourceException(false, le);
168      }
169    }
170    catch (final Exception e)
171    {
172      Debug.debugException(e);
173      close();
174      throw new EntrySourceException(false, e);
175    }
176  }
177
178
179
180  /**
181   * {@inheritDoc}
182   */
183  @Override()
184  public void close()
185  {
186    if (closed.compareAndSet(false, true))
187    {
188      try
189      {
190        ldifReader.close();
191      }
192      catch (final Exception e)
193      {
194        Debug.debugException(e);
195      }
196    }
197  }
198}