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.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.e2etrace.config.DefaultTraceConfig;
22 import org.e2etrace.config.ITraceConfig;
23 import org.e2etrace.config.PropertiesTraceConfig;
24
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
50
51
52
53
54
55
56
57
58
59 public abstract class AbstractTraceSessionManager implements ITraceSessionManager {
60
61 private static final Log log = LogFactory.getLog(AbstractTraceSessionManager.class);
62 private static final String CONFIG_PROPERTY = "e2etrace.configuration";
63
64 private ITraceConfig config;
65 private ITraceSession noopSession;
66
67
68
69
70
71 protected AbstractTraceSessionManager() {
72 this.config = initConfiguration();
73 this.noopSession = new NoopTraceSession();
74 }
75
76
77
78
79
80
81
82 private ITraceConfig initConfiguration() {
83 ITraceConfig initConfig = null;
84 String configFile;
85
86 configFile = System.getProperty(CONFIG_PROPERTY);
87
88 if (configFile != null) {
89 try {
90 if (configFile.endsWith(".properties")) {
91 PropertiesTraceConfig propConfig = new PropertiesTraceConfig();
92
93 propConfig.loadConfigFile("/" + configFile);
94
95 initConfig = propConfig;
96 }
97 } catch (Exception e) {
98 log
99 .error(
100 "Error while initializing e2etrace configuration! Will use default configuration instead.",e);
101 }
102 }
103
104
105
106 if (initConfig == null) {
107 initConfig = new DefaultTraceConfig();
108 }
109
110 return initConfig;
111
112 }
113
114
115
116
117
118
119
120
121
122
123
124 protected abstract ITraceSession requestCurrentSession();
125
126
127
128
129
130
131
132
133
134
135
136 protected abstract void assignCurrentSession(ITraceSession session);
137
138
139 public final void setConfig(ITraceConfig config) {
140 this.config = config;
141
142 }
143
144
145 public final ITraceConfig getConfig() {
146 return this.config;
147 }
148
149
150 public ITraceSession getCurrentSession() {
151 ITraceSession currentSession;
152
153
154 if (getConfig().isTraceEnabled()) {
155 currentSession = requestCurrentSession();
156
157 if( currentSession == null ) {
158 currentSession = this.noopSession;
159 }
160 } else {
161 currentSession = this.noopSession;
162 }
163
164 return currentSession;
165 }
166
167
168 public void setCurrentSession(ITraceSession session) {
169
170 if (session.getRootStep() == null || !(getConfig().isTraceEnabledForId(session.getRootStep().getId()))) {
171 assignCurrentSession(this.noopSession);
172 } else {
173 session.setConfig(this.getConfig());
174 assignCurrentSession(session);
175 }
176
177 }
178
179 }