View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.kns.util.cache;
17  
18  import java.util.HashSet;
19  import java.util.Set;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import com.opensymphony.oscache.base.events.CacheEntryEvent;
25  import com.opensymphony.oscache.base.events.CacheEntryEventListener;
26  import com.opensymphony.oscache.base.events.CacheGroupEvent;
27  import com.opensymphony.oscache.base.events.CachePatternEvent;
28  import com.opensymphony.oscache.base.events.CachewideEvent;
29  
30  
31  /**
32   * Monitor entry-related events for a cache.
33   */
34  @SuppressWarnings("unchecked")
35  abstract public class OSCacheMonitor implements CacheEntryEventListener {
36      private static final Log LOG = LogFactory.getLog(OSCacheMonitor.class);
37  
38      private final String purpose;
39      private final Set entries;
40  
41      /**
42       * Constructs a OSCacheMonitor with the given purpose
43       * 
44       * @param purpose
45       */
46  	public OSCacheMonitor(String purpose) {
47          this.purpose = purpose;
48          entries = new HashSet();
49  
50          if ( LOG.isInfoEnabled() ) {
51          	LOG.info("created " + purpose + " CacheMonitor ");
52          }
53      }
54  
55  
56      public void cacheEntryAdded(CacheEntryEvent event) {
57          entries.add(event.getKey());
58          if ( LOG.isDebugEnabled() ) {
59          	logEntryEvent("added", event);
60          }
61      }
62  
63      public void cacheEntryUpdated(CacheEntryEvent event) {
64      	if ( LOG.isDebugEnabled() ) {
65      		logEntryEvent("updated", event);
66      	}
67      }
68  
69      public void cacheEntryFlushed(CacheEntryEvent event) {
70          entries.remove(event.getKey());
71          if ( LOG.isDebugEnabled() ) {
72          	logEntryEvent("flushed", event);
73          }
74      }
75  
76      public void cacheEntryRemoved(CacheEntryEvent event) {
77          entries.remove(event.getKey());
78          if ( LOG.isDebugEnabled() ) {
79          	logEntryEvent("removed", event);
80          }
81      }
82  
83      public void cacheFlushed(CachewideEvent event) {
84          entries.clear();
85          if ( LOG.isDebugEnabled() ) {
86          	LOG.debug(purpose + " flushed cache (" + entries.size() + " entries)");
87          }
88      }
89  
90  
91      public void cacheGroupFlushed(CacheGroupEvent event) {
92          // if I read the code correctly, each entry gets its own cacheEntryFlused event
93      	if ( LOG.isInfoEnabled() ) {
94      		LOG.info(purpose + " flushed cache group '" + event.getGroup() + "'");
95      	}
96      }
97  
98      public void cachePatternFlushed(CachePatternEvent event) {
99          // if I read the code correctly, each entry gets its own cacheEntryFlused event
100     	if ( LOG.isInfoEnabled() ) {
101     		LOG.info(purpose + " flushed cache pattern '" + event.getPattern() + "'");
102     	}
103     }
104 
105     private void logEntryEvent(String verb, CacheEntryEvent event) {
106         LOG.debug(purpose + " " + verb + " entry '" + event.getKey() + "' (" + entries.size() + " entries)");
107     }
108 }