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.Status;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  
26  /***
27   * A counting cache manager listener.
28   *
29   * @author Greg Luck
30   * @version $Id: CountingCacheManagerEventListener.html 13146 2011-08-01 17:12:39Z oletizi $
31   */
32  public class CountingCacheManagerEventListener implements CacheManagerEventListener {
33  
34  
35      private static List cacheNamesAdded = new ArrayList();
36      private static List cacheNamesRemoved = new ArrayList();
37  
38      /***
39       * Accessor
40       */
41      public static List getCacheNamesAdded() {
42          return cacheNamesAdded;
43      }
44  
45      /***
46       * Resets the counters to 0
47       */
48      public static void resetCounters() {
49          cacheNamesAdded.clear();
50          cacheNamesRemoved.clear();
51      }
52  
53      /***
54       * Accessor
55       */
56      public static List getCacheNamesRemoved() {
57          return cacheNamesRemoved;
58      }
59  
60      /***
61       * Call to start the listeners and do any other required initialisation.
62       */
63      public void init() throws CacheException {
64          //noop
65      }
66  
67      /***
68       * Returns the listener status.
69       *
70       * @return the status at the point in time the method is called
71       */
72      public Status getStatus() {
73          return Status.STATUS_ALIVE;
74      }
75  
76      /***
77       * Stop the listener and free any resources.
78       *
79       * @throws net.sf.ehcache.CacheException - all exceptions are wrapped in CacheException
80       */
81      public void dispose() throws CacheException {
82          //noop
83      }
84  
85      /***
86       * Called immediately after a cache has been added and activated.
87       * <p/>
88       * Note that the CacheManager calls this method from a synchronized method. Any attempt to call a synchronized
89       * method on CacheManager from this method will cause a deadlock.
90       * <p/>
91       * Note that activation will also cause a CacheEventListener status change notification from
92       * {@link net.sf.ehcache.Status#STATUS_UNINITIALISED} to {@link net.sf.ehcache.Status#STATUS_ALIVE}. Care should be
93       * taken on processing that notification because:
94       * <ul>
95       * <li>the cache will not yet be accessible from the CacheManager.
96       * <li>the addCaches methods whih cause this notification are synchronized on the CacheManager. An attempt to call
97       * {@link net.sf.ehcache.CacheManager#getCache(String)} will cause a deadlock.
98       * </ul>
99       * The calling method will block until this method returns.
100      * <p/>
101      *
102      * @param cacheName the name of the <code>Cache</code> the operation relates to
103      * @see net.sf.ehcache.event.CacheEventListener
104      */
105     public void notifyCacheAdded(String cacheName) {
106         cacheNamesAdded.add(cacheName);
107     }
108 
109     /***
110      * Called immediately after a cache has been disposed and removed. The calling method will block until
111      * this method returns.
112      * <p/>
113      * Note that the CacheManager calls this method from a synchronized method. Any attempt to call a synchronized
114      * method on CacheManager from this method will cause a deadlock.
115      * <p/>
116      * Note that a {@link net.sf.ehcache.event.CacheEventListener} status changed will also be triggered. Any attempt from that notification
117      * to access CacheManager will also result in a deadlock.
118      *
119      * @param cacheName the name of the <code>Cache</code> the operation relates to
120      */
121     public void notifyCacheRemoved(String cacheName) {
122         cacheNamesRemoved.add(cacheName);
123     }
124 }