| 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.text.ParseException; |
| 36 | | | import java.util.HashMap; |
| 37 | | | import java.util.Map; |
| 38 | | | |
| 39 | | | import org.apache.commons.pool.ObjectPool; |
| 40 | | | import org.apache.commons.pool.PoolableObjectFactory; |
| 41 | | | import org.apache.commons.pool.impl.StackObjectPool; |
| 42 | | | |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | (1) | public class LogFileEntry |
| 52 | | | extends LogItem |
| 53 | | | { |
| 54 | 100 | | private static final ObjectPool POOL = new StackObjectPool(); |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | | | private LogFileEntry mEntryCursor; |
| 59 | 100 | | private LogFileEntry mRootEntry = null; |
| 60 | | | |
| 61 | 100 | | private final Map mFormatMap = new HashMap(); |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | 100 | | private boolean mMetStackTraceLine = false; |
| 66 | | | |
| 67 | | | private final boolean mPooled; |
| 68 | | | |
| 69 | | | private static final class LogFileEntryFactory |
| 70 | | | implements PoolableObjectFactory |
| 71 | | | { |
| 72 | | | |
| 73 | | | |
| 74 | | | |
| 75 | | | private LogFileEntryFactory () |
| 76 | 100 | | { |
| 77 | | | |
| 78 | 100 | | } |
| 79 | | | |
| 80 | | | {@inheritDoc} |
| 81 | | | public Object makeObject () |
| 82 | | | { |
| 83 | 100 | | return new LogFileEntry(true); |
| 84 | | | } |
| 85 | | | |
| 86 | | | {@inheritDoc} |
| 87 | | | public void destroyObject (Object arg0) |
| 88 | | | { |
| 89 | | | |
| 90 | 100 | | } |
| 91 | | | |
| 92 | | | {@inheritDoc} |
| 93 | | | public void activateObject (Object arg) |
| 94 | | | { |
| 95 | | | |
| 96 | 100 | | } |
| 97 | | | |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | | | |
| 102 | | | |
| 103 | | | |
| 104 | | | @param |
| 105 | | | |
| 106 | | | @see |
| 107 | | | |
| 108 | | | public void passivateObject (final Object arg) |
| 109 | | | { |
| 110 | 0 | | final LogFileEntry entry = (LogFileEntry) arg; |
| 111 | 0 | | final LogFileEntry nested = (LogFileEntry) entry.getNestedItem(); |
| 112 | 0 | | entry.setNestedEntry(null); |
| 113 | 0 | | entry.reset(); |
| 114 | 0 | | if (nested != null) |
| 115 | | | { |
| 116 | 0 | | nested.release(); |
| 117 | | | } |
| 118 | 0 | | } |
| 119 | | | |
| 120 | | | |
| 121 | | | |
| 122 | | | |
| 123 | | | @param |
| 124 | | | |
| 125 | | | @return |
| 126 | | | |
| 127 | | | @see |
| 128 | | | |
| 129 | | | public boolean validateObject (Object arg) |
| 130 | | | { |
| 131 | 100 | | return true; |
| 132 | | | } |
| 133 | | | } |
| 134 | | | |
| 135 | | | static |
| 136 | | | { |
| 137 | 100 | | POOL.setFactory(new LogFileEntryFactory()); |
| 138 | 100 | | } |
| 139 | | | |
| 140 | | | protected LogFileEntry () |
| 141 | | | { |
| 142 | 0 | | this(false); |
| 143 | 0 | | } |
| 144 | | | |
| 145 | | | |
| 146 | | | |
| 147 | | | |
| 148 | | | @param |
| 149 | | | |
| 150 | | | private LogFileEntry (final boolean pooled) |
| 151 | 100 | | { |
| 152 | 100 | | mEntryCursor = this; |
| 153 | 100 | | mRootEntry = this; |
| 154 | 100 | | mPooled = pooled; |
| 155 | 100 | | } |
| 156 | | | |
| 157 | | | |
| 158 | | | |
| 159 | | | {@linkplain } |
| 160 | | | |
| 161 | | | @return |
| 162 | | | |
| 163 | | | @throws |
| 164 | | | |
| 165 | | | static LogFileEntry getLogFileEntry () |
| 166 | | | { |
| 167 | | | try |
| 168 | | | { |
| 169 | 100 | | return (LogFileEntry) POOL.borrowObject(); |
| 170 | | | } |
| 171 | 0 | | catch (Exception ex) |
| 172 | | | { |
| 173 | 0 | | throw new LoggingException( |
| 174 | | | "Error retrieving an instance of LogFileEntry", ex); |
| 175 | | | } |
| 176 | | | } |
| 177 | | | |
| 178 | | | |
| 179 | | | |
| 180 | | | |
| 181 | | | |
| 182 | | | @param |
| 183 | | | |
| 184 | | | @return |
| 185 | | | |
| 186 | | | |
| 187 | | | @throws |
| 188 | | | @throws |
| 189 | | | |
| 190 | | | boolean addLogLine (final StringBuffer logLine) |
| 191 | | | throws ParseException |
| 192 | | | { |
| 193 | 0 | | boolean rc = false; |
| 194 | 0 | | LogLineFormat.LogLineType type = null; |
| 195 | 0 | | if (logLine.length() == 0) |
| 196 | | | { |
| 197 | 0 | | rc = true; |
| 198 | | | } |
| 199 | | | else |
| 200 | | | { |
| 201 | 0 | | type = LogLineFormat.getLogLineType(logLine.charAt(0)); |
| 202 | 0 | | final LogFileEntry entry = findEntry(type); |
| 203 | | | |
| 204 | 0 | | if (entry != null) |
| 205 | | | { |
| 206 | 0 | | entry.handleLogLine(type, logLine); |
| 207 | 0 | | rc = true; |
| 208 | | | } |
| 209 | | | } |
| 210 | 0 | | return rc; |
| 211 | | | } |
| 212 | | | |
| 213 | | | |
| 214 | | | |
| 215 | | | |
| 216 | | | |
| 217 | | | |
| 218 | | | @param |
| 219 | | | |
| 220 | | | |
| 221 | | | void addToStackTrace (final StackTraceInfo stackTraceElement) |
| 222 | | | { |
| 223 | 0 | | if (stackTraceElement.isCauseLine()) |
| 224 | | | { |
| 225 | | | |
| 226 | 0 | | if (getNestedItem() == null) |
| 227 | | | { |
| 228 | 0 | | throw new LoggingException("Found a 'caused-by' stack trace line, " |
| 229 | | | + "but have not got a nested element: " |
| 230 | | | + stackTraceElement.toString()); |
| 231 | | | } |
| 232 | 0 | | setCurrentEntry((LogFileEntry) getNestedItem()); |
| 233 | | | } |
| 234 | | | else |
| 235 | | | { |
| 236 | 0 | | getStackTraceLines().add(stackTraceElement); |
| 237 | | | } |
| 238 | 0 | | } |
| 239 | | | |
| 240 | | | private LogFileEntry findEntry (final LogLineFormat.LogLineType type) |
| 241 | | | { |
| 242 | 0 | | LogFileEntry entry = null; |
| 243 | | | |
| 244 | 0 | | if (type != LogLineFormat.STACKTRACE_MESSAGE) |
| 245 | | | { |
| 246 | | | |
| 247 | | | |
| 248 | | | |
| 249 | | | |
| 250 | 0 | | if (! mRootEntry.mMetStackTraceLine |
| 251 | | | && ((getCurrentEntry().getType() == null) |
| 252 | | | || (type != LogLineFormat.TRACE_MESSAGE |
| 253 | | | && type != LogLineFormat.EXCEPTION_MESSAGE |
| 254 | | | && type != LogLineFormat.LOG_MESSAGE |
| 255 | | | && type != LogLineFormat.ERROR_MESSAGE))) |
| 256 | | | { |
| 257 | 0 | | entry = getCurrentEntry(); |
| 258 | | | } |
| 259 | | | } |
| 260 | | | else |
| 261 | | | { |
| 262 | 0 | | if (! mRootEntry.mMetStackTraceLine) |
| 263 | | | { |
| 264 | | | |
| 265 | 0 | | entry = mRootEntry; |
| 266 | 0 | | setCurrentEntry(entry); |
| 267 | 0 | | mRootEntry.mMetStackTraceLine = true; |
| 268 | | | } |
| 269 | | | else |
| 270 | | | { |
| 271 | 0 | | entry = getCurrentEntry(); |
| 272 | | | } |
| 273 | | | } |
| 274 | 0 | | return entry; |
| 275 | | | } |
| 276 | | | |
| 277 | | | private LogLineFormat getFormat (final LogLineFormat.LogLineType type) |
| 278 | | | { |
| 279 | 0 | | LogLineFormat rc = (LogLineFormat) mFormatMap.get(type); |
| 280 | 0 | | if (rc == null) |
| 281 | | | { |
| 282 | 0 | | rc = LogLineFormatFactory.create(type); |
| 283 | 0 | | mFormatMap .put(type, rc); |
| 284 | | | } |
| 285 | 0 | | return rc; |
| 286 | | | } |
| 287 | | | |
| 288 | | | private void handleLogLine ( |
| 289 | | | final LogLineFormat.LogLineType type, |
| 290 | | | final StringBuffer sb) |
| 291 | | | throws ParseException |
| 292 | | | { |
| 293 | 0 | | if ((type == LogLineFormat.TRACE_MESSAGE) |
| 294 | | | || (type == LogLineFormat.LOG_MESSAGE) |
| 295 | | | || (type == LogLineFormat.EXCEPTION_MESSAGE) |
| 296 | | | || (type == LogLineFormat.ERROR_MESSAGE)) |
| 297 | | | { |
| 298 | 0 | | setType(String.valueOf(type.getTypeSpecifier())); |
| 299 | | | } |
| 300 | 0 | | if (type == LogLineFormat.NESTED_MESSAGE) |
| 301 | | | { |
| 302 | 0 | | final LogFileEntry entry = LogFileEntry.getLogFileEntry(); |
| 303 | 0 | | setCurrentEntry(entry); |
| 304 | 0 | | setNestedEntry(entry); |
| 305 | 0 | | getFormat(type).parse(sb, entry); |
| 306 | 0 | | } |
| 307 | | | else |
| 308 | | | { |
| 309 | 0 | | getFormat(type).parse(sb, this); |
| 310 | | | } |
| 311 | 0 | | } |
| 312 | | | |
| 313 | | | |
| 314 | | | |
| 315 | | | |
| 316 | | | |
| 317 | | | |
| 318 | | | @throws |
| 319 | | | |
| 320 | | | public void reset () |
| 321 | | | throws LoggingException |
| 322 | | | { |
| 323 | 100 | | final LogFileEntry nested = (LogFileEntry) getNestedItem(); |
| 324 | 100 | | super.reset(); |
| 325 | 100 | | mEntryCursor = this; |
| 326 | 100 | | mMetStackTraceLine = false; |
| 327 | 100 | | mRootEntry = this; |
| 328 | | | |
| 329 | 100 | | if (nested != null) |
| 330 | | | { |
| 331 | 0 | | nested.release(); |
| 332 | | | } |
| 333 | 100 | | } |
| 334 | | | |
| 335 | | | |
| 336 | | | |
| 337 | | | |
| 338 | | | |
| 339 | | | |
| 340 | | | @throws |
| 341 | | | |
| 342 | | | void release () |
| 343 | | | throws LoggingException |
| 344 | | | { |
| 345 | | | try |
| 346 | | | { |
| 347 | 100 | | reset(); |
| 348 | 100 | | if (mPooled) |
| 349 | | | { |
| 350 | 100 | | POOL.returnObject(this); |
| 351 | | | } |
| 352 | | | } |
| 353 | 0 | | catch (Exception ex) |
| 354 | | | { |
| 355 | 0 | | throw new LoggingException("Error releasing this " + this, ex); |
| 356 | 100 | | } |
| 357 | 100 | | } |
| 358 | | | |
| 359 | | | |
| 360 | | | |
| 361 | | | |
| 362 | | | |
| 363 | | | @param |
| 364 | | | |
| 365 | | | private void setNestedEntry (final LogFileEntry nestedEntry) |
| 366 | | | { |
| 367 | 0 | | nestedEntry.mRootEntry = this.mRootEntry; |
| 368 | 0 | | setNestedItem(nestedEntry); |
| 369 | 0 | | } |
| 370 | | | |
| 371 | | | private LogFileEntry getCurrentEntry () |
| 372 | | | { |
| 373 | 0 | | return mRootEntry.mEntryCursor; |
| 374 | | | } |
| 375 | | | |
| 376 | | | private void setCurrentEntry (final LogFileEntry entry) |
| 377 | | | { |
| 378 | 0 | | mRootEntry.mEntryCursor = entry; |
| 379 | 0 | | } |
| 380 | | | |
| 381 | | | } |