001/*
002 * Copyright 2009-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2018 Ping Identity Corporation
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 */
021package com.unboundid.ldap.sdk.unboundidds.logs;
022
023
024
025import java.io.BufferedReader;
026import java.io.Closeable;
027import java.io.File;
028import java.io.FileReader;
029import java.io.IOException;
030import java.io.Reader;
031
032import com.unboundid.util.NotMutable;
033import com.unboundid.util.ThreadSafety;
034import com.unboundid.util.ThreadSafetyLevel;
035
036
037
038/**
039 * This class provides a mechanism for reading message from a Directory Server
040 * error log.
041 * <BR>
042 * <BLOCKQUOTE>
043 *   <B>NOTE:</B>  This class, and other classes within the
044 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
045 *   supported for use against Ping Identity, UnboundID, and Alcatel-Lucent 8661
046 *   server products.  These classes provide support for proprietary
047 *   functionality or for external specifications that are not considered stable
048 *   or mature enough to be guaranteed to work in an interoperable way with
049 *   other types of LDAP servers.
050 * </BLOCKQUOTE>
051 */
052@NotMutable()
053@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
054public final class ErrorLogReader
055       implements Closeable
056{
057  // The reader used to read the contents of the log file.
058  private final BufferedReader reader;
059
060
061
062  /**
063   * Creates a new error log reader that will read messages from the specified
064   * log file.
065   *
066   * @param  path  The path of the log file to read.
067   *
068   * @throws  IOException  If a problem occurs while opening the file for
069   *                       reading.
070   */
071  public ErrorLogReader(final String path)
072         throws IOException
073  {
074    reader = new BufferedReader(new FileReader(path));
075  }
076
077
078
079  /**
080   * Creates a new error log reader that will read messages from the specified
081   * log file.
082   *
083   * @param  file  The log file to read.
084   *
085   * @throws  IOException  If a problem occurs while opening the file for
086   *                       reading.
087   */
088  public ErrorLogReader(final File file)
089         throws IOException
090  {
091    reader = new BufferedReader(new FileReader(file));
092  }
093
094
095
096  /**
097   * Creates a new error log reader that will read messages using the provided
098   * {@code Reader} object.
099   *
100   * @param  reader  The reader to use to read log messages.
101   */
102  public ErrorLogReader(final Reader reader)
103  {
104    if (reader instanceof BufferedReader)
105    {
106      this.reader = (BufferedReader) reader;
107    }
108    else
109    {
110      this.reader = new BufferedReader(reader);
111    }
112  }
113
114
115
116  /**
117   * Reads the next error log message from the log file.
118   *
119   * @return  The error log message read from the log file, or {@code null} if
120   *          there are no more messages to be read.
121   *
122   * @throws  IOException  If an error occurs while trying to read from the
123   *                       file.
124   *
125   * @throws  LogException  If an error occurs while trying to parse the log
126   *                        message.
127   */
128  public ErrorLogMessage read()
129         throws IOException, LogException
130  {
131    while (true)
132    {
133      final String line = reader.readLine();
134      if (line == null)
135      {
136        return null;
137      }
138
139      if ((line.length() == 0) || (line.charAt(0) == '#'))
140      {
141        continue;
142      }
143
144      return new ErrorLogMessage(line);
145    }
146  }
147
148
149
150  /**
151   * Closes this error log reader.
152   *
153   * @throws  IOException  If a problem occurs while closing the reader.
154   */
155  public void close()
156         throws IOException
157  {
158    reader.close();
159  }
160}