type = MessageType.Text;
else if (body instanceof javax.jms.Message)
type = MessageType.JMSMessage;
else if (body instanceof Serializable)
type = MessageType.Object;
else throw new ControlException("Cannot determine message type from body");
}
switch (type) {
case Object:
checkBody(body, Serializable.class);
m = session.createObjectMessage((Serializable) body);
break;
case Bytes:
BytesMessage sm;
checkBody(body, byte[].class);
m = sm = session.createBytesMessage();
sm.writeBytes((byte[]) body);
break;
case Text:
if (XMLOBJ_CLASS != null && XMLOBJ_CLASS.isAssignableFrom(body.getClass())) {
try {
Method xmlText = XMLOBJ_CLASS.getMethod("xmlText", new Class[]{});
body = xmlText.invoke(body, new Object[]{});
}
catch (NoSuchMethodException e) {
throw new ControlException(e.getMessage(), e);
}
catch (IllegalAccessException e) {
throw new ControlException(e.getMessage(), e);
}
catch (InvocationTargetException e) {
throw new ControlException(e.getMessage(), e);
}
}
checkBody(body, String.class);
m = session.createTextMessage((String) body);
break;
case Map:
MapMessage mm;
checkBody(body, Map.class);
m = mm = session.createMapMessage();
Map map = (Map)body;
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
mm.setObject(key, map.get(key));
}
break;
case JMSMessage:
checkBody(body, javax.jms.Message.class);
m = (javax.jms.Message) body;
break;
}
/*
* Set the correlation id if given.
*/
String correlationProp = props.sendCorrelationProperty();
if (correlationProp != null && correlationProp.length() == 0)
correlationProp = null;
if (correlationId == null)
correlationId = getHeaderAsString(HeaderType.JMSCorrelationID);
Properties jmsProps = (Properties)_context.getMethodPropertySet(method, Properties.class);
if (jmsProps != null && jmsProps.value() != null) {
PropertyValue[] jprops = jmsProps.value();
for (int i = 0; i < jprops.length; i++) {
PropertyValue jprop = jprops[i];
Class cls = jprop.type();
if (cls.equals(String.class))
m.setStringProperty(jprop.name(), jprop.value());
else if (cls.equals(Integer.class))
m.setIntProperty(jprop.name(), valueAsInteger(jprop.value()));
else if (cls.equals(Long.class))
m.setLongProperty(jprop.name(), valueAsLong(jprop.value()));
else
throw new IllegalArgumentException("Invalid type for property-value");
}
}
/*
* Set the properties/headers of the message from
* the parameters to the invoke.
*/
if (hasAnnotation) {
for (int i = 0; i < args.length; i++) {
Property jmsProp = (Property) _context.getParameterPropertySet(method, i, Property.class);
if (jmsProp != null)
m.setObjectProperty(jmsProp.name(), args[i]);
Priority jmsPriority = (Priority) _context.getParameterPropertySet(method, i, Priority.class);
if (jmsPriority != null) {
Integer p = valueAsInteger(args[i]);
if (p != null)
priority = p.intValue();
}
Expiration jmsExpiration = (Expiration) _context.getParameterPropertySet(method, i, Expiration.class);
if (jmsExpiration != null) {
Long e = valueAsLong(args[i]);
if (e != null)
expiration = e.longValue();
}
Delivery jmsDelivery = (Delivery) _context.getParameterPropertySet(method, i, Delivery.class);
if (jmsDelivery != null && args[i] != null) {
deliveryMode = deliveryModeToJmsMode(args[i]);
}
t = (Type) _context.getParameterPropertySet(method, i, Type.class);
if (t != null && args[i] != null)
jmsType = (String) args[i];
CorrelationId jmsId = (CorrelationId)_context.getParameterPropertySet(method, i, CorrelationId.class);
if (jmsId != null && args[i] != null)
correlationId = (String) args[i];
}
}
if (correlationProp != null)
m.setStringProperty(correlationProp, correlationId);
else
m.setJMSCorrelationID(correlationId);
/* Set the headers and properties from maps provided by setProperties() and setHeaders() */
m.setJMSExpiration(expiration);
m.setJMSDeliveryMode(deliveryMode);
m.setJMSPriority(priority);
setMessageHeaders(m);
setMessageProperties(m);
expiration = m.getJMSExpiration();
deliveryMode = m.getJMSDeliveryMode();
priority = m.getJMSPriority();
_headers = null;
_properties = null;
/* Send the message. */
switch (getDestinationType()) {
case Topic:
((TopicPublisher) getProducer()).publish(m, deliveryMode, priority, expiration);
break;
case Queue:
getProducer().send(m, deliveryMode, priority, expiration);
break;
}
}
catch (JMSException e) {
throw new ControlException("Error in sending message", e);
}
return m;
}