KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > ExtendedCacheEntryEventListenerImpl


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.deliver.util;
25
26 import com.opensymphony.oscache.base.NeedsRefreshException;
27 import com.opensymphony.oscache.base.events.CacheEntryEvent;
28 import com.opensymphony.oscache.base.events.CachewideEvent;
29 import com.opensymphony.oscache.extra.CacheEntryEventListenerImpl;
30
31 public class ExtendedCacheEntryEventListenerImpl extends CacheEntryEventListenerImpl
32 {
33     private int totalSize = 0;
34
35     private int getOldSize(CacheEntryEvent event)
36     {
37         try
38         {
39             return event.getMap().getFromCache(event.getKey()).toString().length();
40         }
41         catch (NeedsRefreshException e)
42         {
43             event.getMap().cancelUpdate(event.getKey());
44             return 0;
45         }
46     }
47     
48     /**
49      * Handles the event fired when an entry is added in the cache.
50      *
51      * @param event The event triggered when a cache entry has been added
52      */

53     public void cacheEntryAdded(CacheEntryEvent event) {
54         super.cacheEntryAdded(event);
55         if(event.getEntry().getContent() instanceof byte[])
56             totalSize = totalSize + ((byte[])event.getEntry().getContent()).length * 8;
57         else
58             totalSize = totalSize + event.getEntry().getContent().toString().length() * 8;
59     }
60
61     /**
62      * Handles the event fired when an entry is flushed from the cache.
63      *
64      * @param event The event triggered when a cache entry has been flushed
65      */

66     public void cacheEntryFlushed(CacheEntryEvent event) {
67         super.cacheEntryFlushed(event);
68         totalSize = totalSize - getOldSize(event);
69     }
70
71     /**
72      * Handles the event fired when an entry is removed from the cache.
73      *
74      * @param event The event triggered when a cache entry has been removed
75      */

76     public void cacheEntryRemoved(CacheEntryEvent event) {
77         super.cacheEntryRemoved(event);
78         totalSize = totalSize - getOldSize(event);
79     }
80
81     /**
82      * Handles the event fired when an entry is updated in the cache.
83      *
84      * @param event The event triggered when a cache entry has been updated
85      */

86     public void cacheEntryUpdated(CacheEntryEvent event) {
87         super.cacheEntryRemoved(event);
88         //totalSize = totalSize - getOldSize(event);
89
}
90
91     /**
92      * Handles the event fired when a cache flush occurs.
93      *
94      * @param event The event triggered when an entire cache is flushed
95      */

96     public void cacheFlushed(CachewideEvent event) {
97         super.cacheFlushed(event);
98         totalSize = 0;
99     }
100
101     /**
102      * Returns the internal values in a string form
103      */

104     public String JavaDoc toString() {
105         return ("Added " + getEntryAddedCount() + ", Approximate size " + this.totalSize + ", Cache Flushed " + getCacheFlushedCount());
106     }
107 }
Popular Tags