KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > cache > replacement > lib > AbstractReplacementManager


1 /**
2  * Copyright (C) 2001-2002
3  * - France Telecom R&D
4  * - Laboratoire Logiciels, Systemes, Reseaux - UMR 5526, CNRS-INPG-UJF
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Release: 1.0
21  *
22  * Authors:
23  *
24  */

25
26 package org.objectweb.perseus.cache.replacement.lib;
27
28 import org.objectweb.fractal.api.control.BindingController;
29 import org.objectweb.perseus.cache.api.CacheException;
30 import org.objectweb.perseus.cache.api.FixableCacheEntry;
31 import org.objectweb.perseus.cache.api.UnbindManager;
32 import org.objectweb.perseus.cache.api.CacheCapacityEventListener;
33 import org.objectweb.perseus.cache.api.CacheCapacityEvent;
34 import org.objectweb.perseus.cache.replacement.api.ReplaceableCacheEntry;
35 import org.objectweb.perseus.cache.replacement.api.ReplacementManager;
36 import org.objectweb.util.monolog.api.BasicLevel;
37 import org.objectweb.util.monolog.api.Logger;
38
39 import java.lang.ref.ReferenceQueue JavaDoc;
40 import java.lang.ref.WeakReference JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Collection JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.SortedSet JavaDoc;
46 import java.util.TreeSet JavaDoc;
47 import java.util.Collections JavaDoc;
48
49 /**
50  * This class is a common implementation of a ReplacementManager.
51  *
52  * @author E.Bruneton
53  * @see FIFOReplacementManager, LRUReplacementManager, MRUReplacementManager
54  */

55 public abstract class AbstractReplacementManager
56         implements ReplacementManager, BindingController, CacheCapacityEventListener {
57
58     public final static String JavaDoc UNBIND_MANAGER_BINDING = "unbind-manager";
59
60     protected UnbindManager ub;
61
62     /**
63      * Sorted list of WeakReplaceableCacheEntry entries. Each entry must have a
64      * unique age, so that this sorted set behaves as a sorted list.
65      */

66     protected SortedSet JavaDoc entries;
67     protected HashMap JavaDoc oid2wentry;
68
69     /**
70      * Queue used to register the weak references stored in the entries list.
71      */

72     protected ReferenceQueue JavaDoc queue;
73
74     protected Logger logger = null;
75
76     public AbstractReplacementManager() {
77         entries = new TreeSet JavaDoc();
78         oid2wentry = new HashMap JavaDoc();
79         queue = new ReferenceQueue JavaDoc();
80     }
81
82     //IMPLEMENTATION OF THE UserBindingControler INTERFACE //
83
//------------------------------------------------------//
84

85     public String JavaDoc[] listFc() {
86         return new String JavaDoc[] {
87             UNBIND_MANAGER_BINDING,
88         };
89     }
90
91     public Object JavaDoc lookupFc(String JavaDoc s) {
92         if (UNBIND_MANAGER_BINDING.equals(s)) {
93             return ub;
94         }
95         return null;
96     }
97
98     public void bindFc(String JavaDoc s, Object JavaDoc o) {
99         if (UNBIND_MANAGER_BINDING.equals(s)) {
100             ub = (UnbindManager) o;
101         } else if ("logger".equals(s)) {
102             logger = (Logger) o;
103         }
104     }
105
106     public void unbindFc(String JavaDoc s) {
107         if (UNBIND_MANAGER_BINDING.equals(s)) {
108             ub = null;
109         }
110     }
111
112     //IMPLEMENTATION OF THE CacheCapacityEventListener INTERFACE //
113
//---------------------------------------------------//
114

115     public void cacheResized(CacheCapacityEvent event) {
116         int size = event.getSize();
117         if (size > 0) {
118             synchronized(entries) {
119                 if (oid2wentry.size() == 0) {
120                     oid2wentry = new HashMap JavaDoc(size);
121                 }
122             }
123         }
124     }
125
126     //IMPLEMENTATION OF THE ReplacementManager INTERFACE //
127
//---------------------------------------------------//
128

129     public void addForReplacement(FixableCacheEntry entry)
130             throws CacheException {
131         if (logger.isLoggable(BasicLevel.DEBUG))
132             logger.log(BasicLevel.DEBUG,
133                 "add the entry " + entry.getCeIdentifier());
134         ReplaceableCacheEntry dentry = check(entry);
135         synchronized(entries) {
136             clean();
137             WeakReplaceableCacheEntry we = (WeakReplaceableCacheEntry)
138                 oid2wentry.get(entry.getCeIdentifier());
139             add(dentry);
140             if (we != null) {
141                 entries.remove(we);
142                 we.updateEntry((ReplaceableCacheEntry) entry);
143             } else {
144                 we = new WeakReplaceableCacheEntry(dentry, queue);
145                 oid2wentry.put(entry.getCeIdentifier(), we);
146             }
147             entries.add(we);
148         }
149     }
150
151     public void adjustForReplacement(FixableCacheEntry entry)
152             throws CacheException {
153         if (logger.isLoggable(BasicLevel.DEBUG)) {
154             logger.log(BasicLevel.DEBUG,
155                 "touch the entry " + entry.getCeIdentifier());
156         }
157         ReplaceableCacheEntry dentry = check(entry);
158         synchronized(entries) {
159             clean();
160             WeakReplaceableCacheEntry we = (WeakReplaceableCacheEntry)
161                 oid2wentry.get(entry.getCeIdentifier());
162             touch(dentry);
163             if (we != null) {
164                 entries.remove(we);
165                 we.updateEntry((ReplaceableCacheEntry) entry);
166             } else {
167                 we = new WeakReplaceableCacheEntry(dentry, queue);
168                 oid2wentry.put(entry.getCeIdentifier(), we);
169             }
170         }
171     }
172
173     public int forceFree(int capacity) throws CacheException {
174         boolean debug = logger.isLoggable(BasicLevel.DEBUG);
175         if (debug) {
176             logger.log(BasicLevel.DEBUG, "forceFree: requested: " + capacity);
177         }
178         // elects entries which could be evicted
179
ArrayList JavaDoc oids = new ArrayList JavaDoc(capacity);
180         int count = 0;
181         synchronized(entries) {
182             Iterator JavaDoc i = entries.iterator();
183             while (count < capacity && i.hasNext()) {
184                 WeakReplaceableCacheEntry wentry = (WeakReplaceableCacheEntry) i.next();
185                 ReplaceableCacheEntry entry = (ReplaceableCacheEntry) wentry.get();
186                 if (entry != null && entry.getCeFixCount() == 0) {
187                     oids.add(entry.getCeIdentifier());
188                     count++;
189                 }
190             }
191         }
192         if (debug) {
193             logger.log(BasicLevel.DEBUG, "forceFree: try to unbound, oids=" + oids);
194         }
195         //Ask the eviction to the UnbindManager
196
Collection JavaDoc _oids = ub.unbindAll(oids, false);
197         if (debug) {
198             logger.log(BasicLevel.DEBUG, "forceFree: entries unbound, oid=" + _oids);
199         }
200         return _oids.size();
201     }
202
203     public void removeForReplacement(Object JavaDoc oid) {
204         synchronized(entries) {
205             Object JavaDoc we = oid2wentry.remove(oid);
206             if (we != null) {
207                 entries.remove(we);
208             }
209         }
210     }
211
212
213     //IMPLEMENTATION OF THE UnbindManager INTERFACE //
214
//----------------------------------------------//
215
public Collection JavaDoc unbindAll(Collection JavaDoc _oids, boolean force) throws CacheException {
216         boolean debug = logger.isLoggable(BasicLevel.DEBUG);
217         //Calculate the list of oid which match to unfixed entries
218
ArrayList JavaDoc oids = new ArrayList JavaDoc();
219         synchronized(entries) {
220             if (entries.isEmpty()) {
221                 return Collections.EMPTY_SET;
222             }
223             oids = new ArrayList JavaDoc();
224             Iterator JavaDoc it = _oids.iterator();
225             while(it.hasNext()) {
226                 Object JavaDoc oid = it.next();
227                 WeakReplaceableCacheEntry wentry =
228                         (WeakReplaceableCacheEntry) oid2wentry.get(oid);
229                 if (wentry != null) {
230                     oids.add(oid);
231                 }
232             }
233         }
234         if (debug) {
235             logger.log(BasicLevel.DEBUG, "unbindAll(oids, " + force
236                     + ") try to unbound, oids=" + oids);
237         }
238         //Ask the eviction to the UnbindManager
239
_oids = ub.unbindAll(oids, force);
240         if (debug) {
241             logger.log(BasicLevel.DEBUG, "entries unbound, oid=" + _oids);
242         }
243         return _oids;
244     }
245
246     public Collection JavaDoc unbindUnfixed(boolean force) throws CacheException {
247         boolean debug = logger.isLoggable(BasicLevel.DEBUG);
248         //Calculate the list of oid matching to evictable entries
249
ArrayList JavaDoc oids;
250         synchronized(entries) {
251             if (entries.isEmpty()) {
252                 return Collections.EMPTY_SET;
253             }
254             oids = new ArrayList JavaDoc();
255             Iterator JavaDoc it = entries.iterator();
256             while(it.hasNext()) {
257                 WeakReplaceableCacheEntry wentry = (WeakReplaceableCacheEntry) it.next();
258                 ReplaceableCacheEntry ce = (ReplaceableCacheEntry) wentry.get();
259                 if (ce == null || ce.getCeFixCount() == 0) {
260                     oids.add(wentry.getCeIdentifier());
261                 }
262             }
263         }
264         if (debug) {
265             logger.log(BasicLevel.DEBUG, "unbindAll(" + force
266                     + ") try to unbound, oids=" + oids);
267         }
268         //Ask the eviction to the UnbindManager
269
Collection JavaDoc _oids = ub.unbindAll(oids, force);
270         if (debug) {
271             logger.log(BasicLevel.DEBUG, "entries unbound, oid=" + _oids);
272         }
273         return _oids;
274     }
275
276     /**
277      * It forces the replacement manager to unbind a CacheEntry instance from
278      * the cache.
279      */

280     public boolean unbind(Object JavaDoc oid, boolean force) throws CacheException {
281         boolean debug = logger.isLoggable(BasicLevel.DEBUG);
282         if (oid == null) {
283             return false;
284         }
285         if (debug)
286             logger.log(BasicLevel.DEBUG, "unbind, oid=" + oid);
287         WeakReplaceableCacheEntry we;
288         try {
289             //try out of synchronization
290
we = (WeakReplaceableCacheEntry ) oid2wentry.get(oid);
291         } catch (Throwable JavaDoc e) {
292             // if a concurrency error is thrown, then try another with the
293
// synchronization
294
synchronized(entries) {
295                 we = (WeakReplaceableCacheEntry ) oid2wentry.get(oid);
296             }
297         }
298         if (we != null) {
299             return ub.unbind(oid, force);
300         } else {
301             logger.log(BasicLevel.WARN,
302                     "The entry has not been found from the cache");
303             return false;
304         }
305     }
306
307     //---------------------------------------------------//
308

309     private ReplaceableCacheEntry check(FixableCacheEntry entry) {
310         try {
311             return (ReplaceableCacheEntry) entry;
312         } catch (ClassCastException JavaDoc e) {
313             throw new IllegalArgumentException JavaDoc(
314                     "This replacement manager works only with ReplaceableCacheEntry entries");
315         }
316     }
317
318     protected abstract void add(ReplaceableCacheEntry entry);
319
320     protected abstract void touch(ReplaceableCacheEntry entry);
321
322     /**
323      * Removes all entries that have been garbage collected. Does not block.
324      */

325     private void clean() {
326         boolean debug = logger.isLoggable(BasicLevel.DEBUG);
327         while (true) {
328             WeakReplaceableCacheEntry wentry =
329                         (WeakReplaceableCacheEntry) queue.poll();
330             if (wentry != null) {
331                 if (debug) {
332                     logger.log(BasicLevel.DEBUG, "auto cleanup, oid="
333                             + wentry.getCeIdentifier());
334                 }
335                 entries.remove(wentry);
336                 oid2wentry.remove(wentry.getCeIdentifier());
337             } else {
338                 return;
339             }
340         }
341     }
342
343     /**
344      * The entries stored in 'entries' are weak references to the real entries.
345      * Each weak entry also stores the age of the real entry at the time it was
346      * added in 'entries'.
347      */

348     private static class WeakReplaceableCacheEntry
349             extends WeakReference JavaDoc implements Comparable JavaDoc {
350
351         private long date;
352         private Object JavaDoc oid;
353
354         public WeakReplaceableCacheEntry(ReplaceableCacheEntry ce, ReferenceQueue JavaDoc queue) {
355             super(ce, queue);
356             oid = ce.getCeIdentifier();
357             date = ce.getCeAge();
358         }
359
360         public Object JavaDoc getCeIdentifier() {
361             return oid;
362         }
363
364         public int compareTo(Object JavaDoc o) {
365             long odate = ((WeakReplaceableCacheEntry) o).date;
366             if (date == odate) {
367                 return 0;
368             } else {
369                 return date < odate ? -1 : 1;
370             }
371         }
372
373         public boolean equals(Object JavaDoc o) {
374             return date == ((WeakReplaceableCacheEntry) o).date;
375         }
376
377         public void updateEntry(ReplaceableCacheEntry ce) {
378             oid = ce.getCeIdentifier();
379             date = ce.getCeAge();
380         }
381     }
382 }
383
Popular Tags