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
34 package info.magnolia.debug;
35
36 import info.magnolia.context.MgnlContext;
37
38 import java.util.HashMap;
39 import java.util.Map;
40
41
42
43
44 public class TrackingStatus {
45
46 private static final String ATTRIBUTE_NAME = TrackingStatus.class.getName();
47
48 private final Map<String, Long>[] traces = new Map[10];
49
50 private final boolean trace;
51 private final Object testString;
52
53
54
55
56
57 public static TrackingStatus getInstance() {
58 return getInstance(true);
59 }
60
61
62
63
64
65
66
67 public static TrackingStatus getInstance(boolean trace) {
68 return getInstance(true, null);
69 }
70
71
72
73
74
75
76
77
78
79 public static TrackingStatus getInstance(boolean trace, String testString) {
80 if(!MgnlContext.hasAttribute(ATTRIBUTE_NAME)) {
81 MgnlContext.setAttribute(ATTRIBUTE_NAME, new TrackingStatus(trace, testString));
82 }
83 return (TrackingStatus) MgnlContext.getAttribute(ATTRIBUTE_NAME);
84 }
85
86 public TrackingStatus(boolean trace, String testString) {
87 for (int i = 0; i < traces.length; i++) {
88 traces[i] = new HashMap<String, Long>();
89 }
90 this.trace = trace;
91 this.testString = testString;
92 }
93
94
95
96
97 public void track() {
98 if (!trace) {
99 return;
100 }
101 StackTraceElement[] trace = new Exception().getStackTrace();
102 int count = 0;
103 for (StackTraceElement el : trace) {
104 String strTrace = el.toString();
105 Map<String, Long> traceMap = traces[count];
106 Long sum = traceMap.get(strTrace);
107 if (sum != null) {
108 sum += 1;
109 } else {
110 sum = 1L;
111 }
112 traceMap.put(strTrace, sum);
113 count++;
114 if (count >= traces.length) {
115 break;
116 }
117 }
118 }
119
120 @Override
121
122
123
124 public String toString() {
125 StringBuilder sb = new StringBuilder();
126 if (totalInits > 0) {
127 sb.append("inits: ").append(totalInits).append(",");
128 }
129 if (totalCalls > 0) {
130 sb.append(", total calls: ").append(totalCalls).append(",");
131
132 }
133 if (totalTime > 0) {
134 sb.append("total time: ").append(totalTime);
135
136 }
137 sb.append("\n");
138 String ind = " ";
139 for (Map<String, Long> trace : traces) {
140 for (Map.Entry<String, Long> entry : trace.entrySet()) {
141 sb.append(ind).append(entry.getValue()).append(";").append(entry.getKey()).append("\n");
142 }
143 ind += " ";
144 }
145 return sb.toString();
146 }
147
148 public long totalCalls;
149 public long totalInits;
150 public long totalTime;
151
152
153
154
155 public void track(String str) {
156 if (testString == null || testString.equals(str)) {
157 track();
158 }
159 }
160 }