package org.jboss.ejb.plugins.lock;
import javax.transaction.Transaction;
import org.jboss.ejb.BeanLock;
import org.jboss.ejb.Container;
import org.jboss.ejb.BeanLockExt;
import org.jboss.invocation.Invocation;
import org.jboss.logging.Logger;
import org.jboss.util.deadlock.Resource;
public abstract class BeanLockSupport implements Resource, BeanLockExt
{
protected Container container = null;
protected int refs = 0;
protected Object id = null;
static Logger log = Logger.getLogger(BeanLock.class);
protected Transaction tx = null;
protected Thread synched = null;
protected int synchedDepth = 0;
protected int txTimeout;
public void setId(Object id) { this.id = id;}
public Object getId() { return id;}
public void setTimeout(int timeout) {txTimeout = timeout;}
public void setContainer(Container container) { this.container = container; }
public Object getResourceHolder() { return tx; }
public boolean attemptSync()
{
boolean didSync = false;
synchronized(this)
{
Thread thread = Thread.currentThread();
if(synched == null || synched.equals(thread) == true)
{
synched = thread;
++ synchedDepth;
didSync = true;
}
}
return didSync;
}
public void sync()
{
synchronized(this)
{
Thread thread = Thread.currentThread();
while(synched != null && synched.equals(thread) == false)
{
try
{
this.wait();
}
catch (InterruptedException ex) { }
}
synched = thread;
++synchedDepth;
}
}
public void releaseSync()
{
synchronized(this)
{
if (--synchedDepth == 0)
synched = null;
this.notify();
}
}
public abstract void schedule(Invocation mi) throws Exception;
public void setTransaction(Transaction tx){this.tx = tx;}
public Transaction getTransaction(){return tx;}
public abstract void endTransaction(Transaction tx);
public abstract void wontSynchronize(Transaction tx);
public abstract void endInvocation(Invocation mi);
public void addRef() { refs++;}
public void removeRef() { refs--;}
public int getRefs() { return refs;}
}