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