%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
net.sf.infrared.agent.MultipleEntryGuard$1 |
|
|
1 | /* |
|
2 | * Copyright 2005 Tavant Technologies and Contributors |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | * |
|
16 | * |
|
17 | * |
|
18 | * Original Author: kamal.govindraj (Tavant Technologies) |
|
19 | * Contributor(s): -; |
|
20 | * |
|
21 | */ |
|
22 | package net.sf.infrared.agent; |
|
23 | ||
24 | import org.apache.log4j.Logger; |
|
25 | ||
26 | import net.sf.infrared.base.model.ExecutionTimer; |
|
27 | import net.sf.infrared.base.util.LoggingFactory; |
|
28 | ||
29 | /** |
|
30 | * |
|
31 | * @author kamal.govindraj |
|
32 | */ |
|
33 | public class MultipleEntryGuard implements MonitorFacade { |
|
34 | ||
35 | private static final Logger log = LoggingFactory.getLogger(MultipleEntryGuard.class); |
|
36 | ||
37 | // private static final String KEY_JDBC_MONITORING_ENABLED = "jdbc-monitoring-enable"; |
|
38 | ||
39 | private static final long NO_OF_FATAL_ERRORS_TOLERATED = 10; |
|
40 | ||
41 | private static final int THRESHOLD = 30; |
|
42 | ||
43 | private MonitorFacade delegate; |
|
44 | ||
45 | private int fatalErrors = 0; |
|
46 | ||
47 | private long methodStartTime = 0; |
|
48 | ||
49 | private ThreadLocal callInProgress = new ThreadLocal() { |
|
50 | 3 | protected synchronized Object initialValue() { |
51 | 0 | return Boolean.FALSE; |
52 | } |
|
53 | }; |
|
54 | ||
55 | public MultipleEntryGuard(MonitorFacade delegate) { |
|
56 | if (delegate == null) { |
|
57 | throw new IllegalArgumentException("Delate MonitorFacade cannot be null"); |
|
58 | } |
|
59 | this.delegate = delegate; |
|
60 | } |
|
61 | ||
62 | public StatisticsCollector recordExecutionBegin(ExecutionTimer timer) { |
|
63 | // if (isCallInProgress()) { |
|
64 | // return null; |
|
65 | // } |
|
66 | try { |
|
67 | // startCall(); |
|
68 | return delegate.recordExecutionBegin(timer); |
|
69 | } catch (Throwable t) { |
|
70 | handleError(); |
|
71 | log.error("Exception generated from InfraRED", t); |
|
72 | return null; |
|
73 | } |
|
74 | // finally { |
|
75 | // endCall(); |
|
76 | // } |
|
77 | } |
|
78 | ||
79 | public void recordExecutionEnd(ExecutionTimer timer) { |
|
80 | // if (isCallInProgress()) { |
|
81 | // return; |
|
82 | // } |
|
83 | try { |
|
84 | // startCall(); |
|
85 | delegate.recordExecutionEnd(timer); |
|
86 | } catch (Throwable t) { |
|
87 | handleError(); |
|
88 | log.error("Exception generated from InfraRED", t); |
|
89 | } |
|
90 | // finally { |
|
91 | // endCall(); |
|
92 | // } |
|
93 | } |
|
94 | ||
95 | public void recordExecutionEnd(ExecutionTimer timer, StatisticsCollector collector) { |
|
96 | // if (isCallInProgress()) { |
|
97 | // return; |
|
98 | // } |
|
99 | try { |
|
100 | // startCall(); |
|
101 | delegate.recordExecutionEnd(timer, collector); |
|
102 | } catch (Throwable t) { |
|
103 | handleError(); |
|
104 | log.error("Exception generated from InfraRED", t); |
|
105 | } |
|
106 | // finally { |
|
107 | // endCall(); |
|
108 | // } |
|
109 | } |
|
110 | ||
111 | public boolean isMonitoringEnabled() { |
|
112 | return delegate.isMonitoringEnabled(); |
|
113 | } |
|
114 | ||
115 | public MonitorConfig getConfiguration() { |
|
116 | return delegate.getConfiguration(); |
|
117 | } |
|
118 | ||
119 | private boolean isCallInProgress() { |
|
120 | // We put only Boolean.TRUE and Boolean.FALSE in the threadlocal |
|
121 | // so identity comparison against those will work |
|
122 | return ((Boolean) callInProgress.get()) == Boolean.TRUE; |
|
123 | } |
|
124 | ||
125 | public String getApplicationName() { |
|
126 | return delegate.getApplicationName(); |
|
127 | } |
|
128 | ||
129 | public String getInstanceId() { |
|
130 | return delegate.getInstanceId(); |
|
131 | } |
|
132 | ||
133 | public void destroy() { |
|
134 | delegate.destroy(); |
|
135 | } |
|
136 | ||
137 | private void startCall() { |
|
138 | // if (log.isDebugEnabled()) { |
|
139 | // log.debug("\nInfraRED start on " + Thread.currentThread()); |
|
140 | // } |
|
141 | callInProgress.set(Boolean.TRUE); |
|
142 | //methodStartTime = System.currentTimeMillis(); |
|
143 | } |
|
144 | ||
145 | private void endCall() { |
|
146 | /* |
|
147 | long timeInInfraRed = System.currentTimeMillis() - methodStartTime; |
|
148 | if (timeInInfraRed > THRESHOLD) { |
|
149 | log.debug("The execution time in InfraRED is above the threshold"); |
|
150 | }*/ |
|
151 | //methodStartTime = 0; |
|
152 | callInProgress.set(Boolean.FALSE); |
|
153 | // if (log.isDebugEnabled()) { |
|
154 | // log.debug("InfraRED end on " + Thread.currentThread() + "\n"); |
|
155 | // } |
|
156 | } |
|
157 | ||
158 | private void handleError() { |
|
159 | fatalErrors++; |
|
160 | if (fatalErrors >= NO_OF_FATAL_ERRORS_TOLERATED) { |
|
161 | log.error("Error thresholds crossed, turning off infrared"); |
|
162 | // Turn off monitoring |
|
163 | delegate.getConfiguration().enableMonitoring(false); |
|
164 | delegate.getConfiguration().enableCallTracing(false); |
|
165 | // delegate.getConfiguration().setProperty(KEY_JDBC_MONITORING_ENABLED, false); |
|
166 | } |
|
167 | } |
|
168 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |