package org.jboss.ejb.plugins;
import org.jboss.ejb.EntityContainer;
import org.jboss.ejb.EntityPersistenceManager;
import org.jboss.ejb.EntityEnterpriseContext;
import org.jboss.ejb.InstancePool;
import org.jboss.ejb.AllowedOperationsAssociation;
import org.jboss.invocation.Invocation;
import javax.ejb.TimedObject;
import javax.ejb.Timer;
import java.lang.reflect.Method;
public class EntityMultiInstanceInterceptor
extends AbstractInterceptor
{
protected static final Method ejbTimeout;
static
{
try
{
ejbTimeout = TimedObject.class.getMethod("ejbTimeout", new Class[]{Timer.class});
}
catch (Exception e)
{
throw new ExceptionInInitializerError(e);
}
}
public Object invokeHome(Invocation mi)
throws Exception
{
EntityContainer ec = (EntityContainer) getContainer();
EntityEnterpriseContext ctx = (EntityEnterpriseContext) ec.getInstancePool().get();
mi.setEnterpriseContext(ctx);
ctx.setTransaction(mi.getTransaction());
ctx.setPrincipal(mi.getPrincipal());
AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_HOME);
Object result;
try
{
result = getNext().invokeHome(mi);
}
finally
{
AllowedOperationsAssociation.popInMethodFlag();
}
if (ctx.getId() == null)
{
ctx.setTransaction(null);
ec.getInstancePool().free(ctx);
}
return result;
}
public Object invoke(Invocation mi)
throws Exception
{
Object key = mi.getId();
EntityEnterpriseContext ctx = null;
EntityContainer ec = (EntityContainer) container;
if (mi.getTransaction() != null)
{
}
if (ctx == null)
{
InstancePool pool = ec.getInstancePool();
ctx = (EntityEnterpriseContext) pool.get();
ctx.setCacheKey(key);
ctx.setId(key);
EntityPersistenceManager pm = ec.getPersistenceManager();
pm.activateEntity(ctx);
}
boolean trace = log.isTraceEnabled();
if( trace ) log.trace("Begin invoke, key="+key);
ctx.setTransaction(mi.getTransaction());
ctx.setPrincipal(mi.getPrincipal());
EnterpriseBeanPolicyContextHandler.setEnterpriseBean(ctx.getInstance());
mi.setEnterpriseContext(ctx);
if (ejbTimeout.equals(mi.getMethod()))
AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_TIMEOUT);
else
AllowedOperationsAssociation.pushInMethodFlag(IN_BUSINESS_METHOD);
try
{
Object ret = getNext().invoke(mi);
return ret;
}
finally
{
AllowedOperationsAssociation.popInMethodFlag();
}
}
}