package org.jboss.tm.usertx.client;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EventListener;
import java.util.Iterator;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
public class ServerVMClientUserTransaction
implements UserTransaction
{
private final static ServerVMClientUserTransaction singleton = new ServerVMClientUserTransaction();
private final TransactionManager tm;
private final Collection listeners = new ArrayList();
public static ServerVMClientUserTransaction getSingleton()
{
return singleton;
}
private ServerVMClientUserTransaction()
{
TransactionManager local = null;
try {
local = (TransactionManager)new InitialContext().lookup("java:/TransactionManager");
} catch (NamingException ex)
{
}
tm = local;
}
public ServerVMClientUserTransaction(final TransactionManager tm)
{
this.tm = tm;
}
public void registerTxStartedListener(UserTransactionStartedListener txStartedListener)
{
listeners.add(txStartedListener);
}
public void unregisterTxStartedListener(UserTransactionStartedListener txStartedListener)
{
listeners.remove(txStartedListener);
}
public void begin()
throws NotSupportedException, SystemException
{
tm.begin();
for (Iterator i = listeners.iterator(); i.hasNext(); )
{
((UserTransactionStartedListener)i.next()).userTransactionStarted();
}
}
public void commit()
throws RollbackException,
HeuristicMixedException,
HeuristicRollbackException,
SecurityException,
IllegalStateException,
SystemException
{
tm.commit();
}
public void rollback()
throws SecurityException,
IllegalStateException,
SystemException
{
tm.rollback();
}
public void setRollbackOnly()
throws IllegalStateException,
SystemException
{
tm.setRollbackOnly();
}
public int getStatus()
throws SystemException
{
return tm.getStatus();
}
public void setTransactionTimeout(int seconds)
throws SystemException
{
tm.setTransactionTimeout(seconds);
}
public interface UserTransactionStartedListener extends EventListener
{
void userTransactionStarted() throws SystemException;
}
}