View Javadoc

1   /**
2    *  Copyright 2003-2006 Greg Luck
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 java.util.ArrayList;
20  import java.util.List;
21  
22  
23  /**
24   * A counting cache manager listener.
25   * @author Greg Luck
26   * @version $Id: CountingCacheManagerEventListener.java 28 2006-04-15 05:12:32Z gregluck $
27   */
28  public class CountingCacheManagerEventListener implements CacheManagerEventListener {
29  
30  
31      private static List cacheNamesAdded = new ArrayList();
32      private static List cacheNamesRemoved = new ArrayList();
33  
34      /**
35       * Accessor
36       */
37      public static List getCacheNamesAdded() {
38          return cacheNamesAdded;
39      }
40  
41      /**
42       * Resets the counters to 0
43       */
44      public static void resetCounters() {
45          cacheNamesAdded.clear();
46          cacheNamesRemoved.clear();
47      }
48  
49      /**
50       * Accessor
51       */
52      public static List getCacheNamesRemoved() {
53          return cacheNamesRemoved;
54      }
55  
56      /**
57       * Called immediately after a cache has been added and activated.
58       * <p/>
59       * Note that the CacheManager calls this method from a synchronized method. Any attempt to call a synchronized
60       * method on CacheManager from this method will cause a deadlock.
61       * <p/>
62       * Note that activation will also cause a CacheEventListener status change notification from
63       * {@link net.sf.ehcache.Status#STATUS_UNINITIALISED} to {@link net.sf.ehcache.Status#STATUS_ALIVE}. Care should be
64       * taken on processing that notification because:
65       * <ul>
66       * <li>the cache will not yet be accessible from the CacheManager.
67       * <li>the addCaches methods whih cause this notification are synchronized on the CacheManager. An attempt to call
68       * {@link net.sf.ehcache.CacheManager#getCache(String)} will cause a deadlock.
69       * </ul>
70       * The calling method will block until this method returns.
71       * <p/>
72       *
73       * @param cacheName the name of the <code>Cache</code> the operation relates to
74       * @see net.sf.ehcache.event.CacheEventListener
75       */
76      public void notifyCacheAdded(String cacheName) {
77          cacheNamesAdded.add(cacheName);
78      }
79  
80      /**
81       * Called immediately after a cache has been disposed and removed. The calling method will block until
82       * this method returns.
83       * <p/>
84       * Note that the CacheManager calls this method from a synchronized method. Any attempt to call a synchronized
85       * method on CacheManager from this method will cause a deadlock.
86       * <p/>
87       * Note that a {@link net.sf.ehcache.event.CacheEventListener} status changed will also be triggered. Any attempt from that notification
88       * to access CacheManager will also result in a deadlock.
89       *
90       * @param cacheName the name of the <code>Cache</code> the operation relates to
91       */
92      public void notifyCacheRemoved(String cacheName) {
93          cacheNamesRemoved.add(cacheName);
94      }
95  }