1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.pool2.impl;
18
19 import java.io.PrintWriter;
20 import java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22
23
24
25
26
27
28
29
30
31 public class ThrowableCallStack implements CallStack {
32
33
34
35
36 private static class Snapshot extends Throwable {
37 private static final long serialVersionUID = 1L;
38 private final long timestampMillis = System.currentTimeMillis();
39 }
40
41 private final String messageFormat;
42
43
44 private final DateFormat dateFormat;
45
46 private volatile Snapshot snapshot;
47
48
49
50
51
52
53
54 public ThrowableCallStack(final String messageFormat, final boolean useTimestamp) {
55 this.messageFormat = messageFormat;
56 this.dateFormat = useTimestamp ? new SimpleDateFormat(messageFormat) : null;
57 }
58
59 @Override
60 public void clear() {
61 snapshot = null;
62 }
63
64 @Override
65 public void fillInStackTrace() {
66 snapshot = new Snapshot();
67 }
68
69 @Override
70 public synchronized boolean printStackTrace(final PrintWriter writer) {
71 final Snapshot snapshotRef = this.snapshot;
72 if (snapshotRef == null) {
73 return false;
74 }
75 final String message;
76 if (dateFormat == null) {
77 message = messageFormat;
78 } else {
79 synchronized (dateFormat) {
80 message = dateFormat.format(Long.valueOf(snapshotRef.timestampMillis));
81 }
82 }
83 writer.println(message);
84 snapshotRef.printStackTrace(writer);
85 return true;
86 }
87 }