KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source
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.jboss.cache.CacheImpl;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.Modification;
12 import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig;
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  * Dummy cache loader that captures the number of times each method is called.
23  *
24  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
25  */

26 public class DummyCacheLoader extends AbstractCacheLoader
27 {
28    private int getChildrenNamesCount = 0, getCount = 0, putCount = 0, existsCount = 0, removeCount = 0;
29    private Map JavaDoc m_transactions = new HashMap JavaDoc();
30
31    public int getGetChildrenNamesCount()
32    {
33       return getChildrenNamesCount;
34    }
35
36    public int getGetCount()
37    {
38       return getCount;
39    }
40
41    public int getPutCount()
42    {
43       return putCount;
44    }
45
46    public int getExistsCount()
47    {
48       return existsCount;
49    }
50
51    public int getRemoveCount()
52    {
53       return removeCount;
54    }
55
56
57    /**
58     * Sets the configuration. Will be called before {@link #create()} and {@link #start()}
59     *
60     * @param url A list of properties, defined in the XML file
61     */

62    public void setConfig(IndividualCacheLoaderConfig config)
63    {
64    }
65
66    public IndividualCacheLoaderConfig getConfig()
67    {
68       return null;
69    }
70
71    /**
72     * This method allows the CacheLoader to set the CacheImpl, therefore allowing the CacheLoader to invoke
73     * methods of the CacheImpl. It can also use the CacheImpl to fetch configuration information. Alternatively,
74     * the CacheLoader could maintain its own configuration<br/>
75     * This method will be called directly after the CacheLoader instance has been created
76     *
77     * @param c The cache on which this loader works
78     */

79    public void setCache(CacheImpl c)
80    {
81    }
82
83    /**
84     * Returns a list of children names, all names are <em>relative</em>. Returns null if the parent node is not found.
85     * The returned set must not be modified, e.g. use Collections.unmodifiableSet(s) to return the result
86     *
87     * @param fqn The FQN of the parent
88     * @return Set<String>. A list of children. Returns null if no children nodes are present, or the parent is
89     * not present
90     */

91    public Set JavaDoc getChildrenNames(Fqn fqn) throws Exception JavaDoc
92    {
93       getChildrenNamesCount++;
94       return null;
95    }
96
97    /**
98     * Returns the value for a given key. Returns null if the node doesn't exist, or the value is not bound
99     *
100     * @param name
101     * @return
102     * @throws Exception
103     */

104    public Object JavaDoc get(Fqn name, Object JavaDoc key) throws Exception JavaDoc
105    {
106       getCount++;
107       return null;
108    }
109
110    /**
111     * Returns all keys and values from the persistent store, given a fully qualified name
112     *
113     * @param name
114     * @return Map<Object,Object> of keys and values for the given node. Returns null if the node was not found, or
115     * if the node has no attributes
116     * @throws Exception
117     */

118    public Map JavaDoc get(Fqn name) throws Exception JavaDoc
119    {
120       getCount++;
121       return null;
122    }
123
124    /**
125     * Checks whether the CacheLoader has a node with Fqn
126     *
127     * @param name
128     * @return True if node exists, false otherwise
129     */

130    public boolean exists(Fqn name) throws Exception JavaDoc
131    {
132       existsCount++;
133       return false;
134    }
135
136    /**
137     * Inserts key and value into the attributes hashmap of the given node. If the node does not exist, all
138     * parent nodes from the root down are created automatically. Returns the old value
139     */

140    public Object JavaDoc put(Fqn name, Object JavaDoc key, Object JavaDoc value) throws Exception JavaDoc
141    {
142       putCount++;
143       return null;
144    }
145
146    /**
147     * Inserts all elements of attributes into the attributes hashmap of the given node, overwriting existing
148     * attributes, but not clearing the existing hashmap before insertion (making it a union of existing and
149     * new attributes)
150     * If the node does not exist, all parent nodes from the root down are created automatically
151     *
152     * @param name The fully qualified name of the node
153     * @param attributes A Map of attributes. Can be null
154     */

155    public void put(Fqn name, Map JavaDoc attributes) throws Exception JavaDoc
156    {
157       putCount++;
158    }
159
160    /**
161     * Inserts all modifications to the backend store. Overwrite whatever is already in
162     * the datastore.
163     *
164     * @param modifications A List<Modification> of modifications
165     * @throws Exception
166     */

167    public void put(List JavaDoc<Modification> modifications) throws Exception JavaDoc
168    {
169       putCount++;
170    }
171
172    /**
173     * Removes the given key and value from the attributes of the given node. No-op if node doesn't exist
174     */

175    public Object JavaDoc remove(Fqn name, Object JavaDoc key) throws Exception JavaDoc
176    {
177       removeCount++;
178       return null;
179    }
180
181    /**
182     * Removes the given node. If the node is the root of a subtree, this will recursively remove all subnodes,
183     * depth-first
184     */

185    public void remove(Fqn name) throws Exception JavaDoc
186    {
187       removeCount++;
188    }
189
190    /**
191     * Removes all attributes from a given node, but doesn't delete the node itself
192     *
193     * @param name
194     * @throws Exception
195     */

196    public void removeData(Fqn name) throws Exception JavaDoc
197    {
198       removeCount++;
199    }
200
201    /**
202     * Prepare the modifications. For example, for a DB-based CacheLoader:
203     * <ol>
204     * <li>Create a local (JDBC) transaction
205     * <li>Associate the local transaction with <code>tx</code> (tx is the key)
206     * <li>Execute the coresponding SQL statements against the DB (statements derived from modifications)
207     * </ol>
208     * For non-transactional CacheLoader (e.g. file-based), this could be a null operation
209     *
210     * @param tx The transaction, just used as a hashmap key
211     * @param modifications List<Modification>, a list of all modifications within the given transaction
212     * @param one_phase Persist immediately and (for example) commit the local JDBC transaction as well. When true,
213     * we won't get a {@link #commit(Object)} or {@link #rollback(Object)} method call later
214     * @throws Exception
215     */

216    public void prepare(Object JavaDoc tx, List JavaDoc<Modification> modifications, boolean one_phase) throws Exception JavaDoc
217    {
218       if (one_phase)
219       {
220          put(modifications);
221       }
222       else
223       {
224          m_transactions.put(tx, modifications);
225       }
226    }
227
228    /**
229     * Commit the transaction. A DB-based CacheLoader would look up the local JDBC transaction asociated
230     * with <code>tx</code> and commit that transaction<br/>
231     * Non-transactional CacheLoaders could simply write the data that was previously saved transiently under the
232     * given <code>tx</code> key, to (for example) a file system (note this only holds if the previous prepare() did
233     * not define one_phase=true
234     *
235     * @param tx
236     */

237    public void commit(Object JavaDoc tx) throws Exception JavaDoc
238    {
239       List JavaDoc modifications = (List JavaDoc) m_transactions.get(tx);
240       if (modifications == null)
241       {
242          return;
243       }
244       put(modifications);
245       m_transactions.remove(tx);
246    }
247
248    /**
249     * Roll the transaction back. A DB-based CacheLoader would look up the local JDBC transaction asociated
250     * with <code>tx</code> and roll back that transaction
251     *
252     * @param tx
253     */

254    public void rollback(Object JavaDoc tx)
255    {
256       List JavaDoc modifications = (List JavaDoc) m_transactions.get(tx);
257       if (modifications == null)
258       {
259          return;
260       }
261       m_transactions.remove(tx);
262    }
263
264    public void loadEntireState(ObjectOutputStream JavaDoc os) throws Exception JavaDoc
265    {
266       //intentional no-op
267
}
268
269    public void loadState(Fqn subtree, ObjectOutputStream JavaDoc os) throws Exception JavaDoc
270    {
271       // intentional no-op
272
}
273
274    public void storeEntireState(ObjectInputStream JavaDoc is) throws Exception JavaDoc
275    {
276       // intentional no-op
277
}
278
279    public void storeState(Fqn subtree, ObjectInputStream JavaDoc is) throws Exception JavaDoc
280    {
281       // intentional no-op
282
}
283
284    public void create() throws Exception JavaDoc
285    {
286    }
287
288    public void start() throws Exception JavaDoc
289    {
290    }
291
292    public void stop()
293    {
294    }
295
296    public void destroy()
297    {
298       getChildrenNamesCount = 0;
299       getCount = 0;
300       putCount = 0;
301       existsCount = 0;
302       removeCount = 0;
303    }
304 }
Popular Tags