1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
package net.sf.infrared.agent; |
23 |
|
|
24 |
|
import java.net.URL; |
25 |
|
import java.util.HashMap; |
26 |
|
import java.util.Map; |
27 |
|
import java.util.Properties; |
28 |
|
|
29 |
|
import net.sf.infrared.base.util.LoggingFactory; |
30 |
|
|
31 |
|
import org.apache.log4j.Logger; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
0 |
public class MonitorConfigImpl implements MonitorConfig { |
38 |
|
public static final String DEFAULT_CONFIG_LOCATION = "infrared-agent.properties"; |
39 |
|
|
40 |
6 |
private static final Logger log = LoggingFactory.getLogger(MonitorConfigImpl.class); |
41 |
|
|
42 |
|
private static final String KEY_ENABLE_MONITORING = "enable-monitoring"; |
43 |
|
|
44 |
|
private static final String KEY_ENABLE_CALL_TRACING = "enable-call-tracing"; |
45 |
|
|
46 |
|
private static final String KEY_PRUNE_THRESHOLD = "prune-threshold"; |
47 |
|
|
48 |
|
private static final String KEY_LAST_INVOCATIONS_COUNT = "last-invocations-to-trace"; |
49 |
|
|
50 |
|
private static final String KEY_COLLECTION_STRATEGY = "collection-strategy"; |
51 |
|
|
52 |
|
private static final boolean DEFAULT_ENABLE_MONITORING = false; |
53 |
|
|
54 |
3 |
private static final Boolean DEFAULT_ENABLE_MONITORING_FOR_CURRENT_THREAD = Boolean.TRUE; |
55 |
|
|
56 |
|
private static final boolean DEFAULT_ENABLE_CALL_TRACING = false; |
57 |
|
|
58 |
|
private static final long DEFAULT_PRUNE_THRESHOLD = 50; |
59 |
|
|
60 |
|
private static final int DEFAULT_LAST_INVOCATIONS_COUNT = 5; |
61 |
|
|
62 |
|
private static final String DEFAULT_COLLECTION_STRATEGY = |
63 |
|
"net.sf.infrared.agent.transport.impl.LoggingCollectionStrategy"; |
64 |
|
|
65 |
|
private Properties rawProps; |
66 |
|
|
67 |
7 |
private Map parsedProps = new HashMap(); |
68 |
|
|
69 |
|
private URL cfg; |
70 |
|
|
71 |
7 |
private boolean isCallFromInfraredPropertiesMBean = false; |
72 |
|
|
73 |
7 |
private ThreadLocal isMonitoringOnForCurrentThread = new ThreadLocal() { |
74 |
|
protected Object initialValue() { |
75 |
|
return DEFAULT_ENABLE_MONITORING_FOR_CURRENT_THREAD; |
76 |
|
} |
77 |
|
}; |
78 |
|
|
79 |
|
public MonitorConfigImpl() { |
80 |
0 |
this(DEFAULT_CONFIG_LOCATION); |
81 |
0 |
} |
82 |
|
|
83 |
3 |
public MonitorConfigImpl(String cfgLocation) { |
84 |
3 |
rawProps = new Properties(); |
85 |
|
try { |
86 |
3 |
ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
87 |
3 |
cfg = loader.getResource(cfgLocation); |
88 |
3 |
rawProps.load(cfg.openStream()); |
89 |
0 |
} catch (Throwable th) { |
90 |
0 |
log.fatal("Error loading configuration from " + cfgLocation |
91 |
|
+ ". Using defaults for all configuration", th); |
92 |
3 |
} |
93 |
3 |
} |
94 |
|
|
95 |
4 |
MonitorConfigImpl(Properties props) { |
96 |
4 |
this.rawProps = props; |
97 |
4 |
} |
98 |
|
|
99 |
|
public boolean isMonitoringEnabled() { |
100 |
0 |
return getProperty(KEY_ENABLE_MONITORING, DEFAULT_ENABLE_MONITORING); |
101 |
|
} |
102 |
|
|
103 |
|
public void enableMonitoring(boolean enable) { |
104 |
0 |
setProperty(KEY_ENABLE_MONITORING, enable); |
105 |
0 |
} |
106 |
|
|
107 |
|
public boolean isMonitoringEnabledForCurrentThread() { |
108 |
0 |
Boolean b = (Boolean) isMonitoringOnForCurrentThread.get(); |
109 |
0 |
return b.booleanValue(); |
110 |
|
} |
111 |
|
|
112 |
|
public void enableMonitoringForCurrentThread(boolean enable) { |
113 |
0 |
isMonitoringOnForCurrentThread.set(Boolean.valueOf(enable)); |
114 |
0 |
} |
115 |
|
|
116 |
|
public boolean isCallTracingEnabled() { |
117 |
0 |
return getProperty(KEY_ENABLE_CALL_TRACING, DEFAULT_ENABLE_CALL_TRACING); |
118 |
|
} |
119 |
|
|
120 |
|
public void enableCallTracing(boolean enable) { |
121 |
0 |
setProperty(KEY_ENABLE_CALL_TRACING, enable); |
122 |
0 |
} |
123 |
|
|
124 |
|
public long getPruneThreshold() { |
125 |
0 |
return getProperty(KEY_PRUNE_THRESHOLD, DEFAULT_PRUNE_THRESHOLD); |
126 |
|
} |
127 |
|
|
128 |
|
public void setPruneThreshold(long pruneThreshold) { |
129 |
0 |
setProperty(KEY_PRUNE_THRESHOLD, pruneThreshold); |
130 |
0 |
} |
131 |
|
|
132 |
|
public int getNoOfLastInvocationsToBeTracked() { |
133 |
0 |
return getProperty(KEY_LAST_INVOCATIONS_COUNT, DEFAULT_LAST_INVOCATIONS_COUNT); |
134 |
|
} |
135 |
|
|
136 |
|
public void setNoOfLastInvocationsToBeTracked(int n) { |
137 |
0 |
setProperty(KEY_LAST_INVOCATIONS_COUNT, n); |
138 |
0 |
} |
139 |
|
|
140 |
|
public String getCollectionStrategy() { |
141 |
3 |
return getProperty(KEY_COLLECTION_STRATEGY, DEFAULT_COLLECTION_STRATEGY); |
142 |
|
} |
143 |
|
|
144 |
|
public String getProperty(String propertyName, String defaultValue) { |
145 |
6 |
return rawProps.getProperty(propertyName, defaultValue); |
146 |
|
} |
147 |
|
|
148 |
|
public void setProperty(String propertyName, String value) { |
149 |
0 |
rawProps.setProperty(propertyName, value); |
150 |
0 |
parsedProps.remove(propertyName); |
151 |
0 |
} |
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
public int getProperty(String propertyName, class="keyword">int defaultValue) { |
161 |
3 |
Integer in = (Integer) parsedProps.get(propertyName); |
162 |
3 |
if (in != null) { |
163 |
0 |
return in.intValue(); |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
3 |
String value = getProperty(propertyName); |
176 |
|
|
177 |
3 |
if (value == null) { |
178 |
1 |
return defaultValue; |
179 |
|
} |
180 |
|
|
181 |
2 |
int i = defaultValue; |
182 |
|
try { |
183 |
2 |
i = Integer.parseInt(value); |
184 |
1 |
parsedProps.put(propertyName, new Integer(i)); |
185 |
1 |
} catch (NumberFormatException e) { |
186 |
1 |
log.error("Failed to parse value " + value + " for property " |
187 |
|
+ propertyName + " to integer, using default " |
188 |
|
+ defaultValue); |
189 |
1 |
} |
190 |
|
|
191 |
2 |
return i; |
192 |
|
} |
193 |
|
|
194 |
|
public void setProperty(String propertyName, int value) { |
195 |
0 |
setProperty(propertyName, Integer.toString(value)); |
196 |
0 |
} |
197 |
|
|
198 |
|
public long getProperty(String propertyName, class="keyword">long defaultValue) { |
199 |
3 |
Long lo = (Long) parsedProps.get(propertyName); |
200 |
3 |
if (lo != null) { |
201 |
0 |
return lo.longValue(); |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
3 |
String value = getProperty(propertyName); |
213 |
|
|
214 |
3 |
if (value == null) { |
215 |
1 |
return defaultValue; |
216 |
|
} |
217 |
|
|
218 |
2 |
long l = defaultValue; |
219 |
|
try { |
220 |
2 |
l = Long.parseLong(value); |
221 |
1 |
parsedProps.put(propertyName, new Long(l)); |
222 |
1 |
} catch (NumberFormatException e) { |
223 |
1 |
log.error("Failed to parse value " + value + " for property " + propertyName |
224 |
|
+ " to long, using default " + defaultValue); |
225 |
1 |
} |
226 |
|
|
227 |
2 |
return l; |
228 |
|
} |
229 |
|
|
230 |
|
public void setProperty(String propertyName, long value) { |
231 |
0 |
setProperty(propertyName, Long.toString(value)); |
232 |
0 |
} |
233 |
|
|
234 |
3 |
private static final String TRUE = "true".intern(); |
235 |
3 |
private static final String FALSE = "false".intern(); |
236 |
|
public boolean getProperty(String propertyName, class="keyword">boolean defaultValue) { |
237 |
8 |
Boolean bo = (Boolean) parsedProps.get(propertyName); |
238 |
8 |
if (bo != null) { |
239 |
2 |
return bo.booleanValue(); |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
6 |
String value = getProperty(propertyName); |
252 |
6 |
if (value == null) { |
253 |
2 |
return defaultValue; |
254 |
|
} |
255 |
4 |
value = value.trim().intern(); |
256 |
|
|
257 |
4 |
if (TRUE == value) { |
258 |
1 |
parsedProps.put(propertyName, Boolean.TRUE); |
259 |
1 |
return true; |
260 |
3 |
} else if (FALSE == value) { |
261 |
1 |
parsedProps.put(propertyName, Boolean.FALSE); |
262 |
1 |
return false; |
263 |
|
} else { |
264 |
2 |
log.error("Failed to parse value " + value + " for property " + propertyName |
265 |
|
+ " to boolean, using default " + defaultValue); |
266 |
2 |
return defaultValue; |
267 |
|
} |
268 |
|
} |
269 |
|
|
270 |
|
public void setProperty(String propertyName, boolean value) { |
271 |
0 |
setProperty(propertyName, Boolean.toString(value)); |
272 |
0 |
} |
273 |
|
|
274 |
|
public String toString() { |
275 |
0 |
return "MonitorConfig[" + cfg +"]"; |
276 |
|
} |
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
private String getProperty(String propertyName) { |
289 |
12 |
return rawProps.getProperty(propertyName); |
290 |
|
} |
291 |
|
} |