if (log.isTraceEnabled()) { log.trace("predeliver"); }
   public Object invoke(Invocation invocation) throws Throwable   {      if (!(invocation instanceof MethodInvocation))      {         return invocation.invokeNext();      }            MethodInvocation mi = (MethodInvocation)invocation;      String methodName = mi.getMethod().getName();            if (log.isTraceEnabled()) { log.trace("In TransactionInterceptor: method is " + methodName); }            if ("createSessionDelegate".equals(methodName))      {         if (log.isTraceEnabled()) { log.trace("createSessionDelegate"); }                  SessionDelegate sd = (SessionDelegate)invocation.invokeNext();                  boolean transacted = ((Boolean)mi.getArguments()[0]).booleanValue();         boolean isXA = ((Boolean)mi.getArguments()[2]).booleanValue();                  if (log.isTraceEnabled())         {            log.trace("transacted:" + transacted + ", xa:" + isXA);         }                  sd.setTransacted(transacted);         sd.setXA(isXA);         if (transacted)         {                        ResourceManager theRm = ((ConnectionDelegate)this.getDelegate(mi)).getResourceManager();                        if (log.isTraceEnabled()) { log.trace("Transacted so creating XAResource"); }                        JBossXAResource theResource = new JBossXAResource(theRm, sd);                                    //Create a local tx                     										            Object Xid = theRm.createLocalTx();                        if (log.isTraceEnabled()) { log.trace("Created local tx"); }                        theResource.setCurrentTxID(Xid);                         sd.setXAResource(theResource);            sd.setResourceManager(theRm);         }                     return sd;      }               else if ("commit".equals(methodName))      {         if (log.isTraceEnabled()) { log.trace("commit"); }                  //SessionDelegate sessDelegate = (SessionDelegate)this.getDelegate(mi);                  if (!this.transacted)         {            throw new IllegalStateException("Cannot commit a non-transacted session");         }         if (this.XA)         {            throw new TransactionInProgressException("Cannot call commit on an XA session");         }                  try         {            rm.commitLocal((LocalTxXid)xaResource.getCurrentTxID());         }         finally         {            //Start new local tx            Object xid = rm.createLocalTx();            xaResource.setCurrentTxID(xid);         }                  return null;      }      else if ("rollback".equals(methodName))      {         if (log.isTraceEnabled()) { log.trace("rollback"); }                  SessionDelegate sessDelegate = (SessionDelegate)this.getDelegate(mi);         if (!this.transacted)         {            throw new IllegalStateException("Cannot rollback a non-transacted session");         }         if (this.XA)         {            throw new TransactionInProgressException("Cannot call rollback on an XA session");         }                  try         {            rm.rollbackLocal((LocalTxXid)xaResource.getCurrentTxID());         }         finally         {            //Start new local tx            Object xid = rm.createLocalTx();            xaResource.setCurrentTxID(xid);         }                   //Rollback causes redelivery of messages         String asfReceiverID = sessDelegate.getAsfReceiverID();         if (log.isTraceEnabled()) { log.trace("asfReceiverID is:" + asfReceiverID); }         if (log.isTraceEnabled()) { log.trace("Calling sessiondelegate.redeliver()"); }         sessDelegate.redeliver(asfReceiverID);         return null;				      }      else if ("send".equals(methodName))      {         if (log.isTraceEnabled()) { log.trace("send"); }                  JMSInvocationHandler handler = getHandler(invocation).getParent();                  SessionDelegate sessDelegate = (SessionDelegate)handler.getDelegate();                  JBossXAResource jbXAResource = (JBossXAResource)sessDelegate.getXAResource();                  ResourceManager theRM = sessDelegate.getResourceManager();                  if (log.isTraceEnabled()) { log.trace("XAResource is: " + jbXAResource); }                  if (sessDelegate.getTransacted())         {            //Session is transacted - so we add message to tx instead of sending now                        Object txID = jbXAResource.getCurrentTxID();                        if (txID == null)            {               boolean isXA = sessDelegate.getXA();               throw new IllegalStateException("Attempt to send message in tx, but txId is null, XA?" + isXA);            }                        Message m = (Message)mi.getArguments()[1];			            theRM.addMessage(txID, 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 ("preDeliver".equals(methodName))      {         if (log.isTraceEnabled()) { log.trace("predeliver"); }                  //SessionDelegate sessDelegate = (SessionDelegate)this.getDelegate(mi);         if (!this.transacted)         {            //Not transacted - do nothing - ack will happen when delivered() is called            if (log.isTraceEnabled()) { log.trace("Session is not transacted"); }            return null;         }         else          {            String messageID = (String)mi.getArguments()[0];            String receiverID = (String)mi.getArguments()[1];                        if (log.isTraceEnabled())            {               log.trace("XAresource: " + xaResource);            }                        Object txID = xaResource.getCurrentTxID();                        if (txID == null)            {               throw new IllegalStateException("Attempt to send message in tx, but txId is null, XA?" + this.XA);            }                        rm.addAck(txID, new AckInfo(messageID, receiverID));                        return null;         }      }        else if ("getXAResource".equals(methodName))      {         if (log.isTraceEnabled()) { log.trace("getXAResource"); }                  return xaResource;      }      else if ("setXAResource".equals(methodName))      {         if (log.isTraceEnabled()) { log.trace("setXAResource"); }         JBossXAResource resource = (JBossXAResource)mi.getArguments()[0];         this.xaResource = resource;         return null;      }      else if ("getResourceManager".equals(methodName))      {         if (log.isTraceEnabled()) { log.trace("getResourceManager"); }                  return this.rm;      }      else if ("setResourceManager".equals(methodName))      {         if (log.isTraceEnabled()) { log.trace("setResourceManager"); }         ResourceManager theRM = (ResourceManager)mi.getArguments()[0];         this.rm = theRM;         return null;      }      else if ("getTransacted".equals(methodName))      {         //SessionState state = getSessionState(mi);         return new Boolean(transacted);      }      else if ("getXA".equals(methodName))      {         //SessionState state = getSessionState(mi);         return new Boolean(XA);      }      else if ("setTransacted".equals(methodName))      {         this.transacted = ((Boolean)mi.getArguments()[0]).booleanValue();         return null;      }      else if ("setXA".equals(methodName))      {         this.XA = ((Boolean)mi.getArguments()[0]).booleanValue();         return null;      }                  return invocation.invokeNext();               }