package org.jboss.ejb.plugins;
import org.jboss.ejb.BeanLock;
import org.jboss.ejb.Container;
import org.jboss.ejb.EnterpriseContext;
import org.jboss.ejb.InstanceCache;
import org.jboss.ejb.InstancePool;
import org.jboss.ejb.StatefulSessionContainer;
import org.jboss.ejb.AllowedOperationsAssociation;
import org.jboss.invocation.Invocation;
import org.jboss.logging.Logger;
import org.jboss.metadata.SessionMetaData;
import javax.ejb.EJBException;
import javax.ejb.EJBObject;
import javax.ejb.Handle;
import javax.ejb.NoSuchObjectLocalException;
import javax.ejb.TimedObject;
import javax.ejb.Timer;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.Synchronization;
import javax.transaction.Transaction;
import java.lang.reflect.Method;
import java.rmi.NoSuchObjectException;
import java.rmi.RemoteException;
public class StatefulSessionInstanceInterceptor
extends AbstractInterceptor
{
protected Logger log = Logger.getLogger(this.getClass());
protected StatefulSessionContainer container;
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;
private static final Method getEJBObject;
private static final Method ejbTimeout;
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);
getEJBObject = Handle.class.getMethod("getEJBObject", noArg);
ejbTimeout = TimedObject.class.getMethod("ejbTimeout", new Class[]{Timer.class});
}
catch (Exception e)
{
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public void setContainer(Container container)
{
this.container = (StatefulSessionContainer)container;
}
public Container getContainer()
{
return container;
}
public Object invokeHome(Invocation mi)
throws Exception
{
if (getEJBObject.equals(mi.getMethod()))
return getNext().invokeHome(mi);
InstancePool pool = container.getInstancePool();
EnterpriseContext ctx = pool.get();
mi.setEnterpriseContext(ctx);
ctx.lock();
ctx.setPrincipal(mi.getPrincipal());
AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_HOME);
try
{
return getNext().invokeHome(mi);
}
finally
{
synchronized (ctx)
{
AllowedOperationsAssociation.popInMethodFlag();
ctx.unlock();
if (ctx.getId() == null)
{
pool.free(ctx);
}
}
}
}
protected void register(EnterpriseContext ctx, Transaction tx, BeanLock lock)
{
InstanceSynchronization synch = new InstanceSynchronization(ctx, lock);
try
{
if (tx.getStatus() == Status.STATUS_MARKED_ROLLBACK)
{
return;
}
try
{
tx.registerSynchronization(synch);
}
catch (Exception ex)
{
getContainer().getLockManager().removeLockRef(lock.getId());
throw ex;
}
synch.afterBegin();
} catch (RollbackException e)
{
} catch (Exception e)
{
throw new EJBException(e);
}
}
public Object invoke(Invocation mi)
throws Exception
{
InstanceCache cache = container.getInstanceCache();
InstancePool pool = container.getInstancePool();
Object methodID = mi.getId();
EnterpriseContext ctx = null;
BeanLock lock = container.getLockManager().getLock(methodID);
try
{
SecurityActions.pushSubjectContext(mi.getPrincipal(), mi.getCredential(), null);
lock.sync();
try
{
try
{
ctx = cache.get(methodID);
}
catch (NoSuchObjectException e)
{
if (mi.isLocal())
throw new NoSuchObjectLocalException(e.getMessage());
else
throw e;
}
mi.setEnterpriseContext(ctx);
EnterpriseBeanPolicyContextHandler.setEnterpriseBean(ctx.getInstance());
boolean isBMT = ((SessionMetaData)container.getBeanMetaData()).isBeanManagedTx();
if (isBMT == false)
{
if (ctx.getTransaction() != null &&
!ctx.getTransaction().equals(mi.getTransaction()))
{
StringBuffer msg = new StringBuffer("Application Error: " +
"tried to enter Stateful bean with different tx context");
msg.append(", contextTx: " + ctx.getTransaction());
msg.append(", methodTx: " + mi.getTransaction());
throw new EJBException(msg.toString());
}
if (ctx.getTransaction() == null && mi.getTransaction() != null)
{
register(ctx, mi.getTransaction(), lock);
}
}
if (!ctx.isLocked())
{
ctx.lock();
}
else
{
if (!isCallAllowed(mi))
{
throw new EJBException("Application Error: no concurrent " +
"calls on stateful beans");
}
else
{
ctx.lock();
}
}
}
finally
{
lock.releaseSync();
}
ctx.setPrincipal(mi.getPrincipal());
if (ejbTimeout.equals(mi.getMethod()))
AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_TIMEOUT);
else
AllowedOperationsAssociation.pushInMethodFlag(IN_BUSINESS_METHOD);
boolean validContext = true;
try
{
Object ret = getNext().invoke(mi);
return ret;
}
catch (RemoteException e)
{
cache.remove(methodID);
pool.discard(ctx);
validContext = false;
throw e;
}
catch (RuntimeException e)
{
cache.remove(methodID);
pool.discard(ctx);
validContext = false;
throw e;
}
catch (Error e)
{
cache.remove(methodID);
pool.discard(ctx);
validContext = false;
throw e;
}
finally
{
AllowedOperationsAssociation.popInMethodFlag();
if (validContext)
{
lock.sync();
try
{
ctx.unlock();
if (ctx.getId() == null)
{
cache.remove(methodID);
pool.free(ctx);
}
}
finally
{
lock.releaseSync();
}
}
}
}
finally
{
container.getLockManager().removeLockRef(lock.getId());
SecurityActions.popSubjectContext();
}
}
protected boolean isCallAllowed(Invocation mi)
{
Method m = mi.getMethod();
if (m.equals(getEJBHome) ||
m.equals(getHandle) ||
m.equals(getPrimaryKey) ||
m.equals(isIdentical) ||
m.equals(remove))
{
return true;
}
return false;
}
private class InstanceSynchronization
implements Synchronization
{
private EnterpriseContext ctx;
private boolean notifySession = false;
private Method afterBegin;
private Method beforeCompletion;
private Method afterCompletion;
private BeanLock lock;
private boolean beforeCompletionInvoked = false;
InstanceSynchronization(EnterpriseContext ctx, BeanLock lock)
{
this.ctx = ctx;
this.lock = lock;
this.lock.addRef();
notifySession = (ctx.getInstance() instanceof javax.ejb.SessionSynchronization);
if (notifySession)
{
try
{
Class sync = Class.forName("javax.ejb.SessionSynchronization");
afterBegin = sync.getMethod("afterBegin", new Class[0]);
beforeCompletion = sync.getMethod("beforeCompletion", new Class[0]);
afterCompletion = sync.getMethod("afterCompletion", new Class[]
{boolean.class});
}
catch (Exception e)
{
log.error("failed to setup InstanceSynchronization", e);
}
}
}
public void afterBegin()
{
if (notifySession)
{
try
{
AllowedOperationsAssociation.pushInMethodFlag(IN_AFTER_BEGIN);
afterBegin.invoke(ctx.getInstance(), new Object[0]);
}
catch (Exception e)
{
log.error("failed to invoke afterBegin", e);
}
finally{
AllowedOperationsAssociation.popInMethodFlag();
}
}
}
public void beforeCompletion()
{
if( log.isTraceEnabled() )
log.trace("beforeCompletion called");
ctx.lock();
beforeCompletionInvoked = true;
if (notifySession)
{
try
{
AllowedOperationsAssociation.pushInMethodFlag(IN_BEFORE_COMPLETION);
beforeCompletion.invoke(ctx.getInstance(), new Object[0]);
}
catch (Exception e)
{
log.error("failed to invoke beforeCompletion", e);
}
finally{
AllowedOperationsAssociation.popInMethodFlag();
}
}
}
public void afterCompletion(int status)
{
if( log.isTraceEnabled() )
log.trace("afterCompletion called");
lock.sync();
try
{
ctx.setTransaction(null);
if (beforeCompletionInvoked)
ctx.unlock();
if (notifySession)
{
try
{
AllowedOperationsAssociation.pushInMethodFlag(IN_AFTER_COMPLETION);
if (status == Status.STATUS_COMMITTED)
{
afterCompletion.invoke(ctx.getInstance(), new Object[]{Boolean.TRUE});
}
else
{
afterCompletion.invoke(ctx.getInstance(), new Object[]{Boolean.FALSE});
}
}
catch (Exception e)
{
log.error("failed to invoke afterCompletion", e);
}
finally{
AllowedOperationsAssociation.popInMethodFlag();
}
}
}
finally
{
lock.releaseSync();
container.getLockManager().removeLockRef(lock.getId());
}
}
}
}