1 package org.e2etrace.trace;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.e2etrace.config.DefaultTraceConfig;
20 import org.e2etrace.config.ITraceConfig;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 public class DefaultTraceSession implements ITraceSession {
37
38 private ITraceStep root;
39 private ITraceStep current;
40 private ITraceStepFactory stepFactory;
41 private ITraceConfig tc;
42
43
44
45
46
47
48
49
50
51
52
53 public DefaultTraceSession(String sessionId, ITraceStepFactory stepFactory) {
54 this.root = new TraceSessionRootStep(new TraceSessionRootStepId(sessionId));
55 this.current = this.root;
56 this.stepFactory = stepFactory;
57 this.tc = new DefaultTraceConfig();
58 }
59
60
61 public void enterStep(Class clazz, String method) {
62 if (this.tc.isTraceEnabled()) {
63 this.enterStep(new MethodTraceStepId(clazz, method));
64 }
65 }
66
67
68 public void leaveStep(Class clazz, String method) {
69 if (this.tc.isTraceEnabled()) {
70 this.leaveStep(new MethodTraceStepId(clazz, method));
71 }
72 }
73
74
75 public void enterStep(String id) {
76 if (this.tc.isTraceEnabled()) {
77 this.enterStep(new SimpleTraceStepId(id));
78 }
79 }
80
81
82 public void leaveStep(String id) {
83 if (this.tc.isTraceEnabled()) {
84 this.leaveStep(new SimpleTraceStepId(id));
85 }
86 }
87
88
89 public void enterStep(ITraceStepId id) {
90 if (this.tc.isTraceEnabledForId(id)) {
91 ITraceStep step;
92
93 step = this.stepFactory.newInstance(id);
94
95
96 this.current.addChild(step);
97
98
99 this.current = step;
100
101
102 step.enter();
103 }
104
105 }
106
107
108 public void leaveStep(ITraceStepId id) {
109 if (this.tc.isTraceEnabledForId(id)) {
110 ITraceStep step = this.current;
111
112
113
114 if (!(this.current.getId().equals(id))) {
115
116
117 step = current.getParent();
118 while (step != null && step != root) {
119 if (step.getId().equals(id)) {
120 break;
121 }
122
123 step = step.getParent();
124
125 }
126
127 }
128
129 if (step != null) {
130
131 step.leave();
132 }
133
134 if (step != root && step != null) {
135
136 this.current = step.getParent();
137 }
138
139 }
140
141 }
142
143
144 public ITraceStep getRootStep() {
145 return this.root;
146 }
147
148
149 public long getDuration() {
150 long duration = 0L;
151 ITraceStep[] rootChildren = this.root.getChildren();
152
153
154
155 for (int i = 0; i < rootChildren.length; i++) {
156 duration += rootChildren[i].getDuration();
157 }
158
159 return duration;
160
161 }
162
163
164 public void setConfig(ITraceConfig tc) {
165 this.tc = tc;
166
167 }
168
169
170 public ITraceConfig getConfig() {
171 return this.tc;
172 }
173
174
175 public ITraceStep getCurrentStep() {
176 return this.current;
177 }
178 }