1 package org
.argeo
.slc
.jms
;
3 import java
.util
.Enumeration
;
5 import javax
.jms
.JMSException
;
6 import javax
.jms
.Message
;
7 import javax
.jms
.Session
;
8 import javax
.jms
.TextMessage
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.slc
.SlcException
;
13 import org
.springframework
.jms
.support
.converter
.MessageConversionException
;
14 import org
.springframework
.jms
.support
.converter
.MessageConverter
;
15 import org
.springframework
.oxm
.Marshaller
;
16 import org
.springframework
.oxm
.Unmarshaller
;
17 import org
.springframework
.xml
.transform
.StringResult
;
18 import org
.springframework
.xml
.transform
.StringSource
;
20 public class MarshallerMessageConverter
implements MessageConverter
{
21 private final static Log log
= LogFactory
22 .getLog(MarshallerMessageConverter
.class);
24 private Marshaller marshaller
;
25 private Unmarshaller unmarshaller
;
27 /** @return the converted message or null if the message itself is null */
28 public Object
fromMessage(Message message
) throws JMSException
,
29 MessageConversionException
{
30 if (message
== null) {
34 if (log
.isTraceEnabled()) {
35 Enumeration
<String
> names
= message
.getPropertyNames();
36 while (names
.hasMoreElements()) {
37 String name
= names
.nextElement();
38 log
.trace("JMS Property: " + name
+ "="
39 + message
.getObjectProperty(name
));
43 if (message
instanceof TextMessage
) {
45 String text
= ((TextMessage
) message
).getText();
48 throw new SlcException(
49 "Cannot unmarshall message without body: " + message
);
52 return unmarshaller
.unmarshal(new StringSource(text
));
53 } catch (Exception e
) {
54 throw new SlcException("Could not unmarshall " + text
, e
);
57 throw new SlcException("This type of messages is not supported: "
62 public Message
toMessage(Object object
, Session session
)
63 throws JMSException
, MessageConversionException
{
64 StringResult result
= new StringResult();
66 marshaller
.marshal(object
, result
);
67 } catch (Exception e
) {
68 throw new SlcException("Could not marshall " + object
, e
);
70 TextMessage msg
= session
.createTextMessage();
71 msg
.setText(result
.toString());
75 public void setMarshaller(Marshaller marshaller
) {
76 this.marshaller
= marshaller
;
79 public void setUnmarshaller(Unmarshaller unmarshaller
) {
80 this.unmarshaller
= unmarshaller
;