KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > support > converter > SimpleMessageConverter


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jms.support.converter;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.jms.BytesMessage JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.MapMessage JavaDoc;
28 import javax.jms.Message JavaDoc;
29 import javax.jms.ObjectMessage JavaDoc;
30 import javax.jms.Session JavaDoc;
31 import javax.jms.TextMessage JavaDoc;
32
33 import org.springframework.util.ObjectUtils;
34
35 /**
36  * A simple message converter which is able to handle TextMessages, BytesMessages,
37  * MapMessages, and ObjectMessages. Used as default conversion strategy
38  * by {@link org.springframework.jms.core.JmsTemplate}, for
39  * <code>convertAndSend</code> and <code>receiveAndConvert</code> operations.
40  *
41  * <p>Converts a String to a {@link javax.jms.TextMessage}, a byte array to a
42  * {@link javax.jms.BytesMessage}, a Map to a {@link javax.jms.MapMessage}, and
43  * a Serializable object to a {@link javax.jms.ObjectMessage} (or vice versa).
44  *
45  * <p>This converter implementation works for both JMS 1.1 and JMS 1.0.2,
46  * except when extracting a byte array from a BytesMessage. So for converting
47  * BytesMessages with a JMS 1.0.2 provider, use {@link SimpleMessageConverter102}.
48  * (As you would expect, {@link org.springframework.jms.core.JmsTemplate102}
49  * uses SimpleMessageConverter102 as default.)
50  *
51  * @author Juergen Hoeller
52  * @since 1.1
53  * @see org.springframework.jms.core.JmsTemplate#convertAndSend
54  * @see org.springframework.jms.core.JmsTemplate#receiveAndConvert
55  * @see SimpleMessageConverter102
56  */

57 public class SimpleMessageConverter implements MessageConverter {
58
59     /**
60      * This implementation creates a TextMessage for a String, a
61      * BytesMessage for a byte array, a MapMessage for a Map,
62      * and an ObjectMessage for a Serializable object.
63      * @see #createMessageForString
64      * @see #createMessageForByteArray
65      * @see #createMessageForMap
66      * @see #createMessageForSerializable
67      */

68     public Message JavaDoc toMessage(Object JavaDoc object, Session JavaDoc session) throws JMSException JavaDoc, MessageConversionException {
69         if (object instanceof Message JavaDoc) {
70             return (Message JavaDoc) object;
71         }
72         else if (object instanceof String JavaDoc) {
73             return createMessageForString((String JavaDoc) object, session);
74         }
75         else if (object instanceof byte[]) {
76             return createMessageForByteArray((byte[]) object, session);
77         }
78         else if (object instanceof Map JavaDoc) {
79             return createMessageForMap((Map JavaDoc) object, session);
80         }
81         else if (object instanceof Serializable JavaDoc) {
82             return createMessageForSerializable(((Serializable JavaDoc) object), session);
83         }
84         else {
85             throw new MessageConversionException("Cannot convert object [" + object + "] to JMS message");
86         }
87     }
88
89     /**
90      * This implementation converts a TextMessage back to a String, a
91      * ByteMessage back to a byte array, a MapMessage back to a Map,
92      * and an ObjectMessage back to a Serializable object. Returns
93      * the plain Message object in case of an unknown message type.
94      * @see #extractStringFromMessage
95      * @see #extractByteArrayFromMessage
96      * @see #extractMapFromMessage
97      * @see #extractSerializableFromMessage
98      */

99     public Object JavaDoc fromMessage(Message JavaDoc message) throws JMSException JavaDoc, MessageConversionException {
100         if (message instanceof TextMessage JavaDoc) {
101             return extractStringFromMessage((TextMessage JavaDoc) message);
102         }
103         else if (message instanceof BytesMessage JavaDoc) {
104             return extractByteArrayFromMessage((BytesMessage JavaDoc) message);
105         }
106         else if (message instanceof MapMessage JavaDoc) {
107             return extractMapFromMessage((MapMessage JavaDoc) message);
108         }
109         else if (message instanceof ObjectMessage JavaDoc) {
110             return extractSerializableFromMessage((ObjectMessage JavaDoc) message);
111         }
112         else {
113             return message;
114         }
115     }
116
117
118     /**
119      * Create a JMS TextMessage for the given String.
120      * @param text the String to convert
121      * @param session current JMS session
122      * @return the resulting message
123      * @throws JMSException if thrown by JMS methods
124      * @see javax.jms.Session#createTextMessage
125      */

126     protected TextMessage JavaDoc createMessageForString(String JavaDoc text, Session JavaDoc session) throws JMSException JavaDoc {
127         return session.createTextMessage(text);
128     }
129
130     /**
131      * Create a JMS BytesMessage for the given byte array.
132      * @param bytes the byyte array to convert
133      * @param session current JMS session
134      * @return the resulting message
135      * @throws JMSException if thrown by JMS methods
136      * @see javax.jms.Session#createBytesMessage
137      */

138     protected BytesMessage JavaDoc createMessageForByteArray(byte[] bytes, Session JavaDoc session) throws JMSException JavaDoc {
139         BytesMessage JavaDoc message = session.createBytesMessage();
140         message.writeBytes(bytes);
141         return message;
142     }
143
144     /**
145      * Create a JMS MapMessage for the given Map.
146      * @param map the Map to convert
147      * @param session current JMS session
148      * @return the resulting message
149      * @throws JMSException if thrown by JMS methods
150      * @see javax.jms.Session#createMapMessage
151      */

152     protected MapMessage JavaDoc createMessageForMap(Map JavaDoc map, Session JavaDoc session) throws JMSException JavaDoc {
153         MapMessage JavaDoc message = session.createMapMessage();
154         for (Iterator JavaDoc it = map.entrySet().iterator(); it.hasNext();) {
155             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
156             if (!(entry.getKey() instanceof String JavaDoc)) {
157                 throw new MessageConversionException("Cannot convert non-String key of type [" +
158                         ObjectUtils.nullSafeClassName(entry.getKey()) + "] to JMS MapMessage entry");
159             }
160             message.setObject((String JavaDoc) entry.getKey(), entry.getValue());
161         }
162         return message;
163     }
164
165     /**
166      * Create a JMS ObjectMessage for the given Serializable object.
167      * @param object the Serializable object to convert
168      * @param session current JMS session
169      * @return the resulting message
170      * @throws JMSException if thrown by JMS methods
171      * @see javax.jms.Session#createObjectMessage
172      */

173     protected ObjectMessage JavaDoc createMessageForSerializable(Serializable JavaDoc object, Session JavaDoc session) throws JMSException JavaDoc {
174         return session.createObjectMessage(object);
175     }
176
177
178     /**
179      * Extract a String from the given TextMessage.
180      * @param message the message to convert
181      * @return the resulting String
182      * @throws JMSException if thrown by JMS methods
183      */

184     protected String JavaDoc extractStringFromMessage(TextMessage JavaDoc message) throws JMSException JavaDoc {
185         return message.getText();
186     }
187
188     /**
189      * Extract a byte array from the given {@link BytesMessage}.
190      * @param message the message to convert
191      * @return the resulting byte array
192      * @throws JMSException if thrown by JMS methods
193      */

194     protected byte[] extractByteArrayFromMessage(BytesMessage JavaDoc message) throws JMSException JavaDoc {
195         byte[] bytes = new byte[(int) message.getBodyLength()];
196         message.readBytes(bytes);
197         return bytes;
198     }
199
200     /**
201      * Extract a Map from the given {@link MapMessage}.
202      * @param message the message to convert
203      * @return the resulting Map
204      * @throws JMSException if thrown by JMS methods
205      */

206     protected Map JavaDoc extractMapFromMessage(MapMessage JavaDoc message) throws JMSException JavaDoc {
207         Map JavaDoc map = new HashMap JavaDoc();
208         Enumeration JavaDoc en = message.getMapNames();
209         while (en.hasMoreElements()) {
210             String JavaDoc key = (String JavaDoc) en.nextElement();
211             map.put(key, message.getObject(key));
212         }
213         return map;
214     }
215
216     /**
217      * Extract a Serializable object from the given {@link ObjectMessage}.
218      * @param message the message to convert
219      * @return the resulting Serializable object
220      * @throws JMSException if thrown by JMS methods
221      */

222     protected Serializable JavaDoc extractSerializableFromMessage(ObjectMessage JavaDoc message) throws JMSException JavaDoc {
223         return message.getObject();
224     }
225
226 }
227
Popular Tags