1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.jms.support.converter;
18
19 import java.io.Serializable;
20 import java.util.Enumeration;
21 import java.util.HashMap;
22 import java.util.Map;
23 import javax.jms.BytesMessage;
24 import javax.jms.JMSException;
25 import javax.jms.MapMessage;
26 import javax.jms.Message;
27 import javax.jms.ObjectMessage;
28 import javax.jms.Session;
29 import javax.jms.TextMessage;
30
31 import org.springframework.util.ObjectUtils;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class SimpleMessageConverter implements MessageConverter {
49
50
51
52
53
54
55
56
57
58
59 @Override
60 public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
61 if (object instanceof Message) {
62 return (Message) object;
63 }
64 else if (object instanceof String) {
65 return createMessageForString((String) object, session);
66 }
67 else if (object instanceof byte[]) {
68 return createMessageForByteArray((byte[]) object, session);
69 }
70 else if (object instanceof Map) {
71 return createMessageForMap((Map<? ,?>) object, session);
72 }
73 else if (object instanceof Serializable) {
74 return createMessageForSerializable(((Serializable) object), session);
75 }
76 else {
77 throw new MessageConversionException("Cannot convert object of type [" +
78 ObjectUtils.nullSafeClassName(object) + "] to JMS message. Supported message " +
79 "payloads are: String, byte array, Map<String,?>, Serializable object.");
80 }
81 }
82
83
84
85
86
87
88
89
90
91
92
93 @Override
94 public Object fromMessage(Message message) throws JMSException, MessageConversionException {
95 if (message instanceof TextMessage) {
96 return extractStringFromMessage((TextMessage) message);
97 }
98 else if (message instanceof BytesMessage) {
99 return extractByteArrayFromMessage((BytesMessage) message);
100 }
101 else if (message instanceof MapMessage) {
102 return extractMapFromMessage((MapMessage) message);
103 }
104 else if (message instanceof ObjectMessage) {
105 return extractSerializableFromMessage((ObjectMessage) message);
106 }
107 else {
108 return message;
109 }
110 }
111
112
113
114
115
116
117
118
119
120
121 protected TextMessage createMessageForString(String text, Session session) throws JMSException {
122 return session.createTextMessage(text);
123 }
124
125
126
127
128
129
130
131
132
133 protected BytesMessage createMessageForByteArray(byte[] bytes, Session session) throws JMSException {
134 BytesMessage message = session.createBytesMessage();
135 message.writeBytes(bytes);
136 return message;
137 }
138
139
140
141
142
143
144
145
146
147 protected MapMessage createMessageForMap(Map<?, ?> map, Session session) throws JMSException {
148 MapMessage message = session.createMapMessage();
149 for (Map.Entry<?, ?> entry : map.entrySet()) {
150 if (!(entry.getKey() instanceof String)) {
151 throw new MessageConversionException("Cannot convert non-String key of type [" +
152 ObjectUtils.nullSafeClassName(entry.getKey()) + "] to JMS MapMessage entry");
153 }
154 message.setObject((String) entry.getKey(), entry.getValue());
155 }
156 return message;
157 }
158
159
160
161
162
163
164
165
166
167 protected ObjectMessage createMessageForSerializable(Serializable object, Session session) throws JMSException {
168 return session.createObjectMessage(object);
169 }
170
171
172
173
174
175
176
177
178 protected String extractStringFromMessage(TextMessage message) throws JMSException {
179 return message.getText();
180 }
181
182
183
184
185
186
187
188 protected byte[] extractByteArrayFromMessage(BytesMessage message) throws JMSException {
189 byte[] bytes = new byte[(int) message.getBodyLength()];
190 message.readBytes(bytes);
191 return bytes;
192 }
193
194
195
196
197
198
199
200 @SuppressWarnings("unchecked")
201 protected Map<String, Object> extractMapFromMessage(MapMessage message) throws JMSException {
202 Map<String, Object> map = new HashMap<String, Object>();
203 Enumeration<String> en = message.getMapNames();
204 while (en.hasMoreElements()) {
205 String key = en.nextElement();
206 map.put(key, message.getObject(key));
207 }
208 return map;
209 }
210
211
212
213
214
215
216
217 protected Serializable extractSerializableFromMessage(ObjectMessage message) throws JMSException {
218 return message.getObject();
219 }
220
221 }