001/*
002 * Copyright 2009-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2020 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-2020 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.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048import com.unboundid.util.Validator;
049
050
051
052/**
053 * This class provides an {@link EntrySource} that will read entries from an
054 * LDIF file.
055 * <BR><BR>
056 * <H2>Example</H2>
057 * The following example demonstrates the process that may be used for iterating
058 * through all entries in an LDIF file using the entry source API:
059 * <PRE>
060 * LDIFEntrySource entrySource =
061 *      new LDIFEntrySource(new LDIFReader(pathToLDIFFile));
062 *
063 * int entriesRead = 0;
064 * int errorsEncountered = 0;
065 * try
066 * {
067 *   while (true)
068 *   {
069 *     try
070 *     {
071 *       Entry entry = entrySource.nextEntry();
072 *       if (entry == null)
073 *       {
074 *         // There are no more entries to be read.
075 *         break;
076 *       }
077 *       else
078 *       {
079 *         // Do something with the entry here.
080 *         entriesRead++;
081 *       }
082 *     }
083 *     catch (EntrySourceException e)
084 *     {
085 *       // Some kind of problem was encountered (e.g., a malformed entry
086 *       // found in the LDIF file, or an I/O error when trying to read).  See
087 *       // if we can continue reading entries.
088 *       errorsEncountered++;
089 *       if (! e.mayContinueReading())
090 *       {
091 *         break;
092 *       }
093 *     }
094 *   }
095 * }
096 * finally
097 * {
098 *   entrySource.close();
099 * }
100 * </PRE>
101 */
102@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
103public final class LDIFEntrySource
104       extends EntrySource
105{
106  // Indicates whether this entry source has been closed.
107  private final AtomicBoolean closed;
108
109  // The LDIF reader from which entries will be read.
110  private final LDIFReader ldifReader;
111
112
113
114  /**
115   * Creates a new LDAP entry source that will obtain entries from the provided
116   * LDIF reader.
117   *
118   * @param  ldifReader  The LDIF reader from which to read entries.  It must
119   *                     not be {@code null}.
120   */
121  public LDIFEntrySource(final LDIFReader ldifReader)
122  {
123    Validator.ensureNotNull(ldifReader);
124
125    this.ldifReader = ldifReader;
126
127    closed = new AtomicBoolean(false);
128  }
129
130
131
132  /**
133   * {@inheritDoc}
134   */
135  @Override()
136  public Entry nextEntry()
137         throws EntrySourceException
138  {
139    if (closed.get())
140    {
141      return null;
142    }
143
144    try
145    {
146      final Entry e = ldifReader.readEntry();
147      if (e == null)
148      {
149        close();
150      }
151
152      return e;
153    }
154    catch (final LDIFException le)
155    {
156      Debug.debugException(le);
157      if (le.mayContinueReading())
158      {
159        throw new EntrySourceException(true, le);
160      }
161      else
162      {
163        close();
164        throw new EntrySourceException(false, le);
165      }
166    }
167    catch (final Exception e)
168    {
169      Debug.debugException(e);
170      close();
171      throw new EntrySourceException(false, e);
172    }
173  }
174
175
176
177  /**
178   * {@inheritDoc}
179   */
180  @Override()
181  public void close()
182  {
183    if (closed.compareAndSet(false, true))
184    {
185      try
186      {
187        ldifReader.close();
188      }
189      catch (final Exception e)
190      {
191        Debug.debugException(e);
192      }
193    }
194  }
195}