001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.filters;
030:
031: import javax.naming.Context;
032: import javax.naming.InitialContext;
033: import javax.naming.NamingException;
034: import javax.servlet.Filter;
035: import javax.servlet.FilterChain;
036: import javax.servlet.FilterConfig;
037: import javax.servlet.ServletException;
038: import javax.servlet.ServletRequest;
039: import javax.servlet.ServletResponse;
040: import javax.transaction.UserTransaction;
041: import java.io.IOException;
042:
043: /**
044: * Wraps the request in a transaction. All database calls for
045: * the request will either succeed together or fail.
046: *
047: * @since Resin 2.0.5
048: */
049: public class TransactionFilter implements Filter {
050: /**
051: * The UserTransaction object.
052: */
053: private UserTransaction _userTransaction;
054:
055: /**
056: * Lookup java:comp/UserTransaction and cache the results.
057: */
058: public void init(FilterConfig config) throws ServletException {
059: try {
060: Context ic = (Context) new InitialContext();
061:
062: _userTransaction = (UserTransaction) ic
063: .lookup("java:comp/UserTransaction");
064: } catch (NamingException e) {
065: throw new ServletException(e);
066: }
067: }
068:
069: /**
070: * Wrap the request in a transaction. If the request returns normally,
071: * the transaction will commit. If an exception is thrown it will
072: * rollback.
073: */
074: public void doFilter(ServletRequest request,
075: ServletResponse response, FilterChain nextFilter)
076: throws ServletException, IOException {
077: try {
078: _userTransaction.begin();
079:
080: nextFilter.doFilter(request, response);
081:
082: _userTransaction.commit();
083: } catch (ServletException e) {
084: rollback();
085: throw e;
086: } catch (IOException e) {
087: rollback();
088: throw e;
089: } catch (RuntimeException e) {
090: rollback();
091: throw e;
092: } catch (Throwable e) {
093: rollback();
094: throw new ServletException(e);
095: }
096: }
097:
098: /**
099: * Rolls the request back.
100: */
101: private void rollback() throws ServletException {
102: try {
103: _userTransaction.rollback();
104: } catch (Exception e) {
105: throw new ServletException(e);
106: }
107: }
108:
109: /**
110: * Any cleanup for the filter.
111: */
112: public void destroy() {
113: }
114: }
|