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;
public class TransactionResponseHandler extends BasicHandler
{
final protected UserTransaction userTransaction;
public TransactionResponseHandler() throws NamingException
{
userTransaction =
(UserTransaction)new InitialContext().
lookup(Constants.USER_TRANSACTION_JNDI_NAME);
}
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);
}
}
}
public void invoke(MessageContext msgContext) throws AxisFault
{
endTransaction(msgContext, true);
}
public void onFault(MessageContext msgContext)
{
try
{
endTransaction(msgContext, false);
}
catch (AxisFault e)
{
}
}
}