KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > loader > DelegatingCacheLoader


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.loader;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.Modification;
13
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 /**
22  * CacheLoader implementation which delegates to another CacheImpl. This allows to stack caches on top of each
23  * other, allowing for hierarchical cache levels. For example, first level cache delegates to a second level cache,
24  * which delegates to a persistent cache.
25  *
26  * @author Bela Ban
27  * @author Daniel Gredler
28  * @version $Id: DelegatingCacheLoader.java,v 1.13 2006/12/30 17:50:01 msurtani Exp $
29  */

30 public abstract class DelegatingCacheLoader extends AbstractCacheLoader
31 {
32    Log log = LogFactory.getLog(getClass());
33    /**
34     * HashMap<Object,List<Modification>>. List of open transactions. Note that this is purely transient, as
35     * we don't use a log, recovery is not available
36     */

37    HashMap JavaDoc<Object JavaDoc, List JavaDoc<Modification>> transactions = new HashMap JavaDoc<Object JavaDoc, List JavaDoc<Modification>>();
38
39    public static final int delegateGetChildrenNames = 1;
40    public static final int delegateGetKey = 2;
41    public static final int delegateGet = 3;
42    public static final int delegateExists = 4;
43    public static final int delegatePutKeyVal = 5;
44    public static final int delegatePut = 6;
45    public static final int delegateRemoveKey = 7;
46    public static final int delegateRemove = 8;
47    public static final int delegateRemoveData = 9;
48    public static final int delegateLoadEntireState = 10;
49    public static final int delegateStoreEntireState = 11;
50    public static final int putList = 12;
51
52    public Set JavaDoc<String JavaDoc> getChildrenNames(Fqn fqn) throws Exception JavaDoc
53    {
54       Set JavaDoc<String JavaDoc> retval = delegateGetChildrenNames(fqn);
55       return retval == null ? null : (retval.size() == 0 ? null : retval);
56    }
57
58    // See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
59

60 // public Object get(Fqn name, Object key) throws Exception {
61
// return delegateGet(name, key);
62
// }
63

64    public Map JavaDoc get(Fqn name) throws Exception JavaDoc
65    {
66       // See http://jira.jboss.com/jira/browse/JBCACHE-118
67
return delegateGet(name);
68    }
69
70    public boolean exists(Fqn name) throws Exception JavaDoc
71    {
72       return delegateExists(name);
73    }
74
75    public Object JavaDoc put(Fqn name, Object JavaDoc key, Object JavaDoc value) throws Exception JavaDoc
76    {
77       return delegatePut(name, key, value);
78    }
79
80    public void put(Fqn name, Map JavaDoc attributes) throws Exception JavaDoc
81    {
82       delegatePut(name, attributes);
83    }
84
85
86    public void put(Fqn fqn, Map JavaDoc attributes, boolean erase) throws Exception JavaDoc
87    {
88       if (erase)
89       {
90          removeData(fqn);
91       }
92       put(fqn, attributes);
93    }
94
95    public Object JavaDoc remove(Fqn name, Object JavaDoc key) throws Exception JavaDoc
96    {
97       return delegateRemove(name, key);
98    }
99
100    public void remove(Fqn name) throws Exception JavaDoc
101    {
102       delegateRemove(name);
103    }
104
105    public void removeData(Fqn name) throws Exception JavaDoc
106    {
107       delegateRemoveData(name);
108    }
109
110    public void prepare(Object JavaDoc tx, List JavaDoc<Modification> modifications, boolean one_phase) throws Exception JavaDoc
111    {
112       if (one_phase)
113       {
114          put(modifications);
115       }
116       else
117       {
118          transactions.put(tx, modifications);
119       }
120    }
121
122    public void commit(Object JavaDoc tx) throws Exception JavaDoc
123    {
124       List JavaDoc modifications = transactions.get(tx);
125       if (modifications == null)
126       {
127          throw new Exception JavaDoc("transaction " + tx + " not found in transaction table");
128       }
129       put(modifications);
130    }
131
132    public void rollback(Object JavaDoc tx)
133    {
134       transactions.remove(tx);
135    }
136
137    public void loadEntireState(ObjectOutputStream JavaDoc os) throws Exception JavaDoc
138    {
139       delegateLoadEntireState(os);
140
141    }
142
143    public void loadState(Fqn subtree, ObjectOutputStream JavaDoc os) throws Exception JavaDoc
144    {
145       delegateLoadState(subtree, os);
146    }
147
148    public void storeEntireState(ObjectInputStream JavaDoc is) throws Exception JavaDoc
149    {
150       delegateStoreEntireState(is);
151    }
152
153    public void storeState(Fqn subtree, ObjectInputStream JavaDoc is) throws Exception JavaDoc
154    {
155       delegateStoreState(subtree, is);
156    }
157
158    public void create() throws Exception JavaDoc
159    {
160       // Empty.
161
}
162
163    public void start() throws Exception JavaDoc
164    {
165       // Empty.
166
}
167
168    public void stop()
169    {
170       // Empty.
171
}
172
173    public void destroy()
174    {
175       // Empty.
176
}
177
178    /*------------------------------ Delegating Methods ------------------------------*/
179
180
181    protected abstract Set JavaDoc delegateGetChildrenNames(Fqn fqn) throws Exception JavaDoc;
182
183    // See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
184

185 // protected abstract Object delegateGet(Fqn name, Object key) throws Exception;
186

187    protected abstract Map JavaDoc delegateGet(Fqn name) throws Exception JavaDoc;
188
189    protected abstract boolean delegateExists(Fqn name) throws Exception JavaDoc;
190
191    protected abstract Object JavaDoc delegatePut(Fqn name, Object JavaDoc key, Object JavaDoc value) throws Exception JavaDoc;
192
193    protected abstract void delegatePut(Fqn name, Map JavaDoc attributes) throws Exception JavaDoc;
194
195    protected abstract Object JavaDoc delegateRemove(Fqn name, Object JavaDoc key) throws Exception JavaDoc;
196
197    protected abstract void delegateRemove(Fqn name) throws Exception JavaDoc;
198
199    protected abstract void delegateRemoveData(Fqn name) throws Exception JavaDoc;
200
201    protected abstract void delegateLoadEntireState(ObjectOutputStream JavaDoc os) throws Exception JavaDoc;
202
203    protected abstract void delegateLoadState(Fqn subtree, ObjectOutputStream JavaDoc os) throws Exception JavaDoc;
204
205    protected abstract void delegateStoreEntireState(ObjectInputStream JavaDoc is) throws Exception JavaDoc;
206
207    protected abstract void delegateStoreState(Fqn subtree, ObjectInputStream JavaDoc is) throws Exception JavaDoc;
208 }
209
Popular Tags