1 22 package org.jboss.tm; 23 24 import javax.naming.InitialContext ; 25 import javax.naming.NamingException ; 26 import javax.transaction.SystemException ; 27 import javax.transaction.Transaction ; 28 import javax.transaction.TransactionManager ; 29 30 39 public class TransactionLocal 40 { 41 42 46 private static final Object NULL_VALUE = new Object (); 47 48 52 protected final TransactionManager transactionManager; 53 54 57 protected TransactionLocalDelegate delegate; 58 59 63 public TransactionLocal() 64 { 65 try 66 { 67 InitialContext context = new InitialContext (); 68 transactionManager = (TransactionManager ) context.lookup("java:/TransactionManager"); 69 } 70 catch(NamingException e) 71 { 72 throw new IllegalStateException ( 73 "An error occured while looking up the transaction manager: " + e 74 ); 75 } 76 initDelegate(); 77 } 78 79 84 public TransactionLocal(TransactionManager tm) 85 { 86 if (tm == null) 87 throw new IllegalArgumentException ("Null transaction manager"); 88 this.transactionManager = tm; 89 initDelegate(); 90 } 91 92 100 public void lock() throws InterruptedException 101 { 102 lock(getTransaction()); 103 } 104 105 114 public void lock(Transaction transaction) throws InterruptedException 115 { 116 if (transaction == null) 118 return; 119 120 delegate.lock(this, transaction); 121 } 122 123 126 public void unlock() 127 { 128 unlock(getTransaction()); 129 } 130 131 136 public void unlock(Transaction transaction) 137 { 138 if (transaction == null) 140 return; 141 142 delegate.unlock(this, transaction); 143 } 144 145 157 protected Object initialValue() 158 { 159 return null; 160 } 161 162 163 166 protected Object getValue(Transaction tx) 167 { 168 return delegate.getValue(this, tx); 169 } 170 171 174 protected void storeValue(Transaction tx, Object value) 175 { 176 delegate.storeValue(this, tx, value); 177 } 178 179 182 protected boolean containsValue(Transaction tx) 183 { 184 return delegate.containsValue(this, tx); 185 } 186 187 194 public Object get() 195 { 196 return get(getTransaction()); 197 } 198 199 200 211 public Object get(Transaction transaction) 212 { 213 if (transaction == null) return initialValue(); 214 215 Object value = getValue(transaction); 216 217 if(value == null) 219 { 220 value = initialValue(); 222 223 if(value == null) 225 { 226 value = NULL_VALUE; 227 } 228 229 storeValue(transaction, value); 231 } 232 233 if(value == NULL_VALUE) 235 { 236 return null; 237 } 238 239 return value; 241 } 242 243 252 public void set(Object value) 253 { 254 set(getTransaction(), value); 255 } 256 257 267 public void set(Transaction transaction, Object value) 268 { 269 if (transaction == null) throw new IllegalStateException ("there is no transaction"); 270 if(!containsValue(transaction)) 274 { 275 initialValue(); 276 } 277 278 if(value == null) 280 { 281 value = NULL_VALUE; 282 } 283 284 storeValue(transaction, value); 286 } 287 288 public Transaction getTransaction() 289 { 290 try 291 { 292 return transactionManager.getTransaction(); 293 } 294 catch(SystemException e) 295 { 296 throw new IllegalStateException ("An error occured while getting the " + 297 "transaction associated with the current thread: " + e); 298 } 299 } 300 301 304 protected void initDelegate() 305 { 306 if (transactionManager instanceof TransactionLocalDelegate) 307 delegate = (TransactionLocalDelegate) transactionManager; 308 else 309 delegate = new TransactionLocalDelegateImpl(transactionManager); 310 } 311 } 312 | Popular Tags |