]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/MarshallerMessageConverter.java
Improve SystemCall
[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 java.util.Enumeration;
4
5 import javax.jms.JMSException;
6 import javax.jms.Message;
7 import javax.jms.Session;
8 import javax.jms.TextMessage;
9
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;
19
20 public class MarshallerMessageConverter implements MessageConverter {
21 private final static Log log = LogFactory
22 .getLog(MarshallerMessageConverter.class);
23
24 private Marshaller marshaller;
25 private Unmarshaller unmarshaller;
26
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) {
31 return null;
32 }
33
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));
40 }
41 }
42
43 if (message instanceof TextMessage) {
44
45 String text = ((TextMessage) message).getText();
46
47 if (text == null)
48 throw new SlcException(
49 "Cannot unmarshall message without body: " + message);
50
51 try {
52 return unmarshaller.unmarshal(new StringSource(text));
53 } catch (Exception e) {
54 throw new SlcException("Could not unmarshall " + text, e);
55 }
56 } else {
57 throw new SlcException("This type of messages is not supported: "
58 + message);
59 }
60 }
61
62 public Message toMessage(Object object, Session session)
63 throws JMSException, MessageConversionException {
64 StringResult result = new StringResult();
65 try {
66 marshaller.marshal(object, result);
67 } catch (Exception e) {
68 throw new SlcException("Could not marshall " + object, e);
69 }
70 TextMessage msg = session.createTextMessage();
71 msg.setText(result.toString());
72 return msg;
73 }
74
75 public void setMarshaller(Marshaller marshaller) {
76 this.marshaller = marshaller;
77 }
78
79 public void setUnmarshaller(Unmarshaller unmarshaller) {
80 this.unmarshaller = unmarshaller;
81 }
82
83 }