1 17 package org.apache.geronimo.jetty.interceptor; 18 19 import javax.transaction.HeuristicMixedException ; 20 import javax.transaction.HeuristicRollbackException ; 21 import javax.transaction.RollbackException ; 22 import javax.transaction.SystemException ; 23 24 import org.apache.geronimo.transaction.context.TransactionContext; 25 import org.apache.geronimo.transaction.context.TransactionContextManager; 26 import org.mortbay.http.HttpRequest; 27 import org.mortbay.http.HttpResponse; 28 29 32 public class TransactionContextBeforeAfter implements BeforeAfter { 33 34 private final BeforeAfter next; 35 private final int oldTxIndex; 36 private final int newTxIndex; 37 private final TransactionContextManager transactionContextManager; 38 39 public TransactionContextBeforeAfter(BeforeAfter next, int oldTxIndex, int newTxIndex, TransactionContextManager transactionContextManager) { 40 this.next = next; 41 this.oldTxIndex = oldTxIndex; 42 this.newTxIndex = newTxIndex; 43 this.transactionContextManager = transactionContextManager; 44 } 45 46 public void before(Object [] context, HttpRequest httpRequest, HttpResponse httpResponse) { 47 TransactionContext oldTransactionContext = transactionContextManager.getContext(); 48 TransactionContext newTransactionContext = null; 49 if (oldTransactionContext == null || !oldTransactionContext.isInheritable()) { 50 newTransactionContext = transactionContextManager.newUnspecifiedTransactionContext(); 51 } 52 context[oldTxIndex] = oldTransactionContext; 53 context[newTxIndex] = newTransactionContext; 54 55 if (next != null) { 56 next.before(context, httpRequest, httpResponse); 57 } 58 } 59 60 public void after(Object [] context, HttpRequest httpRequest, HttpResponse httpResponse) { 61 try { 62 if (next != null) { 63 next.after(context, httpRequest, httpResponse); 64 } 65 } finally { 66 TransactionContext oldTransactionContext = (TransactionContext) context[oldTxIndex]; 67 TransactionContext newTransactionContext = (TransactionContext) context[newTxIndex]; 68 try { 69 if (newTransactionContext != null) { 70 if (newTransactionContext != transactionContextManager.getContext()) { 71 transactionContextManager.getContext().rollback(); 72 newTransactionContext.rollback(); 73 throw new RuntimeException ("WRONG EXCEPTION! returned from servlet call with wrong tx context"); 74 } 75 newTransactionContext.commit(); 76 77 } else { 78 if (oldTransactionContext != transactionContextManager.getContext()) { 79 if (transactionContextManager.getContext() != null) { 80 transactionContextManager.getContext().rollback(); 81 } 82 throw new RuntimeException ("WRONG EXCEPTION! returned from servlet call with wrong tx context"); 83 } 84 } 85 } catch (SystemException e) { 86 throw new RuntimeException ("WRONG EXCEPTION!", e); 87 } catch (HeuristicMixedException e) { 88 throw new RuntimeException ("WRONG EXCEPTION!", e); 89 } catch (HeuristicRollbackException e) { 90 throw new RuntimeException ("WRONG EXCEPTION!", e); 91 } catch (RollbackException e) { 92 throw new RuntimeException ("WRONG EXCEPTION!", e); 93 } finally { 94 transactionContextManager.setContext(oldTransactionContext); 96 } 97 } 98 } 99 100 } 101 | Popular Tags |