| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.logging; |
|---|
| 34 | |
|---|
| 35 | import java.io.BufferedReader; |
|---|
| 36 | import java.io.File; |
|---|
| 37 | import java.io.FileReader; |
|---|
| 38 | import java.io.IOException; |
|---|
| 39 | import java.util.ArrayList; |
|---|
| 40 | import java.util.Iterator; |
|---|
| 41 | import java.util.List; |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * This implements a reader reading from log files. It can be used for reading |
|---|
| 45 | * the next entry from the log file, which matches the filter criteria or |
|---|
| 46 | * skipping a number of log file entries. |
|---|
| 47 | * |
|---|
| 48 | */ |
|---|
| 49 | public class LogReader |
|---|
| 50 | { |
|---|
| 51 | private final List mBufferedLines = new ArrayList(); |
|---|
| 52 | private final BufferedReader mReader; |
|---|
| 53 | private final File mFile; |
|---|
| 54 | private final List mFilters = new ArrayList(); |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Creates a new LogReader for reading from the supplied file. |
|---|
| 58 | * |
|---|
| 59 | * @param fileName The name of the file to read. |
|---|
| 60 | * |
|---|
| 61 | * @throws InstantiationException in case there is an error opening the file |
|---|
| 62 | * for reading. |
|---|
| 63 | */ |
|---|
| 64 | LogReader (final String fileName) |
|---|
| 65 | throws InstantiationException |
|---|
| 66 | { |
|---|
| 67 | try |
|---|
| 68 | { |
|---|
| 69 | mFile = new File(fileName); |
|---|
| 70 | mReader = new BufferedReader(new FileReader(mFile)); |
|---|
| 71 | } |
|---|
| 72 | catch (Exception ex) |
|---|
| 73 | { |
|---|
| 74 | final InstantiationException iex = new InstantiationException( |
|---|
| 75 | "Cannot install LogReader for file '" + fileName + "'."); |
|---|
| 76 | iex.initCause(ex); |
|---|
| 77 | throw iex; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Installs a new filter for filtering log file entries. |
|---|
| 84 | * |
|---|
| 85 | * @param filter The filter to add to the already stored filters. |
|---|
| 86 | */ |
|---|
| 87 | void addFilter (final Filter filter) |
|---|
| 88 | { |
|---|
| 89 | mFilters.add(filter); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Gets the next LogFileEntry from the log file, which passes all installed |
|---|
| 94 | * filters. If end of file is reached before an entry has been found matching |
|---|
| 95 | * all criteria, this returns null. |
|---|
| 96 | * Each LogFileEntry instance being returned by this should be released if it |
|---|
| 97 | * is not needed anymore. |
|---|
| 98 | * |
|---|
| 99 | * @return the next LogFileEnbtry passing all filters or null if no such |
|---|
| 100 | * available. |
|---|
| 101 | * @throws LoggingException if an error occurs. |
|---|
| 102 | */ |
|---|
| 103 | LogFileEntry readLogFileEntry () |
|---|
| 104 | throws LoggingException |
|---|
| 105 | { |
|---|
| 106 | LogFileEntry rc = null; |
|---|
| 107 | final LogFileEntry currentEntry = LogFileEntry.getLogFileEntry(); |
|---|
| 108 | int numBufferedLines = mBufferedLines.size(); |
|---|
| 109 | int bufferedLine = 0; |
|---|
| 110 | boolean readBuffered; |
|---|
| 111 | boolean consumedLine = false; |
|---|
| 112 | |
|---|
| 113 | while (((bufferedLine < numBufferedLines) || available()) && (rc == null)) |
|---|
| 114 | { |
|---|
| 115 | final StringBuffer currentLine; |
|---|
| 116 | if (bufferedLine < numBufferedLines) |
|---|
| 117 | { |
|---|
| 118 | currentLine = (StringBuffer) mBufferedLines.get(bufferedLine++); |
|---|
| 119 | readBuffered = true; |
|---|
| 120 | } |
|---|
| 121 | else |
|---|
| 122 | { |
|---|
| 123 | currentLine = readLine(); |
|---|
| 124 | readBuffered = false; |
|---|
| 125 | } |
|---|
| 126 | if (currentLine != null) |
|---|
| 127 | { |
|---|
| 128 | try |
|---|
| 129 | { |
|---|
| 130 | if (currentEntry.addLogLine(currentLine)) |
|---|
| 131 | { |
|---|
| 132 | if (! readBuffered) |
|---|
| 133 | { |
|---|
| 134 | mBufferedLines.add(currentLine); |
|---|
| 135 | } |
|---|
| 136 | consumedLine = true; |
|---|
| 137 | } |
|---|
| 138 | else |
|---|
| 139 | { |
|---|
| 140 | mBufferedLines.clear(); |
|---|
| 141 | mBufferedLines.add(currentLine); |
|---|
| 142 | numBufferedLines = 1; |
|---|
| 143 | bufferedLine = 0; |
|---|
| 144 | consumedLine = false; |
|---|
| 145 | if (passesFilters(currentEntry)) |
|---|
| 146 | { |
|---|
| 147 | rc = currentEntry; |
|---|
| 148 | } |
|---|
| 149 | else |
|---|
| 150 | { |
|---|
| 151 | currentEntry.reset(); |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | catch (Exception ex) |
|---|
| 156 | { |
|---|
| 157 | /* in case of exception the entry currently in process is |
|---|
| 158 | returned and the current log line is discarded */ |
|---|
| 159 | System.err.println("Got an exception when processing line: " |
|---|
| 160 | + currentLine); |
|---|
| 161 | System.err.println(ex); |
|---|
| 162 | ex.printStackTrace(); |
|---|
| 163 | mBufferedLines.clear(); |
|---|
| 164 | numBufferedLines = 0; |
|---|
| 165 | bufferedLine = 0; |
|---|
| 166 | if (consumedLine && passesFilters(currentEntry)) |
|---|
| 167 | { |
|---|
| 168 | rc = currentEntry; |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | if ((rc == null) && consumedLine && passesFilters(currentEntry)) |
|---|
| 174 | { |
|---|
| 175 | rc = currentEntry; |
|---|
| 176 | mBufferedLines.clear(); |
|---|
| 177 | } |
|---|
| 178 | if (rc != currentEntry) |
|---|
| 179 | { |
|---|
| 180 | currentEntry.release(); |
|---|
| 181 | } |
|---|
| 182 | return rc; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | /** |
|---|
| 186 | * Checks whether more data is to read from the log file. |
|---|
| 187 | * |
|---|
| 188 | * @return true if the log file contains data not already read by this; |
|---|
| 189 | * false, else. |
|---|
| 190 | * |
|---|
| 191 | * TODO: Add some more criteria, e.g. modification time to detect log file |
|---|
| 192 | * switches etc. |
|---|
| 193 | */ |
|---|
| 194 | boolean available () |
|---|
| 195 | { |
|---|
| 196 | boolean rc = false; |
|---|
| 197 | |
|---|
| 198 | try |
|---|
| 199 | { |
|---|
| 200 | rc = mReader.ready(); |
|---|
| 201 | } |
|---|
| 202 | catch (IOException iex) |
|---|
| 203 | { |
|---|
| 204 | System.err.println( |
|---|
| 205 | "Error while checking whether more data is available"); |
|---|
| 206 | iex.printStackTrace(); |
|---|
| 207 | } |
|---|
| 208 | return rc; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | /** |
|---|
| 212 | * Checks whether the LogFileEntry passes all filters. An implicit filter is |
|---|
| 213 | * that the type of the entry has been successfully parsed. |
|---|
| 214 | * |
|---|
| 215 | * @param entry The LogFileEntry to check. |
|---|
| 216 | * |
|---|
| 217 | * @return true if <code>entry</code>passes all filters. |
|---|
| 218 | */ |
|---|
| 219 | private boolean passesFilters (final LogFileEntry entry) |
|---|
| 220 | { |
|---|
| 221 | boolean rc = entry.getType() != null; |
|---|
| 222 | for (final Iterator filterIterator = mFilters.iterator(); |
|---|
| 223 | filterIterator.hasNext() && rc; ) |
|---|
| 224 | { |
|---|
| 225 | final Filter filter = (Filter) filterIterator.next(); |
|---|
| 226 | rc = filter.isPassable(entry); |
|---|
| 227 | } |
|---|
| 228 | return rc; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | /** |
|---|
| 232 | * Reads the current line from the log file. |
|---|
| 233 | * |
|---|
| 234 | * @return line wrapped in a StringBuffer |
|---|
| 235 | */ |
|---|
| 236 | private StringBuffer readLine () |
|---|
| 237 | { |
|---|
| 238 | StringBuffer rc = null; |
|---|
| 239 | try |
|---|
| 240 | { |
|---|
| 241 | final String line = mReader.readLine(); |
|---|
| 242 | if (line != null) |
|---|
| 243 | { |
|---|
| 244 | rc = new StringBuffer(line); |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | catch (Exception ex) |
|---|
| 248 | { |
|---|
| 249 | System.err.println("Caught an exception reading the current log line"); |
|---|
| 250 | ex.printStackTrace(); |
|---|
| 251 | } |
|---|
| 252 | return rc; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | void close () |
|---|
| 256 | { |
|---|
| 257 | if (mReader != null) |
|---|
| 258 | { |
|---|
| 259 | try |
|---|
| 260 | { |
|---|
| 261 | mReader.close(); |
|---|
| 262 | } |
|---|
| 263 | catch (IOException iex) |
|---|
| 264 | { |
|---|
| 265 | System.err.println("Error closing this:" + iex); |
|---|
| 266 | iex.printStackTrace(); |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | } |
|---|