/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.net.axis.server;

import org.jboss.axis.AxisFault;
import org.jboss.axis.MessageContext;
import org.jboss.axis.handlers.BasicHandler;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

/**
 * This handler is to finish a previously opened client-side transaction.
 * <br>
 * <h3>Change notes</h3>
 *   <ul>
 *   </ul>
 * @created  22.03.2002
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @version $Revision: 1.1.4.3 $
 */

public class TransactionResponseHandler extends BasicHandler
{

   final protected UserTransaction userTransaction;

   public TransactionResponseHandler() throws NamingException
   {
      userTransaction =
              (UserTransaction)new InitialContext().
              lookup(Constants.USER_TRANSACTION_JNDI_NAME);
   }
   
   //
   // Protected Helpers
   //

   protected void endTransaction(MessageContext msgContext, boolean commit)
           throws AxisFault
   {
      Object tx =
              msgContext.getProperty(Constants.TRANSACTION_PROPERTY);
      if (tx != null)
      {
         try
         {
            if (commit)
            {
               userTransaction.commit();
            }
            else
            {
               userTransaction.rollback();
            }
         }
         catch (RollbackException e)
         {
            throw new AxisFault("Could not rollback tx.", e);
         }
         catch (SystemException e)
         {
            throw new AxisFault("Could not influence tx setting.", e);
         }
         catch (HeuristicMixedException e)
         {
            throw new AxisFault("Could not commit tx.", e);
         }
         catch (HeuristicRollbackException e)
         {
            throw new AxisFault("Could not commit tx.", e);
         }
         finally
         {
            msgContext.setProperty(Constants.TRANSACTION_PROPERTY, null);
         }
      }
   }

   //
   // API
   //

   /*
    * @see Handler#invoke(MessageContext)
    */
   public void invoke(MessageContext msgContext) throws AxisFault
   {
      endTransaction(msgContext, true);
   }

   /*
    * @see Handler#onFault(MessageContext)
    */
   public void onFault(MessageContext msgContext)
   {
      try
      {
         endTransaction(msgContext, false);
      }
      catch (AxisFault e)
      {
      }
   }

}