1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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 package net.sf.ehcache.statistics.sampled;
18
19 import net.sf.ehcache.statistics.CacheUsageListener;
20 import net.sf.ehcache.util.FailSafeTimer;
21
22 /***
23 * An implementation of {@link SampledCacheStatistics} and also implements {@link CacheUsageListener} and depends on the notification
24 * received from
25 * these to update the stats. Uses separate delegates depending on whether
26 * sampled statistics is enabled or not.
27 * <p />
28 * To collect statistics data, instances of this class should be registered as a {@link CacheUsageListener} to a Cache
29 *
30 * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
31 * @since 1.7
32 */
33 public class SampledCacheStatisticsWrapper implements CacheUsageListener, SampledCacheStatistics {
34
35 private static final NullSampledCacheStatistics NULL_SAMPLED_CACHE_STATISTICS = new NullSampledCacheStatistics();
36
37 private volatile SampledCacheStatistics delegate;
38
39 /***
40 * Default constructor.
41 */
42 public SampledCacheStatisticsWrapper() {
43 delegate = new NullSampledCacheStatistics();
44 }
45
46 /***
47 * Enable sampled statistics collection
48 *
49 * @param timer
50 */
51 public void enableSampledStatistics(FailSafeTimer timer) {
52 delegate.dispose();
53 delegate = new SampledCacheStatisticsImpl(timer);
54 }
55
56 /***
57 * Disable sampled statistics collection
58 */
59 public void disableSampledStatistics() {
60 delegate.dispose();
61 delegate = NULL_SAMPLED_CACHE_STATISTICS;
62 }
63
64 /***
65 * {@inheritDoc}
66 */
67 public boolean isSampledStatisticsEnabled() {
68 return delegate instanceof SampledCacheStatisticsImpl;
69 }
70
71 /***
72 * {@inheritDoc}
73 *
74 */
75 public void dispose() {
76 delegate.dispose();
77 }
78
79 /***
80 * {@inheritDoc}
81 *
82 */
83 public long getAverageGetTimeMostRecentSample() {
84 return delegate.getAverageGetTimeMostRecentSample();
85 }
86
87 /***
88 * {@inheritDoc}
89 *
90 */
91 public long getCacheElementEvictedMostRecentSample() {
92 return delegate.getCacheElementEvictedMostRecentSample();
93 }
94
95 /***
96 * {@inheritDoc}
97 *
98 */
99 public long getCacheElementExpiredMostRecentSample() {
100 return delegate.getCacheElementExpiredMostRecentSample();
101 }
102
103 /***
104 * {@inheritDoc}
105 *
106 */
107 public long getCacheElementPutMostRecentSample() {
108 return delegate.getCacheElementPutMostRecentSample();
109 }
110
111 /***
112 * {@inheritDoc}
113 *
114 */
115 public long getCacheElementRemovedMostRecentSample() {
116 return delegate.getCacheElementRemovedMostRecentSample();
117 }
118
119 /***
120 * {@inheritDoc}
121 *
122 */
123 public long getCacheElementUpdatedMostRecentSample() {
124 return delegate.getCacheElementUpdatedMostRecentSample();
125 }
126
127 /***
128 * {@inheritDoc}
129 *
130 */
131 public long getCacheHitInMemoryMostRecentSample() {
132 return delegate.getCacheHitInMemoryMostRecentSample();
133 }
134
135 /***
136 * {@inheritDoc}
137 *
138 */
139 public long getCacheHitOffHeapMostRecentSample() {
140 return delegate.getCacheHitOffHeapMostRecentSample();
141 }
142
143 /***
144 * {@inheritDoc}
145 *
146 */
147 public long getCacheHitMostRecentSample() {
148 return delegate.getCacheHitMostRecentSample();
149 }
150
151 /***
152 * {@inheritDoc}
153 *
154 */
155 public long getCacheHitOnDiskMostRecentSample() {
156 return delegate.getCacheHitOnDiskMostRecentSample();
157 }
158
159 /***
160 * {@inheritDoc}
161 *
162 */
163 public long getCacheMissExpiredMostRecentSample() {
164 return delegate.getCacheMissExpiredMostRecentSample();
165 }
166
167 /***
168 * {@inheritDoc}
169 *
170 */
171 public long getCacheMissMostRecentSample() {
172 return delegate.getCacheMissMostRecentSample();
173 }
174
175 /***
176 * {@inheritDoc}
177 *
178 */
179 public long getCacheMissInMemoryMostRecentSample() {
180 return delegate.getCacheMissInMemoryMostRecentSample();
181 }
182
183 /***
184 * {@inheritDoc}
185 *
186 */
187 public long getCacheMissOffHeapMostRecentSample() {
188 return delegate.getCacheMissOffHeapMostRecentSample();
189 }
190
191 /***
192 * {@inheritDoc}
193 *
194 */
195 public long getCacheMissOnDiskMostRecentSample() {
196 return delegate.getCacheMissOnDiskMostRecentSample();
197 }
198
199 /***
200 * {@inheritDoc}
201 *
202 */
203 public long getCacheMissNotFoundMostRecentSample() {
204 return delegate.getCacheMissNotFoundMostRecentSample();
205 }
206
207 /***
208 * {@inheritDoc}
209 *
210 */
211 public int getStatisticsAccuracy() {
212 return delegate.getStatisticsAccuracy();
213 }
214
215 /***
216 * {@inheritDoc}
217 *
218 * @see net.sf.ehcache.statistics.sampled.SampledCacheStatistics#clearStatistics()
219 */
220 public void clearStatistics() {
221 delegate.clearStatistics();
222 }
223
224 /***
225 * {@inheritDoc}
226 *
227 */
228 public String getStatisticsAccuracyDescription() {
229 return delegate.getStatisticsAccuracyDescription();
230 }
231
232 private CacheUsageListener getDelegateAsListener() {
233 return (CacheUsageListener) delegate;
234 }
235
236 /***
237 * {@inheritDoc}
238 *
239 */
240 public void notifyCacheElementEvicted() {
241 getDelegateAsListener().notifyCacheElementEvicted();
242 }
243
244 /***
245 * {@inheritDoc}
246 *
247 */
248 public void notifyCacheElementExpired() {
249 getDelegateAsListener().notifyCacheElementExpired();
250 }
251
252 /***
253 * {@inheritDoc}
254 *
255 */
256 public void notifyCacheElementPut() {
257 getDelegateAsListener().notifyCacheElementPut();
258 }
259
260 /***
261 * {@inheritDoc}
262 *
263 */
264 public void notifyCacheElementRemoved() {
265 getDelegateAsListener().notifyCacheElementRemoved();
266 }
267
268 /***
269 * {@inheritDoc}
270 *
271 */
272 public void notifyCacheElementUpdated() {
273 getDelegateAsListener().notifyCacheElementUpdated();
274 }
275
276 /***
277 * {@inheritDoc}
278 *
279 */
280 public void notifyCacheHitInMemory() {
281 getDelegateAsListener().notifyCacheHitInMemory();
282 }
283
284 /***
285 * {@inheritDoc}
286 */
287 public void notifyCacheHitOffHeap() {
288 getDelegateAsListener().notifyCacheHitOffHeap();
289 }
290
291 /***
292 * {@inheritDoc}
293 *
294 */
295 public void notifyCacheHitOnDisk() {
296 getDelegateAsListener().notifyCacheHitOnDisk();
297 }
298
299 /***
300 * {@inheritDoc}
301 *
302 */
303 public void notifyCacheMissedWithExpired() {
304 getDelegateAsListener().notifyCacheMissedWithExpired();
305 }
306
307 /***
308 * {@inheritDoc}
309 *
310 */
311 public void notifyCacheMissedWithNotFound() {
312 getDelegateAsListener().notifyCacheMissedWithNotFound();
313 }
314
315 /***
316 * {@inheritDoc}
317 *
318 */
319 public void notifyCacheMissInMemory() {
320 getDelegateAsListener().notifyCacheMissInMemory();
321 }
322
323 /***
324 * {@inheritDoc}
325 *
326 */
327 public void notifyCacheMissOffHeap() {
328 getDelegateAsListener().notifyCacheMissOffHeap();
329 }
330
331 /***
332 * {@inheritDoc}
333 *
334 */
335 public void notifyCacheMissOnDisk() {
336 getDelegateAsListener().notifyCacheMissOnDisk();
337 }
338
339 /***
340 * {@inheritDoc}
341 *
342 */
343 public void notifyRemoveAll() {
344 getDelegateAsListener().notifyRemoveAll();
345 }
346
347 /***
348 * {@inheritDoc}
349 *
350 */
351 public void notifyStatisticsAccuracyChanged(int statisticsAccuracy) {
352 getDelegateAsListener().notifyStatisticsAccuracyChanged(statisticsAccuracy);
353 }
354
355 /***
356 * {@inheritDoc}
357 *
358 */
359 public void notifyStatisticsCleared() {
360 getDelegateAsListener().notifyStatisticsCleared();
361 }
362
363 /***
364 * {@inheritDoc}
365 *
366 */
367 public void notifyStatisticsEnabledChanged(boolean enableStatistics) {
368 getDelegateAsListener().notifyStatisticsEnabledChanged(enableStatistics);
369 }
370
371 /***
372 * {@inheritDoc}
373 *
374 */
375 public void notifyTimeTakenForGet(long millis) {
376 getDelegateAsListener().notifyTimeTakenForGet(millis);
377 }
378
379 /***
380 * {@inheritDoc}
381 */
382 public long getAverageSearchTime() {
383 return delegate.getAverageSearchTime();
384 }
385
386 /***
387 * {@inheritDoc}
388 */
389 public long getSearchesPerSecond() {
390 return delegate.getSearchesPerSecond();
391 }
392
393 /***
394 * {@inheritDoc}
395 */
396 public void notifyCacheSearch(long executeTime) {
397 getDelegateAsListener().notifyCacheSearch(executeTime);
398 }
399
400 /***
401 * {@inheritDoc}
402 */
403 public void notifyXaCommit() {
404 getDelegateAsListener().notifyXaCommit();
405 }
406
407 /***
408 * {@inheritDoc}
409 */
410 public void notifyXaRollback() {
411 getDelegateAsListener().notifyXaRollback();
412 }
413
414 /***
415 * {@inheritDoc}
416 */
417 public long getCacheXaCommitsMostRecentSample() {
418 return delegate.getCacheXaCommitsMostRecentSample();
419 }
420
421 /***
422 * {@inheritDoc}
423 */
424 public long getCacheXaRollbacksMostRecentSample() {
425 return delegate.getCacheXaRollbacksMostRecentSample();
426 }
427 }