SessionState sessionState = (SessionState)producerState.getParent();
// Generate the message id
ConnectionState connectionState = (ConnectionState)sessionState.getParent();
JBossMessage messageToSend;
boolean foreign = false;
if (!(m instanceof MessageProxy))
{
// it's a foreign message
foreign = true;
// 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)
{
messageToSend = new JBossBytesMessage((BytesMessage)m,0);
}
else if(m instanceof MapMessage)
{
messageToSend = new JBossMapMessage((MapMessage)m,0);
}
else if(m instanceof ObjectMessage)
{
messageToSend = new JBossObjectMessage((ObjectMessage)m,0);
}
else if(m instanceof StreamMessage)
{
messageToSend = new JBossStreamMessage((StreamMessage)m,0);
}
else if(m instanceof TextMessage)
{
messageToSend = new JBossTextMessage((TextMessage)m,0);
}
else
{
messageToSend = new JBossMessage(m, 0);
}
messageToSend.setJMSMessageID(null);
//We must set the destination *after* converting from foreign message
messageToSend.setJMSDestination(destination);
if(connectionState.getRemotingConnection().isStrictTck())
{
m.setJMSDestination(destination);
}
}
else
{
// get the actual message
MessageProxy proxy = (MessageProxy)m;
m.setJMSDestination(destination);
//Get the underlying message
messageToSend = proxy.getMessage();
proxy.beforeSend();
}
// Set the new id
if (!keepID)
{
String id = "ID:JBM-" + UUID.randomUUID().toString();
messageToSend.setJMSMessageID(id);
}
// This only really used for BytesMessages and StreamMessages to reset their state
messageToSend.doBeforeSend();
// now that we know the messageID, set it also on the foreign message, if is the case
if (foreign)
{
m.setJMSMessageID(messageToSend.getJMSMessageID());
}
// we now invoke the send(Message) method on the session, which will eventually be fielded
// by connection endpoint
((SessionDelegate)sessionState.getDelegate()).send(messageToSend, false);