| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | (1) | public class LogReader |
| 50 | | | { |
| 51 | 0 | | private final List mBufferedLines = new ArrayList(); |
| 52 | | | private final BufferedReader mReader; |
| 53 | | | private final File mFile; |
| 54 | 0 | | private final List mFilters = new ArrayList(); |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | | | |
| 59 | | | @param |
| 60 | | | |
| 61 | | | @throws |
| 62 | | | |
| 63 | | | |
| 64 | | | LogReader (final String fileName) |
| 65 | | | throws InstantiationException |
| 66 | 0 | | { |
| 67 | | | try |
| 68 | | | { |
| 69 | 0 | (2) | mFile = new File(fileName); |
| 70 | 0 | | mReader = new BufferedReader(new FileReader(mFile)); |
| 71 | | | } |
| 72 | 0 | | catch (Exception ex) |
| 73 | | | { |
| 74 | 0 | | final InstantiationException iex = new InstantiationException( |
| 75 | | | "Cannot install LogReader for file '" + fileName + "'."); |
| 76 | 0 | | iex.initCause(ex); |
| 77 | 0 | | throw iex; |
| 78 | 0 | | } |
| 79 | | | |
| 80 | 0 | | } |
| 81 | | | |
| 82 | | | |
| 83 | | | |
| 84 | | | |
| 85 | | | @param |
| 86 | | | |
| 87 | | | void addFilter (final Filter filter) |
| 88 | | | { |
| 89 | 0 | | mFilters.add(filter); |
| 90 | 0 | | } |
| 91 | | | |
| 92 | | | |
| 93 | | | |
| 94 | | | |
| 95 | | | |
| 96 | | | |
| 97 | | | |
| 98 | | | |
| 99 | | | @return |
| 100 | | | |
| 101 | | | @throws |
| 102 | | | |
| 103 | | (3) | LogFileEntry readLogFileEntry () |
| 104 | | | throws LoggingException |
| 105 | | | { |
| 106 | 0 | | LogFileEntry rc = null; |
| 107 | 0 | | final LogFileEntry currentEntry = LogFileEntry.getLogFileEntry(); |
| 108 | 0 | | int numBufferedLines = mBufferedLines.size(); |
| 109 | 0 | | int bufferedLine = 0; |
| 110 | | | boolean readBuffered; |
| 111 | 0 | | boolean consumedLine = false; |
| 112 | | | |
| 113 | 0 | | while (((bufferedLine < numBufferedLines) || available()) && (rc == null)) |
| 114 | | | { |
| 115 | | | final StringBuffer currentLine; |
| 116 | 0 | | if (bufferedLine < numBufferedLines) |
| 117 | | | { |
| 118 | 0 | | currentLine = (StringBuffer) mBufferedLines.get(bufferedLine++); |
| 119 | 0 | | readBuffered = true; |
| 120 | | | } |
| 121 | | | else |
| 122 | | | { |
| 123 | 0 | | currentLine = readLine(); |
| 124 | 0 | | readBuffered = false; |
| 125 | | | } |
| 126 | 0 | | if (currentLine != null) |
| 127 | | | { |
| 128 | | | try |
| 129 | | | { |
| 130 | 0 | | if (currentEntry.addLogLine(currentLine)) |
| 131 | | | { |
| 132 | 0 | | if (! readBuffered) |
| 133 | | | { |
| 134 | 0 | | mBufferedLines.add(currentLine); |
| 135 | | | } |
| 136 | 0 | | consumedLine = true; |
| 137 | | | } |
| 138 | | | else |
| 139 | | | { |
| 140 | 0 | | mBufferedLines.clear(); |
| 141 | 0 | | mBufferedLines.add(currentLine); |
| 142 | 0 | | numBufferedLines = 1; |
| 143 | 0 | | bufferedLine = 0; |
| 144 | 0 | | consumedLine = false; |
| 145 | 0 | | if (passesFilters(currentEntry)) |
| 146 | | | { |
| 147 | 0 | | rc = currentEntry; |
| 148 | | | } |
| 149 | | | else |
| 150 | | | { |
| 151 | 0 | | currentEntry.reset(); |
| 152 | | | } |
| 153 | | | } |
| 154 | | | } |
| 155 | 0 | | catch (Exception ex) |
| 156 | | | { |
| 157 | | | |
| 158 | | | |
| 159 | 0 | (4) | System.err.println("Got an exception when processing line: " |
| 160 | | | + currentLine); |
| 161 | 0 | (5) | System.err.println(ex); |
| 162 | 0 | (6) | ex.printStackTrace(); |
| 163 | 0 | | mBufferedLines.clear(); |
| 164 | 0 | | numBufferedLines = 0; |
| 165 | 0 | | bufferedLine = 0; |
| 166 | 0 | | if (consumedLine && passesFilters(currentEntry)) |
| 167 | | | { |
| 168 | 0 | | rc = currentEntry; |
| 169 | | | } |
| 170 | 0 | | } |
| 171 | | | } |
| 172 | 0 | | } |
| 173 | 0 | | if ((rc == null) && consumedLine && passesFilters(currentEntry)) |
| 174 | | | { |
| 175 | 0 | | rc = currentEntry; |
| 176 | 0 | | mBufferedLines.clear(); |
| 177 | | | } |
| 178 | 0 | | if (rc != currentEntry) |
| 179 | | | { |
| 180 | 0 | | currentEntry.release(); |
| 181 | | | } |
| 182 | 0 | | return rc; |
| 183 | | | } |
| 184 | | | |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | @return |
| 189 | | | |
| 190 | | | |
| 191 | | (7) | |
| 192 | | | |
| 193 | | | |
| 194 | | | boolean available () |
| 195 | | | { |
| 196 | 0 | | boolean rc = false; |
| 197 | | | |
| 198 | | | try |
| 199 | | | { |
| 200 | 0 | | rc = mReader.ready(); |
| 201 | | | } |
| 202 | 0 | | catch (IOException iex) |
| 203 | | | { |
| 204 | 0 | (8) | System.err.println( |
| 205 | | | "Error while checking whether more data is available"); |
| 206 | 0 | (9) | iex.printStackTrace(); |
| 207 | 0 | | } |
| 208 | 0 | | return rc; |
| 209 | | | } |
| 210 | | | |
| 211 | | | |
| 212 | | | |
| 213 | | | |
| 214 | | | |
| 215 | | | @param |
| 216 | | | |
| 217 | | | @return<code></code> |
| 218 | | | |
| 219 | | | private boolean passesFilters (final LogFileEntry entry) |
| 220 | | | { |
| 221 | 0 | | boolean rc = entry.getType() != null; |
| 222 | 0 | | for (final Iterator filterIterator = mFilters.iterator(); |
| 223 | 0 | | filterIterator.hasNext() && rc; ) |
| 224 | | | { |
| 225 | 0 | | final Filter filter = (Filter) filterIterator.next(); |
| 226 | 0 | | rc = filter.isPassable(entry); |
| 227 | 0 | | } |
| 228 | 0 | | return rc; |
| 229 | | | } |
| 230 | | | |
| 231 | | | |
| 232 | | | |
| 233 | | | |
| 234 | | | @return |
| 235 | | | |
| 236 | | | private StringBuffer readLine () |
| 237 | | | { |
| 238 | 0 | | StringBuffer rc = null; |
| 239 | | | try |
| 240 | | | { |
| 241 | 0 | | final String line = mReader.readLine(); |
| 242 | 0 | | if (line != null) |
| 243 | | | { |
| 244 | 0 | | rc = new StringBuffer(line); |
| 245 | | | } |
| 246 | | | } |
| 247 | 0 | | catch (Exception ex) |
| 248 | | | { |
| 249 | 0 | (10) | System.err.println("Caught an exception reading the current log line"); |
| 250 | 0 | (11) | ex.printStackTrace(); |
| 251 | 0 | | } |
| 252 | 0 | | return rc; |
| 253 | | | } |
| 254 | | | |
| 255 | | | void close () |
| 256 | | | { |
| 257 | 0 | | if (mReader != null) |
| 258 | | | { |
| 259 | | | try |
| 260 | | | { |
| 261 | 0 | | mReader.close(); |
| 262 | | | } |
| 263 | 0 | | catch (IOException iex) |
| 264 | | | { |
| 265 | 0 | (12) | System.err.println("Error closing this:" + iex); |
| 266 | 0 | (13) | iex.printStackTrace(); |
| 267 | 0 | | } |
| 268 | | | } |
| 269 | 0 | | } |
| 270 | | | } |