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 public Object
fromMessage(Message message
) throws JMSException
,
28 MessageConversionException
{
29 if (log
.isTraceEnabled()) {
30 Enumeration
<String
> names
= message
.getPropertyNames();
31 while (names
.hasMoreElements()) {
32 String name
= names
.nextElement();
33 log
.trace("JMS Property: " + name
+ "="
34 + message
.getObjectProperty(name
));
38 if (message
instanceof TextMessage
) {
40 String text
= ((TextMessage
) message
).getText();
42 return unmarshaller
.unmarshal(new StringSource(text
));
43 } catch (Exception e
) {
44 throw new SlcException("Could not unmarshall " + text
, e
);
47 throw new SlcException("This type of messages is not supported: "
52 public Message
toMessage(Object object
, Session session
)
53 throws JMSException
, MessageConversionException
{
54 StringResult result
= new StringResult();
56 marshaller
.marshal(object
, result
);
57 } catch (Exception e
) {
58 throw new SlcException("Could not marshall " + object
, e
);
60 TextMessage msg
= session
.createTextMessage();
61 msg
.setText(result
.toString());
65 public void setMarshaller(Marshaller marshaller
) {
66 this.marshaller
= marshaller
;
69 public void setUnmarshaller(Unmarshaller unmarshaller
) {
70 this.unmarshaller
= unmarshaller
;