KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 public class LRUProcessCache implements ProcessCache {
20    private final int defaultCacheSize=100;
21
22    /**
23     * LRU process cache.
24     */

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

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

54    public void add (String JavaDoc procId, WfProcessInternal proc) throws RootException {
55       synchronized(this) {
56          //if (cache.get(proc.key())!=null) System.err.println("Warining - already have proc "+proc+" in cache!!!");
57
cache.put(procId, proc);
58       }
59    }
60
61    /**
62     * Removes process from the process cache.
63     *
64     * @param procId Process id.
65     *
66     * @exception RootException Thrown if an error occurs.
67     */

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

82    public WfProcessInternal get (String JavaDoc procId) throws RootException {
83       WfProcessInternal proc = null;
84       synchronized(this) {
85          proc=(WfProcessInternal)cache.get(procId);
86       }
87       return proc;
88    }
89
90    /**
91     * Sets size of the process cache to value <i>size</i>. The value 0 means
92     * that the cache is disabled. The negative value means that the cache
93     * is unbounded. The positive number defines max number of cache entries.
94     *
95     * @param size New size of the process cache.
96     * @exception RootException Thrown if an error occurs.
97     */

98    public void setSize (int size) throws RootException {
99       if (size<0) throw new RootException("Can't set negative process cache size");
100       synchronized(this) {
101          cache.setMaximumSize(size);
102       }
103    }
104
105    /**
106     * Returns the size of the process cache.
107     *
108     * @return Size of the process cache.
109     * @exception RootException Thrown if an error occurs.
110     */

111    public int getSize () throws RootException {
112       return cache.getMaximumSize();
113    }
114
115    public int howManyEntries() throws RootException {
116       return cache.size();
117    }
118
119    /**
120     * Returns all processes from the process cache.
121     *
122     * @return All processes from the cache as List.
123     * @exception RootException Thrown if an error occurs.
124     */

125    public java.util.List JavaDoc getAll() throws RootException {
126       if (cache.size()>0) {
127          synchronized(this) {
128             return new ArrayList(cache.values());
129          }
130       } else {
131          return new ArrayList();
132       }
133    }
134
135 }
136
137
Popular Tags