package org.jboss.ejb;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.rmi.RemoteException;
import java.security.Principal;
import java.util.Collection;
import java.util.Date;
import javax.ejb.EJBContext;
import javax.ejb.EJBException;
import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import javax.transaction.UserTransaction;
import org.jboss.metadata.MessageDrivenMetaData;
import org.jboss.metadata.MetaData;
public class MessageDrivenEnterpriseContext
extends EnterpriseContext
{
private MessageDrivenContext ctx;
public MessageDrivenEnterpriseContext(Object instance, Container con)
throws Exception
{
super(instance, con);
ctx = new MessageDrivenContextImpl();
((MessageDrivenBean)instance).setMessageDrivenContext(ctx);
try
{
AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_CREATE);
Method ejbCreate = instance.getClass().getMethod("ejbCreate", new Class[0]);
ejbCreate.invoke(instance, new Object[0]);
}
catch (InvocationTargetException e)
{
Throwable t = e.getTargetException();
if (t instanceof RuntimeException) {
if (t instanceof EJBException) {
throw (EJBException)t;
}
else {
throw new EJBException((RuntimeException)t);
}
}
else if (t instanceof Exception) {
throw (Exception)t;
}
else if (t instanceof Error) {
throw (Error)t;
}
else {
throw new org.jboss.util.NestedError("Unexpected Throwable", t);
}
}
}
public MessageDrivenContext getMessageDrivenContext()
{
return ctx;
}
public void discard() throws RemoteException
{
((MessageDrivenBean)instance).ejbRemove();
}
public EJBContext getEJBContext()
{
return ctx;
}
protected class MessageDrivenContextImpl
extends EJBContextImpl
implements MessageDrivenContext
{
public EJBHome getEJBHome()
{
throw new IllegalStateException("getEJBHome should not be access from a message driven bean");
}
public EJBLocalHome getEJBLocalHome()
{
throw new IllegalStateException("getEJBHome should not be access from a message driven bean");
}
public TimerService getTimerService() throws IllegalStateException
{
AllowedOperationsAssociation.assertAllowedIn("getTimerService",
IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
return new TimerServiceWrapper(this, super.getTimerService());
}
public Principal getCallerPrincipal()
{
AllowedOperationsAssociation.assertAllowedIn("getCallerPrincipal",
IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
return super.getCallerPrincipal();
}
public boolean isCallerInRole(String id)
{
throw new IllegalStateException("isCallerInRole should not be access from a message driven bean");
}
public UserTransaction getUserTransaction()
{
if (isContainerManagedTx())
throw new IllegalStateException("getUserTransaction should not be access for container managed Tx");
AllowedOperationsAssociation.assertAllowedIn("getUserTransaction",
IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
return super.getUserTransaction();
}
public boolean getRollbackOnly()
{
if (isUserManagedTx())
throw new IllegalStateException("getRollbackOnly should not be access for user managed Tx");
AllowedOperationsAssociation.assertAllowedIn("getRollbackOnly",
IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
if (!isTxRequired()) {
throw new IllegalStateException
("getRollbackOnly must only be called in the context of a transaction (EJB 2.0 - 15.5.1)");
}
return super.getRollbackOnly();
}
public void setRollbackOnly()
{
if (isUserManagedTx())
throw new IllegalStateException("setRollbackOnly should not be access for user managed Tx");
AllowedOperationsAssociation.assertAllowedIn("getRollbackOnly",
IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
if (!isTxRequired()) {
throw new IllegalStateException
("setRollbackOnly must only be called in the context of a transaction (EJB 2.0 - 15.5.1)");
}
super.setRollbackOnly();
}
private boolean isTxRequired()
{
MessageDrivenMetaData md = (MessageDrivenMetaData)con.getBeanMetaData();
return md.getMethodTransactionType() == MetaData.TX_REQUIRED;
}
}
public class TimerServiceWrapper implements TimerService
{
private EnterpriseContext.EJBContextImpl context;
private TimerService timerService;
public TimerServiceWrapper(EnterpriseContext.EJBContextImpl ctx, TimerService timerService)
{
this.context = ctx;
this.timerService = timerService;
}
public Timer createTimer(long duration, Serializable info) throws IllegalArgumentException, IllegalStateException, EJBException
{
assertAllowedIn("TimerService.createTimer");
return timerService.createTimer(duration, info);
}
public Timer createTimer(long initialDuration, long intervalDuration, Serializable info) throws IllegalArgumentException, IllegalStateException, EJBException
{
assertAllowedIn("TimerService.createTimer");
return timerService.createTimer(initialDuration, intervalDuration, info);
}
public Timer createTimer(Date expiration, Serializable info) throws IllegalArgumentException, IllegalStateException, EJBException
{
assertAllowedIn("TimerService.createTimer");
return timerService.createTimer(expiration, info);
}
public Timer createTimer(Date initialExpiration, long intervalDuration, Serializable info) throws IllegalArgumentException, IllegalStateException, EJBException
{
assertAllowedIn("TimerService.createTimer");
return timerService.createTimer(initialExpiration, intervalDuration, info);
}
public Collection getTimers() throws IllegalStateException, EJBException
{
assertAllowedIn("TimerService.getTimers");
return timerService.getTimers();
}
private void assertAllowedIn(String timerMethod)
{
AllowedOperationsAssociation.assertAllowedIn(timerMethod,
IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
}
}
}