1 /** 2 * 3 * Copyright 2003-2004 The Apache Software Foundation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.geronimo.tomcat.valve; 18 19 import java.io.IOException; 20 import javax.servlet.ServletException; 21 import javax.transaction.HeuristicMixedException; 22 import javax.transaction.HeuristicRollbackException; 23 import javax.transaction.RollbackException; 24 import javax.transaction.SystemException; 25 26 import org.apache.catalina.connector.Request; 27 import org.apache.catalina.connector.Response; 28 import org.apache.catalina.valves.ValveBase; 29 import org.apache.geronimo.transaction.context.TransactionContext; 30 import org.apache.geronimo.transaction.context.TransactionContextManager; 31 32 /** 33 * @version $Rev: $ $Date: $ 34 */ 35 public class TransactionContextValve extends ValveBase { 36 37 private final TransactionContextManager transactionContextManager; 38 39 public TransactionContextValve(TransactionContextManager transactionContextManager){ 40 this.transactionContextManager = transactionContextManager; 41 } 42 43 public void invoke(Request request, Response response) throws IOException, ServletException { 44 45 TransactionContext oldTransactionContext = transactionContextManager.getContext(); 46 TransactionContext newTransactionContext = null; 47 48 if (oldTransactionContext == null || !oldTransactionContext.isInheritable()) { 49 newTransactionContext = transactionContextManager.newUnspecifiedTransactionContext(); 50 } 51 52 // Pass this request on to the next valve in our pipeline 53 getNext().invoke(request, response); 54 55 try { 56 if (newTransactionContext != null) { 57 if (newTransactionContext != transactionContextManager.getContext()) { 58 transactionContextManager.getContext().rollback(); 59 newTransactionContext.rollback(); 60 throw new RuntimeException("WRONG EXCEPTION! returned from servlet call with wrong tx context"); 61 } 62 newTransactionContext.commit(); 63 64 } else { 65 if (oldTransactionContext != transactionContextManager.getContext()) { 66 if (transactionContextManager.getContext() != null) { 67 transactionContextManager.getContext().rollback(); 68 } 69 throw new RuntimeException("WRONG EXCEPTION! returned from servlet call with wrong tx context"); 70 } 71 } 72 } catch (SystemException e) { 73 throw new RuntimeException("WRONG EXCEPTION!", e); 74 } catch (HeuristicMixedException e) { 75 throw new RuntimeException("WRONG EXCEPTION!", e); 76 } catch (HeuristicRollbackException e) { 77 throw new RuntimeException("WRONG EXCEPTION!", e); 78 } catch (RollbackException e) { 79 throw new RuntimeException("WRONG EXCEPTION!", e); 80 } finally { 81 //this is redundant when we enter with an inheritable context and nothing goes wrong. 82 transactionContextManager.setContext(oldTransactionContext); 83 } 84 } 85 }