KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > TransactionTable


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;
8
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.jboss.cache.lock.NodeLock;
13 import org.jboss.cache.marshall.MethodCall;
14
15 import javax.transaction.Transaction JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.concurrent.ConcurrentHashMap JavaDoc;
20
21 /**
22  * Maintains the mapping between local (Transaction) and global transactions
23  * (GlobalTransaction). Also keys modifications and undo-operations) under a
24  * given TX.
25  *
26  * @author <a HREF="mailto:bela@jboss.org">Bela Ban</a> Apr 14, 2003
27  * @version $Revision: 1.11 $
28  */

29 public class TransactionTable {
30
31     /** Map<Transaction,GlobalTransaction>, mapping between local (javax.transaction.Transaction)
32     * and GlobalTransactions. New: a local TX can have a number of GTXs */

33    protected Map JavaDoc tx_map=new ConcurrentHashMap JavaDoc();
34
35    /** Map<GlobalTransaction,TransactionEntry>, mappings between GlobalTransactions and modifications */
36    protected Map JavaDoc txs=new ConcurrentHashMap JavaDoc();
37
38    /** our logger */
39    private static final Log log=LogFactory.getLog(TransactionTable.class);
40
41    /**
42     * Constructs a new table.
43     */

44    public TransactionTable() {
45    }
46
47    /**
48     * Returns the number of local transactions.
49     */

50    public int getNumLocalTransactions() {
51       return tx_map.size();
52    }
53
54    /**
55     * Returns the number of global transactions.
56     */

57    public int getNumGlobalTransactions() {
58       return txs.size();
59    }
60
61    /**
62     * Returns the global transaction associated with the local transaction.
63     * Returns null if tx is null or it was not found.
64     */

65    public GlobalTransaction get(Transaction JavaDoc tx) {
66       if (tx == null) return null;
67       return (GlobalTransaction) tx_map.get(tx);
68    }
69
70    /**
71     * Returns the local transaction associated with a GlobalTransaction. Not
72     * very efficient as the values have to be iterated over, don't use
73     * frequently
74     *
75     * @param gtx The GlobalTransaction
76     * @return Transaction. The local transaction associated with a given
77     * GlobalTransaction). This will be null if no local transaction is
78     * associated with a given GTX
79     */

80    public Transaction JavaDoc getLocalTransaction(GlobalTransaction gtx) {
81       Map.Entry JavaDoc entry;
82       Transaction JavaDoc local_tx;
83       GlobalTransaction global_tx;
84
85       if(gtx == null)
86          return null;
87       for(Iterator JavaDoc it=tx_map.entrySet().iterator(); it.hasNext();) {
88          entry=(Map.Entry JavaDoc)it.next();
89          local_tx=(Transaction JavaDoc)entry.getKey();
90          global_tx=(GlobalTransaction)entry.getValue();
91          if(gtx.equals(global_tx)) {
92             return local_tx;
93          }
94       }
95       return null;
96    }
97
98    /**
99     * Associates the global transaction with the local transaction.
100     */

101    public void put(Transaction JavaDoc tx, GlobalTransaction gtx) {
102       if(tx == null) {
103          log.error("key (Transaction) is null");
104          return;
105       }
106       tx_map.put(tx, gtx);
107    }
108
109    /**
110     * Returns the local transaction entry for the global transaction.
111     * Returns null if tx is null or it was not found.
112     */

113    public TransactionEntry get(GlobalTransaction gtx) {
114       return gtx != null ? (TransactionEntry)txs.get(gtx) : null;
115    }
116
117    /**
118     * Associates the global transaction with a transaction entry.
119     */

120    public void put(GlobalTransaction tx, TransactionEntry entry) {
121       if(tx == null) {
122          log.error("key (GlobalTransaction) is null");
123          return;
124       }
125       txs.put(tx, entry);
126    }
127
128    /**
129     * Removes a global transation, returns the old transaction entry.
130     */

131    public TransactionEntry remove(GlobalTransaction tx) {
132       return (TransactionEntry)txs.remove(tx);
133    }
134
135    /**
136     * Removes a local transation, returns the global transaction entry.
137     */

138    public GlobalTransaction remove(Transaction JavaDoc tx) {
139       if(tx == null)
140          return null;
141       return (GlobalTransaction)tx_map.remove(tx);
142    }
143
144    /**
145     * Adds a motification to the global transaction.
146     */

147    public void addModification(GlobalTransaction gtx, MethodCall m) {
148       TransactionEntry entry=get(gtx);
149       if(entry == null) {
150          log.error("transaction not found (gtx=" + gtx + ")");
151          return;
152       }
153       entry.addModification(m);
154    }
155
156     public void addCacheLoaderModification(GlobalTransaction gtx, MethodCall m)
157     {
158         TransactionEntry entry = get(gtx);
159         if(entry == null) {
160          log.error("transaction not found (gtx=" + gtx + ")");
161          return;
162       }
163       entry.addCacheLoaderModification(m);
164     }
165
166
167    /**
168     * Adds an undo operation to the global transaction.
169     */

170    public void addUndoOperation(GlobalTransaction gtx, MethodCall m) {
171       TransactionEntry entry=get(gtx);
172       if(entry == null) {
173          log.error("transaction not found (gtx=" + gtx + ")");
174          return;
175       }
176       entry.addUndoOperation(m);
177    }
178
179    /**
180     * Adds a lock to the global transaction.
181     */

182    public void addLock(GlobalTransaction gtx, NodeLock l) {
183       TransactionEntry entry=get(gtx);
184       if(entry == null) {
185          log.error("transaction entry not found for (gtx=" + gtx + ")");
186          return;
187       }
188       entry.addLock(l);
189    }
190
191    /**
192     * Adds a collection of locks to the global transaction.
193     */

194    public void addLocks(GlobalTransaction gtx, Collection JavaDoc locks) {
195       TransactionEntry entry=get(gtx);
196       if(entry == null) {
197          log.error("transaction entry not found for (gtx=" + gtx + ")");
198          return;
199       }
200       entry.addLocks(locks);
201    }
202
203    /**
204     * Returns summary debug information.
205     */

206    public String JavaDoc toString() {
207       StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
208       sb.append(tx_map.size()).append(" mappings, ");
209       sb.append(txs.size()).append(" transactions");
210       return sb.toString();
211    }
212
213    /**
214     * Returns detailed debug information.
215     */

216    public String JavaDoc toString(boolean print_details) {
217       if(!print_details)
218          return toString();
219       StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
220       Map.Entry JavaDoc entry;
221       sb.append("LocalTransactions: ").append(tx_map.size()).append("\n");
222       sb.append("GlobalTransactions: ").append(txs.size()).append("\n");
223       sb.append("tx_map:\n");
224       for(Iterator JavaDoc it=tx_map.entrySet().iterator(); it.hasNext();) {
225          entry=(Map.Entry JavaDoc)it.next();
226          sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
227       }
228       sb.append("txs:\n");
229       for(Iterator JavaDoc it=txs.entrySet().iterator(); it.hasNext();) {
230          entry=(Map.Entry JavaDoc)it.next();
231          sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
232       }
233       return sb.toString();
234    }
235
236 }
237
Popular Tags