boolean keepID = args.length>5? ((Boolean)args[5]).booleanValue() : false;
// configure the message for sending, using attributes stored as metadata
ProducerState producerState = getProducerState(mi);
if (deliveryMode == -1)
{
// Use the delivery mode of the producer
deliveryMode = producerState.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 = producerState.getPriority();
if (trace) { log.trace("Using producer's default priority: " + priority); }
}
if (priority < 0 || priority > 9)
{
throw new MessageFormatException("Invalid message priority (" + priority + "). " +
"Valid priorities are 0-9");
}
m.setJMSPriority(priority);
if (producerState.isDisableMessageTimestamp())
{
m.setJMSTimestamp(0l);
}
else
{
m.setJMSTimestamp(System.currentTimeMillis());
}
if (timeToLive == Long.MIN_VALUE)
{
// Use time to live value from producer
timeToLive = producerState.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 = producerState.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
if (producerState.getDestination() != null &&
!producerState.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");
}
}
// destination should aways be a JBossDestination as this is tested on the MessageProducer creation
// and on the send, so it should be safe to just do this cast
if (((JBossDestination)destination).isTemporary())
{
validateTemporaryDestination(destination);
}
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());
}
// Processing the ordering group message here.
if (producerState.isOrderingGroupEnabled())
{
String grpName = producerState.getOrderingGroupName();
messageToSend.setJBMOrderingGroupName(grpName);
}
// we now invoke the send(Message) method on the session, which will eventually be fielded
// by connection endpoint