1 22 package org.jboss.aspects.tx; 23 24 import javax.transaction.Transaction ; 25 import javax.transaction.TransactionManager ; 26 import org.jboss.aop.advice.Interceptor; 27 import org.jboss.aop.joinpoint.Invocation; 28 import org.jboss.logging.Logger; 29 30 37 public class TxInterceptor 38 { 39 private static final Logger log = Logger.getLogger(TxInterceptor.class); 40 private static final TxTimeoutReader txTimeoutReader = TxTimeoutReaderFactory.getTxTimeoutReader(); 41 42 public static class Never implements Interceptor 43 { 44 protected TransactionManager tm; 45 protected TxPolicy policy; 46 protected int timeout; 47 48 public Never(TransactionManager tm, TxPolicy policy) 49 { 50 this(tm, policy, -1); 51 } 52 53 public Never(TransactionManager tm, TxPolicy policy, int timeout) 54 { 55 this.tm = tm; 56 this.policy = policy; 57 this.timeout = timeout; 58 } 59 60 public Object invoke(Invocation invocation) throws Throwable 61 { 62 if (tm.getTransaction() != null) 63 { 64 throw new IllegalStateException ("Transaction present on server in Never call"); 65 } return policy.invokeInNoTx(invocation); 67 } 68 69 public String getName() 70 { 71 return this.getClass().getName(); 72 } 73 } 74 75 public static class NotSupported implements Interceptor 76 { 77 protected TransactionManager tm; 78 protected TxPolicy policy; 79 protected int timeout; 80 81 public NotSupported(TransactionManager tm, TxPolicy policy) 82 { 83 this(tm, policy, -1); 84 } 85 86 public NotSupported(TransactionManager tm, TxPolicy policy, int timeout) 87 { 88 this.tm = tm; 89 this.policy = policy; 90 this.timeout = timeout; 91 } 92 93 public Object invoke(Invocation invocation) 94 throws Throwable 95 { 96 Transaction tx = tm.getTransaction(); 97 if (tx != null) 98 { 99 tm.suspend(); 100 try 101 { 102 return policy.invokeInNoTx(invocation); 103 } 104 finally 105 { 106 tm.resume(tx); 107 } 108 109 } 110 else 111 { 112 return policy.invokeInNoTx(invocation); 113 } 114 } 115 116 public String getName() 117 { 118 return this.getClass().getName(); 119 } 120 } 121 122 public static class Supports implements Interceptor 123 { 124 protected TransactionManager tm; 125 protected TxPolicy policy; 126 protected int timeout; 127 128 public Supports(TransactionManager tm, TxPolicy policy) 129 { 130 this(tm, policy, -1); 131 } 132 133 public Supports(TransactionManager tm, TxPolicy policy, int timeout) 134 { 135 this.tm = tm; 136 this.policy = policy; 137 this.timeout = timeout; 138 } 139 140 public Object invoke(Invocation invocation) throws Throwable 141 { 142 if (tm.getTransaction() == null) 143 { 144 return policy.invokeInNoTx(invocation); 145 } 146 else 147 { 148 return policy.invokeInCallerTx(invocation, tm.getTransaction()); 149 } 150 } 151 152 public String getName() 153 { 154 return this.getClass().getName(); 155 } 156 } 157 158 public static class Required implements Interceptor 159 { 160 private static final Logger log = Logger.getLogger(Required.class); 161 162 protected TransactionManager tm; 163 protected TxPolicy policy; 164 protected int timeout; 165 166 public Required(TransactionManager tm, TxPolicy policy) 167 { 168 this(tm, policy, -1); 169 } 170 171 public Required(TransactionManager tm, TxPolicy policy, int timeout) 172 { 173 this.tm = tm; 174 this.policy = policy; 175 this.timeout = timeout; 176 } 177 178 public Object invoke(Invocation invocation) throws Throwable 179 { 180 int oldTimeout = txTimeoutReader.getTransactionTimeOut(tm); 181 182 try 183 { 184 if (timeout != -1 && tm != null) 185 { 186 tm.setTransactionTimeout(timeout); 187 } 188 189 Transaction tx = tm.getTransaction(); 190 191 if (tx == null) 192 { 193 return policy.invokeInOurTx(invocation, tm); 194 } 195 else 196 { 197 return policy.invokeInCallerTx(invocation, tx); 198 } 199 } 200 finally 201 { 202 if (tm != null) 203 { 204 tm.setTransactionTimeout(oldTimeout); 205 } 206 } 207 } 208 209 public String getName() 210 { 211 return this.getClass().getName(); 212 } 213 } 214 215 public static class RequiresNew implements Interceptor 216 { 217 protected TransactionManager tm; 218 protected TxPolicy policy; 219 protected int timeout; 220 221 public RequiresNew(TransactionManager tm, TxPolicy policy) 222 { 223 this(tm, policy, -1); 224 } 225 226 public RequiresNew(TransactionManager tm, TxPolicy policy, int timeout) 227 { 228 this.tm = tm; 229 this.policy = policy; 230 this.timeout = timeout; 231 } 232 233 public String getName() 234 { 235 return this.getClass().getName(); 236 } 237 238 public Object invoke(Invocation invocation) throws Throwable 239 { 240 int oldTimeout = txTimeoutReader.getTransactionTimeOut(tm); 241 242 try 243 { 244 if (timeout != -1 && tm != null) 245 { 246 tm.setTransactionTimeout(timeout); 247 } 248 249 Transaction tx = tm.getTransaction(); 250 if (tx != null) 251 { 252 tm.suspend(); 253 try 254 { 255 return policy.invokeInOurTx(invocation, tm); 256 } 257 finally 258 { 259 tm.resume(tx); 260 } 261 } 262 else 263 { 264 return policy.invokeInOurTx(invocation, tm); 265 } 266 } 267 finally 268 { 269 if (tm != null) 270 { 271 tm.setTransactionTimeout(oldTimeout); 272 } 273 } 274 } 275 } 276 277 public static class Mandatory implements Interceptor 278 { 279 protected TransactionManager tm; 280 protected TxPolicy policy; 281 protected int timeout; 282 283 public Mandatory(TransactionManager tm, TxPolicy policy) 284 { 285 this(tm, policy, -1); 286 } 287 288 public Mandatory(TransactionManager tm, TxPolicy policy, int timeout) 289 { 290 this.tm = tm; 291 this.policy = policy; 292 this.timeout = timeout; 293 } 294 295 public String getName() 296 { 297 return this.getClass().getName(); 298 } 299 300 public Object invoke(Invocation invocation) throws Throwable 301 { 302 Transaction tx = tm.getTransaction(); 303 if (tx == null) 304 { 305 policy.throwMandatory(invocation); 306 } 307 return policy.invokeInCallerTx(invocation, tx); 308 } 309 310 } 311 } 312 | Popular Tags |