KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > caching > LRUResourceCache


1 /* LRUResourceCache.java */
2
3 package org.enhydra.shark.caching;
4
5 import java.util.*;
6
7 import org.enhydra.shark.api.internal.caching.ResourceCache;
8 import org.enhydra.shark.api.internal.working.WfResourceInternal;
9 import org.enhydra.shark.api.RootException;
10 import org.enhydra.shark.utilities.LRUMap;
11
12 import org.enhydra.shark.api.internal.working.CallbackUtilities;
13 /**
14  * This class is LRU (least recently used mechanism) cache for storing resources.
15  * @author Vladimir Puskas
16  * @author Tanja Jovanovic
17  */

18 public class LRUResourceCache implements ResourceCache {
19
20    private final int defaultCacheSize=100;
21
22    /**
23     * LRU resource cache.
24     */

25    protected LRUMap cache;
26
27    /**
28     * Configures resource cache.
29     *
30     * @param cus an instance of CallbackUtilities used to get
31     * properties for configuring resource cache.
32     * @exception RootException Thrown if an error occurs.
33     */

34    public void configure (CallbackUtilities cus) throws RootException {
35       cus.getProperties();
36       String JavaDoc resCacheSize=cus.getProperty("LRUResourceCache.Size");
37       try {
38          int cacheSize=Integer.parseInt(resCacheSize.trim());
39          cache = new LRUMap(cacheSize);
40       } catch (Exception JavaDoc ex){
41          cache = new LRUMap(defaultCacheSize);
42       }
43       cus.info("Resource Cache configured - max. size is "+cache.getMaximumSize());
44    }
45
46    /**
47     * Adds resource to the resource cache.
48     *
49     * @param username Username of the resource.
50     * @param res WfResourceInternal object to be added to the resource cache.
51     *
52     * @exception RootException Thrown if an error occurs.
53     */

54    public void add(String JavaDoc username, WfResourceInternal res) throws RootException {
55      synchronized(this) {
56         //if (cache.get(res.resource_key())!=null) System.err.println("Warning - already have res "+res+" in cache!!!");
57
cache.put(username, res);
58      }
59    }
60
61    /**
62     * Removes resource from the resource cache.
63     *
64     * @param username Username of the resource.
65     * @exception RootException Thrown if an error occurs.
66     */

67    public void remove(String JavaDoc username) throws RootException {
68      synchronized(this) {
69         cache.remove(username);
70      }
71    }
72
73    /**
74     * Returns the resource from the resource cache with username <i>username</i>.
75     *
76     * @param username Username of the resource.
77     * @return Resource from the cache with the username <i>username</i> if exists,
78     * otherwise null.
79     * @exception RootException Thrown if an error occurs.
80     */

81    public synchronized WfResourceInternal get(String JavaDoc username) throws RootException {
82      WfResourceInternal res = null;
83      synchronized(this) {
84         res =(WfResourceInternal)cache.get(username);
85      }
86      return res;
87    }
88
89    /**
90     * Returns all resources from the resource cache.
91     *
92     * @return All resources from the cache as List.
93     * @exception RootException Thrown if an error occurs.
94     */

95    public java.util.List JavaDoc getAll () throws RootException {
96       if (cache.size()> 0) {
97          synchronized(this) {
98             return new ArrayList(cache.values());
99          }
100       } else {
101          return new ArrayList();
102       }
103    }
104
105    /**
106     * Sets size of the resource cache to value <i>size</i>. The value 0 means
107     * that the cache is disabled. The negative value means that the cache
108     * is unbounded. The positive number defines max number of cache entries.
109     *
110     * @param size New size of the resource cache.
111     * @exception RootException Thrown if an error occurs.
112     */

113    public void setSize (int size) throws RootException {
114       if (size<0) throw new RootException("Can't set negative resource cache size");
115       synchronized(this) {
116          cache.setMaximumSize(size);
117       }
118    }
119
120    /**
121     * Returns the size of the resource cache.
122     *
123     * @return Size of the resource cache.
124     * @exception RootException Thrown if an error occurs.
125     */

126    public int getSize () throws RootException {
127       return cache.getMaximumSize();
128    }
129
130    public int howManyEntries() throws RootException {
131       return cache.size();
132    }
133
134 }
135 /* End of LRUResourceCache.java */
136
137
Popular Tags