package org.jboss.ejb.plugins;
import org.jboss.ejb.EnterpriseContext;
import org.jboss.ejb.InstancePool;
import org.jboss.ejb.MessageDrivenContainer;
import org.jboss.ejb.AllowedOperationsAssociation;
import org.jboss.invocation.Invocation;
import javax.ejb.TimedObject;
import javax.ejb.Timer;
import java.rmi.RemoteException;
import java.lang.reflect.Method;
public class MessageDrivenInstanceInterceptor
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(final Invocation mi)
throws Exception
{
throw new Error("Not valid for MessageDriven beans");
}
public Object invoke(final Invocation mi)
throws Exception
{
MessageDrivenContainer mdc = (MessageDrivenContainer) container;
InstancePool pool = mdc.getInstancePool();
EnterpriseContext ctx = pool.get();
ctx.setPrincipal(mi.getPrincipal());
mi.setEnterpriseContext(ctx);
EnterpriseBeanPolicyContextHandler.setEnterpriseBean(ctx.getInstance());
if (ejbTimeout.equals(mi.getMethod()))
AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_TIMEOUT);
else
AllowedOperationsAssociation.pushInMethodFlag(IN_BUSINESS_METHOD);
try
{
Object obj = getNext().invoke(mi);
return obj;
}
catch (RuntimeException e) {
mi.setEnterpriseContext(null);
throw e;
}
catch (RemoteException e) {
mi.setEnterpriseContext(null);
throw e;
}
catch (Error e) {
mi.setEnterpriseContext(null);
throw e;
}
finally
{
AllowedOperationsAssociation.popInMethodFlag();
if (mi.getEnterpriseContext() != null)
{
pool.free((EnterpriseContext) mi.getEnterpriseContext());
}
else
{
pool.discard(ctx);
}
}
}
}