| 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.PrintWriter; |
|---|
| 36 | import java.math.BigInteger; |
|---|
| 37 | import java.util.Calendar; |
|---|
| 38 | import java.util.Iterator; |
|---|
| 39 | import java.util.List; |
|---|
| 40 | import java.util.Set; |
|---|
| 41 | import java.util.TimeZone; |
|---|
| 42 | |
|---|
| 43 | import javax.xml.bind.JAXBContext; |
|---|
| 44 | import javax.xml.bind.JAXBException; |
|---|
| 45 | import javax.xml.bind.Marshaller; |
|---|
| 46 | |
|---|
| 47 | import org.apache.commons.pool.BaseKeyedPoolableObjectFactory; |
|---|
| 48 | import org.apache.commons.pool.impl.StackKeyedObjectPool; |
|---|
| 49 | import org.jcoderz.commons.util.Assert; |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * This printer formats the log messages into an Xml format and prints the log |
|---|
| 55 | * messages as a sequence of xml elements. |
|---|
| 56 | * |
|---|
| 57 | */ |
|---|
| 58 | public final class XmlPrinter |
|---|
| 59 | extends LogPrinter |
|---|
| 60 | { |
|---|
| 61 | private final LogRecordTypesObjectPool mXmlObjectsPool; |
|---|
| 62 | private final ObjectFactory mObjectFactory = new ObjectFactory(); |
|---|
| 63 | private final JAXBContext mJaxbContext; |
|---|
| 64 | private final Marshaller mMarshaller; |
|---|
| 65 | |
|---|
| 66 | private static final class LogRecordTypesObjectPool |
|---|
| 67 | extends StackKeyedObjectPool |
|---|
| 68 | { |
|---|
| 69 | private LogRecordType borrowLogRecordType () |
|---|
| 70 | throws LoggingException |
|---|
| 71 | { |
|---|
| 72 | try |
|---|
| 73 | { |
|---|
| 74 | return (LogRecordType) borrowObject(LogRecordType.class); |
|---|
| 75 | } |
|---|
| 76 | catch (Exception ex) |
|---|
| 77 | { |
|---|
| 78 | throw new LoggingException("Error borrowing LogRecordType", ex); |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | private LogRecordType borrowLogRecord () |
|---|
| 83 | throws LoggingException |
|---|
| 84 | { |
|---|
| 85 | try |
|---|
| 86 | { |
|---|
| 87 | return (LogRecordType) borrowObject(LogRecord.class); |
|---|
| 88 | } |
|---|
| 89 | catch (Exception ex) |
|---|
| 90 | { |
|---|
| 91 | throw new LoggingException("Error borrowing LogRecord", ex); |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | private void returnLogRecord (final LogRecordType record) |
|---|
| 96 | throws LoggingException |
|---|
| 97 | { |
|---|
| 98 | try |
|---|
| 99 | { |
|---|
| 100 | returnObject(LogRecordType.class, record); |
|---|
| 101 | } |
|---|
| 102 | catch (Exception ex) |
|---|
| 103 | { |
|---|
| 104 | throw new LoggingException("Error returning LogRecordType", ex); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | private FrameType borrowFrame () |
|---|
| 109 | throws LoggingException |
|---|
| 110 | { |
|---|
| 111 | try |
|---|
| 112 | { |
|---|
| 113 | return (FrameType) borrowObject(FrameType.class); |
|---|
| 114 | } |
|---|
| 115 | catch (Exception ex) |
|---|
| 116 | { |
|---|
| 117 | throw new LoggingException("Error borrowing FrameType", ex); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | private void returnFrame (final FrameType frame) |
|---|
| 122 | throws LoggingException |
|---|
| 123 | { |
|---|
| 124 | try |
|---|
| 125 | { |
|---|
| 126 | returnObject(FrameType.class, frame); |
|---|
| 127 | } |
|---|
| 128 | catch (Exception ex) |
|---|
| 129 | { |
|---|
| 130 | throw new LoggingException("Error returning FrameType", ex); |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | private ParameterType borrowParameter () |
|---|
| 135 | throws LoggingException |
|---|
| 136 | { |
|---|
| 137 | try |
|---|
| 138 | { |
|---|
| 139 | return (ParameterType) borrowObject(ParameterType.class); |
|---|
| 140 | } |
|---|
| 141 | catch (Exception ex) |
|---|
| 142 | { |
|---|
| 143 | throw new LoggingException("Error borrowing ParameterType", ex); |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | private void returnParameter (final ParameterType parameter) |
|---|
| 148 | throws LoggingException |
|---|
| 149 | { |
|---|
| 150 | try |
|---|
| 151 | { |
|---|
| 152 | returnObject(ParameterType.class, parameter); |
|---|
| 153 | } |
|---|
| 154 | catch (Exception ex) |
|---|
| 155 | { |
|---|
| 156 | throw new LoggingException("Error returning ParameterType", ex); |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | private CauseType borrowCause () |
|---|
| 161 | throws LoggingException |
|---|
| 162 | { |
|---|
| 163 | try |
|---|
| 164 | { |
|---|
| 165 | return (CauseType) borrowObject(CauseType.class); |
|---|
| 166 | } |
|---|
| 167 | catch (Exception ex) |
|---|
| 168 | { |
|---|
| 169 | throw new LoggingException("Error borrowing CauseType", ex); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | private void returnCause (final CauseType cause) |
|---|
| 174 | throws LoggingException |
|---|
| 175 | { |
|---|
| 176 | try |
|---|
| 177 | { |
|---|
| 178 | returnObject(CauseType.class, cause); |
|---|
| 179 | } |
|---|
| 180 | catch (Exception ex) |
|---|
| 181 | { |
|---|
| 182 | throw new LoggingException("Error returning CauseType", ex); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | private ExceptionType borrowException () |
|---|
| 187 | throws LoggingException |
|---|
| 188 | { |
|---|
| 189 | try |
|---|
| 190 | { |
|---|
| 191 | return (ExceptionType) borrowObject(ExceptionType.class); |
|---|
| 192 | } |
|---|
| 193 | catch (Exception ex) |
|---|
| 194 | { |
|---|
| 195 | throw new LoggingException("Error borrowing ExceptionType", ex); |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | private void returnException (final ExceptionType ex) |
|---|
| 200 | throws LoggingException |
|---|
| 201 | { |
|---|
| 202 | try |
|---|
| 203 | { |
|---|
| 204 | returnObject(ExceptionType.class, ex); |
|---|
| 205 | } |
|---|
| 206 | catch (Exception ex1) |
|---|
| 207 | { |
|---|
| 208 | throw new LoggingException("Error returning ExceptionType", ex1); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | private StacktraceType borrowStacktrace () |
|---|
| 213 | throws LoggingException |
|---|
| 214 | { |
|---|
| 215 | try |
|---|
| 216 | { |
|---|
| 217 | return (StacktraceType) borrowObject(StacktraceType.class); |
|---|
| 218 | } |
|---|
| 219 | catch (Exception ex) |
|---|
| 220 | { |
|---|
| 221 | throw new LoggingException("Error borrowing StacktraceType", ex); |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | private void returnStacktrace (final StacktraceType stacktrace) |
|---|
| 226 | throws LoggingException |
|---|
| 227 | { |
|---|
| 228 | try |
|---|
| 229 | { |
|---|
| 230 | returnObject(StacktraceType.class, stacktrace); |
|---|
| 231 | } |
|---|
| 232 | catch (Exception ex) |
|---|
| 233 | { |
|---|
| 234 | throw new LoggingException("Error returning StacktraceType", ex); |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | private Calendar borrowCalendar () |
|---|
| 239 | throws LoggingException |
|---|
| 240 | { |
|---|
| 241 | try |
|---|
| 242 | { |
|---|
| 243 | return (Calendar) borrowObject(Calendar.class); |
|---|
| 244 | } |
|---|
| 245 | catch (Exception ex) |
|---|
| 246 | { |
|---|
| 247 | throw new LoggingException("Error borrowing Calendar", ex); |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | private void returnCalendar (final Calendar cal) |
|---|
| 252 | throws LoggingException |
|---|
| 253 | { |
|---|
| 254 | try |
|---|
| 255 | { |
|---|
| 256 | returnObject(Calendar.class, cal); |
|---|
| 257 | } |
|---|
| 258 | catch (Exception ex) |
|---|
| 259 | { |
|---|
| 260 | throw new LoggingException("Error returning Calendar", ex); |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | private static final class XmlObjectFactory |
|---|
| 266 | extends BaseKeyedPoolableObjectFactory |
|---|
| 267 | { |
|---|
| 268 | private final LogRecordTypesObjectPool mPool; |
|---|
| 269 | private final ObjectFactory mJaxbFactory; |
|---|
| 270 | |
|---|
| 271 | private XmlObjectFactory ( |
|---|
| 272 | final ObjectFactory factory, |
|---|
| 273 | final LogRecordTypesObjectPool pool) |
|---|
| 274 | { |
|---|
| 275 | mPool = pool; |
|---|
| 276 | mJaxbFactory = factory; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | /** |
|---|
| 280 | * Makes an object for the key given by <code>key</code>. |
|---|
| 281 | * This accepts as keys classes of the jaxb objects used by this. |
|---|
| 282 | * |
|---|
| 283 | * @param key The class of the requested object. |
|---|
| 284 | * |
|---|
| 285 | * @return Instance of specified class. |
|---|
| 286 | * |
|---|
| 287 | * @see org.apache.commons.pool.BaseKeyedPoolableObjectFactory#makeObject(java.lang.Object) |
|---|
| 288 | */ |
|---|
| 289 | public Object makeObject (Object key) |
|---|
| 290 | throws JAXBException |
|---|
| 291 | { |
|---|
| 292 | final Object rc; |
|---|
| 293 | |
|---|
| 294 | if (key == LogRecordType.class) |
|---|
| 295 | { |
|---|
| 296 | rc = mJaxbFactory.createLogRecordType(); |
|---|
| 297 | } |
|---|
| 298 | else if (key == LogRecord.class) |
|---|
| 299 | { |
|---|
| 300 | rc = mJaxbFactory.createLogRecord(); |
|---|
| 301 | } |
|---|
| 302 | else if (key == FrameType.class) |
|---|
| 303 | { |
|---|
| 304 | rc = mJaxbFactory.createFrameType(); |
|---|
| 305 | } |
|---|
| 306 | else if (key == StacktraceType.class) |
|---|
| 307 | { |
|---|
| 308 | rc = mJaxbFactory.createStacktraceType(); |
|---|
| 309 | } |
|---|
| 310 | else if (key == ParameterType.class) |
|---|
| 311 | { |
|---|
| 312 | rc = mJaxbFactory.createParameterType(); |
|---|
| 313 | } |
|---|
| 314 | else if (key == ExceptionType.class) |
|---|
| 315 | { |
|---|
| 316 | rc = mJaxbFactory.createExceptionType(); |
|---|
| 317 | } |
|---|
| 318 | else if (key == CauseType.class) |
|---|
| 319 | { |
|---|
| 320 | rc = mJaxbFactory.createCauseType(); |
|---|
| 321 | } |
|---|
| 322 | else if (key == Calendar.class) |
|---|
| 323 | { |
|---|
| 324 | rc = Calendar.getInstance(TimeZone.getTimeZone("UTC")); |
|---|
| 325 | } |
|---|
| 326 | else |
|---|
| 327 | { |
|---|
| 328 | throw new IllegalArgumentException("Cannot make an object for " |
|---|
| 329 | + key); |
|---|
| 330 | } |
|---|
| 331 | return rc; |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | /** |
|---|
| 335 | * Sets all required sub type fields of the supplied object. |
|---|
| 336 | * |
|---|
| 337 | * @param key The key of the object to activate. |
|---|
| 338 | * @param xmlObj The object to activate. |
|---|
| 339 | * |
|---|
| 340 | * @see org.apache.commons.pool.KeyedPoolableObjectFactory#activateObject(java.lang.Object, java.lang.Object) |
|---|
| 341 | */ |
|---|
| 342 | public void activateObject (Object key, Object xmlObj) |
|---|
| 343 | throws LoggingException |
|---|
| 344 | { |
|---|
| 345 | if ((key == LogRecordType.class) || (key == LogRecord.class)) |
|---|
| 346 | { |
|---|
| 347 | final LogRecordType logRecord = (LogRecordType) xmlObj; |
|---|
| 348 | |
|---|
| 349 | // set all required sub typed elements |
|---|
| 350 | // every log record has a source |
|---|
| 351 | logRecord.setSource(mPool.borrowFrame()); |
|---|
| 352 | // ... and a timestamp |
|---|
| 353 | logRecord.setTimestamp(mPool.borrowCalendar()); |
|---|
| 354 | } |
|---|
| 355 | else if (key == FrameType.class) |
|---|
| 356 | { |
|---|
| 357 | // nop |
|---|
| 358 | } |
|---|
| 359 | else if (key == StacktraceType.class) |
|---|
| 360 | { |
|---|
| 361 | // nop |
|---|
| 362 | } |
|---|
| 363 | else if (key == ParameterType.class) |
|---|
| 364 | { |
|---|
| 365 | // nop |
|---|
| 366 | } |
|---|
| 367 | else if (key == ExceptionType.class) |
|---|
| 368 | { |
|---|
| 369 | // nop |
|---|
| 370 | } |
|---|
| 371 | else if (key == CauseType.class) |
|---|
| 372 | { |
|---|
| 373 | // nop |
|---|
| 374 | } |
|---|
| 375 | else if (key == Calendar.class) |
|---|
| 376 | { |
|---|
| 377 | // nop |
|---|
| 378 | } |
|---|
| 379 | else |
|---|
| 380 | { |
|---|
| 381 | throw new IllegalArgumentException("Cannot make an object for " |
|---|
| 382 | + key); |
|---|
| 383 | } |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | /** |
|---|
| 387 | * Sets all elements of the object to passivate to null and puts back |
|---|
| 388 | * into the pool all sub typed objects. |
|---|
| 389 | * |
|---|
| 390 | * @param key The key of the object, which is the type of the object. |
|---|
| 391 | * @param jaxbObj The jaxb object to passivate |
|---|
| 392 | * |
|---|
| 393 | * @see org.apache.commons.pool.KeyedPoolableObjectFactory#passivateObject(java.lang.Object, java.lang.Object) |
|---|
| 394 | */ |
|---|
| 395 | public void passivateObject (Object key, Object jaxbObj) |
|---|
| 396 | throws LoggingException |
|---|
| 397 | { |
|---|
| 398 | if ((key == LogRecordType.class) || (key == LogRecord.class)) |
|---|
| 399 | { |
|---|
| 400 | passivateLogRecord((LogRecordType) jaxbObj); |
|---|
| 401 | } |
|---|
| 402 | else if (key == FrameType.class) |
|---|
| 403 | { |
|---|
| 404 | passivateFrame((FrameType) jaxbObj); |
|---|
| 405 | } |
|---|
| 406 | else if (key == StacktraceType.class) |
|---|
| 407 | { |
|---|
| 408 | passivateStackTrace((StacktraceType) jaxbObj); |
|---|
| 409 | } |
|---|
| 410 | else if (key == ParameterType.class) |
|---|
| 411 | { |
|---|
| 412 | passivateParameter((ParameterType) jaxbObj); |
|---|
| 413 | } |
|---|
| 414 | else if (key == ExceptionType.class) |
|---|
| 415 | { |
|---|
| 416 | passivateException((ExceptionType) jaxbObj); |
|---|
| 417 | } |
|---|
| 418 | else if (key == CauseType.class) |
|---|
| 419 | { |
|---|
| 420 | passivateCause((CauseType) jaxbObj); |
|---|
| 421 | } |
|---|
| 422 | else if (key == Calendar.class) |
|---|
| 423 | { |
|---|
| 424 | // nop |
|---|
| 425 | } |
|---|
| 426 | else |
|---|
| 427 | { |
|---|
| 428 | throw new IllegalArgumentException("Cannot make an object for " |
|---|
| 429 | + key); |
|---|
| 430 | } |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | /** |
|---|
| 434 | * Passivates an object of type <code>CauseType</code>. |
|---|
| 435 | * |
|---|
| 436 | * @param cause The CauseType object to passivate |
|---|
| 437 | * |
|---|
| 438 | * @throws Exception thrown by called methods. |
|---|
| 439 | */ |
|---|
| 440 | private void passivateCause (final CauseType cause) |
|---|
| 441 | throws LoggingException |
|---|
| 442 | { |
|---|
| 443 | final ExceptionType ex = cause.getException(); |
|---|
| 444 | cause.setException(null); |
|---|
| 445 | |
|---|
| 446 | final LogRecordType record = cause.getNestedRecord(); |
|---|
| 447 | cause.setNestedRecord(null); |
|---|
| 448 | |
|---|
| 449 | if (ex != null) |
|---|
| 450 | { |
|---|
| 451 | mPool.returnException(ex); |
|---|
| 452 | } |
|---|
| 453 | if (record != null) |
|---|
| 454 | { |
|---|
| 455 | mPool.returnLogRecord(record); |
|---|
| 456 | } |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | /** |
|---|
| 460 | * Passivates an object of type <code>ExceptionType</code>. |
|---|
| 461 | * |
|---|
| 462 | * @param ex The ExceptionType object to passivate |
|---|
| 463 | * |
|---|
| 464 | * @throws Exception thrown by called methods. |
|---|
| 465 | */ |
|---|
| 466 | private void passivateException (final ExceptionType ex) |
|---|
| 467 | throws LoggingException |
|---|
| 468 | { |
|---|
| 469 | ex.setMessage(null); |
|---|
| 470 | |
|---|
| 471 | final CauseType cause = ex.getCause(); |
|---|
| 472 | ex.setCause(null); |
|---|
| 473 | |
|---|
| 474 | final StacktraceType stacktrace = ex.getStacktrace(); |
|---|
| 475 | ex.setStacktrace(null); |
|---|
| 476 | |
|---|
| 477 | if (cause != null) |
|---|
| 478 | { |
|---|
| 479 | mPool.returnCause(cause); |
|---|
| 480 | } |
|---|
| 481 | if (stacktrace != null) |
|---|
| 482 | { |
|---|
| 483 | mPool.returnStacktrace(stacktrace); |
|---|
| 484 | } |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | /** |
|---|
| 488 | * Passivates an object of type <code>ParameterType</code>. |
|---|
| 489 | * |
|---|
| 490 | * @param parameter The ParameterType object to passivate |
|---|
| 491 | */ |
|---|
| 492 | private void passivateParameter (final ParameterType parameter) |
|---|
| 493 | { |
|---|
| 494 | parameter.setName(null); |
|---|
| 495 | parameter.getValue().clear(); |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | /** |
|---|
| 499 | * Passivates an object of type <code>StacktraceType</code>. |
|---|
| 500 | * |
|---|
| 501 | * @param stacktrace The stacktrace object to passivate |
|---|
| 502 | * |
|---|
| 503 | * @throws Exception thrown by called methods. |
|---|
| 504 | */ |
|---|
| 505 | private void passivateStackTrace (final StacktraceType stacktrace) |
|---|
| 506 | throws LoggingException |
|---|
| 507 | { |
|---|
| 508 | for (final Iterator iter |
|---|
| 509 | = stacktrace.getStacktraceElement().iterator(); iter.hasNext(); ) |
|---|
| 510 | { |
|---|
| 511 | mPool.returnFrame((FrameType) iter.next()); |
|---|
| 512 | } |
|---|
| 513 | stacktrace.getStacktraceElement().clear(); |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | /** |
|---|
| 517 | * Passivates an object of type <code>LogRecordType</code>. |
|---|
| 518 | * |
|---|
| 519 | * @param logRecord The LogRecordType object to passivate |
|---|
| 520 | * |
|---|
| 521 | * @throws Exception thrown by called methods. |
|---|
| 522 | */ |
|---|
| 523 | private void passivateLogRecord (final LogRecordType logRecord) |
|---|
| 524 | throws LoggingException |
|---|
| 525 | { |
|---|
| 526 | logRecord.setBusinessImpact(null); |
|---|
| 527 | logRecord.setCategory(null); |
|---|
| 528 | logRecord.setInstanceId(null); |
|---|
| 529 | logRecord.setLevel(null); |
|---|
| 530 | logRecord.setMessage(null); |
|---|
| 531 | logRecord.setNodeId(null); |
|---|
| 532 | logRecord.setSolution(null); |
|---|
| 533 | logRecord.setSymbol(null); |
|---|
| 534 | logRecord.setTrackingNumber(null); |
|---|
| 535 | |
|---|
| 536 | final Calendar cal = logRecord.getTimestamp(); |
|---|
| 537 | logRecord.setTimestamp(null); |
|---|
| 538 | |
|---|
| 539 | final StacktraceType stack = logRecord.getStacktrace(); |
|---|
| 540 | logRecord.setStacktrace(null); |
|---|
| 541 | |
|---|
| 542 | final FrameType source = logRecord.getSource(); |
|---|
| 543 | logRecord.setSource(null); |
|---|
| 544 | |
|---|
| 545 | final CauseType cause = logRecord.getCause(); |
|---|
| 546 | logRecord.setCause(null); |
|---|
| 547 | |
|---|
| 548 | if (cal != null) |
|---|
| 549 | { |
|---|
| 550 | mPool.returnCalendar(cal); |
|---|
| 551 | } |
|---|
| 552 | if (stack != null) |
|---|
| 553 | { |
|---|
| 554 | mPool.returnStacktrace(stack); |
|---|
| 555 | } |
|---|
| 556 | if (source != null) |
|---|
| 557 | { |
|---|
| 558 | mPool.returnFrame(source); |
|---|
| 559 | } |
|---|
| 560 | if (cause != null) |
|---|
| 561 | { |
|---|
| 562 | mPool.returnCause(cause); |
|---|
| 563 | } |
|---|
| 564 | for (final Iterator iter = logRecord.getParameter().iterator(); |
|---|
| 565 | iter.hasNext(); ) |
|---|
| 566 | { |
|---|
| 567 | mPool.returnParameter((ParameterType) iter.next()); |
|---|
| 568 | } |
|---|
| 569 | |
|---|
| 570 | logRecord.getParameter().clear(); |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | /** |
|---|
| 574 | * Passivates an object of type <code>FrameType</code>. |
|---|
| 575 | * |
|---|
| 576 | * @param frame The FrameType object to passivate |
|---|
| 577 | * |
|---|
| 578 | * @throws Exception thrown by called methods. |
|---|
| 579 | */ |
|---|
| 580 | private void passivateFrame (final FrameType frame) |
|---|
| 581 | { |
|---|
| 582 | frame.setSourceMethod(null); |
|---|
| 583 | frame.setSourceClass(null); |
|---|
| 584 | frame.setSourceLine(null); |
|---|
| 585 | } |
|---|
| 586 | } |
|---|
| 587 | |
|---|
| 588 | /** |
|---|
| 589 | * Creates a new instance of this. Allocates object pools and jaxb resources. |
|---|
| 590 | * |
|---|
| 591 | * @throws InstantiationException if an error occurs allocating the |
|---|
| 592 | * resources. |
|---|
| 593 | */ |
|---|
| 594 | public XmlPrinter () |
|---|
| 595 | throws InstantiationException |
|---|
| 596 | { |
|---|
| 597 | super(); |
|---|
| 598 | try |
|---|
| 599 | { |
|---|
| 600 | mJaxbContext = JAXBContext |
|---|
| 601 | .newInstance("org.jcoderz.commons.logging"); |
|---|
| 602 | mMarshaller = mJaxbContext.createMarshaller(); |
|---|
| 603 | mMarshaller.setProperty("jaxb.formatted.output", |
|---|
| 604 | Boolean.valueOf(true)); |
|---|
| 605 | } |
|---|
| 606 | catch (JAXBException ex) |
|---|
| 607 | { |
|---|
| 608 | final InstantiationException iex |
|---|
| 609 | = new InstantiationException("Cannot initialize jaxb resources"); |
|---|
| 610 | iex.initCause(ex); |
|---|
| 611 | throw iex; |
|---|
| 612 | } |
|---|
| 613 | mXmlObjectsPool = new LogRecordTypesObjectPool(); |
|---|
| 614 | mXmlObjectsPool.setFactory( |
|---|
| 615 | new XmlObjectFactory(mObjectFactory, mXmlObjectsPool)); |
|---|
| 616 | } |
|---|
| 617 | |
|---|
| 618 | /** |
|---|
| 619 | * Prints the log data using the supplied print writer in xml format. |
|---|
| 620 | * |
|---|
| 621 | * @param printer The PrintWriter to use for printing the data. |
|---|
| 622 | * @param logRecord The log data to format into xml and print using |
|---|
| 623 | * <code>printer</code>. |
|---|
| 624 | * |
|---|
| 625 | * @see LogPrinter#print(PrintWriter, LogItem) |
|---|
| 626 | */ |
|---|
| 627 | public void print ( |
|---|
| 628 | final PrintWriter printer, |
|---|
| 629 | final LogItem logRecord) |
|---|
| 630 | { |
|---|
| 631 | try |
|---|
| 632 | { |
|---|
| 633 | Assert.notNull(printer, "Printer"); |
|---|
| 634 | Assert.notNull(logRecord, "entry"); |
|---|
| 635 | Assert.notNull(logRecord.getType(), "logRecord.getType()"); |
|---|
| 636 | |
|---|
| 637 | LogItem logEntry = logRecord; |
|---|
| 638 | CauseType parentsCause = null; |
|---|
| 639 | LogRecordType rootRecord = null; |
|---|
| 640 | |
|---|
| 641 | while (logEntry != null) |
|---|
| 642 | { |
|---|
| 643 | if (! logEntry.isExceptionItem()) |
|---|
| 644 | { |
|---|
| 645 | final LogRecordType currentRecord; |
|---|
| 646 | |
|---|
| 647 | if (rootRecord == null) |
|---|
| 648 | { |
|---|
| 649 | currentRecord = mXmlObjectsPool.borrowLogRecord(); |
|---|
| 650 | rootRecord = currentRecord; |
|---|
| 651 | } |
|---|
| 652 | else |
|---|
| 653 | { |
|---|
| 654 | currentRecord = mXmlObjectsPool.borrowLogRecordType(); |
|---|
| 655 | } |
|---|
| 656 | fillLogRecord(currentRecord, logEntry); |
|---|
| 657 | |
|---|
| 658 | if (parentsCause != null) |
|---|
| 659 | { |
|---|
| 660 | parentsCause.setNestedRecord(currentRecord); |
|---|
| 661 | } |
|---|
| 662 | logEntry = logEntry.getNestedItem(); |
|---|
| 663 | parentsCause = setCause(logEntry, currentRecord); |
|---|
| 664 | } |
|---|
| 665 | else |
|---|
| 666 | { |
|---|
| 667 | final ExceptionType currentException |
|---|
| 668 | = mXmlObjectsPool.borrowException(); |
|---|
| 669 | fillException(currentException, logEntry); |
|---|
| 670 | if (parentsCause != null) |
|---|
| 671 | { |
|---|
| 672 | parentsCause.setException(currentException); |
|---|
| 673 | } |
|---|
| 674 | logEntry = logEntry.getNestedItem(); |
|---|
| 675 | parentsCause = setCause(logEntry, currentException); |
|---|
| 676 | } |
|---|
| 677 | } |
|---|
| 678 | if (rootRecord != null) |
|---|
| 679 | { |
|---|
| 680 | mMarshaller.marshal(rootRecord, printer); |
|---|
| 681 | mXmlObjectsPool.returnLogRecord(rootRecord); |
|---|
| 682 | } |
|---|
| 683 | } |
|---|
| 684 | catch (Exception ex) |
|---|
| 685 | { |
|---|
| 686 | System.err.println("Error formatting log file entry into xml: " + ex); |
|---|
| 687 | ex.printStackTrace(); |
|---|
| 688 | } |
|---|
| 689 | } |
|---|
| 690 | |
|---|
| 691 | private void fillLogRecord ( |
|---|
| 692 | final LogRecordType logRecord, |
|---|
| 693 | final LogItem entry) |
|---|
| 694 | { |
|---|
| 695 | logRecord.setNodeId(entry.getNodeId()); |
|---|
| 696 | logRecord.setInstanceId(entry.getInstanceId()); |
|---|
| 697 | logRecord.setLevel(entry.getLoggerLevel().toString()); |
|---|
| 698 | logRecord.setSymbol(entry.getSymbol()); |
|---|
| 699 | logRecord.setSymbolId(entry.getSymbolId()); |
|---|
| 700 | |
|---|
| 701 | // the timestamp object is set when the log record is borrowed from the |
|---|
| 702 | // pool |
|---|
| 703 | logRecord.getTimestamp().setTime(entry.getTimestamp().toUtilDate()); |
|---|
| 704 | |
|---|
| 705 | logRecord.setTrackingNumber(entry.getTrackingNumber()); |
|---|
| 706 | |
|---|
| 707 | logRecord.getSource().setSourceClass( |
|---|
| 708 | entry.getSourceClass()); |
|---|
| 709 | logRecord.getSource().setSourceMethod( |
|---|
| 710 | entry.getSourceMethod()); |
|---|
| 711 | logRecord.setThread(entry.getThreadId()); |
|---|
| 712 | logRecord.setThreadName(entry.getThreadName()); |
|---|
| 713 | logRecord.setMessage(entry.getMessage()); |
|---|
| 714 | logRecord.setSolution(entry.getSolution()); |
|---|
| 715 | logRecord.setBusinessImpact( |
|---|
| 716 | entry.getBusinessImpact().toString()); |
|---|
| 717 | logRecord.setCategory( |
|---|
| 718 | entry.getCategory().toString()); |
|---|
| 719 | |
|---|
| 720 | logRecord.setMessage(entry.getMessage()); |
|---|
| 721 | |
|---|
| 722 | final Set params = entry.getParameterNames(); |
|---|
| 723 | for (final Iterator iter = params.iterator(); iter.hasNext(); ) |
|---|
| 724 | { |
|---|
| 725 | final String parameterName = (String) iter.next(); |
|---|
| 726 | final ParameterType param |
|---|
| 727 | = mXmlObjectsPool.borrowParameter(); |
|---|
| 728 | |
|---|
| 729 | param.setName(parameterName); |
|---|
| 730 | final List values |
|---|
| 731 | = entry.getParameterValues(parameterName); |
|---|
| 732 | if (values != null) |
|---|
| 733 | { |
|---|
| 734 | for (final Iterator valueIter = values.iterator(); |
|---|
| 735 | valueIter.hasNext(); ) |
|---|
| 736 | { |
|---|
| 737 | param.getValue().add(valueIter.next().toString()); |
|---|
| 738 | } |
|---|
| 739 | } |
|---|
| 740 | logRecord.getParameter().add(param); |
|---|
| 741 | } |
|---|
| 742 | fillStacktrace(logRecord, entry); |
|---|
| 743 | } |
|---|
| 744 | |
|---|
| 745 | /** |
|---|
| 746 | * Fills the stack trace according to the display options. |
|---|
| 747 | * |
|---|
| 748 | * @param logRecord The jaxb log record object to be filled, |
|---|
| 749 | * @param entry The log entry from which to get the information. |
|---|
| 750 | */ |
|---|
| 751 | private void fillStacktrace (LogRecordType logRecord, LogItem entry) |
|---|
| 752 | { |
|---|
| 753 | if (displayStackTrace(entry)) |
|---|
| 754 | { |
|---|
| 755 | logRecord.setStacktrace(getStackTrace(entry)); |
|---|
| 756 | } |
|---|
| 757 | } |
|---|
| 758 | |
|---|
| 759 | private void fillException ( |
|---|
| 760 | final ExceptionType exception, |
|---|
| 761 | final LogItem entry) |
|---|
| 762 | { |
|---|
| 763 | exception.setMessage(entry.getMessage()); |
|---|
| 764 | if (displayStackTrace(entry)) |
|---|
| 765 | { |
|---|
| 766 | exception.setStacktrace(getStackTrace(entry)); |
|---|
| 767 | } |
|---|
| 768 | } |
|---|
| 769 | |
|---|
| 770 | private StacktraceType getStackTrace (final LogItem entry) |
|---|
| 771 | { |
|---|
| 772 | final StacktraceType rc; |
|---|
| 773 | if (entry.getStackTraceLines().isEmpty()) |
|---|
| 774 | { |
|---|
| 775 | rc = null; |
|---|
| 776 | } |
|---|
| 777 | else |
|---|
| 778 | { |
|---|
| 779 | rc = mXmlObjectsPool.borrowStacktrace(); |
|---|
| 780 | fillStackTrace(rc, entry); |
|---|
| 781 | } |
|---|
| 782 | return rc; |
|---|
| 783 | } |
|---|
| 784 | |
|---|
| 785 | private void fillStackTrace ( |
|---|
| 786 | final StacktraceType stack, |
|---|
| 787 | final LogItem entry) |
|---|
| 788 | { |
|---|
| 789 | for (final Iterator iter = entry.getStackTraceLines().iterator(); |
|---|
| 790 | iter.hasNext(); ) |
|---|
| 791 | { |
|---|
| 792 | final StackTraceInfo info = (StackTraceInfo) iter.next(); |
|---|
| 793 | |
|---|
| 794 | // not interested in lines, which contain the exception message, |
|---|
| 795 | // this information is stored in other elements already. |
|---|
| 796 | if (info.isLocationLine()) |
|---|
| 797 | { |
|---|
| 798 | final FrameType frame = mXmlObjectsPool.borrowFrame(); |
|---|
| 799 | frame.setSourceClass(info.getClassName()); |
|---|
| 800 | frame.setSourceMethod(info.getMethodName()); |
|---|
| 801 | if (info.getLine() != 0) |
|---|
| 802 | { |
|---|
| 803 | frame.setSourceLine(BigInteger.valueOf(info.getLine())); |
|---|
| 804 | } |
|---|
| 805 | stack.getStacktraceElement().add(frame); |
|---|
| 806 | } |
|---|
| 807 | else if (info.isMoreLine()) |
|---|
| 808 | { |
|---|
| 809 | Assert.assertTrue(info + " must be last line of a StackTrace,", |
|---|
| 810 | ! iter.hasNext()); |
|---|
| 811 | fillMoreStackTrace(entry, stack, info); |
|---|
| 812 | } |
|---|
| 813 | } |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | /** |
|---|
| 817 | * This fills the stack trace if a '...nnn more' line has been encountered. |
|---|
| 818 | * According to the display settings it might be necessary to take the stack |
|---|
| 819 | * trace lines of parent entries. |
|---|
| 820 | * |
|---|
| 821 | * @param entry The log file entry with the current stack trace line. |
|---|
| 822 | * @param stack The jaxb stack trace object, into which to fill the stack |
|---|
| 823 | * trace. |
|---|
| 824 | * @param info The current stack trace info from <code>entry</code>. |
|---|
| 825 | */ |
|---|
| 826 | private void fillMoreStackTrace ( |
|---|
| 827 | final LogItem entry, |
|---|
| 828 | final StacktraceType stack, |
|---|
| 829 | final StackTraceInfo info) |
|---|
| 830 | { |
|---|
| 831 | final LogItem stackTraceEntry |
|---|
| 832 | = getEntryForMoreStackTrace(entry, info); |
|---|
| 833 | if (stackTraceEntry == null) |
|---|
| 834 | { |
|---|
| 835 | throw new IllegalStateException("Did not find correct stack trace to " |
|---|
| 836 | + "display for " + entry); |
|---|
| 837 | } |
|---|
| 838 | else if (entry == stackTraceEntry) |
|---|
| 839 | { |
|---|
| 840 | // in this case the stack trace has been displayed already and we can |
|---|
| 841 | // display the more line again. |
|---|
| 842 | final FrameType frame = mXmlObjectsPool.borrowFrame(); |
|---|
| 843 | frame.setSourceClass(info.toString()); |
|---|
| 844 | frame.setSourceMethod(""); |
|---|
| 845 | stack.getStacktraceElement().add(frame); |
|---|
| 846 | } |
|---|
| 847 | else |
|---|
| 848 | { |
|---|
| 849 | fillStackTrace(stack, stackTraceEntry); |
|---|
| 850 | } |
|---|
| 851 | } |
|---|
| 852 | |
|---|
| 853 | private CauseType setCause ( |
|---|
| 854 | final LogItem entry, |
|---|
| 855 | final LogRecordType record) |
|---|
| 856 | { |
|---|
| 857 | final CauseType rc; |
|---|
| 858 | if (entry != null) |
|---|
| 859 | { |
|---|
| 860 | rc = mXmlObjectsPool.borrowCause(); |
|---|
| 861 | record.setCause(rc); |
|---|
| 862 | } |
|---|
| 863 | else |
|---|
| 864 | { |
|---|
| 865 | rc = null; |
|---|
| 866 | } |
|---|
| 867 | return rc; |
|---|
| 868 | } |
|---|
| 869 | |
|---|
| 870 | private CauseType setCause ( |
|---|
| 871 | final LogItem entry, |
|---|
| 872 | final ExceptionType exception) |
|---|
| 873 | { |
|---|
| 874 | final CauseType rc; |
|---|
| 875 | if (entry != null) |
|---|
| 876 | { |
|---|
| 877 | rc = mXmlObjectsPool.borrowCause(); |
|---|
| 878 | exception.setCause(rc); |
|---|
| 879 | } |
|---|
| 880 | else |
|---|
| 881 | { |
|---|
| 882 | rc = null; |
|---|
| 883 | } |
|---|
| 884 | return rc; |
|---|
| 885 | } |
|---|
| 886 | } |
|---|