1 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 ; 16 import java.util.Collection ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.concurrent.ConcurrentHashMap ; 20 21 29 public class TransactionTable { 30 31 33 protected Map tx_map=new ConcurrentHashMap (); 34 35 36 protected Map txs=new ConcurrentHashMap (); 37 38 39 private static final Log log=LogFactory.getLog(TransactionTable.class); 40 41 44 public TransactionTable() { 45 } 46 47 50 public int getNumLocalTransactions() { 51 return tx_map.size(); 52 } 53 54 57 public int getNumGlobalTransactions() { 58 return txs.size(); 59 } 60 61 65 public GlobalTransaction get(Transaction tx) { 66 if (tx == null) return null; 67 return (GlobalTransaction) tx_map.get(tx); 68 } 69 70 80 public Transaction getLocalTransaction(GlobalTransaction gtx) { 81 Map.Entry entry; 82 Transaction local_tx; 83 GlobalTransaction global_tx; 84 85 if(gtx == null) 86 return null; 87 for(Iterator it=tx_map.entrySet().iterator(); it.hasNext();) { 88 entry=(Map.Entry )it.next(); 89 local_tx=(Transaction )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 101 public void put(Transaction 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 113 public TransactionEntry get(GlobalTransaction gtx) { 114 return gtx != null ? (TransactionEntry)txs.get(gtx) : null; 115 } 116 117 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 131 public TransactionEntry remove(GlobalTransaction tx) { 132 return (TransactionEntry)txs.remove(tx); 133 } 134 135 138 public GlobalTransaction remove(Transaction tx) { 139 if(tx == null) 140 return null; 141 return (GlobalTransaction)tx_map.remove(tx); 142 } 143 144 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 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 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 194 public void addLocks(GlobalTransaction gtx, Collection 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 206 public String toString() { 207 StringBuffer sb=new StringBuffer (); 208 sb.append(tx_map.size()).append(" mappings, "); 209 sb.append(txs.size()).append(" transactions"); 210 return sb.toString(); 211 } 212 213 216 public String toString(boolean print_details) { 217 if(!print_details) 218 return toString(); 219 StringBuffer sb=new StringBuffer (); 220 Map.Entry 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 it=tx_map.entrySet().iterator(); it.hasNext();) { 225 entry=(Map.Entry )it.next(); 226 sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n"); 227 } 228 sb.append("txs:\n"); 229 for(Iterator it=txs.entrySet().iterator(); it.hasNext();) { 230 entry=(Map.Entry )it.next(); 231 sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n"); 232 } 233 return sb.toString(); 234 } 235 236 } 237 | Popular Tags |