Boolean transacted = (Boolean)getMetaData(handler, JMSAdvisor.TRANSACTED); if (!transacted.booleanValue()) { throw new IllegalStateException("Session is not transacted - cannot call commit()"); }
   public Object invoke(Invocation invocation) throws Throwable   {      if (invocation instanceof MethodInvocation)      {         MethodInvocation mi = (MethodInvocation)invocation;               String methodName = mi.getMethod().getName();         			if ("createConnectionDelegate".equals(methodName))			{				if (log.isTraceEnabled()) log.trace("creating resource manager");				//Create new ResourceManager and add to meta-data for invocation handler				//for connection				ConnectionDelegate connectionDelegate = (ConnectionDelegate)invocation.invokeNext();				ResourceManager rm = new ResourceManager(connectionDelegate);				JMSInvocationHandler handler = (JMSInvocationHandler)Proxy.getInvocationHandler(connectionDelegate);								handler.getMetaData().addMetaData(JMSAdvisor.JMS, JMSAdvisor.RESOURCE_MANAGER, rm, PayloadKey.TRANSIENT);				return connectionDelegate;			}						else if ("createSessionDelegate".equals(methodName))         {            if (log.isTraceEnabled()) log.trace("createSessionDelegate");                        SessionDelegate sd = (SessionDelegate)invocation.invokeNext();                        //If we get here, the session has been created                        Boolean transacted = (Boolean)mi.getArguments()[0];            				if (log.isTraceEnabled()) log.trace("Transacted: " + transacted);                        if (transacted == null)               throw new IllegalStateException("Cannot find transacted argument");            if (transacted.booleanValue())            {					if (log.isTraceEnabled()) log.trace("Session is transacted");                              //Session has been created and is transacted so we start a tx                              					JMSInvocationHandler handler = ((JMSMethodInvocation)invocation).getHandler();										ResourceManager rm =						(ResourceManager)handler.getMetaData().getMetaData(JMSAdvisor.JMS, JMSAdvisor.RESOURCE_MANAGER);						               Object Xid = rm.createLocalTx();               					if (log.isTraceEnabled()) log.trace("Created tx");                              //And set it on the meta-data of the returned delegate's invocation handler               JMSInvocationHandler sessionHandler = getHandler(sd);                              setMetaData(sessionHandler, JMSAdvisor.XID, Xid);                              					if (log.isTraceEnabled()) log.trace("Added Xid to meta-data");            }                        return sd;         }                  else if ("commit".equals(methodName))         {            JMSInvocationHandler handler = ((JMSMethodInvocation)invocation).getHandler();            Boolean transacted = (Boolean)getMetaData(handler, JMSAdvisor.TRANSACTED);            if (!transacted.booleanValue())            {               throw new IllegalStateException("Session is not transacted - cannot call commit()");            }				if (log.isTraceEnabled()) log.trace("commit");				            Object Xid = getMetaData(mi, JMSAdvisor.XID);                                    //SessionDelegate sd = (SessionDelegate)getDelegate(mi);  				ResourceManager rm = (ResourceManager)getHandler(invocation).getParent().getMetaData().getMetaData(JMSAdvisor.JMS, JMSAdvisor.RESOURCE_MANAGER);            Object newXid = rm.commit(Xid);            setMetaData(mi, JMSAdvisor.XID, newXid);   								return null;         }         else if ("rollback".equals(methodName))         {            JMSInvocationHandler handler = ((JMSMethodInvocation)invocation).getHandler();            Boolean transacted = (Boolean)getMetaData(handler, JMSAdvisor.TRANSACTED);            if (!transacted.booleanValue())            {               throw new IllegalStateException("Session is not transacted - cannot call rollback()");            }				if (log.isTraceEnabled()) log.trace("rollback");            Object Xid = mi.getMetaData().getMetaData(JMSAdvisor.JMS, JMSAdvisor.XID);				ResourceManager rm = (ResourceManager)getHandler(invocation).getParent().getMetaData().getMetaData(JMSAdvisor.JMS, JMSAdvisor.RESOURCE_MANAGER);            Object newXid = rm.rollback(Xid);            setMetaData(mi, JMSAdvisor.XID, newXid); 								//Rollback causes redelivery of messages				SessionDelegate sd = (SessionDelegate)getDelegate(mi);				sd.redeliver();				return null;				         }         else if ("send".equals(methodName))         {            if (log.isTraceEnabled()) log.trace("send");             JMSInvocationHandler sessionInvocationHandler = getHandler(invocation).getParent();            if (log.isTraceEnabled()) { log.trace("sessionInvocationHandler: " + sessionInvocationHandler); }            Object Xid = getMetaData(sessionInvocationHandler, JMSAdvisor.XID);            if (Xid != null)            {               //Session is transacted - so we add message to tx instead of sending now               if (log.isTraceEnabled())  log.trace("Session is transacted, storing mesage until commit");               Message m = (Message)mi.getArguments()[1];					ResourceManager rm = (ResourceManager)getHandler(invocation).getParent().getParent().getMetaData().getMetaData(JMSAdvisor.JMS, JMSAdvisor.RESOURCE_MANAGER);					rm.addMessage(Xid, m);               //And we don't invoke any further interceptors in the stack               return null;                           }            else            {               if (log.isTraceEnabled()) { log.trace("Session is not transacted, sending message now"); }            }         }         else if ("acknowledge".equals(methodName))			{				if (log.isTraceEnabled()) log.trace("acknowledge");								Object Xid = mi.getMetaData(JMSAdvisor.JMS, JMSAdvisor.XID);								if (Xid == null)				{					//Not transacted - this goes through to the ServerSessionDelegate					if (log.isTraceEnabled()) { log.trace("Session is not transacted, acking message now"); }				}				else 				{					String messageID = (String)mi.getArguments()[0];					String receiverID = (String)mi.getArguments()[1];										ResourceManager rm = (ResourceManager)getHandler(invocation).getParent().getMetaData().getMetaData(JMSAdvisor.JMS, JMSAdvisor.RESOURCE_MANAGER);										rm.addAck(Xid, new AckInfo(messageID, receiverID));										return null;				}			}                        }                     return invocation.invokeNext();               }