View Javadoc

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.event;
18  
19  import net.sf.ehcache.CacheException;
20  import net.sf.ehcache.Ehcache;
21  import net.sf.ehcache.Element;
22  
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  
28  /***
29   * A Null Object Pattern implementation of CacheEventListener. It simply logs the calls made.
30   *
31   * @author Greg Luck
32   * @version $Id: NullCacheEventListener.html 13146 2011-08-01 17:12:39Z oletizi $
33   * @since 1.2
34   */
35  public class NullCacheEventListener implements CacheEventListener {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(NullCacheEventListener.class.getName());
38  
39  
40      /***
41       * {@inheritDoc}
42       */
43      public void notifyElementRemoved(final Ehcache cache, final Element element) {
44          if (LOG.isDebugEnabled()) {
45              LOG.debug("notifyElementRemoved called for cache " + cache + " for element with key " + element.getObjectKey());
46          }
47      }
48  
49      /***
50       * {@inheritDoc}
51       */
52      public void notifyElementPut(final Ehcache cache, final Element element) {
53  
54          Object key = null;
55          if (element != null) {
56              key = element.getObjectKey();
57          }
58          if (LOG.isDebugEnabled()) {
59              LOG.debug("notifyElementPut called for cache " + cache.getName() + " for element with key " + key);
60          }
61      }
62  
63      /***
64       * Called immediately after an element has been put into the cache and the element already
65       * existed in the cache. This is thus an update.
66       * <p/>
67       * The {@link net.sf.ehcache.Cache#put(net.sf.ehcache.Element)} method
68       * will block until this method returns.
69       * <p/>
70       * Implementers may wish to have access to the Element's fields, including value, so the element is provided.
71       * Implementers should be careful not to modify the element. The effect of any modifications is undefined.
72       *
73       * @param cache   the cache emitting the notification
74       * @param element the element which was just put into the cache.
75       */
76      public void notifyElementUpdated(final Ehcache cache, final Element element) throws CacheException {
77  
78          Object key = null;
79          if (element != null) {
80              key = element.getObjectKey();
81          }
82          if (LOG.isDebugEnabled()) {
83              LOG.debug("notifyElementUpdated called for cache " + cache.getName() + " for element with key " + key);
84          }
85      }
86  
87      /***
88       * {@inheritDoc}
89       */
90      public void notifyElementExpired(final Ehcache cache, final Element element) {
91          if (LOG.isDebugEnabled()) {
92              LOG.debug("notifyElementExpired called for cache " + cache.getName() + " for element with key " + element.getObjectKey());
93          }
94      }
95  
96      /***
97       * {@inheritDoc}
98       */
99      public void notifyElementEvicted(final Ehcache cache, final Element element) {
100         //
101     }
102 
103     /***
104      * {@inheritDoc}
105      */
106     public void notifyRemoveAll(final Ehcache cache) {
107         //
108     }
109 
110     /***
111      * Give the replicator a chance to cleanup and free resources when no longer needed
112      */
113     public void dispose() {
114         //nothing to do
115     }
116 
117     /***
118      * Creates a clone of this listener. This method will only be called by ehcache before a cache is initialized.
119      * <p/>
120      * This may not be possible for listeners after they have been initialized. Implementations should throw
121      * CloneNotSupportedException if they do not support clone.
122      *
123      * @return a clone
124      * @throws CloneNotSupportedException if the listener could not be cloned.
125      */
126     public Object clone() throws CloneNotSupportedException {
127         return super.clone();
128     }
129 }