View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation
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  package org.apache.commons.cache;
17  
18  import java.util.ArrayList;
19  import java.io.Serializable;
20  
21  /**
22   * An abstract base {@link Cache} implementation,
23   * managing the registration of listeners and the
24   * broadcast of events.
25   *
26   * @version $Id: BaseCache.java 155435 2005-02-26 13:17:27Z dirkv $
27   * @author Rodney Waldhoff
28   */
29  public abstract class BaseCache implements Cache {
30    public abstract boolean store(Serializable key, Serializable val, Long expiry, Long cost, Serializable group);
31    public abstract Serializable retrieve(Serializable key);
32    public abstract Serializable[] getKeysForGroup(Serializable group);
33    public abstract boolean contains(Serializable key);
34    public abstract void clear(Serializable key);
35    public abstract void clear();
36  
37    public long getStat(CacheStat stat) throws UnsupportedOperationException {
38      throw new UnsupportedOperationException();
39    }
40  
41    public synchronized void clearGroup(Serializable group) {
42      Serializable[] keys = getKeysForGroup(group);
43      if(null != keys) {
44        for(int i=0,m=keys.length;i<m;i++) {
45          try {
46            clear(keys[i]);
47          } catch(Exception e) {
48            e.printStackTrace();
49            /* ignored */
50          }
51        }
52      }
53    }
54  
55    public boolean store(Serializable key, Serializable val, Long expiry, Long cost) {
56      return store(key,val,expiry,cost,null);
57    }
58  
59    /** My list of {@link StorageListener}s. */
60    protected ArrayList _storageListeners = new ArrayList();
61  
62    /** My list of {@link RetrievalListener}s. */
63    protected ArrayList _retrievalListeners = new ArrayList();
64  
65    /**
66     * Add the given {@link StorageListener} to my
67     * set of {@link StorageListener}s.
68     * @link obs the observer to add
69     */
70    public synchronized void registerStorageListener(StorageListener obs) {
71      if(!_storageListeners.contains(obs)) {
72        _storageListeners.add(obs);
73      }
74    }
75  
76    /**
77     * Remove the given {@link StorageListener} from my
78     * set of {@link StorageListener}s.
79     * @link obs the observer to remove
80     */
81    public synchronized void unregisterStorageListener(StorageListener obs) {
82      for(boolean found=true;found;found=_storageListeners.remove(obs));
83    }
84  
85    /**
86     * Clear my set of {@link StorageListener}s.
87     */
88    public synchronized void unregisterStorageListeners() {
89      _storageListeners.clear();
90    }
91  
92    /**
93     * Add the given {@link RetrievalListener} to my
94     * set of {@link RetrievalListener}s.
95     * @link obs the observer to add
96     */
97    public synchronized void registerRetrievalListener(RetrievalListener obs) {
98      if(!_retrievalListeners.contains(obs)) {
99        _retrievalListeners.add(obs);
100     }
101   }
102 
103   /**
104    * Remove the given {@link RetrievalListener} from my
105    * set of {@link RetrievalListener}s.
106    * @link obs the observer to remove
107    */
108   public synchronized void unregisterRetrievalListener(RetrievalListener obs) {
109     for(boolean found=true;found;found=_retrievalListeners.remove(obs));
110   }
111 
112   /**
113    * Clear my set of {@link RetrievalListener}s.
114    */
115   public synchronized void unregisterRetrievalListeners() {
116     _retrievalListeners.clear();
117   }
118 
119   /**
120    * Broadcast a {@link StorageListener#storeRequested(java.io.Serializable,java.io.Serializable,java.lang.Long,java.lang.Long,java.io.Serializable)}
121    * event to my set of {@link StorageListener}s.
122    *
123    * @param key the cache key
124    * @param val the cache value
125    * @param expiresAt the expiration timestamp, or <tt>null</tt>
126    * @param cost the cost of the object, or <tt>null</tt>
127    */
128   protected synchronized void broadcastStoreRequested(Serializable key, Serializable val, Long expiresAt, Long cost, Serializable group) {
129     for(int i=0,m=_storageListeners.size();i<m;i++) {
130       ((StorageListener)(_storageListeners.get(i))).storeRequested(key,val,expiresAt,cost,group);
131     }
132   }
133 
134   /**
135    * Broadcast a {@link StorageListener#stored(java.io.Serializable,java.io.Serializable,java.lang.Long,java.lang.Long,java.io.Serializable)}
136    * event to my set of {@link StorageListener}s.
137    *
138    * @param key the cache key
139    * @param val the cache value
140    * @param expiresAt the expiration timestamp, or <tt>null</tt>
141    * @param cost the cost of the object, or <tt>null</tt>
142    */
143   protected synchronized void broadcastStored(Serializable key, Serializable val, Long expiresAt, Long cost, Serializable group) {
144     for(int i=0,m=_storageListeners.size();i<m;i++) {
145       ((StorageListener)(_storageListeners.get(i))).stored(key,val,expiresAt,cost,group);
146     }
147   }
148 
149   /**
150    * Broadcast a {@link StorageListener#notStored(java.io.Serializable,java.io.Serializable,java.lang.Long,java.lang.Long,java.io.Serializable)}
151    * event to my set of {@link StorageListener}s.
152    *
153    * @param key the cache key
154    * @param val the cache value
155    * @param expiresAt the expiration timestamp, or <tt>null</tt>
156    * @param cost the cost of the object, or <tt>null</tt>
157    */
158   protected synchronized void broadcastNotStored(Serializable key, Serializable val, Long expiresAt, Long cost, Serializable group) {
159     for(int i=0,m=_storageListeners.size();i<m;i++) {
160       ((StorageListener)(_storageListeners.get(i))).notStored(key,val,expiresAt,cost, group);
161     }
162   }
163 
164   /**
165    * Broadcast a {@link StorageListener#cleared(java.io.Serializable)}
166    * event to my set of {@link StorageListener}s.
167    *
168    * @param key the cache key
169    */
170   protected synchronized void broadcastCleared(Serializable key) {
171     for(int i=0,m=_storageListeners.size();i<m;i++) {
172       ((StorageListener)(_storageListeners.get(i))).cleared(key);
173     }
174   }
175 
176   /**
177    * Broadcast a {@link StorageListener#cleared()}
178    * event to my set of {@link StorageListener}s.
179    */
180   protected synchronized void broadcastCleared() {
181     for(int i=0,m=_storageListeners.size();i<m;i++) {
182       ((StorageListener)(_storageListeners.get(i))).cleared();
183     }
184   }
185 
186   /**
187    * Broadcast a {@link RetrievalListener#retrieveRequested(java.io.Serializable)}
188    * event to my set of {@link RetrievalListener}s.
189    *
190    * @param key the cache key
191    */
192   protected synchronized void broadcastRetrieveRequested(Serializable key) {
193     for(int i=0,m=_retrievalListeners.size();i<m;i++) {
194       ((RetrievalListener)(_retrievalListeners.get(i))).retrieveRequested(key);
195     }
196   }
197 
198   /**
199    * Broadcast a {@link RetrievalListener#retrieved(java.io.Serializable)}
200    * event to my set of {@link RetrievalListener}s.
201    *
202    * @param key the cache key
203    */
204   protected synchronized void broadcastRetrieved(Serializable key) {
205 // System.out.println(System.currentTimeMillis() + ": BaseCache.broadcastRetrieved starting");
206     for(int i=0,m=_retrievalListeners.size();i<m;i++) {
207 // System.out.println(System.currentTimeMillis() + ": BaseCache.broadcastRetrieved about to broadcast to " + _retrievalListeners.get(i));
208       ((RetrievalListener)(_retrievalListeners.get(i))).retrieved(key);
209 // System.out.println(System.currentTimeMillis() + ": BaseCache.broadcastRetrieved just broadcasted to " + _retrievalListeners.get(i));
210     }
211 // System.out.println(System.currentTimeMillis() + ": BaseCache.broadcastRetrieved ending");
212   }
213 
214   /**
215    * Broadcast a {@link RetrievalListener#notRetrieved(java.io.Serializable)}
216    * event to my set of {@link RetrievalListener}s.
217    *
218    * @param key the cache key
219    */
220   protected synchronized void broadcastNotRetrieved(Serializable key) {
221     for(int i=0,m=_retrievalListeners.size();i<m;i++) {
222       ((RetrievalListener)(_retrievalListeners.get(i))).notRetrieved(key);
223     }
224   }
225 }