]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/MarshallerMessageConverter.java
Introduce remote launching of agents
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.activemq / src / main / java / org / argeo / slc / jms / MarshallerMessageConverter.java
1 package org.argeo.slc.jms;
2
3 import javax.jms.JMSException;
4 import javax.jms.Message;
5 import javax.jms.Session;
6 import javax.jms.TextMessage;
7
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;
15
16 public class MarshallerMessageConverter implements MessageConverter {
17 private Marshaller marshaller;
18 private Unmarshaller unmarshaller;
19
20 public Object fromMessage(Message message) throws JMSException,
21 MessageConversionException {
22 if (message instanceof TextMessage) {
23 String text = ((TextMessage) message).getText();
24 try {
25 return unmarshaller.unmarshal(new StringSource(text));
26 } catch (Exception e) {
27 throw new SlcException("Could not unmarshall " + text, e);
28 }
29 } else {
30 throw new SlcException("Only JMS TextMessage are supported.");
31 }
32 }
33
34 public Message toMessage(Object object, Session session)
35 throws JMSException, MessageConversionException {
36 StringResult result = new StringResult();
37 try {
38 marshaller.marshal(object, result);
39 } catch (Exception e) {
40 throw new SlcException("Could not marshall " + object, e);
41 }
42 TextMessage msg = session.createTextMessage();
43 msg.setText(result.toString());
44 return msg;
45 }
46
47 public void setMarshaller(Marshaller marshaller) {
48 this.marshaller = marshaller;
49 }
50
51 public void setUnmarshaller(Unmarshaller unmarshaller) {
52 this.unmarshaller = unmarshaller;
53 }
54
55 }