1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
package net.sf.infrared.agent; |
24 |
|
|
25 |
|
import net.sf.infrared.agent.transport.CollectionStrategy; |
26 |
|
import net.sf.infrared.agent.transport.impl.DoNothingCollectionStrategy; |
27 |
|
import net.sf.infrared.base.model.ExecutionTimer; |
28 |
|
import net.sf.infrared.base.util.LoggingFactory; |
29 |
|
|
30 |
|
import org.apache.log4j.Logger; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
public class MonitorFacadeImpl implements MonitorFacade { |
38 |
|
public static final String KEY_PRINT_FACADE_CREATION = "infrared.print.applications"; |
39 |
|
|
40 |
|
private static final Logger log = LoggingFactory.getLogger(MonitorFacadeImpl.class); |
41 |
|
|
42 |
|
private ThreadLocal statisticsCollector = new ThreadLocal() { |
43 |
4 |
protected synchronized Object initialValue() { |
44 |
0 |
if (log.isDebugEnabled()) { |
45 |
0 |
log.debug("Initializing statistics collector for thread " + Thread.currentThread() |
46 |
|
+ "\n" + "\t collectionStratergy = " + collectionStrategy + "\n" |
47 |
|
+ "\t applicationName = " + applicationName + "\n" + "\t instanceId = " |
48 |
|
+ instanceId); |
49 |
|
} |
50 |
0 |
return new StatisticsCollector(collectionStrategy, |
51 |
|
applicationName, instanceId, configuration); |
52 |
|
} |
53 |
|
}; |
54 |
|
|
55 |
|
private CollectionStrategy collectionStrategy = null; |
56 |
|
|
57 |
|
private String applicationName; |
58 |
|
|
59 |
|
private String instanceId; |
60 |
|
|
61 |
|
private MonitorConfig configuration; |
62 |
|
|
63 |
|
public MonitorFacadeImpl(String applicationName, |
64 |
|
String instanceId, MonitorConfig config, boolean print) { |
65 |
|
|
66 |
|
this(applicationName, instanceId, config); |
67 |
|
if (print && Boolean.getBoolean(KEY_PRINT_FACADE_CREATION)) { |
68 |
|
System.out.println("[InfraRED] Created MonitorFacade for " + applicationName |
69 |
|
+ ", instance " + instanceId + ", with config " + config); |
70 |
|
} |
71 |
|
} |
72 |
|
|
73 |
|
public MonitorFacadeImpl(String applicationName, String instanceId, MonitorConfig config) { |
74 |
|
if (log.isDebugEnabled()) { |
75 |
|
log.debug("Creating MonitorFacadeImpl for application " + applicationName + ", instance " |
76 |
|
+ instanceId); |
77 |
|
} |
78 |
|
|
79 |
|
this.applicationName = applicationName; |
80 |
|
this.instanceId = instanceId; |
81 |
|
this.configuration = config; |
82 |
|
|
83 |
|
if (log.isDebugEnabled()) { |
84 |
|
log.debug("Collection stategy for application " + applicationName + ", instance " |
85 |
|
+ instanceId + " is " + config.getCollectionStrategy()); |
86 |
|
} |
87 |
|
|
88 |
|
try { |
89 |
|
Class collectionStrategyClass = Class.forName(config.getCollectionStrategy()); |
90 |
|
collectionStrategy = (CollectionStrategy) collectionStrategyClass.newInstance(); |
91 |
|
collectionStrategy.init(config); |
92 |
|
} catch (ClassNotFoundException e) { |
93 |
|
log.error("Error creating MonitorFacadeImpl for application " + applicationName |
94 |
|
+ ", instance " + instanceId, e); |
95 |
|
} catch (InstantiationException e) { |
96 |
|
log.error("Error creating MonitorFacadeImpl for application " + applicationName |
97 |
|
+ ", instance " + instanceId, e); |
98 |
|
} catch (IllegalAccessException e) { |
99 |
|
log.error("Error creating MonitorFacadeImpl for application " + applicationName |
100 |
|
+ ", instance " + instanceId, e); |
101 |
|
} finally { |
102 |
|
if (collectionStrategy == null) { |
103 |
|
collectionStrategy = new DoNothingCollectionStrategy(); |
104 |
|
} |
105 |
|
} |
106 |
|
|
107 |
|
if (log.isDebugEnabled()) { |
108 |
|
log.debug("Created MonitorFacadeImpl for application " + applicationName + ", instance " |
109 |
|
+ instanceId + " with configuration " + config); |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
|
public StatisticsCollector recordExecutionBegin(ExecutionTimer timer) { |
114 |
|
StatisticsCollector collector = getStatisticsCollectorOfThisThread(); |
115 |
|
collector.recordExecutionBegin(timer); |
116 |
|
return collector; |
117 |
|
} |
118 |
|
|
119 |
|
public void recordExecutionEnd(ExecutionTimer timer) { |
120 |
|
recordExecutionEnd(timer, getStatisticsCollectorOfThisThread()); |
121 |
|
} |
122 |
|
|
123 |
|
public void recordExecutionEnd(ExecutionTimer timer, StatisticsCollector collector) { |
124 |
|
collector.recordExecutionEnd(timer); |
125 |
|
} |
126 |
|
|
127 |
|
public boolean isMonitoringEnabled() { |
128 |
|
return getConfiguration().isMonitoringEnabled() |
129 |
|
&& getConfiguration().isMonitoringEnabledForCurrentThread(); |
130 |
|
} |
131 |
|
|
132 |
|
public MonitorConfig getConfiguration() { |
133 |
|
return configuration; |
134 |
|
} |
135 |
|
|
136 |
|
public String toString() { |
137 |
|
return "MonitorFacadeImpl (app = " + applicationName + ", instance = " + instanceId + ")"; |
138 |
|
} |
139 |
|
|
140 |
|
public String getApplicationName() { |
141 |
|
return applicationName; |
142 |
|
} |
143 |
|
|
144 |
|
public String getInstanceId() { |
145 |
|
return instanceId; |
146 |
|
} |
147 |
|
|
148 |
|
public void destroy() { |
149 |
|
collectionStrategy.destroy(); |
150 |
|
} |
151 |
|
|
152 |
|
CollectionStrategy getCollectionStrategy() { |
153 |
|
return collectionStrategy; |
154 |
|
} |
155 |
|
|
156 |
|
StatisticsCollector getStatisticsCollectorOfThisThread() { |
157 |
|
return (StatisticsCollector) statisticsCollector.get(); |
158 |
|
} |
159 |
|
} |