1 package org
.argeo
.slc
.jms
;
3 import javax
.jms
.JMSException
;
4 import javax
.jms
.Message
;
5 import javax
.jms
.Session
;
6 import javax
.jms
.TextMessage
;
8 import org
.argeo
.slc
.SlcException
;
9 import org
.springframework
.jms
.support
.converter
.MessageConversionException
;
10 import org
.springframework
.jms
.support
.converter
.MessageConverter
;
11 import org
.springframework
.oxm
.Marshaller
;
12 import org
.springframework
.oxm
.Unmarshaller
;
13 import org
.springframework
.xml
.transform
.StringResult
;
14 import org
.springframework
.xml
.transform
.StringSource
;
16 public class MarshallerMessageConverter
implements MessageConverter
{
17 private Marshaller marshaller
;
18 private Unmarshaller unmarshaller
;
20 public Object
fromMessage(Message message
) throws JMSException
,
21 MessageConversionException
{
22 if (message
instanceof TextMessage
) {
23 String text
= ((TextMessage
) message
).getText();
25 return unmarshaller
.unmarshal(new StringSource(text
));
26 } catch (Exception e
) {
27 throw new SlcException("Could not unmarshall " + text
, e
);
30 throw new SlcException("Only JMS TextMessage are supported.");
34 public Message
toMessage(Object object
, Session session
)
35 throws JMSException
, MessageConversionException
{
36 StringResult result
= new StringResult();
38 marshaller
.marshal(object
, result
);
39 } catch (Exception e
) {
40 throw new SlcException("Could not marshall " + object
, e
);
42 TextMessage msg
= session
.createTextMessage();
43 msg
.setText(result
.toString());
47 public void setMarshaller(Marshaller marshaller
) {
48 this.marshaller
= marshaller
;
51 public void setUnmarshaller(Unmarshaller unmarshaller
) {
52 this.unmarshaller
= unmarshaller
;