foreign = true;
   public Object handleSend(Invocation invocation) throws Throwable   {       MethodInvocation mi = (MethodInvocation)invocation;            Object[] args = mi.getArguments();            Destination destination = (Destination)args[0];      Message m = (Message)args[1];      int deliveryMode = ((Integer)args[2]).intValue();      int priority = ((Integer)args[3]).intValue();      long timeToLive = ((Long)args[4]).longValue();      // configure the message for sending, using attributes stored as metadata      ProducerState theState = getProducerState(mi);      if (deliveryMode == -1)      {         //Use the delivery mode of the producer         deliveryMode = theState.getDeliveryMode();         if (trace) { log.trace("Using producer's default delivery mode: " + deliveryMode); }      }      m.setJMSDeliveryMode(deliveryMode);      if (priority == -1)      {         //Use the priority of the producer         priority = theState.getPriority();         if (trace) { log.trace("Using producer's default priority: " + priority); }      }      m.setJMSPriority(priority);      if (theState.isDisableMessageTimestamp())      {         m.setJMSTimestamp(0l);      }      else      {         m.setJMSTimestamp(System.currentTimeMillis());      }      if (timeToLive == Long.MIN_VALUE)      {         //Use time to live value from producer         timeToLive = theState.getTimeToLive();         if (trace) { log.trace("Using producer's default timeToLive: " + timeToLive); }      }      if (timeToLive == 0)      {         //Zero implies never expires         m.setJMSExpiration(0);      }      else      {         m.setJMSExpiration(System.currentTimeMillis() + timeToLive);      }      if (destination == null)      {         //Use destination from producer         destination = theState.getDestination();                  if (destination == null)         {            throw new UnsupportedOperationException("Destination not specified");         }         if (trace) { log.trace("Using producer's default destination: " + destination); }      }      else      {         //If a default destination was already specified then this must be same destination as that         //specified in the arguments         //Destination defaultDestination = state.destination;                  if (theState.getDestination() != null && !theState.getDestination().equals(destination))         {            throw new UnsupportedOperationException("Where a default destination is specified for the sender " +                                                    "and a destination is specified in the arguments to the " +                                                    "send, these destinations must be equal");                        }      }      m.setJMSDestination(destination);            JBossMessage toSend;      if (!(m instanceof MessageProxy))      {         //It's a foreign message                  // JMS 1.1 Sect. 3.11.4: A provider must be prepared to accept, from a client,         // a message whose implementation is not one of its own.         // Create a matching JBossMessage Type from JMS Type         if(m instanceof BytesMessage)         {            toSend = new JBossBytesMessage((BytesMessage)m,0);         }         else if(m instanceof MapMessage)         {            toSend = new JBossMapMessage((MapMessage)m,0);         }         else if(m instanceof ObjectMessage)         {            toSend = new JBossObjectMessage((ObjectMessage)m,0);         }         else if(m instanceof StreamMessage)         {            toSend = new JBossStreamMessage((StreamMessage)m,0);         }         else if(m instanceof TextMessage)         {            toSend = new JBossTextMessage((TextMessage)m,0);         }         else         {            toSend = new JBossMessage(m, 0);         }         toSend.doAfterSend();      }      else      {         //Get the actual message         MessageProxy del = (MessageProxy)m;         toSend = del.getMessage();         toSend.doAfterSend();         del.setSent();      }            //Set the id            ConnectionState cState = getConnectionState(invocation);            long id = cState.getIdGenerator().getId();            toSend.setJMSMessageID(null);      toSend.setMessageId(id);            SessionState sState = getSessionState(invocation);                    // We now invoke the send(Message) method on the connection      ((SessionDelegate)sState.getDelegate()).send(toSend);            return null;   }