001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 021 package org.apache.geronimo.jetty6.handler; 022 023 import java.io.IOException; 024 025 import javax.servlet.ServletException; 026 import javax.servlet.http.HttpServletRequest; 027 import javax.servlet.http.HttpServletResponse; 028 import javax.transaction.Status; 029 import javax.transaction.SystemException; 030 import javax.transaction.UserTransaction; 031 032 import org.mortbay.jetty.handler.AbstractHandler; 033 034 /** 035 * @version $Rev: 535017 $ $Date: 2007-05-03 19:10:58 -0400 (Thu, 03 May 2007) $ 036 */ 037 public class UserTransactionHandler extends AbstractImmutableHandler { 038 private final UserTransaction userTransaction; 039 040 public UserTransactionHandler(AbstractHandler next, UserTransaction userTransaction) { 041 super(next); 042 this.userTransaction = userTransaction; 043 } 044 045 public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { 046 boolean active = isActive(); 047 try { 048 next.handle(target, request, response, dispatch); 049 } finally { 050 if ((!active && isMarkedRollback()) || (dispatch == REQUEST && isActive())) { 051 try { 052 userTransaction.rollback(); 053 } catch (SystemException e) { 054 throw new ServletException("Error rolling back transaction left open by user program", e); 055 } 056 } 057 } 058 } 059 060 public void lifecycleCommand(LifecycleCommand lifecycleCommand) throws Exception { 061 boolean active = isActive(); 062 try { 063 super.lifecycleCommand(lifecycleCommand); 064 } finally { 065 if (!active && isActive()) { 066 try { 067 userTransaction.rollback(); 068 } catch (SystemException e) { 069 throw new ServletException("Error rolling back transaction left open by user program", e); 070 } 071 } 072 } 073 } 074 075 private boolean isActive() throws ServletException { 076 try { 077 return !(userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION 078 || userTransaction.getStatus() == Status.STATUS_COMMITTED); 079 } catch (SystemException e) { 080 throw new ServletException("Could not determine transaction status", e); 081 } 082 } 083 private boolean isMarkedRollback() throws ServletException { 084 try { 085 return userTransaction.getStatus() == Status.STATUS_MARKED_ROLLBACK; 086 } catch (SystemException e) { 087 throw new ServletException("Could not determine transaction status", e); 088 } 089 } 090 }