001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jms.support.converter;
018:
019: import java.io.Serializable;
020: import java.util.Enumeration;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import javax.jms.BytesMessage;
026: import javax.jms.JMSException;
027: import javax.jms.MapMessage;
028: import javax.jms.Message;
029: import javax.jms.ObjectMessage;
030: import javax.jms.Session;
031: import javax.jms.TextMessage;
032:
033: import org.springframework.util.ObjectUtils;
034:
035: /**
036: * A simple message converter which is able to handle TextMessages, BytesMessages,
037: * MapMessages, and ObjectMessages. Used as default conversion strategy
038: * by {@link org.springframework.jms.core.JmsTemplate}, for
039: * <code>convertAndSend</code> and <code>receiveAndConvert</code> operations.
040: *
041: * <p>Converts a String to a {@link javax.jms.TextMessage}, a byte array to a
042: * {@link javax.jms.BytesMessage}, a Map to a {@link javax.jms.MapMessage}, and
043: * a Serializable object to a {@link javax.jms.ObjectMessage} (or vice versa).
044: *
045: * <p>This converter implementation works for both JMS 1.1 and JMS 1.0.2,
046: * except when extracting a byte array from a BytesMessage. So for converting
047: * BytesMessages with a JMS 1.0.2 provider, use {@link SimpleMessageConverter102}.
048: * (As you would expect, {@link org.springframework.jms.core.JmsTemplate102}
049: * uses SimpleMessageConverter102 as default.)
050: *
051: * @author Juergen Hoeller
052: * @since 1.1
053: * @see org.springframework.jms.core.JmsTemplate#convertAndSend
054: * @see org.springframework.jms.core.JmsTemplate#receiveAndConvert
055: * @see SimpleMessageConverter102
056: */
057: public class SimpleMessageConverter implements MessageConverter {
058:
059: /**
060: * This implementation creates a TextMessage for a String, a
061: * BytesMessage for a byte array, a MapMessage for a Map,
062: * and an ObjectMessage for a Serializable object.
063: * @see #createMessageForString
064: * @see #createMessageForByteArray
065: * @see #createMessageForMap
066: * @see #createMessageForSerializable
067: */
068: public Message toMessage(Object object, Session session)
069: throws JMSException, MessageConversionException {
070: if (object instanceof Message) {
071: return (Message) object;
072: } else if (object instanceof String) {
073: return createMessageForString((String) object, session);
074: } else if (object instanceof byte[]) {
075: return createMessageForByteArray((byte[]) object, session);
076: } else if (object instanceof Map) {
077: return createMessageForMap((Map) object, session);
078: } else if (object instanceof Serializable) {
079: return createMessageForSerializable(
080: ((Serializable) object), session);
081: } else {
082: throw new MessageConversionException(
083: "Cannot convert object [" + object
084: + "] to JMS message");
085: }
086: }
087:
088: /**
089: * This implementation converts a TextMessage back to a String, a
090: * ByteMessage back to a byte array, a MapMessage back to a Map,
091: * and an ObjectMessage back to a Serializable object. Returns
092: * the plain Message object in case of an unknown message type.
093: * @see #extractStringFromMessage
094: * @see #extractByteArrayFromMessage
095: * @see #extractMapFromMessage
096: * @see #extractSerializableFromMessage
097: */
098: public Object fromMessage(Message message) throws JMSException,
099: MessageConversionException {
100: if (message instanceof TextMessage) {
101: return extractStringFromMessage((TextMessage) message);
102: } else if (message instanceof BytesMessage) {
103: return extractByteArrayFromMessage((BytesMessage) message);
104: } else if (message instanceof MapMessage) {
105: return extractMapFromMessage((MapMessage) message);
106: } else if (message instanceof ObjectMessage) {
107: return extractSerializableFromMessage((ObjectMessage) message);
108: } else {
109: return message;
110: }
111: }
112:
113: /**
114: * Create a JMS TextMessage for the given String.
115: * @param text the String to convert
116: * @param session current JMS session
117: * @return the resulting message
118: * @throws JMSException if thrown by JMS methods
119: * @see javax.jms.Session#createTextMessage
120: */
121: protected TextMessage createMessageForString(String text,
122: Session session) throws JMSException {
123: return session.createTextMessage(text);
124: }
125:
126: /**
127: * Create a JMS BytesMessage for the given byte array.
128: * @param bytes the byyte array to convert
129: * @param session current JMS session
130: * @return the resulting message
131: * @throws JMSException if thrown by JMS methods
132: * @see javax.jms.Session#createBytesMessage
133: */
134: protected BytesMessage createMessageForByteArray(byte[] bytes,
135: Session session) throws JMSException {
136: BytesMessage message = session.createBytesMessage();
137: message.writeBytes(bytes);
138: return message;
139: }
140:
141: /**
142: * Create a JMS MapMessage for the given Map.
143: * @param map the Map to convert
144: * @param session current JMS session
145: * @return the resulting message
146: * @throws JMSException if thrown by JMS methods
147: * @see javax.jms.Session#createMapMessage
148: */
149: protected MapMessage createMessageForMap(Map map, Session session)
150: throws JMSException {
151: MapMessage message = session.createMapMessage();
152: for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
153: Map.Entry entry = (Map.Entry) it.next();
154: if (!(entry.getKey() instanceof String)) {
155: throw new MessageConversionException(
156: "Cannot convert non-String key of type ["
157: + ObjectUtils.nullSafeClassName(entry
158: .getKey())
159: + "] to JMS MapMessage entry");
160: }
161: message
162: .setObject((String) entry.getKey(), entry
163: .getValue());
164: }
165: return message;
166: }
167:
168: /**
169: * Create a JMS ObjectMessage for the given Serializable object.
170: * @param object the Serializable object to convert
171: * @param session current JMS session
172: * @return the resulting message
173: * @throws JMSException if thrown by JMS methods
174: * @see javax.jms.Session#createObjectMessage
175: */
176: protected ObjectMessage createMessageForSerializable(
177: Serializable object, Session session) throws JMSException {
178: return session.createObjectMessage(object);
179: }
180:
181: /**
182: * Extract a String from the given TextMessage.
183: * @param message the message to convert
184: * @return the resulting String
185: * @throws JMSException if thrown by JMS methods
186: */
187: protected String extractStringFromMessage(TextMessage message)
188: throws JMSException {
189: return message.getText();
190: }
191:
192: /**
193: * Extract a byte array from the given {@link BytesMessage}.
194: * @param message the message to convert
195: * @return the resulting byte array
196: * @throws JMSException if thrown by JMS methods
197: */
198: protected byte[] extractByteArrayFromMessage(BytesMessage message)
199: throws JMSException {
200: byte[] bytes = new byte[(int) message.getBodyLength()];
201: message.readBytes(bytes);
202: return bytes;
203: }
204:
205: /**
206: * Extract a Map from the given {@link MapMessage}.
207: * @param message the message to convert
208: * @return the resulting Map
209: * @throws JMSException if thrown by JMS methods
210: */
211: protected Map extractMapFromMessage(MapMessage message)
212: throws JMSException {
213: Map map = new HashMap();
214: Enumeration en = message.getMapNames();
215: while (en.hasMoreElements()) {
216: String key = (String) en.nextElement();
217: map.put(key, message.getObject(key));
218: }
219: return map;
220: }
221:
222: /**
223: * Extract a Serializable object from the given {@link ObjectMessage}.
224: * @param message the message to convert
225: * @return the resulting Serializable object
226: * @throws JMSException if thrown by JMS methods
227: */
228: protected Serializable extractSerializableFromMessage(
229: ObjectMessage message) throws JMSException {
230: return message.getObject();
231: }
232:
233: }
|