package org.jboss.cache.transaction;
import org.jboss.logging.Logger;
import javax.transaction.*;
import javax.transaction.xa.XAResource;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class DummyTransaction implements Transaction {
int status=Status.STATUS_UNKNOWN;
Logger log=Logger.getLogger(DummyTransaction.class);
DummyBaseTransactionManager tm_;
List participants=new LinkedList();
public DummyTransaction(DummyBaseTransactionManager tm) {
tm_=tm;
status=Status.STATUS_ACTIVE;
}
public void commit() throws RollbackException, HeuristicMixedException,
HeuristicRollbackException, SecurityException, SystemException {
boolean doCommit;
status=Status.STATUS_PREPARING;
try {
boolean outcome=notifyBeforeCompletion();
if(outcome == true && status != Status.STATUS_MARKED_ROLLBACK) {
status=Status.STATUS_COMMITTING;
doCommit=true;
}
else {
status=Status.STATUS_ROLLING_BACK;
doCommit=false;
}
notifyAfterCompletion(doCommit? Status.STATUS_COMMITTED : Status.STATUS_MARKED_ROLLBACK);
status=doCommit? Status.STATUS_COMMITTED : Status.STATUS_MARKED_ROLLBACK;
if(doCommit == false)
throw new RollbackException();
}
finally {
tm_.setTransaction(null);
}
}
public void rollback() throws IllegalStateException, SystemException {
try {
status=Status.STATUS_ROLLING_BACK;
notifyAfterCompletion(Status.STATUS_ROLLEDBACK);
}
catch(Throwable t) {
}
status=Status.STATUS_ROLLEDBACK;
tm_.setTransaction(null);
}
public void setRollbackOnly() throws IllegalStateException, SystemException {
status=Status.STATUS_MARKED_ROLLBACK;
}
public int getStatus() throws SystemException {
return status;
}
public void setTransactionTimeout(int seconds) throws SystemException {
throw new SystemException("not supported");
}
public boolean enlistResource(XAResource xaRes)
throws RollbackException, IllegalStateException, SystemException {
throw new SystemException("not supported");
}
public boolean delistResource(XAResource xaRes, int flag)
throws IllegalStateException, SystemException {
throw new SystemException("not supported");
}
public void registerSynchronization(Synchronization sync)
throws RollbackException, IllegalStateException, SystemException {
if(sync == null)
throw new IllegalArgumentException("null synchronization " + this);
switch(status) {
case Status.STATUS_ACTIVE:
case Status.STATUS_PREPARING:
break;
case Status.STATUS_PREPARED:
throw new IllegalStateException("already prepared. " + this);
case Status.STATUS_COMMITTING:
throw new IllegalStateException("already started committing. " + this);
case Status.STATUS_COMMITTED:
throw new IllegalStateException("already committed. " + this);
case Status.STATUS_MARKED_ROLLBACK:
throw new RollbackException("already marked for rollback " + this);
case Status.STATUS_ROLLING_BACK:
throw new RollbackException("already started rolling back. " + this);
case Status.STATUS_ROLLEDBACK:
throw new RollbackException("already rolled back. " + this);
case Status.STATUS_NO_TRANSACTION:
throw new IllegalStateException("no transaction. " + this);
case Status.STATUS_UNKNOWN:
throw new IllegalStateException("unknown state " + this);
default:
throw new IllegalStateException("illegal status: " + status + " tx=" + this);
}
synchronized(participants) {
if(!participants.contains(sync)) {
log.debug("registering synchronization handler " + sync);
participants.add(sync);
}
}
}
void setStatus(int new_status) {
status=new_status;
}
boolean notifyBeforeCompletion() {
boolean retval=true;
List tmp;
synchronized(participants) {
tmp=new LinkedList(participants);
}
for(Iterator it=tmp.iterator(); it.hasNext();) {
Synchronization s=(Synchronization)it.next();
log.debug("processing beforeCompletion for " + s);
try {
s.beforeCompletion();
}
catch(Throwable t) {
retval=false;
log.error("beforeCompletion() failed for " + s, t);
}
}
return retval;
}
void notifyAfterCompletion(int status) {
List tmp;
synchronized(participants) {
tmp=new LinkedList(participants);
}
for(Iterator it=tmp.iterator(); it.hasNext();) {
Synchronization s=(Synchronization)it.next();
log.debug("processing afterCompletion for " + s);
try {
s.afterCompletion(status);
}
catch(Throwable t) {
log.error("afterCompletion() failed for " + s, t);
}
}
synchronized(participants) {
participants.clear();
}
}
}