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