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;
18
19 import net.sf.ehcache.CacheException;
20 import net.sf.ehcache.Ehcache;
21 import net.sf.ehcache.Element;
22
23 /***
24 * An implementation of {@link LiveCacheStatistics} and also implements {@link LiveCacheStatisticsData}. Uses separate delegates depending
25 * on whether
26 * statistics is enabled or not.
27 * <p />
28 * To collect statistics element put/update/remove/expired data, instances of this class must be registered as a CacheEventListener to a
29 * Cache
30 *
31 * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
32 * @since 1.7
33 */
34 public class LiveCacheStatisticsWrapper implements LiveCacheStatistics, LiveCacheStatisticsData {
35
36 private static final LiveCacheStatistics NULL_LIVE_CACHE_STATISTICS = new NullLiveCacheStatisticsData();
37
38 private final LiveCacheStatisticsImpl liveDelegate;
39 private volatile LiveCacheStatistics delegate;
40
41 /***
42 * Constructor accepting the backing cache.
43 *
44 * @param cache
45 */
46 public LiveCacheStatisticsWrapper(Ehcache cache) {
47 liveDelegate = new LiveCacheStatisticsImpl(cache);
48
49 setStatisticsEnabled(true);
50 }
51
52 /***
53 * {@inheritDoc}
54 *
55 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#setStatisticsEnabled(boolean)
56 */
57 public void setStatisticsEnabled(boolean enableStatistics) {
58 if (enableStatistics) {
59 delegate = liveDelegate;
60 } else {
61 delegate = NULL_LIVE_CACHE_STATISTICS;
62 }
63 }
64
65 /***
66 * {@inheritDoc}
67 *
68 * @see net.sf.ehcache.statistics.LiveCacheStatistics#isStatisticsEnabled()
69 */
70 public boolean isStatisticsEnabled() {
71 return delegate instanceof LiveCacheStatisticsImpl;
72 }
73
74 /***
75 * {@inheritDoc}
76 *
77 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#registerCacheUsageListener(net.sf.ehcache.statistics.CacheUsageListener)
78 */
79 public void registerCacheUsageListener(CacheUsageListener cacheUsageListener) throws IllegalStateException {
80
81 liveDelegate.registerCacheUsageListener(cacheUsageListener);
82 }
83
84 /***
85 * {@inheritDoc}
86 *
87 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#removeCacheUsageListener(net.sf.ehcache.statistics.CacheUsageListener)
88 */
89 public void removeCacheUsageListener(CacheUsageListener cacheUsageListener) throws IllegalStateException {
90
91 liveDelegate.removeCacheUsageListener(cacheUsageListener);
92 }
93
94 /***
95 * {@inheritDoc}
96 *
97 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#setStatisticsAccuracy(int)
98 */
99 public void setStatisticsAccuracy(int statisticsAccuracy) {
100
101 liveDelegate.setStatisticsAccuracy(statisticsAccuracy);
102 }
103
104 /***
105 * {@inheritDoc}
106 *
107 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getStatisticsAccuracy()
108 */
109 public int getStatisticsAccuracy() {
110
111 return liveDelegate.getStatisticsAccuracy();
112 }
113
114 /***
115 * {@inheritDoc}
116 *
117 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getStatisticsAccuracyDescription()
118 */
119 public String getStatisticsAccuracyDescription() {
120
121 return liveDelegate.getStatisticsAccuracyDescription();
122 }
123
124 /***
125 * {@inheritDoc}
126 *
127 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getCacheName()
128 */
129 public String getCacheName() {
130
131 return liveDelegate.getCacheName();
132 }
133
134 /***
135 * {@inheritDoc}
136 *
137 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getSize()
138 */
139 public long getSize() {
140 return delegate.getSize();
141 }
142
143 /***
144 * {@inheritDoc}
145 *
146 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getInMemorySize()
147 * @deprecated see {@link #getLocalHeapSize()}
148 */
149 @Deprecated
150 public long getInMemorySize() {
151 return delegate.getInMemorySize();
152 }
153
154 /***
155 * {@inheritDoc}
156 *
157 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getOffHeapSize()
158 * @deprecated see {@link #getLocalOffHeapSize()}
159 */
160 @Deprecated
161 public long getOffHeapSize() {
162 return delegate.getOffHeapSize();
163 }
164
165 /***
166 * {@inheritDoc}
167 *
168 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getOnDiskSize()
169 * @deprecated see {@link #getLocalDiskSize()}
170 */
171 @Deprecated
172 public long getOnDiskSize() {
173 return delegate.getOnDiskSize();
174 }
175
176 /***
177 * {@inheritDoc}
178 */
179 public long getLocalHeapSize() {
180 return delegate.getLocalHeapSize();
181 }
182
183 /***
184 * {@inheritDoc}
185 */
186 public long getLocalOffHeapSize() {
187 return delegate.getLocalOffHeapSize();
188 }
189
190 /***
191 * {@inheritDoc}
192 */
193 public long getLocalDiskSize() {
194 return delegate.getLocalDiskSize();
195 }
196
197 /***
198 * Returns the usage of local heap in bytes
199 * @return memory usage in bytes
200 */
201 public long getLocalHeapSizeInBytes() {
202 return delegate.getLocalHeapSizeInBytes();
203 }
204
205 /***
206 * Returns the usage of local off heap in bytes
207 * @return memory usage in bytes
208 */
209 public long getLocalOffHeapSizeInBytes() {
210 return delegate.getLocalOffHeapSizeInBytes();
211 }
212
213 /***
214 * Returns the usage of local disks in bytes
215 * @return memory usage in bytes
216 */
217 public long getLocalDiskSizeInBytes() {
218 return delegate.getLocalDiskSizeInBytes();
219 }
220
221 /***
222 * {@inheritDoc}
223 *
224 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getAverageGetTimeMillis()
225 */
226 public float getAverageGetTimeMillis() {
227 return delegate.getAverageGetTimeMillis();
228 }
229
230 /***
231 * {@inheritDoc}
232 *
233 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getCacheHitCount()
234 */
235 public long getCacheHitCount() {
236 return delegate.getCacheHitCount();
237 }
238
239 /***
240 * {@inheritDoc}
241 *
242 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getCacheMissCount()
243 */
244 public long getCacheMissCount() {
245 return delegate.getCacheMissCount();
246 }
247
248 /***
249 * {@inheritDoc}
250 *
251 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getInMemoryMissCount()
252 */
253 public long getInMemoryMissCount() {
254 return delegate.getInMemoryMissCount();
255 }
256
257 /***
258 * {@inheritDoc}
259 *
260 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getOffHeapMissCount()
261 */
262 public long getOffHeapMissCount() {
263 return delegate.getOffHeapMissCount();
264 }
265
266 /***
267 * {@inheritDoc}
268 *
269 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getOnDiskMissCount()
270 */
271 public long getOnDiskMissCount() {
272 return delegate.getOnDiskMissCount();
273 }
274
275 /***
276 * {@inheritDoc}
277 *
278 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getCacheMissCountExpired()
279 */
280 public long getCacheMissCountExpired() {
281 return delegate.getCacheMissCountExpired();
282 }
283
284 /***
285 * {@inheritDoc}
286 *
287 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getEvictedCount()
288 */
289 public long getEvictedCount() {
290 return delegate.getEvictedCount();
291 }
292
293 /***
294 * {@inheritDoc}
295 *
296 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getExpiredCount()
297 */
298 public long getExpiredCount() {
299 return delegate.getExpiredCount();
300 }
301
302 /***
303 * {@inheritDoc}
304 *
305 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getInMemoryHitCount()
306 */
307 public long getInMemoryHitCount() {
308 return delegate.getInMemoryHitCount();
309 }
310
311 /***
312 * {@inheritDoc}
313 *
314 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getOffHeapHitCount()
315 */
316 public long getOffHeapHitCount() {
317 return delegate.getOffHeapHitCount();
318 }
319
320 /***
321 * {@inheritDoc}
322 *
323 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getOnDiskHitCount()
324 */
325 public long getOnDiskHitCount() {
326 return delegate.getOnDiskHitCount();
327 }
328
329 /***
330 * {@inheritDoc}
331 *
332 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getPutCount()
333 */
334 public long getPutCount() {
335 return delegate.getPutCount();
336 }
337
338 /***
339 * {@inheritDoc}
340 *
341 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getRemovedCount()
342 */
343 public long getRemovedCount() {
344 return delegate.getRemovedCount();
345 }
346
347 /***
348 * {@inheritDoc}
349 *
350 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getUpdateCount()
351 */
352 public long getUpdateCount() {
353 return delegate.getUpdateCount();
354 }
355
356 private LiveCacheStatisticsData getDelegateAsLiveStatisticsData() {
357 return (LiveCacheStatisticsData) delegate;
358 }
359
360 /***
361 * {@inheritDoc}
362 *
363 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#addGetTimeMillis(long)
364 */
365 public void addGetTimeMillis(long millis) {
366 getDelegateAsLiveStatisticsData().addGetTimeMillis(millis);
367 }
368
369 /***
370 * {@inheritDoc}
371 *
372 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMaxGetTimeMillis()
373 */
374 public long getMaxGetTimeMillis() {
375 return delegate.getMaxGetTimeMillis();
376 }
377
378 /***
379 * {@inheritDoc}
380 *
381 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getXaCommitCount()
382 */
383 public long getXaCommitCount() {
384 return delegate.getXaCommitCount();
385 }
386
387 /***
388 * {@inheritDoc}
389 *
390 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getXaRollbackCount()
391 */
392 public long getXaRollbackCount() {
393 return delegate.getXaRollbackCount();
394 }
395
396 /***
397 * {@inheritDoc}
398 */
399 public long getWriterQueueLength() {
400 return delegate.getWriterQueueLength();
401 }
402
403 /***
404 * {@inheritDoc}
405 *
406 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMinGetTimeMillis()
407 */
408 public long getMinGetTimeMillis() {
409 return delegate.getMinGetTimeMillis();
410 }
411
412 /***
413 * {@inheritDoc}
414 *
415 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheHitInMemory()
416 */
417 public void cacheHitInMemory() {
418 getDelegateAsLiveStatisticsData().cacheHitInMemory();
419 }
420
421 /***
422 * {@inheritDoc}
423 *
424 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheHitOffHeap()
425 */
426 public void cacheHitOffHeap() {
427 getDelegateAsLiveStatisticsData().cacheHitOffHeap();
428 }
429
430 /***
431 * {@inheritDoc}
432 *
433 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheHitOnDisk()
434 */
435 public void cacheHitOnDisk() {
436 getDelegateAsLiveStatisticsData().cacheHitOnDisk();
437 }
438
439 /***
440 * {@inheritDoc}
441 *
442 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheMissExpired()
443 */
444 public void cacheMissExpired() {
445 getDelegateAsLiveStatisticsData().cacheMissExpired();
446 }
447
448 /***
449 * {@inheritDoc}
450 *
451 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#xaCommit()
452 */
453 public void xaCommit() {
454 getDelegateAsLiveStatisticsData().xaCommit();
455 }
456
457 /***
458 * {@inheritDoc}
459 *
460 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#xaRollback()
461 */
462 public void xaRollback() {
463 getDelegateAsLiveStatisticsData().xaRollback();
464 }
465
466 /***
467 * {@inheritDoc}
468 *
469 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheMissNotFound()
470 */
471 public void cacheMissNotFound() {
472 getDelegateAsLiveStatisticsData().cacheMissNotFound();
473 }
474
475 /***
476 * {@inheritDoc}
477 *
478 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheMissNotFound()
479 */
480 public void cacheMissInMemory() {
481 getDelegateAsLiveStatisticsData().cacheMissInMemory();
482 }
483
484 /***
485 * {@inheritDoc}
486 *
487 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheMissNotFound()
488 */
489 public void cacheMissOffHeap() {
490 getDelegateAsLiveStatisticsData().cacheMissOffHeap();
491 }
492
493 /***
494 * {@inheritDoc}
495 *
496 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#cacheMissNotFound()
497 */
498 public void cacheMissOnDisk() {
499 getDelegateAsLiveStatisticsData().cacheMissOnDisk();
500 }
501
502 /***
503 * {@inheritDoc}
504 *
505 * @see net.sf.ehcache.statistics.LiveCacheStatisticsData#clearStatistics()
506 */
507 public void clearStatistics() {
508 getDelegateAsLiveStatisticsData().clearStatistics();
509 }
510
511 /***
512 * {@inheritDoc}
513 *
514 * @see net.sf.ehcache.event.CacheEventListener#dispose()
515 */
516 public void dispose() {
517 getDelegateAsLiveStatisticsData().dispose();
518 }
519
520 /***
521 * {@inheritDoc}
522 *
523 * @see net.sf.ehcache.event.CacheEventListener#notifyElementEvicted(net.sf.ehcache.Ehcache, net.sf.ehcache.Element)
524 */
525 public void notifyElementEvicted(Ehcache cache, Element element) {
526 getDelegateAsLiveStatisticsData().notifyElementEvicted(cache, element);
527 }
528
529 /***
530 * {@inheritDoc}
531 *
532 * @see net.sf.ehcache.event.CacheEventListener#notifyElementExpired(net.sf.ehcache.Ehcache, net.sf.ehcache.Element)
533 */
534 public void notifyElementExpired(Ehcache cache, Element element) {
535 getDelegateAsLiveStatisticsData().notifyElementExpired(cache, element);
536 }
537
538 /***
539 * {@inheritDoc}
540 *
541 * @see net.sf.ehcache.event.CacheEventListener#notifyElementPut(net.sf.ehcache.Ehcache, net.sf.ehcache.Element)
542 */
543 public void notifyElementPut(Ehcache cache, Element element) throws CacheException {
544 getDelegateAsLiveStatisticsData().notifyElementPut(cache, element);
545 }
546
547 /***
548 * {@inheritDoc}
549 *
550 * @see net.sf.ehcache.event.CacheEventListener#notifyElementRemoved(net.sf.ehcache.Ehcache, net.sf.ehcache.Element)
551 */
552 public void notifyElementRemoved(Ehcache cache, Element element) throws CacheException {
553 getDelegateAsLiveStatisticsData().notifyElementRemoved(cache, element);
554 }
555
556 /***
557 * {@inheritDoc}
558 *
559 * @see net.sf.ehcache.event.CacheEventListener#notifyElementUpdated(net.sf.ehcache.Ehcache, net.sf.ehcache.Element)
560 */
561 public void notifyElementUpdated(Ehcache cache, Element element) throws CacheException {
562 getDelegateAsLiveStatisticsData().notifyElementUpdated(cache, element);
563 }
564
565 /***
566 * {@inheritDoc}
567 *
568 * @see net.sf.ehcache.event.CacheEventListener#notifyRemoveAll(net.sf.ehcache.Ehcache)
569 */
570 public void notifyRemoveAll(Ehcache cache) {
571 getDelegateAsLiveStatisticsData().notifyRemoveAll(cache);
572 }
573
574 /***
575 * {@inheritDoc}
576 *
577 * @see java.lang.Object#clone()
578 */
579 @Override
580 public Object clone() throws CloneNotSupportedException {
581 super.clone();
582 throw new CloneNotSupportedException();
583 }
584 }