1 package org.e2etrace.trace;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Iterator;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.e2etrace.timer.ITimer;
24 import org.e2etrace.timer.ITimerFactory;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class DefaultTraceStep extends AbstractTraceStep {
50
51 private static final long serialVersionUID = 1L;
52 private static final Log log = LogFactory.getLog(DefaultTraceStep.class);
53
54 private long duration = 0L;
55 private boolean active = false;
56 private transient ITimer timer;
57 private transient ITimerFactory timerFactory;
58
59
60
61
62
63
64
65
66
67
68 DefaultTraceStep(ITraceStepId id, ITimerFactory timerFactory) {
69 super(id);
70
71 if (timerFactory == null) {
72 throw new IllegalArgumentException("timerFactory must not be null");
73 }
74
75 this.timerFactory = timerFactory;
76 }
77
78
79 public void enter() {
80 if (isActive()) {
81
82 log.warn("enter() has been called before for trace step '"
83 + this.getId().asString() + "'");
84
85 return;
86 }
87
88 if (timerFactory == null) {
89
90 log.warn("enter() cannot be invoked after Serialization of the trace step");
91
92 return;
93 }
94
95 if (log.isDebugEnabled()) {
96 log.debug(">>> Entering " + this.getId().asString());
97 }
98
99 this.timer = this.timerFactory.newInstance();
100 this.timer.start();
101 this.active = true;
102
103 }
104
105
106 public void leave() {
107 if (timerFactory == null) {
108
109 log.warn("leave() cannot be invoked after Serialization of the trace step");
110
111 return;
112 }
113
114
115 if (!isActive()) {
116
117
118 log
119 .warn("leave() has been called before OR leave() has been called without prior enter() for trace step '"
120 + this.getId().asString() + "'");
121
122 return;
123 }
124
125
126 leaveAllChildren();
127
128
129 this.duration = this.timer.measure();
130 this.active = false;
131
132 if (log.isDebugEnabled()) {
133 log.debug("<<< Leaving " + this.getId().asString() + " (" + this.duration + "ms)");
134 }
135
136 }
137
138
139 public boolean isActive() {
140 return this.active;
141 }
142
143
144 public long getDuration() {
145 return this.duration;
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 public long getIsolatedDuration() {
168 long duration;
169
170 duration = this.getDuration();
171
172
173 if (this.children != null) {
174 for (Iterator iter = this.children.iterator(); iter.hasNext();) {
175 ITraceStep element = (ITraceStep) iter.next();
176
177 duration -= element.getDuration();
178 }
179 }
180
181
182
183
184 if (duration < 0) {
185 duration = -1;
186 }
187 return duration;
188 }
189
190
191
192
193
194
195 public String toString() {
196 StringBuffer buffer = new StringBuffer();
197
198 buffer.append("[DefaultTraceStep:");
199 buffer.append(" id: ");
200 buffer.append(this.getId());
201 buffer.append(" duration: ");
202 buffer.append(duration);
203 buffer.append(" parent: ");
204 buffer.append(this.getParent());
205 buffer.append("]");
206
207 return buffer.toString();
208 }
209
210 }