/*
 * 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 javax.transaction.NotSupportedException;
import javax.transaction.Status;
import javax.transaction.SystemException;

/**
 * This handler is to create an artifical "client"-side transaction
 * around the web-service request. Useful for interacting with entity beans.
 * It should be complemented by a seperate 
 * <code>org.jboss.net.axis.server.TransactionResponseHandler</code>
 * in the response chain to finish the 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 TransactionRequestHandler extends TransactionResponseHandler
{

   protected static final Object MARKER = new Object();

   public TransactionRequestHandler() throws Exception
   {
   }

   //
   // API
   //

   /**
    * begins a new transaction if not yet started
    * @see Handler#invoke(MessageContext)
    */
   public void invoke(MessageContext msgContext) throws AxisFault
   {
      try
      {
         if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION
                 && msgContext.getProperty(Constants.TRANSACTION_PROPERTY) == null)
         {
            userTransaction.begin();
            msgContext.setProperty(Constants.TRANSACTION_PROPERTY, MARKER);
         }
      }
      catch (SystemException e)
      {
         throw new AxisFault("Could not analyze tx setting.", e);
      }
      catch (NotSupportedException e)
      {
         throw new AxisFault("Could not begin tx.", e);
      }
   }
}
