KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > interceptors > OptimisticInterceptor


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.interceptors;
8
9 import org.jboss.cache.CacheException;
10 import org.jboss.cache.CacheSPI;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.GlobalTransaction;
13 import org.jboss.cache.NodeSPI;
14 import org.jboss.cache.OptimisticTransactionEntry;
15 import org.jboss.cache.TransactionTable;
16 import org.jboss.cache.optimistic.TransactionWorkspace;
17
18 import javax.transaction.TransactionManager JavaDoc;
19 import java.util.List JavaDoc;
20
21 /**
22  * Abstract interceptor for optimistic locking
23  *
24  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
25  */

26 public class OptimisticInterceptor extends Interceptor
27 {
28    protected TransactionManager JavaDoc txManager = null;
29    protected TransactionTable txTable = null;
30
31    public void setCache(CacheSPI cache)
32    {
33       super.setCache(cache);
34       txManager = cache.getTransactionManager();
35       txTable = cache.getTransactionTable();
36    }
37
38    protected TransactionWorkspace getTransactionWorkspace(GlobalTransaction gtx) throws CacheException
39    {
40       OptimisticTransactionEntry transactionEntry = (OptimisticTransactionEntry) txTable.get(gtx);
41
42       if (transactionEntry == null)
43       {
44          throw new CacheException("unable to map global transaction " + gtx + " to transaction entry");
45       }
46
47       // try and get the workspace from the transaction
48
return transactionEntry.getTransactionWorkSpace();
49    }
50
51    /**
52     * Adds the Fqn of the node as well as all children and childrens children to the list.
53     *
54     * @param list
55     * @param n
56     */

57    protected void greedyGetFqns(List JavaDoc<Fqn> list, NodeSPI n, Fqn newBase)
58    {
59       list.add(n.getFqn());
60       Fqn newFqn = new Fqn(newBase, n.getFqn().getLastElement());
61       list.add(newFqn);
62
63       for (NodeSPI child : n.getChildrenDirect())
64       {
65          greedyGetFqns(list, child, newFqn);
66       }
67    }
68
69
70 }
71
Popular Tags