package org.jboss.ejb.plugins;
import java.lang.reflect.Method;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
import javax.ejb.EJBException;
import javax.transaction.Transaction;
import javax.transaction.Status;
import org.jboss.invocation.Invocation;
import org.jboss.invocation.InvocationType;
import org.jboss.ejb.Container;
import org.jboss.ejb.EntityEnterpriseContext;
import org.jboss.metadata.EntityMetaData;
import org.jboss.ejb.plugins.lock.Entrancy;
import org.jboss.ejb.plugins.lock.NonReentrantLock;
import org.jboss.ejb.plugins.cmp.jdbc.bridge.CMRInvocation;
public class EntityReentranceInterceptor
extends AbstractInterceptor
{
protected boolean reentrant = false;
public void setContainer(Container container)
{
super.setContainer(container);
if (container != null)
{
EntityMetaData meta = (EntityMetaData) container.getBeanMetaData();
reentrant = meta.isReentrant();
}
}
protected boolean isTxExpired(Transaction miTx) throws Exception
{
if (miTx != null && miTx.getStatus() == Status.STATUS_MARKED_ROLLBACK)
{
return true;
}
return false;
}
public Object invoke(Invocation mi)
throws Exception
{
EntityEnterpriseContext ctx = (EntityEnterpriseContext) mi.getEnterpriseContext();
boolean nonReentrant = !(reentrant || isReentrantMethod(mi));
NonReentrantLock methodLock = ctx.getMethodLock();
Transaction miTx = ctx.getTransaction();
boolean locked = false;
try
{
while (!locked)
{
if (methodLock.attempt(5000, miTx, nonReentrant))
{
locked = true;
}
else
{
if (isTxExpired(miTx))
{
log.error("Saw rolled back tx=" + miTx);
throw new RuntimeException("Transaction marked for rollback, possibly a timeout");
}
}
}
}
catch (NonReentrantLock.ReentranceException re)
{
if (mi.getType() == InvocationType.REMOTE)
{
throw new RemoteException("Reentrant method call detected: "
+ container.getBeanMetaData().getEjbName() + " "
+ ctx.getId().toString());
}
else
{
throw new EJBException("Reentrant method call detected: "
+ container.getBeanMetaData().getEjbName() + " "
+ ctx.getId().toString());
}
}
try
{
ctx.lock();
return getNext().invoke(mi);
}
finally
{
ctx.unlock();
methodLock.release(nonReentrant);
}
}
private static final Method getEJBHome;
private static final Method getHandle;
private static final Method getPrimaryKey;
private static final Method isIdentical;
private static final Method remove;
static
{
try
{
Class[] noArg = new Class[0];
getEJBHome = EJBObject.class.getMethod("getEJBHome", noArg);
getHandle = EJBObject.class.getMethod("getHandle", noArg);
getPrimaryKey = EJBObject.class.getMethod("getPrimaryKey", noArg);
isIdentical = EJBObject.class.getMethod("isIdentical", new Class[]{EJBObject.class});
remove = EJBObject.class.getMethod("remove", noArg);
}
catch (Exception e)
{
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
protected boolean isReentrantMethod(Invocation mi)
{
Method m = mi.getMethod();
if (m != null && (
m.equals(getEJBHome) ||
m.equals(getHandle) ||
m.equals(getPrimaryKey) ||
m.equals(isIdentical) ||
m.equals(remove)))
{
return true;
}
if (mi instanceof CMRInvocation)
{
Entrancy entrancy = ((CMRInvocation) mi).getEntrancy();
if (entrancy == Entrancy.NON_ENTRANT)
{
log.trace("NON_ENTRANT invocation");
return true;
}
}
return false;
}
}