package org.jboss.mq.pm;
import java.io.Externalizable;
import java.util.ArrayList;
import javax.jms.JMSException;
public class Tx implements Comparable, Externalizable
{
static final long serialVersionUID = -5428592493990463362L;
private static final int OPEN = 0;
private static final int ENDED = 2;
long value = 0;
boolean persisted = false;
transient ArrayList postCommitTasks;
transient ArrayList postRollbackTasks;
transient int status = OPEN;
transient Object lock = new Object();
public Tx()
{
}
public Tx(long value)
{
this.value = value;
}
public void setValue(long tx)
{
value = tx;
}
public long longValue()
{
return value;
}
public synchronized boolean checkPersisted()
{
boolean result = persisted;
persisted = true;
return result;
}
public synchronized boolean wasPersisted()
{
return persisted;
}
public int compareTo(Tx anotherLong)
{
long thisVal = this.value;
long anotherVal = anotherLong.value;
return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
}
public int compareTo(Object o)
{
return compareTo((Tx) o);
}
public void readExternal(java.io.ObjectInput in) throws java.io.IOException
{
value = in.readLong();
}
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException
{
out.writeLong(value);
}
public String toString()
{
return String.valueOf(value);
}
public int hashCode()
{
return (int) (value ^ (value >> 32));
}
void commit(PersistenceManager pm) throws JMSException
{
synchronized (lock)
{
if (status == ENDED)
throw new JMSException("Transaction is not active for commit");
status = ENDED;
postRollbackTasks = null;
}
pm.commitPersistentTx(this);
synchronized (lock)
{
if (postCommitTasks == null)
return;
for (int i = 0; i < postCommitTasks.size(); ++i)
{
Runnable task = (Runnable) postCommitTasks.get(i);
task.run();
}
postCommitTasks = null;
}
}
void addPostCommitTask(Runnable task) throws JMSException
{
synchronized (lock)
{
if (status == ENDED)
throw new JMSException("Transacation is not active.");
if (postCommitTasks == null)
postCommitTasks = new ArrayList();
postCommitTasks.add(task);
}
}
public void rollback(PersistenceManager pm) throws JMSException
{
synchronized (lock)
{
if (status == ENDED)
throw new JMSException("Transaction is not active for rollback");
status = ENDED;
postCommitTasks = null;
}
pm.rollbackPersistentTx(this);
synchronized (lock)
{
if (postRollbackTasks == null)
return;
for (int i = 0; i < postRollbackTasks.size(); ++i)
{
Runnable task = (Runnable) postRollbackTasks.get(i);
task.run();
}
postRollbackTasks = null;
}
}
public void addPostRollbackTask(Runnable task) throws JMSException
{
synchronized (lock)
{
if (status == ENDED)
throw new JMSException("Transacation is not active.");
if (postRollbackTasks == null)
postRollbackTasks = new ArrayList();
postRollbackTasks.add(task);
}
}
}